server/peer.c, server/admin.c: Introduce `p_destroyall'.
[tripe] / server / admin.c
index acdb973..05cd168 100644 (file)
@@ -260,7 +260,9 @@ static void a_flush(int fd, unsigned mode, void *v)
  *
  *               * "?PEER" PEER -- peer's name
  *
- *               * "?ERRNO" ERRNO -- system error code
+ *               * "?ERR" CODE -- system error code
+ *
+ *               * "?ERRNO" -- system error code from @errno@
  *
  *               * "[!]..." ... -- @dstr_putf@-like string as single token
  */
@@ -303,7 +305,11 @@ void a_vformat(dstr *d, const char *fmt, va_list *ap)
        while (*av) u_quotify(d, *av++);
       } else if (strcmp(fmt, "?PEER") == 0)
        u_quotify(d, p_name(va_arg(*ap, peer *)));
-      else if (strcmp(fmt, "?ERRNO") == 0) {
+      else if (strcmp(fmt, "?ERR") == 0) {
+       int e = va_arg(*ap, int);
+       dstr_putf(d, " E%d", e);
+       u_quotify(d, strerror(e));
+      } else if (strcmp(fmt, "?ERRNO") == 0) {
        dstr_putf(d, " E%d", errno);
        u_quotify(d, strerror(errno));
       } else
@@ -558,9 +564,8 @@ void a_notify(const char *fmt, ...)
 
 void a_quit(void)
 {
-  close(sock.fd);
-  if (sockname) unlink(sockname);
-  FOREACH_PEER(p, { p_destroy(p, 1); });
+  a_unlisten();
+  p_destroyall();
   ps_quit();
   exit(0);
 }
@@ -2508,8 +2513,10 @@ void a_listen(const char *name, uid_t u, gid_t g, mode_t m)
   /* --- Set up the socket address --- */
 
   sz = strlen(name) + 1;
-  if (sz > sizeof(sun.sun_path))
-    die(EXIT_FAILURE, "socket name `%s' too long", name);
+  if (sz > sizeof(sun.sun_path)) {
+    a_warn("ADMIN", "admin-socket", "%s", name, "name-too-long", A_END);
+    exit(EXIT_FAILURE);
+  }
   BURN(sun);
   sun.sun_family = AF_UNIX;
   memcpy(sun.sun_path, name, sz);
@@ -2519,47 +2526,68 @@ void a_listen(const char *name, uid_t u, gid_t g, mode_t m)
 
   omask = umask(0077);
 again:
