[master] fcfc338 Instead of our own home-rolled collision check, reuse VPF and always put a pid-file in the workdir.

Poul-Henning Kamp phk at FreeBSD.org
Fri May 5 21:19:05 CEST 2017


commit fcfc338d762c1e74e093defdb5d2c7a7ce90102e
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Fri May 5 19:17:56 2017 +0000

    Instead of our own home-rolled collision check, reuse VPF and always
    put a pid-file in the workdir.
    
    Improve the collision error message for -P by including the other pid.

diff --git a/bin/varnishd/mgt/mgt_main.c b/bin/varnishd/mgt/mgt_main.c
index 2d3a9dc..712c106 100644
--- a/bin/varnishd/mgt/mgt_main.c
+++ b/bin/varnishd/mgt/mgt_main.c
@@ -499,6 +499,7 @@ main(int argc, char * const *argv)
 	struct f_arg *fa;
 	struct vsb *vsb;
 	VTAILQ_HEAD(,f_arg) f_args = VTAILQ_HEAD_INITIALIZER(f_args);
+	pid_t pid;
 
 	setbuf(stdout, NULL);
 	setbuf(stderr, NULL);
@@ -787,9 +788,15 @@ main(int argc, char * const *argv)
 	}
 
 	VJ_master(JAIL_MASTER_FILE);
-	if (P_arg && (pfh = VPF_Open(P_arg, 0644, NULL)) == NULL)
-		ARGV_ERR("Could not open pid/lock (-P) file (%s): %s\n",
-		    P_arg, strerror(errno));
+	if (P_arg) {
+		pfh = VPF_Open(P_arg, 0644, &pid);
+		if (pfh == NULL && errno == EEXIST)
+			ARGV_ERR("Varnishd is already running (pid=%jd)\n",
+			    (intmax_t)pid);
+		if (pfh == NULL)
+			ARGV_ERR("Could not open pid-file (%s): %s\n",
+			    P_arg, strerror(errno));
+	}
 	VJ_master(JAIL_MASTER_LOW);
 
 	/* If no -s argument specified, process default -s argument */
diff --git a/bin/varnishd/mgt/mgt_shmem.c b/bin/varnishd/mgt/mgt_shmem.c
index e41018d..45bb39e 100644
--- a/bin/varnishd/mgt/mgt_shmem.c
+++ b/bin/varnishd/mgt/mgt_shmem.c
@@ -43,6 +43,7 @@
 #include "common/heritage.h"
 
 #include "vfl.h"
+#include "vpf.h"
 #include "vsm_priv.h"
 #include "vfil.h"
 
@@ -59,6 +60,8 @@
 static void *mgt_vsm_p;
 static ssize_t mgt_vsm_l;
 
+static struct vpf_fh *priv_vpf;
+
 /*--------------------------------------------------------------------
  * Use a bogo-VSM to hold master-copies of the VSM chunks the master
  * publishes, such as -S & -T arguments.
@@ -84,60 +87,6 @@ mgt_SHM_static_alloc(const void *ptr, ssize_t size,
 }
 
 /*--------------------------------------------------------------------
- * Check that we are not started with the same -n argument as an already
- * running varnishd.
- *
- * Non-zero return means we should exit and not trample the file.
- *
- */
-
-static int
-vsm_n_check(void)
-{
-	int fd, i;
-	struct stat st;
-	pid_t pid;
-	struct VSM_head vsmh;
-	int retval = 1;
-
-	fd = open(VSM_FILENAME, O_RDWR);
-	if (fd < 0)
-		return (0);
-
-	AZ(fstat(fd, &st));
-	if (!S_ISREG(st.st_mode)) {
-		fprintf(stderr,
-		    "VSM (%s) not a regular file.\n", VSM_FILENAME);
-	} else {
-		i = VFL_Test(fd, &pid);
-		if (i < 0) {
-			fprintf(stderr,
-			    "Cannot determine locking status of VSM (%s)\n.",
-			    VSM_FILENAME);
-		} else if (i == 0) {
-			/*
-			 * File is unlocked, mark it as dead, to help any
-			 * consumers still stuck on it.
-			 */
-			if (pread(fd, &vsmh, sizeof vsmh, 0) == sizeof vsmh) {
-				vsmh.alloc_seq = 0;
-				assert(sizeof vsmh ==
-				    pwrite(fd, &vsmh, sizeof vsmh, 0));
-			}
-			retval = 0;
-		} else {
-			/* The VSM is locked, we won't touch it. */
-			fprintf(stderr,
-			    "VSM locked by running varnishd master (pid=%jd)\n"
-			    "(Use unique -n arguments if you want"
-			    "  multiple instances)\n", (intmax_t)pid);
-		}
-	}
-	(void)close(fd);
-	return (retval);
-}
-
-/*--------------------------------------------------------------------
  * Build a zeroed file
  */
 
@@ -327,12 +276,15 @@ mgt_shm_atexit(void)
 void
 mgt_SHM_Init(void)
 {
-	int i;
-
-	/* Collision check with already running varnishd */
-	i = vsm_n_check();
-	if (i)
-		exit(2);
+	pid_t pid = 0;
+
+	priv_vpf = VPF_Open("_.pid", 0644, &pid);
+	if (priv_vpf == NULL && errno == EEXIST)
+		ARGV_ERR("Varnishd is already running (pid=%jd)\n",
+		    (intmax_t)pid);
+	if (priv_vpf == NULL)
+		ARGV_ERR("Failure on _.pid: %s\n", strerror(errno));
+	AZ(VPF_Write(priv_vpf));
 
 	/* Create our static VSM instance */
 	static_vsm = VSM_common_new(static_vsm_buf, sizeof static_vsm_buf);
diff --git a/bin/varnishtest/tests/b00045.vtc b/bin/varnishtest/tests/b00045.vtc
index 8863bad..47a676f 100644
--- a/bin/varnishtest/tests/b00045.vtc
+++ b/bin/varnishtest/tests/b00045.vtc
@@ -14,5 +14,14 @@ client c1 {
 
 delay .2
 
-shell -err -expect {Could not open pid/lock} \
-	"varnishd -P ${v1_name}/varnishd.pid -b 127.0.0.1:80 -a :0 -n ${tmpdir}"
+shell -err -expect {Error: Could not open pid-file} {
+	varnishd -P /dev/tty -b 127.0.0.1:80 -a :0 -n ${tmpdir}
+}
+
+shell -err -expect {Error: Varnishd is already running} {
+	varnishd -P ${v1_name}/varnishd.pid -b 127.0.0.1:80 -a :0 -n ${tmpdir}
+}
+
+shell -err -expect {Error: Varnishd is already running} {
+	varnishd -b 127.0.0.1:80 -a:0 -n ${tmpdir}/v1 -F
+}



More information about the varnish-commit mailing list