[master] cf14a0fd7 Add convenience function VSB_tofile() to write a VSB to a fd.

Poul-Henning Kamp phk at FreeBSD.org
Wed May 22 08:50:14 UTC 2019


commit cf14a0fd774fcbcd4bfd2d50bcbe029f63ff7e0e
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Wed May 22 06:55:20 2019 +0000

    Add convenience function VSB_tofile() to write a VSB to a fd.

diff --git a/bin/varnishd/common/common_vsmw.c b/bin/varnishd/common/common_vsmw.c
index 7a2d9c040..498045d4e 100644
--- a/bin/varnishd/common/common_vsmw.c
+++ b/bin/varnishd/common/common_vsmw.c
@@ -169,7 +169,6 @@ static void
 vsmw_addseg(struct vsmw *vsmw, struct vsmwseg *seg)
 {
 	int fd;
-	ssize_t s;
 
 	VTAILQ_INSERT_TAIL(&vsmw->segs, seg, list);
 	fd = openat(vsmw->vdirfd, vsmw->idx, O_APPEND | O_WRONLY);
@@ -177,9 +176,7 @@ vsmw_addseg(struct vsmw *vsmw, struct vsmwseg *seg)
 	VSB_clear(vsmw->vsb);
 	vsmw_fmt_index(vsmw, seg);
 	AZ(VSB_finish(vsmw->vsb));
-	s = write(fd, VSB_data(vsmw->vsb), VSB_len(vsmw->vsb));
-	// XXX handle ENOSPC? #2764
-	assert(s == VSB_len(vsmw->vsb));
+	XXXAZ(VSB_tofile(fd, vsmw->vsb)); // XXX handle ENOSPC? #2764
 	closefd(&fd);
 }
 
@@ -189,7 +186,6 @@ static void
 vsmw_delseg(struct vsmw *vsmw, struct vsmwseg *seg, int fixidx)
 {
 	char *t = NULL;
-	ssize_t s;
 	int fd;
 
 	CHECK_OBJ_NOTNULL(vsmw, VSMW_MAGIC);
@@ -212,9 +208,7 @@ vsmw_delseg(struct vsmw *vsmw, struct vsmwseg *seg, int fixidx)
 		VTAILQ_FOREACH(seg, &vsmw->segs, list)
 			vsmw_fmt_index(vsmw, seg);
 		AZ(VSB_finish(vsmw->vsb));
-		s = write(fd, VSB_data(vsmw->vsb), VSB_len(vsmw->vsb));
-		// XXX handle ENOSPC? #2764
-		assert(s == VSB_len(vsmw->vsb));
+		XXXAZ(VSB_tofile(fd, vsmw->vsb)); // XXX handle ENOSPC? #2764
 		closefd(&fd);
 		AZ(renameat(vsmw->vdirfd, t, vsmw->vdirfd, vsmw->idx));
 		REPLACE(t, NULL);
diff --git a/bin/varnishd/mgt/mgt_cli.c b/bin/varnishd/mgt/mgt_cli.c
index 63a696eaa..2798eceb6 100644
--- a/bin/varnishd/mgt/mgt_cli.c
+++ b/bin/varnishd/mgt/mgt_cli.c
@@ -144,8 +144,7 @@ mcf_askchild(struct cli *cli, const char * const *av, void *priv)
 	}
 	VSB_putc(vsb, '\n');
 	AZ(VSB_finish(vsb));
