From: phk Date: Fri, 4 Aug 2006 07:21:50 +0000 (+0000) Subject: Implement CLI ping in manager, this is a "per hop" command. X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1d1bf8921aec2359f71bf6cf44ffd8d2d7ca4979;p=varnish Implement CLI ping in manager, this is a "per hop" command. Add mgt_cli_askchild() function to poke the CLI interface to the child. Use it to ping the child every second. git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@633 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- diff --git a/varnish-cache/bin/varnishd/mgt.h b/varnish-cache/bin/varnishd/mgt.h index f3740db2..ef2eeaa9 100644 --- a/varnish-cache/bin/varnishd/mgt.h +++ b/varnish-cache/bin/varnishd/mgt.h @@ -15,6 +15,8 @@ void mgt_cli_init(void); void mgt_cli_setup(int fdi, int fdo, int verbose); void mgt_cli_start_child(int fdi, int fdo); void mgt_cli_stop_child(void); +int mgt_cli_askchild(int *status, char **resp, const char *fmt, ...); + /* tcp.c */ int open_tcp(const char *port); diff --git a/varnish-cache/bin/varnishd/mgt_child.c b/varnish-cache/bin/varnishd/mgt_child.c index 71fa133e..aa8892f9 100644 --- a/varnish-cache/bin/varnishd/mgt_child.c +++ b/varnish-cache/bin/varnishd/mgt_child.c @@ -63,8 +63,8 @@ child_poker(void *arg) (void)arg; while (1) { sleep (1); - /* CLI: ping/pong */ - child_ticker = 0; + if (!mgt_cli_askchild(NULL, NULL, "ping\n")) + child_ticker = 0; } } diff --git a/varnish-cache/bin/varnishd/mgt_cli.c b/varnish-cache/bin/varnishd/mgt_cli.c index e6630b17..cc75a179 100644 --- a/varnish-cache/bin/varnishd/mgt_cli.c +++ b/varnish-cache/bin/varnishd/mgt_cli.c @@ -9,6 +9,7 @@ #include #include #include +#include #include #include @@ -99,6 +100,7 @@ static struct cli_proto *cli_proto; static struct cli_proto mgt_cli_proto[] = { { CLI_HELP, cli_func_help, NULL }, /* must be first */ + { CLI_PING, cli_func_ping }, { CLI_SERVER_START, mcf_server_startstop, NULL }, { CLI_SERVER_STOP, mcf_server_startstop, &cli_proto }, { CLI_CONFIG_LOAD }, @@ -163,6 +165,36 @@ mgt_cli_init(void) /* XXX: open listening sockets, contact cluster server etc */ } +/*-------------------------------------------------------------------- + * Ask the child something over CLI, return zero only if everything is + * happy happy. + */ + +int +mgt_cli_askchild(int *status, char **resp, const char *fmt, ...) +{ + char *p; + int i, j; + va_list ap; + + va_start(ap, fmt); + i = vasprintf(&p, fmt, ap); + va_end(ap); + if (i < 0) + return (i); + AZ(pthread_mutex_lock(&cli_mtx)); + assert(p[i - 1] == '\n'); + i = write(cli_o, p, strlen(p)); + assert(i == strlen(p)); + free(p); + + i = cli_readres(cli_i, &j, resp); + AZ(pthread_mutex_unlock(&cli_mtx)); + if (status != NULL) + *status = j; + return (j == CLIS_OK ? 0 : j); +} + /*--------------------------------------------------------------------*/ void