chiark / gitweb /
build-sys: point the development/bug report address at the fd.o list
[elogind.git] / main.c
diff --git a/main.c b/main.c
index 5b23c937b7386e69db20551c3942574d6b248c93..bba2975e467fea4112bc95a4ba7bd9ad6872310a 100644 (file)
--- 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,51 +177,40 @@ static void install_crash_handler(void) {
         assert_se(sigaction(SIGABRT, &sa, NULL) == 0);
 }
 
-static int console_setup(bool do_reset) {
-        int tty_fd = -1, null_fd = -1, r = 0;
-
-        /* 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;
-        }
+static int make_null_stdio(void) {
+        int null_fd, r;
 
-        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 (do_reset)
-                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 (!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 (null_fd >= 0)
-                close_nointr_nofail(null_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;
 }
 
@@ -463,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;
 }
 
@@ -472,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"
@@ -544,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)
@@ -613,17 +623,18 @@ int main(int argc, char *argv[]) {
                 umask(0);
         }
 
+        /* 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_INIT)
+        if (running_as != MANAGER_SESSION && action == ACTION_RUN) {
                 console_setup(getpid() == 1 && !serialization);
-
-        /* Make sure D-Bus doesn't fiddle with the SIGPIPE handlers */
-        dbus_connection_set_change_sigpipe(FALSE);
+                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. */
@@ -632,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));
@@ -641,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