chiark / gitweb /
yaid.c: Use `socklen_t' as appropriate.
[yaid] / yaid.c
diff --git a/yaid.c b/yaid.c
index da015e128f78d54be6a90913976507b6baf55889..d0d917aa2e4b8eab6bfc1f29c36c6bfee554cde0 100644 (file)
--- a/yaid.c
+++ b/yaid.c
@@ -88,7 +88,6 @@ static fwatch polfw;                  /* Watch policy file for changes */
 
 static unsigned char tokenbuf[4096];   /* Random-ish data for tokens */
 static size_t tokenptr = sizeof(tokenbuf); /* Current read position */
-static int randfd;                     /* File descriptor for random data */
 
 static struct client *dead_clients = 0;        /* List of defunct clients */
 static struct proxy *dead_proxies = 0; /* List of defunct proxies */
@@ -223,26 +222,21 @@ static void init_writebuf(struct writebuf *wb,
 
 /*----- General utilities -------------------------------------------------*/
 
-/* Format and log MSG somewhere sensible, at the syslog(3) priority PRIO.
- * Prefix it with a description of the query Q, if non-null.
- */
-void logmsg(const struct query *q, int prio, const char *msg, ...)
+static void vlogmsg(const struct query *q, int prio,
+                   const char *msg, va_list *ap)
 {
-  va_list ap;
   dstr d = DSTR_INIT;
   time_t t;
   struct tm *tm;
   char buf[64];
 
-  va_start(ap, msg);
   if (q) {
     dputsock(&d, q->ao, &q->s[L]);
     dstr_puts(&d, " <-> ");
     dputsock(&d, q->ao, &q->s[R]);
     dstr_puts(&d, ": ");
   }
-  dstr_vputf(&d, msg, &ap);
-  va_end(ap);
+  dstr_vputf(&d, msg, ap);
 
   if (!(flags & F_RUNNING))
     moan("%s", d.buf);
@@ -258,6 +252,29 @@ void logmsg(const struct query *q, int prio, const char *msg, ...)
   dstr_destroy(&d);
 }
 
+/* Format and log MSG somewhere sensible, at the syslog(3) priority PRIO.
+ * Prefix it with a description of the query Q, if non-null.
+ */
+void logmsg(const struct query *q, int prio, const char *msg, ...)
+{
+  va_list ap;
+
+  va_start(ap, msg);
+  vlogmsg(q, prio, msg, &ap);
+  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.
  */
@@ -626,8 +643,7 @@ static void user_token(char *p)
    * from the kernel.
    */
   if (tokenptr + TOKENRANDSZ >= sizeof(tokenbuf)) {
-    if (read(randfd, tokenbuf, sizeof(tokenbuf)) < sizeof(tokenbuf))
-      die(1, "unexpected short read or error from `/dev/urandom'");
+    fill_random(tokenbuf, sizeof(tokenbuf));
     tokenptr = 0;
   }
 
@@ -845,7 +861,7 @@ static void accept_client(int fd, unsigned mode, void *p)
   struct listen *l = p;
   struct client *c;
   struct sockaddr_storage ssr, ssl;
-  size_t ssz = sizeof(ssr);
+  socklen_t ssz = sizeof(ssr);
   int sk;
 
   /* Accept the new connection. */
@@ -1092,12 +1108,6 @@ int main(int argc, char *argv[])
   if (load_policy_file(policyfile, &policy))
     exit(1);
 
-  /* Open the random data source. */
-  if ((randfd = open("/dev/urandom", O_RDONLY)) < 0) {
-    die(1, "failed to open `/dev/urandom' for reading: %s",
-       strerror(errno));
-  }
-
   /* Set up the I/O event system. */
   sel_init(&sel);