chiark / gitweb /
main: make sure we don't accidentally acquire a controlling terminal
[elogind.git] / main.c
diff --git a/main.c b/main.c
index 0aa3796e878755fe3bce4df60f508c8d69fe9870..1f407dce214977b81465db106ab188351341b1ea 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,23 @@ _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;
+
+                        if ((fd = acquire_terminal("/dev/console", false, true)) < 0) {
+                                log_error("Failed to acquire terminal: %s", strerror(-fd));
+                                _exit(1);
+                        }
+
+                        if (dup2(fd, STDIN_FILENO) < 0 ||
+                            dup2(fd, STDOUT_FILENO) < 0 ||
+                            dup2(fd, STDERR_FILENO) < 0) {
+                                log_error("Failed to duplicate terminal fd: %s", strerror(errno));
+                                _exit(1);
+                        }
+
+                        if (fd >= 3)
+                                close_nointr_nofail(fd);
+
                         execl("/bin/sh", "/bin/sh", NULL);
 
                         log_error("execl() failed: %s", strerror(errno));
@@ -173,7 +191,7 @@ static int console_setup(bool do_reset) {
 
         release_terminal();
 
-        if ((tty_fd = open_terminal("/dev/console", O_WRONLY)) < 0) {
+        if ((tty_fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY)) < 0) {
                 log_error("Failed to open /dev/console: %s", strerror(-tty_fd));
                 r = -tty_fd;
                 goto finish;
@@ -463,6 +481,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;
 }
 
@@ -615,7 +642,7 @@ int main(int argc, char *argv[]) {
 
         /* Reset the console, but only if this is really init and we
          * are freshly booted */
-        if (running_as == MANAGER_INIT)
+        if (running_as == MANAGER_INIT && action == ACTION_RUN)
                 console_setup(getpid() == 1 && !serialization);
 
         /* Make sure D-Bus doesn't fiddle with the SIGPIPE handlers */
@@ -632,8 +659,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 +670,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
@@ -668,7 +697,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");
                 }
 
@@ -678,7 +707,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;