]> err.no Git - varnish/commitdiff
Add a timeout to reads from the child CLI pipe so we don't hang
authorphk <phk@d4fa192b-c00b-0410-8231-f00ffab90ce4>
Sat, 5 Aug 2006 12:45:45 +0000 (12:45 +0000)
committerphk <phk@d4fa192b-c00b-0410-8231-f00ffab90ce4>
Sat, 5 Aug 2006 12:45:45 +0000 (12:45 +0000)
for ever.

git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@668 d4fa192b-c00b-0410-8231-f00ffab90ce4

varnish-cache/bin/varnishd/common_cli.c
varnish-cache/bin/varnishd/common_cli.h
varnish-cache/bin/varnishd/mgt_cli.c

index 9435db37e56b391e95cfeb0772f3ef8b71398ffa..716f162c9ef3c538175e711b3d6c398b8d8724f4 100644 (file)
@@ -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);
index add36893da019f343ac3fb9d491e88595b4e94ba..12d19d4ff3f1ac2ecf3c234f729f01ca2d5c78ab 100644 (file)
@@ -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;
index e8e29d86fc1e36edb2afc413ba180fd87fbc769e..fff5974c556a4608d6b5c85b69a301a27f7aeaa1 100644 (file)
@@ -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);