diff options
Diffstat (limited to 'src/sentinel.c')
-rw-r--r-- | src/sentinel.c | 42 |
1 files changed, 13 insertions, 29 deletions
diff --git a/src/sentinel.c b/src/sentinel.c index 4f269b8..989acf1 100644 --- a/src/sentinel.c +++ b/src/sentinel.c @@ -1,12 +1,10 @@ /* This file is part of genrc -Copyryght (C) 2018 Sergey Poznyakoff +Copyryght (C) 2018, 2019 Sergey Poznyakoff License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. */ #include "genrc.h" -#include <unistd.h> -#include <fcntl.h> #include <syslog.h> #include <time.h> @@ -19,7 +17,11 @@ xpipe(int p[2]) } } -#define max(a,b) ((a) > (b) ? (a) : (b)) +static inline int +max(int a, int b) +{ + return a > b ? a : b; +} static void write_pid_file(pid_t pid) @@ -234,8 +236,8 @@ pid_t start_command(int p[]) { int errpipe[2], outpipe[2]; - pid_t pid; - + pid_t pid; + xpipe(errpipe); xpipe(outpipe); pid = fork(); @@ -245,31 +247,13 @@ start_command(int p[]) } if (pid == 0) { - char *argv[] = { SHELL, "-c", NULL, NULL }; - int i; - - runas(); - - close(0); - close(1); - close(2); - open("/dev/null", O_RDWR); - dup(outpipe[1]); - dup(errpipe[1]); - - i = max(max(outpipe[0],outpipe[1]), - max(errpipe[0],errpipe[1])); - while (--i > 2) - close(i); - - argv[2] = genrc_command; - execvp(SHELL, argv); - system_error(errno, "failed to exec %s", genrc_program); - exit(127); + p[0] = outpipe[1]; + p[1] = errpipe[1]; + spawn(p); } write_pid_file(pid); - + close(outpipe[1]); close(errpipe[1]); @@ -349,7 +333,7 @@ sentinel(void) int p[2]; struct ratectl ctl; struct sigaction act; - + /* Detach from the controlling terminal */ pid = fork(); if (pid == -1) { |