about summary refs log tree commit homepage
path: root/httpd.h
diff options
context:
space:
mode:
Diffstat (limited to 'httpd.h')
-rw-r--r--httpd.h51
1 files changed, 31 insertions, 20 deletions
diff --git a/httpd.h b/httpd.h
index afce2a1..8e58286 100644
--- a/httpd.h
+++ b/httpd.h
@@ -1130,6 +1130,31 @@ static void h1d_event_step(struct mw_h1d *h1d)
         non_fatal_pause(fail_fn);
 }
 
+static void h1d_unlink(struct mw_h1d *h1d, bool do_close)
+{
+        union mw_sockaddr sa;
+        socklen_t len = (socklen_t)sizeof(sa);
+
+        if (h1d->lfd < 0 || !h1d->pid_len)
+                return;
+        if (getsockname(h1d->lfd, &sa.any, &len) < 0) {
+                fprintf(stderr, "getsockname: %m\n");
+                return;
+        }
+        if (do_close) { /* only safe to close if thread isn't running */
+                (void)close(h1d->lfd);
+                h1d->lfd = -1;
+        }
+
+        char p[sizeof(h1d->pid_str)];
+        int rc = snprintf(p, sizeof(p), "%d", (int)getpid());
+
+        if (rc == (int)h1d->pid_len && !memcmp(p, h1d->pid_str, rc))
+                if (unlink(sa.un.sun_path) && errno != ENOENT)
+                        fprintf(stderr, "unlink(%s): %m\n", sa.un.sun_path);
+        h1d->pid_len = 0;
+}
+
 /* @env is getenv("MWRAP") */
 static int h1d_init(struct mw_h1d *h1d, const char *menv)
 {
@@ -1175,7 +1200,7 @@ static int h1d_init(struct mw_h1d *h1d, const char *menv)
                 return fprintf(stderr, "we suck at snprintf: %m\n");
         h1d->pid_len = rc - sizeof(".sock") + 1;
         memcpy(h1d->pid_str, p, h1d->pid_len);
-        if (unlink(sa.un.sun_path) < 0 && errno != ENOENT)
+        if (unlink(sa.un.sun_path) && errno != ENOENT)
                 return fprintf(stderr, "unlink(%s): %m\n", sa.un.sun_path);
         h1d->lfd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0);
         if (h1d->lfd < 0)
@@ -1193,8 +1218,7 @@ static int h1d_init(struct mw_h1d *h1d, const char *menv)
         CDS_INIT_LIST_HEAD(&h1d->conn);
         return 0;
 close_fail:
-        close(h1d->lfd);
-        h1d->lfd = -1;
+        h1d_unlink(h1d, true);
         return 1;
 }
 
@@ -1269,19 +1293,7 @@ static void *h1d_run(void *x) /* pthread_create cb */
 
 static void h1d_atexit(void)
 {
-        union mw_sockaddr sa;
-        socklen_t len = (socklen_t)sizeof(sa);
-
-        if (g_h1d.lfd < 0 || !g_h1d.pid_len)
-                return;
-        if (getsockname(g_h1d.lfd, &sa.any, &len) < 0)
-                return;
-
-        char p[sizeof(g_h1d.pid_str)];
-        int rc = snprintf(p, sizeof(p), "%d", (int)getpid());
-
-        if (rc == (int)g_h1d.pid_len && !memcmp(p, g_h1d.pid_str, rc))
-                unlink(sa.un.sun_path);
+        h1d_unlink(&g_h1d, false);
 }
 
 static void h1d_stop_join(struct mw_h1d *h1d)
@@ -1321,8 +1333,7 @@ join_thread:
                 fprintf(stderr, "BUG? pthread_join: %s\n", strerror(e));
                 abort();
         }
-        (void)close(h1d->lfd);
-        h1d->lfd = -1;
+        h1d_unlink(h1d, true);
 }
 
 static void h1d_atfork_prepare(void)
@@ -1337,9 +1348,9 @@ static void h1d_start(void) /* may be called as pthread_atfork child cb */
                 int rc = pthread_create(&g_h1d.tid, NULL, h1d_run, &g_h1d);
                 if (rc) { /* non-fatal */
                         fprintf(stderr, "pthread_create: %s\n", strerror(rc));
-                        (void)close(g_h1d.lfd);
-                        g_h1d.lfd = -1;
                         g_h1d.alive = 0;
+                        g_h1d.running = 0;
+                        h1d_unlink(&g_h1d, true);
                 }
         }
 }