-  if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0)
-    die(EXIT_FAILURE, "couldn't create socket: %s", strerror(errno));
+  if ((fd = socket(PF_UNIX, SOCK_STREAM, 0)) < 0) {
+    a_warn("ADMIN", "admin-socket", "%s", sun.sun_path,
+          "create-failed", "?ERRNO", A_END);
+    exit(EXIT_FAILURE);
+  }
   if (bind(fd, (struct sockaddr *)&sun, sz) < 0) {
     struct stat st;
     int e = errno;
     if (errno != EADDRINUSE) {
-      die(EXIT_FAILURE, "couldn't bind to address `%s': %s",
-         sun.sun_path, strerror(e));
+      a_warn("ADMIN", "admin-socket", "%s", sun.sun_path,
+            "bind-failed", "?ERRNO", A_END);
+      exit(EXIT_FAILURE);
+    }
+    if (!n) {
+      a_warn("ADMIN", "admin-socket", "%s", sun.sun_path,
+            "too-many-retries", A_END);
+      exit(EXIT_FAILURE);
     }
-    if (!n)
-      die(EXIT_FAILURE, "too many retries; giving up");
     n--;
     if (!connect(fd, (struct sockaddr *)&sun, sz)) {
-      die(EXIT_FAILURE, "server already listening on admin socket `%s'",
-         sun.sun_path);
+      a_warn("ADMIN", "admin-socket", "%s", sun.sun_path,
+            "already-in-use", A_END);
+      exit(EXIT_FAILURE);
+    }
+    if (errno != ECONNREFUSED) {
+      a_warn("ADMIN", "admin-socket", "%s", sun.sun_path,
+            "bind-failed", "?ERR", e, A_END);
+      exit(EXIT_FAILURE);
     }
-    if (errno != ECONNREFUSED)
-      die(EXIT_FAILURE, "couldn't bind to address: %s", strerror(e));
     if (stat(sun.sun_path, &st)) {
-      die(EXIT_FAILURE, "couldn't stat `%s': %s",
-         sun.sun_path, strerror(errno));
+      if (errno == ENOENT) { close(fd); goto again; }
+      a_warn("ADMIN", "admin-socket", "%s", sun.sun_path,
+            "stat-failed", "?ERRNO", A_END);
+      exit(EXIT_FAILURE);
+    }
+    if (!S_ISSOCK(st.st_mode)) {
+      a_warn("ADMIN", "admin-socket", "%s", sun.sun_path,
+            "not-a-socket", A_END);
+      exit(EXIT_FAILURE);
     }
-    if (!S_ISSOCK(st.st_mode))
-      die(EXIT_FAILURE, "object `%s' isn't a socket", sun.sun_path);
     T( trace(T_ADMIN, "admin: stale socket found; removing it"); )
     unlink(sun.sun_path);
     close(fd);
     goto again;
   }
   if (chown(sun.sun_path, u, g)) {
-    die(EXIT_FAILURE, "failed to set socket owner: %s",
-       strerror(errno));
+    a_warn("ADMIN", "admin-socket", "%s", sun.sun_path,
+          "chown-failed", "?ERRNO", A_END);
+    exit(EXIT_FAILURE);
   }
   if (chmod(sun.sun_path, m)) {
-    die(EXIT_FAILURE, "failed to set socket permissions: %s",
-       strerror(errno));
+    a_warn("ADMIN", "admin-socket", "%s", sun.sun_path,
+          "chmod-failed", "?ERRNO", A_END);
+    exit(EXIT_FAILURE);
   }
   umask(omask);
   fdflags(fd, O_NONBLOCK, O_NONBLOCK, FD_CLOEXEC, FD_CLOEXEC);
-  if (listen(fd, 5))
-    die(EXIT_FAILURE, "couldn't listen on socket: %s", strerror(errno));
+  if (listen(fd, 5)) {
+    a_warn("ADMIN", "admin-socket", "%s", sun.sun_path,
+          "listen-failed", "?ERRNO", A_END);
+    exit(EXIT_FAILURE);
+  }
 
   /* --- Listen to the socket --- */
 
@@ -2568,6 +2596,23 @@ again:
   sockname = name;
 }
 
+/* --- @a_unlisten@ --- *
+ *
+ * Arguments:  ---
+ *
+ * Returns:    ---
+ *
+ * Use:                Stops listening to the administration socket and removes it.
+ */
+
+void a_unlisten(void)
+{
+  if (!sockname) return;
+  sel_rmfile(&sock);
+  unlink(sockname);
+  close(sock.fd);
+}
+
 /* --- @a_switcherr@ --- *
  *
  * Arguments:  ---
@@ -2637,8 +2682,10 @@ void a_init(void)
                       (adns_if_permit_ipv4 | adns_if_permit_ipv6 |
                        adns_if_noserverwarn | adns_if_nosigpipe |
                        adns_if_noautosys),
-                      0)) != 0)
-    die(EXIT_FAILURE, "failed to initialize ADNS: %s", strerror(errno));
+                      0)) != 0) {
+    a_warn("ADMIN", "adns-init-failed", "?ERRNO", A_END);
+    exit(EXIT_FAILURE);
+  }
   sel_addhook(&sel, &hook, before_select, after_select, 0);
 #else
   bres_init(&sel);