Commit 1b429348 authored by Poul-Henning Kamp's avatar Poul-Henning Kamp

Renovate the VLU API (Inspired by #2481)

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