summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMarius Halden <marius.h@lden.org>2016-11-04 14:54:48 +0100
committerMarius Halden <marius.h@lden.org>2016-11-04 14:54:48 +0100
commit8f0ad1035b76b61bd3946b551aff5065cb5bdb6c (patch)
treef38760eb79a1d0fdbc9337f966bf11c5926bc30e
parent48e4273e1a0fd7f0480f92350932ec457dbcfe13 (diff)
downloadrunq-8f0ad1035b76b61bd3946b551aff5065cb5bdb6c.tar.gz
runq-8f0ad1035b76b61bd3946b551aff5065cb5bdb6c.tar.bz2
runq-8f0ad1035b76b61bd3946b551aff5065cb5bdb6c.tar.xz
Make pread async in case fd is valid but not what we are looking for
-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()");
}