[master] d72139a Add -P (PID-file) and -D (daemon mode) options to the utilities libraries.

Martin Blix Grydeland martin at varnish-cache.org
Thu Jun 13 12:41:24 CEST 2013


commit d72139a6ab5c801f1898f08e3f192a4cd9cc441c
Author: Martin Blix Grydeland <martin at varnish-software.com>
Date:   Mon May 27 16:29:59 2013 +0200

    Add -P (PID-file) and -D (daemon mode) options to the utilities libraries.
    
    Make varnishlog use these options.

diff --git a/bin/varnishlog/varnishlog_options.h b/bin/varnishlog/varnishlog_options.h
index 801ca38..0eb4821 100644
--- a/bin/varnishlog/varnishlog_options.h
+++ b/bin/varnishlog/varnishlog_options.h
@@ -28,13 +28,16 @@
  */
 
 #include "vapi/vapi_options.h"
+#include "vut_options.h"
 
 VSL_OPT_a
 VSL_OPT_d
+VUT_OPT_D
 VSL_OPT_g
 VSL_OPT_i
 VSM_OPT_n
 VSM_OPT_N
+VUT_OPT_P
 VSL_OPT_r
 VSL_OPT_u
 VSL_OPT_v
diff --git a/include/vut.h b/include/vut.h
index dcb5a77..a016994 100644
--- a/include/vut.h
+++ b/include/vut.h
@@ -35,7 +35,9 @@ struct VUT {
 	/* Options */
 	int		a_opt;
 	int		d_opt;
+	int		D_opt;
 	int		g_arg;
+	char		*P_arg;
 	char		*r_arg;
 	int		u_opt;
 	char		*w_arg;
@@ -46,6 +48,7 @@ struct VUT {
 	struct VSM_data	*vsm;
 	struct VSLQ	*vslq;
 	FILE		*fo;
+	struct vpf_fh	*pfh;
 };
 
 extern struct VUT VUT;
diff --git a/include/vut_options.h b/include/vut_options.h
new file mode 100644
index 0000000..3034b41
--- /dev/null
+++ b/include/vut_options.h
@@ -0,0 +1,40 @@
+/*-
+ * Copyright (c) 2006 Verdens Gang AS
+ * Copyright (c) 2006-2013 Varnish Software AS
+ * All rights reserved.
+ *
+ * Author: Martin Blix Grydeland <martin at varnish-software.com>
+ *
+ * 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.
+ */
+
+/* VUT options */
+
+#define VUT_OPT_D							\
+	VOPT("D", "[-D]", "Daemonize",					\
+	    "Daemonize."						\
+	)
+
+#define VUT_OPT_P							\
+	VOPT("P:", "[-P file]", "PID file",				\
+		"Write the process' PID to the specified file."		\
+	)
diff --git a/lib/libvarnishtools/vut.c b/lib/libvarnishtools/vut.c
index 29ff56b..f4f99c0 100644
--- a/lib/libvarnishtools/vut.c
+++ b/lib/libvarnishtools/vut.c
@@ -29,6 +29,8 @@
  * Common functions for the utilities
  */
 
+#include "config.h"
+
 #include <stdint.h>
 #include <stdarg.h>
 #include <stdlib.h>
@@ -38,6 +40,7 @@
 #include <string.h>
 
 #include "compat/daemon.h"
+#include "vpf.h"
 #include "vapi/vsm.h"
 #include "vapi/vsc.h"
 #include "vapi/vsl.h"
@@ -49,6 +52,15 @@
 
 struct VUT VUT;
 
+static void
+vut_vpf_remove(void)
+{
+	if (VUT.pfh) {
+		VPF_Remove(VUT.pfh);
+		VUT.pfh = NULL;
+	}
+}
+
 void
 VUT_Error(int status, const char *fmt, ...)
 {
@@ -84,6 +96,17 @@ VUT_Arg(int opt, const char *arg)
 		/* Binary file append */
 		VUT.a_opt = 1;
 		return (1);
+	case 'd':
+		/* Head */
+		VUT.d_opt = 1;
+		return (1);
+	case 'D':
+		/* Daemon mode */
+		VUT.D_opt = 1;
+		return (1);
+	case 'g':
+		/* Grouping */
+		return (VUT_g_Arg(arg));
 	case 'n':
 		/* Varnish instance */
 		if (VUT.vsm == NULL)
@@ -92,13 +115,10 @@ VUT_Arg(int opt, const char *arg)
 		if (VSM_n_Arg(VUT.vsm, arg) <= 0)
 			VUT_Error(1, "%s", VSM_Error(VUT.vsm));
 		return (1);
-	case 'd':
-		/* Head */
-		VUT.d_opt = 1;
+	case 'P':
+		/* PID file */
+		REPLACE(VUT.P_arg, arg);
 		return (1);
-	case 'g':
-		/* Grouping */
-		return (VUT_g_Arg(arg));
 	case 'r':
 		/* Binary file input */
 		REPLACE(VUT.r_arg, arg);
@@ -165,12 +185,34 @@ VUT_Setup(void)
 	if (VUT.vslq == NULL)
 		VUT_Error(1, "Query parse error (%s)", VSL_Error(VUT.vsl));
 	AZ(c);
+
+	/* Open PID file */
+	if (VUT.P_arg) {
+		AZ(VUT.pfh);
+		VUT.pfh = VPF_Open(VUT.P_arg, 0644, NULL);
+		if (VUT.pfh == NULL)
+			VUT_Error(1, "%s: %s", VUT.P_arg, strerror(errno));
+	}
+
+	/* Daemon mode */
+	if (VUT.D_opt && varnish_daemon(0, 0) == -1)
+		VUT_Error(1, "Daemon mode: %s", strerror(errno));
+
+	/* Write PID and setup exit handler */
+	if (VUT.pfh != NULL) {
+		VPF_Write(VUT.pfh);
+		AZ(atexit(&vut_vpf_remove));
+	}
 }
 
 void
 VUT_Fini(void)
 {
 	free(VUT.r_arg);
+	free(VUT.P_arg);
+
+	vut_vpf_remove();
+	AZ(VUT.pfh);
 
 	if (VUT.vslq)
 		VSLQ_Delete(&VUT.vslq);



More information about the varnish-commit mailing list