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