r1572 - trunk/varnish-cache/bin/varnishd

phk at projects.linpro.no phk at projects.linpro.no
Mon Jun 25 23:12:18 CEST 2007


Author: phk
Date: 2007-06-25 23:12:17 +0200 (Mon, 25 Jun 2007)
New Revision: 1572

Modified:
   trunk/varnish-cache/bin/varnishd/heritage.h
   trunk/varnish-cache/bin/varnishd/mgt.h
   trunk/varnish-cache/bin/varnishd/mgt_child.c
   trunk/varnish-cache/bin/varnishd/mgt_param.c
   trunk/varnish-cache/bin/varnishd/mgt_vcc.c
   trunk/varnish-cache/bin/varnishd/varnishd.c
Log:
Redo the -n argument code, it had too many problems:
 
We need to process -P and -f arguments before we change directory.
(ticket 120)

(XXX: what about storage and hash arguments ??)

The daemon(3) call should not change our directory subsequently.
(ticket 121)
 
There is no need to enforce a hostname style format on the argument,
a directory nam makes much more sense, since that is what we need. 
Defaulting to /tmp instead of our hostname makes more sense (ticket 119).

This also allows the admin to use a different directory if /tmp is
mounted noexec (ticket 111) 
 
Put the directoryname used in the proctitle (via heritage)
 
XXX: for docs: vcl.load CLI commands will work relative to the -n directory.



Modified: trunk/varnish-cache/bin/varnishd/heritage.h
===================================================================
--- trunk/varnish-cache/bin/varnishd/heritage.h	2007-06-25 20:44:06 UTC (rev 1571)
+++ trunk/varnish-cache/bin/varnishd/heritage.h	2007-06-25 21:12:17 UTC (rev 1572)
@@ -62,6 +62,8 @@
 
 	/* Hash method */
 	struct hash_slinger		*hash;
