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