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;
35 static void handle_imsg_quit(struct imsgbuf*, struct imsg*, size_t);
36 static void handle_imsg_log(struct imsgbuf*, struct imsg*, size_t);
37 static void handle_dispatch_imsg(int, short, void*);
39 static imsg_handlerfn *handlers[] = {
40 [IMSG_QUIT] = handle_imsg_quit,
41 [IMSG_LOG] = handle_imsg_log,
52 strftime(buf, sizeof(buf), "%F %T",
53 localtime_r(&t, &tminfo));
54 fprintf(stderr, "[%s] ", buf);
58 should_log(int priority)
66 return conf.verbose >= 1;
68 return conf.verbose >= 2;
70 return conf.verbose >= 3;
77 send_log(int priority, const char *msg, size_t len)
79 imsg_compose(&logibuf, IMSG_LOG, priority, 0, -1, msg, len);
84 fatal(const char *fmt, ...)
92 if ((r = vasprintf(&fmted, fmt, ap)) != -1) {
93 send_log(LOG_CRIT, fmted, r+1);
96 /* wait for the logger process to shut down */
106 vlog(int priority, struct client *c,
107 const char *fmt, va_list ap)
109 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
114 if (!should_log(priority))
118 len = sizeof(c->addr);
119 ec = getnameinfo((struct sockaddr*)&c->addr, len,
122 NI_NUMERICHOST | NI_NUMERICSERV);
124 fatal("getnameinfo: %s", gai_strerror(ec));
127 if (vasprintf(&fmted, fmt, ap) == -1)
128 fatal("vasprintf: %s", strerror(errno));
131 ec = asprintf(&s, "internal: %s", fmted);
133 ec = asprintf(&s, "%s:%s %s", hbuf, sbuf, fmted);
136 fatal("asprintf: %s", strerror(errno));
138 send_log(priority, s, ec+1);
145 log_err(struct client *c, const char *fmt, ...)
150 vlog(LOG_ERR, c, fmt, ap);
155 log_warn(struct client *c, const char *fmt, ...)
160 vlog(LOG_WARNING, c, fmt, ap);
165 log_notice(struct client *c, const char *fmt, ...)
170 vlog(LOG_NOTICE, c, fmt, ap);
175 log_info(struct client *c, const char *fmt, ...)
180 vlog(LOG_INFO, c, fmt, ap);
185 log_debug(struct client *c, const char *fmt, ...)
190 vlog(LOG_DEBUG, c, fmt, ap);
194 /* strchr, but with a bound */
196 gmid_strnchr(char *s, int c, size_t len)
200 for (i = 0; i < len; ++i)
207 log_request(struct client *c, char *meta, size_t l)
209 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV], b[GEMINI_URL_LEN];
214 len = sizeof(c->addr);
215 ec = getnameinfo((struct sockaddr*)&c->addr, len,
218 NI_NUMERICHOST | NI_NUMERICSERV);
220 fatal("getnameinfo: %s", gai_strerror(ec));
222 if (c->iri.schema != NULL) {
223 /* serialize the IRI */
224 strlcpy(b, c->iri.schema, sizeof(b));
225 strlcat(b, "://", sizeof(b));
227 /* log the decoded host name, but if it was invalid
228 * use the raw one. */
229 if (*c->domain != '\0')
230 strlcat(b, c->domain, sizeof(b));
232 strlcat(b, c->iri.host, sizeof(b));
234 strlcat(b, "/", sizeof(b));
235 strlcat(b, c->iri.path, sizeof(b)); /* TODO: sanitize UTF8 */
236 if (*c->iri.query != '\0') { /* TODO: sanitize UTF8 */
237 strlcat(b, "?", sizeof(b));
238 strlcat(b, c->iri.query, sizeof(b));
241 strlcpy(b, c->req, sizeof(b));
244 if ((t = gmid_strnchr(meta, '\r', l)) == NULL)
247 ec = asprintf(&fmted, "%s:%s GET %s %.*s", hbuf, sbuf, b,
248 (int)(t-meta), meta);
251 send_log(LOG_NOTICE, fmted, ec+1);
258 handle_imsg_quit(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
264 handle_imsg_log(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
270 msg[datalen-1] = '\0';
272 priority = imsg->hdr.peerid;
275 if (priority == LOG_CRIT) {
280 if (conf.foreground) {
282 fprintf(stderr, "%s\n", msg);
284 syslog(LOG_DAEMON | priority, "%s", msg);
291 handle_dispatch_imsg(int fd, short ev, void *d)
293 struct imsgbuf *ibuf = d;
294 dispatch_imsg(ibuf, handlers, sizeof(handlers));
298 logger_main(int fd, struct imsgbuf *ibuf)
302 event_set(&imsgev, fd, EV_READ | EV_PERSIST, &handle_dispatch_imsg, ibuf);
303 event_add(&imsgev, NULL);
305 sandbox_logger_process();