Commit 12898505 authored by Poul-Henning Kamp's avatar Poul-Henning Kamp

Add a timeout to reads from the child CLI pipe so we don't hang

for ever.


git-svn-id: http://www.varnish-cache.org/svn/trunk/varnish-cache@668 d4fa192b-c00b-0410-8231-f00ffab90ce4
parent 3b228c3a
......@@ -4,6 +4,7 @@
#include <errno.h>
#include <assert.h>
#include <poll.h>
#include <stdarg.h>
#include <stdio.h>
#include <sys/uio.h>
......@@ -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);
......
......@@ -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;
......@@ -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);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment