]> git.ipfire.org Git - thirdparty/systemd.git/blame_incremental - src/getty-generator/getty-generator.c
tree-wide: use -EBADF for fd initialization
[thirdparty/systemd.git] / src / getty-generator / getty-generator.c
... / ...
CommitLineData
1/* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3#include <errno.h>
4#include <fcntl.h>
5#include <unistd.h>
6
7#include "alloc-util.h"
8#include "errno-util.h"
9#include "fd-util.h"
10#include "fileio.h"
11#include "generator.h"
12#include "log.h"
13#include "mkdir-label.h"
14#include "parse-util.h"
15#include "path-util.h"
16#include "process-util.h"
17#include "proc-cmdline.h"
18#include "strv.h"
19#include "terminal-util.h"
20#include "unit-name.h"
21#include "virt.h"
22
23static const char *arg_dest = NULL;
24static bool arg_enabled = true;
25
26static int add_symlink(const char *fservice, const char *tservice) {
27 char *from, *to;
28 int r;
29
30 assert(fservice);
31 assert(tservice);
32
33 from = strjoina(SYSTEM_DATA_UNIT_DIR "/", fservice);
34 to = strjoina(arg_dest, "/getty.target.wants/", tservice);
35
36 (void) mkdir_parents_label(to, 0755);
37
38 r = symlink(from, to);
39 if (r < 0) {
40 /* In case console=hvc0 is passed this will very likely result in EEXIST */
41 if (errno == EEXIST)
42 return 0;
43
44 return log_error_errno(errno, "Failed to create symlink %s: %m", to);
45 }
46
47 return 0;
48}
49
50static int add_serial_getty(const char *tty) {
51 _cleanup_free_ char *n = NULL;
52 int r;
53
54 assert(tty);
55
56 log_debug("Automatically adding serial getty for /dev/%s.", tty);
57
58 r = unit_name_from_path_instance("serial-getty", tty, ".service", &n);
59 if (r < 0)
60 return log_error_errno(r, "Failed to generate service name: %m");
61
62 return add_symlink("serial-getty@.service", n);
63}
64
65static int add_container_getty(const char *tty) {
66 _cleanup_free_ char *n = NULL;
67 int r;
68
69 assert(tty);
70
71 log_debug("Automatically adding container getty for /dev/pts/%s.", tty);
72
73 r = unit_name_from_path_instance("container-getty", tty, ".service", &n);
74 if (r < 0)
75 return log_error_errno(r, "Failed to generate service name: %m");
76
77 return add_symlink("container-getty@.service", n);
78}
79
80static int verify_tty(const char *name) {
81 _cleanup_close_ int fd = -EBADF;
82 const char *p;
83
84 /* Some TTYs are weird and have been enumerated but don't work
85 * when you try to use them, such as classic ttyS0 and
86 * friends. Let's check that and open the device and run
87 * isatty() on it. */
88
89 p = strjoina("/dev/", name);
90
91 /* O_NONBLOCK is essential here, to make sure we don't wait
92 * for DCD */
93 fd = open(p, O_RDWR|O_NONBLOCK|O_NOCTTY|O_CLOEXEC|O_NOFOLLOW);
94 if (fd < 0)
95 return -errno;
96
97 errno = 0;
98 if (isatty(fd) <= 0)
99 return errno_or_else(EIO);
100
101 return 0;
102}
103
104static int run_container(void) {
105 _cleanup_free_ char *container_ttys = NULL;
106 int r;
107
108 log_debug("Automatically adding console shell.");
109
110 r = add_symlink("console-getty.service", "console-getty.service");
111 if (r < 0)
112 return r;
113
114 /* When $container_ttys is set for PID 1, spawn gettys on all ptys named therein.
115 * Note that despite the variable name we only support ptys here. */
116
117 (void) getenv_for_pid(1, "container_ttys", &container_ttys);
118
119 for (const char *p = container_ttys;;) {
120 _cleanup_free_ char *word = NULL;
121
122 r = extract_first_word(&p, &word, NULL, 0);
123 if (r < 0)
124 return log_error_errno(r, "Failed to parse $container_ttys: %m");
125 if (r == 0)
126 return 0;
127
128 const char *tty = word;
129
130 /* First strip off /dev/ if it is specified */
131 tty = path_startswith(tty, "/dev/") ?: tty;
132
133 /* Then, make sure it's actually a pty */
134 tty = path_startswith(tty, "pts/");
135 if (!tty)
136 continue;
137
138 r = add_container_getty(tty);
139 if (r < 0)
140 return r;
141 }
142}
143
144static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
145 int r;
146
147 assert(key);
148
149 if (proc_cmdline_key_streq(key, "systemd.getty_auto")) {
150 r = value ? parse_boolean(value) : 1;
151 if (r < 0)
152 log_warning_errno(r, "Failed to parse getty_auto switch \"%s\", ignoring: %m", value);
153 else
154 arg_enabled = r;
155 }
156
157 return 0;
158}
159
160static int run(const char *dest, const char *dest_early, const char *dest_late) {
161 _cleanup_free_ char *getty_auto = NULL;
162 int r;
163
164 assert_se(arg_dest = dest);
165
166 r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, 0);
167 if (r < 0)
168 log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
169
170 r = getenv_for_pid(1, "SYSTEMD_GETTY_AUTO", &getty_auto);
171 if (r < 0)
172 log_warning_errno(r, "Failed to parse $SYSTEMD_GETTY_AUTO environment variable, ignoring: %m");
173 else if (r > 0) {
174 r = parse_boolean(getty_auto);
175 if (r < 0)
176 log_warning_errno(r, "Failed to parse $SYSTEMD_GETTY_AUTO value \"%s\", ignoring: %m", getty_auto);
177 else
178 arg_enabled = r;
179 }
180
181 if (!arg_enabled) {
182 log_debug("Disabled, exiting.");
183 return 0;
184 }
185
186 if (detect_container() > 0)
187 /* Add console shell and look at $container_ttys, but don't do add any
188 * further magic if we are in a container. */
189 return run_container();
190
191 /* Automatically add in a serial getty on all active kernel consoles */
192 _cleanup_free_ char *active = NULL;
193 (void) read_one_line_file("/sys/class/tty/console/active", &active);
194 for (const char *p = active;;) {
195 _cleanup_free_ char *tty = NULL;
196
197 r = extract_first_word(&p, &tty, NULL, 0);
198 if (r < 0)
199 return log_error_errno(r, "Failed to parse /sys/class/tty/console/active: %m");
200 if (r == 0)
201 break;
202
203 /* We assume that gettys on virtual terminals are started via manual configuration and do
204 * this magic only for non-VC terminals. */
205
206 if (isempty(tty) || tty_is_vc(tty))
207 continue;
208
209 if (verify_tty(tty) < 0)
210 continue;
211
212 r = add_serial_getty(tty);
213 if (r < 0)
214 return r;
215 }
216
217 /* Automatically add in a serial getty on the first virtualizer console */
218 FOREACH_STRING(j,
219 "hvc0",
220 "xvc0",
221 "hvsi0",
222 "sclp_line0",
223 "ttysclp0",
224 "3270!tty1") {
225 _cleanup_free_ char *p = NULL;
226
227 p = path_join("/sys/class/tty", j);
228 if (!p)
229 return -ENOMEM;
230 if (access(p, F_OK) < 0)
231 continue;
232
233 r = add_serial_getty(j);
234 if (r < 0)
235 return r;
236 }
237
238 return 0;
239}
240
241DEFINE_MAIN_GENERATOR_FUNCTION(run);