[master] 7559381 Bring flopen/fltest into the V* namespace

Poul-Henning Kamp phk at FreeBSD.org
Tue May 10 10:38:05 CEST 2016


commit 75593811e651e423d4d14a775bbf3913de5951c9
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Tue May 10 08:36:59 2016 +0000

    Bring flopen/fltest into the V* namespace

diff --git a/bin/varnishd/mgt/mgt_shmem.c b/bin/varnishd/mgt/mgt_shmem.c
index 9345d38..fe6a638 100644
--- a/bin/varnishd/mgt/mgt_shmem.c
+++ b/bin/varnishd/mgt/mgt_shmem.c
@@ -44,7 +44,7 @@
 #include "mgt/mgt.h"
 #include "common/heritage.h"
 
-#include "flopen.h"
+#include "vfl.h"
 #include "vsm_priv.h"
 #include "vmb.h"
 #include "vfil.h"
@@ -112,7 +112,7 @@ vsm_n_check(void)
 		fprintf(stderr,
 		    "VSM (%s) not a regular file.\n", VSM_FILENAME);
 	} else {
-		i = fltest(fd, &pid);
+		i = VFL_Test(fd, &pid);
 		if (i < 0) {
 			fprintf(stderr,
 			    "Cannot determine locking status of VSM (%s)\n.",
@@ -150,7 +150,7 @@ vsm_zerofile(const char *fn, ssize_t size)
 	int fd;
 	int flags;
 
-	fd = flopen(fn, O_RDWR | O_CREAT | O_EXCL | O_NONBLOCK, 0640);
+	fd = VFL_Open(fn, O_RDWR | O_CREAT | O_EXCL | O_NONBLOCK, 0640);
 	if (fd < 0) {
 		fprintf(stderr, "Could not create %s: %s\n",
 		    fn, strerror(errno));
diff --git a/bin/varnishhist/Makefile.am b/bin/varnishhist/Makefile.am
index 21d4285..e4d5e97 100644
--- a/bin/varnishhist/Makefile.am
+++ b/bin/varnishhist/Makefile.am
@@ -13,7 +13,7 @@ varnishhist_SOURCES = varnishhist.c \
 	$(top_srcdir)/lib/libvarnish/version.c \
 	$(top_srcdir)/lib/libvarnish/vpf.c \
 	$(top_srcdir)/lib/libvarnish/vtim.c \
-	$(top_srcdir)/lib/libvarnish/flopen.c \
+	$(top_srcdir)/lib/libvarnish/vfl.c \
 	$(top_srcdir)/lib/libvarnishtools/vut.c
 
 varnishhist_CFLAGS = \
diff --git a/bin/varnishlog/Makefile.am b/bin/varnishlog/Makefile.am
index 9747e49..d291d62 100644
--- a/bin/varnishlog/Makefile.am
+++ b/bin/varnishlog/Makefile.am
@@ -12,8 +12,8 @@ varnishlog_SOURCES = \
 	varnishlog_options.c \
 	$(top_srcdir)/lib/libvarnishtools/vut.c \
 	$(top_srcdir)/lib/libvarnish/vas.c \
-	$(top_srcdir)/lib/libvarnish/flopen.c \
 	$(top_srcdir)/lib/libvarnish/version.c \
+	$(top_srcdir)/lib/libvarnish/vfl.c \
 	$(top_srcdir)/lib/libvarnish/vsb.c \
 	$(top_srcdir)/lib/libvarnish/vpf.c \
 	$(top_srcdir)/lib/libvarnish/vtim.c
diff --git a/bin/varnishncsa/Makefile.am b/bin/varnishncsa/Makefile.am
index b51df5f..077b33c 100644
--- a/bin/varnishncsa/Makefile.am
+++ b/bin/varnishncsa/Makefile.am
@@ -14,8 +14,8 @@ varnishncsa_SOURCES = \
 	base64.c \
 	$(top_srcdir)/lib/libvarnishtools/vut.c \
 	$(top_srcdir)/lib/libvarnish/vas.c \
-	$(top_srcdir)/lib/libvarnish/flopen.c \
 	$(top_srcdir)/lib/libvarnish/version.c \
+	$(top_srcdir)/lib/libvarnish/vfl.c \
 	$(top_srcdir)/lib/libvarnish/vpf.c \
 	$(top_srcdir)/lib/libvarnish/vtim.c \
 	$(top_srcdir)/lib/libvarnish/vsb.c
diff --git a/bin/varnishtop/Makefile.am b/bin/varnishtop/Makefile.am
index f176728..afed891 100644
--- a/bin/varnishtop/Makefile.am
+++ b/bin/varnishtop/Makefile.am
@@ -11,8 +11,8 @@ varnishtop_SOURCES = varnishtop.c \
 	varnishtop_options.c \
 	$(top_srcdir)/lib/libvarnishtools/vut.c \
 	$(top_srcdir)/lib/libvarnish/vas.c \
-	$(top_srcdir)/lib/libvarnish/flopen.c \
 	$(top_srcdir)/lib/libvarnish/version.c \
+	$(top_srcdir)/lib/libvarnish/vfl.c \
 	$(top_srcdir)/lib/libvarnish/vpf.c \
 	$(top_srcdir)/lib/libvarnish/vtim.c \
 	$(top_srcdir)/lib/libvarnish/vsb.c
diff --git a/include/Makefile.am b/include/Makefile.am
index 816e416..d1c2670 100644
--- a/include/Makefile.am
+++ b/include/Makefile.am
@@ -66,7 +66,7 @@ nobase_noinst_HEADERS = \
 	binary_heap.h \
 	compat/daemon.h \
 	compat/execinfo.h \
-	flopen.h \
+	vfl.h \
 	libvcc.h \
 	vcli_common.h \
 	vcli_priv.h \
diff --git a/include/flopen.h b/include/flopen.h
deleted file mode 100644
index 9833efa..0000000
--- a/include/flopen.h
+++ /dev/null
@@ -1,37 +0,0 @@
-/*-
- * Copyright (c) 2007 Dag-Erling Coïdan Smørgrav
- * All rights reserved.
- *
- * 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
- *    in this position and unchanged.
- * 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 THE 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.
- *
- * Derived from:
- * $FreeBSD: src/lib/libutil/libutil.h,v 1.44 2007/05/10 15:01:42 des Exp $
- */
-
-#ifndef FLOPEN_H_INCLUDED
-#define FLOPEN_H_INCLUDED
-
-int flopen(const char *, int, ...);
-int fltest(int fd, pid_t *pid);
-
-#endif
diff --git a/include/vfl.h b/include/vfl.h
new file mode 100644
index 0000000..a07b21f
--- /dev/null
+++ b/include/vfl.h
@@ -0,0 +1,37 @@
+/*-
+ * Copyright (c) 2007 Dag-Erling Coïdan Smørgrav
+ * All rights reserved.
+ *
+ * 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
+ *    in this position and unchanged.
+ * 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 THE 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.
+ *
+ * Derived from:
+ * $FreeBSD: src/lib/libutil/libutil.h,v 1.44 2007/05/10 15:01:42 des Exp $
+ */
+
+#ifndef VFL_H_INCLUDED
+#define VFL_H_INCLUDED
+
+int VFL_Open(const char *, int, ...);
+int VFL_Test(int fd, pid_t *pid);
+
+#endif
diff --git a/lib/libvarnish/Makefile.am b/lib/libvarnish/Makefile.am
index bdda37d..6d6665a 100644
--- a/lib/libvarnish/Makefile.am
+++ b/lib/libvarnish/Makefile.am
@@ -17,7 +17,7 @@ libvarnish_la_SOURCES = \
 	cli_auth.c \
 	cli_common.c \
 	cli_serve.c \
-	flopen.c \
+	vfl.c \
 	vnum.c \
 	vtim.c \
 	vtcp.c \
diff --git a/lib/libvarnish/flopen.c b/lib/libvarnish/flopen.c
deleted file mode 100644
index 483d8d6..0000000
--- a/lib/libvarnish/flopen.c
+++ /dev/null
@@ -1,133 +0,0 @@
-/*-
- * Copyright (c) 2007 Dag-Erling Coïdan Smørgrav
- * All rights reserved.
- *
- * 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
- *    in this position and unchanged.
- * 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 THE 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.
- * Derived from:
- * $FreeBSD: head/lib/libutil/flopen.c 184094 2008-10-20 18:11:30Z des $
- */
-
-#include "config.h"
-
-#include <sys/stat.h>
-
-#include <errno.h>
-#include <fcntl.h>
-#include <stdarg.h>
-#include <string.h>
-#include <unistd.h>
-
-#include "flopen.h"
-
-int
-flopen(const char *path, int flags, ...)
-{
-	int fd, operation, serrno, trunc;
-	struct flock lock;
-	struct stat sb, fsb;
-	mode_t mode;
-
-#ifdef O_EXLOCK
-	flags &= ~O_EXLOCK;
-#endif
-
-	mode = 0;
-	if (flags & O_CREAT) {
-		va_list ap;
-
-		va_start(ap, flags);
-		mode = (mode_t)va_arg(ap, int); /* mode_t promoted to int */
-		va_end(ap);
-	}
-
-	memset(&lock, 0, sizeof lock);
-	lock.l_type = ((flags & O_ACCMODE) == O_RDONLY) ? F_RDLCK : F_WRLCK;
-	lock.l_whence = SEEK_SET;
-	operation = (flags & O_NONBLOCK) ? F_SETLK : F_SETLKW;
-
-	trunc = (flags & O_TRUNC);
-	flags &= ~O_TRUNC;
-
-	for (;;) {
-		if ((fd = open(path, flags, mode)) == -1)
-			/* non-existent or no access */
-			return (-1);
-		if (fcntl(fd, operation, &lock) == -1) {
-			/* unsupported or interrupted */
-			serrno = errno;
-			(void)close(fd);
-			errno = serrno;
-			return (-1);
-		}
-		if (stat(path, &sb) == -1) {
-			/* disappeared from under our feet */
-			(void)close(fd);
-			continue;
-		}
-		if (fstat(fd, &fsb) == -1) {
-			/* can't happen [tm] */
-			serrno = errno;
-			(void)close(fd);
-			errno = serrno;
-			return (-1);
-		}
-		if (sb.st_dev != fsb.st_dev ||
-		    sb.st_ino != fsb.st_ino) {
-			/* changed under our feet */
-			(void)close(fd);
-			continue;
-		}
-		if (trunc && ftruncate(fd, 0) != 0) {
-			/* can't happen [tm] */
-			serrno = errno;
-			(void)close(fd);
-			errno = serrno;
-			return (-1);
-		}
-		return (fd);
-	}
-}
-
-/* Tests if the given fd is locked through flopen
- * If pid is non-NULL, stores the pid of the process holding the lock there
- * Returns 1 if the file is locked
- * Returns 0 if the file is unlocked
- * Returns -1 on error (and errno)
- */
-int
-fltest(int fd, pid_t *pid)
-{
-	struct flock lock;
-
-	memset(&lock, 0, sizeof lock);
-	lock.l_type = F_WRLCK;
-	lock.l_whence = SEEK_SET;
-
-	if (fcntl(fd, F_GETLK, &lock) == -1)
-		return (-1);
-	if (lock.l_type == F_UNLCK)
-		return (0);
-	if (pid != NULL)
-		*pid = lock.l_pid;
-	return (1);
-}
diff --git a/lib/libvarnish/vfl.c b/lib/libvarnish/vfl.c
new file mode 100644
index 0000000..f7302ec
--- /dev/null
+++ b/lib/libvarnish/vfl.c
@@ -0,0 +1,133 @@
+/*-
+ * Copyright (c) 2007 Dag-Erling Coïdan Smørgrav
+ * All rights reserved.
+ *
+ * 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
+ *    in this position and unchanged.
+ * 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 THE 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.
+ * Derived from:
+ * $FreeBSD: head/lib/libutil/flopen.c 184094 2008-10-20 18:11:30Z des $
+ */
+
+#include "config.h"
+
+#include <sys/stat.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdarg.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "vfl.h"
+
+int
+VFL_Open(const char *path, int flags, ...)
+{
+	int fd, operation, serrno, trunc;
+	struct flock lock;
+	struct stat sb, fsb;
+	mode_t mode;
+
+#ifdef O_EXLOCK
+	flags &= ~O_EXLOCK;
+#endif
+
+	mode = 0;
+	if (flags & O_CREAT) {
+		va_list ap;
+
+		va_start(ap, flags);
+		mode = (mode_t)va_arg(ap, int); /* mode_t promoted to int */
+		va_end(ap);
+	}
+
+	memset(&lock, 0, sizeof lock);
+	lock.l_type = ((flags & O_ACCMODE) == O_RDONLY) ? F_RDLCK : F_WRLCK;
+	lock.l_whence = SEEK_SET;
+	operation = (flags & O_NONBLOCK) ? F_SETLK : F_SETLKW;
+
+	trunc = (flags & O_TRUNC);
+	flags &= ~O_TRUNC;
+
+	for (;;) {
+		if ((fd = open(path, flags, mode)) == -1)
+			/* non-existent or no access */
+			return (-1);
+		if (fcntl(fd, operation, &lock) == -1) {
+			/* unsupported or interrupted */
+			serrno = errno;
+			(void)close(fd);
+			errno = serrno;
+			return (-1);
+		}
+		if (stat(path, &sb) == -1) {
+			/* disappeared from under our feet */
+			(void)close(fd);
+			continue;
+		}
+		if (fstat(fd, &fsb) == -1) {
+			/* can't happen [tm] */
+			serrno = errno;
+			(void)close(fd);
+			errno = serrno;
+			return (-1);
+		}
+		if (sb.st_dev != fsb.st_dev ||
+		    sb.st_ino != fsb.st_ino) {
+			/* changed under our feet */
+			(void)close(fd);
+			continue;
+		}
+		if (trunc && ftruncate(fd, 0) != 0) {
+			/* can't happen [tm] */
+			serrno = errno;
+			(void)close(fd);
+			errno = serrno;
+			return (-1);
+		}
+		return (fd);
+	}
+}
+
+/* Tests if the given fd is locked through flopen
+ * If pid is non-NULL, stores the pid of the process holding the lock there
+ * Returns 1 if the file is locked
+ * Returns 0 if the file is unlocked
+ * Returns -1 on error (and errno)
+ */
+int
+VFL_Test(int fd, pid_t *pid)
+{
+	struct flock lock;
+
+	memset(&lock, 0, sizeof lock);
+	lock.l_type = F_WRLCK;
+	lock.l_whence = SEEK_SET;
+
+	if (fcntl(fd, F_GETLK, &lock) == -1)
+		return (-1);
+	if (lock.l_type == F_UNLCK)
+		return (0);
+	if (pid != NULL)
+		*pid = lock.l_pid;
+	return (1);
+}
diff --git a/lib/libvarnish/vpf.c b/lib/libvarnish/vpf.c
index 7875c15..7670d87 100644
--- a/lib/libvarnish/vpf.c
+++ b/lib/libvarnish/vpf.c
@@ -40,7 +40,7 @@
 #include <string.h>
 #include <unistd.h>
 
-#include "flopen.h"
+#include "vfl.h"
 #include "vas.h"
 #include "vpf.h"
 
@@ -128,7 +128,7 @@ VPF_Open(const char *path, mode_t mode, pid_t *pidptr)
 	 * PID file will be truncated again in VPF_Write(), so
 	 * VPF_Write() can be called multiple times.
 	 */
-	fd = flopen(pfh->pf_path,
+	fd = VFL_Open(pfh->pf_path,
 	    O_WRONLY | O_CREAT | O_TRUNC | O_NONBLOCK, mode);
 	if (fd == -1) {
 		if (errno == EWOULDBLOCK && pidptr != NULL) {



More information about the varnish-commit mailing list