summaryrefslogtreecommitdiffstats
path: root/runfd.c
diff options
context:
space:
mode:
Diffstat (limited to 'runfd.c')
-rw-r--r--runfd.c27
1 files changed, 25 insertions, 2 deletions
diff --git a/runfd.c b/runfd.c
index 50eea7f..59b9d8c 100644
--- a/runfd.c
+++ b/runfd.c
@@ -14,6 +14,24 @@
char *args[] = { "batchrun", NULL };
+void
+set_async(int fd, int on)
+{
+ int flags;
+
+ if ((flags = fcntl(fd, F_GETFL)) == -1)
+ err(1, "fcntl()");
+
+ if (on) {
+ flags |= O_NONBLOCK;
+ } else {
+ flags &= ~O_NONBLOCK;
+ }
+
+ if (fcntl(fd, F_SETFL, flags) == -1)
+ err(1, "fcntl()");
+}
+
int
main(int argc, char **argv)
{
@@ -28,6 +46,8 @@ main(int argc, char **argv)
errx(1, "Filedescriptor supplied (\"%s\") is not a number", argv[1]);
}
+ set_async(fd, 1);
+
retry:
if (tries++ >= 3)
errx(1, "Failed to read file header");
@@ -35,16 +55,19 @@ retry:
ret = pread(fd, buf, sizeof(buf), 0);
if (ret == 2) {
if (strncmp(buf, "#!", 2) != 0) {
- fcntl(3, F_SETFD, FD_CLOEXEC);
+ fcntl(fd, F_SETFD, FD_CLOEXEC);
}
} else if (ret == -1) {
if (errno == EINTR)
goto retry;
- err(1, "pread()");
+ else if (errno != EAGAIN)
+ err(1, "pread()");
} else if (ret != 0) {
goto retry;
}
+ set_async(fd, 0);
+
fexecve(fd, args, NULL);
err(1, "fexecve()");
}