chiark / gitweb /
auto-getty: rework auto console getty logic to work in conjunction with single user...
authorLennart Poettering <lennart@poettering.net>
Tue, 17 Aug 2010 01:29:46 +0000 (03:29 +0200)
committerLennart Poettering <lennart@poettering.net>
Tue, 17 Aug 2010 01:29:46 +0000 (03:29 +0200)
.gitignore
Makefile.am
fixme
src/auto-console-getty.c [new file with mode: 0644]
src/initctl.c
src/main.c
units/systemd-auto-console-getty.service.in [new file with mode: 0644]
units/systemd-random-seed-load.service.in
units/systemd-random-seed-save.service.in
units/systemd-update-utmp-runlevel.service.in
units/systemd-update-utmp-shutdown.service.in

index 6b8edf5ba941d22645a0e2a0014617be2b39993a..7a2050541674abde84a4eeb9571065b6e48a03c9 100644 (file)
@@ -1,3 +1,4 @@
+systemd-auto-console-getty
 systemd-shutdownd
 systemd-random-seed
 systemd-update-utmp
index 013a603fc1c35138957fe3633550f2e45f9dcaf5..de85b7979021ae080657b3f05f9925dd48a3be21 100644 (file)
@@ -72,7 +72,8 @@ rootlibexec_PROGRAMS = \
        systemd-initctl \
        systemd-update-utmp \
        systemd-random-seed \
-       systemd-shutdownd
+       systemd-shutdownd \
+       systemd-auto-console-getty
 
 noinst_PROGRAMS = \
        test-engine \
@@ -139,6 +140,7 @@ dist_systemunit_DATA = \
        units/systemd-initctl.socket \
        units/systemd-logger.socket \
        units/systemd-shutdownd.socket \
+       units/systemd-auto-console-getty.service \
        units/dev-hugepages.automount \
        units/dev-hugepages.mount \
        units/dev-mqueue.automount \
@@ -164,6 +166,7 @@ nodist_systemunit_DATA = \
        units/systemd-initctl.service \
        units/systemd-logger.service \
        units/systemd-shutdownd.service \
+       units/systemd-auto-console-getty.service \
        units/systemd-update-utmp-runlevel.service \
        units/systemd-update-utmp-shutdown.service \
        units/systemd-random-seed-save.service \
@@ -186,6 +189,7 @@ EXTRA_DIST = \
        units/systemd-initctl.service.in \
        units/systemd-logger.service.in \
        units/systemd-shutdownd.service.in \
+       units/systemd-auto-console-getty.service.in \
        units/systemd-update-utmp-runlevel.service.in \
        units/systemd-update-utmp-shutdown.service.in \
        units/systemd-random-seed-save.service.in \
@@ -545,6 +549,18 @@ systemd_shutdownd_CFLAGS = \
 systemd_shutdownd_LDADD = \
        libsystemd-basic.la
 
+systemd_auto_console_getty_SOURCES = \
+       src/auto-console-getty.c \
+       src/dbus-common.c
+
+systemd_auto_console_getty_CFLAGS = \
+       $(AM_CFLAGS) \
+       $(DBUS_CFLAGS)
+
+systemd_auto_console_getty_LDADD = \
+       libsystemd-basic.la \
+       $(DBUS_LIBS)
+
 systemd_cgroups_agent_SOURCES = \
        src/cgroups-agent.c \
        src/dbus-common.c
