mirror of
https://github.com/systemd/systemd-stable.git
synced 2024-12-22 13:33:56 +03:00
auto-getty: rework auto console getty logic to work in conjunction with single user mode
This commit is contained in:
parent
16061c20dd
commit
5192bd1945
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,3 +1,4 @@
|
||||
systemd-auto-console-getty
|
||||
systemd-shutdownd
|
||||
systemd-random-seed
|
||||
systemd-update-utmp
|
||||
|
23
Makefile.am
23
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 && \
|
||||
|
6
fixme
6
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:
|
||||
|
||||
|
189
src/auto-console-getty.c
Normal file
189
src/auto-console-getty.c
Normal file
@ -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;
|
||||
}
|
@ -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;
|
||||
}
|
||||
|
||||
|
39
src/main.c
39
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();
|
||||
|
||||
|
16
units/systemd-auto-console-getty.service.in
Normal file
16
units/systemd-auto-console-getty.service.in
Normal file
@ -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
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -14,4 +14,4 @@ Before=shutdown.target
|
||||
|
||||
[Service]
|
||||
Type=oneshot
|
||||
ExecStart=-@rootlibexecdir@/systemd-update-utmp runlevel
|
||||
ExecStart=@rootlibexecdir@/systemd-update-utmp runlevel
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user