r2598 - trunk/varnish-cache/bin/varnishd

phk at projects.linpro.no phk at projects.linpro.no
Wed Mar 12 15:07:08 CET 2008


Author: phk
Date: 2008-03-12 15:07:08 +0100 (Wed, 12 Mar 2008)
New Revision: 2598

Modified:
   trunk/varnish-cache/bin/varnishd/cache.h
   trunk/varnish-cache/bin/varnishd/cache_acceptor.c
   trunk/varnish-cache/bin/varnishd/cache_ban.c
   trunk/varnish-cache/bin/varnishd/cache_cli.c
   trunk/varnish-cache/bin/varnishd/cache_main.c
   trunk/varnish-cache/bin/varnishd/cache_vcl.c
   trunk/varnish-cache/bin/varnishd/mgt.h
Log:
Further revamp the CLI handling in the cacher process, making it
possible for various modules to add cli functions so they can
be manipulated on the fly.

CLI_AddFuncs() registers a set of CLI functions.  We operate
with three lists: the ones not shown in "help" because the
manager already showed them, the normal ones and the debug
commands which are also not shown in a plain "help".

Move the registration of cli functions out to the code they
belong in: VCL, BAN and VCA.

Give VCA a real Init function, and have the cli function ("start")
initiate the acceptor thread which listens for incoming connections.

Split CLI_Init() into CLI_Init() and CLI_Run()



Modified: trunk/varnish-cache/bin/varnishd/cache.h
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache.h	2008-03-12 13:46:34 UTC (rev 2597)
+++ trunk/varnish-cache/bin/varnishd/cache.h	2008-03-12 14:07:08 UTC (rev 2598)
@@ -79,6 +79,7 @@
 struct addrinfo;
 struct esi_bit;
 struct vrt_backend;
+struct cli_proto;
 
 /*--------------------------------------------------------------------*/
 
@@ -415,8 +416,6 @@
 /* cache_ban.c */
 void AddBan(const char *, int hash);
 void BAN_Init(void);
-void ccf_url_purge(struct cli *cli, const char * const *av, void *priv);
-void ccf_hash_purge(struct cli *cli, const char * const *av, void *priv);
 void BAN_NewObj(struct object *o);
 int BAN_CheckObject(struct object *o, const char *url, const char *hash);
 
@@ -426,6 +425,9 @@
 
 /* cache_cli.c [CLI] */
 void CLI_Init(void);
+void CLI_Run(void);
+enum cli_set_e {MASTER_CLI, PUBLIC_CLI, DEBUG_CLI};
+void CLI_AddFuncs(enum cli_set_e which, struct cli_proto *p);
 extern pthread_t cli_thread;
 #define ASSERT_CLI() do {assert(pthread_self() == cli_thread);} while (0)
 
@@ -558,13 +560,6 @@
 #undef VCL_MET_MAC
 #undef VCL_RET_MAC
 
-#ifdef CLI_PRIV_H
-cli_func_t	ccf_config_list;
-cli_func_t	ccf_config_load;
-cli_func_t	ccf_config_discard;
-cli_func_t	ccf_config_use;
-#endif
-
 /* cache_vrt_esi.c */
 
 void ESI_Deliver(struct sess *);

Modified: trunk/varnish-cache/bin/varnishd/cache_acceptor.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache_acceptor.c	2008-03-12 13:46:34 UTC (rev 2597)
+++ trunk/varnish-cache/bin/varnishd/cache_acceptor.c	2008-03-12 14:07:08 UTC (rev 2598)
@@ -50,6 +50,8 @@
 #include "compat/srandomdev.h"
 #endif
 
+#include "cli.h"
+#include "cli_priv.h"
 #include "shmlog.h"
 #include "cache.h"
 #include "cache_acceptor.h"
@@ -274,11 +276,13 @@
 
 /*--------------------------------------------------------------------*/
 
-void
-VCA_Init(void)
+static void
+ccf_start(struct cli *cli, const char * const *av, void *priv)
 {
 
-
+	(void)cli;
+	(void)av;
+	(void)priv;
 	/* XXX: Add selector mechanism at some point */
 	vca_act = vca_acceptors[0];
 
@@ -291,3 +295,15 @@
 	AZ(pthread_create(&vca_thread_acct, NULL, vca_acct, NULL));
 	VSL(SLT_Debug, 0, "Acceptor is %s", vca_act->name);
 }
+
+static struct cli_proto vca_cmds[] = {
+	{ CLI_SERVER_START,	ccf_start },
+	{ NULL }
+};
+
+void
+VCA_Init(void)
+{
+
+	CLI_AddFuncs(MASTER_CLI, vca_cmds);
+}

