diff --git a/bin/sh/main.c b/bin/sh/main.c index 94e8da6b492..0ca2d494242 100644 --- a/bin/sh/main.c +++ b/bin/sh/main.c @@ -39,6 +39,9 @@ #include #include #include +#include +#include +#include #include "shell.h" #include "main.h" @@ -124,6 +127,22 @@ main(int argc, char *argv[]) trputs("Shell args: "); trargs(argv); #endif rootpid = getpid(); + if (rootpid == 1) { + /* + * Make sh usable for invocation as interactive init + * substitute with init_path=/bin/sh, by opening + * file descriptors 0, 1, and 2 on /dev/console. + */ + if (fcntl(STDIN_FILENO, F_GETFL, NULL) == -1 && errno == EBADF) { + (void)open(_PATH_CONSOLE, O_RDWR); + (void)setsid(); + (void)tcsetsid(STDIN_FILENO, rootpid); + } + if (fcntl(STDOUT_FILENO, F_GETFL, NULL) == -1 && errno == EBADF) + (void)dup2(STDIN_FILENO, STDOUT_FILENO); + if (fcntl(STDERR_FILENO, F_GETFL, NULL) == -1 && errno == EBADF) + (void)dup2(STDIN_FILENO, STDERR_FILENO); + } rootshell = 1; INTOFF; initvar();