chiark / gitweb /
Properly log fatal errors encountered after program startup.
authorMark Wooding <mdw@distorted.org.uk>
Sat, 13 Feb 2016 19:33:40 +0000 (19:33 +0000)
committerMark Wooding <mdw@distorted.org.uk>
Thu, 18 Feb 2016 15:56:13 +0000 (15:56 +0000)
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
yaid.c
yaid.h

diff --git a/linux.c b/linux.c
index ecd43a1530800ffd5d6d70dc7216f764f71c7c0a..571b179b2bf09559b15139a24635e197eadc2c87 100644 (file)
--- 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 d2244b61b218fda4e5f1a63e33ae5a699e663bdd..7de374dd6cd1f546f46f1e441c2898b99f9d819b 100644 (file)
--- 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 a148743e8315eefb07656df49f5c54a2c9ac0373..eb874cbb1ed9da919554949d862355f748ab4265 100644 (file)
--- 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.