]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/getty-generator/getty-generator.c
Merge pull request #22629 from nishalkulkarni/oomd_service_result
[thirdparty/systemd.git] / src / getty-generator / getty-generator.c
CommitLineData
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
LP
20#include "unit-name.h"
21#include "util.h"
22#include "virt.h"
2a796654 23
9d22f97b 24static const char *arg_dest = NULL;
ee3fddcc 25static bool arg_enabled = true;
2a796654
LP
26
27static int add_symlink(const char *fservice, const char *tservice) {
f5650614 28 char *from, *to;
2a796654
LP
29 int r;
30
4dc380d1
LP
31 assert(fservice);
32 assert(tservice);
33
835cf75a 34 from = strjoina(SYSTEM_DATA_UNIT_DIR "/", fservice);
63c372cb 35 to = strjoina(arg_dest, "/getty.target.wants/", tservice);
2a796654 36
35cd0ba5 37 (void) mkdir_parents_label(to, 0755);
2a796654 38
a17b785b
LP
39 r = symlink(from, to);
40 if (r < 0) {
7410616c 41 /* In case console=hvc0 is passed this will very likely result in EEXIST */
a17b785b 42 if (errno == EEXIST)
f85fc845 43 return 0;
7410616c
LP
44
45 return log_error_errno(errno, "Failed to create symlink %s: %m", to);
2a796654
LP
46 }
47
f85fc845 48 return 0;
2a796654
LP
49}
50
4dc380d1 51static int add_serial_getty(const char *tty) {
f85fc845 52 _cleanup_free_ char *n = NULL;
7410616c 53 int r;
4dc380d1
LP
54
55 assert(tty);
56
57 log_debug("Automatically adding serial getty for /dev/%s.", tty);
58
7410616c
LP
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");
4dc380d1 62
f85fc845 63 return add_symlink("serial-getty@.service", n);
4dc380d1
LP
64}
65
1d97ff7d
LP
66static int add_container_getty(const char *tty) {
67 _cleanup_free_ char *n = NULL;
7410616c 68 int r;
1d97ff7d
LP
69
70 assert(tty);
71
72 log_debug("Automatically adding container getty for /dev/pts/%s.", tty);
73
7410616c
LP
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");
1d97ff7d
LP
77
78 return add_symlink("container-getty@.service", n);
79}
80
54340751
LP
81static 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
63c372cb 90 p = strjoina("/dev/", name);
54340751
LP
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)
66855de7 100 return errno_or_else(EIO);
54340751
LP
101
102 return 0;
103}
104
2417658d
ZJS
105static int run_container(void) {
106 _cleanup_free_ char *container_ttys = NULL;
1d97ff7d 107 int r;
2a796654 108
2417658d 109 log_debug("Automatically adding console shell.");
07719a21 110
2417658d
ZJS
111 r = add_symlink("console-getty.service", "console-getty.service");
112 if (r < 0)
113 return r;
1d97ff7d 114
2417658d
ZJS
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. */
2a796654 117
2417658d 118 (void) getenv_for_pid(1, "container_ttys", &container_ttys);
2a796654 119
2417658d
ZJS
120 for (const char *p = container_ttys;;) {
121 _cleanup_free_ char *word = NULL;
1d97ff7d 122
2417658d
ZJS
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;
1d97ff7d 128
2417658d 129 const char *tty = word;
1d97ff7d 130
2417658d
ZJS
131 /* First strip off /dev/ if it is specified */
132 tty = path_startswith(tty, "/dev/") ?: tty;
1d97ff7d 133
2417658d
ZJS
134 /* Then, make sure it's actually a pty */
135 tty = path_startswith(tty, "pts/");
136 if (!tty)
137 continue;
1d97ff7d 138
2417658d
ZJS
139 r = add_container_getty(tty);
140 if (r < 0)
141 return r;
142 }
143}
1d97ff7d 144
ee3fddcc
LB
145static 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
2417658d 161static int run(const char *dest, const char *dest_early, const char *dest_late) {
ee3fddcc 162 _cleanup_free_ char *getty_auto = NULL;
2417658d 163 int r;
1d97ff7d 164
2417658d 165 assert_se(arg_dest = dest);
2a796654 166
ee3fddcc
LB
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
2417658d
ZJS
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();
39f0570d 191
2417658d
ZJS
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;
2a796654 197
2417658d
ZJS
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;
39f0570d 203
2417658d
ZJS
204 /* We assume that gettys on virtual terminals are started via manual configuration and do
205 * this magic only for non-VC terminals. */
54194afb 206
2417658d
ZJS
207 if (isempty(tty) || tty_is_vc(tty))
208 continue;
39f0570d 209
2417658d
ZJS
210 if (verify_tty(tty) < 0)
211 continue;
54340751 212
2417658d
ZJS
213 r = add_serial_getty(tty);
214 if (r < 0)
215 return r;
2a796654
LP
216 }
217
218 /* Automatically add in a serial getty on the first
219 * virtualizer console */
2417658d 220 const char *j;
9d22f97b
ZJS
221 FOREACH_STRING(j,
222 "hvc0",
223 "xvc0",
224 "hvsi0",
225 "sclp_line0",
226 "ttysclp0",
227 "3270!tty1") {
6d946490 228 _cleanup_free_ char *p = NULL;
980fc73d 229
6d946490
YW
230 p = path_join("/sys/class/tty", j);
231 if (!p)
232 return -ENOMEM;
f85fc845 233 if (access(p, F_OK) < 0)
3c20189a 234 continue;
980fc73d 235
9d22f97b
ZJS
236 r = add_serial_getty(j);
237 if (r < 0)
238 return r;
2a796654
LP
239 }
240
9d22f97b 241 return 0;
2a796654 242}
9d22f97b
ZJS
243
244DEFINE_MAIN_GENERATOR_FUNCTION(run);