]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/auto-serial-getty.c
update fixme
[thirdparty/systemd.git] / src / auto-serial-getty.c
CommitLineData
5192bd19
LP
1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3/***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
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.
12
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.
17
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/>.
20***/
21
22#include <errno.h>
23#include <string.h>
24#include <stdlib.h>
25
26#include <dbus/dbus.h>
27
28#include "util.h"
29#include "log.h"
30#include "dbus-common.h"
31
32static int spawn_getty(DBusConnection *bus, const char *console) {
33 DBusMessage *m = NULL, *reply = NULL;
34 DBusError error;
35 const char *fail = "fail";
36 char *name;
37 int r = -EIO;
38
39 dbus_error_init(&error);
40
41 assert(bus);
42 assert(console);
43
44 /* FIXME: we probably should escape the tty name properly here */
93a45c56 45 if (asprintf(&name, "serial-getty@%s.service", console) < 0)
5192bd19
LP
46 return -ENOMEM;
47
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.");
50 goto finish;
51 }
52
53 if (!dbus_message_append_args(m,
54 DBUS_TYPE_STRING, &name,
55 DBUS_TYPE_STRING, &fail,
56 DBUS_TYPE_INVALID)) {
57 log_error("Could not attach target and flag information to message.");
58 goto finish;
59 }
60
61 if (!(reply = dbus_connection_send_with_reply_and_block(bus, m, -1, &error))) {
4cf5d675 62 log_error("Failed to start unit: %s", bus_error_message(&error));
5192bd19
LP
63 goto finish;
64 }
65
66 r = 0;
67
68finish:
69 if (m)
70 dbus_message_unref(m);
71
72 if (reply)
73 dbus_message_unref(reply);
74
75 dbus_error_free(&error);
76
77 free(name);
78
79 return r;
80}
81
82static int parse_proc_cmdline_word(const char *word, char **console) {
83 assert(word);
84
85 if (startswith(word, "console=")) {
86 const char *k;
87 size_t l;
88 char *w = NULL;
89
90 k = word + 8;
91 l = strcspn(k, ",");
92
93 if (l < 4 ||
94 !startswith(k, "tty") ||
95 k[3+strspn(k+3, "0123456789")] != 0) {
96
97 if (!(w = strndup(k, l)))
98 return -ENOMEM;
99
100 }
101
102 free(*console);
103 *console = w;
104 }
105
106 return 0;
107}
108
109static int parse_proc_cmdline(char **console) {
110 char *line;
111 int r;
112 char *w;
113 size_t l;
114 char *state;
115
116 assert(console);
117
93a45c56 118 if ((r = read_one_line_file("/proc/cmdline", &line)) < 0) {
5192bd19
LP
119 log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
120 return 0;
121 }
122
123 FOREACH_WORD_QUOTED(w, l, line, state) {
124 char *word;
125
126 if (!(word = strndup(w, l))) {
127 r = -ENOMEM;
128 goto finish;
129 }
130
131 r = parse_proc_cmdline_word(word, console);
132 free(word);
133
134 if (r < 0)
135 goto finish;
136 }
137
138 r = 0;
139
140finish:
141 free(line);
142 return r;
143}
144
145int main(int argc, char *argv[]) {
146 DBusError error;
147 int r = 1;
148 char *console = NULL;
149 DBusConnection *bus = NULL;
150
151 dbus_error_init(&error);
152
153 if (argc > 1) {
154 log_error("This program does not take arguments.");
155 return 1;
156 }
157
158 log_set_target(LOG_TARGET_SYSLOG_OR_KMSG);
159 log_parse_environment();
160 log_open();
161
162 if (bus_connect(DBUS_BUS_SYSTEM, &bus, NULL, &error) < 0) {
4cf5d675 163 log_error("Failed to get D-Bus connection: %s", bus_error_message(&error));
5192bd19
LP
164 goto finish;
165 }
166
167 if (parse_proc_cmdline(&console) < 0)
168 goto finish;
169
170 if (console)
171 if (spawn_getty(bus, console) < 0)
172 goto finish;
173
174 r = 0;
175
176finish:
177 free(console);
178
179 if (bus) {
180 dbus_connection_close(bus);
181 dbus_connection_unref(bus);
182 }
183
184 dbus_error_free(&error);
185
186 dbus_shutdown();
187
188 return r;
189}