Modified: trunk/varnish-cache/bin/varnishd/cache_ban.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache_ban.c	2008-03-12 13:46:34 UTC (rev 2597)
+++ trunk/varnish-cache/bin/varnishd/cache_ban.c	2008-03-12 14:07:08 UTC (rev 2598)
@@ -40,6 +40,7 @@
 #include <regex.h>
 
 #include "shmlog.h"
+#include "cli.h"
 #include "cli_priv.h"
 #include "cache.h"
 
@@ -103,7 +104,7 @@
 	return (0);
 }
 
-void
+static void
 ccf_url_purge(struct cli *cli, const char * const *av, void *priv)
 {
 
@@ -112,7 +113,7 @@
 	cli_out(cli, "PURGE %s\n", av[2]);
 }
 
-void
+static void
 ccf_hash_purge(struct cli *cli, const char * const *av, void *priv)
 {
 
@@ -121,9 +122,16 @@
 	cli_out(cli, "PURGE %s\n", av[2]);
 }
 
+static struct cli_proto ban_cmds[] = {
+	{ CLI_URL_PURGE,	ccf_url_purge },
+	{ CLI_HASH_PURGE,	ccf_hash_purge },
+	{ NULL }
+};
+
 void
 BAN_Init(void)
 {
 
+	CLI_AddFuncs(PUBLIC_CLI, ban_cmds);
 	AddBan("\001", 0);
 }

Modified: trunk/varnish-cache/bin/varnishd/cache_cli.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache_cli.c	2008-03-12 13:46:34 UTC (rev 2597)
+++ trunk/varnish-cache/bin/varnishd/cache_cli.c	2008-03-12 14:07:08 UTC (rev 2598)
@@ -27,6 +27,13 @@
  * SUCH DAMAGE.
  *
  * $Id$
+ *
+ * Caching process CLI handling.
+ *
+ * We only have one CLI source, the stdin/stdout pipes from the manager
+ * process, but we complicate things by having undocumented commands that
+ * we do not want to show in a plain help, and by having commands that the
+ * manager has already shown in help before asking us.
  */
 
 #include "config.h"
@@ -47,106 +54,45 @@
 #include "vsb.h"
 
 pthread_t	cli_thread;
+static MTX	cli_mtx;
 
-/*--------------------------------------------------------------------*/
+/*
+ * The CLI commandlist is split in three:
+ *  - Commands we get from/share with the manager, we don't show these
+ *	in help, as the manager already did that.
+ *  - Cache process commands, show in help
+ *  - Undocumented debug commands, show in undocumented "help -d"
+ */
 
-static void
-cli_debug_sizeof(struct cli *cli, const char * const *av, void *priv)
-{
-	(void)av;
-	(void)priv;
+static struct cli_proto *ccf_master_cli, *ccf_public_cli, *ccf_debug_cli;
 
-#define SZOF(foo)       cli_out(cli, \
-    "sizeof(%s) = %zd = 0x%zx\n", #foo, sizeof(foo), sizeof(foo));
-        SZOF(struct ws);
-        SZOF(struct http);
-        SZOF(struct http_conn);
-        SZOF(struct acct);
-        SZOF(struct worker);
-        SZOF(struct workreq);
-        SZOF(struct bereq);
-        SZOF(struct storage);
-        SZOF(struct object);
-        SZOF(struct objhead);
-        SZOF(struct sess);
-        SZOF(struct vbe_conn);
-}
-
-/*--------------------------------------------------------------------*/
-
-static void
-ccf_start(struct cli *cli, const char * const *av, void *priv)
-{
-
-	(void)cli;
-	(void)av;
-	(void)priv;
-	VCA_Init();
-	return;
-}
-
-/*--------------------------------------------------------------------*/
-
-static void ccf_help(struct cli *cli, const char * const *av, void *priv);
-
 /*--------------------------------------------------------------------
- * The CLI commandlist is split in three:
- *	Commands we get from/share with the manager
- *	Cache process commands
- *	Undocumented commands
+ * Add CLI functions to the appropriate command set
  */
 
