]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/vconsole/vconsole-setup.c
Merge pull request #12535 from poettering/color-grey
[thirdparty/systemd.git] / src / vconsole / vconsole-setup.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 /***
3 Copyright © 2016 Michal Soltys <soltys@ziu.info>
4 ***/
5
6 #include <errno.h>
7 #include <fcntl.h>
8 #include <limits.h>
9 #include <linux/kd.h>
10 #include <linux/tiocl.h>
11 #include <linux/vt.h>
12 #include <stdbool.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <sys/ioctl.h>
16 #include <sysexits.h>
17 #include <termios.h>
18 #include <sys/stat.h>
19 #include <sys/types.h>
20 #include <unistd.h>
21
22 #include "alloc-util.h"
23 #include "env-file.h"
24 #include "fd-util.h"
25 #include "fileio.h"
26 #include "io-util.h"
27 #include "locale-util.h"
28 #include "log.h"
29 #include "proc-cmdline.h"
30 #include "process-util.h"
31 #include "signal-util.h"
32 #include "stdio-util.h"
33 #include "string-util.h"
34 #include "strv.h"
35 #include "terminal-util.h"
36 #include "util.h"
37 #include "virt.h"
38
39 static int verify_vc_device(int fd) {
40 unsigned char data[] = {
41 TIOCL_GETFGCONSOLE,
42 };
43
44 int r;
45
46 r = ioctl(fd, TIOCLINUX, data);
47 if (r < 0)
48 return -errno;
49
50 return r;
51 }
52
53 static int verify_vc_allocation(unsigned idx) {
54 char vcname[sizeof("/dev/vcs") + DECIMAL_STR_MAX(unsigned) - 2];
55
56 xsprintf(vcname, "/dev/vcs%u", idx);
57
58 if (access(vcname, F_OK) < 0)
59 return -errno;
60
61 return 0;
62 }
63
64 static int verify_vc_allocation_byfd(int fd) {
65 struct vt_stat vcs = {};
66
67 if (ioctl(fd, VT_GETSTATE, &vcs) < 0)
68 return -errno;
69
70 return verify_vc_allocation(vcs.v_active);
71 }
72
73 static int toggle_utf8(const char *name, int fd, bool utf8) {
74 int r;
75 struct termios tc = {};
76
77 assert(name);
78
79 r = vt_verify_kbmode(fd);
80 if (r == -EBUSY) {
81 log_warning_errno(r, "Virtual console %s is not in K_XLATE or K_UNICODE: %m", name);
82 return 0;
83 } else if (r < 0)
84 return log_warning_errno(r, "Failed to verify kbdmode on %s: %m", name);
85
86 r = ioctl(fd, KDSKBMODE, utf8 ? K_UNICODE : K_XLATE);
87 if (r < 0)
88 return log_warning_errno(errno, "Failed to %s UTF-8 kbdmode on %s: %m", enable_disable(utf8), name);
89
90 r = loop_write(fd, utf8 ? "\033%G" : "\033%@", 3, false);
91 if (r < 0)
92 return log_warning_errno(r, "Failed to %s UTF-8 term processing on %s: %m", enable_disable(utf8), name);
93
94 r = tcgetattr(fd, &tc);
95 if (r >= 0) {
96 SET_FLAG(tc.c_iflag, IUTF8, utf8);
97 r = tcsetattr(fd, TCSANOW, &tc);
98 }
99 if (r < 0)
100 return log_warning_errno(errno, "Failed to %s iutf8 flag on %s: %m", enable_disable(utf8), name);
101
102 log_debug("UTF-8 kbdmode %sd on %s", enable_disable(utf8), name);
103 return 0;
104 }
105
106 static int toggle_utf8_sysfs(bool utf8) {
107 int r;
108
109 r = write_string_file("/sys/module/vt/parameters/default_utf8", one_zero(utf8), WRITE_STRING_FILE_DISABLE_BUFFER);
110 if (r < 0)
111 return log_warning_errno(r, "Failed to %s sysfs UTF-8 flag: %m", enable_disable(utf8));
112
113 log_debug("Sysfs UTF-8 flag %sd", enable_disable(utf8));
114 return 0;
115 }
116
117 static int keyboard_load_and_wait(const char *vc, const char *map, const char *map_toggle, bool utf8) {
118 const char *args[8];
119 unsigned i = 0;
120 pid_t pid;
121 int r;
122
123 /* An empty map means kernel map */
124 if (isempty(map))
125 return 0;
126
127 args[i++] = KBD_LOADKEYS;
128 args[i++] = "-q";
129 args[i++] = "-C";
130 args[i++] = vc;
131 if (utf8)
132 args[i++] = "-u";
133 args[i++] = map;
134 if (map_toggle)
135 args[i++] = map_toggle;
136 args[i++] = NULL;
137
138 if (DEBUG_LOGGING) {
139 _cleanup_free_ char *cmd;
140
141 cmd = strv_join((char**) args, " ");
142 log_debug("Executing \"%s\"...", strnull(cmd));
143 }
144
145 r = safe_fork("(loadkeys)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG, &pid);
146 if (r < 0)
147 return r;
148 if (r == 0) {
149 execv(args[0], (char **) args);
150 _exit(EXIT_FAILURE);
151 }
152
153 return wait_for_terminate_and_check(KBD_LOADKEYS, pid, WAIT_LOG);
154 }
155
156 static int font_load_and_wait(const char *vc, const char *font, const char *map, const char *unimap) {
157 const char *args[9];
158 unsigned i = 0;
159 pid_t pid;
160 int r;
161
162 /* Any part can be set independently */
163 if (isempty(font) && isempty(map) && isempty(unimap))
164 return 0;
165
166 args[i++] = KBD_SETFONT;
167 args[i++] = "-C";
168 args[i++] = vc;
169 if (!isempty(map)) {
170 args[i++] = "-m";
171 args[i++] = map;
172 }
173 if (!isempty(unimap)) {
174 args[i++] = "-u";
175 args[i++] = unimap;
176 }
177 if (!isempty(font))
178 args[i++] = font;
179 args[i++] = NULL;
180
181 if (DEBUG_LOGGING) {
182 _cleanup_free_ char *cmd;
183
184 cmd = strv_join((char**) args, " ");
185 log_debug("Executing \"%s\"...", strnull(cmd));
186 }
187
188 r = safe_fork("(setfont)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG, &pid);
189 if (r < 0)
190 return r;
191 if (r == 0) {
192 execv(args[0], (char **) args);
193 _exit(EXIT_FAILURE);
194 }
195
196 return wait_for_terminate_and_check(KBD_SETFONT, pid, WAIT_LOG);
197 }
198
199 /*
200 * A newly allocated VT uses the font from the source VT. Here
201 * we update all possibly already allocated VTs with the configured
202 * font. It also allows to restart systemd-vconsole-setup.service,
203 * to apply a new font to all VTs.
204 *
205 * We also setup per-console utf8 related stuff: kbdmode, term
206 * processing, stty iutf8.
207 */
208 static void setup_remaining_vcs(int src_fd, unsigned src_idx, bool utf8) {
209 struct console_font_op cfo = {
210 .op = KD_FONT_OP_GET,
211 .width = UINT_MAX, .height = UINT_MAX,
212 .charcount = UINT_MAX,
213 };
214 struct unimapinit adv = {};
215 struct unimapdesc unimapd;
216 _cleanup_free_ struct unipair* unipairs = NULL;
217 _cleanup_free_ void *fontbuf = NULL;
218 unsigned i;
219 int r;
220
221 unipairs = new(struct unipair, USHRT_MAX);
222 if (!unipairs) {
223 log_oom();
224 return;
225 }
226
227 /* get metadata of the current font (width, height, count) */
228 r = ioctl(src_fd, KDFONTOP, &cfo);
229 if (r < 0)
230 log_warning_errno(errno, "KD_FONT_OP_GET failed while trying to get the font metadata: %m");
231 else {
232 /* verify parameter sanity first */
233 if (cfo.width > 32 || cfo.height > 32 || cfo.charcount > 512)
234 log_warning("Invalid font metadata - width: %u (max 32), height: %u (max 32), count: %u (max 512)",
235 cfo.width, cfo.height, cfo.charcount);
236 else {
237 /*
238 * Console fonts supported by the kernel are limited in size to 32 x 32 and maximum 512
239 * characters. Thus with 1 bit per pixel it requires up to 65536 bytes. The height always
240 * requires 32 per glyph, regardless of the actual height - see the comment above #define
241 * max_font_size 65536 in drivers/tty/vt/vt.c for more details.
242 */
243 fontbuf = malloc_multiply((cfo.width + 7) / 8 * 32, cfo.charcount);
244 if (!fontbuf) {
245 log_oom();
246 return;
247 }
248 /* get fonts from the source console */
249 cfo.data = fontbuf;
250 r = ioctl(src_fd, KDFONTOP, &cfo);
251 if (r < 0)
252 log_warning_errno(errno, "KD_FONT_OP_GET failed while trying to read the font data: %m");
253 else {
254 unimapd.entries = unipairs;
255 unimapd.entry_ct = USHRT_MAX;
256 r = ioctl(src_fd, GIO_UNIMAP, &unimapd);
257 if (r < 0)
258 log_warning_errno(errno, "GIO_UNIMAP failed while trying to read unicode mappings: %m");
259 else
260 cfo.op = KD_FONT_OP_SET;
261 }
262 }
263 }
264
265 if (cfo.op != KD_FONT_OP_SET)
266 log_warning("Fonts will not be copied to remaining consoles");
267
268 for (i = 1; i <= 63; i++) {
269 char ttyname[sizeof("/dev/tty63")];
270 _cleanup_close_ int fd_d = -1;
271
272 if (i == src_idx || verify_vc_allocation(i) < 0)
273 continue;
274
275 /* try to open terminal */
276 xsprintf(ttyname, "/dev/tty%u", i);
277 fd_d = open_terminal(ttyname, O_RDWR|O_CLOEXEC|O_NOCTTY);
278 if (fd_d < 0) {
279 log_warning_errno(fd_d, "Unable to open tty%u, fonts will not be copied: %m", i);
280 continue;
281 }
282
283 if (vt_verify_kbmode(fd_d) < 0)
284 continue;
285
286 toggle_utf8(ttyname, fd_d, utf8);
287
288 if (cfo.op != KD_FONT_OP_SET)
289 continue;
290
291 r = ioctl(fd_d, KDFONTOP, &cfo);
292 if (r < 0) {
293 int last_errno, mode;
294
295 /* The fonts couldn't have been copied. It might be due to the
296 * terminal being in graphical mode. In this case the kernel
297 * returns -EINVAL which is too generic for distinguishing this
298 * specific case. So we need to retrieve the terminal mode and if
299 * the graphical mode is in used, let's assume that something else
300 * is using the terminal and the failure was expected as we
301 * shouldn't have tried to copy the fonts. */
302
303 last_errno = errno;
304 if (ioctl(fd_d, KDGETMODE, &mode) >= 0 && mode != KD_TEXT)
305 log_debug("KD_FONT_OP_SET skipped: tty%u is not in text mode", i);
306 else
307 log_warning_errno(last_errno, "KD_FONT_OP_SET failed, fonts will not be copied to tty%u: %m", i);
308
309 continue;
310 }
311
312 /*
313 * copy unicode translation table unimapd is a ushort count and a pointer
314 * to an array of struct unipair { ushort, ushort }
315 */
316 r = ioctl(fd_d, PIO_UNIMAPCLR, &adv);
317 if (r < 0) {
318 log_warning_errno(errno, "PIO_UNIMAPCLR failed, unimaps might be incorrect for tty%u: %m", i);
319 continue;
320 }
321
322 r = ioctl(fd_d, PIO_UNIMAP, &unimapd);
323 if (r < 0) {
324 log_warning_errno(errno, "PIO_UNIMAP failed, unimaps might be incorrect for tty%u: %m", i);
325 continue;
326 }
327
328 log_debug("Font and unimap successfully copied to %s", ttyname);
329 }
330 }
331
332 static int find_source_vc(char **ret_path, unsigned *ret_idx) {
333 _cleanup_free_ char *path = NULL;
334 int r, err = 0;
335 unsigned i;
336
337 path = new(char, sizeof("/dev/tty63"));
338 if (!path)
339 return log_oom();
340
341 for (i = 1; i <= 63; i++) {
342 _cleanup_close_ int fd = -1;
343
344 r = verify_vc_allocation(i);
345 if (r < 0) {
346 if (!err)
347 err = -r;
348 continue;
349 }
350
351 sprintf(path, "/dev/tty%u", i);
352 fd = open_terminal(path, O_RDWR|O_CLOEXEC|O_NOCTTY);
353 if (fd < 0) {
354 if (!err)
355 err = -fd;
356 continue;
357 }
358 r = vt_verify_kbmode(fd);
359 if (r < 0) {
360 if (!err)
361 err = -r;
362 continue;
363 }
364
365 /* all checks passed, return this one as a source console */
366 *ret_idx = i;
367 *ret_path = TAKE_PTR(path);
368 return TAKE_FD(fd);
369 }
370
371 return log_error_errno(err, "No usable source console found: %m");
372 }
373
374 static int verify_source_vc(char **ret_path, const char *src_vc) {
375 _cleanup_close_ int fd = -1;
376 char *path;
377 int r;
378
379 fd = open_terminal(src_vc, O_RDWR|O_CLOEXEC|O_NOCTTY);
380 if (fd < 0)
381 return log_error_errno(fd, "Failed to open %s: %m", src_vc);
382
383 r = verify_vc_device(fd);
384 if (r < 0)
385 return log_error_errno(r, "Device %s is not a virtual console: %m", src_vc);
386
387 r = verify_vc_allocation_byfd(fd);
388 if (r < 0)
389 return log_error_errno(r, "Virtual console %s is not allocated: %m", src_vc);
390
391 r = vt_verify_kbmode(fd);
392 if (r < 0)
393 return log_error_errno(r, "Virtual console %s is not in K_XLATE or K_UNICODE: %m", src_vc);
394
395 path = strdup(src_vc);
396 if (!path)
397 return log_oom();
398
399 *ret_path = path;
400 return TAKE_FD(fd);
401 }
402
403 int main(int argc, char **argv) {
404 _cleanup_free_ char
405 *vc = NULL,
406 *vc_keymap = NULL, *vc_keymap_toggle = NULL,
407 *vc_font = NULL, *vc_font_map = NULL, *vc_font_unimap = NULL;
408 _cleanup_close_ int fd = -1;
409 bool utf8, keyboard_ok;
410 unsigned idx = 0;
411 int r;
412
413 log_setup_service();
414
415 umask(0022);
416
417 if (argv[1])
418 fd = verify_source_vc(&vc, argv[1]);
419 else
420 fd = find_source_vc(&vc, &idx);
421 if (fd < 0)
422 return EXIT_FAILURE;
423
424 utf8 = is_locale_utf8();
425
426 r = parse_env_file(NULL, "/etc/vconsole.conf",
427 "KEYMAP", &vc_keymap,
428 "KEYMAP_TOGGLE", &vc_keymap_toggle,
429 "FONT", &vc_font,
430 "FONT_MAP", &vc_font_map,
431 "FONT_UNIMAP", &vc_font_unimap);
432 if (r < 0 && r != -ENOENT)
433 log_warning_errno(r, "Failed to read /etc/vconsole.conf: %m");
434
435 /* Let the kernel command line override /etc/vconsole.conf */
436 r = proc_cmdline_get_key_many(
437 PROC_CMDLINE_STRIP_RD_PREFIX,
438 "vconsole.keymap", &vc_keymap,
439 "vconsole.keymap_toggle", &vc_keymap_toggle,
440 "vconsole.font", &vc_font,
441 "vconsole.font_map", &vc_font_map,
442 "vconsole.font_unimap", &vc_font_unimap,
443 /* compatibility with obsolete multiple-dot scheme */
444 "vconsole.keymap.toggle", &vc_keymap_toggle,
445 "vconsole.font.map", &vc_font_map,
446 "vconsole.font.unimap", &vc_font_unimap);
447 if (r < 0 && r != -ENOENT)
448 log_warning_errno(r, "Failed to read /proc/cmdline: %m");
449
450 (void) toggle_utf8_sysfs(utf8);
451 (void) toggle_utf8(vc, fd, utf8);
452
453 r = font_load_and_wait(vc, vc_font, vc_font_map, vc_font_unimap);
454 keyboard_ok = keyboard_load_and_wait(vc, vc_keymap, vc_keymap_toggle, utf8) == 0;
455
456 if (idx > 0) {
457 if (r == 0)
458 setup_remaining_vcs(fd, idx, utf8);
459 else if (r == EX_OSERR)
460 /* setfont returns EX_OSERR when ioctl(KDFONTOP/PIO_FONTX/PIO_FONTX) fails.
461 * This might mean various things, but in particular lack of a graphical
462 * console. Let's be generous and not treat this as an error. */
463 log_notice("Setting fonts failed with a \"system error\", ignoring.");
464 else
465 log_warning("Setting source virtual console failed, ignoring remaining ones");
466 }
467
468 return IN_SET(r, 0, EX_OSERR) && keyboard_ok ? EXIT_SUCCESS : EXIT_FAILURE;
469 }