[master] 5823ee9 Extract the wait4() handling to a subroutine

Poul-Henning Kamp phk at FreeBSD.org
Tue Mar 27 08:52:12 UTC 2018


commit 5823ee99c3238e28bc1068e5fdc3ecdfffa29c86
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Tue Mar 27 08:05:12 2018 +0000

    Extract the wait4() handling to a subroutine

diff --git a/bin/varnishtest/vtc.h b/bin/varnishtest/vtc.h
index b404552..8cb2640 100644
--- a/bin/varnishtest/vtc.h
+++ b/bin/varnishtest/vtc.h
@@ -138,6 +138,7 @@ void b64_settings(const struct http *hp, const char *s);
 struct vsb *vtc_hex_to_bin(struct vtclog *vl, const char *arg);
 void vtc_expect(struct vtclog *, const char *, const char *, const char *,
     const char *, const char *);
+void vtc_wait4(struct vtclog *, long, int, int);
 
 /* vtc_term.c */
 struct term *Term_New(struct vtclog *, int, int);
diff --git a/bin/varnishtest/vtc_subr.c b/bin/varnishtest/vtc_subr.c
index 2c1439a..4cdea40 100644
--- a/bin/varnishtest/vtc_subr.c
+++ b/bin/varnishtest/vtc_subr.c
@@ -29,9 +29,12 @@
 #include "config.h"
 
 #include <sys/types.h>
+#include <errno.h>
 #include <math.h>
 #include <string.h>
 #include <stdint.h>
+#include <sys/wait.h>
+#include <sys/resource.h>
 
 #include "vct.h"
 #include "vnum.h"
@@ -128,3 +131,37 @@ vtc_expect(struct vtclog *vl,
 		vtc_log(vl, 4, "EXPECT %s (%s) %s \"%s\" match",
 		    olhs, lhs, cmp, rhs);
 }
+
+void
+vtc_wait4(struct vtclog *vl, long pid, int expect_status, int expect_signal)
+{
+	int status, r;
+	struct rusage ru;
+
+	r = wait4(pid, &status, 0, &ru);
+	if (r < 0)
+		vtc_fatal(vl, "wait4 failed on pid %ld: %s",
+		    pid, strerror(errno));
+	vtc_log(vl, 2, "WAIT4 pid=%ld r=%d status=0x%04x (user %.6f sys %.6f)",
+	    pid, r, status,
+	    ru.ru_utime.tv_sec + 1e-6 * ru.ru_utime.tv_usec,
+	    ru.ru_stime.tv_sec + 1e-6 * ru.ru_stime.tv_usec
+	);
+
+	if (WIFEXITED(status) && (WEXITSTATUS(status) == expect_status))
+		return;
+	if (WIFSIGNALED(status) && (WTERMSIG(status) == expect_signal))
+		return;
+#ifdef WCOREDUMP
+	vtc_fatal(vl, "Bad exit code: 0x%04x exit 0x%x signal %d core %d",
+	    status,
+	    WEXITSTATUS(status),
+	    WIFSIGNALED(status) ? WTERMSIG(status) : 0,
+	    WCOREDUMP(status));
+#else
+	vtc_fatal(vl, "Bad exit code: 0x%04x exit 0x%x signal %d",
+	    status,
+	    WEXITSTATUS(status),
+	    WIFSIGNALED(status) ? WTERMSIG(status) : 0);
+#endif
+}
diff --git a/bin/varnishtest/vtc_varnish.c b/bin/varnishtest/vtc_varnish.c
index 12e4244..680c1aa 100644
--- a/bin/varnishtest/vtc_varnish.c
+++ b/bin/varnishtest/vtc_varnish.c
@@ -29,9 +29,7 @@
 #include "config.h"
 
 #include <sys/types.h>
-#include <sys/wait.h>
 #include <sys/socket.h>
-#include <sys/resource.h>
 
 #include <errno.h>
 #include <fcntl.h>
@@ -198,7 +196,7 @@ vsl_catchup(const struct varnish *v)
 	int vsl_idle;
 
 	vsl_idle = v->vsl_idle;
-	while (!vtc_error && v->pid && vsl_idle == v->vsl_idle)
+	while (!vtc_error && vsl_idle == v->vsl_idle)
 		VTIM_sleep(0.1);
 }
 
@@ -224,7 +222,7 @@ varnishlog_thread(void *priv)
 
 	c = NULL;
 	opt = 0;
-	while (v->pid || c != NULL) {
+	while (v->fds[1] > 0 || c != NULL) {
 		if (c == NULL) {
 			if (vtc_error)
 				break;
@@ -386,7 +384,7 @@ varnish_thread(void *priv)
 		memset(fds, 0, sizeof fds);
 		fds->fd = v->fds[0];
 		fds->events = POLLIN;
-		i = poll(fds, 1, 1000);
+		i = poll(fds, 1, 10000);
 		if (i == 0)
 			continue;
 		if (fds->revents & POLLIN) {
@@ -635,8 +633,6 @@ static void
 varnish_cleanup(struct varnish *v)
 {
 	void *p;
-	int status, r;
-	struct rusage ru;
 
 	/* Close the CLI connection */
 	closefd(&v->cli_fd);
@@ -648,26 +644,11 @@ varnish_cleanup(struct varnish *v)
 	AZ(pthread_join(v->tp, &p));
 	closefd(&v->fds[0]);
 
-	r = wait4(v->pid, &status, 0, &ru);
-	v->pid = 0;
-	vtc_log(v->vl, 2, "R %d Status: %04x (u %.6f s %.6f)", r, status,
-	    ru.ru_utime.tv_sec + 1e-6 * ru.ru_utime.tv_usec,
-	    ru.ru_stime.tv_sec + 1e-6 * ru.ru_stime.tv_usec
-	);
-
 	/* Pick up the VSL thread */
 	AZ(pthread_join(v->tp_vsl, &p));
 
-	if (WIFEXITED(status) && (WEXITSTATUS(status) == v->expect_exit))
-		return;
-#ifdef WCOREDUMP
-	vtc_fatal(v->vl, "Bad exit code: %04x sig %x exit %x core %x",
-	    status, WTERMSIG(status), WEXITSTATUS(status),
-	    WCOREDUMP(status));
-#else
-	vtc_fatal(v->vl, "Bad exit code: %04x sig %x exit %x",
-	    status, WTERMSIG(status), WEXITSTATUS(status));
-#endif
+	vtc_wait4(v->vl, v->pid, v->expect_exit, 0);
+	v->pid = 0;
 }
 
 /**********************************************************************


More information about the varnish-commit mailing list