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