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>
32 static struct event imsgev;
34 static void handle_imsg_quit(struct imsgbuf*, struct imsg*, size_t);
35 static void handle_imsg_log(struct imsgbuf*, struct imsg*, size_t);
36 static void handle_dispatch_imsg(int, short, void*);
38 static imsg_handlerfn *handlers[] = {
39 [IMSG_QUIT] = handle_imsg_quit,
40 [IMSG_LOG] = handle_imsg_log,
51 strftime(buf, sizeof(buf), "%F %T",
52 localtime_r(&t, &tminfo));
53 fprintf(stderr, "[%s] ", buf);
57 fatal(const char *fmt, ...)
63 if (conf.foreground) {
65 vfprintf(stderr, fmt, ap);
66 fprintf(stderr, "\n");
68 vsyslog(LOG_DAEMON | LOG_CRIT, fmt, ap);
75 should_log(int priority)
83 return conf.verbose >= 1;
85 return conf.verbose >= 2;
87 return conf.verbose >= 3;
94 send_log(const char *msg, size_t len)
96 imsg_compose(&logibuf, IMSG_LOG, 0, 0, -1, msg, len);
97 /* XXX: use event_once() */
102 vlog(int priority, struct client *c,
103 const char *fmt, va_list ap)
105 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
110 if (!should_log(priority))
114 len = sizeof(c->addr);
115 ec = getnameinfo((struct sockaddr*)&c->addr, len,
118 NI_NUMERICHOST | NI_NUMERICSERV);
120 fatal("getnameinfo: %s", gai_strerror(ec));
123 if (vasprintf(&fmted, fmt, ap) == -1)
124 fatal("vasprintf: %s", strerror(errno));
127 ec = asprintf(&s, "internal: %s", fmted);
129 ec = asprintf(&s, "%s:%s %s", hbuf, sbuf, fmted);
132 fatal("asprintf: %s", strerror(errno));
141 log_err(struct client *c, const char *fmt, ...)
146 vlog(LOG_ERR, c, fmt, ap);
151 log_warn(struct client *c, const char *fmt, ...)
156 vlog(LOG_WARNING, c, fmt, ap);
161 log_notice(struct client *c, const char *fmt, ...)
166 vlog(LOG_NOTICE, c, fmt, ap);
171 log_info(struct client *c, const char *fmt, ...)
176 vlog(LOG_INFO, c, fmt, ap);
181 log_debug(struct client *c, const char *fmt, ...)
186 vlog(LOG_DEBUG, c, fmt, ap);
190 /* strchr, but with a bound */
192 gmid_strnchr(char *s, int c, size_t len)
196 for (i = 0; i < len; ++i)
203 log_request(struct client *c, char *meta, size_t l)
205 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV], b[GEMINI_URL_LEN];
210 len = sizeof(c->addr);
211 ec = getnameinfo((struct sockaddr*)&c->addr, len,
214 NI_NUMERICHOST | NI_NUMERICSERV);
216 fatal("getnameinfo: %s", gai_strerror(ec));
218 if (c->iri.schema != NULL) {
219 /* serialize the IRI */
220 strlcpy(b, c->iri.schema, sizeof(b));
221 strlcat(b, "://", sizeof(b));
223 /* log the decoded host name, but if it was invalid
224 * use the raw one. */
225 if (*c->domain != '\0')
226 strlcat(b, c->domain, sizeof(b));
228 strlcat(b, c->iri.host, sizeof(b));
230 strlcat(b, "/", sizeof(b));
231 strlcat(b, c->iri.path, sizeof(b)); /* TODO: sanitize UTF8 */
232 if (*c->iri.query != '\0') { /* TODO: sanitize UTF8 */
233 strlcat(b, "?", sizeof(b));
234 strlcat(b, c->iri.query, sizeof(b));
237 strlcpy(b, c->req, sizeof(b));
240 if ((t = gmid_strnchr(meta, '\r', l)) == NULL)
243 ec = asprintf(&fmted, "%s:%s GET %s %.*s", hbuf, sbuf, b,
244 (int)(t-meta), meta);
247 send_log(fmted, ec+1);
254 handle_imsg_quit(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
260 handle_imsg_log(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen)
265 msg[datalen-1] = '\0';
267 if (conf.foreground) {
269 fprintf(stderr, "%s\n", msg);
271 syslog(LOG_DAEMON, "%s", msg);
275 handle_dispatch_imsg(int fd, short ev, void *d)
277 struct imsgbuf *ibuf = d;
278 dispatch_imsg(ibuf, handlers, sizeof(handlers));
282 logger_main(int fd, struct imsgbuf *ibuf)
286 event_set(&imsgev, fd, EV_READ | EV_PERSIST, &handle_dispatch_imsg, ibuf);
287 event_add(&imsgev, NULL);
289 sandbox_logger_process();