From 40cf0121601f69a4f5c491ab1e2fd54bd7fe4b75 Mon Sep 17 00:00:00 2001 From: Marius Halden Date: Fri, 4 Nov 2016 14:02:37 +0100 Subject: Initial for executing filedescriptor --- .gitignore | 1 + Makefile | 21 +++++++++++++++++++++ batchd.c | 2 +- newbatch.c | 13 ++++++++++++- runfd.c | 15 +++++++++++++++ 5 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 Makefile create mode 100644 runfd.c diff --git a/.gitignore b/.gitignore index 113002d..d48beeb 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ done new tmp failed +runfd diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..ffb3f6b --- /dev/null +++ b/Makefile @@ -0,0 +1,21 @@ +CC = cc +CFLAGS = -O2 -Wall -Wextra -Werror -pedantic -std=c99 + +all: newbatch createspool batchd + +newbatch: newbatch.c + $(CC) -o newbatch $(CFLAGS) newbatch.c + +createspool: createspool.c + $(CC) -o createspool $(CFLAGS) createspool.c + +batchd: batchd.c + $(CC) -o batchd $(CFLAGS) batchd.c + +runfd: runfd.c + $(CC) -o runfd $(CFLAGS) runfd.c + +.PHONY: clean + +clean: + rm -f newbatch createspool batchd diff --git a/batchd.c b/batchd.c index c2cadc8..75930b7 100644 --- a/batchd.c +++ b/batchd.c @@ -33,7 +33,7 @@ run_job(char *queuedir, int fd) return -1; case 0: asprintf(&run, "%s/run", queuedir); - dup2(fd, STDIN_FILENO); + dup2(fd, 3); close(fd); execl(run, "run", (char*)NULL); perror("execle()"); diff --git a/newbatch.c b/newbatch.c index 6b27234..0bac7ee 100644 --- a/newbatch.c +++ b/newbatch.c @@ -18,6 +18,7 @@ main(int argc, char **argv) char *timestr = NULL, *queuedir = NULL, *tmpfile = NULL, *newfile = NULL; char buf[1024], *tmp; int fd, l, m; + int perm = 0640; mypid = getpid(); gettimeofday(&tv, NULL); @@ -35,12 +36,19 @@ main(int argc, char **argv) free(timestr); free(queuedir); - fd = open(tmpfile, O_WRONLY | O_CREAT | O_EXCL, 0660); + if (getenv("NEWBATCH_EXEC") != NULL) { + perm = 0750; + } + + fd = open(tmpfile, O_WRONLY | O_CREAT | O_EXCL, perm); if (fd == -1) err(1, "open()"); while ((l = read(STDIN_FILENO, buf, sizeof(buf))) != 0) { if (l == -1) { + if (errno == EINTR) + continue; + unlink(tmpfile); err(1, NULL); } @@ -49,6 +57,9 @@ main(int argc, char **argv) while (l > 0) { m = write(fd, tmp, l); if (m == -1) { + if (errno == EINTR) + continue; + unlink(tmpfile); err(1, NULL); } diff --git a/runfd.c b/runfd.c new file mode 100644 index 0000000..6a92bb6 --- /dev/null +++ b/runfd.c @@ -0,0 +1,15 @@ +#include +#include +#include +#include + +char *args[] = { "batchrun", NULL }; + +int +main() +{ + //fcntl(3, F_SETFD, FD_CLOEXEC); + + fexecve(3, args, NULL); + err(1, "fexecve()"); +} -- cgit v1.2.3