Blob


1 /*
2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
17 #include "gmid.h"
19 #include <sys/types.h>
20 #include <sys/uio.h>
22 #include <errno.h>
23 #include <event.h>
24 #include <imsg.h>
25 #include <netdb.h>
26 #include <poll.h>
27 #include <stdarg.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <syslog.h>
31 #include <time.h>
33 #include "log.h"
35 static struct event imsgev;
37 static FILE *log;
39 static void handle_imsg_quit(struct imsgbuf*, struct imsg*, size_t);
40 static void handle_imsg_log(struct imsgbuf*, struct imsg*, size_t);
41 static void handle_imsg_log_type(struct imsgbuf*, struct imsg*, size_t);
42 static void handle_dispatch_imsg(int, short, void*);
44 static imsg_handlerfn *handlers[] = {
45 [IMSG_QUIT] = handle_imsg_quit,
46 [IMSG_LOG] = handle_imsg_log,
47 [IMSG_LOG_REQUEST] = handle_imsg_log,
48 [IMSG_LOG_TYPE] = handle_imsg_log_type,
49 };
51 static inline void
52 print_date(FILE *f)
53 {
54 struct tm tminfo;
55 time_t t;
56 char buf[20];
58 time(&t);
59 strftime(buf, sizeof(buf), "%F %T",
60 localtime_r(&t, &tminfo));
61 fprintf(f, "[%s] ", buf);
62 }
64 static inline int
65 should_log(int priority)
66 {
67 switch (priority) {
68 case LOG_ERR:
69 return 1;
70 case LOG_WARNING:
71 return 1;
72 case LOG_NOTICE:
73 return conf.verbose >= 1;
74 case LOG_INFO:
75 return conf.verbose >= 2;
76 case LOG_DEBUG:
77 return conf.verbose >= 3;
78 default:
79 return 0;
80 }
81 }
83 static inline void
84 send_log(int type, int priority, const char *msg, size_t len)
85 {
86 imsg_compose(&logibuf, type, priority, 0, -1, msg, len);
87 imsg_flush(&logibuf);
88 }
90 static __dead void
91 fatal_impl(int use_err, const char *fmt, va_list ap)
92 {
93 struct pollfd pfd;
94 char *str, *tmp;
95 int r, t, err;
97 err = errno;
99 if ((r = vasprintf(&str, fmt, ap)) != -1) {
100 if (use_err &&
101 (t = asprintf(&tmp, "%s: %s", str, strerror(err))) !=
102 -1) {
103 free(str);
104 str = tmp;
105 r = t;
107 } else
108 str = NULL, r = 0;
110 send_log(IMSG_LOG, LOG_CRIT, str, r);
111 free(str);
113 /* wait for the logger process to shut down */
114 memset(&pfd, 0, sizeof(pfd));
115 pfd.fd = logibuf.fd;
116 pfd.events = POLLIN;
117 poll(&pfd, 1, 1000);
119 exit(1);
122 void __dead
123 fatal(const char *fmt, ...)
125 va_list ap;
127 va_start(ap, fmt);
128 fatal_impl(1, fmt, ap);
129 va_end(ap);
132 void __dead
133 fatalx(const char *fmt, ...)
135 va_list ap;
137 va_start(ap, fmt);
138 fatal_impl(0, fmt, ap);
139 va_end(ap);
142 static inline void
143 vlog(int priority, struct client *c,
144 const char *fmt, va_list ap)
146 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
147 char *fmted, *s;
148 size_t len;
149 int ec;
151 if (!should_log(priority))
152 return;
154 if (c != NULL) {
155 len = sizeof(c->addr);
156 ec = getnameinfo((struct sockaddr*)&c->addr, len,
157 hbuf, sizeof(hbuf),
158 sbuf, sizeof(sbuf),
159 NI_NUMERICHOST | NI_NUMERICSERV);
160 if (ec != 0)
161 fatal("getnameinfo: %s", gai_strerror(ec));
164 if (vasprintf(&fmted, fmt, ap) == -1)
165 fatal("vasprintf: %s", strerror(errno));
167 if (c == NULL)
168 ec = asprintf(&s, "internal: %s", fmted);
169 else
170 ec = asprintf(&s, "%s:%s %s", hbuf, sbuf, fmted);
172 if (ec < 0)
173 fatal("asprintf: %s", strerror(errno));
175 send_log(IMSG_LOG, priority, s, ec+1);
177 free(fmted);
178 free(s);
181 void
182 log_err(struct client *c, const char *fmt, ...)
184 va_list ap;
186 va_start(ap, fmt);
187 vlog(LOG_ERR, c, fmt, ap);
188 va_end(ap);
191 void
192 log_warn(struct client *c, const char *fmt, ...)
194 va_list ap;
196 va_start(ap, fmt);
197 vlog(LOG_WARNING, c, fmt, ap);
198 va_end(ap);
201 void
202 log_notice(struct client *c, const char *fmt, ...)
204 va_list ap;
206 va_start(ap, fmt);
207 vlog(LOG_NOTICE, c, fmt, ap);
208 va_end(ap);
211 void
212 log_info(struct client *c, const char *fmt, ...)
214 va_list ap;
216 va_start(ap, fmt);
217 vlog(LOG_INFO, c, fmt, ap);
218 va_end(ap);
221 void
222 log_debug(struct client *c, const char *fmt, ...)
224 va_list ap;
226 va_start(ap, fmt);
227 vlog(LOG_DEBUG, c, fmt, ap);
228 va_end(ap);
231 /* strchr, but with a bound */
232 static char *
233 gmid_strnchr(char *s, int c, size_t len)
235 size_t i;
237 for (i = 0; i < len; ++i)
238 if (s[i] == c)
239 return &s[i];
240 return NULL;
243 void
244 log_request(struct client *c, char *meta, size_t l)
246 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV], b[GEMINI_URL_LEN];
247 char *fmted;
248 const char *t;
249 size_t len;
250 int ec;
252 len = sizeof(c->addr);
253 ec = getnameinfo((struct sockaddr*)&c->addr, len,
254 hbuf, sizeof(hbuf),
255 sbuf, sizeof(sbuf),
256 NI_NUMERICHOST | NI_NUMERICSERV);
257 if (ec != 0)
258 fatalx("getnameinfo: %s", gai_strerror(ec));
260 if (c->iri.schema != NULL) {
261 /* serialize the IRI */
262 strlcpy(b, c->iri.schema, sizeof(b));
263 strlcat(b, "://", sizeof(b));
265 /* log the decoded host name, but if it was invalid
266 * use the raw one. */
267 if (*c->domain != '\0')
268 strlcat(b, c->domain, sizeof(b));
269 else
270 strlcat(b, c->iri.host, sizeof(b));
272 if (*c->iri.path != '/')
273 strlcat(b, "/", sizeof(b));
274 strlcat(b, c->iri.path, sizeof(b)); /* TODO: sanitize UTF8 */
275 if (*c->iri.query != '\0') { /* TODO: sanitize UTF8 */
276 strlcat(b, "?", sizeof(b));
277 strlcat(b, c->iri.query, sizeof(b));
279 } else {
280 if ((t = c->req) == NULL)
281 t = "";
282 strlcpy(b, t, sizeof(b));
285 if ((t = gmid_strnchr(meta, '\r', l)) == NULL)
286 t = meta + len;
288 ec = asprintf(&fmted, "%s:%s GET %s %.*s", hbuf, sbuf, b,
289 (int)(t-meta), meta);
290 if (ec < 0)
291 err(1, "asprintf");
292 send_log(IMSG_LOG_REQUEST, LOG_NOTICE, fmted, ec+1);
293 free(fmted);
298 static void
299 do_log(int type, int priority, const char *msg)
301 int quit = 0;
303 if (priority == LOG_CRIT) {
304 quit = 1;
305 priority = LOG_ERR;
308 if (log != NULL) {
309 if (type != IMSG_LOG_REQUEST)
310 print_date(log);
311 fprintf(log, "%s\n", msg);
312 } else
313 syslog(LOG_DAEMON | priority, "%s", msg);
315 if (quit)
316 exit(1);
319 static void
320 handle_imsg_quit(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
322 event_loopbreak();
325 static void
326 handle_imsg_log(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
328 int priority;
329 char *msg;
331 msg = imsg->data;
332 msg[datalen-1] = '\0';
333 priority = imsg->hdr.peerid;
334 do_log(imsg->hdr.type, priority, msg);
337 static void
338 handle_imsg_log_type(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
340 if (log != NULL && log != stderr) {
341 fflush(log);
342 fclose(log);
344 log = NULL;
346 if (imsg->fd != -1) {
347 if ((log = fdopen(imsg->fd, "a")) == NULL) {
348 syslog(LOG_DAEMON | LOG_ERR, "fdopen: %s",
349 strerror(errno));
350 exit(1);
355 static void
356 handle_dispatch_imsg(int fd, short ev, void *d)
358 struct imsgbuf *ibuf = d;
359 dispatch_imsg(ibuf, handlers, sizeof(handlers));
362 int
363 logger_main(int fd, struct imsgbuf *ibuf)
365 log = stderr;
367 openlog(getprogname(), LOG_NDELAY, LOG_DAEMON);
368 tzset();
370 event_init();
372 event_set(&imsgev, fd, EV_READ | EV_PERSIST, &handle_dispatch_imsg, ibuf);
373 event_add(&imsgev, NULL);
375 sandbox_logger_process();
377 event_dispatch();
379 closelog();
381 return 0;