+
+	const char			*n_arg;
 };
 
 struct params {

Modified: trunk/varnish-cache/bin/varnishd/mgt.h
===================================================================
--- trunk/varnish-cache/bin/varnishd/mgt.h	2007-06-25 20:44:06 UTC (rev 1571)
+++ trunk/varnish-cache/bin/varnishd/mgt.h	2007-06-25 21:12:17 UTC (rev 1572)
@@ -58,7 +58,7 @@
 
 /* mgt_vcc.c */
 void mgt_vcc_init(void);
-int mgt_vcc_default(const char *bflag, const char *fflag, int Cflag);
+int mgt_vcc_default(const char *bflag, const char *fflag, int f_fd, int Cflag);
 int mgt_push_vcls_and_start(unsigned *status, char **p);
 
 #include "stevedore.h"

Modified: trunk/varnish-cache/bin/varnishd/mgt_child.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/mgt_child.c	2007-06-25 20:44:06 UTC (rev 1571)
+++ trunk/varnish-cache/bin/varnishd/mgt_child.c	2007-06-25 21:12:17 UTC (rev 1572)
@@ -204,7 +204,7 @@
 		AZ(close(heritage.fds[0]));
 		AZ(close(heritage.fds[3]));
 
-		setproctitle("Varnish-Chld");
+		setproctitle("Varnish-Chld %s", heritage.n_arg);
 
 		signal(SIGINT, SIG_DFL);
 		signal(SIGTERM, SIG_DFL);
@@ -408,7 +408,7 @@
 	e->name = "mgt_sigchild";
 	AZ(ev_add(mgt_evb, e));
 
-	setproctitle("Varnish-Mgr");
+	setproctitle("Varnish-Mgr %s", heritage.n_arg);
 
 	sac.sa_handler = SIG_IGN;
 	sac.sa_flags = SA_RESTART;

Modified: trunk/varnish-cache/bin/varnishd/mgt_param.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/mgt_param.c	2007-06-25 20:44:06 UTC (rev 1571)
+++ trunk/varnish-cache/bin/varnishd/mgt_param.c	2007-06-25 21:12:17 UTC (rev 1572)
@@ -499,72 +499,6 @@
 
 /*--------------------------------------------------------------------*/
 
-#define NAME_RE "^([0-9A-Za-z-]+)(\\.[0-9A-Za-z-]+)*$"
-static regex_t *name_re;
-
-static void
-tweak_name(struct cli *cli, struct parspec *par, const char *arg)
-{
-	char hostname[1024], path[1024];
-	int error;
-
-	(void)par;
-
-	if (arg == NULL) {
-		cli_out(cli, "\"%s\"", master.name);
-		return;
-	}
-
-	/* empty string -> hostname */
-	if (*arg == '\0') {
-		if (gethostname(hostname, sizeof hostname) == 0)
-			arg = hostname;
-		else
-			arg = "localhost";
-	}
-
-	/* check that the new name follows hostname convention */
-	if (name_re == NULL) {
-		name_re = malloc(sizeof *name_re);
-		AN(name_re);
-		AZ(regcomp(name_re, NAME_RE, REG_EXTENDED|REG_NOSUB));
-	}
-	if (regexec(name_re, arg, 0, NULL, 0) != 0) {
-		cli_out(cli, "Invalid instance name");
-		cli_result(cli, CLIS_PARAM);
-		return;
-	}
-
-	error = 0;
-	snprintf(path, sizeof path, "/tmp/%s", arg); /* XXX overflow */
-	if (master.name && *master.name) {
-		struct stat old_st;
-		char old_path[1024];
-
-		/* rename old directory */
-		snprintf(old_path, sizeof old_path, "/tmp/%s", master.name); /* XXX overflow */
-		if (stat(old_path, &old_st) == 0 && S_ISDIR(old_st.st_mode)) {
-			error = rename(old_path, path);
-		} else {
-			error = (mkdir(path, 0755) != 0 && errno != EEXIST);
-		}
-	} else {
-		error = (mkdir(path, 0755) != 0 && errno != EEXIST);
-	}
-
-	if (error || chdir(path) != 0) {
-		cli_out(cli, "could not create %s: %s", path, strerror(errno));
-		cli_result(cli, CLIS_CANT);
-		return;
-	}
-
-	/* Everything is fine, store the (new) name */
-	master.name = strdup(arg);
-	XXXAN(master.name);
-}
-
-/*--------------------------------------------------------------------*/
-
 /*
  * Make sure to end all lines with either a space or newline of the
  * formatting will go haywire.
@@ -738,12 +672,6 @@
 		"it possible to attach a debugger to the child.\n"
 		MUST_RESTART,
 		"3", "seconds" },
-	{ "name", tweak_name,
-		"Name of varnishd instance. Must follow hostname "
-		"naming conventions. Makes it possible to run "
-		"multiple varnishd instances on one server.\n"
-		EXPERIMENTAL,
-		"" },
 	{ NULL, NULL, NULL }
 };
 

Modified: trunk/varnish-cache/bin/varnishd/mgt_vcc.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/mgt_vcc.c	2007-06-25 20:44:06 UTC (rev 1571)
+++ trunk/varnish-cache/bin/varnishd/mgt_vcc.c	2007-06-25 21:12:17 UTC (rev 1572)
@@ -254,11 +254,11 @@
 }
 
 static char *
-mgt_VccCompileFile(struct vsb *sb, const char *fn, int C_flag)
+mgt_VccCompileFile(struct vsb *sb, const char *fn, int C_flag, int fd)
 {
 	char *csrc, *vf = NULL;
 
-	csrc = VCC_CompileFile(sb, fn, -1);
+	csrc = VCC_CompileFile(sb, fn, fd);
 	if (csrc != NULL) {
 		if (C_flag)
 			fputs(csrc, stdout);
@@ -314,7 +314,7 @@
 /*--------------------------------------------------------------------*/
 
 int
-mgt_vcc_default(const char *b_arg, const char *f_arg, int C_flag)
+mgt_vcc_default(const char *b_arg, const char *f_arg, int f_fd, int C_flag)
 {
 	char *addr, *port;
 	char *buf, *vf;
@@ -349,7 +349,7 @@
 		vf = mgt_VccCompile(sb, buf, NULL, C_flag);
 		free(buf);
 	} else {
-		vf = mgt_VccCompileFile(sb, f_arg, C_flag);
+		vf = mgt_VccCompileFile(sb, f_arg, C_flag, f_fd);
 	}
 	vsb_finish(sb);
 	if (vsb_len(sb) > 0) {
@@ -461,7 +461,7 @@
 
 	sb = vsb_new(NULL, NULL, 0, VSB_AUTOEXTEND);
 	XXXAN(sb);
-	vf = mgt_VccCompileFile(sb, av[3], 0);
+	vf = mgt_VccCompileFile(sb, av[3], 0, -1);
 	vsb_finish(sb);
 	if (vsb_len(sb) > 0) {
 		cli_out(cli, "%s", vsb_data(sb));

Modified: trunk/varnish-cache/bin/varnishd/varnishd.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/varnishd.c	2007-06-25 20:44:06 UTC (rev 1571)
+++ trunk/varnish-cache/bin/varnishd/varnishd.c	2007-06-25 21:12:17 UTC (rev 1572)
@@ -44,6 +44,7 @@
 #include <string.h>
 #include <time.h>
 #include <unistd.h>
+#include <sys/stat.h>
 
 #ifndef HAVE_DAEMON
 #include "compat/daemon.h"
@@ -178,7 +179,8 @@
 	    "  -h classic  [default]");
 	fprintf(stderr, "    %-28s # %s\n", "",
 	    "  -h classic,<buckets>");
-	fprintf(stderr, "    %-28s # %s\n", "-n name", "varnishd instance name");
+	fprintf(stderr, "    %-28s # %s\n", "-n dir",
+	    "varnishd working directory");
 	fprintf(stderr, "    %-28s # %s\n", "-P file", "PID file");
 	fprintf(stderr, "    %-28s # %s\n", "-p param=value",
 	    "set parameter");
@@ -405,7 +407,9 @@
 	unsigned F_flag = 0;
 	const char *b_arg = NULL;
 	const char *f_arg = NULL;
+	int f_fd = -1;
 	const char *h_arg = "classic";
+	const char *n_arg = "/tmp";
 	const char *P_arg = NULL;
 	const char *s_arg = "file";
 	const char *T_arg = NULL;
@@ -455,7 +459,7 @@
 			h_arg = optarg;
 			break;
 		case 'n':
-			MCF_ParamSet(cli, "name", optarg);
+			n_arg = optarg;
 			break;
 		case 'P':
 			P_arg = optarg;
@@ -499,6 +503,7 @@
 		usage();
 	}
 
+	/* XXX: we can have multiple CLI actions above, is this enough ? */
 	if (cli[0].result != CLIS_OK) {
 		fprintf(stderr, "Parameter errors:\n");
 		vsb_finish(cli[0].sb);
@@ -519,14 +524,39 @@
 		fprintf(stderr, "One of -b or -f must be specified\n");
 		usage();
 	}
+	
+	if (f_arg != NULL) {
+		f_fd = open(f_arg, O_RDONLY);
+		if (f_fd < 0) {
+			fprintf(stderr, "Cannot open '%s': %s\n",
+			    f_arg, strerror(errno));
+			exit(1);
+		}
+	}
 
+	if (mkdir(n_arg, 0755) < 0 && errno != EEXIST) {
+		fprintf(stderr, "Cannot create working directory '%s': %s\n",
+		    n_arg, strerror(errno));
+		exit(1);
+	}
+
+	if (chdir(n_arg) < 0) {
+		fprintf(stderr, "Cannot change to working directory '%s': %s\n",
+		    n_arg, strerror(errno));
+		exit(1);
+	}
+
+	heritage.n_arg = n_arg;
+
+	/* XXX: should this be relative to the -n arg ? */
 	if (P_arg && (pfh = vpf_open(P_arg, 0600, NULL)) == NULL) {
 		perror(P_arg);
 		exit(1);
 	}
 
-	if (mgt_vcc_default(b_arg, f_arg, C_flag))
+	if (mgt_vcc_default(b_arg, f_arg, f_fd, C_flag))
 		exit (2);
+
 	if (C_flag)
 		exit (0);
 
@@ -538,7 +568,7 @@
 	if (d_flag == 1)
 		DebugStunt();
 	if (d_flag < 2 && !F_flag)
-		daemon(d_flag, d_flag);
+		daemon(1, d_flag);
 	if (d_flag == 1)
 		printf("%d\n", getpid());
 




More information about the varnish-commit mailing list