xref: /openbsd/usr.sbin/ldpd/log.c (revision 9b2c1562)
1*9b2c1562Sbluhm /*	$OpenBSD: log.c,v 1.34 2017/03/21 12:06:55 bluhm 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 <stdio.h>
21ab0c2486Smichele #include <stdlib.h>
22ab0c2486Smichele #include <string.h>
23ab0c2486Smichele #include <syslog.h>
24ab0c2486Smichele #include <unistd.h>
25a8c39dc0Srenato #include <netdb.h>
264dcd314eSrenato #include <limits.h>
27ab0c2486Smichele 
28ab0c2486Smichele #include "log.h"
293e2a4faaSbenno #include "ldpd.h"
30ab0c2486Smichele 
313e2a4faaSbenno int		 debug;
323e2a4faaSbenno int		 verbose;
333e2a4faaSbenno const char	*log_procname;
34ab0c2486Smichele 
35ab0c2486Smichele void
log_init(int n_debug)36ab0c2486Smichele log_init(int n_debug)
37ab0c2486Smichele {
38ab0c2486Smichele 	extern char	*__progname;
39ab0c2486Smichele 
40ab0c2486Smichele 	debug = n_debug;
41ab0c2486Smichele 
42ab0c2486Smichele 	if (!debug)
43ab0c2486Smichele 		openlog(__progname, LOG_PID | LOG_NDELAY, LOG_DAEMON);
44ab0c2486Smichele 
45ab0c2486Smichele 	tzset();
46ab0c2486Smichele }
47ab0c2486Smichele 
48ab0c2486Smichele void
log_verbose(int v)497dc5fe12Sclaudio log_verbose(int v)
507dc5fe12Sclaudio {
517dc5fe12Sclaudio 	verbose = v;
527dc5fe12Sclaudio }
537dc5fe12Sclaudio 
547dc5fe12Sclaudio void
logit(int pri,const char * fmt,...)55ab0c2486Smichele logit(int pri, const char *fmt, ...)
56ab0c2486Smichele {
57ab0c2486Smichele 	va_list	ap;
58ab0c2486Smichele 
59ab0c2486Smichele 	va_start(ap, fmt);
60ab0c2486Smichele 	vlog(pri, fmt, ap);
61ab0c2486Smichele 	va_end(ap);
62ab0c2486Smichele }
63ab0c2486Smichele 
643e2a4faaSbenno void
vlog(int pri,const char * fmt,va_list ap)65ab0c2486Smichele vlog(int pri, const char *fmt, va_list ap)
66ab0c2486Smichele {
67ab0c2486Smichele 	char	*nfmt;
68ab0c2486Smichele 
69ab0c2486Smichele 	if (debug) {
70ab0c2486Smichele 		/* best effort in out of mem situations */
71ab0c2486Smichele 		if (asprintf(&nfmt, "%s\n", fmt) == -1) {
72ab0c2486Smichele 			vfprintf(stderr, fmt, ap);
73ab0c2486Smichele 			fprintf(stderr, "\n");
74ab0c2486Smichele 		} else {
75ab0c2486Smichele 			vfprintf(stderr, nfmt, ap);
76ab0c2486Smichele 			free(nfmt);
77ab0c2486Smichele 		}
78ab0c2486Smichele 		fflush(stderr);
79ab0c2486Smichele 	} else
80ab0c2486Smichele 		vsyslog(pri, fmt, ap);
81ab0c2486Smichele }
82ab0c2486Smichele 
83ab0c2486Smichele void
log_warn(const char * emsg,...)84ab0c2486Smichele log_warn(const char *emsg, ...)
85ab0c2486Smichele {
86ab0c2486Smichele 	char	*nfmt;
87ab0c2486Smichele 	va_list	 ap;
88ab0c2486Smichele 
89ab0c2486Smichele 	/* best effort to even work in out of memory situations */
90ab0c2486Smichele 	if (emsg == NULL)
91*9b2c1562Sbluhm 		logit(LOG_ERR, "%s", strerror(errno));
92ab0c2486Smichele 	else {
93ab0c2486Smichele 		va_start(ap, emsg);
94ab0c2486Smichele 
95ab0c2486Smichele 		if (asprintf(&nfmt, "%s: %s", emsg, strerror(errno)) == -1) {
96ab0c2486Smichele 			/* we tried it... */
97*9b2c1562Sbluhm 			vlog(LOG_ERR, emsg, ap);
98*9b2c1562Sbluhm 			logit(LOG_ERR, "%s", strerror(errno));
99ab0c2486Smichele 		} else {
100*9b2c1562Sbluhm 			vlog(LOG_ERR, nfmt, ap);
101ab0c2486Smichele 			free(nfmt);
102ab0c2486Smichele 		}
103ab0c2486Smichele 		va_end(ap);
104ab0c2486Smichele 	}
105ab0c2486Smichele }
106ab0c2486Smichele 
107ab0c2486Smichele void
log_warnx(const char * emsg,...)108ab0c2486Smichele log_warnx(const char *emsg, ...)
109ab0c2486Smichele {
110ab0c2486Smichele 	va_list	 ap;
111ab0c2486Smichele 
112ab0c2486Smichele 	va_start(ap, emsg);
113*9b2c1562Sbluhm 	vlog(LOG_ERR, emsg, ap);
114ab0c2486Smichele 	va_end(ap);
115ab0c2486Smichele }
116ab0c2486Smichele 
117ab0c2486Smichele void
log_info(const char * emsg,...)118ab0c2486Smichele log_info(const char *emsg, ...)
119ab0c2486Smichele {
120ab0c2486Smichele 	va_list	 ap;
121ab0c2486Smichele 
122ab0c2486Smichele 	va_start(ap, emsg);
123ab0c2486Smichele 	vlog(LOG_INFO, emsg, ap);
124ab0c2486Smichele 	va_end(ap);
125ab0c2486Smichele }
126ab0c2486Smichele 
127ab0c2486Smichele void
log_debug(const char * emsg,...)128ab0c2486Smichele log_debug(const char *emsg, ...)
129ab0c2486Smichele {
130ab0c2486Smichele 	va_list	 ap;
131ab0c2486Smichele 
13284f16a94Sclaudio 	if (verbose & LDPD_OPT_VERBOSE) {
133ab0c2486Smichele 		va_start(ap, emsg);
134ab0c2486Smichele 		vlog(LOG_DEBUG, emsg, ap);
135ab0c2486Smichele 		va_end(ap);
136ab0c2486Smichele 	}
137ab0c2486Smichele }
138ab0c2486Smichele 
139ab0c2486Smichele void
fatal(const char * emsg)140ab0c2486Smichele fatal(const char *emsg)
141ab0c2486Smichele {
142ab0c2486Smichele 	if (emsg == NULL)
1433e2a4faaSbenno 		logit(LOG_CRIT, "fatal in %s: %s", log_procname,
144ab0c2486Smichele 		    strerror(errno));
145ab0c2486Smichele 	else
146ab0c2486Smichele 		if (errno)
147ab0c2486Smichele 			logit(LOG_CRIT, "fatal in %s: %s: %s",
1483e2a4faaSbenno 			    log_procname, emsg, strerror(errno));
149ab0c2486Smichele 		else
150ab0c2486Smichele 			logit(LOG_CRIT, "fatal in %s: %s",
1513e2a4faaSbenno 			    log_procname, emsg);
152ab0c2486Smichele 
153ab0c2486Smichele 	exit(1);
154ab0c2486Smichele }
155ab0c2486Smichele 
156ab0c2486Smichele void
fatalx(const char * emsg)157ab0c2486Smichele fatalx(const char *emsg)
158ab0c2486Smichele {
159ab0c2486Smichele 	errno = 0;
160ab0c2486Smichele 	fatal(emsg);
161ab0c2486Smichele }
162