Blob


1 /*
2 * Copyright (c) 2003, 2004 Henning Brauer <henning@openbsd.org>
3 * Copyright (c) 2021 Matthias Schmidt <xhr@giessen.ccc.de>
4 *
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16 */
18 #define _GNU_SOURCE
20 #include <sys/types.h>
21 #include <sys/stat.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <string.h>
27 #include <syslog.h>
28 #include <errno.h>
29 #include <fcntl.h>
30 #include <unistd.h>
31 #include <time.h>
33 #include "log.h"
34 #include "twind.h"
36 #define MAXLOGLINE 1024
38 static int debug;
39 static int verbose;
40 static int access_fd;
41 static int error_fd;
43 static const char *log_procname;
45 void
46 log_init(int n_debug, int facility)
47 {
48 extern char *__progname;
50 debug = n_debug;
51 verbose = n_debug;
52 log_procinit(__progname);
54 if (!debug)
55 openlog(__progname, LOG_PID | LOG_NDELAY, facility);
57 tzset();
58 }
60 void
61 log_procinit(const char *procname)
62 {
63 if (procname != NULL)
64 log_procname = procname;
65 }
67 void
68 log_setverbose(int v)
69 {
70 verbose = v;
71 }
73 int
74 log_getverbose(void)
75 {
76 return (verbose);
77 }
79 void
80 logit(int pri, const char *fmt, ...)
81 {
82 va_list ap;
84 va_start(ap, fmt);
85 vlog(pri, fmt, ap);
86 va_end(ap);
87 }
89 void
90 vlog(int pri, const char *fmt, va_list ap)
91 {
92 char *nfmt;
93 int saved_errno = errno;
95 if (debug) {
96 /* best effort in out of mem situations */
97 if (asprintf(&nfmt, "%s\n", fmt) == -1) {
98 vfprintf(stderr, fmt, ap);
99 fprintf(stderr, "\n");
100 } else {
101 vfprintf(stderr, nfmt, ap);
102 free(nfmt);
104 fflush(stderr);
105 } else
106 vsyslog(pri, fmt, ap);
108 errno = saved_errno;
111 void
112 log_warn(const char *emsg, ...)
114 char *nfmt;
115 va_list ap;
116 int saved_errno = errno;
118 /* best effort to even work in out of memory situations */
119 if (emsg == NULL)
120 logit(LOG_ERR, "%s", strerror(saved_errno));
121 else {
122 va_start(ap, emsg);
124 if (asprintf(&nfmt, "%s: %s", emsg,
125 strerror(saved_errno)) == -1) {
126 /* we tried it... */
127 vlog(LOG_ERR, emsg, ap);
128 logit(LOG_ERR, "%s", strerror(saved_errno));
129 } else {
130 vlog(LOG_ERR, nfmt, ap);
131 free(nfmt);
133 va_end(ap);
136 errno = saved_errno;
139 void
140 log_warnx(const char *emsg, ...)
142 va_list ap;
144 va_start(ap, emsg);
145 vlog(LOG_ERR, emsg, ap);
146 va_end(ap);
149 void
150 log_info(const char *emsg, ...)
152 va_list ap;
154 va_start(ap, emsg);
155 vlog(LOG_INFO, emsg, ap);
156 va_end(ap);
159 void
160 log_debug(const char *emsg, ...)
162 va_list ap;
164 if (verbose) {
165 va_start(ap, emsg);
166 vlog(LOG_DEBUG, emsg, ap);
167 va_end(ap);
171 static void
172 vfatalc(int code, const char *emsg, va_list ap)
174 static char s[BUFSIZ];
175 const char *sep;
177 if (emsg != NULL) {
178 (void)vsnprintf(s, sizeof(s), emsg, ap);
179 sep = ": ";
180 } else {
181 s[0] = '\0';
182 sep = "";
184 if (code)
185 logit(LOG_CRIT, "fatal in %s: %s%s%s",
186 log_procname, s, sep, strerror(code));
187 else
188 logit(LOG_CRIT, "fatal in %s%s%s", log_procname, sep, s);
191 void
192 fatal(const char *emsg, ...)
194 va_list ap;
196 va_start(ap, emsg);
197 vfatalc(errno, emsg, ap);
198 va_end(ap);
199 exit(1);
202 void
203 fatalx(const char *emsg, ...)
205 va_list ap;
207 va_start(ap, emsg);
208 vfatalc(0, emsg, ap);
209 va_end(ap);
210 exit(1);
213 void
214 open_twind_logs(void)
216 if ((access_fd = open(_PATH_TWIND_ACCESS_LOG, O_WRONLY|O_APPEND|O_CREAT, 0644))
217 == -1)
218 fatalx("Cannot open access log: %s", _PATH_TWIND_ACCESS_LOG);
220 if ((error_fd = open(_PATH_TWIND_ERROR_LOG, O_WRONLY|O_APPEND|O_CREAT, 0644))
221 == -1)
222 fatalx("Cannot open error log: %s", _PATH_TWIND_ACCESS_LOG);
224 log_debug("Log files open");
227 void
228 close_twind_logs(void)
230 log_debug("Closing log file");
231 close(access_fd);
232 close(error_fd);
235 void
236 log_access(const struct client_connection *cc, const char *fmt, ...)
238 char tmstr[29];
239 struct tm tm;
240 time_t t;
242 t = time(NULL);
243 tm = *localtime(&t);
245 if (strftime(tmstr, sizeof(tmstr), "%d/%b/%Y:%H:%M:%S %Z", &tm) == 0) {
246 log_warn("strftime couldn't save time string");
247 return;
250 user_log(0, "%s - - [%s] %s", cc->client_addr, tmstr, fmt);
253 void
254 log_error(const struct client_connection *cc, const char *fmt, ...)
256 char tmstr[29];
257 struct tm tm;
258 time_t t;
260 t = time(NULL);
261 tm = *localtime(&t);
262 if (strftime(tmstr, sizeof(tmstr), "%d/%b/%Y:%H:%M:%S %Z", &tm) == 0) {
263 log_warn("strftime couldn't save time string");
264 return;
267 user_log(1, "[%s] [error] [client %s] %s", tmstr,
268 cc->client_addr, fmt);
271 void
272 user_log(int target, const char *fmt, ...)
274 va_list ap;
275 int fd = -1;
277 va_start(ap, fmt);
278 if (target == 0)
279 fd = access_fd;
280 else if (target == 1)
281 fd = error_fd;
282 else {
283 log_warn("Non-existent user log target");
284 return;
287 vdprintf(fd, fmt, ap);
288 dprintf(fd, "\n");
290 va_end(ap);