From 429dcd8d6a215be133a0b587b69516197922b9ff Mon Sep 17 00:00:00 2001 Message-Id: <429dcd8d6a215be133a0b587b69516197922b9ff.1714632328.git.mdw@distorted.org.uk> From: Mark Wooding Date: Sat, 13 Feb 2016 19:33:40 +0000 Subject: [PATCH] Properly log fatal errors encountered after program startup. Organization: Straylight/Edgeware From: Mark Wooding The code used to use mLib's `die', which reports to stderr, but we ought properly to report errors to syslog if that's been requested. So introduce and use a new function `fatal' which does the right thing, and use it in the places where it's necessary. --- linux.c | 10 +++++----- yaid.c | 11 +++++++++++ yaid.h | 3 +++ 3 files changed, 19 insertions(+), 5 deletions(-) diff --git a/linux.c b/linux.c index ecd43a1..571b179 100644 --- a/linux.c +++ b/linux.c @@ -47,8 +47,8 @@ void fill_random(void *p, size_t sz) ssize_t n; n = read(randfd, p, sz); - if (n < 0) die(1, "error reading `/dev/urandom': %s", strerror(errno)); - else if (n < sz) die(1, "unexpected short read from `/dev/urandom'"); + if (n < 0) fatal("error reading `/dev/urandom': %s", strerror(errno)); + else if (n < sz) fatal("unexpected short read from `/dev/urandom'"); } /*----- Address-type operations -------------------------------------------*/ @@ -118,7 +118,7 @@ static int get_default_gw(int af, union addr *a) /* Open a netlink socket for interrogating the kernel. */ if ((fd = socket(AF_NETLINK, SOCK_DGRAM, NETLINK_ROUTE)) < 0) - die(1, "failed to create netlink socket: %s", strerror(errno)); + fatal("failed to create netlink socket: %s", strerror(errno)); /* We want to read the routing table. There doesn't seem to be a good way * to do this without just crawling through the whole thing. @@ -135,14 +135,14 @@ static int get_default_gw(int af, union addr *a) rtgen->rtgen_family = af; if (write(fd, nlmsg, nlmsg->nlmsg_len) < 0) - die(1, "failed to send RTM_GETROUTE request: %s", strerror(errno)); + fatal("failed to send RTM_GETROUTE request: %s", strerror(errno)); /* Now we try to parse the answer. */ for (;;) { /* Not finished yet, so read another chunk of answer. */ if ((n = read(fd, buf, sizeof(buf))) < 0) - die(1, "failed to read RTM_GETROUTE response: %s", strerror(errno)); + fatal("failed to read RTM_GETROUTE response: %s", strerror(errno)); /* Start at the beginning of the response. */ nlmsg = (struct nlmsghdr *)buf; diff --git a/yaid.c b/yaid.c index d2244b6..7de374d 100644 --- a/yaid.c +++ b/yaid.c @@ -264,6 +264,17 @@ void logmsg(const struct query *q, int prio, const char *msg, ...) va_end(ap); } +/* Format and report MSG as a fatal error, and exit. */ +void fatal(const char *msg, ...) +{ + va_list ap; + + va_start(ap, msg); + vlogmsg(0, LOG_CRIT, msg, &ap); + va_end(ap); + exit(1); +} + /* Fix up a socket FD so that it won't bite us. Returns zero on success, or * nonzero on error. */ diff --git a/yaid.h b/yaid.h index a148743..eb874cb 100644 --- a/yaid.h +++ b/yaid.h @@ -252,6 +252,9 @@ struct query { extern void PRINTF_LIKE(3, 4) logmsg(const struct query */*q*/, int /*prio*/, const char */*msg*/, ...); +/* Format and report MSG as a fatal error, and exit. */ +extern void PRINTF_LIKE(1, 2) fatal(const char */*msg*/, ...); + /*----- System-specific connection identification code --------------------*/ /* Find out who is responsible for the connection described in the query Q. -- [mdw]