]> git.itanic.dy.fi Git - rrdd/commitdiff
process.c: Ensure stderr gets read after process has died
authorTimo Kokkonen <kaapeli@itanic.dy.fi>
Fri, 11 Mar 2011 20:07:56 +0000 (22:07 +0200)
committerTimo Kokkonen <kaapeli@itanic.dy.fi>
Fri, 11 Mar 2011 21:26:11 +0000 (23:26 +0200)
For some reason select() doesn't indicate that stderr might contain
bytes that can be read. Workaround for the issue is to read stderr
just in case after we receive EOF from stdin.

Signed-off-by: Timo Kokkonen <kaapeli@itanic.dy.fi>
process.c

index 59489c82b30eded816dc769fdbe262d3c8c6cf83..9e2814a2109deaa1831fbabfa66915a05c38a689 100644 (file)
--- a/process.c
+++ b/process.c
@@ -219,6 +219,7 @@ int run(const char *cmd, char *const argv[])
        int ofd, efd;
        fd_set rfds;
        int maxfd;
+       int eof = 0;
        char stdoutstr[32], stderrstr[32], indent[16] = { "                " };
 
        indent[get_parent_count() + 1] = 0;
@@ -234,7 +235,7 @@ int run(const char *cmd, char *const argv[])
        FD_SET(ofd, &rfds);
        FD_SET(efd, &rfds);
 
-       while (1) {
+       while (!eof) {
                char *sptr , *eptr;
                char rbuf[BUF_SIZE];
                int bytes;
@@ -264,8 +265,19 @@ print:
                        pr_err("read() failed: %s\n", strerror(error));
                        break;
                }
-               if (bytes == 0)
-                       break;
+
+               /*
+                * Workaround: When a process had die and it has only
+                * written to stderr, select() doesn't indicate that
+                * there might be something to read in stderr fd. To
+                * work around this issue, we try to read stderr just
+                * in case in order to ensure everything gets read.
+                */
+               if (bytes == 0) {
+                       bytes = read(efd, rbuf, BUF_SIZE);
+                       typestr = stderrstr;
+                       eof = 1;
+               }
 
                sptr = eptr = rbuf;
                while(bytes--) {