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