main: minor code modernization for initializing the console

This commit is contained in:
Lennart Poettering
2014-08-15 18:06:07 +02:00
parent 563b1bdc09
commit 56d96fc00c
2 changed files with 25 additions and 21 deletions
+20 -18
View File
@@ -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();
+5 -3
View File
@@ -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)