diff options
author | Rob Landley <rob@landley.net> | 2019-12-26 16:45:58 -0600 |
---|---|---|
committer | Rob Landley <rob@landley.net> | 2019-12-26 16:45:58 -0600 |
commit | 4817036313bde019e4639bf52e7a6c52ca715c61 (patch) | |
tree | 491388acce8b337368aec1e2766984ca62680294 /toys/other | |
parent | 908584d5e7f59f273dbfb46d96abddc0b02432b7 (diff) | |
download | toybox-4817036313bde019e4639bf52e7a6c52ca715c61.tar.gz |
Fix setsid with vfork, redo command line arguments.
Switch -t to -c (like man page says), add -w (wait) and -d (detach from tty)
Diffstat (limited to 'toys/other')
-rw-r--r-- | toys/other/setsid.c | 36 |
1 files changed, 29 insertions, 7 deletions
diff --git a/toys/other/setsid.c b/toys/other/setsid.c index 95698260..57a926ef 100644 --- a/toys/other/setsid.c +++ b/toys/other/setsid.c @@ -2,27 +2,49 @@ * * Copyright 2006 Rob Landley <rob@landley.net> -USE_SETSID(NEWTOY(setsid, "^<1t", TOYFLAG_USR|TOYFLAG_BIN)) +USE_SETSID(NEWTOY(setsid, "^<1wcd[!dc]", TOYFLAG_USR|TOYFLAG_BIN)) config SETSID bool "setsid" default y help - usage: setsid [-t] command [args...] + usage: setsid [-cdw] command [args...] Run process in a new session. - -t Grab tty (become foreground process, receiving keyboard signals) + -d Detach from tty + -c Control tty (become foreground process & receive keyboard signals) */ +#define FOR_setsid #include "toys.h" void setsid_main(void) { - while (setsid()<0) if (XVFORK()) _exit(0); - if (toys.optflags) { - setpgid(0, 0); - tcsetpgrp(0, getpid()); + int i; + + // This must be before vfork() or tcsetpgrp() will hang waiting for parent. + setpgid(0, 0); + + // setsid() fails if we're already session leader, ala "exec setsid" from sh. + // Second call can't fail, so loop won't continue endlessly. + while (setsid()<0) { + pid_t pid = XVFORK(); + + if (pid) { + i = 0; + if (FLAG(w)) { + i = 127; + if (pid>0) i = xwaitpid(pid); + } + _exit(i); + } + } + + if (FLAG(c)) tcsetpgrp(0, getpid()); + if (FLAG(d) && (i = open("/dev/tty", O_RDONLY)) != -1) { + ioctl(i, TIOCNOTTY); + close(i); } xexec(toys.optargs); } |