commit 8a50fc0330f50c1c0c5fc0b541e0b8a19f900ea5 from: Omar Polo date: Wed Jul 07 09:18:24 2021 UTC initialize the logger early Initialize the logger as soon as possible and log by default to stderr. With this, some (common?) errors are printed early instead of ending up in syslog. # NB: this is in configless mode % ./gmid -p 80 [2021-07-07 11:05:57] bind: Address already in use % ./gmid -p 81 [2021-07-07 11:13:53] bind: Permission denied % commit - d336aeafd4b848f3d6f3711e8d233d8b75ce297b commit + 8a50fc0330f50c1c0c5fc0b541e0b8a19f900ea5 blob - 8a2a9401ac5517b38c92199114a0d13d64f3774c blob + ba1948bf6999d716cff5b5b172e2652e6bd2385b --- gmid.c +++ gmid.c @@ -526,9 +526,6 @@ 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); @@ -542,6 +539,7 @@ main(int argc, char **argv) int ch, conftest = 0, configless = 0; int pidfd, old_ipv6, old_port; + logger_init(); init_config(); while ((ch = getopt_long(argc, argv, opts, longopts, NULL)) != -1) { @@ -557,7 +555,8 @@ main(int argc, char **argv) case 'D': if (cmdline_symset(optarg) == -1) - errx(1, "invalid macro: %s", optarg); + fatal("could not parse macro definition: %s", + optarg); break; case 'd': @@ -623,7 +622,7 @@ main(int argc, char **argv) } if (config_path != NULL && (argc > 0 || configless)) - errx(1, "can't specify options in config mode."); + fatal("can't specify options in config mode."); if (conftest) { parse_conf(config_path); @@ -632,15 +631,17 @@ main(int argc, char **argv) } if (!conf.foreground && !configless) { + /* log to syslog */ + imsg_compose(&logibuf, IMSG_LOG_TYPE, 0, 0, -1, NULL, 0); + imsg_flush(&logibuf); + if (daemon(1, 1) == -1) - err(1, "daemon"); + fatal("daemon: %s", strerror(errno)); } if (config_path != NULL) parse_conf(config_path); - logger_init(); - sock4 = make_socket(conf.port, AF_INET); sock6 = -1; if (conf.ipv6) @@ -654,11 +655,6 @@ 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 - 9d0e2675a689e81a5da8690d6e69a410481aed33 blob + e67ae7c2f4b206da17a4898a6f034ba19a9717ee --- log.c +++ log.c @@ -299,11 +299,11 @@ handle_imsg_log(struct imsgbuf *ibuf, struct imsg *ims static void handle_imsg_log_type(struct imsgbuf *ibuf, struct imsg *imsg, size_t datalen) { - if (log != NULL) { + if (log != NULL && log != stderr) { fflush(log); fclose(log); - log = NULL; } + log = NULL; if (imsg->fd != -1) { if ((log = fdopen(imsg->fd, "a")) == NULL) { @@ -324,6 +324,8 @@ handle_dispatch_imsg(int fd, short ev, void *d) int logger_main(int fd, struct imsgbuf *ibuf) { + log = stderr; + event_init(); event_set(&imsgev, fd, EV_READ | EV_PERSIST, &handle_dispatch_imsg, ibuf);