From 56d96fc00cd009e92e611c11f15c1bfb1b1eb9e8 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Fri, 15 Aug 2014 18:01:52 +0200 Subject: [PATCH] main: minor code modernization for initializing the console --- src/core/main.c | 38 ++++++++++++++++++++------------------ src/shared/util.c | 8 +++++--- 2 files changed, 25 insertions(+), 21 deletions(-) diff --git a/src/core/main.c b/src/core/main.c index f33b78d37..e15355dcd 100644 --- a/src/core/main.c +++ b/src/core/main.c @@ -228,31 +228,25 @@ static void install_crash_handler(void) { sigaction_many(&sa, SIGNALS_CRASH_HANDLER, -1); } -static int console_setup(bool do_reset) { - int tty_fd, r; - - /* If we are init, we connect stdin/stdout/stderr to /dev/null - * and make sure we don't have a controlling tty. */ - - release_terminal(); - - if (!do_reset) - return 0; +static int console_setup(void) { + _cleanup_close_ int tty_fd = -1; + int r; tty_fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC); if (tty_fd < 0) { log_error("Failed to open /dev/console: %s", strerror(-tty_fd)); - return -tty_fd; + return tty_fd; } - /* We don't want to force text mode. - * plymouth may be showing pictures already from initrd. */ + /* We don't want to force text mode. plymouth may be showing + * pictures already from initrd. */ r = reset_terminal_fd(tty_fd, false); - if (r < 0) + if (r < 0) { log_error("Failed to reset /dev/console: %s", strerror(-r)); + return r; + } - safe_close(tty_fd); - return r; + return 0; } static int set_default_unit(const char *u) { @@ -1537,8 +1531,16 @@ int main(int argc, char *argv[]) { /* Reset the console, but only if this is really init and we * are freshly booted */ - if (arg_running_as == SYSTEMD_SYSTEM && arg_action == ACTION_RUN) - console_setup(getpid() == 1 && !skip_setup); + if (arg_running_as == SYSTEMD_SYSTEM && arg_action == ACTION_RUN) { + + /* If we are init, we connect stdin/stdout/stderr to + * /dev/null and make sure we don't have a controlling + * tty. */ + release_terminal(); + + if (getpid() == 1 && !skip_setup) + console_setup(); + } /* Open the logging devices, if possible and necessary */ log_open(); diff --git a/src/shared/util.c b/src/shared/util.c index d6cea4d20..18d40f398 100644 --- a/src/shared/util.c +++ b/src/shared/util.c @@ -2084,12 +2084,14 @@ fail: } int release_terminal(void) { - int r = 0; - struct sigaction sa_old, sa_new = { + static const struct sigaction sa_new = { .sa_handler = SIG_IGN, .sa_flags = SA_RESTART, }; - _cleanup_close_ int fd; + + _cleanup_close_ int fd = -1; + struct sigaction sa_old; + int r = 0; fd = open("/dev/tty", O_RDWR|O_NOCTTY|O_NDELAY|O_CLOEXEC); if (fd < 0) -- 2.30.2