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>
20 #include <sys/queue.h>
32 struct imsgbuf parent_ibuf, child_ibuf;
33 struct event sigusr2, inlog;
36 static void handle_log(int, short, void*);
37 static int logger_main(int, struct imsgbuf*);
40 fatal(const char *fmt, ...)
46 if (conf.foreground) {
47 vfprintf(stderr, fmt, ap);
48 fprintf(stderr, "\n");
50 vsyslog(LOG_DAEMON | LOG_CRIT, fmt, ap);
57 should_log(int priority)
65 return conf.verbose >= 1;
67 return conf.verbose >= 2;
69 return conf.verbose >= 3;
76 send_log(const char *msg, size_t len)
78 imsg_compose(&parent_ibuf, 0, 0, 0, -1, msg, len);
79 /* XXX: use event_once() */
80 imsg_flush(&parent_ibuf);
84 vlog(int priority, struct client *c,
85 const char *fmt, va_list ap)
87 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
92 if (!should_log(priority))
96 len = sizeof(c->addr);
97 ec = getnameinfo((struct sockaddr*)&c->addr, len,
100 NI_NUMERICHOST | NI_NUMERICSERV);
102 fatal("getnameinfo: %s", gai_strerror(ec));
105 if (vasprintf(&fmted, fmt, ap) == -1)
106 fatal("vasprintf: %s", strerror(errno));
109 ec = asprintf(&s, "internal: %s", fmted);
111 ec = asprintf(&s, "%s:%s %s", hbuf, sbuf, fmted);
114 fatal("asprintf: %s", strerror(errno));
123 log_err(struct client *c, const char *fmt, ...)
128 vlog(LOG_ERR, c, fmt, ap);
133 log_warn(struct client *c, const char *fmt, ...)
138 vlog(LOG_WARNING, c, fmt, ap);
143 log_notice(struct client *c, const char *fmt, ...)
148 vlog(LOG_NOTICE, c, fmt, ap);
153 log_info(struct client *c, const char *fmt, ...)
158 vlog(LOG_INFO, c, fmt, ap);
163 log_debug(struct client *c, const char *fmt, ...)
168 vlog(LOG_DEBUG, c, fmt, ap);
172 /* strchr, but with a bound */
174 gmid_strnchr(char *s, int c, size_t len)
178 for (i = 0; i < len; ++i)
185 log_request(struct client *c, char *meta, size_t l)
187 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV], b[GEMINI_URL_LEN];
192 len = sizeof(c->addr);
193 ec = getnameinfo((struct sockaddr*)&c->addr, len,
196 NI_NUMERICHOST | NI_NUMERICSERV);
198 fatal("getnameinfo: %s", gai_strerror(ec));
200 if (c->iri.schema != NULL) {
201 /* serialize the IRI */
202 strlcpy(b, c->iri.schema, sizeof(b));
203 strlcat(b, "://", sizeof(b));
205 /* log the decoded host name, but if it was invalid
206 * use the raw one. */
207 if (*c->domain != '\0')
208 strlcat(b, c->domain, sizeof(b));
210 strlcat(b, c->iri.host, sizeof(b));
212 strlcat(b, "/", sizeof(b));
213 strlcat(b, c->iri.path, sizeof(b)); /* TODO: sanitize UTF8 */
214 if (*c->iri.query != '\0') { /* TODO: sanitize UTF8 */
215 strlcat(b, "?", sizeof(b));
216 strlcat(b, c->iri.query, sizeof(b));
219 strlcpy(b, c->req, sizeof(b));
222 if ((t = gmid_strnchr(meta, '\r', l)) == NULL)
225 ec = asprintf(&fmted, "%s:%s GET %s %.*s", hbuf, sbuf, b,
226 (int)(t-meta), meta);
229 send_log(fmted, ec+1);
236 handle_log(int fd, short ev, void *d)
238 struct imsgbuf *ibuf = d;
243 if ((n = imsg_read(ibuf)) == -1) {
244 if (errno == EAGAIN || errno == EWOULDBLOCK)
249 errx(1, "connection lost?");
252 if ((n = imsg_get(ibuf, &imsg)) == -1)
253 err(1, "read error");
257 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
261 /* ignore imsg.hdr.type for now */
263 fprintf(stderr, "%s\n", msg);
265 syslog(LOG_DAEMON, "%s", msg);
272 logger_main(int fd, struct imsgbuf *ibuf)
276 event_set(&inlog, fd, EV_READ | EV_PERSIST, &handle_log, ibuf);
277 event_add(&inlog, NULL);
280 if (pledge("stdio", NULL) == -1)
294 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, p) == -1)
295 err(1, "socketpair");
303 setproctitle("logger");
304 imsg_init(&child_ibuf, p[1]);
306 _exit(logger_main(p[1], &child_ibuf));
310 imsg_init(&parent_ibuf, p[0]);