server/admin.c (a_format): Introduce `?ERR' for explicitly named errors.
[tripe] / server / admin.c
index e530620..6ff0dbf 100644 (file)
@@ -68,7 +68,7 @@ static const trace_opt w_opts[] = {
 static admin *admins;
 static admin *a_dead;
 static sel_file sock;
-static const char *sockname;
+static const char *sockname = 0;
 static sym_table a_svcs;
 static unsigned flags = 0;
 static admin *a_stdin = 0;
@@ -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
@@ -559,7 +565,7 @@ void a_notify(const char *fmt, ...)
 void a_quit(void)
 {
   close(sock.fd);
-  unlink(sockname);
+  if (sockname) unlink(sockname);
   FOREACH_PEER(p, { p_destroy(p, 1); });
   ps_quit();
   exit(0);
@@ -2405,7 +2411,9 @@ static void a_line(char *p, size_t len, void *vp)
  * Returns:    ---
  *
  * Use:                Creates a new admin connection.  It's safe to call this
- *             before @a_init@.
+ *             before @a_init@ -- and, indeed, this makes sense if you also
+ *             call @a_switcherr@ to report initialization errors through
+ *             the administration machinery.
  */
 
 void a_create(int fd_in, int fd_out, unsigned f)
@@ -2483,7 +2491,7 @@ void a_preselect(void) { if (a_dead) a_destroypending(); }
 
 void a_daemon(void) { flags |= F_DAEMON; }
 
-/* --- @a_init@ --- *
+/* --- @a_listen@ --- *
  *
  * Arguments:  @const char *name@ = socket name to create
  *             @uid_t u@ = user to own the socket
@@ -2495,21 +2503,13 @@ void a_daemon(void) { flags |= F_DAEMON; }
  * Use:                Creates the admin listening socket.
  */
 
-void a_init(const char *name, uid_t u, gid_t g, mode_t m)
+void a_listen(const char *name, uid_t u, gid_t g, mode_t m)
 {
   int fd;
   int n = 5;
   struct sockaddr_un sun;
-  struct sigaction sa;
   size_t sz;
   mode_t omask;
-#ifdef HAVE_LIBADNS
-  int err;
-#endif
-
-  /* --- Create services table --- */
-
-  sym_create(&a_svcs);
 
   /* --- Set up the socket address --- */
 
@@ -2544,6 +2544,7 @@ again:
     if (errno != ECONNREFUSED)
       die(EXIT_FAILURE, "couldn't bind to address: %s", strerror(e));
     if (stat(sun.sun_path, &st)) {
+      if (errno == ENOENT) { close(fd); goto again; }
       die(EXIT_FAILURE, "couldn't stat `%s': %s",
          sun.sun_path, strerror(errno));
     }
@@ -2572,6 +2573,72 @@ again:
   sel_initfile(&sel, &sock, fd, SEL_READ, a_accept, 0);
   sel_addfile(&sock);
   sockname = name;
+}
+
+/* --- @a_switcherr@ --- *
+ *
+ * Arguments:  ---
+ *
+ * Returns:    ---
+ *
+ * Use:                Arrange to report warnings, trace messages, etc. to
+ *             administration clients rather than the standard-error stream.
+ *
+ *             Obviously this makes no sense unless there is at least one
+ *             client established.  Calling @a_listen@ won't help with this,
+ *             because the earliest a new client can connect is during the
+ *             first select-loop iteration, which is too late: some initial
+ *             client must have been added manually using @a_create@.
+ */
+
+void a_switcherr(void)
+{
+  T( trace_custom(a_trace, 0);
+     trace(T_ADMIN, "admin: enabled custom tracing"); )
+  flags |= F_INIT;
+}
+
+/* --- @a_signals@ --- *
+ *
+ * Arguments:  ---
+ *
+ * Returns:    ---
+ *
+ * Use:                Establishes handlers for the obvious signals.
+ */
+
+void a_signals(void)
+{
+  struct sigaction sa;
+
+  sig_add(&s_term, SIGTERM, a_sigdie, 0);
+  sig_add(&s_hup, SIGHUP, a_sighup, 0);
+  sigaction(SIGINT, 0, &sa);
+  if (sa.sa_handler != SIG_IGN)
+    sig_add(&s_int, SIGINT, a_sigdie, 0);
+}
+
+/* --- @a_init@ --- *
+ *
+ * Arguments:  ---
+ *
+ * Returns:    ---
+ *
+ * Use:                Creates the admin listening socket.
+ */
+
+void a_init(void)
+{
+#ifdef HAVE_LIBADNS
+  int err;
+#endif
+
+  /* --- Create services table --- */
+
+  sym_create(&a_svcs);
+
+  /* --- Prepare the background name resolver --- */
+
 #ifdef HAVE_LIBADNS
   if ((err = adns_init(&ads,
                       (adns_if_permit_ipv4 | adns_if_permit_ipv6 |
@@ -2583,17 +2650,6 @@ again:
 #else
   bres_init(&sel);
 #endif
-  T( trace_custom(a_trace, 0);
-     trace(T_ADMIN, "admin: enabled custom tracing"); )
-  flags |= F_INIT;
-
-  /* --- Set up signal handlers --- */
-
-  sig_add(&s_term, SIGTERM, a_sigdie, 0);
-  sig_add(&s_hup, SIGHUP, a_sighup, 0);
-  sigaction(SIGINT, 0, &sa);
-  if (sa.sa_handler != SIG_IGN)
-    sig_add(&s_int, SIGINT, a_sigdie, 0);
 }
 
 /*----- That's all, folks -------------------------------------------------*/