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