diff options
Diffstat (limited to 'toys/other/oneit.c')
-rw-r--r-- | toys/other/oneit.c | 33 |
1 files changed, 26 insertions, 7 deletions
diff --git a/toys/other/oneit.c b/toys/other/oneit.c index 4c8bb1f6..c0b0c09d 100644 --- a/toys/other/oneit.c +++ b/toys/other/oneit.c @@ -65,13 +65,37 @@ static void oneit_signaled(int signal) void oneit_main(void) { int i, pid, pipes[] = {SIGUSR1, SIGUSR2, SIGTERM, SIGINT}; + char *ss = toybuf+5; // Setup signal handlers for signals of interest for (i = 0; i<ARRAY_LEN(pipes); i++) xsignal(pipes[i], oneit_signaled); + // Autodetect console from sysfs if no -c + memcpy(toybuf, "/dev/", 5); + i = sizeof(toybuf)-6; + if (!TT.c && (TT.c = readfile("/sys/class/tty/console/active", ss, i))) { + // Take last entry, remove newline terminator + while (TT.c[i = strcspn(TT.c, " \n")]) { + TT.c[i++] = 0; + if (TT.c[i]) TT.c += i; + else break; + } + // Ensure exactly one /dev prefix + strstart(&TT.c, "/dev/"); + memmove(toybuf+5, TT.c, strlen(TT.c)); + TT.c = toybuf; + } + + // Redirect stdin/out/err. Remember, O_CLOEXEC is backwards for xopen() + close(0); + xopen_stdio(TT.c ? : "/dev/tty0", O_RDWR|O_CLOEXEC); + close(1); + dup(0); + close(2); + dup(0); + if (FLAG(3)) { // Ensure next available filehandles are #3 and #4 - while (xopen_stdio("/", 0) < 3); close(3); close(4); xpipe(pipes); @@ -93,13 +117,8 @@ void oneit_main(void) oneit_signaled(FLAG(p) ? SIGUSR2 : SIGTERM); } else { - // Redirect stdio to TT.c, with new session ID, so ctrl-c works. + // new session ID in child, so ctrl-c works. setsid(); - for (i=0; i<3; i++) { - close(i); - // Remember, O_CLOEXEC is backwards for xopen() - xopen_stdio(TT.c ? : "/dev/tty0", O_RDWR|O_CLOEXEC); - } // Can't xexec() here, we vforked so we don't want to error_exit(). toy_exec(toys.optargs); |