xref: /openbsd/usr.sbin/httpd/log.c (revision a6445c1d)
1 /*	$OpenBSD: log.c,v 1.3 2014/10/25 03:23:49 lteo 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 MIND, USE, DATA OR PROFITS, WHETHER
15  * IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING
16  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include <sys/types.h>
20 #include <sys/queue.h>
21 #include <sys/socket.h>
22 #include <sys/tree.h>
23 
24 #include <net/if.h>
25 #include <netinet/in.h>
26 #include <netinet/ip.h>
27 #include <arpa/inet.h>
28 
29 #include <errno.h>
30 #include <stdarg.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <syslog.h>
35 #include <event.h>
36 #include <netdb.h>
37 #include <ctype.h>
38 
39 #include "httpd.h"
40 
41 int	 debug;
42 int	 verbose;
43 
44 void	 vlog(int, const char *, va_list)
45 	    __attribute__((__format__ (printf, 2, 0)));
46 void	 logit(int, const char *, ...)
47 	    __attribute__((__format__ (printf, 2, 3)));
48 
49 void
50 log_init(int n_debug)
51 {
52 	extern char	*__progname;
53 
54 	debug = n_debug;
55 	verbose = n_debug;
56 
57 	if (!debug)
58 		openlog(__progname, LOG_PID | LOG_NDELAY, LOG_DAEMON);
59 
60 	tzset();
61 }
62 
63 void
64 log_verbose(int v)
65 {
66 	verbose = v;
67 }
68 
69 void
70 logit(int pri, const char *fmt, ...)
71 {
72 	va_list	ap;
73 
74 	va_start(ap, fmt);
75 	vlog(pri, fmt, ap);
76 	va_end(ap);
77 }
78 
79 void
80 vlog(int pri, const char *fmt, va_list ap)
81 {
82 	char	*nfmt;
83 
84 	if (debug) {
85 		/* best effort in out of mem situations */
86 		if (asprintf(&nfmt, "%s\n", fmt) == -1) {
87 			vfprintf(stderr, fmt, ap);
88 			fprintf(stderr, "\n");
89 		} else {
90 			vfprintf(stderr, nfmt, ap);
91 			free(nfmt);
92 		}
93 		fflush(stderr);
94 	} else
95 		vsyslog(pri, fmt, ap);
96 }
97 
98 
99 void
100 log_warn(const char *emsg, ...)
101 {
102 	char	*nfmt;
103 	va_list	 ap;
104 
105 	/* best effort to even work in out of memory situations */
106 	if (emsg == NULL)
107 		logit(LOG_CRIT, "%s", strerror(errno));
108 	else {
109 		va_start(ap, emsg);
110 
111 		if (asprintf(&nfmt, "%s: %s", emsg, strerror(errno)) == -1) {
112 			/* we tried it... */
113 			vlog(LOG_CRIT, emsg, ap);
114 			logit(LOG_CRIT, "%s", strerror(errno));
115 		} else {
116 			vlog(LOG_CRIT, nfmt, ap);
117 			free(nfmt);
118 		}
119 		va_end(ap);
120 	}
121 }
122 
123 void
124 log_warnx(const char *emsg, ...)
125 {
126 	va_list	 ap;
127 
128 	va_start(ap, emsg);
129 	vlog(LOG_CRIT, emsg, ap);
130 	va_end(ap);
131 }
132 
133 void
134 log_info(const char *emsg, ...)
135 {
136 	va_list	 ap;
137 
138 	va_start(ap, emsg);
139 	vlog(LOG_INFO, emsg, ap);
140 	va_end(ap);
141 }
142 
143 void
144 log_debug(const char *emsg, ...)
145 {
146 	va_list	 ap;
147 
148 	if (verbose > 1) {
149 		va_start(ap, emsg);
150 		vlog(LOG_DEBUG, emsg, ap);
151 		va_end(ap);
152 	}
153 }
154 
155 void
156 fatal(const char *emsg)
157 {
158 	if (emsg == NULL)
159 		logit(LOG_CRIT, "fatal: %s", strerror(errno));
160 	else
161 		if (errno)
162 			logit(LOG_CRIT, "fatal: %s: %s",
163 			    emsg, strerror(errno));
164 		else
165 			logit(LOG_CRIT, "fatal: %s", emsg);
166 
167 	exit(1);
168 }
169 
170 void
171 fatalx(const char *emsg)
172 {
173 	errno = 0;
174 	fatal(emsg);
175 }
176 
177 const char *
178 print_host(struct sockaddr_storage *ss, char *buf, size_t len)
179 {
180 	if (getnameinfo((struct sockaddr *)ss, ss->ss_len,
181 	    buf, len, NULL, 0, NI_NUMERICHOST) != 0) {
182 		buf[0] = '\0';
183 		return (NULL);
184 	}
185 	return (buf);
186 }
187 
188 const char *
189 print_time(struct timeval *a, struct timeval *b, char *buf, size_t len)
190 {
191 	struct timeval		tv;
192 	u_long			h, sec, min;
193 
194 	timerclear(&tv);
195 	timersub(a, b, &tv);
196 	sec = tv.tv_sec % 60;
197 	min = tv.tv_sec / 60 % 60;
198 	h = tv.tv_sec / 60 / 60;
199 
200 	snprintf(buf, len, "%.2lu:%.2lu:%.2lu", h, min, sec);
201 	return (buf);
202 }
203 
204 const char *
205 printb_flags(const u_int32_t v, const char *bits)
206 {
207 	static char	 buf[2][BUFSIZ];
208 	static int	 idx = 0;
209 	int		 i, any = 0;
210 	char		 c, *p, *r;
211 
212 	p = r = buf[++idx % 2];
213 	memset(p, 0, BUFSIZ);
214 
215 	if (bits) {
216 		bits++;
217 		while ((i = *bits++)) {
218 			if (v & (1 << (i - 1))) {
219 				if (any) {
220 					*p++ = ',';
221 					*p++ = ' ';
222 				}
223 				any = 1;
224 				for (; (c = *bits) > 32; bits++) {
225 					if (c == '_')
226 						*p++ = ' ';
227 					else
228 						*p++ = tolower((u_char)c);
229 				}
230 			} else
231 				for (; *bits > 32; bits++)
232 					;
233 		}
234 	}
235 
236 	return (r);
237 }
238 
239 void
240 getmonotime(struct timeval *tv)
241 {
242 	struct timespec	 ts;
243 
244 	if (clock_gettime(CLOCK_MONOTONIC, &ts))
245 		fatal("clock_gettime");
246 
247 	TIMESPEC_TO_TIMEVAL(tv, &ts);
248 }
249