As discussed, an update of the signal handling.

Cheers,
Dridi
From 74fd7063e8098356c1fdd224f2d9c121942a399f Mon Sep 17 00:00:00 2001
From: Dridi Boukelmoune <[email protected]>
Date: Fri, 5 Jun 2015 12:22:19 +0200
Subject: [PATCH 3/3] Rewrite signal handling in a varnishtest bg process

Relying on kill(1) for portability, and terminating with a
TERM-WAIT-KILL sequence for more safety.
---
 bin/varnishtest/tbl_signal.h  | 64 -----------------------------------------
 bin/varnishtest/vtc_process.c | 66 +++++++++++++++----------------------------
 2 files changed, 22 insertions(+), 108 deletions(-)
 delete mode 100644 bin/varnishtest/tbl_signal.h

diff --git a/bin/varnishtest/tbl_signal.h b/bin/varnishtest/tbl_signal.h
deleted file mode 100644
index 7ed9276..0000000
--- a/bin/varnishtest/tbl_signal.h
+++ /dev/null
@@ -1,64 +0,0 @@
-/*-
- * Copyright (c) 2015 Varnish Software AS
- * All rights reserved.
- *
- * Author: Dridi Boukelmoune <[email protected]>
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright
- *    notice, this list of conditions and the following disclaimer in the
- *    documentation and/or other materials provided with the distribution.
- *
- * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
- * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
- * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
- * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
- * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
- * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
- * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
- * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
- * SUCH DAMAGE.
- *
- * Generated with:
- *    kill -l | tr '\t' '\n' | sed /^31/q | sed 's/.*SIG\(.*\)/SIGNAL(\1)/'
- */
-
-/*lint -save -e525 -e539 */
-SIGNAL(HUP)
-SIGNAL(INT)
-SIGNAL(QUIT)
-SIGNAL(ILL)
-SIGNAL(TRAP)
-SIGNAL(ABRT)
-SIGNAL(BUS)
-SIGNAL(FPE)
-SIGNAL(KILL)
-SIGNAL(USR1)
-SIGNAL(SEGV)
-SIGNAL(USR2)
-SIGNAL(PIPE)
-SIGNAL(ALRM)
-SIGNAL(TERM)
-SIGNAL(STKFLT)
-SIGNAL(CHLD)
-SIGNAL(CONT)
-SIGNAL(STOP)
-SIGNAL(TSTP)
-SIGNAL(TTIN)
-SIGNAL(TTOU)
-SIGNAL(URG)
-SIGNAL(XCPU)
-SIGNAL(XFSZ)
-SIGNAL(VTALRM)
-SIGNAL(PROF)
-SIGNAL(WINCH)
-SIGNAL(IO)
-SIGNAL(PWR)
-SIGNAL(SYS)
-/*lint -restore */
diff --git a/bin/varnishtest/vtc_process.c b/bin/varnishtest/vtc_process.c
index 2bd17e7..2918ca3 100644
--- a/bin/varnishtest/vtc_process.c
+++ b/bin/varnishtest/vtc_process.c
@@ -68,17 +68,6 @@ struct process {
 static VTAILQ_HEAD(, process)	processes =
     VTAILQ_HEAD_INITIALIZER(processes);
 
-#define SIGNAL(name)	{SIG##name, "SIG" #name, #name},
-struct signal {
-	int		signum;
-	const char	*sigspec;
-	const char	*name;
-} signals[] = {
-#include "tbl_signal.h"
-{-1, NULL, NULL}
-};
-#undef SIGNAL
-
 /**********************************************************************
  * Allocate and initialize a process
  */
@@ -248,27 +237,11 @@ process_wait(struct process *p)
  * Send a signal to a process
  */
 
-static int
-parse_signal(const char *sig)
-{
-	struct signal *s;
-
-	// TODO? if (sig is int) return atoi(sig)
-
-	s = signals;
-	while (s->name) {
-		if (!strcmp(sig, s->sigspec) || !strcmp(sig, s->name))
-			break;
-		s++;
-	}
-
-	return (s->signum);
-}
-
 static void
 process_kill(struct process *p, const char *sig)
 {
-	int signum;
+	int s, l;
+	char buf[64];
 
 	CHECK_OBJ_NOTNULL(p, PROCESS_MAGIC);
 	AN(sig);
@@ -278,18 +251,13 @@ process_kill(struct process *p, const char *sig)
 		return;
 	}
 
-	signum = parse_signal(sig);
-	if (signum <= 0) {
-		vtc_log(p->vl, 0, "Unknown signal: %s", sig);
-		return;
-	}
-
-	vtc_log(p->vl, 4, "Sending signal %s (%d) to process %d",
-	    sig, signum, p->pid);
+	vtc_log(p->vl, 4, "CMD: /usr/bin/kill -%s %d", sig, p->pid);
 
-	if (kill(p->pid, signum) < 0)
-		vtc_log(p->vl, 0, "Failed to send signal %s: %s",
-		    sig, strerror(errno));
+	l = snprintf(buf, sizeof buf, "/usr/bin/kill -%s %d", sig, p->pid);
+	AN(l < sizeof buf);
+	s = system(buf);
+	if (s != 0)
+		vtc_log(p->vl, 0, "Failed to send signal (exit status: %d)", s);
 }
 
 static inline void
@@ -299,6 +267,18 @@ process_stop(struct process *p)
 	process_kill(p, "SIGTERM");
 }
 
+static inline void
+process_terminate(struct process *p)
+{
+	struct timespec ts;
+
+	process_kill(p, "SIGTERM");
+	ts.tv_sec = 1;
+	ts.tv_nsec = 0;
+	if (pthread_timedjoin_np(p->tp, NULL, &ts) != 0)
+		process_kill(p, "SIGKILL");
+}
+
 /**********************************************************************
  * Write to a process' stdin
  */
@@ -350,10 +330,8 @@ cmd_process(CMD_ARGS)
 	if (av == NULL) {
 		/* Reset and free */
 		VTAILQ_FOREACH_SAFE(p, &processes, list, p2) {
-			if (p->running && p->pid) {
-				process_stop(p);
-				process_wait(p);
-			}
+			if (p->running && p->pid)
+				process_terminate(p);
 			VTAILQ_REMOVE(&processes, p, list);
 			process_delete(p);
 		}
-- 
2.1.0

_______________________________________________
varnish-dev mailing list
[email protected]
https://www.varnish-cache.org/lists/mailman/listinfo/varnish-dev

Reply via email to