commit e952c5052a0c524eee6d8151b1af96ce2c94ca18 from: Omar Polo date: Tue Jun 15 08:06:10 2021 UTC allow sending fd to log on to the logger process the logger process now can receive a file descriptor to write logs to. At the moment the logic is simple, if it receives a file it logs there, otherwise it logs to syslog. This will allow to log on custom log files. commit - 0f2124e29194b42209e879a539e35d295f525311 commit + e952c5052a0c524eee6d8151b1af96ce2c94ca18 blob - 88c9e001db0e87985040651c7e8569076530e08c blob + bbfb30857b1c4614cef1947cf1fc26d9317562b4 --- gmid.c +++ gmid.c @@ -507,8 +507,12 @@ setup_configless(int argc, char **argv, const char *cg loc = xcalloc(1, sizeof(*loc)); TAILQ_INSERT_HEAD(&host->locations, loc, locations); + + imsg_compose(&logibuf, IMSG_LOG_TYPE, 0, 0, 2, NULL, 0); + imsg_flush(&logibuf); serve(argc, argv, NULL); + imsg_compose(&logibuf, IMSG_QUIT, 0, 0, -1, NULL, 0); imsg_flush(&logibuf); } @@ -623,6 +627,11 @@ main(int argc, char **argv) return 0; } + if (conf.foreground) { + imsg_compose(&logibuf, IMSG_LOG_TYPE, 0, 0, 2, NULL, 0); + imsg_flush(&logibuf); + } + pidfd = write_pidfile(pidfile); /* Linux seems to call the event handlers even when we're blob - ee4152f49c22ead2eb68e376f4fdabf97cfd554e blob + a013af503ead875842509f3a46fd4b5c20e07ea1 --- gmid.h +++ gmid.h @@ -296,6 +296,7 @@ enum imsg_type { IMSG_FCGI_REQ, IMSG_FCGI_FD, IMSG_LOG, + IMSG_LOG_TYPE, IMSG_QUIT, }; blob - df6073283720bca9656ea4f9ae78a5b0a3e22555 blob + 0668bd492138ee3d707ecba2846ea14aa07ca728 --- log.c +++ log.c @@ -32,17 +32,21 @@ static struct event imsgev; +static FILE *log; + static void handle_imsg_quit(struct imsgbuf*, struct imsg*, size_t); static void handle_imsg_log(struct imsgbuf*, struct imsg*, size_t); +static void handle_imsg_log_type(struct imsgbuf*, struct imsg*, size_t); static void handle_dispatch_imsg(int, short, void*); static imsg_handlerfn *handlers[] = { [IMSG_QUIT] = handle_imsg_quit, [IMSG_LOG] = handle_imsg_log, + [IMSG_LOG_TYPE] = handle_imsg_log_type, }; static inline void -print_date(void) +print_date(FILE *f) { struct tm tminfo; time_t t; @@ -51,7 +55,7 @@ print_date(void) time(&t); strftime(buf, sizeof(buf), "%F %T", localtime_r(&t, &tminfo)); - fprintf(stderr, "[%s] ", buf); + fprintf(f, "[%s] ", buf); } static inline int @@ -255,6 +259,26 @@ log_request(struct client *c, char *meta, size_t l) static void +do_log(int priority, const char *msg) +{ + int quit = 0; + + if (priority == LOG_CRIT) { + quit = 1; + priority = LOG_ERR; + } + + if (log != NULL) { + print_date(log); + fprintf(log, "%s\n", msg); + } else + syslog(LOG_DAEMON | priority, "%s", msg); + + if (quit) + exit(1); +} + +static void handle_imsg_quit(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen) { event_loopbreak(); @@ -268,23 +292,26 @@ handle_imsg_log(struct imsgbuf *ibuf, struct imsg *ims msg = imsg->data; msg[datalen-1] = '\0'; - priority = imsg->hdr.peerid; + do_log(priority, msg); +} - quit = 0; - if (priority == LOG_CRIT) { - quit = 1; - priority = LOG_ERR; +static void +handle_imsg_log_type(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen) +{ + if (log != NULL) { + fflush(log); + fclose(log); + log = NULL; } - if (conf.foreground) { - print_date(); - fprintf(stderr, "%s\n", msg); - } else - syslog(LOG_DAEMON | priority, "%s", msg); - - if (quit) - exit(1); + if (imsg->fd != -1) { + if ((log = fdopen(imsg->fd, "a")) == NULL) { + syslog(LOG_DAEMON | LOG_ERR, "fdopen: %s", + strerror(errno)); + exit(1); + } + } } static void blob - d2236d7ff967a489d0306a956a1e79dddedc95f5 blob + 345edae55b2e1b7f50457714005a20c700d0106a --- sandbox.c +++ sandbox.c @@ -344,7 +344,7 @@ sandbox_executor_process(void) void sandbox_logger_process(void) { - if (pledge("stdio", NULL) == -1) + if (pledge("stdio recvfd", NULL) == -1) err(1, "pledge"); }