From: phk Date: Fri, 4 Aug 2006 06:23:08 +0000 (+0000) Subject: (Re)Implement passthru of cli commands, we can now talk with the X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4c749b01abe18fe74c2e4309b68803c7733857c7;p=varnish (Re)Implement passthru of cli commands, we can now talk with the cache process again. git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@629 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- diff --git a/varnish-cache/bin/varnishd/mgt_cli.c b/varnish-cache/bin/varnishd/mgt_cli.c index 41fc4e94..0b6b3b22 100644 --- a/varnish-cache/bin/varnishd/mgt_cli.c +++ b/varnish-cache/bin/varnishd/mgt_cli.c @@ -20,6 +20,7 @@ #include "mgt.h" static int cli_i = -1, cli_o = -1; +static pthread_mutex_t cli_mtx; /*--------------------------------------------------------------------*/ @@ -35,6 +36,69 @@ mcf_server_startstop(struct cli *cli, char **av, void *priv) mgt_start_child(); } +/*-------------------------------------------------------------------- + * Passthru of cli commands. + */ + +static void +mcf_passthru(struct cli *cli, char **av, void *priv) +{ + char buf[BUFSIZ], *bp, *be; + char *p; + unsigned u, v; + int i, j, k; + + AZ(pthread_mutex_lock(&cli_mtx)); + /* Request */ + if (cli_o <= 0) { + AZ(pthread_mutex_unlock(&cli_mtx)); + cli_result(cli, CLIS_CANT); + cli_out(cli, "Cache process not running"); + return; + } + (void)priv; + bp = buf; + be = bp + sizeof buf; + for (u = 1; av[u] != NULL; u++) { + v = strlen(av[u]); + if (5 + bp + 4 * v > be) { + *bp = '\0'; + v = bp - buf; + i = write(cli_o, buf, v); + assert(i == v); + bp = buf; + } + *bp++ = '"'; + for (p = av[u]; *p; p++) { + switch (*p) { + case '\\': *bp++ = '\\'; *bp++ = '\\'; break; + case '\n': *bp++ = '\\'; *bp++ = 'n'; break; + case '"': *bp++ = '\\'; *bp++ = '"'; break; + default: *bp++ = *p; break; + } + } + *bp++ = '"'; + *bp++ = ' '; + } + if (bp != buf) { + *bp++ = '\n'; + v = bp - buf; + i = write(cli_o, buf, v); + assert(i == v); + } + + /* Response */ + i = read(cli_i, buf, sizeof buf - 1); + assert(i > 0); + buf[i] = '\0'; + j = sscanf(buf, "%u %u\n%n", &u, &v, &k); + assert(j == 2); + assert(i == k + v + 1); + cli_result(cli, u); + cli_out(cli, "%*.*s", v, v, buf + k); + AZ(pthread_mutex_unlock(&cli_mtx)); +} + /*--------------------------------------------------------------------*/ static struct cli_proto *cli_proto; @@ -70,6 +134,7 @@ mgt_cli_init(void) unsigned u, v; + AZ(pthread_mutex_init(&cli_mtx, NULL)); /* * Build the joint cli_proto by combining the manager process * entries with with the cache process entries. The latter @@ -93,7 +158,7 @@ mgt_cli_init(void) if (v < u) continue; cli_proto[u] = *cp; - cli_proto[u].func = NULL; /* XXX: pass */ + cli_proto[u].func = mcf_passthru; u++; }