xref: /openbsd-src/usr.sbin/ripd/log.c (revision 91f110e064cd7c194e59e019b83bb7496c1c84d4)
1 /*	$OpenBSD: log.c,v 1.4 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 "ripd.h"
28 #include "log.h"
29 
30 static const char * const procnames[] = {
31 	"parent",
32 	"ripe",
33 	"rde"
34 };
35 
36 int	debug;
37 int	verbose;
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 log_verbose(int v)
56 {
57 	verbose = v;
58 }
59 
60 void
61 logit(int pri, const char *fmt, ...)
62 {
63 	va_list	ap;
64 
65 	va_start(ap, fmt);
66 	vlog(pri, fmt, ap);
67 	va_end(ap);
68 }
69 
70 void
71 vlog(int pri, const char *fmt, va_list ap)
72 {
73 	char	*nfmt;
74 
75 	if (debug) {
76 		/* best effort in out of mem situations */
77 		if (asprintf(&nfmt, "%s\n", fmt) == -1) {
78 			vfprintf(stderr, fmt, ap);
79 			fprintf(stderr, "\n");
80 		} else {
81 			vfprintf(stderr, nfmt, ap);
82 			free(nfmt);
83 		}
84 		fflush(stderr);
85 	} else
86 		vsyslog(pri, fmt, ap);
87 }
88 
89 void
90 log_warn(const char *emsg, ...)
91 {
92 	char	*nfmt;
93 	va_list	 ap;
94 
95 	/* best effort to even work in out of memory situations */
96 	if (emsg == NULL)
97 		logit(LOG_CRIT, "%s", strerror(errno));
98 	else {
99 		va_start(ap, emsg);
100 
101 		if (asprintf(&nfmt, "%s: %s", emsg, strerror(errno)) == -1) {
102 			/* we tried it... */
103 			vlog(LOG_CRIT, emsg, ap);
104 			logit(LOG_CRIT, "%s", strerror(errno));
105 		} else {
106 			vlog(LOG_CRIT, nfmt, ap);
107 			free(nfmt);
108 		}
109 		va_end(ap);
110 	}
111 }
112 
113 void
114 log_warnx(const char *emsg, ...)
115 {
116 	va_list	 ap;
117 
118 	va_start(ap, emsg);
119 	vlog(LOG_CRIT, emsg, ap);
120 	va_end(ap);
121 }
122 
123 void
124 log_info(const char *emsg, ...)
125 {
126 	va_list	 ap;
127 
128 	va_start(ap, emsg);
129 	vlog(LOG_INFO, emsg, ap);
130 	va_end(ap);
131 }
132 
133 void
134 log_debug(const char *emsg, ...)
135 {
136 	va_list	 ap;
137 
138 	if (verbose) {
139 		va_start(ap, emsg);
140 		vlog(LOG_DEBUG, emsg, ap);
141 		va_end(ap);
142 	}
143 }
144 
145 void
146 fatal(const char *emsg)
147 {
148 	if (emsg == NULL)
149 		logit(LOG_CRIT, "fatal in %s: %s", procnames[ripd_process],
150 		    strerror(errno));
151 	else
152 		if (errno)
153 			logit(LOG_CRIT, "fatal in %s: %s: %s",
154 			    procnames[ripd_process], emsg, strerror(errno));
155 		else
156 			logit(LOG_CRIT, "fatal in %s: %s",
157 			    procnames[ripd_process], emsg);
158 
159 	if (ripd_process == PROC_MAIN)
160 		exit(1);
161 	else				/* parent copes via SIGCHLD */
162 		_exit(1);
163 }
164 
165 void
166 fatalx(const char *emsg)
167 {
168 	errno = 0;
169 	fatal(emsg);
170 }
171 
172 /* names */
173 const char *
174 nbr_state_name(int state)
175 {
176 	switch (state) {
177 	case NBR_STA_DOWN:
178 		return ("DOWN");
179 	case NBR_STA_REQ_RCVD:
180 		return ("REQUEST RCVD");
181 	case NBR_STA_ACTIVE:
182 		return ("ACTIVE");
183 	default:
184 		return ("UNKNOWN");
185 	}
186 }
187 
188 const char *
189 if_type_name(enum iface_type type)
190 {
191 	switch (type) {
192 	case IF_TYPE_POINTOPOINT:
193 		return ("POINTOPOINT");
194 	case IF_TYPE_BROADCAST:
195 		return ("BROADCAST");
196 	case IF_TYPE_NBMA:
197 		return ("NBMA");
198 	case IF_TYPE_POINTOMULTIPOINT:
199 		return ("POINTOMULTIPOINT");
200 	}
201 	/* NOTREACHED */
202 	return ("UNKNOWN");
203 }
204 
205 const char *
206 if_auth_name(enum auth_type type)
207 {
208 	switch (type) {
209 	case AUTH_NONE:
210 		return ("none");
211 	case AUTH_SIMPLE:
212 		return ("simple");
213 	case AUTH_CRYPT:
214 		return ("crypt");
215 	}
216 	/* NOTREACHED */
217 	return ("unknown");
218 }
219 
220 const char *
221 if_state_name(int state)
222 {
223 	switch (state) {
224 	case IF_STA_DOWN:
225 		return ("DOWN");
226 	case IF_STA_ACTIVE:
227 		return ("ACTIVE");
228 	default:
229 		return ("UNKNOWN");
230 	}
231 }
232