]> err.no Git - varnish/commitdiff
More defensive coding and a couple of bugs less.
authorphk <phk@d4fa192b-c00b-0410-8231-f00ffab90ce4>
Sat, 5 Aug 2006 14:24:21 +0000 (14:24 +0000)
committerphk <phk@d4fa192b-c00b-0410-8231-f00ffab90ce4>
Sat, 5 Aug 2006 14:24:21 +0000 (14:24 +0000)
git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@670 d4fa192b-c00b-0410-8231-f00ffab90ce4

varnish-cache/bin/varnishd/common_cli.c
varnish-cache/bin/varnishd/mgt.h
varnish-cache/bin/varnishd/mgt_child.c
varnish-cache/bin/varnishd/mgt_cli.c
varnish-cache/bin/varnishd/mgt_event.c
varnish-cache/bin/varnishd/mgt_vcc.c

index 716f162c9ef3c538175e711b3d6c398b8d8724f4..382cda421d266ec04eac784f5c8473c8ad4b167f 100644 (file)
@@ -76,11 +76,11 @@ static int
 read_tmo(int fd, void *ptr, unsigned len, double tmo)
 {
        int i;
-       struct pollfd pfd[1];
+       struct pollfd pfd;
 
-       pfd->fd = fd;
-       pfd->events = POLLIN;
-       i = poll(pfd, 1, (int)(tmo * 1e3));
+       pfd.fd = fd;
+       pfd.events = POLLIN;
+       i = poll(&pfd, 1, (int)(tmo * 1e3));
        if (i == 0) {
                errno = ETIMEDOUT;
                return (-1);
index 23d5909d760b53fcdb5d41d5d911018bf3cdcf02..807fe4552a735b9dc8ea6fa83f1973292a4cdd76 100644 (file)
@@ -15,14 +15,14 @@ extern pid_t mgt_pid, child_pid;
 
 void mgt_cli_init(void);
 void mgt_cli_setup(int fdi, int fdo, int verbose);
-int mgt_cli_askchild(int *status, char **resp, const char *fmt, ...);
+int mgt_cli_askchild(unsigned *status, char **resp, const char *fmt, ...);
 void mgt_cli_start_child(int fdi, int fdo);
 void mgt_cli_stop_child(void);
 
 /* mgt_vcc.c */
 void mgt_vcc_init(void);
 int mgt_vcc_default(const char *bflag, const char *fflag);
-int mgt_push_vcls_and_start(int *status, char **p);
+int mgt_push_vcls_and_start(unsigned *status, char **p);
 
 /* tcp.c */
 int open_tcp(const char *port);
index 8d913aa3196c94b034ce5a662508880f26de3f78..ac74847d2bc3a554bd911849ce910e7498691707 100644 (file)
@@ -32,6 +32,8 @@ static int            child_fds[2];
 static unsigned        child_should_run;
 
 struct evbase          *mgt_evb;
+struct ev              *ev_poker;
+struct ev              *ev_listen;
 
 /*--------------------------------------------------------------------*/
 
@@ -42,11 +44,15 @@ child_listener(struct ev *e, int what)
        char buf[BUFSIZ];
 
        (void)e;
-       if ((what & ~EV_RD))
+       if ((what & ~EV_RD)) {
+               ev_listen = NULL;
                return (1);
+       }
        i = read(child_fds[0], buf, sizeof buf - 1);
-       if (i <= 0)
+       if (i <= 0) {
+               ev_listen = NULL;
                return (1);
+       }
        buf[i] = '\0';
        printf("Child said: <<%s>>\n", buf);
        return (0);
@@ -120,6 +126,7 @@ start_child(void)
        e->name = "Child listener";
        e->callback = child_listener;
        AZ(ev_add(mgt_evb, e));
+       ev_listen = e;
 
        e = ev_new();
        assert(e != NULL);
@@ -127,7 +134,7 @@ start_child(void)
        e->callback = child_poker;
        e->name = "child poker";
        AZ(ev_add(mgt_evb, e));
-
+       ev_poker = e;
 
        mgt_cli_start_child(heritage.fds[0], heritage.fds[3]);
        AZ(close(heritage.fds[1]));
@@ -151,6 +158,10 @@ stop_child(void)
        if (child_pid < 0)
                return;
 
+       if (ev_poker != NULL)
+               ev_del(mgt_evb, ev_poker);
+       ev_poker = NULL;
+
        child_should_run = 0;
 
        printf("Clean child\n");
@@ -175,6 +186,11 @@ mgt_sigchld(struct ev *e, int what)
 
        (void)e;
        (void)what;
+
+       if (ev_poker != NULL)
+               ev_del(mgt_evb, ev_poker);
+       ev_poker = NULL;
+
        r = wait4(-1, &status, WNOHANG, NULL);
        if (r != child_pid) {
                printf("Unknown child died pid=%d status=0x%x\n",
@@ -195,6 +211,10 @@ mgt_sigchld(struct ev *e, int what)
                heritage.fds[3] = -1;
        }
 
+       if (ev_listen != NULL)
+               ev_del(mgt_evb, ev_listen);
+       ev_listen = NULL;
+
        AZ(close(child_fds[0]));
        child_fds[0] = -1;
        printf("Child cleaned\n");
@@ -261,7 +281,8 @@ mgt_run(int dflag)
        AZ(sigaction(SIGPIPE, &sac, NULL));
        AZ(sigaction(SIGHUP, &sac, NULL));
 
-       printf("rolling...\n");
+       printf("rolling(1)...\n");
+       fprintf(stderr, "rolling(2)...\n");
        if (!dflag)
                start_child();
 
index fff5974c556a4608d6b5c85b69a301a27f7aeaa1..908511a9d2b0e764c4c11f1c4a3f7f26e59a807a 100644 (file)
@@ -171,11 +171,12 @@ mgt_cli_init(void)
  */
 
 int
-mgt_cli_askchild(int *status, char **resp, const char *fmt, ...)
+mgt_cli_askchild(unsigned *status, char **resp, const char *fmt, ...)
 {
        char *p;
-       int i, j;
+       int i;
        va_list ap;
+       unsigned u;
 
        va_start(ap, fmt);
        i = vasprintf(&p, fmt, ap);
@@ -187,11 +188,11 @@ mgt_cli_askchild(int *status, char **resp, const char *fmt, ...)
        assert(i == strlen(p));
        free(p);
 
-       i = cli_readres(cli_i, &j, resp, 3.0);
+       i = cli_readres(cli_i, &u, resp, 3.0);
        assert(i == 0);
        if (status != NULL)
-               *status = j;
-       return (j == CLIS_OK ? 0 : j);
+               *status = u;
+       return (u == CLIS_OK ? 0 : u);
 }
 
 /*--------------------------------------------------------------------*/
@@ -254,6 +255,7 @@ mgt_cli_callback(struct ev *e, int what)
                if (p == NULL)
                        return (0);
                *p = '\0';
+fprintf(stderr, "CLI <%s>\n", cp->buf);
                sbuf_clear(cp->cli->sb);
                cli_dispatch(cp->cli, cli_proto, cp->buf);
                sbuf_finish(cp->cli->sb);
index 16f62d70412f13f577cd6e8f0b1b8c45daab9b8c..9541d56002986901b866cc7d3ae3768ab174dbd4 100644 (file)
@@ -89,10 +89,10 @@ ev_get_pfd(struct evbase *evb)
 
        if (evb->npfd > 256)
                u = evb->npfd + 256;
-       else if (evb->npfd > 8)
-               u = evb->npfd * 2;
-       else
+       else if (evb->npfd < 8)
                u = 8;
+       else
+               u = evb->npfd * 2;
        p = realloc(evb->pfd, sizeof *evb->pfd * u);
        if (p == NULL)
                return (1);
@@ -217,6 +217,7 @@ ev_add(struct evbase *evb, struct ev *e)
        }
 
        if (e->fd >= 0) {
+               assert(evb->lpfd < evb->npfd);
                evb->pfd[evb->lpfd].fd = e->fd;
                evb->pfd[evb->lpfd].events =
                    e->fd_flags & (EV_RD|EV_WR|EV_ERR|EV_HUP);
@@ -268,7 +269,10 @@ ev_del(struct evbase *evb, struct ev *e)
 
        if (e->fd >= 0) {
                evb->pfd[e->__poll_idx].fd = -1;
-               evb->compact_pfd++;
+               if (e->__poll_idx == evb->lpfd - 1)
+                       evb->lpfd--;
+               else
+                       evb->compact_pfd++;
                e->fd = -1;
        }
 
@@ -311,7 +315,25 @@ ev_schedule(struct evbase *evb)
 static void
 ev_compact_pfd(struct evbase *evb)
 {
-       /* XXX TBD */
+       unsigned u;
+       struct pollfd *p;
+       struct ev *ep;
+
+       p = evb->pfd;
+       ep = TAILQ_FIRST(&evb->events);
+       for (u = 0; u < evb->lpfd; u++, p++) {
+               if (p->fd >= 0)
+                       continue;
+               for(; ep != NULL; ep = TAILQ_NEXT(ep, __list)) {
+                       if (ep->fd >= 0 && ep->__poll_idx > u)
+                               break;
+               } 
+               if (ep == NULL)
+                       break;
+               *p = evb->pfd[ep->__poll_idx];
+               ep->__poll_idx = u;
+       }
+       evb->lpfd = u;
        evb->compact_pfd = 0;
 }
 
@@ -370,8 +392,8 @@ ev_schedule_one(struct evbase *evb)
        CHECK_OBJ_NOTNULL(evb, EVBASE_MAGIC);
        e = binheap_root(evb->binheap);
        if (e != NULL) {
-               assert(e->__binheap_idx == 1);
                CHECK_OBJ_NOTNULL(e, EV_MAGIC);
+               assert(e->__binheap_idx == 1);
                t = ev_now();
                if (e->__when <= t)
                        return (ev_sched_timeout(evb, e, t));
@@ -389,6 +411,7 @@ ev_schedule_one(struct evbase *evb)
 
        if (evb->psig)
                return (ev_sched_signal(evb));
+       assert(evb->lpfd < evb->npfd);
        i = poll(evb->pfd, evb->lpfd, tmo);
        if(i == -1 && errno == EINTR)
                return (ev_sched_signal(evb));
@@ -407,7 +430,6 @@ ev_schedule_one(struct evbase *evb)
                assert(e->__poll_idx < evb->lpfd);
                pfd = &evb->pfd[e->__poll_idx];
                assert(pfd->fd == e->fd);
-               assert(pfd->events == e->fd_flags);
                if (!pfd->revents)
                        continue;
                j = e->callback(e, pfd->revents);
index 1295079bffd391228f24d118aebc6166e9d148b1..3b7b8767168073bc700d4cb935c61ea3e7f0c470 100644 (file)
@@ -170,7 +170,7 @@ mgt_vcc_default(const char *bflag, const char *fflag)
 /*--------------------------------------------------------------------*/
 
 int
-mgt_push_vcls_and_start(int *status, char **p)
+mgt_push_vcls_and_start(unsigned *status, char **p)
 {
        struct vclprog *vp;
 
@@ -221,7 +221,7 @@ mcf_config_inline(struct cli *cli, char **av, void *priv)
 {
        char *vf, *p;
        struct sbuf *sb;
-       int status;
+       unsigned status;
 
        (void)priv;
 
@@ -251,7 +251,7 @@ mcf_config_load(struct cli *cli, char **av, void *priv)
 {
        char *vf;
        struct sbuf *sb;
-       int status;
+       unsigned status;
        char *p;
 
        (void)priv;
@@ -295,7 +295,7 @@ mcf_find_vcl(struct cli *cli, const char *name)
 void
 mcf_config_use(struct cli *cli, char **av, void *priv)
 {
-       int status;
+       unsigned status;
        char *p;
        struct vclprog *vp;
 
@@ -321,7 +321,7 @@ mcf_config_use(struct cli *cli, char **av, void *priv)
 void
 mcf_config_discard(struct cli *cli, char **av, void *priv)
 {
-       int status;
+       unsigned status;
        char *p;
        struct vclprog *vp;
 
@@ -345,7 +345,7 @@ mcf_config_discard(struct cli *cli, char **av, void *priv)
 void
 mcf_config_list(struct cli *cli, char **av, void *priv)
 {
-       int status;
+       unsigned status;
        char *p;
        struct vclprog *vp;