-static struct cli_proto master_cmds[] = {
-	{ CLI_PING,		cli_func_ping },
-	{ CLI_SERVER_START,	ccf_start },
-	{ CLI_VCL_LOAD,		ccf_config_load },
-	{ CLI_VCL_LIST,		ccf_config_list },
-	{ CLI_VCL_DISCARD,	ccf_config_discard },
-	{ CLI_VCL_USE,		ccf_config_use },
-	{ NULL }
-};
-
-static struct cli_proto cacher_cmds[] = {
-	{ CLI_HELP,             ccf_help, NULL },
-	{ CLI_URL_PURGE,	ccf_url_purge },
-	{ CLI_HASH_PURGE,	ccf_hash_purge },
-#if 0
-	{ CLI_URL_QUERY,	ccf_url_query },
-#endif
-	{ NULL }
-};
-
-static struct cli_proto undoc_cmds[] = {
-	{ "debug.sizeof", "debug.sizeof",
-		"\tDump sizeof various data structures\n",
-		0, 0, cli_debug_sizeof },
-	{ NULL }
-};
-
-
-/*--------------------------------------------------------------------*/
-
-static void
-ccf_help(struct cli *cli, const char * const *av, void *priv)
+void
+CLI_AddFuncs(enum cli_set_e which, struct cli_proto *p)
 {
+	struct cli_proto *c, **cp;
 
-	(void)priv;
-	/* "+1" to skip "help" entry, manager already did that. */
-	cli_func_help(cli, av, cacher_cmds + 1);
-
-	if (av[2] != NULL && !strcmp(av[2], "-d")) {
-		/* Also list undocumented commands */
-		cli_out(cli, "\nDebugging commands:\n");
-		cli_func_help(cli, av, undoc_cmds);
-	} else if (cli->result == CLIS_UNKNOWN) {
-		/* Otherwise, try the undocumented list */
-		vsb_clear(cli->sb);
-		cli->result = CLIS_OK;
-		cli_func_help(cli, av, undoc_cmds);
+	switch (which) {
+	case MASTER_CLI: cp = &ccf_master_cli; break;
+	case PUBLIC_CLI: cp = &ccf_public_cli; break;
+	case DEBUG_CLI:	 cp = &ccf_debug_cli;  break;
+	default: INCOMPL();
 	}
+	LOCK(&cli_mtx);
+	c = cli_concat(*cp, p);
+	AN(c);
+	free(*cp);
+	*cp = c;
+	UNLOCK(&cli_mtx);
 }
 
-/*--------------------------------------------------------------------*/
+/*--------------------------------------------------------------------
+ * Called when we have a full line, look through all three command 
+ * lists to find it.
+ */
 
 static int
 cli_vlu(void *priv, const char *p)
@@ -157,17 +103,19 @@
 	cli = priv;
 	VSL(SLT_CLI, 0, "Rd %s", p);
 	vsb_clear(cli->sb);
-	cli_dispatch(cli, master_cmds, p);
+	LOCK(&cli_mtx);
+	cli_dispatch(cli, ccf_master_cli, p);
 	if (cli->result == CLIS_UNKNOWN) {
 		vsb_clear(cli->sb);
 		cli->result = CLIS_OK;
-		cli_dispatch(cli, cacher_cmds, p);
+		cli_dispatch(cli, ccf_public_cli, p);
 	}
 	if (cli->result == CLIS_UNKNOWN) {
 		vsb_clear(cli->sb);
 		cli->result = CLIS_OK;
-		cli_dispatch(cli, undoc_cmds, p);
+		cli_dispatch(cli, ccf_debug_cli, p);
 	}
+	UNLOCK(&cli_mtx);
 	vsb_finish(cli->sb);
 	AZ(vsb_overflowed(cli->sb));
 	i = cli_writeres(heritage.fds[1], cli);
@@ -179,8 +127,12 @@
 	return (0);
 }
 
+/*--------------------------------------------------------------------
+ * Run CLI on stdin/stdout pipe from manager
+ */
+
 void
-CLI_Init(void)
+CLI_Run(void)
 {
 	struct pollfd pfd[1];
 	struct cli *cli, clis;
@@ -190,7 +142,6 @@
 	cli = &clis;
 	memset(cli, 0, sizeof *cli);
 
-	cli_thread = pthread_self();
 	cli->sb = vsb_new(NULL, NULL, 0, VSB_AUTOEXTEND);
 	XXXAN(cli->sb);
 	vlu = VLU_New(cli, cli_vlu, params->cli_buffer);
@@ -219,3 +170,80 @@
 		}
 	}
 }
