xref: /openbsd/usr.sbin/dvmrpd/log.c (revision cca36db2)
1 /*	$OpenBSD: log.c,v 1.3 2011/08/20 19:02:28 sthen Exp $ */
2 
3 /*
4  * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
5  *
6  * Permission to use, copy, modify, and distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <errno.h>
20 #include <stdarg.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <syslog.h>
25 #include <unistd.h>
26 
27 #include "igmp.h"
28 #include "dvmrpd.h"
29 #include "log.h"
30 
31 static const char * const procnames[] = {
32 	"parent",
33 	"dvmrpe",
34 	"rde"
35 };
36 
37 int	debug;
38 int	verbose;
39 
40 void	 logit(int, const char *, ...);
41 
42 void
43 log_init(int n_debug)
44 {
45 	extern char	*__progname;
46 
47 	debug = n_debug;
48 
49 	if (!debug)
50 		openlog(__progname, LOG_PID | LOG_NDELAY, LOG_DAEMON);
51 
52 	tzset();
53 }
54 
55 void
56 log_verbose(int v)
57 {
58 	verbose = v;
59 }
60 
61 void
62 logit(int pri, const char *fmt, ...)
63 {
64 	va_list	ap;
65 
66 	va_start(ap, fmt);
67 	vlog(pri, fmt, ap);
68 	va_end(ap);
69 }
70 
71 void
72 vlog(int pri, const char *fmt, va_list ap)
73 {
74 	char	*nfmt;
75 
76 	if (debug) {
77 		/* best effort in out of mem situations */
78 		if (asprintf(&nfmt, "%s\n", fmt) == -1) {
79 			vfprintf(stderr, fmt, ap);
80 			fprintf(stderr, "\n");
81 		} else {
82 			vfprintf(stderr, nfmt, ap);
83 			free(nfmt);
84 		}
85 		fflush(stderr);
86 	} else
87 		vsyslog(pri, fmt, ap);
88 }
89 
90 void
91 log_warn(const char *emsg, ...)
92 {
93 	char	*nfmt;
94 	va_list	 ap;
95 
96 	/* best effort to even work in out of memory situations */
97 	if (emsg == NULL)
98 		logit(LOG_CRIT, "%s", strerror(errno));
99 	else {
100 		va_start(ap, emsg);
101 
102 		if (asprintf(&nfmt, "%s: %s", emsg, strerror(errno)) == -1) {
103 			/* we tried it... */
104 			vlog(LOG_CRIT, emsg, ap);
105 			logit(LOG_CRIT, "%s", strerror(errno));
106 		} else {
107 			vlog(LOG_CRIT, nfmt, ap);
108 			free(nfmt);
109 		}
110 		va_end(ap);
111 	}
112 }
113 
114 void
115 log_warnx(const char *emsg, ...)
116 {
117 	va_list	 ap;
118 
119 	va_start(ap, emsg);
120 	vlog(LOG_CRIT, emsg, ap);
121 	va_end(ap);
122 }
123 
124 void
125 log_info(const char *emsg, ...)
126 {
127 	va_list	 ap;
128 
129 	va_start(ap, emsg);
130 	vlog(LOG_INFO, emsg, ap);
131 	va_end(ap);
132 }
133 
134 void
135 log_debug(const char *emsg, ...)
136 {
137 	va_list	 ap;
138 
139 	if (verbose) {
140 		va_start(ap, emsg);
141 		vlog(LOG_DEBUG, emsg, ap);
142 		va_end(ap);
143 	}
144 }
145 
146 void
147 fatal(const char *emsg)
148 {
149 	if (emsg == NULL)
150 		logit(LOG_CRIT, "fatal in %s: %s", procnames[dvmrpd_process],
151 		    strerror(errno));
152 	else
153 		if (errno)
154 			logit(LOG_CRIT, "fatal in %s: %s: %s",
155 			    procnames[dvmrpd_process], emsg, strerror(errno));
156 		else
157 			logit(LOG_CRIT, "fatal in %s: %s",
158 			    procnames[dvmrpd_process], emsg);
159 
160 	if (dvmrpd_process == PROC_MAIN)
161 		exit(1);
162 	else				/* parent copes via SIGCHLD */
163 		_exit(1);
164 }
165 
166 void
167 fatalx(const char *emsg)
168 {
169 	errno = 0;
170 	fatal(emsg);
171 }
172 
173 /* names */
174 const char *
175 nbr_state_name(int state)
176 {
177 	switch (state) {
178 	case NBR_STA_DOWN:
179 		return ("DOWN");
180 	case NBR_STA_1_WAY:
181 		return ("1-WAY");
182 	case NBR_STA_2_WAY:
183 		return ("2-WAY");
184 	default:
185 		return ("UNKNOWN");
186 	}
187 }
188 
189 const char *
190 if_state_name(int state)
191 {
192 	switch (state) {
193 	case IF_STA_DOWN:
194 		return ("DOWN");
195 	case IF_STA_QUERIER:
196 		return ("QUERIER");
197 	case IF_STA_NONQUERIER:
198 		return ("NONQUERIER");
199 	default:
200 		return ("UNKNOWN");
201 	}
202 }
203 
204 const char *
205 group_state_name(int state)
206 {
207 	switch (state) {
208 	case GRP_STA_NO_MEMB_PRSNT:
209 		return ("NO MEMBER");
210 	case GRP_STA_MEMB_PRSNT:
211 		return ("MEMBER");
212 	case GRP_STA_V1_MEMB_PRSNT:
213 		return ("V1 MEMBER");
214 	case GRP_STA_CHECK_MEMB:
215 		return ("CHECKING");
216 	default:
217 		return ("UNKNOWN");
218 	}
219 }
220 
221 const char *
222 if_type_name(enum iface_type type)
223 {
224 	switch (type) {
225 	case IF_TYPE_POINTOPOINT:
226 		return ("POINTOPOINT");
227 	case IF_TYPE_BROADCAST:
228 		return ("BROADCAST");
229 	}
230 	/* NOTREACHED */
231 	return ("UNKNOWN");
232 }
233