mwrap (Perl version) user+dev discussion/patches/pulls/bugs/help
 help / color / mirror / code / Atom feed
From: Eric Wong <mwrap-perl@80x24.org>
To: mwrap-perl@80x24.org
Subject: [PATCH 2/3] httpd: more thorough unlinking of stale sockets
Date: Fri, 16 Dec 2022 22:57:53 +0000	[thread overview]
Message-ID: <20221216225754.96103-3-mwrap-perl@80x24.org> (raw)
In-Reply-To: <20221216225754.96103-1-mwrap-perl@80x24.org>

Lets try not to leave stale sockets lying around since they can
eat away at inode space.
---
 httpd.h             | 51 +++++++++++++++++++++++++++------------------
 mwrap_core.h        |  3 +--
 script/mwrap-rproxy |  5 +++++
 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

  parent reply	other threads:[~2022-12-16 22:56 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-16 22:57 [PATCH 0/3] more rproxy+httpd tweaks Eric Wong
2022-12-16 22:57 ` [PATCH 1/3] rproxy: manpage updates Eric Wong
2022-12-16 22:57 ` Eric Wong [this message]
2022-12-16 22:57 ` [PATCH 3/3] rproxy: more thorough connectivity check Eric Wong

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221216225754.96103-3-mwrap-perl@80x24.org \
    --to=mwrap-perl@80x24.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mwrap-perl.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).