[master] 55995ab0f Renovate libvarnisapi signal handling.

Poul-Henning Kamp phk at FreeBSD.org
Mon Nov 12 10:14:13 UTC 2018


commit 55995ab0f96c458b1b585afe49c3af737e363205
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Mon Nov 12 10:10:28 2018 +0000

    Renovate libvarnisapi signal handling.
    
    Introduce VSIG which simply counts number of signals, which everybody
    else can then look at.
    
    If you use libvarnishapi and want your own signal handler, you must
    increment these VSIG_{int|hup|term|usr1} counters yourself, for
    libvarnishapi to work correctly.
    
    The old VUT_Signal()/VUT_Signaled() API is retained and should be
    deprecated in a future version.
    
    Inspired by:    #2818

diff --git a/bin/varnishhist/varnishhist.c b/bin/varnishhist/varnishhist.c
index 626a80714..6d441ca6f 100644
--- a/bin/varnishhist/varnishhist.c
+++ b/bin/varnishhist/varnishhist.c
@@ -371,14 +371,6 @@ accumulate(struct VSL_data *vsl, struct VSL_transaction * const pt[],
 	return (0);
 }
 
-static int v_matchproto_(VUT_cb_f)
-sighup(struct VUT *v)
-{
-	assert(v == vut);
-	quit = 1;
-	return (1);
-}
-
 static void * v_matchproto_(pthread_t)
 do_curses(void *arg)
 {
@@ -392,7 +384,7 @@ do_curses(void *arg)
 	intrflush(stdscr, FALSE);
 	curs_set(0);
 	erase();
-	while (!quit) {
+	while (!quit && !vut->last_sighup) {
 		AZ(pthread_mutex_lock(&mtx));
 		update();
 		AZ(pthread_mutex_unlock(&mtx));
@@ -480,14 +472,6 @@ profile_error(const char *s)
 	exit(1);
 }
 
-static void
-vut_sighandler(int sig)
-{
-
-	if (vut != NULL)
-		VUT_Signaled(vut, sig);
-}
-
 int
 main(int argc, char **argv)
 {
@@ -638,7 +622,6 @@ main(int argc, char **argv)
 
 	log_ten = log(10.0);
 
-	VUT_Signal(vut_sighandler);
 	VUT_Setup(vut);
 	if (vut->vsm)
 		ident = VSM_Dup(vut->vsm, "Arg", "-i");
@@ -647,7 +630,6 @@ main(int argc, char **argv)
 	AZ(pthread_create(&thr, NULL, do_curses, NULL));
 	vut->dispatch_f = accumulate;
 	vut->dispatch_priv = NULL;
-	vut->sighup_f = sighup;
 	(void)VUT_Main(vut);
 	end_of_file = 1;
 	AZ(pthread_join(thr, NULL));
diff --git a/bin/varnishlog/varnishlog.c b/bin/varnishlog/varnishlog.c
index 8abab3037..09f8983d5 100644
--- a/bin/varnishlog/varnishlog.c
+++ b/bin/varnishlog/varnishlog.c
@@ -32,6 +32,7 @@
 
 #include "config.h"
 
+#include <signal.h>
 #include <stdarg.h>
 #include <stdlib.h>
 #include <stdio.h>
@@ -102,21 +103,6 @@ flushout(struct VUT *v)
 	return (0);
 }
 
-static int v_matchproto_(VUT_cb_f)
-sighup(struct VUT *v)
-{
-	assert(v == vut);
-	return (1);
-}
-
-static void
-vut_sighandler(int sig)
-{
-
-	if (vut != NULL)
-		VUT_Signaled(vut, sig);
-}
-
 int
 main(int argc, char * const *argv)
 {
@@ -160,7 +146,6 @@ main(int argc, char * const *argv)
 		vut->dispatch_f = VSL_PrintTransactions;
 	else
 		vut->dispatch_f = VSL_WriteTransactions;
-	vut->sighup_f = sighup;
 	if (LOG.w_arg) {
 		openout(LOG.a_opt);
 		AN(LOG.fo);
@@ -170,7 +155,6 @@ main(int argc, char * const *argv)
 		LOG.fo = stdout;
 	vut->idle_f = flushout;
 
-	VUT_Signal(vut_sighandler);
 	VUT_Setup(vut);
 	(void)VUT_Main(vut);
 	VUT_Fini(&vut);
diff --git a/bin/varnishncsa/varnishncsa.c b/bin/varnishncsa/varnishncsa.c
index f3ed20a78..ca470e2a4 100644
--- a/bin/varnishncsa/varnishncsa.c
+++ b/bin/varnishncsa/varnishncsa.c
@@ -1050,21 +1050,6 @@ dispatch_f(struct VSL_data *vsl, struct VSL_transaction * const pt[],
 	return (0);
 }
 
-static int v_matchproto_(VUT_cb_f)
-sighup(struct VUT *v)
-{
-	assert(v == vut);
-	return (1);
-}
-
-static void
-vut_sighandler(int sig)
-{
-
-	if (vut != NULL)
-		VUT_Signaled(vut, sig);
-}
-
 static char *
 read_format(const char *formatfile)
 {
@@ -1175,7 +1160,6 @@ main(int argc, char * const *argv)
 	/* Setup output */
 	vut->dispatch_f = dispatch_f;
 	vut->dispatch_priv = NULL;
-	vut->sighup_f = sighup;
 	if (CTX.w_arg) {
 		openout(CTX.a_opt);
 		AN(CTX.fo);
@@ -1185,7 +1169,6 @@ main(int argc, char * const *argv)
 		CTX.fo = stdout;
 	vut->idle_f = flushout;
 
-	VUT_Signal(vut_sighandler);
 	VUT_Setup(vut);
 	(void)VUT_Main(vut);
 	VUT_Fini(&vut);
diff --git a/bin/varnishtop/varnishtop.c b/bin/varnishtop/varnishtop.c
index f91e3cd01..1fc844f27 100644
--- a/bin/varnishtop/varnishtop.c
+++ b/bin/varnishtop/varnishtop.c
@@ -182,22 +182,6 @@ accumulate(struct VSL_data *vsl, struct VSL_transaction * const pt[],
 	return (0);
 }
 
-static int v_matchproto_(VUT_cb_f)
-sighup(struct VUT *v)
-{
-	assert(v == vut);
-	quit = 1;
-	return (1);
-}
-
-static void
-vut_sighandler(int sig)
-{
-
-	if (vut != NULL)
-		VUT_Signaled(vut, sig);
-}
-
 static void
 update(int p)
 {
@@ -273,7 +257,7 @@ do_curses(void *arg)
 	(void)curs_set(0);
 	AC(erase());
 	timeout(1000);
-	while (!quit) {
+	while (!quit && !vut->last_sighup) {
 		AZ(pthread_mutex_lock(&mtx));
 		update(period);
 		AZ(pthread_mutex_unlock(&mtx));
@@ -364,7 +348,6 @@ main(int argc, char **argv)
 	if (optind != argc)
 		VUT_Usage(vut, &vopt_spec, 1);
 
-	VUT_Signal(vut_sighandler);
 	VUT_Setup(vut);
 	if (vut->vsm)
 		ident = VSM_Dup(vut->vsm, "Arg", "-i");
@@ -372,7 +355,6 @@ main(int argc, char **argv)
 		ident = strdup("");
 	vut->dispatch_f = accumulate;
 	vut->dispatch_priv = NULL;
-	vut->sighup_f = sighup;
 	if (once) {
 		(void)VUT_Main(vut);
 		dump();
diff --git a/include/Makefile.am b/include/Makefile.am
index 1479a6384..50142a64b 100644
--- a/include/Makefile.am
+++ b/include/Makefile.am
@@ -39,13 +39,15 @@ nobase_pkginclude_HEADERS = \
 	tbl/vhp_static.h \
 	tbl/vrt_stv_var.h \
 	tbl/vsc_levels.h \
+	tbl/vsig_list.h \
 	tbl/vsl_tags.h \
 	tbl/vsl_tags_http.h \
 	tbl/waiters.h \
-	vapi/vsm.h \
 	vapi/vsc.h \
+	vapi/vsig.h \
 	vapi/vsl.h \
 	vapi/vsl_int.h \
+	vapi/vsm.h \
 	vapi/voptget.h \
 	vapi/vapi_options.h \
 	vcli.h \
diff --git a/include/tbl/vsig_list.h b/include/tbl/vsig_list.h
new file mode 100644
index 000000000..f7a05d60c
--- /dev/null
+++ b/include/tbl/vsig_list.h
@@ -0,0 +1,40 @@
+/*-
+ * Copyright (c) 2018 Varnish Software AS
+ * All rights reserved.
+ *
+ * Author: Poul-Henning Kamp <phk at phk.freebsd.dk>
+ *
+ * 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.
+ *
+ * NB: Changing the list of signals requires updating libvarnishapi.map
+ *
+ */
+
+/*lint -save -e525 -e539 */
+
+VSIG_SIGNAL(INT,	int)
+VSIG_SIGNAL(HUP,	hup)
+VSIG_SIGNAL(TERM,	term)
+VSIG_SIGNAL(USR1,	usr1)
+#undef VSIG_SIGNAL
+
+/*lint -restore */
diff --git a/include/vapi/vsig.h b/include/vapi/vsig.h
new file mode 100644
index 000000000..1556ae779
--- /dev/null
+++ b/include/vapi/vsig.h
@@ -0,0 +1,40 @@
+/*-
+ * Copyright (c) 2018 Varnish Software AS
+ * All rights reserved.
+ *
+ * Author: Poul-Henning Kamp <phk at phk.freebsd.dk>
+ *
+ * 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.
+ *
+ */
+
+#ifndef VAPI_VSIG_H_INCLUDED
+#define VAPI_VSIG_H_INCLUDED
+
+#define VSIG_SIGNAL(UPPER, lower)		\
+	extern sig_atomic_t VSIG_##lower;	\
+	void VSIG_Got_##lower(int sig);		\
+	void VSIG_Arm_##lower(void);
+
+#include "tbl/vsig_list.h"
+
+#endif /* VAPI_VSC_H_INCLUDED */
diff --git a/include/vut.h b/include/vut.h
index 57eb7c02a..8a35b0267 100644
--- a/include/vut.h
+++ b/include/vut.h
@@ -56,9 +56,9 @@ struct VUT {
 	struct VSL_data	*vsl;
 	struct vsm	*vsm;
 	struct VSLQ	*vslq;
-	int		sighup;
-	int		sigint;
-	int		sigusr1;
+
+	sig_atomic_t	last_sighup;
+	sig_atomic_t	last_sigusr1;
 
 	/* Callback functions */
 	VUT_cb_f	*idle_f;
diff --git a/lib/libvarnishapi/Makefile.am b/lib/libvarnishapi/Makefile.am
index 5e13de019..77e96d666 100644
--- a/lib/libvarnishapi/Makefile.am
+++ b/lib/libvarnishapi/Makefile.am
@@ -31,18 +31,19 @@ libvarnishapi_la_SOURCES = \
 	../libvarnish/vtim.c \
 	../libvarnish/vnum.c \
 	../libvarnish/vsha256.c \
-	vsm.c \
+	vsc.c \
+	vsig.c \
+	vsl.c \
 	vsl_arg.c \
 	vsl_cursor.c \
 	vsl_dispatch.c \
 	vsl_query.c \
-	vsl.c \
-	vsc.c \
+	vsm.c \
 	vut.c \
 	vxp.c \
-	vxp_parse.c \
-	vxp_lexer.c \
 	vxp_fixed_token.c \
+	vxp_lexer.c \
+	vxp_parse.c \
 	libvarnishapi.map
 
 if ! HAVE_DAEMON
diff --git a/lib/libvarnishapi/libvarnishapi.map b/lib/libvarnishapi/libvarnishapi.map
index 1c8a6ff2b..addfb3077 100644
--- a/lib/libvarnishapi/libvarnishapi.map
+++ b/lib/libvarnishapi/libvarnishapi.map
@@ -148,3 +148,22 @@ LIBVARNISHAPI_2.1 {
     local:
 	*;
 };
+
+LIBVARNISHAPI_2.2 {
+    global:
+	# vsig.c
+	VSIG_int;
+	VSIG_Got_int;
+	VSIG_Arm_int;
+	VSIG_hup;
+	VSIG_Got_hup;
+	VSIG_Arm_hup;
+	VSIG_term;
+	VSIG_Got_term;
+	VSIG_Arm_term;
+	VSIG_usr1;
+	VSIG_Got_usr1;
+	VSIG_Arm_usr1;
+    local:
+	*;
+};
diff --git a/lib/libvarnishapi/vsig.c b/lib/libvarnishapi/vsig.c
new file mode 100644
index 000000000..924973e3c
--- /dev/null
+++ b/lib/libvarnishapi/vsig.c
@@ -0,0 +1,56 @@
+/*-
+ * Copyright (c) 2018 Varnish Software AS
+ * All rights reserved.
+ *
+ * Author: Poul-Henning Kamp <phk at phk.freebsd.dk>
+ *
+ * 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.
+ *
+ * Common functions for the utilities
+ */
+
+#include "config.h"
+
+#include <signal.h>
+
+#include "vapi/vsig.h"
+
+#define VSIG_SIGNAL(UPPER, lower)				\
+	sig_atomic_t VSIG_##lower;
+#include "tbl/vsig_list.h"
+
+#define VSIG_SIGNAL(UPPER, lower)				\
+	void							\
+	VSIG_Got_##lower(int sig)				\
+	{							\
+		(void)sig;					\
+		VSIG_##lower++;					\
+	}
+#include "tbl/vsig_list.h"
+
+#define VSIG_SIGNAL(UPPER, lower)				\
+	void							\
+	VSIG_Arm_##lower(void)					\
+	{							\
+		(void)signal(SIG##UPPER, VSIG_Got_##lower);	\
+	}
+#include "tbl/vsig_list.h"
diff --git a/lib/libvarnishapi/vut.c b/lib/libvarnishapi/vut.c
index 9c767bfe7..61c200147 100644
--- a/lib/libvarnishapi/vut.c
+++ b/lib/libvarnishapi/vut.c
@@ -54,6 +54,7 @@
 #include "vut.h"
 
 #include "vapi/voptget.h"
+#include "vapi/vsig.h"
 
 static int vut_synopsis(const struct vopt_spec *);
 static int vut_options(const struct vopt_spec *);
@@ -206,6 +207,11 @@ VUT_Init(const char *progname, int argc, char * const *argv,
 	AN(argv);
 	AN(voc);
 
+	VSIG_Arm_hup();
+	VSIG_Arm_int();
+	VSIG_Arm_term();
+	VSIG_Arm_usr1();
+
 	ALLOC_OBJ(vut, VUT_MAGIC);
 	AN(vut);
 
@@ -243,9 +249,9 @@ VUT_Signaled(struct VUT *vut, int sig)
 {
 
 	CHECK_OBJ_NOTNULL(vut, VUT_MAGIC);
-	vut->sighup |= (int)(sig == SIGHUP);
-	vut->sigint |= (int)(sig == SIGINT || sig == SIGTERM);
-	vut->sigusr1 |= (int)(sig == SIGUSR1);
+#define VSIG_SIGNAL(UPPER, lower) \
+	VSIG_##lower += (int)(sig == SIG##UPPER);
+#include "tbl/vsig_list.h"
 }
 
 void
@@ -353,18 +359,21 @@ VUT_Main(struct VUT *vut)
 	CHECK_OBJ_NOTNULL(vut, VUT_MAGIC);
 	AN(vut->vslq);
 
-	while (!vut->sigint) {
-		if (vut->sighup && vut->sighup_f) {
+	while (!VSIG_int && !VSIG_term) {
+		if (VSIG_hup != vut->last_sighup) {
 			/* sighup callback */
-			vut->sighup = 0;
-			i = vut->sighup_f(vut);
+			vut->last_sighup = VSIG_hup;
+			if (vut->sighup_f != NULL)
+				i = vut->sighup_f(vut);
+			else
+				i = 1;
 			if (i)
 				break;
 		}
 
-		if (vut->sigusr1) {
+		if (VSIG_usr1 != vut->last_sigusr1) {
 			/* Flush and report any incomplete records */
-			vut->sigusr1 = 0;
+			vut->last_sigusr1 = VSIG_usr1;
 			(void)VSLQ_Flush(vut->vslq, vut_dispatch, vut);
 		}
 
@@ -412,7 +421,7 @@ VUT_Main(struct VUT *vut)
 
 		do
 			i = VSLQ_Dispatch(vut->vslq, vut_dispatch, vut);
-		while (i == vsl_more && !vut->sighup && !vut->sigusr1);
+		while (i == vsl_more && !VSIG_hup && !VSIG_usr1);
 
 		if (i == vsl_more)
 			continue;


More information about the varnish-commit mailing list