[master] 1b42934 Renovate the VLU API (Inspired by #2481)

Poul-Henning Kamp phk at FreeBSD.org
Mon Nov 6 09:44:06 UTC 2017


commit 1b42934877a5cde698ca080e0a490ab426965952
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Mon Nov 6 08:06:06 2017 +0000

    Renovate the VLU API (Inspired by #2481)

diff --git a/bin/varnishd/mgt/mgt_child.c b/bin/varnishd/mgt/mgt_child.c
index 3174cde..0c2eb11 100644
--- a/bin/varnishd/mgt/mgt_child.c
+++ b/bin/varnishd/mgt/mgt_child.c
@@ -221,7 +221,7 @@ static int __match_proto__(vev_cb_f)
 child_listener(const struct vev *e, int what)
 {
 
-	if ((what & ~EV_RD) || VLU_Fd(child_output, child_std_vlu)) {
+	if ((what & ~EV_RD) || VLU_Fd(child_std_vlu, child_output)) {
 		ev_listen = NULL;
 		if (e != NULL)
 			mgt_reap_child();
@@ -361,7 +361,7 @@ mgt_launch_child(struct cli *cli)
 	MCH_Fd_Inherit(heritage.cli_out, NULL);
 	closefd(&heritage.cli_out);
 
-	child_std_vlu = VLU_New(NULL, child_line, 0);
+	child_std_vlu = VLU_New(child_line, NULL, 0);
 	AN(child_std_vlu);
 
 	AZ(ev_listen);
@@ -515,7 +515,7 @@ mgt_reap_child(void)
 	/* Pick up any stuff lingering on stdout/stderr */
 	(void)child_listener(NULL, EV_RD);
 	closefd(&child_output);
-	VLU_Destroy(child_std_vlu);
+	VLU_Destroy(&child_std_vlu);
 
 	child_pid = -1;
 
diff --git a/bin/varnishtest/vtc_process.c b/bin/varnishtest/vtc_process.c
index 1f80adc..cb168eb 100644
--- a/bin/varnishtest/vtc_process.c
+++ b/bin/varnishtest/vtc_process.c
@@ -169,16 +169,10 @@ process_thread(void *priv)
 	struct process *p;
 	struct rusage ru;
 	int r;
-	struct vlu *vlu;
 
 	CAST_OBJ_NOTNULL(p, priv, PROCESS_MAGIC);
-	if (p->fd_from > 0) {
-		vlu = VLU_New(p, process_vlu_func, 1024);
-		AN(vlu);
-		while (!VLU_Fd(p->fd_from, vlu))
-			continue;
-		VLU_Destroy(vlu);
-	}
+	if (p->fd_from > 0)
+		(void)VLU_File(p->fd_from, process_vlu_func, p, 1024);
 	r = wait4(p->pid, &p->status, 0, &ru);
 
 	AZ(pthread_mutex_lock(&p->mtx));
diff --git a/include/vlu.h b/include/vlu.h
index 1b5cd4a..f92642e 100644
--- a/include/vlu.h
+++ b/include/vlu.h
@@ -27,11 +27,13 @@
  * a function on each.
  */
 
-#ifndef VLU_H_INCLUDED
+#ifdef VLU_H_INCLUDED
+#  error "vlu.h included multiple times"
+#endif
 #define VLU_H_INCLUDED
 
 typedef int (vlu_f)(void *, const char *);
-struct vlu *VLU_New(void *priv, vlu_f *func, unsigned bufsize);
-int VLU_Fd(int fd, struct vlu *l);
-void VLU_Destroy(struct vlu *l);
-#endif
+struct vlu *VLU_New(vlu_f *, void *, unsigned);
+int VLU_Fd(struct vlu *, int);
+void VLU_Destroy(struct vlu **);
+int VLU_File(int, vlu_f *, void *, unsigned);
diff --git a/lib/libvarnish/vcli_serve.c b/lib/libvarnish/vcli_serve.c
index 2196388..aff32a2 100644
--- a/lib/libvarnish/vcli_serve.c
+++ b/lib/libvarnish/vcli_serve.c
@@ -431,7 +431,7 @@ VCLS_AddFd(struct VCLS *cs, int fdi, int fdo, cls_cb_f *closefunc, void *priv)
 	cfd->fdo = fdo;
 	cfd->cli = &cfd->clis;
 	cfd->cli->magic = CLI_MAGIC;
-	cfd->cli->vlu = VLU_New(cfd, cls_vlu, *cs->maxlen);
+	cfd->cli->vlu = VLU_New(cls_vlu, cfd, *cs->maxlen);
 	AN(cfd->cli->vlu);
 	cfd->cli->sb = VSB_new_auto();
 	AN(cfd->cli->sb);
@@ -453,7 +453,7 @@ cls_close_fd(struct VCLS *cs, struct VCLS_fd *cfd)
 
 	VTAILQ_REMOVE(&cs->fds, cfd, list);
 	cs->nfd--;
-	VLU_Destroy(cfd->cli->vlu);
+	VLU_Destroy(&cfd->cli->vlu);
 	VSB_destroy(&cfd->cli->sb);
 	if (cfd->closefunc == NULL) {
 		(void)close(cfd->fdi);
@@ -552,7 +552,7 @@ VCLS_PollFd(struct VCLS *cs, int fd, int timeout)
 	if (pfd[0].revents & POLLHUP)
 		k = 1;
 	else
-		k = VLU_Fd(cfd->fdi, cfd->cli->vlu);
+		k = VLU_Fd(cfd->cli->vlu, cfd->fdi);
 	if (k)
 		cls_close_fd(cs, cfd);
 	return (k);
@@ -591,7 +591,7 @@ VCLS_Poll(struct VCLS *cs, int timeout)
 			if (pfd[i].revents & POLLHUP)
 				k = 1;
 			else
-				k = VLU_Fd(cfd->fdi, cfd->cli->vlu);
+				k = VLU_Fd(cfd->cli->vlu, cfd->fdi);
 			if (k)
 				cls_close_fd(cs, cfd);
 			i++;
diff --git a/lib/libvarnish/vlu.c b/lib/libvarnish/vlu.c
index 7a9672e..a636804 100644
--- a/lib/libvarnish/vlu.c
+++ b/lib/libvarnish/vlu.c
@@ -50,7 +50,7 @@ struct vlu {
 };
 
 struct vlu *
-VLU_New(void *priv, vlu_f *func, unsigned bufsize)
+VLU_New(vlu_f *func, void *priv, unsigned bufsize)
 {
 	struct vlu *l;
 
@@ -71,9 +71,13 @@ VLU_New(void *priv, vlu_f *func, unsigned bufsize)
 }
 
 void
-VLU_Destroy(struct vlu *l)
+VLU_Destroy(struct vlu **lp)
 {
+	struct vlu *l;
 
+	AN(lp);
+	l = *lp;
+	*lp = NULL;
 	CHECK_OBJ_NOTNULL(l, LINEUP_MAGIC);
 	free(l->buf);
 	FREE_OBJ(l);
@@ -111,7 +115,7 @@ LineUpProcess(struct vlu *l)
 }
 
 int
-VLU_Fd(int fd, struct vlu *l)
+VLU_Fd(struct vlu *l, int fd)
 {
 	int i;
 
@@ -122,3 +126,18 @@ VLU_Fd(int fd, struct vlu *l)
 	l->bufp += i;
 	return (LineUpProcess(l));
 }
+
+int
+VLU_File(int fd, vlu_f *func, void *priv, unsigned bufsize)
+{
+	struct vlu *vlu;
+	int i;
+
+	vlu = VLU_New(func, priv, bufsize);
+	AN(vlu);
+	do {
+		i = VLU_Fd(vlu, fd);
+	} while (i == 0);
+	VLU_Destroy(&vlu);
+	return (i);
+}
diff --git a/lib/libvarnish/vsub.c b/lib/libvarnish/vsub.c
index 9881b0d..48f56d7 100644
--- a/lib/libvarnish/vsub.c
+++ b/lib/libvarnish/vsub.c
@@ -89,7 +89,6 @@ VSUB_run(struct vsb *sb, vsub_func_f *func, void *priv, const char *name,
 {
 	int rv, p[2], status;
 	pid_t pid;
-	struct vlu *vlu;
 	struct vsub_priv sp;
 
 	sp.sb = sb;
@@ -126,12 +125,7 @@ VSUB_run(struct vsb *sb, vsub_func_f *func, void *priv, const char *name,
 		_exit(4);
 	}
 	closefd(&p[1]);
-	vlu = VLU_New(&sp, vsub_vlu, 0);
-	AN(vlu);
-	while (!VLU_Fd(p[0], vlu))
-		continue;
-	closefd(&p[0]);
-	VLU_Destroy(vlu);
+	(void)VLU_File(p[0], vsub_vlu, &sp, 0);
 	if (sp.maxlines >= 0 && sp.lines > sp.maxlines)
 		VSB_printf(sb, "[%d lines truncated]\n",
 		    sp.lines - sp.maxlines);


More information about the varnish-commit mailing list