X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=main.c;h=bba2975e467fea4112bc95a4ba7bd9ad6872310a;hp=29be801125616f773878129a17af20735ca291f7;hb=1b00a255225bd65bcc3f6257bd4b1e2089f231b4;hpb=a16e112358ea8fea381ee106b89e645aed8b0a8c diff --git a/main.c b/main.c index 29be80112..bba2975e4 100644 --- a/main.c +++ b/main.c @@ -36,6 +36,7 @@ #include "log.h" #include "mount-setup.h" #include "hostname-setup.h" +#include "loopback-setup.h" #include "load-fragment.h" #include "fdset.h" @@ -135,6 +136,18 @@ _noreturn static void crash(int sig) { if ((pid = fork()) < 0) log_error("Failed to fork off crash shell: %s", strerror(errno)); else if (pid == 0) { + int fd, r; + + if ((fd = acquire_terminal("/dev/console", false, true)) < 0) { + log_error("Failed to acquire terminal: %s", strerror(-fd)); + _exit(1); + } + + if ((r = make_stdio(fd)) < 0) { + log_error("Failed to duplicate terminal fd: %s", strerror(-r)); + _exit(1); + } + execl("/bin/sh", "/bin/sh", NULL); log_error("execl() failed: %s", strerror(errno)); @@ -164,50 +177,40 @@ static void install_crash_handler(void) { assert_se(sigaction(SIGABRT, &sa, NULL) == 0); } -static int console_setup(void) { - int tty_fd = -1, null_fd = -1, r = 0; +static int make_null_stdio(void) { + int null_fd, r; - /* If we are init, we connect stdout/stderr to /dev/console - * and stdin to /dev/null and make sure we don't have a - * controlling tty. */ - - release_terminal(); - - if ((tty_fd = open_terminal("/dev/console", O_WRONLY)) < 0) { - log_error("Failed to open /dev/console: %s", strerror(-tty_fd)); - r = -tty_fd; - goto finish; - } - - if ((null_fd = open("/dev/null", O_RDONLY)) < 0) { + if ((null_fd = open("/dev/null", O_RDWR)) < 0) { log_error("Failed to open /dev/null: %m"); - r = -errno; - goto finish; + return -errno; } - assert(tty_fd >= 3); - assert(null_fd >= 3); + if ((r = make_stdio(null_fd)) < 0) + log_warning("Failed to dup2() device: %s", strerror(-r)); - if (reset_terminal(tty_fd) < 0) - log_error("Failed to reset /dev/console: %m"); + return r; +} - if (dup2(tty_fd, STDOUT_FILENO) < 0 || - dup2(tty_fd, STDERR_FILENO) < 0 || - dup2(null_fd, STDIN_FILENO) < 0) { - log_error("Failed to dup2() device: %m"); - r = -errno; - goto finish; - } +static int console_setup(bool do_reset) { + int tty_fd, r; - r = 0; + /* If we are init, we connect stdin/stdout/stderr to /dev/null + * and make sure we don't have a controlling tty. */ -finish: - if (tty_fd >= 0) - close_nointr_nofail(tty_fd); + release_terminal(); - if (null_fd >= 0) - close_nointr_nofail(null_fd); + if (!do_reset) + return 0; + if ((tty_fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC)) < 0) { + log_error("Failed to open /dev/console: %s", strerror(-tty_fd)); + return -tty_fd; + } + + if ((r = reset_terminal(tty_fd)) < 0) + log_error("Failed to reset /dev/console: %s", strerror(-r)); + + close_nointr_nofail(tty_fd); return r; } @@ -462,6 +465,15 @@ static int parse_argv(int argc, char *argv[]) { return -EINVAL; } + /* PID 1 will get the kernel arguments as parameters, which we + * ignore and unconditionally read from + * /proc/cmdline. However, we need to ignore those arguments + * here. */ + if (running_as != MANAGER_INIT && optind < argc) { + log_error("Excess arguments."); + return -EINVAL; + } + return 0; } @@ -471,7 +483,7 @@ static int help(void) { " -h --help Show this help\n" " --default=UNIT Set default unit\n" " --log-level=LEVEL Set log level\n" - " --log-target=TARGET Set log target (console, syslog, kmsg)\n" + " --log-target=TARGET Set log target (console, syslog, kmsg, syslog-or-kmsg)\n" " --running-as=AS Set running as (init, system, session)\n" " --test Determine startup sequence, dump it and exit\n" " --dump-configuration-items Dump understood unit configuration items\n" @@ -543,11 +555,10 @@ int main(int argc, char *argv[]) { FDSet *fds = NULL; bool reexecute = false; - if (getpid() == 1) + if (getpid() == 1) { running_as = MANAGER_INIT; - else if (getuid() == 0) - running_as = MANAGER_SYSTEM; - else + log_set_target(LOG_TARGET_SYSLOG_OR_KMSG); + } else running_as = MANAGER_SESSION; if (set_default_unit(SPECIAL_DEFAULT_TARGET) < 0) @@ -612,15 +623,18 @@ int main(int argc, char *argv[]) { umask(0); } - if (running_as == MANAGER_INIT) - console_setup(); - /* Make sure D-Bus doesn't fiddle with the SIGPIPE handlers */ dbus_connection_set_change_sigpipe(FALSE); + /* Reset the console, but only if this is really init and we + * are freshly booted */ + if (running_as != MANAGER_SESSION && action == ACTION_RUN) { + console_setup(getpid() == 1 && !serialization); + make_null_stdio(); + } + /* Open the logging devices, if possible and necessary */ - log_open_syslog(); - log_open_kmsg(); + log_open(); /* Make sure we leave a core dump without panicing the * kernel. */ @@ -629,8 +643,10 @@ int main(int argc, char *argv[]) { log_debug("systemd running in %s mode.", manager_running_as_to_string(running_as)); - if (running_as == MANAGER_INIT) + if (running_as == MANAGER_INIT) { hostname_setup(); + loopback_setup(); + } if ((r = manager_new(running_as, confirm_spawn, &m)) < 0) { log_error("Failed to allocate manager object: %s", strerror(-r)); @@ -638,7 +654,7 @@ int main(int argc, char *argv[]) { } if ((r = manager_startup(m, serialization, fds)) < 0) - log_error("Failed to fully startup daemon: %s", strerror(-r)); + log_error("Failed to fully start up daemon: %s", strerror(-r)); if (fds) { /* This will close all file descriptors that were opened, but @@ -665,7 +681,7 @@ int main(int argc, char *argv[]) { } if (action == ACTION_TEST) { - printf("→ By units:\n"); + printf("-> By units:\n"); manager_dump_units(m, stdout, "\t"); } @@ -675,7 +691,7 @@ int main(int argc, char *argv[]) { } if (action == ACTION_TEST) { - printf("→ By jobs:\n"); + printf("-> By jobs:\n"); manager_dump_jobs(m, stdout, "\t"); retval = 0; goto finish; @@ -701,7 +717,6 @@ int main(int argc, char *argv[]) { break; case MANAGER_REEXECUTE: - if (prepare_reexecute(m, &serialization, &fds) < 0) goto finish;