]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
2a796654 | 2 | |
2a796654 | 3 | #include <errno.h> |
54340751 | 4 | #include <fcntl.h> |
07630cea LP |
5 | #include <string.h> |
6 | #include <unistd.h> | |
2a796654 | 7 | |
b5efdb8a | 8 | #include "alloc-util.h" |
3ffd4af2 | 9 | #include "fd-util.h" |
07630cea | 10 | #include "fileio.h" |
afe44c8f | 11 | #include "generator.h" |
2a796654 | 12 | #include "log.h" |
49e942b2 | 13 | #include "mkdir.h" |
1d97ff7d | 14 | #include "path-util.h" |
0b452006 | 15 | #include "process-util.h" |
07630cea | 16 | #include "string-util.h" |
288a74cc | 17 | #include "terminal-util.h" |
07630cea LP |
18 | #include "unit-name.h" |
19 | #include "util.h" | |
20 | #include "virt.h" | |
2a796654 | 21 | |
6a39419f | 22 | static const char *arg_dest = "/tmp"; |
2a796654 LP |
23 | |
24 | static int add_symlink(const char *fservice, const char *tservice) { | |
f5650614 | 25 | char *from, *to; |
2a796654 LP |
26 | int r; |
27 | ||
4dc380d1 LP |
28 | assert(fservice); |
29 | assert(tservice); | |
30 | ||
63c372cb LP |
31 | from = strjoina(SYSTEM_DATA_UNIT_PATH "/", fservice); |
32 | to = strjoina(arg_dest, "/getty.target.wants/", tservice); | |
2a796654 | 33 | |
d2e54fae | 34 | mkdir_parents_label(to, 0755); |
2a796654 | 35 | |
a17b785b LP |
36 | r = symlink(from, to); |
37 | if (r < 0) { | |
7410616c | 38 | /* In case console=hvc0 is passed this will very likely result in EEXIST */ |
a17b785b | 39 | if (errno == EEXIST) |
f85fc845 | 40 | return 0; |
7410616c LP |
41 | |
42 | return log_error_errno(errno, "Failed to create symlink %s: %m", to); | |
2a796654 LP |
43 | } |
44 | ||
f85fc845 | 45 | return 0; |
2a796654 LP |
46 | } |
47 | ||
4dc380d1 | 48 | static int add_serial_getty(const char *tty) { |
f85fc845 | 49 | _cleanup_free_ char *n = NULL; |
7410616c | 50 | int r; |
4dc380d1 LP |
51 | |
52 | assert(tty); | |
53 | ||
54 | log_debug("Automatically adding serial getty for /dev/%s.", tty); | |
55 | ||
7410616c LP |
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"); | |
4dc380d1 | 59 | |
f85fc845 | 60 | return add_symlink("serial-getty@.service", n); |
4dc380d1 LP |
61 | } |
62 | ||
1d97ff7d LP |
63 | static int add_container_getty(const char *tty) { |
64 | _cleanup_free_ char *n = NULL; | |
7410616c | 65 | int r; |
1d97ff7d LP |
66 | |
67 | assert(tty); | |
68 | ||
69 | log_debug("Automatically adding container getty for /dev/pts/%s.", tty); | |
70 | ||
7410616c LP |
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"); | |
1d97ff7d LP |
74 | |
75 | return add_symlink("container-getty@.service", n); | |
76 | } | |
77 | ||
54340751 LP |
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 | ||
63c372cb | 87 | p = strjoina("/dev/", name); |
54340751 LP |
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) | |
f5e5c28f | 97 | return errno > 0 ? -errno : -EIO; |
54340751 LP |
98 | |
99 | return 0; | |
100 | } | |
101 | ||
2a796654 | 102 | int main(int argc, char *argv[]) { |
3c20189a LP |
103 | |
104 | static const char virtualization_consoles[] = | |
105 | "hvc0\0" | |
106 | "xvc0\0" | |
07901fc1 HB |
107 | "hvsi0\0" |
108 | "sclp_line0\0" | |
fc6c7fe9 HB |
109 | "ttysclp0\0" |
110 | "3270!tty1\0"; | |
3c20189a | 111 | |
f85fc845 | 112 | _cleanup_free_ char *active = NULL; |
3c20189a | 113 | const char *j; |
1d97ff7d | 114 | int r; |
2a796654 | 115 | |
07719a21 LP |
116 | if (argc > 1 && argc != 4) { |
117 | log_error("This program takes three or no arguments."); | |
2a796654 LP |
118 | return EXIT_FAILURE; |
119 | } | |
120 | ||
07719a21 LP |
121 | if (argc > 1) |
122 | arg_dest = argv[1]; | |
123 | ||
afe44c8f | 124 | log_setup_generator(); |
2a796654 | 125 | |
75f86906 | 126 | if (detect_container() > 0) { |
1d97ff7d LP |
127 | _cleanup_free_ char *container_ttys = NULL; |
128 | ||
6705c2df | 129 | log_debug("Automatically adding console shell."); |
2a796654 | 130 | |
337eebb9 | 131 | if (add_symlink("console-getty.service", "console-getty.service") < 0) |
f85fc845 | 132 | return EXIT_FAILURE; |
2a796654 | 133 | |
1d97ff7d LP |
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); | |
207d1d09 | 139 | if (r > 0) { |
a2a5291b | 140 | const char *word, *state; |
1d97ff7d LP |
141 | size_t l; |
142 | ||
a2a5291b | 143 | FOREACH_WORD(word, l, container_ttys, state) { |
1d97ff7d LP |
144 | const char *t; |
145 | char tty[l + 1]; | |
146 | ||
a2a5291b | 147 | memcpy(tty, word, l); |
1d97ff7d LP |
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 */ | |
3fa5dd6d | 156 | t = path_startswith(t, "pts/"); |
1d97ff7d LP |
157 | if (!t) |
158 | continue; | |
159 | ||
160 | if (add_container_getty(t) < 0) | |
161 | return EXIT_FAILURE; | |
162 | } | |
163 | } | |
164 | ||
2a796654 | 165 | /* Don't add any further magic if we are in a container */ |
f85fc845 | 166 | return EXIT_SUCCESS; |
2a796654 LP |
167 | } |
168 | ||
169 | if (read_one_line_file("/sys/class/tty/console/active", &active) >= 0) { | |
a2a5291b | 170 | const char *word, *state; |
39f0570d MM |
171 | size_t l; |
172 | ||
173 | /* Automatically add in a serial getty on all active | |
174 | * kernel consoles */ | |
a2a5291b | 175 | FOREACH_WORD(word, l, active, state) { |
f85fc845 | 176 | _cleanup_free_ char *tty = NULL; |
2a796654 | 177 | |
a2a5291b | 178 | tty = strndup(word, l); |
39f0570d | 179 | if (!tty) { |
f85fc845 LP |
180 | log_oom(); |
181 | return EXIT_FAILURE; | |
39f0570d MM |
182 | } |
183 | ||
54194afb AJ |
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 | ||
f85fc845 | 188 | if (isempty(tty) || tty_is_vc(tty)) |
39f0570d | 189 | continue; |
39f0570d | 190 | |
54340751 LP |
191 | if (verify_tty(tty) < 0) |
192 | continue; | |
193 | ||
f85fc845 LP |
194 | if (add_serial_getty(tty) < 0) |
195 | return EXIT_FAILURE; | |
3c20189a | 196 | } |
2a796654 LP |
197 | } |
198 | ||
199 | /* Automatically add in a serial getty on the first | |
200 | * virtualizer console */ | |
3c20189a | 201 | NULSTR_FOREACH(j, virtualization_consoles) { |
f5650614 | 202 | char *p; |
980fc73d | 203 | |
63c372cb | 204 | p = strjoina("/sys/class/tty/", j); |
f85fc845 | 205 | if (access(p, F_OK) < 0) |
3c20189a | 206 | continue; |
980fc73d | 207 | |
f85fc845 LP |
208 | if (add_serial_getty(j) < 0) |
209 | return EXIT_FAILURE; | |
2a796654 LP |
210 | } |
211 | ||
f85fc845 | 212 | return EXIT_SUCCESS; |
2a796654 | 213 | } |