r670 - trunk/varnish-cache/bin/varnishd

phk at projects.linpro.no phk at projects.linpro.no
Sat Aug 5 16:24:21 CEST 2006


Author: phk
Date: 2006-08-05 16:24:21 +0200 (Sat, 05 Aug 2006)
New Revision: 670

Modified:
   trunk/varnish-cache/bin/varnishd/common_cli.c
   trunk/varnish-cache/bin/varnishd/mgt.h
   trunk/varnish-cache/bin/varnishd/mgt_child.c
   trunk/varnish-cache/bin/varnishd/mgt_cli.c
   trunk/varnish-cache/bin/varnishd/mgt_event.c
   trunk/varnish-cache/bin/varnishd/mgt_vcc.c
Log:
More defensive coding and a couple of bugs less.


Modified: trunk/varnish-cache/bin/varnishd/common_cli.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/common_cli.c	2006-08-05 14:22:33 UTC (rev 669)
+++ trunk/varnish-cache/bin/varnishd/common_cli.c	2006-08-05 14:24:21 UTC (rev 670)
@@ -76,11 +76,11 @@
 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);

Modified: trunk/varnish-cache/bin/varnishd/mgt.h
===================================================================
--- trunk/varnish-cache/bin/varnishd/mgt.h	2006-08-05 14:22:33 UTC (rev 669)
+++ trunk/varnish-cache/bin/varnishd/mgt.h	2006-08-05 14:24:21 UTC (rev 670)
@@ -15,14 +15,14 @@
 
 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);

Modified: trunk/varnish-cache/bin/varnishd/mgt_child.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/mgt_child.c	2006-08-05 14:22:33 UTC (rev 669)
+++ trunk/varnish-cache/bin/varnishd/mgt_child.c	2006-08-05 14:24:21 UTC (rev 670)
@@ -32,6 +32,8 @@
 static unsigned 	child_should_run;
 
 struct evbase		*mgt_evb;
+struct ev		*ev_poker;
+struct ev		*ev_listen;
 
 /*--------------------------------------------------------------------*/
 
@@ -42,11 +44,15 @@
 	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 @@
 	e->name = "Child listener";
 	e->callback = child_listener;
 	AZ(ev_add(mgt_evb, e));
+	ev_listen = e;
 
 	e = ev_new();
 	assert(e != NULL);
@@ -127,8 +134,8 @@
 	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]));
 	heritage.fds[1] = -1;
@@ -151,6 +158,10 @@
 	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 @@
 
 	(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 @@
 		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 @@
 	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();
 

Modified: trunk/varnish-cache/bin/varnishd/mgt_cli.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/mgt_cli.c	2006-08-05 14:22:33 UTC (rev 669)
+++ trunk/varnish-cache/bin/varnishd/mgt_cli.c	2006-08-05 14:24:21 UTC (rev 670)
@@ -171,11 +171,12 @@
  */
 
 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 @@
 	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 @@
 		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);

Modified: trunk/varnish-cache/bin/varnishd/mgt_event.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/mgt_event.c	2006-08-05 14:22:33 UTC (rev 669)
+++ trunk/varnish-cache/bin/varnishd/mgt_event.c	2006-08-05 14:24:21 UTC (rev 670)
@@ -89,10 +89,10 @@
 
 	if (evb->npfd > 256)
 		u = evb->npfd + 256;
-	else if (evb->npfd > 8)
+	else if (evb->npfd < 8)
+		u = 8;
+	else
 		u = evb->npfd * 2;
-	else
-		u = 8;
 	p = realloc(evb->pfd, sizeof *evb->pfd * u);
 	if (p == NULL)
 		return (1);
@@ -217,6 +217,7 @@
 	}
 
 	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 @@
 
 	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 @@
 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 @@
 	CHECK_OBJ_NOTNULL(evb, EVBASE_MAGIC);
 	e = binheap_root(evb->binheap);
 	if (e != NULL) {
+		CHECK_OBJ_NOTNULL(e, EV_MAGIC);
 		assert(e->__binheap_idx == 1);
-		CHECK_OBJ_NOTNULL(e, EV_MAGIC);
 		t = ev_now();
 		if (e->__when <= t)
 			return (ev_sched_timeout(evb, e, t));
@@ -389,6 +411,7 @@
 
 	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 @@
 		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);

Modified: trunk/varnish-cache/bin/varnishd/mgt_vcc.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/mgt_vcc.c	2006-08-05 14:22:33 UTC (rev 669)
+++ trunk/varnish-cache/bin/varnishd/mgt_vcc.c	2006-08-05 14:24:21 UTC (rev 670)
@@ -170,7 +170,7 @@
 /*--------------------------------------------------------------------*/
 
 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 @@
 {
 	char *vf, *p;
 	struct sbuf *sb;
-	int status;
+	unsigned status;
 
 	(void)priv;
 
@@ -251,7 +251,7 @@
 {
 	char *vf;
 	struct sbuf *sb;
-	int status;
+	unsigned status;
 	char *p;
 
 	(void)priv;
@@ -295,7 +295,7 @@
 void
 mcf_config_use(struct cli *cli, char **av, void *priv)
 {
-	int status;
+	unsigned status;
 	char *p;
 	struct vclprog *vp;
 
@@ -321,7 +321,7 @@
 void
 mcf_config_discard(struct cli *cli, char **av, void *priv)
 {
-	int status;
+	unsigned status;
 	char *p;
 	struct vclprog *vp;
 
@@ -345,7 +345,7 @@
 void
 mcf_config_list(struct cli *cli, char **av, void *priv)
 {
-	int status;
+	unsigned status;
 	char *p;
 	struct vclprog *vp;
 




More information about the varnish-commit mailing list