r2475 - trunk/varnish-cache/bin/varnishd

phk at projects.linpro.no phk at projects.linpro.no
Fri Feb 15 12:15:59 CET 2008


Author: phk
Date: 2008-02-15 12:15:59 +0100 (Fri, 15 Feb 2008)
New Revision: 2475

Modified:
   trunk/varnish-cache/bin/varnishd/cache_cli.c
Log:
Rewrite the childs CLI handling to use VLU


Modified: trunk/varnish-cache/bin/varnishd/cache_cli.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache_cli.c	2008-02-15 11:15:41 UTC (rev 2474)
+++ trunk/varnish-cache/bin/varnishd/cache_cli.c	2008-02-15 11:15:59 UTC (rev 2475)
@@ -43,6 +43,7 @@
 #include "cli_priv.h"
 #include "cli_common.h"
 #include "cache.h"
+#include "vlu.h"
 #include "vsb.h"
 #include "heritage.h"
 
@@ -108,13 +109,33 @@
 	{ NULL }
 };
 
+static int
+cli_vlu(void *priv, const char *p)
+{
+	struct cli *cli;
+	int i;
+
+	cli = priv;
+	VSL(SLT_CLI, 0, "Rd %s", p);
+	vsb_clear(cli->sb);
+	cli_dispatch(cli, CLI_cmds, p);
+	vsb_finish(cli->sb);
+	AZ(vsb_overflowed(cli->sb));
+	i = cli_writeres(heritage.fds[1], cli);
+	if (i)
+		VSL(SLT_Error, 0, "CLI write failed (errno=%d)", errno);
+	else
+		VSL(SLT_CLI, 0, "Wr %d %d %s",
+		    i, cli->result, vsb_data(cli->sb));
+	return (0);
+}
+
 void
 CLI_Init(void)
 {
 	struct pollfd pfd[1];
-	char *buf, *p;
-	unsigned nbuf, lbuf;
 	struct cli *cli, clis;
+	struct vlu *vlu;
 	int i;
 
 	cli = &clis;
@@ -123,11 +144,9 @@
 	cli_thread = pthread_self();
 	cli->sb = vsb_new(NULL, NULL, 0, VSB_AUTOEXTEND);
 	XXXAN(cli->sb);
-	lbuf = 4096;
-	buf = malloc(lbuf);
-	XXXAN(buf);
-	nbuf = 0;
-	printf("CLI ready\n");
+	vlu = VLU_New(cli, cli_vlu, 0);
+	XXXAN(vlu);
+	printf("Ready\n");
 	while (1) {
 		pfd[0].fd = heritage.fds[2];
 		pfd[0].events = POLLIN;
@@ -136,39 +155,18 @@
 			VCL_Idle();
 			continue;
 		}
-		if ((nbuf + 2) >= lbuf) {
-			lbuf += lbuf;
-			buf = realloc(buf, lbuf);
-			XXXAN(buf);
+		if (pfd[0].revents & POLLHUP) {
+			fprintf(stderr,
+			    "EOF on CLI connection, exiting\n");
+			break;
 		}
-		i = read(heritage.fds[2], buf + nbuf, lbuf - nbuf);
-		if (i <= 0) {
-			VSL(SLT_Error, 0, "CLI read %d (errno=%d)", i, errno);
-			free(buf);
-			return;
-		}
-		nbuf += i;
-		p = strchr(buf, '\n');
-		if (p == NULL)
-			continue;
-		*p = '\0';
-		VSL(SLT_CLI, 0, "Rd %s", buf);
-		vsb_clear(cli->sb);
-		cli_dispatch(cli, CLI_cmds, buf);
-		vsb_finish(cli->sb);
-		AZ(vsb_overflowed(cli->sb));
-		i = cli_writeres(heritage.fds[1], cli);
+		i = VLU_Fd(heritage.fds[2], vlu);
 		if (i) {
-			VSL(SLT_Error, 0, "CLI write failed (errno=%d)", errno);
-			free(buf);
-			return;
+			fprintf(stderr,
+			    "Error on CLI connection, exiting "
+			    "(VLU_Fd %d ev: %x)\n",
+				i, pfd[0].revents);
+			break;
 		}
-		VSL(SLT_CLI, 0, "Wr %d %d %s",
-		    i, cli->result, vsb_data(cli->sb));
-		i = ++p - buf;
-		assert(i <= nbuf);
-		if (i < nbuf)
-			memcpy(buf, p, nbuf - i);
-		nbuf -= i;
 	}
 }




More information about the varnish-commit mailing list