[master] 06b3c20 And rename the various file-accesors to VFIL_

Poul-Henning Kamp phk at varnish-cache.org
Sun Oct 9 21:30:23 CEST 2011


commit 06b3c206e526c919722ff797aa9e7d0add3fd0b0
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Sun Oct 9 19:29:19 2011 +0000

    And rename the various file-accesors to VFIL_
    
    Remove srandomdev() look-alike, we already have a compat version

diff --git a/bin/varnishd/mgt_vcc.c b/bin/varnishd/mgt_vcc.c
index bbf813a..df46c41 100644
--- a/bin/varnishd/mgt_vcc.c
+++ b/bin/varnishd/mgt_vcc.c
@@ -44,6 +44,7 @@
 #include "vcli.h"
 #include "vsub.h"
 #include "vcl.h"
+#include "vfil.h"
 #include "cli_priv.h"
 #include "mgt_cli.h"
 
@@ -227,7 +228,7 @@ mgt_run_cc(const char *vcl, struct vsb *sb, int C_flag)
 	struct vcc_priv vp;
 
 	/* Create temporary C source file */
-	sfd = vtmpfile(sf);
+	sfd = VFIL_tmpfile(sf);
 	if (sfd < 0) {
 		VSB_printf(sb, "Failed to create %s: %s", sf, strerror(errno));
 		return (NULL);
@@ -245,7 +246,7 @@ mgt_run_cc(const char *vcl, struct vsb *sb, int C_flag)
 	}
 
 	if (C_flag) {
-		csrc = vreadfile(NULL, sf, NULL);
+		csrc = VFIL_readfile(NULL, sf, NULL);
 		XXXAN(csrc);
 		(void)fputs(csrc, stdout);
 		free(csrc);
@@ -517,7 +518,7 @@ mcf_config_load(struct cli *cli, const char * const *av, void *priv)
 		return;
 	}
 
