[master] 293edb1 Move the worker process stack magic to child_main.c where it belongs.

Poul-Henning Kamp phk at FreeBSD.org
Sat Oct 21 22:07:11 UTC 2017


commit 293edb1e4a25e576f4d0cdac8bebb022a9c90997
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Sat Oct 21 22:06:12 2017 +0000

    Move the worker process stack magic to child_main.c where it belongs.

diff --git a/bin/varnishd/cache/cache_main.c b/bin/varnishd/cache/cache_main.c
index 5622bf6..cfaec23 100644
--- a/bin/varnishd/cache/cache_main.c
+++ b/bin/varnishd/cache/cache_main.c
@@ -33,6 +33,9 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#ifdef HAVE_SIGALTSTACK
+#  include <sys/mman.h>
+#endif
 
 #ifdef HAVE_PTHREAD_NP_H
 #  include <pthread_np.h>
@@ -121,7 +124,7 @@ THR_GetName(void)
  */
 #ifdef HAVE_SIGALTSTACK
 #include <signal.h>
-extern stack_t altstack;
+static stack_t altstack;
 #endif
 
 void
@@ -222,10 +225,81 @@ child_malloc_fail(void *p, const char *s)
 }
 #endif
 
+/*=====================================================================
+ * signal handler for child process
+ */
+
+static void __match_proto__()
+child_signal_handler(int s, siginfo_t *si, void *c)
+{
+	char buf[1024];
+	struct sigaction sa;
+
+	(void)c;
+
+	/* Don't come back */
+	memset(&sa, 0, sizeof sa);
+	sa.sa_handler = SIG_DFL;
+	(void)sigaction(SIGSEGV, &sa, NULL);
+	(void)sigaction(SIGABRT, &sa, NULL);
+
+	bprintf(buf, "Signal %d (%s) received at %p si_code %d",
+		s, strsignal(s), si->si_addr, si->si_code);
+	VAS_Fail(__func__,
+		 __FILE__,
+		 __LINE__,
+		 buf,
+		 VAS_WRONG);
+}
+
+/*=====================================================================
+ * Magic for panicing properly on signals
+ */
+
+static void
+child_sigmagic(size_t altstksz)
+{
+	struct sigaction sa;
+
+	memset(&sa, 0, sizeof sa);
+
+#ifdef HAVE_SIGALTSTACK
+	size_t sz = SIGSTKSZ + 4096;
+	if (sz < altstksz)
+		sz = altstksz;
+	altstack.ss_sp = mmap(NULL, sz,  PROT_READ | PROT_WRITE,
+			      MAP_PRIVATE | MAP_ANONYMOUS,
+			      -1, 0);
+	AN(altstack.ss_sp != MAP_FAILED);
+	AN(altstack.ss_sp);
+	altstack.ss_size = sz;
+	altstack.ss_flags = 0;
+	sa.sa_flags |= SA_ONSTACK;
+#endif
+
+	THR_Init();
+
+	sa.sa_sigaction = child_signal_handler;
+	sa.sa_flags |= SA_SIGINFO;
+	(void)sigaction(SIGBUS, &sa, NULL);
+	(void)sigaction(SIGABRT, &sa, NULL);
+	(void)sigaction(SIGSEGV, &sa, NULL);
+}
+
+
+/*=====================================================================
+ * Run the child process
+ */
+
 void
-child_main(void)
+child_main(int sigmagic, size_t altstksz)
 {
 
+	if (sigmagic)
+		child_sigmagic(altstksz);
+	(void)signal(SIGINT, SIG_DFL);
+	(void)signal(SIGTERM, SIG_DFL);
+
 	setbuf(stdout, NULL);
 	setbuf(stderr, NULL);
 	printf("Child starts\n");
diff --git a/bin/varnishd/common/heritage.h b/bin/varnishd/common/heritage.h
index ebc30fe..9e26079 100644
--- a/bin/varnishd/common/heritage.h
+++ b/bin/varnishd/common/heritage.h
@@ -109,7 +109,7 @@ void MCH_Fd_Inherit(int fd, const char *what);
 	} while (0)
 
 /* cache/cache_main.c */
