[master] 9e27227 Implement varnishd -I <cli_file>

Poul-Henning Kamp phk at FreeBSD.org
Sun Feb 26 00:52:05 CET 2017


commit 9e27227550179cb2cc0e8619fc507079efe29130
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Sat Feb 25 23:41:35 2017 +0000

    Implement varnishd -I <cli_file>
    
    The CLI commands in the file are executed before the worker
    process is started, and the output is sent to stderr.
    
    CLI failures (!= 200) are fatal, unless the command is prefixed
    with a '-' in the file.
    
    You can now do things like:
    
    	$ cat I.file
    	vcl.load vcl1 /foo/bar1.vcl
    	vcl.load vcl2 /foo/bar2.vcl
    	vcl.load vcl3 /foo/bar3.vcl
    	start
    	$ varnishd -f '' -I I.file -a :80 [...]

diff --git a/bin/varnishd/mgt/mgt.h b/bin/varnishd/mgt/mgt.h
index d2dc522..041dba4 100644
--- a/bin/varnishd/mgt/mgt.h
+++ b/bin/varnishd/mgt/mgt.h
@@ -62,7 +62,7 @@ void MCH_Cli_Fail(void);
 /* mgt_cli.c */
 
 typedef void mgt_cli_close_f(void *priv);
-void mgt_cli_setup(int fdi, int fdo, int verbose, const char *ident,
+void mgt_cli_setup(int fdi, int fdo, int auth, const char *ident,
     mgt_cli_close_f *close_func, void *priv);
 int mgt_cli_askchild(unsigned *status, char **resp, const char *fmt, ...)
     __v_printflike(3, 4);
diff --git a/bin/varnishd/mgt/mgt_cli.c b/bin/varnishd/mgt/mgt_cli.c
index e6a187f..1eb3ce8 100644
--- a/bin/varnishd/mgt/mgt_cli.c
+++ b/bin/varnishd/mgt/mgt_cli.c
@@ -329,6 +329,8 @@ static void
 mgt_cli_cb_before(const struct cli *cli)
 {
 
+	if (cli->priv == stderr)
+		fprintf(stderr, "> %s\n", cli->cmd);
 	MGT_Complain(C_CLI, "CLI %s Rd %s", cli->ident, cli->cmd);
 }
 
@@ -338,6 +340,12 @@ mgt_cli_cb_after(const struct cli *cli)
 
 	MGT_Complain(C_CLI, "CLI %s Wr %03u %s",
 	    cli->ident, cli->result, VSB_data(cli->sb));
+	if (cli->priv == stderr &&
+	    cli->result != CLIS_OK && cli->cmd[0] != '-') {
+		MGT_Complain(C_ERR, "-I file CLI command failed (%d)\n%s\n",
+		    cli->result, VSB_data(cli->sb));
+		exit(2);
+	}
 }
 
 /*--------------------------------------------------------------------*/
@@ -384,20 +392,17 @@ mgt_cli_callback2(const struct vev *e, int what)
 /*--------------------------------------------------------------------*/
 
 void
