]> err.no Git - varnish/commitdiff
Rework the cache process CLI handling:
authorphk <phk@d4fa192b-c00b-0410-8231-f00ffab90ce4>
Thu, 3 Aug 2006 06:45:58 +0000 (06:45 +0000)
committerphk <phk@d4fa192b-c00b-0410-8231-f00ffab90ce4>
Thu, 3 Aug 2006 06:45:58 +0000 (06:45 +0000)
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.

git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@615 d4fa192b-c00b-0410-8231-f00ffab90ce4

varnish-cache/bin/varnishd/Makefile.am
varnish-cache/bin/varnishd/cache.h
varnish-cache/bin/varnishd/cache_cli.c [new file with mode: 0644]
varnish-cache/bin/varnishd/cache_main.c
varnish-cache/bin/varnishd/cli_event.h

index a8f4e80e28a830bc3424393c0d69adba7673537c..c11e96cd02c0d4517a65529dbb3868455065ea9f 100644 (file)
@@ -18,6 +18,7 @@ varnishd_SOURCES = \
        cache_backend.c \
        cache_ban.c \
        cache_center.c \
+       cache_cli.c \
        cache_expire.c \
        cache_fetch.c \
        cache_hash.c \
index e5f89eefa98bbb8951584ceae7214bba5b3112d9..02bfeaab2bb078a055fa0a4e672c72235f222a82 100644 (file)
@@ -301,6 +301,9 @@ int BAN_CheckObject(struct object *o, const char *url);
 /* 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);
diff --git a/varnish-cache/bin/varnishd/cache_cli.c b/varnish-cache/bin/varnishd/cache_cli.c
new file mode 100644 (file)
index 0000000..16a322f
--- /dev/null
@@ -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;
+       }
+}
index 6672c9f560d8b2f34af8a28932192d54c732a7e9..e70acce2b7fc3d6aa084c6ef4495c7ac8e133052 100644 (file)
@@ -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 @@ static struct cli_proto cli_proto[] = {
 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,24 +45,14 @@ child_main(void)
        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");
 }
index ed33a46da805884777b8843e816cd15dd9d36ece..877c7d229bf0313b38d5849aab4653d1da0794f8 100644 (file)
@@ -15,3 +15,4 @@ struct cli *cli_setup(struct event_base *eb, int fdr, int fdw, int ver, struct c
 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[];