chiark / gitweb /
server/: Split peer and admin initialization into smaller pieces.
[tripe] / server / admin.c
index 87bb90530653529c39902db80e770584d8fef373..acdb973af784bfa0899d444554485c53f7a81720 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;
@@ -559,7 +559,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);
@@ -1233,7 +1233,7 @@ static void a_resolve(admin *a, admin_resop *r, const char *tag,
     { fam = "ANY"; af = AF_UNSPEC; i++; }
   else for (j = 0; j < NADDRFAM; j++) {
     if (mystrieq(av[i], aftab[j].name)) {
-      if (udpsock[j].fd < 0) {
+      if (udpsock[j].sf.fd < 0) {
        a_fail(a, "disabled-address-family", "%s", aftab[j].name, A_END);
        goto fail;
       }
@@ -1286,7 +1286,7 @@ static void a_resolve(admin *a, admin_resop *r, const char *tag,
   aihint.ai_flags = AI_NUMERICHOST;
   if (!getaddrinfo(av[i], 0, &aihint, &ailist)) {
     for (ai = ailist; ai; ai = ai->ai_next) {
-      if ((j = afix(ai->ai_family)) >= 0 && udpsock[j].fd >= 0)
+      if ((j = afix(ai->ai_family)) >= 0 && udpsock[j].sf.fd >= 0)
        break;
     }
     if (!ai) {
@@ -1315,7 +1315,7 @@ static void a_resolve(admin *a, admin_resop *r, const char *tag,
 #ifdef HAVE_LIBADNS
   qf = adns_qf_search;
   for (j = 0; j < NADDRFAM; j++) {
-    if ((af == AF_UNSPEC || af == aftab[i].af) && udpsock[j].fd >= 0)
+    if ((af == AF_UNSPEC || af == aftab[i].af) && udpsock[j].sf.fd >= 0)
       qf |= aftab[j].qf;
   }
   if ((err = adns_submit(ads, r->addr, adns_r_addr, qf, r, &r->q)) != 0) {
@@ -1331,7 +1331,7 @@ static void a_resolve(admin *a, admin_resop *r, const char *tag,
     a_bgfail(&r->bg, "resolve-error", "%s", r->addr, A_END);
     goto fail_release;
   }
-  if (udpsock[AFIX_INET].fd < 0) {
+  if (udpsock[AFIX_INET].sf.fd < 0) {
     a_bgfail(&r->bg, "disabled-address-family", "INET", A_END);
     goto fail_release;
   }
@@ -1870,16 +1870,16 @@ static void acmd_port(admin *a, unsigned ac, char *av[])
     a_fail(a, "unknown-address-family", "%s", av[0], A_END);
     return;
   found:
-    if (udpsock[i].fd < 0) {
+    if (udpsock[i].sf.fd < 0) {
       a_fail(a, "disabled-address-family", "%s", aftab[i].name, A_END);
       return;
     }
   } else {
     for (i = 0; i < NADDRFAM; i++)
-      if (udpsock[i].fd >= 0) goto found;
+      if (udpsock[i].sf.fd >= 0) goto found;
     abort();
   }
-  a_info(a, "%u", p_port(i), A_END);
+  a_info(a, "%u", udpsock[i].port, A_END);
   a_ok(a);
 }
 
@@ -2404,7 +2404,10 @@ static void a_line(char *p, size_t len, void *vp)
  *
  * Returns:    ---
  *
- * Use:                Creates a new admin connection.
+ * Use:                Creates a new admin connection.  It's safe to call this
+ *             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)
@@ -2482,7 +2485,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
@@ -2494,21 +2497,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 --- */
 
@@ -2571,6 +2566,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 |
@@ -2582,17 +2643,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 -------------------------------------------------*/