Commit 87fbff31 authored by Poul-Henning Kamp's avatar Poul-Henning Kamp Committed by Reza Naghibi

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

Conflicts:
    bin/varnishd/common/common_vsmw.c
parent 84fe7970
......@@ -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;
......
......@@ -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)
......
......@@ -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;
......
......@@ -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));
......
......@@ -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);
}
......
......@@ -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);
}
......
......@@ -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);
}
......@@ -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
......
......@@ -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);
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment