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>
31 static struct imsgbuf parent_ibuf, child_ibuf;
32 static struct event inlog;
35 static void handle_log(int, short, void*);
36 static int logger_main(int, struct imsgbuf*);
39 fatal(const char *fmt, ...)
45 if (conf.foreground) {
46 vfprintf(stderr, fmt, ap);
47 fprintf(stderr, "\n");
49 vsyslog(LOG_DAEMON | LOG_CRIT, fmt, ap);
56 should_log(int priority)
64 return conf.verbose >= 1;
66 return conf.verbose >= 2;
68 return conf.verbose >= 3;
75 send_log(const char *msg, size_t len)
77 imsg_compose(&parent_ibuf, 0, 0, 0, -1, msg, len);
78 /* XXX: use event_once() */
79 imsg_flush(&parent_ibuf);
83 vlog(int priority, struct client *c,
84 const char *fmt, va_list ap)
86 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
91 if (!should_log(priority))
95 len = sizeof(c->addr);
96 ec = getnameinfo((struct sockaddr*)&c->addr, len,
99 NI_NUMERICHOST | NI_NUMERICSERV);
101 fatal("getnameinfo: %s", gai_strerror(ec));
104 if (vasprintf(&fmted, fmt, ap) == -1)
105 fatal("vasprintf: %s", strerror(errno));
108 ec = asprintf(&s, "internal: %s", fmted);
110 ec = asprintf(&s, "%s:%s %s", hbuf, sbuf, fmted);
113 fatal("asprintf: %s", strerror(errno));
122 log_err(struct client *c, const char *fmt, ...)
127 vlog(LOG_ERR, c, fmt, ap);
132 log_warn(struct client *c, const char *fmt, ...)
137 vlog(LOG_WARNING, c, fmt, ap);
142 log_notice(struct client *c, const char *fmt, ...)
147 vlog(LOG_NOTICE, c, fmt, ap);
152 log_info(struct client *c, const char *fmt, ...)
157 vlog(LOG_INFO, c, fmt, ap);
162 log_debug(struct client *c, const char *fmt, ...)
167 vlog(LOG_DEBUG, c, fmt, ap);
171 /* strchr, but with a bound */
173 gmid_strnchr(char *s, int c, size_t len)
177 for (i = 0; i < len; ++i)
184 log_request(struct client *c, char *meta, size_t l)
186 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV], b[GEMINI_URL_LEN];
191 len = sizeof(c->addr);
192 ec = getnameinfo((struct sockaddr*)&c->addr, len,
195 NI_NUMERICHOST | NI_NUMERICSERV);
197 fatal("getnameinfo: %s", gai_strerror(ec));
199 if (c->iri.schema != NULL) {
200 /* serialize the IRI */
201 strlcpy(b, c->iri.schema, sizeof(b));
202 strlcat(b, "://", sizeof(b));
204 /* log the decoded host name, but if it was invalid
205 * use the raw one. */
206 if (*c->domain != '\0')
207 strlcat(b, c->domain, sizeof(b));
209 strlcat(b, c->iri.host, sizeof(b));
211 strlcat(b, "/", sizeof(b));
212 strlcat(b, c->iri.path, sizeof(b)); /* TODO: sanitize UTF8 */
213 if (*c->iri.query != '\0') { /* TODO: sanitize UTF8 */
214 strlcat(b, "?", sizeof(b));
215 strlcat(b, c->iri.query, sizeof(b));
218 strlcpy(b, c->req, sizeof(b));
221 if ((t = gmid_strnchr(meta, '\r', l)) == NULL)
224 ec = asprintf(&fmted, "%s:%s GET %s %.*s", hbuf, sbuf, b,
225 (int)(t-meta), meta);
228 send_log(fmted, ec+1);
235 handle_log(int fd, short ev, void *d)
237 struct imsgbuf *ibuf = d;
242 if ((n = imsg_read(ibuf)) == -1) {
243 if (errno == EAGAIN || errno == EWOULDBLOCK)
248 errx(1, "connection lost?");
251 if ((n = imsg_get(ibuf, &imsg)) == -1)
252 err(1, "read error");
256 datalen = imsg.hdr.len - IMSG_HEADER_SIZE;
260 /* ignore imsg.hdr.type for now */
262 fprintf(stderr, "%s\n", msg);
264 syslog(LOG_DAEMON, "%s", msg);
271 logger_main(int fd, struct imsgbuf *ibuf)
275 event_set(&inlog, fd, EV_READ | EV_PERSIST, &handle_log, ibuf);
276 event_add(&inlog, NULL);
279 if (pledge("stdio", NULL) == -1)
293 if (socketpair(AF_UNIX, SOCK_STREAM, PF_UNSPEC, p) == -1)
294 err(1, "socketpair");
302 setproctitle("logger");
303 imsg_init(&child_ibuf, p[1]);
305 _exit(logger_main(p[1], &child_ibuf));
309 imsg_init(&parent_ibuf, p[0]);