-mgt_cli_setup(int fdi, int fdo, int verbose, const char *ident,
+mgt_cli_setup(int fdi, int fdo, int auth, const char *ident,
     mgt_cli_close_f *closefunc, void *priv)
 {
 	struct cli *cli;
 	struct vev *ev;
 
-	(void)ident;
-	(void)verbose;
-
 	cli = VCLS_AddFd(mgt_cls, fdi, fdo, closefunc, priv);
 
-	cli->ident = strdup(ident);
+	REPLACE(cli->ident, ident);
 
-	if (fdi != 0 && secret_file != NULL) {
+	if (!auth && secret_file != NULL) {
 		cli->auth = MCF_NOAUTH;
 		mgt_cli_challenge(cli);
 	} else {
@@ -407,7 +412,6 @@ mgt_cli_setup(int fdi, int fdo, int verbose, const char *ident,
 	AZ(VSB_finish(cli->sb));
 	(void)VCLI_WriteResult(fdo, cli->result, VSB_data(cli->sb));
 
-
 	ev = vev_new();
 	AN(ev);
 	ev->name = cli->ident;
diff --git a/bin/varnishd/mgt/mgt_main.c b/bin/varnishd/mgt/mgt_main.c
index fc24024..538ba17 100644
--- a/bin/varnishd/mgt/mgt_main.c
+++ b/bin/varnishd/mgt/mgt_main.c
@@ -66,6 +66,7 @@ int			exit_status = 0;
 struct vsb		*vident;
 struct VSC_C_mgt	static_VSC_C_mgt;
 struct VSC_C_mgt	*VSC_C_mgt;
+static int		I_fd = -1;
 
 static struct vpf_fh *pfh = NULL;
 
@@ -73,7 +74,7 @@ int optreset;	// Some has it, some doesn't.  Cheaper than auto*
 
 /*--------------------------------------------------------------------*/
 
-static void
+static void __attribute__((__noreturn__))
 usage(void)
 {
 #define FMT "    %-28s # %s\n"
@@ -97,6 +98,7 @@ usage(void)
 	fprintf(stderr, FMT, "", "  -h classic");
 	fprintf(stderr, FMT, "", "  -h classic,<buckets>");
 	fprintf(stderr, FMT, "-i identity", "Identity of varnish instance");
+	fprintf(stderr, FMT, "-I file", "Initialization CLI commands");
 	fprintf(stderr, FMT, "-j jail[,jailoptions]", "Jail specification");
 #ifdef HAVE_SETPPRIV
 	fprintf(stderr, FMT, "", "  -j solaris");
@@ -420,6 +422,16 @@ mgt_uptime(const struct vev *e, int what)
 
 /*--------------------------------------------------------------------*/
 
+static void
+mgt_I_close(void *priv)
+{
+	(void)priv;
+	fprintf(stderr, "END of -I file processing\n");
+	closefd(&I_fd);
+}
+
+/*--------------------------------------------------------------------*/
+
 struct f_arg {
 	unsigned		magic;
 #define F_ARG_MAGIC		0x840649a8
@@ -428,6 +440,8 @@ struct f_arg {
 	VTAILQ_ENTRY(f_arg)	list;
 };
 
+static const char opt_spec[] = "a:b:Cdf:Fh:i:I:j:l:M:n:P:p:r:S:s:T:t:VW:x:";
+
 int
 main(int argc, char * const *argv)
 {
@@ -455,7 +469,6 @@ main(int argc, char * const *argv)
 	char *dirname;
 	char **av;
 	char Cn_arg[] = "/tmp/varnishd_C_XXXXXXX";
-	const char * opt_spec = "a:b:Cdf:Fh:i:j:l:M:n:P:p:r:S:s:T:t:VW:x:";
 	unsigned u;
 	struct sigaction sac;
 	struct vev *e;
@@ -478,6 +491,7 @@ main(int argc, char * const *argv)
 		switch (o) {
 		case '?':
 			usage();
+			break;
 		case 'b':
 			b_arg = optarg;
 			break;
@@ -626,6 +640,14 @@ main(int argc, char * const *argv)
 		case 'i':
 			i_arg = optarg;
 			break;
+		case 'I':
+			if (I_fd >= 0)
+				ARGV_ERR("\tOnly one -I allowed\n");
+			I_fd = open(optarg, O_RDONLY);
+			if (I_fd < 0)
+				ARGV_ERR("\tCant open %s: %s\n",
+				    optarg, strerror(errno));
+			break;
 		case 'l':
 			av = VAV_Parse(optarg, NULL, ARGV_COMMA);
 			AN(av);
@@ -817,6 +839,17 @@ main(int argc, char * const *argv)
 
 	u = MCH_Init(d_flag || novcl ? 0 : 1);
 
+	if (I_fd >= 0) {
+		fprintf(stderr, "BEGIN of -I file processing\n");
+		mgt_cli_setup(I_fd, 2, 1, "-I file", mgt_I_close, stderr);
+		while (I_fd >= 0) {
+			o = vev_schedule_one(mgt_evb);
+			if (o != 1)
+				MGT_Complain(C_ERR,
+				    "vev_schedule_one() = %d", o);
+		}
+	}
+
 	if (eric_fd >= 0)
 		mgt_eric_im_done(eric_fd, u);
 
@@ -849,7 +882,6 @@ main(int argc, char * const *argv)
 	e->name = "mgt_sigint";
 	AZ(vev_add(mgt_evb, e));
 
-
 	o = vev_schedule(mgt_evb);
 	if (o != 0)
 		MGT_Complain(C_ERR, "vev_schedule() = %d", o);
diff --git a/lib/libvarnish/vcli_serve.c b/lib/libvarnish/vcli_serve.c
index b501d96..e80d1b7 100644
--- a/lib/libvarnish/vcli_serve.c
+++ b/lib/libvarnish/vcli_serve.c
@@ -343,7 +343,9 @@ cls_vlu(void *priv, const char *p)
 		if (*p == '\0')
 			return (0);
 		REPLACE(cli->cmd, p);
+		AN(p);	/* for FlexeLint */
 
+		/* We ignore a single leading '-' (for -I cli_file) */
 		if (p[0] == '-')
 			av = VAV_Parse(p + 1, NULL, 0);
 		else



More information about the varnish-commit mailing list