-	i = write(cli_o, VSB_data(vsb), VSB_len(vsb));
-	if (i != VSB_len(vsb)) {
+	if (VSB_tofile(cli_o, vsb)) {
 		VSB_destroy(&vsb);
 		VCLI_SetResult(cli, CLIS_COMMS);
 		VCLI_Out(cli, "CLI communication error");
@@ -176,7 +175,7 @@ static struct cli_proto cli_askchild[] = {
 int
 mgt_cli_askchild(unsigned *status, char **resp, const char *fmt, ...)
 {
-	int i, j;
+	int i;
 	va_list ap;
 	unsigned u;
 
@@ -202,8 +201,7 @@ mgt_cli_askchild(unsigned *status, char **resp, const char *fmt, ...)
 	AZ(VSB_finish(cli_buf));
 	i = VSB_len(cli_buf);
 	assert(i > 0 && VSB_data(cli_buf)[i - 1] == '\n');
-	j = write(cli_o, VSB_data(cli_buf), i);
-	if (j != i) {
+	if (VSB_tofile(cli_o, cli_buf)) {
 		if (status != NULL)
 			*status = CLIS_COMMS;
 		if (resp != NULL)
diff --git a/bin/varnishd/proxy/cache_proxy_proto.c b/bin/varnishd/proxy/cache_proxy_proto.c
index 867c48ccb..ee9c19601 100644
--- a/bin/varnishd/proxy/cache_proxy_proto.c
+++ b/bin/varnishd/proxy/cache_proxy_proto.c
@@ -645,7 +645,7 @@ VPX_Send_Proxy(int fd, int version, const struct sess *sp)
 		WRONG("Wrong proxy version");
 
 	AZ(VSB_finish(vsb));
-	(void)write(fd, VSB_data(vsb), VSB_len(vsb));
+	(void)VSB_tofile(fd, vsb);	// XXX: Error handling ?
 	if (!DO_DEBUG(DBG_PROTOCOL)) {
 		VSB_delete(vsb);
 		return;
diff --git a/bin/varnishtest/vtc_haproxy.c b/bin/varnishtest/vtc_haproxy.c
index a93a93c3b..ccd0fba80 100644
--- a/bin/varnishtest/vtc_haproxy.c
+++ b/bin/varnishtest/vtc_haproxy.c
@@ -190,7 +190,6 @@ cmd_haproxy_cli_send(CMD_ARGS)
 {
 	struct vsb *vsb;
 	struct haproxy_cli *hc;
-	ssize_t wr;
 
 	(void)cmd;
 	(void)vl;
@@ -221,8 +220,7 @@ cmd_haproxy_cli_send(CMD_ARGS)
 	}
 	vtc_dump(hc->vl, 4, "CLI send", VSB_data(vsb), -1);
 
-	wr = write(hc->sock, VSB_data(vsb), VSB_len(vsb));
-	if (wr != VSB_len(vsb))
+	if (VSB_tofile(hc->sock, vsb))
 		vtc_fatal(hc->vl,
 		    "CLI fd %d send error %s", hc->sock, strerror(errno));
 
diff --git a/bin/varnishtest/vtc_http.c b/bin/varnishtest/vtc_http.c
index 6e249ae24..2df6cab45 100644
--- a/bin/varnishtest/vtc_http.c
+++ b/bin/varnishtest/vtc_http.c
@@ -212,14 +212,12 @@ synth_body(const char *len, int rnd)
 static void
 http_write(const struct http *hp, int lvl, const char *pfx)
 {
-	ssize_t l;
 
 	AZ(VSB_finish(hp->vsb));
 	vtc_dump(hp->vl, lvl, pfx, VSB_data(hp->vsb), VSB_len(hp->vsb));
-	l = write(hp->fd, VSB_data(hp->vsb), VSB_len(hp->vsb));
-	if (l != VSB_len(hp->vsb))
-		vtc_log(hp->vl, hp->fatal, "Write failed: (%zd vs %zd) %s",
-		    l, VSB_len(hp->vsb), strerror(errno));
+	if (VSB_tofile(hp->fd, hp->vsb))
+		vtc_log(hp->vl, hp->fatal, "Write failed: %s",
+		    strerror(errno));
 }
 
 /**********************************************************************
@@ -1467,7 +1465,6 @@ cmd_http_sendhex(CMD_ARGS)
 {
 	struct vsb *vsb;
 	struct http *hp;
-	int j;
 
 	(void)cmd;
 	(void)vl;
@@ -1477,8 +1474,9 @@ cmd_http_sendhex(CMD_ARGS)
 	vsb = vtc_hex_to_bin(hp->vl, av[1]);
 	assert(VSB_len(vsb) >= 0);
 	vtc_hexdump(hp->vl, 4, "sendhex", VSB_data(vsb), VSB_len(vsb));
-	j = write(hp->fd, VSB_data(vsb), VSB_len(vsb));
-	assert(j == VSB_len(vsb));
+	if (VSB_tofile(hp->fd, vsb))
+		vtc_log(hp->vl, hp->fatal, "Write failed: %s",
+		    strerror(errno));
 	VSB_destroy(&vsb);
 }
 
diff --git a/bin/varnishtest/vtc_process.c b/bin/varnishtest/vtc_process.c
index 2b1534be1..92a591786 100644
--- a/bin/varnishtest/vtc_process.c
+++ b/bin/varnishtest/vtc_process.c
@@ -785,7 +785,6 @@ static void
 process_write_hex(const struct process *p, const char *text)
 {
 	struct vsb *vsb;
-	int j;
 
 	if (!p->hasthread)
 		vtc_fatal(p->vl, "Cannot write to a non-running process");
@@ -793,8 +792,7 @@ process_write_hex(const struct process *p, const char *text)
 	vsb = vtc_hex_to_bin(p->vl, text);
 	assert(VSB_len(vsb) >= 0);
 	vtc_hexdump(p->vl, 4, "sendhex", VSB_data(vsb), VSB_len(vsb));
-	j = write(p->fd_term, VSB_data(vsb), VSB_len(vsb));
-	assert(j == VSB_len(vsb));
+	AZ(VSB_tofile(p->fd_term, vsb));
 	VSB_destroy(&vsb);
 }
 
diff --git a/bin/varnishtest/vtc_proxy.c b/bin/varnishtest/vtc_proxy.c
index 03e186b57..4477ab056 100644
--- a/bin/varnishtest/vtc_proxy.c
+++ b/bin/varnishtest/vtc_proxy.c
@@ -84,7 +84,7 @@ vtc_send_proxy(int fd, int version, const struct suckaddr *sac,
 	char pc[VTCP_PORTBUFSIZE];
 	char hs[VTCP_ADDRBUFSIZE];
 	char ps[VTCP_PORTBUFSIZE];
-	int i, len;
+	int i;
 	int proto;
 
 	AN(sac);
@@ -126,8 +126,7 @@ vtc_send_proxy(int fd, int version, const struct suckaddr *sac,
 		WRONG("Wrong proxy version");
 
 	AZ(VSB_finish(vsb));
-	len = VSB_len(vsb);
-	i = write(fd, VSB_data(vsb), len);
+	i = VSB_tofile(fd, vsb);
 	VSB_delete(vsb);
-	return (i != len);
+	return (i);
 }
diff --git a/include/vsb.h b/include/vsb.h
index ea1ab4de8..47d66ac2e 100644
--- a/include/vsb.h
+++ b/include/vsb.h
@@ -86,6 +86,7 @@ void		 VSB_quote_pfx(struct vsb *, const char*, const void *,
 		     int len, int how);
 void		 VSB_quote(struct vsb *, const void *, int len, int how);
 void		 VSB_indent(struct vsb *, int);
+int		 VSB_tofile(int fd, const struct vsb *);
 #ifdef __cplusplus
 };
 #endif
diff --git a/lib/libvarnish/vsb.c b/lib/libvarnish/vsb.c
index 5101a9007..f95debe0d 100644
--- a/lib/libvarnish/vsb.c
+++ b/lib/libvarnish/vsb.c
@@ -34,6 +34,7 @@ __FBSDID("$FreeBSD: head/sys/kern/subr_vsb.c 222004 2011-05-17 06:36:32Z phk $")
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <unistd.h>
 
 #include "vdef.h"
 #include "vas.h"	// XXX Flexelint "not used" - but req'ed for assert()
@@ -618,7 +619,7 @@ VSB_quote(struct vsb *s, const void *v, int len, int how)
  */
 
 void
-VSB_indent(struct vsb * s, int i)
+VSB_indent(struct vsb *s, int i)
 {
 
 	assert_VSB_integrity(s);
@@ -627,3 +628,14 @@ VSB_indent(struct vsb * s, int i)
 	else
 		s->s_indent += i;
 }
+
+int
+VSB_tofile(int fd, const struct vsb *s)
+{
+	int sz;
+
+	assert_VSB_integrity(s);
+	assert_VSB_state(s, VSB_FINISHED);
+	sz = write(fd, VSB_data(s), VSB_len(s));
+	return (sz == VSB_len(s) ? 0 : -1);
+}


More information about the varnish-commit mailing list