Skip to content

Commit

Permalink
Add convenience function VSB_tofile() to write a VSB to a fd.
Browse files Browse the repository at this point in the history
  • Loading branch information
bsdphk committed May 22, 2019
1 parent 7b5d416 commit cf14a0f
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 33 deletions.
10 changes: 2 additions & 8 deletions bin/varnishd/common/common_vsmw.c
Expand Up @@ -169,17 +169,14 @@ 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);
assert(fd >= 0);
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);
}

Expand All @@ -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);
Expand All @@ -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);
Expand Down
8 changes: 3 additions & 5 deletions bin/varnishd/mgt/mgt_cli.c
Expand Up @@ -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");
Expand Down Expand Up @@ -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;

Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion bin/varnishd/proxy/cache_proxy_proto.c
Expand Up @@ -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;
Expand Down
4 changes: 1 addition & 3 deletions bin/varnishtest/vtc_haproxy.c
Expand Up @@ -190,7 +190,6 @@ cmd_haproxy_cli_send(CMD_ARGS)
{
struct vsb *vsb;
struct haproxy_cli *hc;
ssize_t wr;

(void)cmd;
(void)vl;
Expand Down Expand Up @@ -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));

Expand Down
14 changes: 6 additions & 8 deletions bin/varnishtest/vtc_http.c
Expand Up @@ -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));
}

/**********************************************************************
Expand Down Expand Up @@ -1467,7 +1465,6 @@ cmd_http_sendhex(CMD_ARGS)
{
struct vsb *vsb;
struct http *hp;
int j;

(void)cmd;
(void)vl;
Expand All @@ -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);
}

Expand Down
4 changes: 1 addition & 3 deletions bin/varnishtest/vtc_process.c
Expand Up @@ -785,16 +785,14 @@ 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");

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);
}

Expand Down
7 changes: 3 additions & 4 deletions bin/varnishtest/vtc_proxy.c
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
1 change: 1 addition & 0 deletions include/vsb.h
Expand Up @@ -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
Expand Down
14 changes: 13 additions & 1 deletion lib/libvarnish/vsb.c
Expand Up @@ -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()
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}

0 comments on commit cf14a0f

Please sign in to comment.