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