From: phk Date: Sat, 5 Aug 2006 12:45:45 +0000 (+0000) Subject: Add a timeout to reads from the child CLI pipe so we don't hang X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=48bdfb913182f11c530a7d2bbd4dff9c984aa05b;p=varnish Add a timeout to reads from the child CLI pipe so we don't hang for ever. git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@668 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- diff --git a/varnish-cache/bin/varnishd/common_cli.c b/varnish-cache/bin/varnishd/common_cli.c index 9435db37..716f162c 100644 --- a/varnish-cache/bin/varnishd/common_cli.c +++ b/varnish-cache/bin/varnishd/common_cli.c @@ -4,6 +4,7 @@ #include #include +#include #include #include #include @@ -71,15 +72,31 @@ cli_writeres(int fd, struct cli *cli) return (i != l); } +static int +read_tmo(int fd, void *ptr, unsigned len, double tmo) +{ + int i; + struct pollfd pfd[1]; + + pfd->fd = fd; + pfd->events = POLLIN; + i = poll(pfd, 1, (int)(tmo * 1e3)); + if (i == 0) { + errno = ETIMEDOUT; + return (-1); + } + return (read(fd, ptr, len)); +} + int -cli_readres(int fd, unsigned *status, char **ptr) +cli_readres(int fd, unsigned *status, char **ptr, double tmo) { - char res[CLI_LINE0_LEN + 1]; /* For NUL */ + char res[CLI_LINE0_LEN]; /* For NUL */ int i, j; unsigned u, v; char *p; - i = read(fd, res, CLI_LINE0_LEN); + i = read_tmo(fd, res, CLI_LINE0_LEN, tmo); if (i < 0) return (i); assert(i == CLI_LINE0_LEN); /* XXX: handle */ @@ -91,7 +108,7 @@ cli_readres(int fd, unsigned *status, char **ptr) *status = u; p = malloc(v + 1); assert(p != NULL); - i = read(fd, p, v + 1); + i = read_tmo(fd, p, v + 1, tmo); if (i < 0) { free(p); return (i); diff --git a/varnish-cache/bin/varnishd/common_cli.h b/varnish-cache/bin/varnishd/common_cli.h index add36893..12d19d4f 100644 --- a/varnish-cache/bin/varnishd/common_cli.h +++ b/varnish-cache/bin/varnishd/common_cli.h @@ -8,7 +8,7 @@ struct cli { }; int cli_writeres(int fd, struct cli *cli); -int cli_readres(int fd, unsigned *status, char **ptr); +int cli_readres(int fd, unsigned *status, char **ptr, double tmo); extern struct cli_proto CLI_cmds[]; cli_func_t cli_func_ping; diff --git a/varnish-cache/bin/varnishd/mgt_cli.c b/varnish-cache/bin/varnishd/mgt_cli.c index e8e29d86..fff5974c 100644 --- a/varnish-cache/bin/varnishd/mgt_cli.c +++ b/varnish-cache/bin/varnishd/mgt_cli.c @@ -85,7 +85,7 @@ mcf_passthru(struct cli *cli, char **av, void *priv) assert(i == v); free(p); - i = cli_readres(cli_i, &u, &p); + i = cli_readres(cli_i, &u, &p, 3.0); assert(i == 0); cli_result(cli, u); cli_out(cli, "%s", p); @@ -187,7 +187,8 @@ mgt_cli_askchild(int *status, char **resp, const char *fmt, ...) assert(i == strlen(p)); free(p); - i = cli_readres(cli_i, &j, resp); + i = cli_readres(cli_i, &j, resp, 3.0); + assert(i == 0); if (status != NULL) *status = j; return (j == CLIS_OK ? 0 : j);