-void child_main(void);
+void child_main(int, size_t);
 
 /* cache/cache_vcl.c */
 int VCL_TestLoad(const char *);
diff --git a/bin/varnishd/mgt/mgt_child.c b/bin/varnishd/mgt/mgt_child.c
index a8e148d..a6f944c 100644
--- a/bin/varnishd/mgt/mgt_child.c
+++ b/bin/varnishd/mgt/mgt_child.c
@@ -85,11 +85,6 @@ static struct vlu	*child_std_vlu;
 
 static struct vsb *child_panic = NULL;
 
-#ifdef HAVE_SIGALTSTACK
-#include <sys/mman.h>
-stack_t altstack;
-#endif
-
 static void mgt_reap_child(void);
 
 /*=====================================================================
@@ -262,66 +257,6 @@ child_poker(const struct vev *e, int what)
 }
 
 /*=====================================================================
- * signal handler for child process
- */
-
-static void __match_proto__()
-child_signal_handler(int s, siginfo_t *si, void *c)
-{
-	char buf[1024];
-	struct sigaction sa;
-
-	(void)c;
-
-	/* Don't come back */
-	memset(&sa, 0, sizeof sa);
-	sa.sa_handler = SIG_DFL;
-	(void)sigaction(SIGSEGV, &sa, NULL);
-	(void)sigaction(SIGABRT, &sa, NULL);
-
-	bprintf(buf, "Signal %d (%s) received at %p si_code %d",
-		s, strsignal(s), si->si_addr, si->si_code);
-	VAS_Fail(__func__,
-		 __FILE__,
-		 __LINE__,
-		 buf,
-		 VAS_WRONG);
-}
-
-/*=====================================================================
- * Launch the child process
- */
-
-static void
-mgt_child_sigmagic(void)
-{
-	struct sigaction sa;
-
-	memset(&sa, 0, sizeof sa);
-	sa.sa_sigaction = child_signal_handler;
-	sa.sa_flags = SA_SIGINFO;
-	(void)sigaction(SIGBUS, &sa, NULL);
-	(void)sigaction(SIGABRT, &sa, NULL);
-
-#ifdef HAVE_SIGALTSTACK
-	size_t sz = SIGSTKSZ + 4096;
-	if (sz < mgt_param.wthread_stacksize)
-		sz = mgt_param.wthread_stacksize;
-	altstack.ss_sp = mmap(NULL, sz,  PROT_READ | PROT_WRITE,
-			      MAP_PRIVATE | MAP_ANONYMOUS,
-			      -1, 0);
-	AN(altstack.ss_sp != MAP_FAILED);
-	AN(altstack.ss_sp);
-	altstack.ss_size = sz;
-	altstack.ss_flags = 0;
-	AZ(sigaltstack(&altstack, NULL));
-	sa.sa_flags |= SA_ONSTACK;
-#endif
-	(void)sigaction(SIGSEGV, &sa, NULL);
-}
-
-
-/*=====================================================================
  * Launch the child process
  */
 
@@ -399,17 +334,17 @@ mgt_launch_child(struct cli *cli)
 		heritage.cls = mgt_cls;
 		heritage.ident = VSB_data(vident) + 1;
 
-		if (mgt_param.sigsegv_handler)
-			mgt_child_sigmagic();
-		(void)signal(SIGINT, SIG_DFL);
-		(void)signal(SIGTERM, SIG_DFL);
-
 		VJ_subproc(JAIL_SUBPROC_WORKER);
 
 		heritage.proc_vsmw = VSMW_New(heritage.vsm_fd, 0640, "_.index");
 		AN(heritage.proc_vsmw);
 
-		child_main();
+		/*
+		 * We pass these two params because child_main needs them
+		 * Well before it has found its own param struct.
+		 */
+		child_main(mgt_param.sigsegv_handler,
+		    mgt_param.wthread_stacksize);
 
 		exit(0);
 	}


More information about the varnish-commit mailing list