From: phk Date: Thu, 3 Aug 2006 06:45:58 +0000 (+0000) Subject: Rework the cache process CLI handling: X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0bec985c9fc52ac25a9ef6bfab96476ae3f1f804;p=varnish 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. git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@615 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- diff --git a/varnish-cache/bin/varnishd/Makefile.am b/varnish-cache/bin/varnishd/Makefile.am index a8f4e80e..c11e96cd 100644 --- a/varnish-cache/bin/varnishd/Makefile.am +++ b/varnish-cache/bin/varnishd/Makefile.am @@ -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 \ diff --git a/varnish-cache/bin/varnishd/cache.h b/varnish-cache/bin/varnishd/cache.h index e5f89eef..02bfeaab 100644 --- a/varnish-cache/bin/varnishd/cache.h +++ b/varnish-cache/bin/varnishd/cache.h @@ -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 index 00000000..16a322f0 --- /dev/null +++ b/varnish-cache/bin/varnishd/cache_cli.c @@ -0,0 +1,130 @@ +/* + * $Id$ + */ + +#include +#include +#include +#include +#include +#include +#include + +#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; + } +} diff --git a/varnish-cache/bin/varnishd/cache_main.c b/varnish-cache/bin/varnishd/cache_main.c index 6672c9f5..e70acce2 100644 --- a/varnish-cache/bin/varnishd/cache_main.c +++ b/varnish-cache/bin/varnishd/cache_main.c @@ -7,84 +7,14 @@ #include #include -#include -#include - #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"); } diff --git a/varnish-cache/bin/varnishd/cli_event.h b/varnish-cache/bin/varnishd/cli_event.h index ed33a46d..877c7d22 100644 --- a/varnish-cache/bin/varnishd/cli_event.h +++ b/varnish-cache/bin/varnishd/cli_event.h @@ -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[];