]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/vconsole/vconsole-setup.c
Merge pull request #12805 from yuwata/test-network-cleanups
[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
aaa709bb 73static int toggle_utf8(const char *name, int fd, bool utf8) {
042d7f50
MS
74 int r;
75 struct termios tc = {};
97c4a07d 76
aaa709bb
ZJS
77 assert(name);
78
13a43c73
BR
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
042d7f50 86 r = ioctl(fd, KDSKBMODE, utf8 ? K_UNICODE : K_XLATE);
97c4a07d 87 if (r < 0)
aaa709bb 88 return log_warning_errno(errno, "Failed to %s UTF-8 kbdmode on %s: %m", enable_disable(utf8), name);
97c4a07d 89
042d7f50
MS
90 r = loop_write(fd, utf8 ? "\033%G" : "\033%@", 3, false);
91 if (r < 0)
aaa709bb 92 return log_warning_errno(r, "Failed to %s UTF-8 term processing on %s: %m", enable_disable(utf8), name);
042d7f50
MS
93
94 r = tcgetattr(fd, &tc);
95 if (r >= 0) {
ab8ee0f2 96 SET_FLAG(tc.c_iflag, IUTF8, utf8);
042d7f50 97 r = tcsetattr(fd, TCSANOW, &tc);
a25d4d0e 98 }
042d7f50 99 if (r < 0)
aaa709bb 100 return log_warning_errno(errno, "Failed to %s iutf8 flag on %s: %m", enable_disable(utf8), name);
d305a67b 101
aaa709bb 102 log_debug("UTF-8 kbdmode %sd on %s", enable_disable(utf8), name);
042d7f50
MS
103 return 0;
104}
d305a67b 105
042d7f50
MS
106static int toggle_utf8_sysfs(bool utf8) {
107 int r;
d305a67b 108
57512c89 109 r = write_string_file("/sys/module/vt/parameters/default_utf8", one_zero(utf8), WRITE_STRING_FILE_DISABLE_BUFFER);
d305a67b 110 if (r < 0)
aaa709bb
ZJS
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;
d305a67b
TG
115}
116
aecb6fcb 117static int keyboard_load_and_wait(const char *vc, const char *map, const char *map_toggle, bool utf8) {
5b396b06 118 const char *args[8];
1142bed2 119 unsigned i = 0;
97c4a07d 120 pid_t pid;
4c253ed1 121 int r;
97c4a07d 122
8931278c
LDM
123 /* An empty map means kernel map */
124 if (isempty(map))
c9d2b3d0 125 return 0;
944d4c91 126
9841e8e3 127 args[i++] = KBD_LOADKEYS;
97c4a07d
LP
128 args[i++] = "-q";
129 args[i++] = "-C";
130 args[i++] = vc;
131 if (utf8)
132 args[i++] = "-u";
133 args[i++] = map;
5b396b06
AB
134 if (map_toggle)
135 args[i++] = map_toggle;
97c4a07d
LP
136 args[i++] = NULL;
137
16dbd110
LP
138 if (DEBUG_LOGGING) {
139 _cleanup_free_ char *cmd;
140
141 cmd = strv_join((char**) args, " ");
142 log_debug("Executing \"%s\"...", strnull(cmd));
143 }
3d623780 144
0672e2c6 145 r = safe_fork("(loadkeys)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG, &pid);
4c253ed1 146 if (r < 0)
b6e1fff1 147 return r;
4c253ed1 148 if (r == 0) {
97c4a07d
LP
149 execv(args[0], (char **) args);
150 _exit(EXIT_FAILURE);
151 }
152
7d4904fe 153 return wait_for_terminate_and_check(KBD_LOADKEYS, pid, WAIT_LOG);
97c4a07d
LP
154}
155
aecb6fcb 156static int font_load_and_wait(const char *vc, const char *font, const char *map, const char *unimap) {
97c4a07d 157 const char *args[9];
1142bed2 158 unsigned i = 0;
97c4a07d 159 pid_t pid;
4c253ed1 160 int r;
97c4a07d 161
c9d2b3d0
MS
162 /* Any part can be set independently */
163 if (isempty(font) && isempty(map) && isempty(unimap))
164 return 0;
944d4c91 165
9841e8e3 166 args[i++] = KBD_SETFONT;
97c4a07d
LP
167 args[i++] = "-C";
168 args[i++] = vc;
c9d2b3d0 169 if (!isempty(map)) {
97c4a07d
LP
170 args[i++] = "-m";
171 args[i++] = map;
172 }
c9d2b3d0 173 if (!isempty(unimap)) {
97c4a07d
LP
174 args[i++] = "-u";
175 args[i++] = unimap;
176 }
c9d2b3d0
MS
177 if (!isempty(font))
178 args[i++] = font;
97c4a07d
LP
179 args[i++] = NULL;
180
16dbd110
LP
181 if (DEBUG_LOGGING) {
182 _cleanup_free_ char *cmd;
183
184 cmd = strv_join((char**) args, " ");
185 log_debug("Executing \"%s\"...", strnull(cmd));
186 }
3d623780 187
0672e2c6 188 r = safe_fork("(setfont)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG, &pid);
4c253ed1 189 if (r < 0)
b6e1fff1 190 return r;
4c253ed1 191 if (r == 0) {
97c4a07d
LP
192 execv(args[0], (char **) args);
193 _exit(EXIT_FAILURE);
194 }
195
7d4904fe 196 return wait_for_terminate_and_check(KBD_SETFONT, pid, WAIT_LOG);
97c4a07d
LP
197}
198
d3b37e84 199/*
1142bed2 200 * A newly allocated VT uses the font from the source VT. Here
d3b37e84
KS
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.
eb22d84b
MS
204 *
205 * We also setup per-console utf8 related stuff: kbdmode, term
206 * processing, stty iutf8.
d3b37e84 207 */
1142bed2 208static void setup_remaining_vcs(int src_fd, unsigned src_idx, bool utf8) {
eb22d84b 209 struct console_font_op cfo = {
5297577f
MS
210 .op = KD_FONT_OP_GET,
211 .width = UINT_MAX, .height = UINT_MAX,
212 .charcount = UINT_MAX,
eb22d84b 213 };
eb22d84b 214 struct unimapinit adv = {};
ff452e76 215 struct unimapdesc unimapd;
6a08e1b0 216 _cleanup_free_ struct unipair* unipairs = NULL;
eb22d84b 217 _cleanup_free_ void *fontbuf = NULL;
1142bed2
MS
218 unsigned i;
219 int r;
dd04aac9 220
6a08e1b0 221 unipairs = new(struct unipair, USHRT_MAX);
e2c9192a
LP
222 if (!unipairs) {
223 log_oom();
6a08e1b0
KR
224 return;
225 }
226
5297577f 227 /* get metadata of the current font (width, height, count) */
1142bed2 228 r = ioctl(src_fd, KDFONTOP, &cfo);
eb22d84b 229 if (r < 0)
5297577f 230 log_warning_errno(errno, "KD_FONT_OP_GET failed while trying to get the font metadata: %m");
eb22d84b 231 else {
a5eebcff 232 /* verify parameter sanity first */
5297577f
MS
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)",
a5eebcff 235 cfo.width, cfo.height, cfo.charcount);
5297577f
MS
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
5238e957 240 * requires 32 per glyph, regardless of the actual height - see the comment above #define
5297577f
MS
241 * max_font_size 65536 in drivers/tty/vt/vt.c for more details.
242 */
8419d457 243 fontbuf = malloc_multiply((cfo.width + 7) / 8 * 32, cfo.charcount);
5297577f
MS
244 if (!fontbuf) {
245 log_oom();
246 return;
247 }
1142bed2 248 /* get fonts from the source console */
5297577f 249 cfo.data = fontbuf;
1142bed2 250 r = ioctl(src_fd, KDFONTOP, &cfo);
5297577f
MS
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;
1142bed2 256 r = ioctl(src_fd, GIO_UNIMAP, &unimapd);
5297577f
MS
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 }
eb22d84b
MS
263 }
264
a5eebcff 265 if (cfo.op != KD_FONT_OP_SET)
5297577f 266 log_warning("Fonts will not be copied to remaining consoles");
eb22d84b 267
9fa71843 268 for (i = 1; i <= 63; i++) {
1142bed2 269 char ttyname[sizeof("/dev/tty63")];
eb22d84b 270 _cleanup_close_ int fd_d = -1;
dd04aac9 271
1142bed2 272 if (i == src_idx || verify_vc_allocation(i) < 0)
dd04aac9
KS
273 continue;
274
eb22d84b 275 /* try to open terminal */
1142bed2
MS
276 xsprintf(ttyname, "/dev/tty%u", i);
277 fd_d = open_terminal(ttyname, O_RDWR|O_CLOEXEC|O_NOCTTY);
eb22d84b 278 if (fd_d < 0) {
1142bed2 279 log_warning_errno(fd_d, "Unable to open tty%u, fonts will not be copied: %m", i);
dd04aac9 280 continue;
eb22d84b 281 }
dd04aac9 282
bb5ac84d 283 if (vt_verify_kbmode(fd_d) < 0)
dd04aac9
KS
284 continue;
285
aaa709bb 286 toggle_utf8(ttyname, fd_d, utf8);
eb22d84b
MS
287
288 if (cfo.op != KD_FONT_OP_SET)
289 continue;
290
291 r = ioctl(fd_d, KDFONTOP, &cfo);
292 if (r < 0) {
d610d201
FB
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
eb22d84b
MS
309 continue;
310 }
ff452e76 311
1142bed2 312 /*
d610d201
FB
313 * copy unicode translation table unimapd is a ushort count and a pointer
314 * to an array of struct unipair { ushort, ushort }
1142bed2 315 */
eb22d84b 316 r = ioctl(fd_d, PIO_UNIMAPCLR, &adv);
aaa709bb 317 if (r < 0) {
1142bed2 318 log_warning_errno(errno, "PIO_UNIMAPCLR failed, unimaps might be incorrect for tty%u: %m", i);
aaa709bb 319 continue;
ff452e76 320 }
aaa709bb
ZJS
321
322 r = ioctl(fd_d, PIO_UNIMAP, &unimapd);
323 if (r < 0) {
1142bed2 324 log_warning_errno(errno, "PIO_UNIMAP failed, unimaps might be incorrect for tty%u: %m", i);
aaa709bb
ZJS
325 continue;
326 }
327
328 log_debug("Font and unimap successfully copied to %s", ttyname);
dd04aac9
KS
329 }
330}
331
1142bed2
MS
332static int find_source_vc(char **ret_path, unsigned *ret_idx) {
333 _cleanup_free_ char *path = NULL;
c10d6bdb 334 int r, err = 0;
1142bed2 335 unsigned i;
1142bed2
MS
336
337 path = new(char, sizeof("/dev/tty63"));
234519ae 338 if (!path)
1142bed2
MS
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 }
bb5ac84d 358 r = vt_verify_kbmode(fd);
1142bed2
MS
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;
ae2a15bc 367 *ret_path = TAKE_PTR(path);
c10d6bdb 368 return TAKE_FD(fd);
1142bed2
MS
369 }
370
371 return log_error_errno(err, "No usable source console found: %m");
372}
373
374static int verify_source_vc(char **ret_path, const char *src_vc) {
1142bed2 375 _cleanup_close_ int fd = -1;
c10d6bdb
LP
376 char *path;
377 int r;
1142bed2
MS
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
bb5ac84d 391 r = vt_verify_kbmode(fd);
1142bed2
MS
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);
234519ae 396 if (!path)
1142bed2
MS
397 return log_oom();
398
399 *ret_path = path;
c10d6bdb 400 return TAKE_FD(fd);
1142bed2
MS
401}
402
97c4a07d 403int main(int argc, char **argv) {
abee28c5 404 _cleanup_free_ char
1142bed2 405 *vc = NULL,
abee28c5
ZJS
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;
1142bed2
MS
409 bool utf8, keyboard_ok;
410 unsigned idx = 0;
93c9a9d2 411 int r;
97c4a07d 412
6bf3c61c 413 log_setup_service();
97c4a07d 414
4c12626c
LP
415 umask(0022);
416
97c4a07d 417 if (argv[1])
1142bed2
MS
418 fd = verify_source_vc(&vc, argv[1]);
419 else
420 fd = find_source_vc(&vc, &idx);
1142bed2 421 if (fd < 0)
abee28c5 422 return EXIT_FAILURE;
03044059 423
653ab83b 424 utf8 = is_locale_utf8();
97c4a07d 425
aa8fbc74 426 r = parse_env_file(NULL, "/etc/vconsole.conf",
2034ec42
MS
427 "KEYMAP", &vc_keymap,
428 "KEYMAP_TOGGLE", &vc_keymap_toggle,
429 "FONT", &vc_font,
430 "FONT_MAP", &vc_font_map,
13df9c39 431 "FONT_UNIMAP", &vc_font_unimap);
2034ec42 432 if (r < 0 && r != -ENOENT)
da927ba9 433 log_warning_errno(r, "Failed to read /etc/vconsole.conf: %m");
2034ec42
MS
434
435 /* Let the kernel command line override /etc/vconsole.conf */
01771226
LP
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");
1ebdf5b6 449
31390963
LP
450 (void) toggle_utf8_sysfs(utf8);
451 (void) toggle_utf8(vc, fd, utf8);
93c9a9d2
ZJS
452
453 r = font_load_and_wait(vc, vc_font, vc_font_map, vc_font_unimap);
c9d2b3d0 454 keyboard_ok = keyboard_load_and_wait(vc, vc_keymap, vc_keymap_toggle, utf8) == 0;
97c4a07d 455
1142bed2 456 if (idx > 0) {
93c9a9d2 457 if (r == 0)
1142bed2 458 setup_remaining_vcs(fd, idx, utf8);
93c9a9d2
ZJS
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.");
eb22d84b 464 else
aaa709bb 465 log_warning("Setting source virtual console failed, ignoring remaining ones");
eb22d84b 466 }
97c4a07d 467
93c9a9d2 468 return IN_SET(r, 0, EX_OSERR) && keyboard_ok ? EXIT_SUCCESS : EXIT_FAILURE;
97c4a07d 469}