summaryrefslogtreecommitdiffstats
path: root/sbin/init/init.c
diff options
context:
space:
mode:
Diffstat (limited to 'sbin/init/init.c')
-rw-r--r--sbin/init/init.c38
1 files changed, 27 insertions, 11 deletions
diff --git a/sbin/init/init.c b/sbin/init/init.c
index 8ee7a8d..a24371c 100644
--- a/sbin/init/init.c
+++ b/sbin/init/init.c
@@ -138,7 +138,7 @@ static void transition(state_t);
static state_t requested_transition;
static state_t current_state = death_single;
-static void setctty(const char *);
+static void open_console(void);
static const char *get_shell(void);
static void write_stderr(const char *message);
@@ -568,19 +568,35 @@ transition(state_t s)
* Only called by children of init after forking.
*/
static void
-setctty(const char *name)
+open_console(void)
{
int fd;
- revoke(name);
- if ((fd = open(name, O_RDWR)) == -1) {
- stall("can't open %s: %m", name);
- _exit(1);
+ /* Try to open /dev/console. */
+ revoke(_PATH_CONSOLE);
+ if ((fd = open(_PATH_CONSOLE, O_RDWR | O_NONBLOCK)) != -1) {
+ if (login_tty(fd) == 0)
+ return;
+ close(fd);
}
- if (login_tty(fd) == -1) {
- stall("can't get %s for controlling terminal: %m", name);
+
+ /* No luck. Log output to file if possible. */
+ if ((fd = open(_PATH_DEVNULL, O_RDWR)) == -1) {
+ stall("cannot open null device.");
_exit(1);
}
+ if (fd != STDIN_FILENO) {
+ dup2(fd, STDIN_FILENO);
+ close(fd);
+ }
+ fd = open(_PATH_INITLOG, O_WRONLY | O_APPEND | O_CREAT, 0644);
+ if (fd == -1)
+ dup2(STDIN_FILENO, STDOUT_FILENO);
+ else if (fd != STDOUT_FILENO) {
+ dup2(fd, STDOUT_FILENO);
+ close(fd);
+ }
+ dup2(STDOUT_FILENO, STDERR_FILENO);
}
static const char *
@@ -638,7 +654,7 @@ single_user(void)
/*
* Start the single user session.
*/
- setctty(_PATH_CONSOLE);
+ open_console();
#ifdef SECURE
/*
@@ -798,7 +814,7 @@ run_script(const char *script)
sigaction(SIGTSTP, &sa, (struct sigaction *)0);
sigaction(SIGHUP, &sa, (struct sigaction *)0);
- setctty(_PATH_CONSOLE);
+ open_console();
char _sh[] = "sh";
char _autoboot[] = "autoboot";
@@ -1572,7 +1588,7 @@ runshutdown(void)
sigaction(SIGTSTP, &sa, (struct sigaction *)0);
sigaction(SIGHUP, &sa, (struct sigaction *)0);
- setctty(_PATH_CONSOLE);
+ open_console();
char _sh[] = "sh";
char _reboot[] = "reboot";
OpenPOWER on IntegriCloud