r615 - trunk/varnish-cache/bin/varnishd

phk at projects.linpro.no phk at projects.linpro.no
Thu Aug 3 08:45:58 CEST 2006


Author: phk
Date: 2006-08-03 08:45:58 +0200 (Thu, 03 Aug 2006)
New Revision: 615

Added:
   trunk/varnish-cache/bin/varnishd/cache_cli.c
Modified:
   trunk/varnish-cache/bin/varnishd/Makefile.am
   trunk/varnish-cache/bin/varnishd/cache.h
   trunk/varnish-cache/bin/varnishd/cache_main.c
   trunk/varnish-cache/bin/varnishd/cli_event.h
Log:
Rework the cache process CLI handling:

We are only accepting CLI from the pipes in heritage, so simply
run a loop reading those, dispatching lines as we see them.

Export CLI_cmds[] so that the management process can see it,
we might as well take advantage of the shared binary where we can.



Modified: trunk/varnish-cache/bin/varnishd/Makefile.am
===================================================================
--- trunk/varnish-cache/bin/varnishd/Makefile.am	2006-08-02 22:53:56 UTC (rev 614)
+++ trunk/varnish-cache/bin/varnishd/Makefile.am	2006-08-03 06:45:58 UTC (rev 615)
@@ -18,6 +18,7 @@
 	cache_backend.c \
 	cache_ban.c \
 	cache_center.c \
+	cache_cli.c \
 	cache_expire.c \
 	cache_fetch.c \
 	cache_hash.c \

Modified: trunk/varnish-cache/bin/varnishd/cache.h
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache.h	2006-08-02 22:53:56 UTC (rev 614)
+++ trunk/varnish-cache/bin/varnishd/cache.h	2006-08-03 06:45:58 UTC (rev 615)
@@ -301,6 +301,9 @@
 /* cache_center.c [CNT] */
 void CNT_Session(struct sess *sp);
 
+/* cache_cli.c [CLI] */
+void CLI_Init(void);
+
 /* cache_expiry.c */
 void EXP_Insert(struct object *o);
 void EXP_Init(void);

Added: trunk/varnish-cache/bin/varnishd/cache_cli.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache_cli.c	2006-08-02 22:53:56 UTC (rev 614)
+++ trunk/varnish-cache/bin/varnishd/cache_cli.c	2006-08-03 06:45:58 UTC (rev 615)
@@ -0,0 +1,130 @@
+/*
+ * $Id$
+ */
+
+#include <stdio.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <poll.h>
+#include <pthread.h>
+
+#include "event.h"		/* XXX only as long as it takes */
+
+#include "libvarnish.h"
+#include "shmlog.h"
+#include "cli.h"
+#include "cli_priv.h"
+#include "cli_event.h"
+#include "cache.h"
+#include "sbuf.h"
+#include "heritage.h"
+
+/*--------------------------------------------------------------------*/
+
+static void
+cli_func_ping(struct cli *cli, char **av, void *priv)
+{
+	time_t t;
+
+	(void)priv;
+#if 0
+	arm_keepalive();
+#endif
+	if (av[2] != NULL) {
+		/* XXX: check clock skew is pointless here */
+	}
+	t = time(NULL);
+	cli_out(cli, "PONG %ld", t);
+}
+
+/*--------------------------------------------------------------------*/
+
+struct cli_proto CLI_cmds[] = {
+	{ CLI_PING,		cli_func_ping },
+#if 0
+	{ CLI_URL_QUERY,	cli_func_url_query },
+#endif
+	{ CLI_URL_PURGE,	cli_func_url_purge },
+	{ CLI_CONFIG_LOAD,	cli_func_config_load },
+	{ CLI_CONFIG_LIST,	cli_func_config_list },
+	{ CLI_CONFIG_UNLOAD,	cli_func_config_unload },
+	{ CLI_CONFIG_USE,	cli_func_config_use },
+	{ NULL }
+};
+
+static int
+cli_writes(const char *s, const char *r, const char *t)
+{
+	int i, l;
+	struct iovec iov[3];
+
+	iov[0].iov_base = (void*)(uintptr_t)s;
+	iov[1].iov_base = (void*)(uintptr_t)r;
+	iov[2].iov_base = (void*)(uintptr_t)t;
+	for (l = i = 0; i < 3; i++) {
+		iov[i].iov_len = strlen(iov[i].iov_base);
+		l += iov[i].iov_len;
+	}
+	i = writev(heritage.fds[1], iov, 3);
+	VSL(SLT_CLI, 0, "Wr %d %s %s", i != l, s, r);
+	return (i != l);
+}
+
+void
+CLI_Init(void)
+{
+	struct pollfd pfd[1];
+	char *buf, *p;
+	unsigned nbuf, lbuf;
+	struct cli *cli, clis;
+	int i;
+	char res[30];
+
+	cli = &clis;
+	memset(cli, 0, sizeof *cli);
+	
+	cli->sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND);
+	assert(cli->sb != NULL);
+	lbuf = 4096;
+	buf = malloc(lbuf);
+	assert(buf != NULL);
+	nbuf = 0;
+	while (1) {
+		pfd[0].fd = heritage.fds[2];
+		pfd[0].events = POLLIN;
+		i = poll(pfd, 1, 5000);
+		if (i == 0)
+			continue;
+		if (nbuf == lbuf) {
+			lbuf += lbuf;
+			buf = realloc(buf, lbuf);
+			assert(buf != NULL);
+		}
+		i = read(heritage.fds[2], buf + nbuf, lbuf - nbuf);
+		if (i <= 0) {
+			VSL(SLT_Error, 0, "CLI read %d (errno=%d)", i, errno);
+			return;
+		}
+		nbuf += i;
+		p = strchr(buf, '\n');
+		if (p == NULL)
+			continue;
+		*p = '\0';
+		VSL(SLT_CLI, 0, "Rd %s", buf);
+		sbuf_clear(cli->sb);
+		cli_dispatch(cli, CLI_cmds, buf);
+		sbuf_finish(cli->sb);
+		sprintf(res, "%d ", cli->result);
+		if (cli_writes(res, sbuf_data(cli->sb), "\n")) {
+			VSL(SLT_Error, 0, "CLI write failed (errno=%d)", errno);
+			return;
+		}
+		i = ++p - buf; 
+		assert(i <= nbuf);
+		if (i < nbuf)
+			memcpy(buf, p, nbuf - i);
+		nbuf -= i;
+	}
+}

