From 85b43c98cef27668208874a91a3a9fc2a22d57c8 Mon Sep 17 00:00:00 2001 From: Marius Halden Date: Thu, 10 Nov 2016 19:58:56 +0100 Subject: Fix batchd subdir and more --- batchd.c | 16 ++++++++-------- runfd.c | 26 +++++++++++++++++++++----- 2 files changed, 29 insertions(+), 13 deletions(-) diff --git a/batchd.c b/batchd.c index 75930b7..e287932 100644 --- a/batchd.c +++ b/batchd.c @@ -61,9 +61,9 @@ process_queue(char *queuedir, int dfd) if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) continue; - asprintf(&name, "new/%s", de->d_name); - asprintf(&work, "work/%s", de->d_name); - asprintf(&failed, "failed/%s", de->d_name); + asprintf(&name, "%s/new/%s", queuedir, de->d_name); + asprintf(&work, "%s/work/%s", queuedir, de->d_name); + asprintf(&failed, "%s/failed/%s", queuedir, de->d_name); fd = open(name, O_RDONLY); if (fd == -1) { @@ -98,7 +98,7 @@ next: } void -wait_all() +wait_all(char *queudir) { int i, status; char *work, *done, *failed; @@ -116,9 +116,9 @@ wait_all() for (i = 0; i < MAX_JOBS; i++) { if (jobs[i].pid == r) { - asprintf(&work, "work/%s", jobs[i].file); - asprintf(&done, "done/%s", jobs[i].file); - asprintf(&failed, "failed/%s", jobs[i].file); + asprintf(&work, "%s/work/%s", queudir, jobs[i].file); + asprintf(&done, "%s/done/%s", queudir, jobs[i].file); + asprintf(&failed, "%s/failed/%s", queudir, jobs[i].file); if (WEXITSTATUS(status) == 0) rename(work, done); @@ -192,7 +192,7 @@ main(int argc, char **argv) for (i = 0; i < ret; i++) { if (kv[i].filter == EVFILT_SIGNAL) { if (kv[i].ident == SIGCHLD) { - wait_all(); + wait_all(queuedir); process_queue(queuedir, dfd); } } else if (kv[i].filter == EVFILT_VNODE) { diff --git a/runfd.c b/runfd.c index 59b9d8c..bebe0ec 100644 --- a/runfd.c +++ b/runfd.c @@ -11,6 +11,7 @@ */ #define DEFAULT_FD 3 +#define BIN_HDR_LEN 2 char *args[] = { "batchrun", NULL }; @@ -32,10 +33,17 @@ set_async(int fd, int on) err(1, "fcntl()"); } +void +set_cloexec(int fd, int on) +{ + if (fcntl(fd, F_SETFD, on ? FD_CLOEXEC : 0) == -1) + err(1, "fcntl()"); +} + int main(int argc, char **argv) { - char buf[2], *end = NULL; + char buf[BIN_HDR_LEN], *end = NULL; ssize_t ret; int tries = 0, fd = DEFAULT_FD; @@ -46,6 +54,7 @@ main(int argc, char **argv) errx(1, "Filedescriptor supplied (\"%s\") is not a number", argv[1]); } + set_cloexec(fd, 1); set_async(fd, 1); retry: @@ -53,9 +62,9 @@ retry: errx(1, "Failed to read file header"); ret = pread(fd, buf, sizeof(buf), 0); - if (ret == 2) { - if (strncmp(buf, "#!", 2) != 0) { - fcntl(fd, F_SETFD, FD_CLOEXEC); + if (ret == sizeof(buf)) { + if (strncmp(buf, "#!", sizeof(buf)) == 0) { + set_cloexec(fd, 0); } } else if (ret == -1) { if (errno == EINTR) @@ -63,7 +72,14 @@ retry: else if (errno != EAGAIN) err(1, "pread()"); } else if (ret != 0) { - goto retry; + /** + * We take a lazy approach here and only want to know if the + * file has more contents (and don't care about errors). + */ + ret = pread(fd, buf + ret, sizeof(buf) - ret, ret); + if (ret != 0) { + goto retry; + } } set_async(fd, 0); -- cgit v1.2.3