diff options
author | Marius Halden <marius.h@lden.org> | 2016-08-19 16:00:54 +0200 |
---|---|---|
committer | Marius Halden <marius.h@lden.org> | 2016-08-19 16:00:54 +0200 |
commit | b6125c9eac61eca81eae42fb63a26ce16a8dc8db (patch) | |
tree | 7633e9b588bc60b1e29a4b23b7f049cdfec0856b /batchd.c | |
download | runq-b6125c9eac61eca81eae42fb63a26ce16a8dc8db.tar.gz runq-b6125c9eac61eca81eae42fb63a26ce16a8dc8db.tar.bz2 runq-b6125c9eac61eca81eae42fb63a26ce16a8dc8db.tar.xz |
Initial commit
Diffstat (limited to 'batchd.c')
-rw-r--r-- | batchd.c | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/batchd.c b/batchd.c new file mode 100644 index 0000000..f3480ba --- /dev/null +++ b/batchd.c @@ -0,0 +1,110 @@ +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <sys/types.h> +#include <sys/event.h> +#include <sys/time.h> +#include <errno.h> +#include <err.h> +#include <dirent.h> +#include <string.h> +#include <fcntl.h> +#include <sys/wait.h> + +#define DEFAULT_QUEUE_DIR "." + +void +run_job(char *queuedir, int fd) +{ + char *run; + switch(fork()) { + case -1: + return; + case 0: + asprintf(&run, "%s/run", queuedir); + dup2(fd, STDIN_FILENO); + close(fd); + execl(run, "run", (char*)NULL); + perror("execle()"); + _exit(1); + default: + wait(NULL); + } +} + +void +process_queue(char *queuedir, int dfd) +{ + int fd; + DIR *dir; + struct dirent *de; + char *name; + + dir = fdopendir(dfd); + while ((de = readdir(dir))) { + if (strcmp(de->d_name, ".") == 0 || strcmp(de->d_name, "..") == 0) + continue; + + asprintf(&name, "new/%s", de->d_name); + + fd = open(name, O_RDONLY); + if (fd == -1) { + free(name); + continue; + } + unlink(name); + run_job(queuedir, fd); + close(fd); + free(name); + } + + rewinddir(dir); + fdclosedir(dir); +} + +int +main(int argc, char **argv) +{ + int kq, dfd, ret; + struct kevent kv; + char *queuedir = NULL, *newdir = NULL; + + if (argc > 1) { + queuedir = strdup(argv[1]); + } else { + queuedir = strdup(DEFAULT_QUEUE_DIR); + } + + asprintf(&newdir, "%s/new", queuedir); + + dfd = open(newdir, O_RDONLY | O_DIRECTORY); + if (dfd == -1) + err(1, "open()"); + + kq = kqueue(); + if (kq == -1) + err(1, "kqueue()"); + + EV_SET(&kv, dfd, EVFILT_VNODE, EV_ADD | EV_CLEAR, NOTE_WRITE, 0, NULL); + + if (kevent(kq, &kv, 1, NULL, 0, NULL) == -1) + err(1, "kevent()"); + + process_queue(queuedir, dfd); + + for (;;) { + ret = kevent(kq, NULL, 0, &kv, 1, NULL); + if (ret == 0) + continue; + else if (ret == -1) { + if (errno == EINTR) + continue; + else + break; + } + + process_queue(queuedir, dfd); + } + + return 0; +} |