Modified: trunk/varnish-cache/bin/varnishd/cache_main.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache_main.c	2006-08-02 22:53:56 UTC (rev 614)
+++ trunk/varnish-cache/bin/varnishd/cache_main.c	2006-08-03 06:45:58 UTC (rev 615)
@@ -7,84 +7,14 @@
 #include <stdlib.h>
 #include <signal.h>
 
-#include <cli.h>
-#include <cli_priv.h>
-
 #include "libvarnish.h"
 #include "heritage.h"
 #include "shmlog.h"
 #include "cache.h"
 #include "event.h"
-#include "cli_event.h"
 
-static struct event ev_keepalive;
-
 struct stevedore	*stevedore;
 
-/*--------------------------------------------------------------------*/
-
-static void
-timer_keepalive(int a, short b, void *c)
-{
-
-	printf("%s(%d, %d, %p)\n", (const char *)__func__, a, (int)b, c);
-	printf("Heeellloooo ?   Ohh bother...\n");
-	exit (1);
-}
-
-static void
-arm_keepalive(void)
-{
-	struct timeval tv;
-
-	tv.tv_sec = 30;
-	tv.tv_usec = 0;
-
-	AZ(evtimer_del(&ev_keepalive));
-	AZ(evtimer_add(&ev_keepalive, &tv));
-}
-
-/*--------------------------------------------------------------------*/
-
-static void
-cli_func_url_query(struct cli *cli, char **av, void *priv)
-{
-
-	(void)priv;
-	cli_out(cli, "url <%s>", av[2]);
-	cli_result(cli, CLIS_UNIMPL);
-}
-
-/*--------------------------------------------------------------------*/
-
-static void
-cli_func_ping(struct cli *cli, char **av, void *priv)
-{
-	time_t t;
-
-	(void)priv;
-	VSL(SLT_CLI, 0, av[1]);
-	arm_keepalive();
-	if (av[2] != NULL) {
-		/* XXX: check clock skew is pointless here */
-	}
-	t = time(NULL);
-	cli_out(cli, "PONG %ld\n", t);
-}
-
-/*--------------------------------------------------------------------*/
-
-static struct cli_proto cli_proto[] = {
-	{ CLI_URL_QUERY,	cli_func_url_query },
-	{ CLI_URL_PURGE,	cli_func_url_purge },
-	{ CLI_CONFIG_LOAD,	cli_func_config_load },
-	{ CLI_CONFIG_LIST,	cli_func_config_list },
-	{ CLI_CONFIG_UNLOAD,	cli_func_config_unload },
-	{ CLI_CONFIG_USE,	cli_func_config_use },
-	{ CLI_PING,		cli_func_ping },
-	{ NULL }
-};
-
 /*--------------------------------------------------------------------
  * XXX: Think more about which order we start things
  */
@@ -92,9 +22,6 @@
 void
 child_main(void)
 {
-	struct event_base *eb;
-	struct cli *cli;
-	int i;
 
 	/* XXX: SO_NOSIGPIPE does not work reliably :-( */
 	signal(SIGPIPE, SIG_IGN);
@@ -118,25 +45,15 @@
 	HSH_Init();
 	BAN_Init();
 
-	eb = event_init();
-	assert(eb != NULL);
-
 	stevedore = heritage.stevedore;
 	if (stevedore->open != NULL)
 		stevedore->open(stevedore);
 
-	cli = cli_setup(eb, heritage.fds[2], heritage.fds[1], 0, cli_proto);
-
-	evtimer_set(&ev_keepalive, timer_keepalive, NULL);
-	AZ(event_base_set(eb, &ev_keepalive));
-	arm_keepalive();
-
 	printf("Ready\n");
 	VSL_stats->start_time = time(NULL);
-	i = event_base_loop(eb, 0);
-	if (i != 0)
-		printf("event_dispatch() = %d\n", i);
 
+	CLI_Init();
+
 	printf("Child dies\n");
 }
 

Modified: trunk/varnish-cache/bin/varnishd/cli_event.h
===================================================================
--- trunk/varnish-cache/bin/varnishd/cli_event.h	2006-08-02 22:53:56 UTC (rev 614)
+++ trunk/varnish-cache/bin/varnishd/cli_event.h	2006-08-03 06:45:58 UTC (rev 615)
@@ -15,3 +15,4 @@
 void cli_suspend(struct cli *cli);
 void cli_resume(struct cli *cli);
 void cli_encode_string(struct evbuffer *buf, char *b);
+extern struct cli_proto CLI_cmds[];




More information about the varnish-commit mailing list