[master] 79d906b Track FreeBSD's pidfile.c as close as possible.

Poul-Henning Kamp phk at FreeBSD.org
Sat May 6 00:52:05 CEST 2017


commit 79d906b7b26e4dd6e878b9aea4e321daa712daa7
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Fri May 5 22:07:19 2017 +0000

    Track FreeBSD's pidfile.c as close as possible.

diff --git a/lib/libvarnish/vpf.c b/lib/libvarnish/vpf.c
index a5fd683..3da5748 100644
--- a/lib/libvarnish/vpf.c
+++ b/lib/libvarnish/vpf.c
@@ -76,7 +76,7 @@ vpf_read(const char *path, pid_t *pidptr)
 	char buf[16], *endptr;
 	int error, fd, i;
 
-	fd = open(path, O_RDONLY);
+	fd = open(path, O_RDONLY | O_CLOEXEC);
 	if (fd == -1)
 		return (errno);
 
@@ -85,6 +85,8 @@ vpf_read(const char *path, pid_t *pidptr)
 	(void)close(fd);
 	if (i == -1)
 		return (error);
+	else if (i == 0)
+		return (EAGAIN);
 	buf[i] = '\0';
 
 	*pidptr = strtol(buf, &endptr, 10);
@@ -105,17 +107,10 @@ VPF_Open(const char *path, mode_t mode, pid_t *pidptr)
 	if (pfh == NULL)
 		return (NULL);
 
-#if 0
-	if (path == NULL)
-		len = snprintf(pfh->pf_path, sizeof(pfh->pf_path),
-		    "/var/run/%s.pid", getprogname());
-	else
-#endif
-	{
-		assert(path != NULL);
-		len = snprintf(pfh->pf_path, sizeof(pfh->pf_path),
-		    "%s", path);
-	}
+	assert(path != NULL);
+	len = snprintf(pfh->pf_path, sizeof(pfh->pf_path),
+	    "%s", path);
+
 	if (len >= (int)sizeof(pfh->pf_path)) {
 		free(pfh);
 		errno = ENAMETOOLONG;
@@ -124,12 +119,12 @@ VPF_Open(const char *path, mode_t mode, pid_t *pidptr)
 
 	/*
 	 * Open the PID file and obtain exclusive lock.
-	 * We truncate PID file here only to remove old PID immediatelly,
+	 * We truncate PID file here only to remove old PID immediately,
 	 * PID file will be truncated again in VPF_Write(), so
 	 * VPF_Write() can be called multiple times.
 	 */
 	fd = VFL_Open(pfh->pf_path,
-	    O_WRONLY | O_CREAT | O_TRUNC | O_NONBLOCK, mode);
+	    O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC | O_NONBLOCK, mode);
 	if (fd == -1) {
 		if (errno == EWOULDBLOCK && pidptr != NULL) {
 			errno = vpf_read(pfh->pf_path, pidptr);
@@ -139,6 +134,7 @@ VPF_Open(const char *path, mode_t mode, pid_t *pidptr)
 		free(pfh);
 		return (NULL);
 	}
+
 	/*
 	 * Remember file information, so in VPF_Write() we are sure we write
 	 * to the proper descriptor.
diff --git a/tools/audit_vpf.sh b/tools/audit_vpf.sh
new file mode 100644
index 0000000..2115e6c
--- /dev/null
+++ b/tools/audit_vpf.sh
@@ -0,0 +1,18 @@
+#!/bin/sh
+#
+# Script to compare vpf.c with FreeBSD's pidfile.c
+#
+# Run this on a up-to-date FreeBSD source tree
+
+sed '
+s/vpf_/pidfile_/g
+s/VPF_/pidfile_/g
+s/pidfile_fh/pidfh/g
+s/pidfile_Write/pidfile_write/g
+s/pidfile_Close/pidfile_close/g
+s/pidfile_Remove/pidfile_remove/g
+s/pidfile_Open/pidfile_open/g
+s/	(void)/	/g
+' lib/libvarnish/vpf.c |
+    diff -ub /usr/src/lib/libutil/pidfile.c -
+



More information about the varnish-commit mailing list