@@ -825,13 +841,14 @@ install-data-hook:
                $(LN_S) $(systemunitdir)/reboot.target ctrl-alt-del.target && \
                $(LN_S) $(systemunitdir)/rescue.target kbrequest.target )
        ( cd $(DESTDIR)$(pkgsysconfdir)/system/getty.target.wants && \
-               rm -f getty@tty1.service getty@tty2.service getty@tty3.service getty@tty4.service getty@tty5.service getty@tty6.service && \
+               rm -f getty@tty1.service getty@tty2.service getty@tty3.service getty@tty4.service getty@tty5.service getty@tty6.service systemd-auto-console-getty.service && \
                $(LN_S) $(systemunitdir)/getty@.service getty@tty1.service && \
                $(LN_S) $(systemunitdir)/getty@.service getty@tty2.service && \
                $(LN_S) $(systemunitdir)/getty@.service getty@tty3.service && \
                $(LN_S) $(systemunitdir)/getty@.service getty@tty4.service && \
                $(LN_S) $(systemunitdir)/getty@.service getty@tty5.service && \
-               $(LN_S) $(systemunitdir)/getty@.service getty@tty6.service )
+               $(LN_S) $(systemunitdir)/getty@.service getty@tty6.service && \
+               $(LN_S) $(systemunitdir)/systemd-auto-console-getty.service systemd-auto-console-getty.service )
        ( cd $(DESTDIR)$(pkgsysconfdir)/system/multi-user.target.wants && \
                rm -f getty.target remote-fs.target && \
                $(LN_S) $(systemunitdir)/getty.target getty.target && \
diff --git a/fixme b/fixme
index 2df43c34b6f8387de3e0afc5f6bf9cf3e8ace884..30f77e8f19d220e7c0c2c77793dbd807e2554e76 100644 (file)
--- a/fixme
+++ b/fixme
 
 * io priority during initialization
 
-* if a service fails too often, make the service enter maintainence mode, and the socket, too.
+* if a service fails too often, make the service enter maintenance mode, and the socket, too.
 
 * Ray: plymouth after/before getty? https://bugzilla.redhat.com/show_bug.cgi?id=623430
 
-* be more forgiving when parsing unit files, when encountering incorrect lines with assignments
+* add no-tab header to .h
 
-* agetty darf nicht mit emergency.service kollidieren
+* fix systemctl daemon-reload in part of boot
 
 External:
 
diff --git a/src/auto-console-getty.c b/src/auto-console-getty.c
new file mode 100644 (file)
index 0000000..44d2eff
--- /dev/null
@@ -0,0 +1,189 @@
+/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
+
+/***
+  This file is part of systemd.
+
+  Copyright 2010 Lennart Poettering
+
+  systemd is free software; you can redistribute it and/or modify it
+  under the terms of the GNU General Public License as published by
+  the Free Software Foundation; either version 2 of the License, or
+  (at your option) any later version.
+
+  systemd is distributed in the hope that it will be useful, but
+  WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+  General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with systemd; If not, see <http://www.gnu.org/licenses/>.
+***/
+
+#include <errno.h>
+#include <string.h>
+#include <stdlib.h>
+
+#include <dbus/dbus.h>
+
+#include "util.h"
+#include "log.h"
+#include "dbus-common.h"
+
+static int spawn_getty(DBusConnection *bus, const char *console) {
+        DBusMessage *m = NULL, *reply = NULL;
+        DBusError error;
+        const char *fail = "fail";
+        char *name;
+        int r = -EIO;
+
+        dbus_error_init(&error);
+
+        assert(bus);
+        assert(console);
+
+        /* FIXME: we probably should escape the tty name properly here */
+        if (asprintf(&name, "getty@%s.service", console) < 0)
+                return -ENOMEM;
+
+        if (!(m = dbus_message_new_method_call("org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "StartUnit"))) {
+                log_error("Could not allocate message.");
+                goto finish;
+        }
+
+        if (!dbus_message_append_args(m,
+                                      DBUS_TYPE_STRING, &name,
+                                      DBUS_TYPE_STRING, &fail,
+                                      DBUS_TYPE_INVALID)) {
+                log_error("Could not attach target and flag information to message.");
+                goto finish;
+        }
+
+        if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
+                log_error("Failed to start unit: %s", error.message);
+                goto finish;
+        }
+
+        r = 0;
+
+finish:
+        if (m)
+                dbus_message_unref(m);
+
+        if (reply)
+                dbus_message_unref(reply);
+
+        dbus_error_free(&error);
+
+        free(name);
+
+        return r;
+}
+
+static int parse_proc_cmdline_word(const char *word, char **console) {
+        assert(word);
+
+        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(*console);
+                *console = w;
+        }
+
+        return 0;
+}
+
+static int parse_proc_cmdline(char **console) {
+        char *line;
+        int r;
+        char *w;
+        size_t l;
+        char *state;
+
+        assert(console);
+
+        if ((r = read_one_line_file("/tmp/cmdline", &line)) < 0) {
+                log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
+                return 0;
+        }
+
+        FOREACH_WORD_QUOTED(w, l, line, state) {
+                char *word;
+
+                if (!(word = strndup(w, l))) {
+                        r = -ENOMEM;
+                        goto finish;
+                }
+
+                r = parse_proc_cmdline_word(word, console);
+                free(word);
+
+                if (r < 0)
+                        goto finish;
+        }
+
+        r = 0;
+
+finish:
+        free(line);
+        return r;
+}
+
+int main(int argc, char *argv[]) {
+        DBusError error;
+        int r = 1;
+        char *console = NULL;
+        DBusConnection *bus = NULL;
+
+        dbus_error_init(&error);
+
+        if (argc > 1) {
+                log_error("This program does not take arguments.");
+                return 1;
+        }
+
+        log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
+        log_parse_environment();
+        log_open();
+
+        if (bus_connect(DBUS_BUS_SYSTEM, &bus, NULL, &error) < 0) {
+                log_error("Failed to get D-Bus connection: %s", error.message);
+                goto finish;
+        }
+
+        if (parse_proc_cmdline(&console) < 0)
+                goto finish;
+
+        if (console)
+                if (spawn_getty(bus, console) < 0)
+                        goto finish;
+
+        r = 0;
+
+finish:
+        free(console);
+
+        if (bus) {
+               dbus_connection_close(bus);
+               dbus_connection_unref(bus);
+        }
+
+        dbus_error_free(&error);
+
+        dbus_shutdown();
+
+        return r;
+}
index 115452e86b445e4d615a0e9b6b8db239215bba5e..7b478a75d577fc7d308308baeca2e903b2e1a59b 100644 (file)
@@ -119,7 +119,7 @@ static void change_runlevel(Server *s, int runlevel) {
                                       DBUS_TYPE_STRING, &target,
                                       DBUS_TYPE_STRING, &replace,
                                       DBUS_TYPE_INVALID)) {
-                log_error("Could not attach target and flag information to signal message.");
+                log_error("Could not attach target and flag information to message.");
                 goto finish;
         }
 
