diff options
author | Omar Polo <op@omarpolo.com> | 2021-04-28 12:43:17 +0000 |
---|---|---|
committer | Omar Polo <op@omarpolo.com> | 2021-04-28 12:43:17 +0000 |
commit | 2ef7f631db592b0baf910eb2d05d9fa45f85c671 (patch) | |
tree | 1d3b3652cd54ec4284224496519bde5cb60b8f07 /log.c | |
parent | 48b69cb2dcb0e50409c531d9052c2da134a82eff (diff) |
wait for logger after fatal()
With -f, when the main process exits after a fatal() (usually) the
shell prompt is printed before the logger message.
This adds a small poll to wait for the logger process to exit.
Diffstat (limited to 'log.c')
-rw-r--r-- | log.c | 25 |
1 files changed, 22 insertions, 3 deletions
@@ -23,6 +23,7 @@ #include <event.h> #include <imsg.h> #include <netdb.h> +#include <poll.h> #include <stdarg.h> #include <stdio.h> #include <string.h> @@ -82,14 +83,20 @@ send_log(int priority, const char *msg, size_t len) void fatal(const char *fmt, ...) { + struct pollfd pfd; va_list ap; int r; char *fmted; va_start(ap, fmt); if ((r = vasprintf(&fmted, fmt, ap)) != -1) { - send_log(LOG_ERR, fmted, r+1); + send_log(LOG_CRIT, fmted, r+1); free(fmted); + + /* wait for the logger process to shut down */ + pfd.fd = logibuf.fd; + pfd.events = POLLIN; + poll(&pfd, 1, 1000); } va_end(ap); exit(1); @@ -256,16 +263,28 @@ handle_imsg_quit(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen) static void handle_imsg_log(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen) { - char *msg; + int priority, quit; + char *msg; msg = imsg->data; msg[datalen-1] = '\0'; + priority = imsg->hdr.peerid; + + quit = 0; + if (priority == LOG_CRIT) { + quit = 1; + priority = LOG_ERR; + } + if (conf.foreground) { print_date(); fprintf(stderr, "%s\n", msg); } else - syslog(LOG_DAEMON | imsg->hdr.peerid, "%s", msg); + syslog(LOG_DAEMON | priority, "%s", msg); + + if (quit) + exit(1); } static void |