From 19590f4157aefc706db79356e3134a34d6e2c8b0 Mon Sep 17 00:00:00 2001 From: Laurent Bercot Date: Wed, 4 Mar 2015 12:57:14 +0000 Subject: Add process group management functionality to s6-setsid --- src/daemontools-extras/s6-setsid.c | 64 +++++++++++++++++++++++++++++--------- 1 file changed, 50 insertions(+), 14 deletions(-) (limited to 'src/daemontools-extras') diff --git a/src/daemontools-extras/s6-setsid.c b/src/daemontools-extras/s6-setsid.c index efd3832..51de048 100644 --- a/src/daemontools-extras/s6-setsid.c +++ b/src/daemontools-extras/s6-setsid.c @@ -1,35 +1,71 @@ /* ISC license. */ #include +#include +#include #include #include +#include #include -#define USAGE "s6-setsid [ -i | -I ] prog..." +#define USAGE "s6-setsid [ -s | -b | -f | -g ] [ -i | -I | -q ] [ -d ctty ] prog..." +#define dieusage() strerr_dieusage(100, USAGE) int main (int argc, char const *const *argv, char const *const *envp) { - int insist = 0 ; + unsigned int ctty = 0, what = 0, insist = 1 ; PROG = "s6-setsid" ; - for (;;) { - register int opt = subgetopt(argc, argv, "iI") ; - if (opt == -1) break ; - switch (opt) + subgetopt_t l = SUBGETOPT_ZERO ; + for (;;) { - case 'i' : insist = 1 ; break ; - case 'I' : insist = 0 ; break ; - default : strerr_dieusage(100, USAGE) ; + register int opt = subgetopt_r(argc, argv, "sbfgiIqt:", &l) ; + if (opt == -1) break ; + switch (opt) + { + case 's' : what = 0 ; break ; + case 'b' : what = 1 ; break ; + case 'f' : what = 2 ; break ; + case 'g' : what = 3 ; break ; + case 'i' : insist = 2 ; break ; + case 'I' : insist = 1 ; break ; + case 'q' : insist = 0 ; break ; + case 't' : if (!uint0_scan(l.arg, &ctty)) dieusage() ; break ; + default : dieusage() ; + } } + argc -= l.ind ; argv += l.ind ; } - argc -= subgetopt_here.ind ; argv += subgetopt_here.ind ; - if (!argc) strerr_dieusage(100, USAGE) ; + if (!argc) dieusage() ; - if (setsid() < 0) + if (what) { - if (insist) strerr_diefu1sys(111, "setsid") ; - else strerr_warnwu1sys("setsid") ; + if (setpgid(0, 0) < 0) switch (insist) + { + case 2 : strerr_diefu1sys(111, "setpgid") ; + case 1 : strerr_warnwu1sys("setpgid") ; break ; + default : break ; + } + + if (what >= 2) + { + if (what == 3) sig_ignore(SIGTTOU) ; + if (tcsetpgrp(ctty, getpid()) < 0) switch (insist) + { + case 2 : strerr_diefu1sys(111, "tcsetpgrp") ; + case 1 : strerr_warnwu1sys("tcsetpgrp") ; break ; + default : break ; + } + if (what == 3) sig_restore(SIGTTOU) ; + } + } + else if (setsid() < 0) switch (insist) + { + case 2 : strerr_diefu1sys(111, "setsid") ; + case 1 : strerr_warnwu1sys("setsid") ; break ; + default : break ; } + pathexec_run(argv[0], argv, envp) ; strerr_dieexec(111, argv[0]) ; } -- cgit v1.2.3