r4759 - in trunk/varnish-cache: bin/varnishd include lib/libvarnish lib/libvarnishapi

phk at varnish-cache.org phk at varnish-cache.org
Tue May 4 23:04:14 CEST 2010


Author: phk
Date: 2010-05-04 23:04:13 +0200 (Tue, 04 May 2010)
New Revision: 4759

Added:
   trunk/varnish-cache/lib/libvarnish/vin.c
Removed:
   trunk/varnish-cache/bin/varnishd/instance.c
   trunk/varnish-cache/lib/libvarnishapi/instance.c
Modified:
   trunk/varnish-cache/bin/varnishd/Makefile.am
   trunk/varnish-cache/bin/varnishd/heritage.h
   trunk/varnish-cache/bin/varnishd/varnishd.c
   trunk/varnish-cache/include/shmlog.h
   trunk/varnish-cache/include/varnishapi.h
   trunk/varnish-cache/lib/libvarnish/Makefile.am
   trunk/varnish-cache/lib/libvarnishapi/Makefile.am
   trunk/varnish-cache/lib/libvarnishapi/shmlog.c
Log:
Cleanup the -n argument to name/dir/shmfilename munging and put it in a
single sourcefile, which appears in both libvarnish and libvarnishapi.



Modified: trunk/varnish-cache/bin/varnishd/Makefile.am
===================================================================
--- trunk/varnish-cache/bin/varnishd/Makefile.am	2010-05-04 14:19:05 UTC (rev 4758)
+++ trunk/varnish-cache/bin/varnishd/Makefile.am	2010-05-04 21:04:13 UTC (rev 4759)
@@ -42,7 +42,6 @@
 	hash_classic.c \
 	hash_critbit.c \
 	hash_simple_list.c \
-	instance.c \
 	mgt_child.c \
 	mgt_cli.c \
 	mgt_param.c \

Modified: trunk/varnish-cache/bin/varnishd/heritage.h
===================================================================
--- trunk/varnish-cache/bin/varnishd/heritage.h	2010-05-04 14:19:05 UTC (rev 4758)
+++ trunk/varnish-cache/bin/varnishd/heritage.h	2010-05-04 21:04:13 UTC (rev 4759)
@@ -62,7 +62,7 @@
 	/* Hash method */
 	struct hash_slinger		*hash;
 
-	char				name[1024];
+	char				*name;
 	char                            identity[1024];
 };
 
@@ -211,6 +211,3 @@
 extern struct heritage heritage;
 
 void child_main(void);
-
-int varnish_instance(const char *n_arg, char *name, size_t namelen,
-    char *dir, size_t dirlen);

Deleted: trunk/varnish-cache/bin/varnishd/instance.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/instance.c	2010-05-04 14:19:05 UTC (rev 4758)
+++ trunk/varnish-cache/bin/varnishd/instance.c	2010-05-04 21:04:13 UTC (rev 4759)
@@ -1 +0,0 @@
-link ../../lib/libvarnishapi/instance.c
\ No newline at end of file

Modified: trunk/varnish-cache/bin/varnishd/varnishd.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/varnishd.c	2010-05-04 14:19:05 UTC (rev 4758)
+++ trunk/varnish-cache/bin/varnishd/varnishd.c	2010-05-04 21:04:13 UTC (rev 4759)
@@ -422,7 +422,7 @@
 	char *p, *vcl = NULL;
 	struct cli cli[1];
 	struct pidfh *pfh = NULL;
-	char dirname[1024];
+	char *dirname;
 
 	/*
 	 * Start out by closing all unwanted file descriptors we might
@@ -615,8 +615,7 @@
 		}
 	}
 
-	if (varnish_instance(n_arg, heritage.name, sizeof heritage.name,
-	    dirname, sizeof dirname) != 0) {
+	if (vin_n_arg(n_arg, &heritage.name, &dirname, NULL) != 0) {
 		fprintf(stderr, "Invalid instance name: %s\n",
 		    strerror(errno));
 		exit(1);

Modified: trunk/varnish-cache/include/shmlog.h
===================================================================
--- trunk/varnish-cache/include/shmlog.h	2010-05-04 14:19:05 UTC (rev 4758)
+++ trunk/varnish-cache/include/shmlog.h	2010-05-04 21:04:13 UTC (rev 4759)
@@ -109,4 +109,7 @@
 	SLT_WRAPMARKER = 255
 };
 
+/* This function lives in both libvarnish and libvarnishapi */
+int vin_n_arg(const char *n_arg, char **name, char **dir, char **vsl);
+
 #endif

