]>
Commit | Line | Data |
---|---|---|
1 | /* SPDX-License-Identifier: LGPL-2.1+ */ | |
2 | ||
3 | #include <errno.h> | |
4 | #include <fcntl.h> | |
5 | #include <string.h> | |
6 | #include <unistd.h> | |
7 | ||
8 | #include "alloc-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 "string-util.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 = "/tmp"; | |
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 > 0 ? -errno : -EIO; | |
98 | ||
99 | return 0; | |
100 | } | |
101 | ||
102 | int main(int argc, char *argv[]) { | |
103 | ||
104 | static const char virtualization_consoles[] = | |
105 | "hvc0\0" | |
106 | "xvc0\0" | |
107 | "hvsi0\0" | |
108 | "sclp_line0\0" | |
109 | "ttysclp0\0" | |
110 | "3270!tty1\0"; | |
111 | ||
112 | _cleanup_free_ char *active = NULL; | |
113 | const char *j; | |
114 | int r; | |
115 | ||
116 | log_setup_generator(); | |
117 | ||
118 | if (argc > 1 && argc != 4) { | |
119 | log_error("This program takes three or no arguments."); | |
120 | return EXIT_FAILURE; | |
121 | } | |
122 | ||
123 | if (argc > 1) | |
124 | arg_dest = argv[1]; | |
125 | ||
126 | if (detect_container() > 0) { | |
127 | _cleanup_free_ char *container_ttys = NULL; | |
128 | ||
129 | log_debug("Automatically adding console shell."); | |
130 | ||
131 | if (add_symlink("console-getty.service", "console-getty.service") < 0) | |
132 | return EXIT_FAILURE; | |
133 | ||
134 | /* When $container_ttys is set for PID 1, spawn | |
135 | * gettys on all ptys named therein. Note that despite | |
136 | * the variable name we only support ptys here. */ | |
137 | ||
138 | r = getenv_for_pid(1, "container_ttys", &container_ttys); | |
139 | if (r > 0) { | |
140 | const char *word, *state; | |
141 | size_t l; | |
142 | ||
143 | FOREACH_WORD(word, l, container_ttys, state) { | |
144 | const char *t; | |
145 | char tty[l + 1]; | |
146 | ||
147 | memcpy(tty, word, l); | |
148 | tty[l] = 0; | |
149 | ||
150 | /* First strip off /dev/ if it is specified */ | |
151 | t = path_startswith(tty, "/dev/"); | |
152 | if (!t) | |
153 | t = tty; | |
154 | ||
155 | /* Then, make sure it's actually a pty */ | |
156 | t = path_startswith(t, "pts/"); | |
157 | if (!t) | |
158 | continue; | |
159 | ||
160 | if (add_container_getty(t) < 0) | |
161 | return EXIT_FAILURE; | |
162 | } | |
163 | } | |
164 | ||
165 | /* Don't add any further magic if we are in a container */ | |
166 | return EXIT_SUCCESS; | |
167 | } | |
168 | ||
169 | if (read_one_line_file("/sys/class/tty/console/active", &active) >= 0) { | |
170 | const char *word, *state; | |
171 | size_t l; | |
172 | ||
173 | /* Automatically add in a serial getty on all active | |
174 | * kernel consoles */ | |
175 | FOREACH_WORD(word, l, active, state) { | |
176 | _cleanup_free_ char *tty = NULL; | |
177 | ||
178 | tty = strndup(word, l); | |
179 | if (!tty) { | |
180 | log_oom(); | |
181 | return EXIT_FAILURE; | |
182 | } | |
183 | ||
184 | /* We assume that gettys on virtual terminals are | |
185 | * started via manual configuration and do this magic | |
186 | * only for non-VC terminals. */ | |
187 | ||
188 | if (isempty(tty) || tty_is_vc(tty)) | |
189 | continue; | |
190 | ||
191 | if (verify_tty(tty) < 0) | |
192 | continue; | |
193 | ||
194 | if (add_serial_getty(tty) < 0) | |
195 | return EXIT_FAILURE; | |
196 | } | |
197 | } | |
198 | ||
199 | /* Automatically add in a serial getty on the first | |
200 | * virtualizer console */ | |
201 | NULSTR_FOREACH(j, virtualization_consoles) { | |
202 | char *p; | |
203 | ||
204 | p = strjoina("/sys/class/tty/", j); | |
205 | if (access(p, F_OK) < 0) | |
206 | continue; | |
207 | ||
208 | if (add_serial_getty(j) < 0) | |
209 | return EXIT_FAILURE; | |
210 | } | |
211 | ||
212 | return EXIT_SUCCESS; | |
213 | } |