-	vcl = vreadfile(mgt_vcl_dir, av[3], NULL);
+	vcl = VFIL_readfile(mgt_vcl_dir, av[3], NULL);
 	if (vcl == NULL) {
 		VCLI_Out(cli, "Cannot open '%s'", av[3]);
 		VCLI_SetResult(cli, CLIS_PARAM);
diff --git a/bin/varnishd/varnishd.c b/bin/varnishd/varnishd.c
index 360cdad..bf61ea3 100644
--- a/bin/varnishd/varnishd.c
+++ b/bin/varnishd/varnishd.c
@@ -55,12 +55,17 @@
 
 #include "vav.h"
 #include "vin.h"
+#include "vfil.h"
 #include "vtim.h"
 #include "heritage.h"
 #include "mgt.h"
 #include "hash_slinger.h"
 #include "stevedore.h"
 
+#ifndef HAVE_SRANDOMDEV
+#include "compat/srandomdev.h"
+#endif
+
 struct heritage		heritage;
 volatile struct params	*params;
 unsigned		d_flag = 0;
@@ -355,7 +360,7 @@ main(int argc, char * const *argv)
 	for (o = getdtablesize(); o > STDERR_FILENO; o--)
 		(void)close(o);
 
-	AZ(seed_random());
+	srandomdev();
 
 	mgt_got_fd(STDERR_FILENO);
 
@@ -543,7 +548,7 @@ main(int argc, char * const *argv)
 	}
 
 	if (f_arg != NULL) {
-		vcl = vreadfile(NULL, f_arg, NULL);
+		vcl = VFIL_readfile(NULL, f_arg, NULL);
 		if (vcl == NULL) {
 			fprintf(stderr, "Cannot read '%s': %s\n",
 			    f_arg, strerror(errno));
diff --git a/include/Makefile.am b/include/Makefile.am
index e852c58..70be7f7 100644
--- a/include/Makefile.am
+++ b/include/Makefile.am
@@ -44,6 +44,7 @@ nobase_noinst_HEADERS = \
 	vct.h \
 	vend.h \
 	vev.h \
+	vfil.h \
 	vin.h \
 	vlu.h \
 	vmb.h \
diff --git a/include/libvarnish.h b/include/libvarnish.h
index 1450578..65287b5 100644
--- a/include/libvarnish.h
+++ b/include/libvarnish.h
@@ -41,12 +41,6 @@ struct vsb;
 /* from libvarnish/version.c */
 void VCS_Message(const char *);
 
-/* from libvarnish/vtmpfile.c */
-int seed_random(void);
-int vtmpfile(char *);
-char *vreadfile(const char *pfx, const char *fn, ssize_t *sz);
-char *vreadfd(int fd, ssize_t *sz);
-
 /* Safe printf into a fixed-size buffer */
 #define bprintf(buf, fmt, ...)						\
 	do {								\
diff --git a/include/vfil.h b/include/vfil.h
new file mode 100644
index 0000000..74885ef
--- /dev/null
+++ b/include/vfil.h
@@ -0,0 +1,35 @@
+/*-
+ * Copyright (c) 2006 Verdens Gang AS
+ * Copyright (c) 2006-2011 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.
+ *
+ */
+
+/* from libvarnish/vfil.c */
+int seed_random(void);
+int VFIL_tmpfile(char *);
+char *VFIL_readfile(const char *pfx, const char *fn, ssize_t *sz);
+char *VFIL_readfd(int fd, ssize_t *sz);
diff --git a/lib/libvarnish/Makefile.am b/lib/libvarnish/Makefile.am
index e890904..ab2cc94 100644
--- a/lib/libvarnish/Makefile.am
+++ b/lib/libvarnish/Makefile.am
@@ -19,6 +19,7 @@ libvarnish_la_SOURCES = \
 	vct.c \
 	version.c \
 	vev.c \
+	vfil.c \
 	vin.c \
 	vlu.c \
 	vmb.c \
@@ -26,8 +27,7 @@ libvarnish_la_SOURCES = \
 	vre.c \
 	vsb.c \
 	vsha256.c \
-	vss.c \
-	vtmpfile.c
+	vss.c
 
 libvarnish_la_CFLAGS = -DVARNISH_STATE_DIR='"${VARNISH_STATE_DIR}"'
 libvarnish_la_LIBADD = ${RT_LIBS} ${NET_LIBS} ${LIBM} @PCRE_LIBS@
diff --git a/lib/libvarnish/vfil.c b/lib/libvarnish/vfil.c
new file mode 100644
index 0000000..884b106
--- /dev/null
+++ b/lib/libvarnish/vfil.c
@@ -0,0 +1,124 @@
+/*-
+ * Copyright (c) 2006 Verdens Gang AS
+ * Copyright (c) 2006-2011 Varnish Software AS
+ * All rights reserved.
+ *
+ * Author: Dag-Erling Smørgrav <des at des.no>
+ *
+ * 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.
+ */
+
+#include "config.h"
+
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <time.h>
+
+#include <sys/stat.h>
+
+#include "vas.h"
+#include "vfil.h"
+#include "libvarnish.h"
+
+int
+VFIL_tmpfile(char *template)
+{
+	char *b, *e, *p;
+	int fd;
+	char ran;
+
+	for (b = template; *b != '#'; ++b)
+		/* nothing */ ;
+	if (*b == '\0') {
+		errno = EINVAL;
+		return (-1);
+	}
+	for (e = b; *e == '#'; ++e)
+		/* nothing */ ;
+
+	for (;;) {
+		for (p = b; p < e; ++p) {
+			ran = random() % 63;
+			if (ran < 10)
+				*p = '0' + ran;
+			else if (ran < 36)
+				*p = 'A' + ran - 10;
+			else if (ran < 62)
+				*p = 'a' + ran - 36;
+			else
+				*p = '_';
+		}
+		fd = open(template, O_RDWR|O_CREAT|O_EXCL, 0600);
+		if (fd >= 0)
+			return (fd);
+		if (errno != EEXIST)
+			return (-1);
+	}
+	/* not reached */
+}
+
+char *
+VFIL_readfd(int fd, ssize_t *sz)
+{
+	struct stat st;
+	char *f;
+	int i;
+
+	assert(0 == fstat(fd, &st));
+	if (!S_ISREG(st.st_mode))
+		return (NULL);
+	f = malloc(st.st_size + 1);
+	assert(f != NULL);
+	i = read(fd, f, st.st_size);
+	assert(i == st.st_size);
+	f[i] = '\0';
+	if (sz != NULL)
+		*sz = st.st_size;
+	return (f);
+}
+
+char *
+VFIL_readfile(const char *pfx, const char *fn, ssize_t *sz)
+{
+	int fd, err;
+	char *r;
+	char fnb[PATH_MAX + 1];
+
+	if (fn[0] == '/')
+		fd = open(fn, O_RDONLY);
+	else if (pfx != NULL) {
+		bprintf(fnb, "/%s/%s", pfx, fn); /* XXX: graceful length check */
+		fd = open(fnb, O_RDONLY);
+	} else
+		fd = open(fn, O_RDONLY);
+	if (fd < 0)
+		return (NULL);
+	r = VFIL_readfd(fd, sz);
+	err = errno;
+	AZ(close(fd));
+	errno = err;
+	return (r);
+}
diff --git a/lib/libvarnish/vtmpfile.c b/lib/libvarnish/vtmpfile.c
deleted file mode 100644
index 327790a..0000000
--- a/lib/libvarnish/vtmpfile.c
+++ /dev/null
@@ -1,142 +0,0 @@
-/*-
- * Copyright (c) 2006 Verdens Gang AS
- * Copyright (c) 2006-2011 Varnish Software AS
- * All rights reserved.
- *
- * Author: Dag-Erling Smørgrav <des at des.no>
- *
- * 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.
- */
-
-#include "config.h"
-
-#include <errno.h>
-#include <fcntl.h>
-#include <limits.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <unistd.h>
-#include <time.h>
-
-#include <sys/stat.h>
-
-#include "libvarnish.h"
-
-int
-seed_random(void)
-{
-	int fd;
-	unsigned seed;
-
-	fd = open("/dev/urandom", O_RDONLY);
-	if (fd == -1) {
-		/* urandom not available, fall back to something
-		 * weaker */
-		srandom(time(NULL));
-		return (0);
-	}
-	if (read(fd, &seed, sizeof seed) != sizeof seed)
-		return (1);
-	(void)close(fd);
-	srandom(seed);
-	return (0);
-}
-
-int
-vtmpfile(char *template)
-{
-	char *b, *e, *p;
-	int fd;
-	char ran;
-
-	for (b = template; *b != '#'; ++b)
-		/* nothing */ ;
-	if (*b == '\0') {
-		errno = EINVAL;
-		return (-1);
-	}
-	for (e = b; *e == '#'; ++e)
-		/* nothing */ ;
-
-	for (;;) {
-		for (p = b; p < e; ++p) {
-			ran = random() % 63;
-			if (ran < 10)
-				*p = '0' + ran;
-			else if (ran < 36)
-				*p = 'A' + ran - 10;
-			else if (ran < 62)
-				*p = 'a' + ran - 36;
-			else
-				*p = '_';
-		}
-		fd = open(template, O_RDWR|O_CREAT|O_EXCL, 0600);
-		if (fd >= 0)
-			return (fd);
-		if (errno != EEXIST)
-			return (-1);
-	}
-	/* not reached */
-}
-
-char *
-vreadfd(int fd, ssize_t *sz)
-{
-	struct stat st;
-	char *f;
-	int i;
-
-	assert(0 == fstat(fd, &st));
-	if (!S_ISREG(st.st_mode))
-		return (NULL);
-	f = malloc(st.st_size + 1);
-	assert(f != NULL);
-	i = read(fd, f, st.st_size);
-	assert(i == st.st_size);
-	f[i] = '\0';
-	if (sz != NULL)
-		*sz = st.st_size;
-	return (f);
-}
-
-char *
-vreadfile(const char *pfx, const char *fn, ssize_t *sz)
-{
-	int fd, err;
-	char *r;
-	char fnb[PATH_MAX + 1];
-
-	if (fn[0] == '/')
-		fd = open(fn, O_RDONLY);
-	else if (pfx != NULL) {
-		bprintf(fnb, "/%s/%s", pfx, fn); /* XXX: graceful length check */
-		fd = open(fnb, O_RDONLY);
-	} else
-		fd = open(fn, O_RDONLY);
-	if (fd < 0)
-		return (NULL);
-	r = vreadfd(fd, sz);
-	err = errno;
-	AZ(close(fd));
-	errno = err;
-	return (r);
-}
diff --git a/lib/libvcl/vcc_compile.c b/lib/libvcl/vcc_compile.c
index 3af30c6..dbae2ab 100644
--- a/lib/libvcl/vcc_compile.c
+++ b/lib/libvcl/vcc_compile.c
@@ -61,6 +61,7 @@
 
 #include "vcc_compile.h"
 #include "libvcl.h"
+#include "vfil.h"
 
 struct method method_tab[] = {
 #define VCL_MET_MAC(l,U,m)	{ "vcl_"#l, m, VCL_MET_##U },
@@ -409,7 +410,7 @@ vcc_file_source(const struct vcc *tl, struct vsb *sb, const char *fn)
 	char *f;
 	struct source *sp;
 
-	f = vreadfile(tl->vcl_dir, fn, NULL);
+	f = VFIL_readfile(tl->vcl_dir, fn, NULL);
 	if (f == NULL) {
 		VSB_printf(sb, "Cannot read file '%s': %s\n",
 		    fn, strerror(errno));
diff --git a/lib/libvmod_std/vmod_std_fileread.c b/lib/libvmod_std/vmod_std_fileread.c
index e1be09a..cae57a2 100644
--- a/lib/libvmod_std/vmod_std_fileread.c
+++ b/lib/libvmod_std/vmod_std_fileread.c
@@ -42,6 +42,7 @@
 #include "../../bin/varnishd/cache.h"
 
 #include "vcc_if.h"
+#include "vfil.h"
 
 struct frfile {
 	unsigned			magic;
@@ -102,7 +103,7 @@ vmod_fileread(struct sess *sp, struct vmod_priv *priv, const char *file_name)
 		return (frf->contents);
 	}
 
-	s = vreadfile(NULL, file_name, NULL);
+	s = VFIL_readfile(NULL, file_name, NULL);
 	if (s != NULL) {
 		ALLOC_OBJ(frf, CACHED_FILE_MAGIC);
 		AN(frf);



More information about the varnish-commit mailing list