Mailing List Archive

[6.0] 87fbff317 Add convenience function VSB_tofile() to write a VSB to a fd.
commit 87fbff3178695c8ae6b6baf4232d44f234742f56
Author: Poul-Henning Kamp <phk@FreeBSD.org>
Date: Wed May 22 06:55:20 2019 +0000

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

Conflicts:
bin/varnishd/common/common_vsmw.c

diff --git a/bin/varnishd/common/common_vsmw.c b/bin/varnishd/common/common_vsmw.c
index 5149b8bfb..dcf83dbd3 100644
--- a/bin/varnishd/common/common_vsmw.c
+++ b/bin/varnishd/common/common_vsmw.c
@@ -198,7 +198,6 @@ static void
vsmw_append_record(struct vsmw *vsmw, struct vsmwseg *seg, char act)
{
int fd;
- ssize_t s;

CHECK_OBJ_NOTNULL(vsmw, VSMW_MAGIC);
CHECK_OBJ_NOTNULL(seg, VSMWSEG_MAGIC);
@@ -207,9 +206,8 @@ vsmw_append_record(struct vsmw *vsmw, struct vsmwseg *seg, char act)
VSB_clear(vsmw->vsb);
vsmw_fmt_index(vsmw, seg, act);
AZ(VSB_finish(vsmw->vsb));
- s = write(fd, VSB_data(vsmw->vsb), VSB_len(vsmw->vsb));
- assert(s == VSB_len(vsmw->vsb)); // XXX handle ENOSPC? #2764
- AZ(close(fd));
+ XXXAZ(VSB_tofile(fd, vsmw->vsb)); // XXX handle ENOSPC? #2764
+ closefd(&fd);
}

/*--------------------------------------------------------------------*/
@@ -230,7 +228,6 @@ static void
vsmw_delseg(struct vsmw *vsmw, struct vsmwseg *seg)
{
char *t = NULL;
- ssize_t s;
int fd;
struct vsmwseg *s2;

@@ -256,9 +253,8 @@ vsmw_delseg(struct vsmw *vsmw, struct vsmwseg *seg)
VTAILQ_FOREACH(s2, &vsmw->segs, list)
vsmw_fmt_index(vsmw, s2, '+');
AZ(VSB_finish(vsmw->vsb));
- s = write(fd, VSB_data(vsmw->vsb), VSB_len(vsmw->vsb));
- assert(s == VSB_len(vsmw->vsb));
- AZ(close(fd));
+ XXXAZ(VSB_tofile(fd, vsmw->vsb)); // XXX handle ENOSPC? #2764
+ closefd(&fd);
AZ(renameat(vsmw->vdirfd, t, vsmw->vdirfd, vsmw->idx));
REPLACE(t, NULL);
vsmw->nsubs = 0;
diff --git a/bin/varnishd/mgt/mgt_cli.c b/bin/varnishd/mgt/mgt_cli.c
index 3677eb620..394664939 100644
--- a/bin/varnishd/mgt/mgt_cli.c
+++ b/bin/varnishd/mgt/mgt_cli.c
@@ -145,8 +145,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");
@@ -177,7 +176,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;

@@ -203,8 +202,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 64a3a2b08..ddcc8db77 100644
--- a/bin/varnishd/proxy/cache_proxy_proto.c
+++ b/bin/varnishd/proxy/cache_proxy_proto.c
@@ -667,7 +667,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 55e986f57..d6565b834 100644
--- a/bin/varnishtest/vtc_haproxy.c
+++ b/bin/varnishtest/vtc_haproxy.c
@@ -143,7 +143,6 @@ cmd_haproxy_cli_send(CMD_ARGS)
{
struct vsb *vsb;
struct haproxy_cli *hc;
- ssize_t wr;

(void)cmd;
(void)vl;
@@ -174,8 +173,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 e17643f8e..20969c27a 100644
--- a/bin/varnishtest/vtc_http.c
+++ b/bin/varnishtest/vtc_http.c
@@ -213,14 +213,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));
}

/**********************************************************************
@@ -1417,7 +1415,6 @@ cmd_http_sendhex(CMD_ARGS)
{
struct vsb *vsb;
struct http *hp;
- int j;

(void)cmd;
(void)vl;
@@ -1427,8 +1424,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 10d545b38..ae98f53e1 100644
--- a/bin/varnishtest/vtc_process.c
+++ b/bin/varnishtest/vtc_process.c
@@ -786,7 +786,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");
@@ -794,8 +793,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 d51088499..10e4c9563 100644
--- a/include/vsb.h
+++ b/include/vsb.h
@@ -85,6 +85,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 1261ee57d..e6519e98c 100644
--- a/lib/libvarnish/vsb.c
+++ b/lib/libvarnish/vsb.c
@@ -35,6 +35,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()
@@ -614,7 +615,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);
@@ -623,3 +624,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);
+}
_______________________________________________
varnish-commit mailing list
varnish-commit@varnish-cache.org
https://www.varnish-cache.org/lists/mailman/listinfo/varnish-commit