aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Timber <dxdt@dev.snart.me>2024-11-12 20:34:02 +0100
committerDavid Timber <dxdt@dev.snart.me>2024-11-12 20:46:09 +0100
commit77b23de79e3a29ab37d92f4a9852a5ad4c33091e (patch)
treecfb3eb16b647ae649b30fb88c39bc61e0c38666b
parenta7df9d245fe0bbc456d564a523911cae3e3e85c0 (diff)
qna/subproc.c: channel IO in nonblocking mode
-rw-r--r--qna/subproc.c26
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;
}
}