diff options
-rw-r--r-- | qna/subproc.c | 26 |
1 files changed, 25 insertions, 1 deletions
diff --git a/qna/subproc.c b/qna/subproc.c index c040810..91f2616 100644 --- a/qna/subproc.c +++ b/qna/subproc.c @@ -7,6 +7,7 @@ #include <signal.h> #include <unistd.h> +#include <fcntl.h> #include <wait.h> #include <poll.h> @@ -29,6 +30,24 @@ struct pbuf { int fd; }; +static bool setnonblock (const int fd, const bool set) { + int fr; + + fr = fcntl(fd, F_GETFL, 0); + if (fr < 0) { + return false; + } + + if (set) { + fr |= O_NONBLOCK; + } + else { + fr &= ~O_NONBLOCK; + } + + return fcntl(fd, F_SETFL, fr) >= 0; +} + static size_t do_output (const int fd, char *buf, size_t len) { char *end; size_t outlen; @@ -87,12 +106,17 @@ int main (const int argc, const char **argv) { close(p.in[0]); close(p.out[1]); close(p.err[1]); + sp->std.in = p.in[1]; pfds[j].fd = sp->std.out = p.out[0]; pfds[j + 1].fd = sp->std.err = p.err[0]; pfds[j].events = pfds[j + 1].events = POLLIN; pbufs[j].fd = STDOUT_FILENO; pbufs[j + 1].fd = STDERR_FILENO; + + setnonblock(sp->std.in, true); + setnonblock(sp->std.out, true); + setnonblock(sp->std.err, true); } else if (sp->pid == 0) { const char *new_argv[] = { "find", path, NULL }; @@ -180,7 +204,7 @@ int main (const int argc, const char **argv) { } while (wait(&s) >= 0) { - if (WIFEXITED(s) && WEXITSTATUS(s) != 0) { + if (WIFSIGNALED(s) || WEXITSTATUS(s) != 0) { ec = 1; } } |