Modified: trunk/varnish-cache/include/varnishapi.h
===================================================================
--- trunk/varnish-cache/include/varnishapi.h	2010-05-04 14:19:05 UTC (rev 4758)
+++ trunk/varnish-cache/include/varnishapi.h	2010-05-04 21:04:13 UTC (rev 4759)
@@ -60,7 +60,4 @@
 const char *VSL_Name(void);
 extern const char *VSL_tags[256];
 
-/* instance.c */
-int varnish_instance(const char *n_arg, char *name, size_t namelen, char *dir,
-    size_t dirlen);
 #endif

Modified: trunk/varnish-cache/lib/libvarnish/Makefile.am
===================================================================
--- trunk/varnish-cache/lib/libvarnish/Makefile.am	2010-05-04 14:19:05 UTC (rev 4758)
+++ trunk/varnish-cache/lib/libvarnish/Makefile.am	2010-05-04 21:04:13 UTC (rev 4759)
@@ -22,6 +22,7 @@
 	vct.c \
 	version.c \
 	vev.c \
+	vin.c \
 	vlu.c \
 	vpf.c \
 	vre.c \
@@ -30,6 +31,7 @@
 	vss.c \
 	vtmpfile.c
 
+libvarnish_la_CFLAGS = -DVARNISH_STATE_DIR='"${VARNISH_STATE_DIR}"'
 libvarnish_la_LIBADD = ${RT_LIBS} ${NET_LIBS} ${LIBM} @PCRE_LIBS@
 
 DISTCLEANFILES = svn_version.c

Added: trunk/varnish-cache/lib/libvarnish/vin.c
===================================================================
--- trunk/varnish-cache/lib/libvarnish/vin.c	                        (rev 0)
+++ trunk/varnish-cache/lib/libvarnish/vin.c	2010-05-04 21:04:13 UTC (rev 4759)
@@ -0,0 +1,102 @@
+/*-
+ * Copyright (c) 2007-2009 Linpro 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.
+ *
+ * XXX: NB: also used in libvarnishapi
+ */
+
+#include "config.h"
+
+#include "svnid.h"
+SVNID("$Id: instance.c 4093 2009-06-06 15:56:17Z des $")
+
+#include <errno.h>
+#include <limits.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "libvarnish.h"
+#include "shmlog.h"
+
+int
+vin_n_arg(const char *n_arg, char **name, char **dir, char **vsl)
+{
+	char nm[PATH_MAX];
+	char dn[PATH_MAX];
+
+
+	/* First: determine the name */
+
+	if (n_arg == NULL || *n_arg == '\0') {
+		if (gethostname(nm, sizeof nm) != 0)
+			return (-1);
+	} else if (strlen(n_arg) >= sizeof nm) {
+		/* preliminary length check to avoid overflowing nm */
+		errno = ENAMETOOLONG;
+		return (-1);
+	} else
+		bprintf(nm, "%s", n_arg);
+
+
+	/* Second: find the directory name */
+
+	if (*nm == '/')
+		strcpy(dn, nm);
+	else if (strlen(VARNISH_STATE_DIR) + 1 + strlen(nm) >= sizeof dn){
+		/* preliminary length check to avoid overflowing dm */
+		errno = ENAMETOOLONG;
+		return (-1);
+	} else {
+		bprintf(dn, "%s/%s", VARNISH_STATE_DIR, nm);
+	}
+
+	/* Definitive length check */
+	if (strlen(dn) + 1 + strlen(SHMLOG_FILENAME) >= sizeof dn) {
+		errno = ENAMETOOLONG;
+		return (-1);
+	}
+
+	strcat(dn, "/");
+
+	if (name != NULL) {
+		*name = strdup(nm);
+		if (*name == NULL)
+			return (-1);
+	}
+	if (dir != NULL) {
+		*dir = strdup(dn);
+		if (*dir == NULL)
+			return (-1);
+	}
+	if (vsl != NULL) {
+		bprintf(nm, "%s%s", dn, SHMLOG_FILENAME);
+		*vsl = strdup(nm);
+		if (*vsl == NULL)
+			return (-1);
+	}
+	return (0);
+}

