2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
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.
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.
19 #include <sys/types.h>
33 static struct event imsgev;
37 static void handle_imsg_quit(struct imsgbuf*, struct imsg*, size_t);
38 static void handle_imsg_log(struct imsgbuf*, struct imsg*, size_t);
39 static void handle_imsg_log_type(struct imsgbuf*, struct imsg*, size_t);
40 static void handle_dispatch_imsg(int, short, void*);
42 static imsg_handlerfn *handlers[] = {
43 [IMSG_QUIT] = handle_imsg_quit,
44 [IMSG_LOG] = handle_imsg_log,
45 [IMSG_LOG_REQUEST] = handle_imsg_log,
46 [IMSG_LOG_TYPE] = handle_imsg_log_type,
57 strftime(buf, sizeof(buf), "%F %T",
58 localtime_r(&t, &tminfo));
59 fprintf(f, "[%s] ", buf);
63 should_log(int priority)
71 return conf.verbose >= 1;
73 return conf.verbose >= 2;
75 return conf.verbose >= 3;
82 send_log(int type, int priority, const char *msg, size_t len)
84 imsg_compose(&logibuf, type, priority, 0, -1, msg, len);
89 fatal(const char *fmt, ...)
97 if ((r = vasprintf(&fmted, fmt, ap)) != -1) {
98 send_log(IMSG_LOG, LOG_CRIT, fmted, r+1);
101 /* wait for the logger process to shut down */
111 vlog(int priority, struct client *c,
112 const char *fmt, va_list ap)
114 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
119 if (!should_log(priority))
123 len = sizeof(c->addr);
124 ec = getnameinfo((struct sockaddr*)&c->addr, len,
127 NI_NUMERICHOST | NI_NUMERICSERV);
129 fatal("getnameinfo: %s: %s",
130 gai_strerror(ec), strerror(errno));
133 if (vasprintf(&fmted, fmt, ap) == -1)
134 fatal("vasprintf: %s", strerror(errno));
137 ec = asprintf(&s, "internal: %s", fmted);
139 ec = asprintf(&s, "%s:%s %s", hbuf, sbuf, fmted);
142 fatal("asprintf: %s", strerror(errno));
144 send_log(IMSG_LOG, priority, s, ec+1);
151 log_err(struct client *c, const char *fmt, ...)
156 vlog(LOG_ERR, c, fmt, ap);
161 log_warn(struct client *c, const char *fmt, ...)
166 vlog(LOG_WARNING, c, fmt, ap);
171 log_notice(struct client *c, const char *fmt, ...)
176 vlog(LOG_NOTICE, c, fmt, ap);
181 log_info(struct client *c, const char *fmt, ...)
186 vlog(LOG_INFO, c, fmt, ap);
191 log_debug(struct client *c, const char *fmt, ...)
196 vlog(LOG_DEBUG, c, fmt, ap);
200 /* strchr, but with a bound */
202 gmid_strnchr(char *s, int c, size_t len)
206 for (i = 0; i < len; ++i)
213 log_request(struct client *c, char *meta, size_t l)
215 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV], b[GEMINI_URL_LEN];
220 len = sizeof(c->addr);
221 ec = getnameinfo((struct sockaddr*)&c->addr, len,
224 NI_NUMERICHOST | NI_NUMERICSERV);
226 fatal("getnameinfo: %s", gai_strerror(ec));
228 if (c->iri.schema != NULL) {
229 /* serialize the IRI */
230 strlcpy(b, c->iri.schema, sizeof(b));
231 strlcat(b, "://", sizeof(b));
233 /* log the decoded host name, but if it was invalid
234 * use the raw one. */
235 if (*c->domain != '\0')
236 strlcat(b, c->domain, sizeof(b));
238 strlcat(b, c->iri.host, sizeof(b));
240 strlcat(b, "/", sizeof(b));
241 strlcat(b, c->iri.path, sizeof(b)); /* TODO: sanitize UTF8 */
242 if (*c->iri.query != '\0') { /* TODO: sanitize UTF8 */
243 strlcat(b, "?", sizeof(b));
244 strlcat(b, c->iri.query, sizeof(b));
247 strlcpy(b, c->req, sizeof(b));
250 if ((t = gmid_strnchr(meta, '\r', l)) == NULL)
253 ec = asprintf(&fmted, "%s:%s GET %s %.*s", hbuf, sbuf, b,
254 (int)(t-meta), meta);
257 send_log(IMSG_LOG_REQUEST, LOG_NOTICE, fmted, ec+1);
264 do_log(int type, int priority, const char *msg)
268 if (priority == LOG_CRIT) {
274 if (type != IMSG_LOG_REQUEST)
276 fprintf(log, "%s\n", msg);
278 syslog(LOG_DAEMON | priority, "%s", msg);
285 handle_imsg_quit(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
291 handle_imsg_log(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
297 msg[datalen-1] = '\0';
298 priority = imsg->hdr.peerid;
299 do_log(imsg->hdr.type, priority, msg);
303 handle_imsg_log_type(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
305 if (log != NULL && log != stderr) {
311 if (imsg->fd != -1) {
312 if ((log = fdopen(imsg->fd, "a")) == NULL) {
313 syslog(LOG_DAEMON | LOG_ERR, "fdopen: %s",
321 handle_dispatch_imsg(int fd, short ev, void *d)
323 struct imsgbuf *ibuf = d;
324 dispatch_imsg(ibuf, handlers, sizeof(handlers));
328 logger_main(int fd, struct imsgbuf *ibuf)
332 openlog(getprogname(), LOG_NDELAY, LOG_DAEMON);
336 event_set(&imsgev, fd, EV_READ | EV_PERSIST, &handle_dispatch_imsg, ibuf);
337 event_add(&imsgev, NULL);
339 sandbox_logger_process();