1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
26 #include <dbus/dbus.h>
30 #include "dbus-common.h"
32 static int spawn_getty(DBusConnection *bus, const char *console) {
33 DBusMessage *m = NULL, *reply = NULL;
35 const char *fail = "fail";
39 dbus_error_init(&error);
44 /* FIXME: we probably should escape the tty name properly here */
45 if (asprintf(&name, "serial-getty@%s.service", console) < 0)
48 if (!(m = dbus_message_new_method_call("org.freedesktop.systemd1", "/org/freedesktop/systemd1", "org.freedesktop.systemd1.Manager", "StartUnit"))) {
49 log_error("Could not allocate message.");
53 if (!dbus_message_append_args(m,
54 DBUS_TYPE_STRING, &name,
55 DBUS_TYPE_STRING, &fail,
57 log_error("Could not attach target and flag information to message.");
61 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
62 log_error("Failed to start unit: %s", bus_error_message(&error));
70 dbus_message_unref(m);
73 dbus_message_unref(reply);
75 dbus_error_free(&error);
82 static int parse_proc_cmdline_word(const char *word, char **console) {
85 if (startswith(word, "console=")) {
94 !startswith(k, "tty") ||
95 k[3+strspn(k+3, "0123456789")] != 0) {
97 if (!(w = strndup(k, l)))
109 static int parse_proc_cmdline(char **console) {
118 if ((r = read_one_line_file("/proc/cmdline", &line)) < 0) {
119 log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
123 FOREACH_WORD_QUOTED(w, l, line, state) {
126 if (!(word = strndup(w, l))) {
131 r = parse_proc_cmdline_word(word, console);
145 int main(int argc, char *argv[]) {
148 char *console = NULL;
149 DBusConnection *bus = NULL;
151 dbus_error_init(&error);
154 log_error("This program does not take arguments.");
158 log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
159 log_parse_environment();
162 if (bus_connect(DBUS_BUS_SYSTEM, &bus, NULL, &error) < 0) {
163 log_error("Failed to get D-Bus connection: %s", bus_error_message(&error));
167 if (parse_proc_cmdline(&console) < 0)
171 if (spawn_getty(bus, console) < 0)
180 dbus_connection_close(bus);
181 dbus_connection_unref(bus);
184 dbus_error_free(&error);