+
+/*--------------------------------------------------------------------*/
+
+static void
+cli_debug_sizeof(struct cli *cli, const char * const *av, void *priv)
+{
+	(void)av;
+	(void)priv;
+
+#define SZOF(foo)       cli_out(cli, \
+    "sizeof(%s) = %zd = 0x%zx\n", #foo, sizeof(foo), sizeof(foo));
+        SZOF(struct ws);
+        SZOF(struct http);
+        SZOF(struct http_conn);
+        SZOF(struct acct);
+        SZOF(struct worker);
+        SZOF(struct workreq);
+        SZOF(struct bereq);
+        SZOF(struct storage);
+        SZOF(struct object);
+        SZOF(struct objhead);
+        SZOF(struct sess);
+        SZOF(struct vbe_conn);
+}
+
+/*--------------------------------------------------------------------*/
+
+static void
+ccf_help(struct cli *cli, const char * const *av, void *priv)
+{
+
+	(void)priv;
+	cli_func_help(cli, av, ccf_public_cli);
+
+	if (av[2] != NULL && !strcmp(av[2], "-d")) {
+		/* Also list undocumented commands */
+		cli_out(cli, "\nDebugging commands:\n");
+		cli_func_help(cli, av, ccf_debug_cli);
+	} else if (cli->result == CLIS_UNKNOWN) {
+		/* Otherwise, try the undocumented list */
+		vsb_clear(cli->sb);
+		cli->result = CLIS_OK;
+		cli_func_help(cli, av, ccf_debug_cli);
+	}
+}
+
+/*--------------------------------------------------------------------*/
+
+static struct cli_proto master_cmds[] = {
+	{ CLI_PING,		cli_func_ping },
+	{ CLI_HELP,             ccf_help, NULL },
+	{ NULL }
+};
+
+static struct cli_proto debug_cmds[] = {
+	{ "debug.sizeof", "debug.sizeof",
+		"\tDump sizeof various data structures\n",
+		0, 0, cli_debug_sizeof },
+	{ NULL }
+};
+
+
+/*--------------------------------------------------------------------
+ * Initialize the CLI subsystem
+ */
+
+void
+CLI_Init(void)
+{
+
+	MTX_INIT(&cli_mtx);
+	cli_thread = pthread_self();
+
+	CLI_AddFuncs(MASTER_CLI, master_cmds);
+	CLI_AddFuncs(DEBUG_CLI, debug_cmds);
+}
+

Modified: trunk/varnish-cache/bin/varnishd/cache_main.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache_main.c	2008-03-12 13:46:34 UTC (rev 2597)
+++ trunk/varnish-cache/bin/varnishd/cache_main.c	2008-03-12 14:07:08 UTC (rev 2598)
@@ -71,6 +71,8 @@
 
 	THR_Name("cache-main");
 
+	CLI_Init();
+
 	CNT_Init();
 	VCL_Init();
 
@@ -85,11 +87,13 @@
 	HSH_Init();
 	BAN_Init();
 
+	VCA_Init();
+
 	STV_open();
 
 	VSL_stats->start_time = (time_t)TIM_real();
 
-	CLI_Init();
+	CLI_Run();
 
 	printf("Child dies\n");
 }

Modified: trunk/varnish-cache/bin/varnishd/cache_vcl.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache_vcl.c	2008-03-12 13:46:34 UTC (rev 2597)
+++ trunk/varnish-cache/bin/varnishd/cache_vcl.c	2008-03-12 14:07:08 UTC (rev 2598)
@@ -208,7 +208,7 @@
 
 /*--------------------------------------------------------------------*/
 
-void
+static void
 ccf_config_list(struct cli *cli, const char * const *av, void *priv)
 {
 	struct vcls *vcl;
@@ -224,7 +224,7 @@
 	}
 }
 
-void
+static void
 ccf_config_load(struct cli *cli, const char * const *av, void *priv)
 {
 
@@ -236,7 +236,7 @@
 	return;
 }
 
-void
+static void
 ccf_config_discard(struct cli *cli, const char * const *av, void *priv)
 {
 	struct vcls *vcl;
@@ -263,7 +263,7 @@
 		VCL_Nuke(vcl);
 }
 
-void
+static void
 ccf_config_use(struct cli *cli, const char * const *av, void *priv)
 {
 	struct vcls *vcl;
@@ -321,9 +321,18 @@
 
 /*--------------------------------------------------------------------*/
 
+static struct cli_proto vcl_cmds[] = {
+	{ CLI_VCL_LOAD,         ccf_config_load },
+	{ CLI_VCL_LIST,         ccf_config_list },
+	{ CLI_VCL_DISCARD,      ccf_config_discard },
+	{ CLI_VCL_USE,          ccf_config_use },
+	{ NULL }        
+};
+
 void
 VCL_Init()
 {
 
+	CLI_AddFuncs(MASTER_CLI, vcl_cmds);
 	MTX_INIT(&vcl_mtx);
 }

Modified: trunk/varnish-cache/bin/varnishd/mgt.h
===================================================================
--- trunk/varnish-cache/bin/varnishd/mgt.h	2008-03-12 13:46:34 UTC (rev 2597)
+++ trunk/varnish-cache/bin/varnishd/mgt.h	2008-03-12 14:07:08 UTC (rev 2598)
@@ -44,7 +44,6 @@
 
 /* mgt_cli.c */
 
-void mgt_cli_init(void);
 void mgt_cli_setup(int fdi, int fdo, int verbose, const char *ident);
 int mgt_cli_askchild(unsigned *status, char **resp, const char *fmt, ...);
 void mgt_cli_start_child(int fdi, int fdo);




More information about the varnish-commit mailing list