chiark / gitweb /
main: automatically spawn a getty on the kernel configured serial console
[elogind.git] / src / main.c
index 5af3587d4b4127fb9b5591b4d552edd74decefd9..cddfc88454f2dd14772195392efb2c60ca608f9f 100644 (file)
@@ -44,6 +44,7 @@
 #include "special.h"
 #include "conf-parser.h"
 #include "bus-errors.h"
+#include "missing.h"
 
 static enum {
         ACTION_RUN,
@@ -54,6 +55,7 @@ static enum {
 } arg_action = ACTION_RUN;
 
 static char *arg_default_unit = NULL;
+static char *arg_console = NULL;
 static ManagerRunningAs arg_running_as = _MANAGER_RUNNING_AS_INVALID;
 
 static bool arg_dump_core = true;
@@ -325,7 +327,26 @@ static int parse_proc_cmdline_word(const char *word) {
 
         } else if (streq(word, "nomodules"))
                 arg_nomodules = true;
-        else if (streq(word, "quiet")) {
+        else if (startswith(word, "console=")) {
+                const char *k;
+                size_t l;
+                char *w = NULL;
+
+                k = word + 8;
+                l = strcspn(k, ",");
+
+                if (l < 4 ||
+                    !startswith(k, "tty") ||
+                    k[3+strspn(k+3, "0123456789")] != 0) {
+
+                        if (!(w = strndup(k, l)))
+                                return -ENOMEM;
+                }
+
+                free(arg_console);
+                arg_console = w;
+
+        } else if (streq(word, "quiet")) {
                 if (!ignore_quiet)
                         arg_show_status = false;
         } else {
@@ -772,7 +793,7 @@ static int prepare_reexecute(Manager *m, FILE **_f, FDSet **_fds) {
         assert(_f);
         assert(_fds);
 
-        if ((r = manager_open_serialization(&f)) < 0) {
+        if ((r = manager_open_serialization(m, &f)) < 0) {
                 log_error("Failed to create serialization faile: %s", strerror(-r));
                 goto fail;
         }
@@ -820,7 +841,6 @@ fail:
 int main(int argc, char *argv[]) {
         Manager *m = NULL;
         Unit *target = NULL;
-        Job *job = NULL;
         int r, retval = 1;
         FDSet *fds = NULL;
         bool reexecute = false;
@@ -835,13 +855,16 @@ int main(int argc, char *argv[]) {
                 return 1;
         }
 
-        log_show_color(true);
+        log_show_color(isatty(STDERR_FILENO) > 0);
         log_show_location(false);
         log_set_max_level(LOG_INFO);
 
         if (getpid() == 1) {
                 arg_running_as = MANAGER_SYSTEM;
                 log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
+
+                if (label_init() < 0)
+                        goto finish;
         } else {
                 arg_running_as = MANAGER_SESSION;
                 log_set_target(LOG_TARGET_CONSOLE);
@@ -935,13 +958,24 @@ int main(int argc, char *argv[]) {
 
         log_info(PACKAGE_STRING " running in %s mode.", manager_running_as_to_string(arg_running_as));
 
-        if (arg_running_as == MANAGER_SYSTEM && !serialization) {
-                if (arg_show_status)
-                        status_welcome();
-                modprobe_setup(arg_nomodules);
-                kmod_setup();
-                hostname_setup();
-                loopback_setup();
+        if (arg_running_as == MANAGER_SYSTEM) {
+
+                /* Disable nscd, to avoid deadlocks when systemd uses
+                 * NSS and the nscd socket is maintained by us. */
+                /* if (nss_disable_nscd) { */
+                /*         log_debug("Disabling nscd"); */
+                /*         nss_disable_nscd(); */
+                /* } else */
+                /*         log_debug("Hmm, can't disable nscd."); */
+
+                if (!serialization) {
+                        if (arg_show_status)
+                                status_welcome();
+                        modprobe_setup(arg_nomodules);
+                        kmod_setup();
+                        hostname_setup();
+                        loopback_setup();
+                }
         }
 
         if ((r = manager_new(arg_running_as, &m)) < 0) {
@@ -990,12 +1024,27 @@ int main(int argc, char *argv[]) {
                         manager_dump_units(m, stdout, "\t");
                 }
 
-                if ((r = manager_add_job(m, JOB_START, target, JOB_REPLACE, false, &error, &job)) < 0) {
+                if ((r = manager_add_job(m, JOB_START, target, JOB_REPLACE, false, &error, NULL)) < 0) {
                         log_error("Failed to start default target: %s", bus_error(&error, r));
                         dbus_error_free(&error);
                         goto finish;
                 }
 
+                if (arg_console && arg_running_as == MANAGER_SYSTEM) {
+                        char *name;
+
+                        if (asprintf(&name, "getty@%s.service", arg_console) < 0)
+                                log_error("Out of memory while generating console getty service name.");
+                        else {
+                                if ((r = manager_add_job_by_name(m, JOB_START, name, JOB_FAIL, false, &error, NULL)) < 0) {
+                                        log_error("Failed to start console getty target: %s", bus_error(&error, r));
+                                        dbus_error_free(&error);
+                                }
+
+                                free(name);
+                        }
+                }
+
                 if (arg_action == ACTION_TEST) {
                         printf("-> By jobs:\n");
                         manager_dump_jobs(m, stdout, "\t");
@@ -1041,6 +1090,7 @@ finish:
                 manager_free(m);
 
         free(arg_default_unit);
+        free(arg_console);
 
         dbus_shutdown();
 
@@ -1101,5 +1151,7 @@ finish:
         if (getpid() == 1)
                 freeze();
 
+        label_finish();
+
         return retval;
 }