]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
2a796654 | 2 | |
2a796654 | 3 | #include <errno.h> |
54340751 | 4 | #include <fcntl.h> |
07630cea | 5 | #include <unistd.h> |
2a796654 | 6 | |
b5efdb8a | 7 | #include "alloc-util.h" |
66855de7 | 8 | #include "errno-util.h" |
3ffd4af2 | 9 | #include "fd-util.h" |
07630cea | 10 | #include "fileio.h" |
afe44c8f | 11 | #include "generator.h" |
2a796654 | 12 | #include "log.h" |
35cd0ba5 | 13 | #include "mkdir-label.h" |
ee3fddcc | 14 | #include "parse-util.h" |
1d97ff7d | 15 | #include "path-util.h" |
0b452006 | 16 | #include "process-util.h" |
ee3fddcc | 17 | #include "proc-cmdline.h" |
9d22f97b | 18 | #include "strv.h" |
288a74cc | 19 | #include "terminal-util.h" |
07630cea | 20 | #include "unit-name.h" |
07630cea | 21 | #include "virt.h" |
2a796654 | 22 | |
9d22f97b | 23 | static const char *arg_dest = NULL; |
ee3fddcc | 24 | static bool arg_enabled = true; |
2a796654 LP |
25 | |
26 | static int add_symlink(const char *fservice, const char *tservice) { | |
f5650614 | 27 | char *from, *to; |
2a796654 LP |
28 | int r; |
29 | ||
4dc380d1 LP |
30 | assert(fservice); |
31 | assert(tservice); | |
32 | ||
835cf75a | 33 | from = strjoina(SYSTEM_DATA_UNIT_DIR "/", fservice); |
63c372cb | 34 | to = strjoina(arg_dest, "/getty.target.wants/", tservice); |
2a796654 | 35 | |
35cd0ba5 | 36 | (void) mkdir_parents_label(to, 0755); |
2a796654 | 37 | |
a17b785b LP |
38 | r = symlink(from, to); |
39 | if (r < 0) { | |
7410616c | 40 | /* In case console=hvc0 is passed this will very likely result in EEXIST */ |
a17b785b | 41 | if (errno == EEXIST) |
f85fc845 | 42 | return 0; |
7410616c LP |
43 | |
44 | return log_error_errno(errno, "Failed to create symlink %s: %m", to); | |
2a796654 LP |
45 | } |
46 | ||
f85fc845 | 47 | return 0; |
2a796654 LP |
48 | } |
49 | ||
4dc380d1 | 50 | static int add_serial_getty(const char *tty) { |
f85fc845 | 51 | _cleanup_free_ char *n = NULL; |
7410616c | 52 | int r; |
4dc380d1 LP |
53 | |
54 | assert(tty); | |
55 | ||
56 | log_debug("Automatically adding serial getty for /dev/%s.", tty); | |
57 | ||
7410616c LP |
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"); | |
4dc380d1 | 61 | |
f85fc845 | 62 | return add_symlink("serial-getty@.service", n); |
4dc380d1 LP |
63 | } |
64 | ||
1d97ff7d LP |
65 | static int add_container_getty(const char *tty) { |
66 | _cleanup_free_ char *n = NULL; | |
7410616c | 67 | int r; |
1d97ff7d LP |
68 | |
69 | assert(tty); | |
70 | ||
71 | log_debug("Automatically adding container getty for /dev/pts/%s.", tty); | |
72 | ||
7410616c LP |
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"); | |
1d97ff7d LP |
76 | |
77 | return add_symlink("container-getty@.service", n); | |
78 | } | |
79 | ||
54340751 | 80 | static int verify_tty(const char *name) { |
254d1313 | 81 | _cleanup_close_ int fd = -EBADF; |
54340751 LP |
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 | ||
63c372cb | 89 | p = strjoina("/dev/", name); |
54340751 LP |
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) | |
66855de7 | 99 | return errno_or_else(EIO); |
54340751 LP |
100 | |
101 | return 0; | |
102 | } | |
103 | ||
2417658d ZJS |
104 | static int run_container(void) { |
105 | _cleanup_free_ char *container_ttys = NULL; | |
1d97ff7d | 106 | int r; |
2a796654 | 107 | |
2417658d | 108 | log_debug("Automatically adding console shell."); |
07719a21 | 109 | |
2417658d ZJS |
110 | r = add_symlink("console-getty.service", "console-getty.service"); |
111 | if (r < 0) | |
112 | return r; | |
1d97ff7d | 113 | |
2417658d ZJS |
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. */ | |
2a796654 | 116 | |
2417658d | 117 | (void) getenv_for_pid(1, "container_ttys", &container_ttys); |
2a796654 | 118 | |
2417658d ZJS |
119 | for (const char *p = container_ttys;;) { |
120 | _cleanup_free_ char *word = NULL; | |
1d97ff7d | 121 | |
2417658d ZJS |
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; | |
1d97ff7d | 127 | |
2417658d | 128 | const char *tty = word; |
1d97ff7d | 129 | |
2417658d ZJS |
130 | /* First strip off /dev/ if it is specified */ |
131 | tty = path_startswith(tty, "/dev/") ?: tty; | |
1d97ff7d | 132 | |
2417658d ZJS |
133 | /* Then, make sure it's actually a pty */ |
134 | tty = path_startswith(tty, "pts/"); | |
135 | if (!tty) | |
136 | continue; | |
1d97ff7d | 137 | |
2417658d ZJS |
138 | r = add_container_getty(tty); |
139 | if (r < 0) | |
140 | return r; | |
141 | } | |
142 | } | |
1d97ff7d | 143 | |
ee3fddcc LB |
144 | static 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 | ||
2417658d | 160 | static int run(const char *dest, const char *dest_early, const char *dest_late) { |
ee3fddcc | 161 | _cleanup_free_ char *getty_auto = NULL; |
2417658d | 162 | int r; |
1d97ff7d | 163 | |
2417658d | 164 | assert_se(arg_dest = dest); |
2a796654 | 165 | |
ee3fddcc LB |
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 | ||
2417658d ZJS |
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(); | |
39f0570d | 190 | |
2417658d ZJS |
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; | |
2a796654 | 196 | |
2417658d ZJS |
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; | |
39f0570d | 202 | |
2417658d ZJS |
203 | /* We assume that gettys on virtual terminals are started via manual configuration and do |
204 | * this magic only for non-VC terminals. */ | |
54194afb | 205 | |
2417658d ZJS |
206 | if (isempty(tty) || tty_is_vc(tty)) |
207 | continue; | |
39f0570d | 208 | |
2417658d ZJS |
209 | if (verify_tty(tty) < 0) |
210 | continue; | |
54340751 | 211 | |
2417658d ZJS |
212 | r = add_serial_getty(tty); |
213 | if (r < 0) | |
214 | return r; | |
2a796654 LP |
215 | } |
216 | ||
5980d463 | 217 | /* Automatically add in a serial getty on the first virtualizer console */ |
9d22f97b ZJS |
218 | FOREACH_STRING(j, |
219 | "hvc0", | |
220 | "xvc0", | |
221 | "hvsi0", | |
222 | "sclp_line0", | |
223 | "ttysclp0", | |
224 | "3270!tty1") { | |
6d946490 | 225 | _cleanup_free_ char *p = NULL; |
980fc73d | 226 | |
6d946490 YW |
227 | p = path_join("/sys/class/tty", j); |
228 | if (!p) | |
229 | return -ENOMEM; | |
f85fc845 | 230 | if (access(p, F_OK) < 0) |
3c20189a | 231 | continue; |
980fc73d | 232 | |
9d22f97b ZJS |
233 | r = add_serial_getty(j); |
234 | if (r < 0) | |
235 | return r; | |
2a796654 LP |
236 | } |
237 | ||
9d22f97b | 238 | return 0; |
2a796654 | 239 | } |
9d22f97b ZJS |
240 | |
241 | DEFINE_MAIN_GENERATOR_FUNCTION(run); |