commit df5058c919cbd1538d0a04cb2a4c179c0291566f from: Omar Polo date: Mon Jun 05 17:07:52 2023 UTC provide a more usual fatal fatal usually appends the error string. Add 'fatalx' that doesn't. Fix callers and move the prototypes to log.h commit - a01a91db06a943ef0cc8fbb7294786814a63b65c commit + df5058c919cbd1538d0a04cb2a4c179c0291566f blob - 2d1dc86cd84eeabfdb308272bb45fb5b97b0f597 blob + 502b994aa5a45f857935587e6708d3e31dd65196 --- fcgi.c +++ fcgi.c @@ -19,6 +19,8 @@ #include #include #include + +#include "log.h" /* * Sometimes it can be useful to inspect the fastcgi traffic as @@ -355,8 +357,7 @@ fcgi_req(struct client *c) NULL, 0, NI_NUMERICHOST); if (e != 0) - fatal("getnameinfo failed: %s (%s)", - gai_strerror(e), strerror(errno)); + fatalx("getnameinfo failed: %s", gai_strerror(e)); fcgi_begin_request(c->cgibev); fcgi_send_param(c->cgibev, "GATEWAY_INTERFACE", "CGI/1.1"); blob - 5681ee2d5b65838617fc49d0275cd8bd132d7774 blob + 4ccbbdc0cd482bea2364d9b2181d0399b12eb14d --- ge.c +++ ge.c @@ -27,6 +27,8 @@ #include #include +#include "log.h" + struct imsgbuf ibuf, logibuf; struct conf conf; @@ -86,7 +88,7 @@ mkdirs(const char *path, mode_t mode) return; mkdirs(dname, mode); if (mkdir(path, mode) != 0 && errno != EEXIST) - fatal("can't mkdir %s: %s", path, strerror(errno)); + fatal("can't mkdir %s", path); } /* $XDG_DATA_HOME/gmid */ @@ -150,7 +152,7 @@ serve(const char *host, int port, const char *dir, str hints.ai_flags = AI_PASSIVE; error = getaddrinfo(host, service, &hints, &res0); if (error) - fatal("%s", gai_strerror(error)); + fatalx("%s", gai_strerror(error)); for (res = res0; res; res = res->ai_next) { sock = socket(res->ai_family, res->ai_socktype, res->ai_protocol); @@ -223,7 +225,8 @@ main(int argc, char **argv) case 'p': conf.port = strtonum(optarg, 0, UINT16_MAX, &errstr); if (errstr) - fatal("port number is %s: %s", errstr, optarg); + fatalx("port number is %s: %s", errstr, + optarg); break; case 'V': puts("Version: " GE_STRING); @@ -276,21 +279,21 @@ main(int argc, char **argv) /* setup tls */ if ((tlsconf = tls_config_new()) == NULL) - fatal("tls_config_new"); /* XXX: fatalx */ + fatal("tls_config_new"); /* optionally accept client certs but don't try to verify them */ tls_config_verify_client_optional(tlsconf); tls_config_insecure_noverifycert(tlsconf); if ((ctx = tls_server()) == NULL) - fatal("tls_server failure"); /* XXX: fatalx */ + fatal("tls_server failure"); if (tls_config_set_keypair_file(tlsconf, host->cert, host->key)) - fatal("can't load the keypair (%s, %s)", - host->cert, host->key); + fatalx("can't load the keypair (%s, %s): %s", + host->cert, host->key, tls_config_error(tlsconf)); if (tls_configure(ctx, tlsconf) == -1) - fatal("tls_configure: %s", tls_error(ctx)); + fatalx("tls_configure: %s", tls_error(ctx)); /* start the server */ signal(SIGPIPE, SIG_IGN); blob - a79ad60f00bc40923cf88bb881e8514d66183a59 blob + c7f3ed30ad9bd89825f289194b305268e8c706b2 --- gmid.c +++ gmid.c @@ -28,6 +28,8 @@ #include #include +#include "log.h" + static const char *opts = "D:df:hnP:Vv"; static const struct option longopts[] = { @@ -92,23 +94,23 @@ make_socket(int port, int family) } if ((sock = socket(family, SOCK_STREAM, 0)) == -1) - fatal("socket: %s", strerror(errno)); + fatal("socket"); v = 1; if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)) == -1) - fatal("setsockopt(SO_REUSEADDR): %s", strerror(errno)); + fatal("setsockopt(SO_REUSEADDR)"); v = 1; if (setsockopt(sock, SOL_SOCKET, SO_REUSEPORT, &v, sizeof(v)) == -1) - fatal("setsockopt(SO_REUSEPORT): %s", strerror(errno)); + fatal("setsockopt(SO_REUSEPORT)"); mark_nonblock(sock); if (bind(sock, addr, len) == -1) - fatal("bind: %s", strerror(errno)); + fatal("bind"); if (listen(sock, 16) == -1) - fatal("listen: %s", strerror(errno)); + fatal("listen"); return sock; } @@ -118,13 +120,14 @@ add_keypair(struct vhost *h) { if (*h->ocsp == '\0') { if (tls_config_add_keypair_file(tlsconf, h->cert, h->key) == -1) - fatal("failed to load the keypair (%s, %s)", - h->cert, h->key); + fatalx("failed to load the keypair (%s, %s): %s", + h->cert, h->key, tls_config_error(tlsconf)); } else { if (tls_config_add_keypair_ocsp_file(tlsconf, h->cert, h->key, h->ocsp) == -1) - fatal("failed to load the keypair (%s, %s, %s)", - h->cert, h->key, h->ocsp); + fatalx("failed to load the keypair (%s, %s, %s): %s", + h->cert, h->key, h->ocsp, + tls_config_error(tlsconf)); } } @@ -141,7 +144,8 @@ setup_tls(void) tls_config_insecure_noverifycert(tlsconf); if (tls_config_set_protocols(tlsconf, conf.protos) == -1) - fatal("tls_config_set_protocols"); + fatalx("tls_config_set_protocols: %s", + tls_config_error(tlsconf)); if ((ctx = tls_server()) == NULL) fatal("tls_server failure"); @@ -150,20 +154,20 @@ setup_tls(void) /* we need to set something, then we can add how many key we want */ if (tls_config_set_keypair_file(tlsconf, h->cert, h->key)) - fatal("tls_config_set_keypair_file failed for (%s, %s)", - h->cert, h->key); + fatalx("tls_config_set_keypair_file failed for (%s, %s): %s", + h->cert, h->key, tls_config_error(tlsconf)); /* same for OCSP */ if (*h->ocsp != '\0' && tls_config_set_ocsp_staple_file(tlsconf, h->ocsp) == -1) - fatal("tls_config_set_ocsp_staple_file failed for (%s)", - h->ocsp); + fatalx("tls_config_set_ocsp_staple_file failed for (%s): %s", + h->ocsp, tls_config_error(tlsconf)); while ((h = TAILQ_NEXT(h, vhosts)) != NULL) add_keypair(h); if (tls_configure(ctx, tlsconf) == -1) - fatal("tls_configure: %s", tls_error(ctx)); + fatalx("tls_configure: %s", tls_error(ctx)); } void @@ -255,16 +259,16 @@ drop_priv(void) struct passwd *pw = NULL; if (*conf.chroot != '\0' && *conf.user == '\0') - fatal("can't chroot without an user to switch to after."); + fatalx("can't chroot without an user to switch to after."); if (*conf.user != '\0') { if ((pw = getpwnam(conf.user)) == NULL) - fatal("can't find user %s", conf.user); + fatalx("can't find user %s", conf.user); } if (*conf.chroot != '\0') { if (chroot(conf.chroot) != 0 || chdir("/") != 0) - fatal("%s: %s", conf.chroot, strerror(errno)); + fatal("%s", conf.chroot); } if (pw != NULL) { @@ -325,11 +329,11 @@ serve(void) for (i = 0; i < conf.prefork; ++i) { if (socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, PF_UNSPEC, p) == -1) - fatal("socketpair: %s", strerror(errno)); + fatal("socketpair"); switch (fork()) { case -1: - fatal("fork: %s", strerror(errno)); + fatal("fork"); case 0: /* child */ close(p[0]); imsg_init(&servibuf[i], p[1]); @@ -352,7 +356,7 @@ write_pidfile(const char *pidfile) return -1; if ((fd = open(pidfile, O_WRONLY|O_CREAT|O_CLOEXEC, 0600)) == -1) - fatal("can't open pidfile %s: %s", pidfile, strerror(errno)); + fatal("can't open pidfile %s", pidfile); lock.l_start = 0; lock.l_len = 0; @@ -360,10 +364,10 @@ write_pidfile(const char *pidfile) lock.l_whence = SEEK_SET; if (fcntl(fd, F_SETLK, &lock) == -1) - fatal("can't lock %s, gmid is already running?", pidfile); + fatalx("can't lock %s, gmid is already running?", pidfile); if (ftruncate(fd, 0) == -1) - fatal("ftruncate: %s: %s", pidfile, strerror(errno)); + fatal("ftruncate %s", pidfile); dprintf(fd, "%d\n", getpid()); @@ -383,7 +387,7 @@ main(int argc, char **argv) switch (ch) { case 'D': if (cmdline_symset(optarg) == -1) - fatal("could not parse macro definition: %s", + fatalx("could not parse macro definition: %s", optarg); break; case 'd': @@ -433,7 +437,7 @@ main(int argc, char **argv) imsg_flush(&logibuf); if (daemon(1, 1) == -1) - fatal("daemon: %s", strerror(errno)); + fatal("daemon"); } sock4 = make_socket(conf.port, AF_INET); blob - a0a44a38910a6ae12758345757b62f32b298041f blob + e2a0128c08d8ba4f9708c9b559097668f0775c0d --- gmid.h +++ gmid.h @@ -308,18 +308,6 @@ void print_conf(void); int cmdline_symset(char *); /* log.c */ -void fatal(const char*, ...) - __attribute__((format (printf, 1, 2))) - __attribute__((__noreturn__)); - -#define LOG_ATTR_FMT __attribute__((format (printf, 2, 3))) -void log_err(struct client*, const char*, ...) LOG_ATTR_FMT; -void log_warn(struct client*, const char*, ...) LOG_ATTR_FMT; -void log_notice(struct client*, const char*, ...) LOG_ATTR_FMT; -void log_info(struct client*, const char*, ...) LOG_ATTR_FMT; -void log_debug(struct client*, const char*, ...) LOG_ATTR_FMT; -void log_request(struct client*, char*, size_t); -int logger_main(int, struct imsgbuf*); /* mime.c */ void init_mime(struct mime*); blob - 049c1cab5c75014495d1a085ffcc3f51cc8a0f3b blob + 2b842240c42dbf2d5835ae49426ca89c3c509a0c --- log.c +++ log.c @@ -30,6 +30,8 @@ #include #include +#include "log.h" + static struct event imsgev; static FILE *log; @@ -85,28 +87,58 @@ send_log(int type, int priority, const char *msg, size imsg_flush(&logibuf); } -void -fatal(const char *fmt, ...) +static __dead void +fatal_impl(int use_err, const char *fmt, va_list ap) { struct pollfd pfd; - va_list ap; - int r; - char *fmted; + char *str, *tmp; + int r, t, err; - va_start(ap, fmt); - if ((r = vasprintf(&fmted, fmt, ap)) != -1) { - send_log(IMSG_LOG, LOG_CRIT, fmted, r+1); - free(fmted); + err = errno; - /* wait for the logger process to shut down */ - pfd.fd = logibuf.fd; - pfd.events = POLLIN; - poll(&pfd, 1, 1000); - } - va_end(ap); + if ((r = vasprintf(&str, fmt, ap)) != -1) { + if (use_err && + (t = asprintf(&tmp, "%s: %s", str, strerror(err))) != + -1) { + free(str); + str = tmp; + r = t; + } + } else + str = NULL, r = 0; + + send_log(IMSG_LOG, LOG_CRIT, str, r); + free(str); + + /* wait for the logger process to shut down */ + memset(&pfd, 0, sizeof(pfd)); + pfd.fd = logibuf.fd; + pfd.events = POLLIN; + poll(&pfd, 1, 1000); + exit(1); } +void __dead +fatal(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + fatal_impl(1, fmt, ap); + va_end(ap); +} + +void __dead +fatalx(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + fatal_impl(0, fmt, ap); + va_end(ap); +} + static inline void vlog(int priority, struct client *c, const char *fmt, va_list ap) @@ -126,8 +158,7 @@ vlog(int priority, struct client *c, sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV); if (ec != 0) - fatal("getnameinfo: %s: %s", - gai_strerror(ec), strerror(errno)); + fatal("getnameinfo: %s", gai_strerror(ec)); } if (vasprintf(&fmted, fmt, ap) == -1) @@ -224,7 +255,7 @@ log_request(struct client *c, char *meta, size_t l) sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV); if (ec != 0) - fatal("getnameinfo: %s", gai_strerror(ec)); + fatalx("getnameinfo: %s", gai_strerror(ec)); if (c->iri.schema != NULL) { /* serialize the IRI */ blob - 1586cf8944d2df4b4ac5f54a3c5aad03db82f13a blob + 9197c8b131e1e9c670c9d7bf13635eaa8b9b1919 --- mime.c +++ mime.c @@ -20,6 +20,8 @@ #include #include +#include "log.h" + void init_mime(struct mime *mime) { @@ -28,7 +30,7 @@ init_mime(struct mime *mime) mime->t = calloc(mime->cap, sizeof(struct etm)); if (mime->t == NULL) - fatal("calloc: %s", strerror(errno)); + fatal("calloc"); } /* register mime for the given extension */ blob - /dev/null blob + 842bbe5d1c365030f23efd658e2ed36ce8f00d53 (mode 644) --- /dev/null +++ log.h @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2023 Omar Polo + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +void fatal(const char *, ...) + __attribute__((format (printf, 1, 2))) + __attribute__((__noreturn__)); +void fatalx(const char *, ...) + __attribute__((format (printf, 1, 2))) + __attribute__((__noreturn__)); + +#define LOG_ATTR_FMT __attribute__((format (printf, 2, 3))) +void log_err(struct client *, const char *, ...) LOG_ATTR_FMT; +void log_warn(struct client *, const char *, ...) LOG_ATTR_FMT; +void log_warnx(struct client *, const char *, ...) LOG_ATTR_FMT; +void log_notice(struct client *, const char *, ...) LOG_ATTR_FMT; +void log_info(struct client *, const char *, ...) LOG_ATTR_FMT; +void log_debug(struct client *, const char *, ...) LOG_ATTR_FMT; +void log_request(struct client *, char *, size_t); +int logger_main(int, struct imsgbuf *); blob - f4d770d69c1a08115598847a8480881c29d9badd blob + a7a17fc07f058fb415d8c078ab09a35159c11406 --- parse.y +++ parse.y @@ -32,6 +32,8 @@ #include #include #include + +#include "log.h" TAILQ_HEAD(files, file) files = TAILQ_HEAD_INITIALIZER(files); static struct file { blob - 0ac83d6ea4ec78bc22aa8787d5c9551ff2dba7d9 blob + 3e5fa347a81fa6b0904dcbb64ae65169eec1d971 --- proxy.c +++ proxy.c @@ -19,6 +19,8 @@ #include #include #include + +#include "log.h" #define MIN(a, b) ((a) < (b) ? (a) : (b)) @@ -240,7 +242,7 @@ proxy_enqueue_req(struct client *c) c->proxybev = bufferevent_new(c->pfd, proxy_read, proxy_write, proxy_error, c); if (c->proxybev == NULL) - fatal("can't allocate bufferevent: %s", strerror(errno)); + fatal("can't allocate bufferevent"); if (!p->notls) { event_set(&c->proxybev->ev_read, c->pfd, EV_READ, blob - 1e95b8e6d6ea2b8422d7d24ca72dc41b35c2792f blob + d0d05b1dfd036d3fcbed464ed83a602baa48dba1 --- sandbox.c +++ sandbox.c @@ -15,6 +15,7 @@ */ #include "gmid.h" +#include "log.h" #if defined(__OpenBSD__) blob - 083b5c2767cd31ffcd2a93844e3a0c04bd76da14 blob + 337318272840745460077e5075f3ba1e0acb5df6 --- server.c +++ server.c @@ -28,6 +28,8 @@ #include #include +#include "log.h" + #define MIN(a, b) ((a) < (b) ? (a) : (b)) int shutting_down; @@ -367,9 +369,9 @@ mark_nonblock(int fd) int flags; if ((flags = fcntl(fd, F_GETFL)) == -1) - fatal("fcntl(F_GETFL): %s", strerror(errno)); + fatal("fcntl(F_GETFL)"); if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1) - fatal("fcntl(F_SETFL): %s", strerror(errno)); + fatal("fcntl(F_SETFL)"); } static void @@ -399,8 +401,7 @@ handle_handshake(int fd, short ev, void *d) c->bev = bufferevent_new(fd, client_read, client_write, client_error, c); if (c->bev == NULL) - fatal("%s: failed to allocate client buffer: %s", - __func__, strerror(errno)); + fatal("%s: failed to allocate client buffer", __func__); event_set(&c->bev->ev_read, c->fd, EV_READ, client_tls_readcb, c->bev); @@ -504,7 +505,7 @@ fmt_sbuf(const char *fmt, struct client *c, const char strlcat(c->sbuf, c->domain, sizeof(c->sbuf)); break; default: - fatal("%s: unknown fmt specifier %c", + fatalx("%s: unknown fmt specifier %c", __func__, *fmt); } } @@ -1283,7 +1284,7 @@ do_accept(int sock, short et, void *d) if (errno == EWOULDBLOCK || errno == EAGAIN || errno == ECONNABORTED) return; - fatal("accept: %s", strerror(errno)); + fatal("accept"); } mark_nonblock(fd); @@ -1329,7 +1330,7 @@ handle_dispatch_imsg(int fd, short ev, void *d) } if (n == 0) - fatal("connection closed."); /* XXX: fatalx */ + fatalx("connection closed."); for (;;) { if ((n = imsg_get(ibuf, &imsg)) == -1) @@ -1354,8 +1355,7 @@ handle_dispatch_imsg(int fd, short ev, void *d) signal_del(&sigusr2); break; default: - /* XXX: fatalx */ - fatal("Unknown message %d", imsg.hdr.type); + fatalx("Unknown message %d", imsg.hdr.type); } imsg_free(&imsg); } @@ -1416,8 +1416,8 @@ load_vhosts(void) continue; l->dirfd = open(l->dir, O_RDONLY | O_DIRECTORY); if (l->dirfd == -1) - fatal("open %s for domain %s: %s", l->dir, - h->domain, strerror(errno)); + fatal("open %s for domain %s", l->dir, + h->domain); } } } @@ -1427,7 +1427,7 @@ server_main(struct tls *ctx_, struct imsgbuf *ibuf, in { drop_priv(); if (load_default_mime(&conf.mime) == -1) - fatal("can't load default mime: %s", strerror(errno)); + fatal("can't load default mime"); sort_mime(&conf.mime); load_vhosts(); loop(ctx_, sock4, sock6, ibuf); blob - f82e6ce0c2b9c46daa9335e06ec7a53a03742b14 blob + 7f9e74b0363de49ec1a83d74b08575c5fb0cdfe8 --- utils.c +++ utils.c @@ -24,6 +24,8 @@ #include #include +#include "log.h" + int starts_with(const char *str, const char *prefix) { @@ -122,23 +124,23 @@ gen_certificate(const char *hostname, const char *cert host); if ((pkey = EVP_PKEY_new()) == NULL) - fatal("couldn't create a new private key"); + fatalx("couldn't create a new private key"); if ((rsa = RSA_new()) == NULL) - fatal("couldn't generate rsa"); + fatalx("couldn't generate rsa"); if ((e = BN_new()) == NULL) - fatal("couldn't allocate a bignum"); + fatalx("couldn't allocate a bignum"); BN_set_word(e, RSA_F4); if (!RSA_generate_key_ex(rsa, 4096, e, NULL)) - fatal("couldn't generate a rsa key"); + fatalx("couldn't generate a rsa key"); if (!EVP_PKEY_assign_RSA(pkey, rsa)) - fatal("couldn't assign the key"); + fatalx("couldn't assign the key"); if ((x509 = X509_new()) == NULL) - fatal("couldn't generate the X509 certificate"); + fatalx("couldn't generate the X509 certificate"); ASN1_INTEGER_set(X509_get_serialNumber(x509), 0); X509_gmtime_adj(X509_get_notBefore(x509), 0); @@ -146,26 +148,26 @@ gen_certificate(const char *hostname, const char *cert X509_set_version(x509, 3); if (!X509_set_pubkey(x509, pkey)) - fatal("couldn't set the public key"); + fatalx("couldn't set the public key"); name = X509_get_subject_name(x509); if (!X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC, host, -1, -1, 0)) - fatal("couldn't add CN to cert"); + fatalx("couldn't add CN to cert"); X509_set_issuer_name(x509, name); if (!X509_sign(x509, pkey, EVP_sha256())) - fatal("couldn't sign the certificate"); + fatalx("couldn't sign the certificate"); if ((f = fopen(keypath, "w")) == NULL) - fatal("fopen(%s): %s", keypath, strerror(errno)); + fatal("can't open %s", keypath); if (!PEM_write_PrivateKey(f, pkey, NULL, NULL, 0, NULL, NULL)) - fatal("couldn't write private key"); + fatalx("couldn't write private key"); fclose(f); if ((f = fopen(certpath, "w")) == NULL) - fatal("fopen(%s): %s", certpath, strerror(errno)); + fatal("can't open %s", certpath); if (!PEM_write_X509(f, x509)) - fatal("couldn't write cert"); + fatalx("couldn't write cert"); fclose(f); BN_free(e);