diff options
author | Sergey Poznyakoff <gray@gnu.org> | 2019-08-16 11:33:07 +0300 |
---|---|---|
committer | Sergey Poznyakoff <gray@gnu.org> | 2019-08-16 11:33:07 +0300 |
commit | b40125990e923f743fa4a1b53c7af05feda422cc (patch) | |
tree | 1c56eed51640589503298846fcb1c6110dff09a7 /runcap.c | |
parent | 5b7e047dfdb34d0fdc3720e14dd08a7693948b2f (diff) | |
download | runcap-b40125990e923f743fa4a1b53c7af05feda422cc.tar.gz runcap-b40125990e923f743fa4a1b53c7af05feda422cc.tar.bz2 |
Run line monitor functions with stream capturing disabled
* runcap.3: Document changes.
* runcap.c (stream_capture_init): Change signature. Change to static.
(stream_capture_free): Change to static.
(stream_capture_get): Special handling for "no capture" streams with
line monitors.
* runcap.h (stream_capture): New member: sc_nocap.
(RCF_STDOUT_NOCAP)
(RCF_STDERR_NOCAP): New flags.
* t/Makefile.am: Add new tests.
* t/testsuite.at: Likewise.
* t/linemon02.at: Add copyright header.
* t/linemon03.at: New test.
* t/nocap.inc: New include file.
* t/nocap00.at: New test.
* t/nocap01.at: New test.
* t/rt.c: New option: -N
Diffstat (limited to 'runcap.c')
-rw-r--r-- | runcap.c | 78 |
1 files changed, 48 insertions, 30 deletions
@@ -23,17 +23,25 @@ #include <fcntl.h> #include <errno.h> #include <signal.h> - +#include <string.h> #include "runcap.h" -int -stream_capture_init(struct stream_capture *cap, size_t size) +static int +stream_capture_init(struct stream_capture *cap, size_t size, int linemon, + int nocap) { if (!cap) { errno = EINVAL; return -1; } + if (size == 0) { + if (linemon) + size = STRCAP_BUFSIZE; + nocap = 1; + } else if (nocap && !linemon) + size = 0; + if (size) { cap->sc_base = malloc(size); if (!cap->sc_base) @@ -47,6 +55,13 @@ stream_capture_init(struct stream_capture *cap, size_t size) cap->sc_cur = 0; cap->sc_storfd = -1; cap->sc_fd = -1; + + if (!linemon) { + cap->sc_linemon = NULL; + cap->sc_monarg = NULL; + } + cap->sc_nocap = nocap; + return 0; } @@ -68,7 +83,7 @@ stream_capture_reset(struct stream_capture *cap) } } -void +static void stream_capture_free(struct stream_capture *cap) { stream_capture_reset(cap); @@ -108,18 +123,20 @@ stream_capture_flush(struct stream_capture *cap) cap->sc_linemon(cap->sc_base + cap->sc_cur, cap->sc_level - cap->sc_cur, cap->sc_monarg); - if (cap->sc_storfd == -1) { - int fd; - char tmpl[] = "/tmp/rcXXXXXX"; - fd = mkstemp(tmpl); - if (fd == -1) + if (!cap->sc_nocap) { + if (cap->sc_storfd == -1) { + int fd; + char tmpl[] = "/tmp/rcXXXXXX"; + fd = mkstemp(tmpl); + if (fd == -1) + return -1; + unlink(tmpl); + cap->sc_storfd = fd; + } + res = full_write(cap->sc_storfd, cap->sc_base, cap->sc_level); + if (res) return -1; - unlink(tmpl); - cap->sc_storfd = fd; } - res = full_write(cap->sc_storfd, cap->sc_base, cap->sc_level); - if (res) - return -1; cap->sc_level = 0; cap->sc_cur = 0; return 0; @@ -136,7 +153,8 @@ stream_capture_get(struct stream_capture *cap, int *feof) return -1; } - rc = read(cap->sc_fd, cap->sc_base + cap->sc_level, cap->sc_size - cap->sc_level); + rc = read(cap->sc_fd, cap->sc_base + cap->sc_level, + cap->sc_size - cap->sc_level); if (rc == -1) { if (errno == EINTR) return 0; @@ -170,7 +188,13 @@ stream_capture_get(struct stream_capture *cap, int *feof) cap->sc_nlines++; } } - + + if (cap->sc_nocap && cap->sc_linemon && cap->sc_level > cap->sc_cur) { + memmove(cap->sc_base, cap->sc_base + cap->sc_cur, + cap->sc_level - cap->sc_cur); + cap->sc_level -= cap->sc_cur; + cap->sc_cur = 0; + } return 0; } @@ -451,33 +475,27 @@ runcap_init(struct runcap *rc, int flags) rc->rc_cap[RUNCAP_STDIN].sc_size; rc->rc_cap[RUNCAP_STDIN].sc_cur = 0; rc->rc_cap[RUNCAP_STDIN].sc_storfd = -1; - } else if (stream_capture_init(&rc->rc_cap[RUNCAP_STDIN], 0)) + } else if (stream_capture_init(&rc->rc_cap[RUNCAP_STDIN], 0, 0, 0)) return -1; - + res = stream_capture_init(&rc->rc_cap[RUNCAP_STDOUT], (flags & RCF_STDOUT_SIZE) ? rc->rc_cap[RUNCAP_STDOUT].sc_size - : STRCAP_BUFSIZE); + : STRCAP_BUFSIZE, + flags & RCF_STDOUT_LINEMON, + flags & RCF_STDOUT_NOCAP); if (res) return res; - if (!(flags & RCF_STDOUT_LINEMON)) { - rc->rc_cap[RUNCAP_STDOUT].sc_linemon = NULL; - rc->rc_cap[RUNCAP_STDOUT].sc_monarg = NULL; - } - res = stream_capture_init(&rc->rc_cap[RUNCAP_STDERR], (flags & RCF_STDERR_SIZE) ? rc->rc_cap[RUNCAP_STDERR].sc_size - : STRCAP_BUFSIZE); + : STRCAP_BUFSIZE, + flags & RCF_STDERR_LINEMON, + flags & RCF_STDERR_NOCAP); if (res) return res; - if (!(flags & RCF_STDERR_LINEMON)) { - rc->rc_cap[RUNCAP_STDERR].sc_linemon = NULL; - rc->rc_cap[RUNCAP_STDERR].sc_monarg = NULL; - } - rc->rc_pid = (pid_t) -1; rc->rc_status = 0; rc->rc_errno = 0; |