From 5192bd1945f59254b3d260ded15dd9f2b8cc2de7 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 17 Aug 2010 03:29:46 +0200 Subject: auto-getty: rework auto console getty logic to work in conjunction with single user mode --- .gitignore | 1 + Makefile.am | 23 +++- fixme | 6 +- src/auto-console-getty.c | 189 ++++++++++++++++++++++++++ src/initctl.c | 2 +- src/main.c | 39 +----- units/systemd-auto-console-getty.service.in | 16 +++ units/systemd-random-seed-load.service.in | 2 +- units/systemd-random-seed-save.service.in | 2 +- units/systemd-update-utmp-runlevel.service.in | 2 +- units/systemd-update-utmp-shutdown.service.in | 2 +- 11 files changed, 237 insertions(+), 47 deletions(-) create mode 100644 src/auto-console-getty.c create mode 100644 units/systemd-auto-console-getty.service.in diff --git a/.gitignore b/.gitignore index 6b8edf5ba9..7a20505416 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +systemd-auto-console-getty systemd-shutdownd systemd-random-seed systemd-update-utmp diff --git a/Makefile.am b/Makefile.am index 013a603fc1..de85b79790 100644 --- a/Makefile.am +++ b/Makefile.am @@ -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 2df43c34b6..30f77e8f19 100644 --- a/fixme +++ b/fixme @@ -63,13 +63,13 @@ * 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 index 0000000000..44d2eff732 --- /dev/null +++ b/src/auto-console-getty.c @@ -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 . +***/ + +#include +#include +#include + +#include + +#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; +} diff --git a/src/initctl.c b/src/initctl.c index 115452e86b..7b478a75d5 100644 --- a/src/initctl.c +++ b/src/initctl.c @@ -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; } diff --git a/src/main.c b/src/main.c index 5b5c05acbc..51253c66c8 100644 --- a/src/main.c +++ b/src/main.c @@ -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 index 0000000000..6542cab18a --- /dev/null +++ b/units/systemd-auto-console-getty.service.in @@ -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 diff --git a/units/systemd-random-seed-load.service.in b/units/systemd-random-seed-load.service.in index 4d5841d6f5..965da06901 100644 --- a/units/systemd-random-seed-load.service.in +++ b/units/systemd-random-seed-load.service.in @@ -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 diff --git a/units/systemd-random-seed-save.service.in b/units/systemd-random-seed-save.service.in index 654c859e22..70cb31d491 100644 --- a/units/systemd-random-seed-save.service.in +++ b/units/systemd-random-seed-save.service.in @@ -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 diff --git a/units/systemd-update-utmp-runlevel.service.in b/units/systemd-update-utmp-runlevel.service.in index 0b4c83dd86..a288d6eb21 100644 --- a/units/systemd-update-utmp-runlevel.service.in +++ b/units/systemd-update-utmp-runlevel.service.in @@ -14,4 +14,4 @@ Before=shutdown.target [Service] Type=oneshot -ExecStart=-@rootlibexecdir@/systemd-update-utmp runlevel +ExecStart=@rootlibexecdir@/systemd-update-utmp runlevel diff --git a/units/systemd-update-utmp-shutdown.service.in b/units/systemd-update-utmp-shutdown.service.in index 4c48c8b53a..e071285e3a 100644 --- a/units/systemd-update-utmp-shutdown.service.in +++ b/units/systemd-update-utmp-shutdown.service.in @@ -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 -- cgit v1.2.3-54-g00ecf