xref: /openbsd-src/usr.sbin/ldpd/log.c (revision 5054e3e78af0749a9bb00ba9a024b3ee2d90290f)
1 /*	$OpenBSD: log.c,v 1.2 2009/11/02 20:34:58 claudio 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 "ldpd.h"
28 #include "log.h"
29 
30 static const char * const procnames[] = {
31 	"parent",
32 	"ldpe",
33 	"lde"
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 	verbose = 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[ldpd_process],
151 		    strerror(errno));
152 	else
153 		if (errno)
154 			logit(LOG_CRIT, "fatal in %s: %s: %s",
155 			    procnames[ldpd_process], emsg, strerror(errno));
156 		else
157 			logit(LOG_CRIT, "fatal in %s: %s",
158 			    procnames[ldpd_process], emsg);
159 
160 	if (ldpd_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_PRESENT:
181 		return ("PRESENT");
182 	case NBR_STA_INITIAL:
183 		return ("INITIALIZED");
184 	case NBR_STA_OPENREC:
185 		return ("OPENREC");
186 	case NBR_STA_OPENSENT:
187 		return ("OPENSENT");
188 	case NBR_STA_OPER:
189 		return ("OPERATIONAL");
190 	case NBR_STA_ACTIVE:
191 		return ("ACTIVE");
192 	default:
193 		return ("UNKNW");
194 	}
195 }
196 
197 const char *
198 if_state_name(int state)
199 {
200 	switch (state) {
201 	case IF_STA_DOWN:
202 		return ("DOWN");
203 	case IF_STA_LOOPBACK:
204 		return ("LOOP");
205 	case IF_STA_POINTTOPOINT:
206 		return ("P2P");
207 	case IF_STA_DROTHER:
208 		return ("OTHER");
209 	case IF_STA_ACTIVE:
210 		return ("ACTIVE");
211 	default:
212 		return ("UNKNW");
213 	}
214 }
215 
216 const char *
217 if_type_name(enum iface_type type)
218 {
219 	switch (type) {
220 	case IF_TYPE_POINTOPOINT:
221 		return ("POINTOPOINT");
222 	case IF_TYPE_BROADCAST:
223 		return ("BROADCAST");
224 	case IF_TYPE_NBMA:
225 		return ("NBMA");
226 	case IF_TYPE_POINTOMULTIPOINT:
227 		return ("POINTOMULTIPOINT");
228 	case IF_TYPE_VIRTUALLINK:
229 		return ("VIRTUALLINK");
230 	}
231 	/* NOTREACHED */
232 	return ("UNKNOWN");
233 }
234