Modified: trunk/varnish-cache/lib/libvarnishapi/Makefile.am
===================================================================
--- trunk/varnish-cache/lib/libvarnishapi/Makefile.am	2010-05-04 14:19:05 UTC (rev 4758)
+++ trunk/varnish-cache/lib/libvarnishapi/Makefile.am	2010-05-04 21:04:13 UTC (rev 4759)
@@ -7,8 +7,8 @@
 libvarnishapi_la_LDFLAGS = -version-info 1:0:0
 
 libvarnishapi_la_SOURCES = \
+	../libvarnish/vin.c \
 	base64.c \
-	instance.c \
 	shmlog.c
 
 libvarnishapi_la_CFLAGS = \

Deleted: trunk/varnish-cache/lib/libvarnishapi/instance.c
===================================================================
--- trunk/varnish-cache/lib/libvarnishapi/instance.c	2010-05-04 14:19:05 UTC (rev 4758)
+++ trunk/varnish-cache/lib/libvarnishapi/instance.c	2010-05-04 21:04:13 UTC (rev 4759)
@@ -1,67 +0,0 @@
-/*-
- * Copyright (c) 2007-2009 Linpro 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 "svnid.h"
-SVNID("$Id$")
-
-#include <errno.h>
-#include <stdio.h>
-#include <unistd.h>
-
-#include "varnishapi.h"
-
-int
-varnish_instance(const char *n_arg,
-    char *name, size_t namelen, char *dir, size_t dirlen)
-{
-	size_t len;
-
-	if (n_arg == NULL) {
-		if (gethostname(name, namelen) != 0)
-			return (-1);
-	} else {
-		len = snprintf(name, namelen, "%s", n_arg);
-		if (len >= namelen) {
-			errno = ENAMETOOLONG;
-			return (-1);
-		}
-	}
-
-	if (*name == '/')
-		len = snprintf(dir, dirlen, "%s", name);
-	else
-		len = snprintf(dir, dirlen, "%s/%s", VARNISH_STATE_DIR, name);
-
-	if (len >= dirlen) {
-		errno = ENAMETOOLONG;
-		return (-1);
-	}
-	return (0);
-}

Modified: trunk/varnish-cache/lib/libvarnishapi/shmlog.c
===================================================================
--- trunk/varnish-cache/lib/libvarnishapi/shmlog.c	2010-05-04 14:19:05 UTC (rev 4758)
+++ trunk/varnish-cache/lib/libvarnishapi/shmlog.c	2010-05-04 21:04:13 UTC (rev 4759)
@@ -127,21 +127,17 @@
 {
 	int i;
 	struct shmloghead slh;
-	char dirname[PATH_MAX], logname[PATH_MAX];
+	char *logname;
 
 	if (vsl_lh != NULL)
 		return (0);
 
-	if (varnish_instance(varnish_name, vsl_name,
-	    sizeof vsl_name, dirname, sizeof dirname) != 0) {
+	if (vin_n_arg(varnish_name, NULL, NULL, &logname)) {
 		fprintf(stderr, "Invalid instance name: %s\n",
 		    strerror(errno));
 		return (1);
 	}
 
-	/* XXX check overflow */
-	snprintf(logname, sizeof logname, "%s/%s", dirname, SHMLOG_FILENAME);
-
 	vsl_fd = open(logname, O_RDONLY);
 	if (vsl_fd < 0) {
 		fprintf(stderr, "Cannot open %s: %s\n",




More information about the varnish-commit mailing list