Commit 440441e6 authored by Poul-Henning Kamp's avatar Poul-Henning Kamp

More defensive coding and a couple of bugs less.


git-svn-id: http://www.varnish-cache.org/svn/trunk/varnish-cache@670 d4fa192b-c00b-0410-8231-f00ffab90ce4
parent e95d58c0
......@@ -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);
......
......@@ -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);
......
......@@ -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();
......
......@@ -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);
......
......@@ -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);
......
......@@ -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;
......
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