index 5b5c05acbc7a6d949e9a17eda41226095e4d8a2a..51253c66c878a09273660f473793d092333d27d3 100644 (file)
@@ -57,7 +57,6 @@ 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;
@@ -245,6 +244,8 @@ static int parse_proc_cmdline_word(const char *word) {
                 "5",      SPECIAL_RUNLEVEL5_TARGET
         };
 
+        assert(word);
+
         if (startswith(word, "systemd.unit="))
                 return set_default_unit(word + 13);
 
@@ -336,26 +337,8 @@ static int parse_proc_cmdline_word(const char *word) {
 
         } else if (streq(word, "nomodules"))
                 arg_nomodules = true;
-        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")) {
+        else if (streq(word, "quiet")) {
                 arg_show_status = false;
                 arg_sysv_console = false;
         } else {
@@ -1061,21 +1044,6 @@ int main(int argc, char *argv[]) {
                         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");
@@ -1121,7 +1089,6 @@ finish:
                 manager_free(m);
 
         free(arg_default_unit);
-        free(arg_console);
 
         dbus_shutdown();
 
diff --git a/units/systemd-auto-console-getty.service.in b/units/systemd-auto-console-getty.service.in
new file mode 100644 (file)
index 0000000..6542cab
--- /dev/null
@@ -0,0 +1,16 @@
+#  This file is part of systemd.
+#
+#  systemd is free software; you can redistribute it and/or modify it
+#  under the terms of the GNU General Public License as published by
+#  the Free Software Foundation; either version 2 of the License, or
+#  (at your option) any later version.
+
+[Unit]
+Description=Automatically Spawn getty on Kernel Console
+
+[Service]
+Type=oneshot
+ExecStart=@rootlibexecdir@/systemd-auto-console-getty
+
+[Install]
+WantedBy=getty.target
index 4d5841d6f554e41138f0f7d789f14404dc944ec0..965da0690107c1d6211e0644939492852a5661cc 100644 (file)
@@ -14,7 +14,7 @@ Before=shutdown.target
 
 [Service]
 Type=oneshot
-ExecStart=-@rootlibexecdir@/systemd-random-seed load
+ExecStart=@rootlibexecdir@/systemd-random-seed load
 
 [Install]
 WantedBy=sysinit.target
index 654c859e22b24e1f4c2687c433b54d97ced4a224..70cb31d491ff18c3f6a44f7e7180371405870244 100644 (file)
@@ -13,7 +13,7 @@ Conflicts=systemd-random-seed-save.service
 
 [Service]
 Type=oneshot
-ExecStart=-@rootlibexecdir@/systemd-random-seed save
+ExecStart=@rootlibexecdir@/systemd-random-seed save
 
 [Install]
 WantedBy=shutdown.target
index 0b4c83dd861df8e4cec651a12139de6a601c3e9b..a288d6eb21e92c2e606066e7b4f134995719f7a4 100644 (file)
@@ -14,4 +14,4 @@ Before=shutdown.target
 
 [Service]
 Type=oneshot
-ExecStart=-@rootlibexecdir@/systemd-update-utmp runlevel
+ExecStart=@rootlibexecdir@/systemd-update-utmp runlevel
index 4c48c8b53ad12428826cab09ecfe54189a845816..e071285e3a16e86a858b3dd01d4bad7dde230419 100644 (file)
@@ -13,4 +13,4 @@ Conflicts=systemd-update-utmp-runlevel.service
 
 [Service]
 Type=oneshot
-ExecStart=-@rootlibexecdir@/systemd-update-utmp shutdown
+ExecStart=@rootlibexecdir@/systemd-update-utmp shutdown