about summary refs log tree commit homepage
diff options
context:
space:
mode:
authorEric Wong <mwrap-perl@80x24.org>2022-12-16 22:57:53 +0000
committerEric Wong <mwrap-perl@80x24.org>2022-12-16 22:58:17 +0000
commit4db2dadf3881ed18eefdc8fb78237b5fca4c3e78 (patch)
treebc48ddbcc3ad80c3255790cf9b44fd15a230626c
parentd0d47aa3c0b4ea80f778b7fde117209c66cd68dd (diff)
downloadmwrap-4db2dadf3881ed18eefdc8fb78237b5fca4c3e78.tar.gz
Lets try not to leave stale sockets lying around since they can
eat away at inode space.
-rw-r--r--httpd.h51
-rw-r--r--mwrap_core.h3
-rw-r--r--script/mwrap-rproxy5
3 files changed, 37 insertions, 22 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);
                 }
         }
 }
diff --git a/mwrap_core.h b/mwrap_core.h
index 9e4f065..7681ca5 100644
--- a/mwrap_core.h
+++ b/mwrap_core.h
@@ -843,8 +843,7 @@ static struct src_loc *src_loc_lookup(const char *str, size_t len)
 #  define O_CLOEXEC 0
 #endif
 static void h1d_atexit(void);
-__attribute__ ((destructor))
-static void dump_destructor(void)
+__attribute__ ((destructor)) static void mwrap_dtor(void)
 {
         const char *opt = getenv("MWRAP");
         const char *modes[] = { "a", "a+", "w", "w+", "r+" };
diff --git a/script/mwrap-rproxy b/script/mwrap-rproxy
index af5bdf9..96e203e 100644
--- a/script/mwrap-rproxy
+++ b/script/mwrap-rproxy
@@ -34,6 +34,11 @@ Inherited socket (fd=3) is non-blocking, making it blocking.
 if ($opt{deflate} && eval { require Plack::Middleware::Deflater } and !$@) {
         $app = Plack::Middleware::Deflater->wrap($app);
 }
+
+# ensure mwrap_dtor() runs if running under mwrap-perl, ourselves
+sub sigexit { exit 0 }
+$SIG{$_} = \&sigexit for (qw(INT TERM));
+
 $runner->run($app);
 __END__
 =head1 NAME