xref: /openbsd-src/usr.sbin/dvmrpd/log.c (revision 43003dfe3ad45d1698bed8a37f2b0f5b14f20d4f)
1 /*	$OpenBSD: log.c,v 1.1 2006/06/01 14:12:20 norby 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 
39 void	 logit(int, const char *, ...);
40 
41 void
42 log_init(int n_debug)
43 {
44 	extern char	*__progname;
45 
46 	debug = n_debug;
47 
48 	if (!debug)
49 		openlog(__progname, LOG_PID | LOG_NDELAY, LOG_DAEMON);
50 
51 	tzset();
52 }
53 
54 void
55 logit(int pri, const char *fmt, ...)
56 {
57 	va_list	ap;
58 
59 	va_start(ap, fmt);
60 	vlog(pri, fmt, ap);
61 	va_end(ap);
62 }
63 
64 void
65 vlog(int pri, const char *fmt, va_list ap)
66 {
67 	char	*nfmt;
68 
69 	if (debug) {
70 		/* best effort in out of mem situations */
71 		if (asprintf(&nfmt, "%s\n", fmt) == -1) {
72 			vfprintf(stderr, fmt, ap);
73 			fprintf(stderr, "\n");
74 		} else {
75 			vfprintf(stderr, nfmt, ap);
76 			free(nfmt);
77 		}
78 		fflush(stderr);
79 	} else
80 		vsyslog(pri, fmt, ap);
81 }
82 
83 void
84 log_warn(const char *emsg, ...)
85 {
86 	char	*nfmt;
87 	va_list	 ap;
88 
89 	/* best effort to even work in out of memory situations */
90 	if (emsg == NULL)
91 		logit(LOG_CRIT, "%s", strerror(errno));
92 	else {
93 		va_start(ap, emsg);
94 
95 		if (asprintf(&nfmt, "%s: %s", emsg, strerror(errno)) == -1) {
96 			/* we tried it... */
97 			vlog(LOG_CRIT, emsg, ap);
98 			logit(LOG_CRIT, "%s", strerror(errno));
99 		} else {
100 			vlog(LOG_CRIT, nfmt, ap);
101 			free(nfmt);
102 		}
103 		va_end(ap);
104 	}
105 }
106 
107 void
108 log_warnx(const char *emsg, ...)
109 {
110 	va_list	 ap;
111 
112 	va_start(ap, emsg);
113 	vlog(LOG_CRIT, emsg, ap);
114 	va_end(ap);
115 }
116 
117 void
118 log_info(const char *emsg, ...)
119 {
120 	va_list	 ap;
121 
122 	va_start(ap, emsg);
123 	vlog(LOG_INFO, emsg, ap);
124 	va_end(ap);
125 }
126 
127 void
128 log_debug(const char *emsg, ...)
129 {
130 	va_list	 ap;
131 
132 	if (debug) {
133 		va_start(ap, emsg);
134 		vlog(LOG_DEBUG, emsg, ap);
135 		va_end(ap);
136 	}
137 }
138 
139 void
140 fatal(const char *emsg)
141 {
142 	if (emsg == NULL)
143 		logit(LOG_CRIT, "fatal in %s: %s", procnames[dvmrpd_process],
144 		    strerror(errno));
145 	else
146 		if (errno)
147 			logit(LOG_CRIT, "fatal in %s: %s: %s",
148 			    procnames[dvmrpd_process], emsg, strerror(errno));
149 		else
150 			logit(LOG_CRIT, "fatal in %s: %s",
151 			    procnames[dvmrpd_process], emsg);
152 
153 	if (dvmrpd_process == PROC_MAIN)
154 		exit(1);
155 	else				/* parent copes via SIGCHLD */
156 		_exit(1);
157 }
158 
159 void
160 fatalx(const char *emsg)
161 {
162 	errno = 0;
163 	fatal(emsg);
164 }
165 
166 /* names */
167 const char *
168 nbr_state_name(int state)
169 {
170 	switch (state) {
171 	case NBR_STA_DOWN:
172 		return ("DOWN");
173 	case NBR_STA_1_WAY:
174 		return ("1-WAY");
175 	case NBR_STA_2_WAY:
176 		return ("2-WAY");
177 	default:
178 		return ("UNKNOWN");
179 	}
180 }
181 
182 const char *
183 if_state_name(int state)
184 {
185 	switch (state) {
186 	case IF_STA_DOWN:
187 		return ("DOWN");
188 	case IF_STA_QUERIER:
189 		return ("QUERIER");
190 	case IF_STA_NONQUERIER:
191 		return ("NONQUERIER");
192 	default:
193 		return ("UNKNOWN");
194 	}
195 }
196 
197 const char *
198 group_state_name(int state)
199 {
200 	switch (state) {
201 	case GRP_STA_NO_MEMB_PRSNT:
202 		return ("NO MEMBER");
203 	case GRP_STA_MEMB_PRSNT:
204 		return ("MEMBER");
205 	case GRP_STA_V1_MEMB_PRSNT:
206 		return ("V1 MEMBER");
207 	case GRP_STA_CHECK_MEMB:
208 		return ("CHECKING");
209 	default:
210 		return ("UNKNOWN");
211 	}
212 }
213 
214 const char *
215 if_type_name(enum iface_type type)
216 {
217 	switch (type) {
218 	case IF_TYPE_POINTOPOINT:
219 		return ("POINTOPOINT");
220 	case IF_TYPE_BROADCAST:
221 		return ("BROADCAST");
222 	}
223 	/* NOTREACHED */
224 	return ("UNKNOWN");
225 }
226