]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
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 | 12 | #include <stdbool.h> |
07630cea LP |
13 | #include <stdlib.h> |
14 | #include <sys/ioctl.h> | |
93c9a9d2 | 15 | #include <sysexits.h> |
042d7f50 | 16 | #include <termios.h> |
ca78ad1d ZJS |
17 | #include <sys/stat.h> |
18 | #include <sys/types.h> | |
07630cea | 19 | #include <unistd.h> |
97c4a07d | 20 | |
b5efdb8a | 21 | #include "alloc-util.h" |
ea575e17 | 22 | #include "creds-util.h" |
686d13b9 | 23 | #include "env-file.h" |
7c248223 | 24 | #include "errno-util.h" |
3ffd4af2 | 25 | #include "fd-util.h" |
a5c32cff | 26 | #include "fileio.h" |
c004493c | 27 | #include "io-util.h" |
8752c575 | 28 | #include "locale-util.h" |
a0043bfa | 29 | #include "lock-util.h" |
07630cea | 30 | #include "log.h" |
01771226 | 31 | #include "proc-cmdline.h" |
0b452006 | 32 | #include "process-util.h" |
ce30c8dc | 33 | #include "signal-util.h" |
d054f0a4 | 34 | #include "stdio-util.h" |
07630cea | 35 | #include "string-util.h" |
3d623780 | 36 | #include "strv.h" |
07630cea | 37 | #include "terminal-util.h" |
07630cea | 38 | #include "virt.h" |
97c4a07d | 39 | |
8886ca62 YW |
40 | typedef enum VCMeta { |
41 | VC_KEYMAP, | |
42 | VC_KEYMAP_TOGGLE, | |
43 | VC_FONT, | |
44 | VC_FONT_MAP, | |
45 | VC_FONT_UNIMAP, | |
46 | _VC_META_MAX, | |
47 | _VC_META_INVALID = -EINVAL, | |
48 | } VCMeta; | |
49 | ||
50 | typedef struct Context { | |
51 | char *config[_VC_META_MAX]; | |
52 | } Context; | |
53 | ||
54 | static const char * const vc_meta_names[_VC_META_MAX] = { | |
64fe2aea ZJS |
55 | [VC_KEYMAP] = "vconsole.keymap", |
56 | [VC_KEYMAP_TOGGLE] = "vconsole.keymap_toggle", | |
57 | [VC_FONT] = "vconsole.font", | |
58 | [VC_FONT_MAP] = "vconsole.font_map", | |
59 | [VC_FONT_UNIMAP] = "vconsole.font_unimap", | |
8886ca62 YW |
60 | }; |
61 | ||
62 | /* compatibility with obsolete multiple-dot scheme */ | |
63 | static const char * const vc_meta_compat_names[_VC_META_MAX] = { | |
64 | [VC_KEYMAP_TOGGLE] = "vconsole.keymap.toggle", | |
65 | [VC_FONT_MAP] = "vconsole.font.map", | |
66 | [VC_FONT_UNIMAP] = "vconsole.font.unimap", | |
67 | }; | |
68 | ||
69 | static const char * const vc_env_names[_VC_META_MAX] = { | |
70 | [VC_KEYMAP] = "KEYMAP", | |
71 | [VC_KEYMAP_TOGGLE] = "KEYMAP_TOGGLE", | |
72 | [VC_FONT] = "FONT", | |
73 | [VC_FONT_MAP] = "FONT_MAP", | |
74 | [VC_FONT_UNIMAP] = "FONT_UNIMAP", | |
75 | }; | |
76 | ||
77 | static void context_done(Context *c) { | |
78 | assert(c); | |
79 | ||
80 | for (VCMeta i = 0; i < _VC_META_MAX; i++) | |
81 | free(c->config[i]); | |
82 | } | |
83 | ||
84 | static void context_merge_config( | |
85 | Context *dst, | |
86 | Context *src, | |
87 | Context *src_compat) { | |
88 | ||
89 | assert(dst); | |
90 | assert(src); | |
91 | ||
92 | for (VCMeta i = 0; i < _VC_META_MAX; i++) | |
93 | if (src->config[i]) | |
94 | free_and_replace(dst->config[i], src->config[i]); | |
95 | else if (src_compat && src_compat->config[i]) | |
96 | free_and_replace(dst->config[i], src_compat->config[i]); | |
97 | } | |
98 | ||
dfc55e34 YW |
99 | static const char* context_get_config(Context *c, VCMeta meta) { |
100 | assert(c); | |
101 | assert(meta >= 0 && meta < _VC_META_MAX); | |
102 | ||
103 | if (meta == VC_KEYMAP) | |
104 | return isempty(c->config[VC_KEYMAP]) ? SYSTEMD_DEFAULT_KEYMAP : c->config[VC_KEYMAP]; | |
105 | ||
106 | return empty_to_null(c->config[meta]); | |
107 | } | |
108 | ||
8886ca62 YW |
109 | static int context_read_creds(Context *c) { |
110 | _cleanup_(context_done) Context v = {}; | |
111 | int r; | |
112 | ||
113 | assert(c); | |
114 | ||
115 | r = read_credential_strings_many( | |
116 | vc_meta_names[VC_KEYMAP], &v.config[VC_KEYMAP], | |
117 | vc_meta_names[VC_KEYMAP_TOGGLE], &v.config[VC_KEYMAP_TOGGLE], | |
118 | vc_meta_names[VC_FONT], &v.config[VC_FONT], | |
119 | vc_meta_names[VC_FONT_MAP], &v.config[VC_FONT_MAP], | |
120 | vc_meta_names[VC_FONT_UNIMAP], &v.config[VC_FONT_UNIMAP]); | |
55ace8e5 ZJS |
121 | if (r < 0) |
122 | log_warning_errno(r, "Failed to import credentials, ignoring: %m"); | |
8886ca62 YW |
123 | |
124 | context_merge_config(c, &v, NULL); | |
125 | return 0; | |
126 | } | |
127 | ||
128 | static int context_read_env(Context *c) { | |
129 | _cleanup_(context_done) Context v = {}; | |
130 | int r; | |
131 | ||
132 | assert(c); | |
133 | ||
134 | r = parse_env_file( | |
135 | NULL, "/etc/vconsole.conf", | |
136 | vc_env_names[VC_KEYMAP], &v.config[VC_KEYMAP], | |
137 | vc_env_names[VC_KEYMAP_TOGGLE], &v.config[VC_KEYMAP_TOGGLE], | |
138 | vc_env_names[VC_FONT], &v.config[VC_FONT], | |
139 | vc_env_names[VC_FONT_MAP], &v.config[VC_FONT_MAP], | |
140 | vc_env_names[VC_FONT_UNIMAP], &v.config[VC_FONT_UNIMAP]); | |
141 | if (r < 0) { | |
142 | if (r != -ENOENT) | |
143 | log_warning_errno(r, "Failed to read /etc/vconsole.conf, ignoring: %m"); | |
144 | return r; | |
145 | } | |
146 | ||
147 | context_merge_config(c, &v, NULL); | |
148 | return 0; | |
149 | } | |
150 | ||
151 | static int context_read_proc_cmdline(Context *c) { | |
152 | _cleanup_(context_done) Context v = {}, w = {}; | |
153 | int r; | |
154 | ||
155 | assert(c); | |
156 | ||
157 | r = proc_cmdline_get_key_many( | |
158 | PROC_CMDLINE_STRIP_RD_PREFIX, | |
159 | vc_meta_names[VC_KEYMAP], &v.config[VC_KEYMAP], | |
160 | vc_meta_names[VC_KEYMAP_TOGGLE], &v.config[VC_KEYMAP_TOGGLE], | |
161 | vc_meta_names[VC_FONT], &v.config[VC_FONT], | |
162 | vc_meta_names[VC_FONT_MAP], &v.config[VC_FONT_MAP], | |
163 | vc_meta_names[VC_FONT_UNIMAP], &v.config[VC_FONT_UNIMAP], | |
164 | vc_meta_compat_names[VC_KEYMAP_TOGGLE], &w.config[VC_KEYMAP_TOGGLE], | |
165 | vc_meta_compat_names[VC_FONT_MAP], &w.config[VC_FONT_MAP], | |
166 | vc_meta_compat_names[VC_FONT_UNIMAP], &w.config[VC_FONT_UNIMAP]); | |
167 | if (r < 0) { | |
168 | if (r != -ENOENT) | |
169 | log_warning_errno(r, "Failed to read /proc/cmdline, ignoring: %m"); | |
170 | return r; | |
171 | } | |
172 | ||
173 | context_merge_config(c, &v, &w); | |
174 | return 0; | |
175 | } | |
176 | ||
177 | static void context_load_config(Context *c) { | |
178 | assert(c); | |
179 | ||
180 | /* Load data from credentials (lowest priority) */ | |
181 | (void) context_read_creds(c); | |
182 | ||
183 | /* Load data from configuration file (middle priority) */ | |
184 | (void) context_read_env(c); | |
185 | ||
186 | /* Let the kernel command line override /etc/vconsole.conf (highest priority) */ | |
187 | (void) context_read_proc_cmdline(c); | |
188 | } | |
189 | ||
1142bed2 | 190 | static int verify_vc_device(int fd) { |
b53f3386 LP |
191 | unsigned char data[] = { |
192 | TIOCL_GETFGCONSOLE, | |
193 | }; | |
194 | ||
7c248223 | 195 | return RET_NERRNO(ioctl(fd, TIOCLINUX, data)); |
97c4a07d LP |
196 | } |
197 | ||
1142bed2 MS |
198 | static int verify_vc_allocation(unsigned idx) { |
199 | char vcname[sizeof("/dev/vcs") + DECIMAL_STR_MAX(unsigned) - 2]; | |
03044059 | 200 | |
1142bed2 | 201 | xsprintf(vcname, "/dev/vcs%u", idx); |
b53f3386 | 202 | |
7c248223 | 203 | return RET_NERRNO(access(vcname, F_OK)); |
03044059 MS |
204 | } |
205 | ||
1142bed2 | 206 | static int verify_vc_allocation_byfd(int fd) { |
03044059 MS |
207 | struct vt_stat vcs = {}; |
208 | ||
b53f3386 LP |
209 | if (ioctl(fd, VT_GETSTATE, &vcs) < 0) |
210 | return -errno; | |
211 | ||
212 | return verify_vc_allocation(vcs.v_active); | |
03044059 MS |
213 | } |
214 | ||
26382cab LP |
215 | static int verify_vc_kbmode(int fd) { |
216 | int curr_mode; | |
217 | ||
218 | /* | |
219 | * Make sure we only adjust consoles in K_XLATE or K_UNICODE mode. | |
220 | * Otherwise we would (likely) interfere with X11's processing of the | |
221 | * key events. | |
222 | * | |
41d6f3bf | 223 | * https://lists.freedesktop.org/archives/systemd-devel/2013-February/008573.html |
26382cab LP |
224 | */ |
225 | ||
226 | if (ioctl(fd, KDGKBMODE, &curr_mode) < 0) | |
227 | return -errno; | |
228 | ||
229 | return IN_SET(curr_mode, K_XLATE, K_UNICODE) ? 0 : -EBUSY; | |
230 | } | |
231 | ||
e8d1d6e7 | 232 | static int toggle_utf8_vc(const char *name, int fd, bool utf8) { |
042d7f50 MS |
233 | int r; |
234 | struct termios tc = {}; | |
97c4a07d | 235 | |
aaa709bb | 236 | assert(name); |
e8d1d6e7 | 237 | assert(fd >= 0); |
aaa709bb | 238 | |
042d7f50 | 239 | r = ioctl(fd, KDSKBMODE, utf8 ? K_UNICODE : K_XLATE); |
97c4a07d | 240 | if (r < 0) |
aaa709bb | 241 | return log_warning_errno(errno, "Failed to %s UTF-8 kbdmode on %s: %m", enable_disable(utf8), name); |
97c4a07d | 242 | |
d89457a1 | 243 | r = loop_write(fd, utf8 ? "\033%G" : "\033%@", SIZE_MAX, false); |
042d7f50 | 244 | if (r < 0) |
aaa709bb | 245 | return log_warning_errno(r, "Failed to %s UTF-8 term processing on %s: %m", enable_disable(utf8), name); |
042d7f50 MS |
246 | |
247 | r = tcgetattr(fd, &tc); | |
248 | if (r >= 0) { | |
ab8ee0f2 | 249 | SET_FLAG(tc.c_iflag, IUTF8, utf8); |
042d7f50 | 250 | r = tcsetattr(fd, TCSANOW, &tc); |
a25d4d0e | 251 | } |
042d7f50 | 252 | if (r < 0) |
aaa709bb | 253 | return log_warning_errno(errno, "Failed to %s iutf8 flag on %s: %m", enable_disable(utf8), name); |
d305a67b | 254 | |
aaa709bb | 255 | log_debug("UTF-8 kbdmode %sd on %s", enable_disable(utf8), name); |
042d7f50 MS |
256 | return 0; |
257 | } | |
d305a67b | 258 | |
042d7f50 MS |
259 | static int toggle_utf8_sysfs(bool utf8) { |
260 | int r; | |
d305a67b | 261 | |
57512c89 | 262 | r = write_string_file("/sys/module/vt/parameters/default_utf8", one_zero(utf8), WRITE_STRING_FILE_DISABLE_BUFFER); |
d305a67b | 263 | if (r < 0) |
aaa709bb ZJS |
264 | return log_warning_errno(r, "Failed to %s sysfs UTF-8 flag: %m", enable_disable(utf8)); |
265 | ||
266 | log_debug("Sysfs UTF-8 flag %sd", enable_disable(utf8)); | |
267 | return 0; | |
d305a67b TG |
268 | } |
269 | ||
dfc55e34 YW |
270 | static int keyboard_load_and_wait(const char *vc, Context *c, bool utf8) { |
271 | const char *map, *map_toggle, *args[8]; | |
1142bed2 | 272 | unsigned i = 0; |
97c4a07d | 273 | pid_t pid; |
4c253ed1 | 274 | int r; |
97c4a07d | 275 | |
dfc55e34 YW |
276 | assert(vc); |
277 | assert(c); | |
278 | ||
279 | map = context_get_config(c, VC_KEYMAP); | |
280 | map_toggle = context_get_config(c, VC_KEYMAP_TOGGLE); | |
281 | ||
8931278c | 282 | /* An empty map means kernel map */ |
1cd42110 | 283 | if (isempty(map)) |
c9d2b3d0 | 284 | return 0; |
944d4c91 | 285 | |
9841e8e3 | 286 | args[i++] = KBD_LOADKEYS; |
97c4a07d LP |
287 | args[i++] = "-q"; |
288 | args[i++] = "-C"; | |
289 | args[i++] = vc; | |
290 | if (utf8) | |
291 | args[i++] = "-u"; | |
292 | args[i++] = map; | |
5b396b06 AB |
293 | if (map_toggle) |
294 | args[i++] = map_toggle; | |
97c4a07d LP |
295 | args[i++] = NULL; |
296 | ||
16dbd110 | 297 | if (DEBUG_LOGGING) { |
c2b2df60 | 298 | _cleanup_free_ char *cmd = NULL; |
16dbd110 LP |
299 | |
300 | cmd = strv_join((char**) args, " "); | |
301 | log_debug("Executing \"%s\"...", strnull(cmd)); | |
302 | } | |
3d623780 | 303 | |
0672e2c6 | 304 | r = safe_fork("(loadkeys)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG, &pid); |
4c253ed1 | 305 | if (r < 0) |
b6e1fff1 | 306 | return r; |
4c253ed1 | 307 | if (r == 0) { |
97c4a07d LP |
308 | execv(args[0], (char **) args); |
309 | _exit(EXIT_FAILURE); | |
310 | } | |
311 | ||
7d4904fe | 312 | return wait_for_terminate_and_check(KBD_LOADKEYS, pid, WAIT_LOG); |
97c4a07d LP |
313 | } |
314 | ||
dfc55e34 YW |
315 | static int font_load_and_wait(const char *vc, Context *c) { |
316 | const char *font, *map, *unimap, *args[9]; | |
1142bed2 | 317 | unsigned i = 0; |
97c4a07d | 318 | pid_t pid; |
4c253ed1 | 319 | int r; |
97c4a07d | 320 | |
dfc55e34 YW |
321 | assert(vc); |
322 | assert(c); | |
323 | ||
324 | font = context_get_config(c, VC_FONT); | |
325 | map = context_get_config(c, VC_FONT_MAP); | |
326 | unimap = context_get_config(c, VC_FONT_UNIMAP); | |
327 | ||
c9d2b3d0 | 328 | /* Any part can be set independently */ |
dfc55e34 | 329 | if (!font && !map && !unimap) |
c9d2b3d0 | 330 | return 0; |
944d4c91 | 331 | |
9841e8e3 | 332 | args[i++] = KBD_SETFONT; |
97c4a07d LP |
333 | args[i++] = "-C"; |
334 | args[i++] = vc; | |
dfc55e34 | 335 | if (map) { |
97c4a07d LP |
336 | args[i++] = "-m"; |
337 | args[i++] = map; | |
338 | } | |
dfc55e34 | 339 | if (unimap) { |
97c4a07d LP |
340 | args[i++] = "-u"; |
341 | args[i++] = unimap; | |
342 | } | |
dfc55e34 | 343 | if (font) |
c9d2b3d0 | 344 | args[i++] = font; |
97c4a07d LP |
345 | args[i++] = NULL; |
346 | ||
16dbd110 | 347 | if (DEBUG_LOGGING) { |
c2b2df60 | 348 | _cleanup_free_ char *cmd = NULL; |
16dbd110 LP |
349 | |
350 | cmd = strv_join((char**) args, " "); | |
351 | log_debug("Executing \"%s\"...", strnull(cmd)); | |
352 | } | |
3d623780 | 353 | |
0672e2c6 | 354 | r = safe_fork("(setfont)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG, &pid); |
4c253ed1 | 355 | if (r < 0) |
b6e1fff1 | 356 | return r; |
4c253ed1 | 357 | if (r == 0) { |
97c4a07d LP |
358 | execv(args[0], (char **) args); |
359 | _exit(EXIT_FAILURE); | |
360 | } | |
361 | ||
7d4904fe | 362 | return wait_for_terminate_and_check(KBD_SETFONT, pid, WAIT_LOG); |
97c4a07d LP |
363 | } |
364 | ||
d3b37e84 | 365 | /* |
1142bed2 | 366 | * A newly allocated VT uses the font from the source VT. Here |
d3b37e84 KS |
367 | * we update all possibly already allocated VTs with the configured |
368 | * font. It also allows to restart systemd-vconsole-setup.service, | |
369 | * to apply a new font to all VTs. | |
eb22d84b MS |
370 | * |
371 | * We also setup per-console utf8 related stuff: kbdmode, term | |
372 | * processing, stty iutf8. | |
d3b37e84 | 373 | */ |
1142bed2 | 374 | static void setup_remaining_vcs(int src_fd, unsigned src_idx, bool utf8) { |
eb22d84b | 375 | struct console_font_op cfo = { |
5297577f MS |
376 | .op = KD_FONT_OP_GET, |
377 | .width = UINT_MAX, .height = UINT_MAX, | |
378 | .charcount = UINT_MAX, | |
eb22d84b | 379 | }; |
eb22d84b | 380 | struct unimapinit adv = {}; |
ff452e76 | 381 | struct unimapdesc unimapd; |
6a08e1b0 | 382 | _cleanup_free_ struct unipair* unipairs = NULL; |
eb22d84b | 383 | _cleanup_free_ void *fontbuf = NULL; |
64fe2aea | 384 | int log_level = LOG_WARNING; |
1142bed2 | 385 | int r; |
dd04aac9 | 386 | |
6a08e1b0 | 387 | unipairs = new(struct unipair, USHRT_MAX); |
64fe2aea ZJS |
388 | if (!unipairs) |
389 | return (void) log_oom(); | |
0ef1adf5 | 390 | |
5297577f | 391 | /* get metadata of the current font (width, height, count) */ |
1142bed2 | 392 | r = ioctl(src_fd, KDFONTOP, &cfo); |
0ef1adf5 FB |
393 | if (r < 0) { |
394 | /* We might be called to operate on the dummy console (to setup keymap | |
395 | * mainly) when fbcon deferred takeover is used for example. In such case, | |
396 | * setting font is not supported and is expected to fail. */ | |
397 | if (errno == ENOSYS) | |
398 | log_level = LOG_DEBUG; | |
399 | ||
400 | log_full_errno(log_level, errno, | |
401 | "KD_FONT_OP_GET failed while trying to get the font metadata: %m"); | |
402 | } else { | |
a5eebcff | 403 | /* verify parameter sanity first */ |
5297577f MS |
404 | if (cfo.width > 32 || cfo.height > 32 || cfo.charcount > 512) |
405 | log_warning("Invalid font metadata - width: %u (max 32), height: %u (max 32), count: %u (max 512)", | |
a5eebcff | 406 | cfo.width, cfo.height, cfo.charcount); |
5297577f MS |
407 | else { |
408 | /* | |
409 | * Console fonts supported by the kernel are limited in size to 32 x 32 and maximum 512 | |
410 | * characters. Thus with 1 bit per pixel it requires up to 65536 bytes. The height always | |
5238e957 | 411 | * requires 32 per glyph, regardless of the actual height - see the comment above #define |
5297577f MS |
412 | * max_font_size 65536 in drivers/tty/vt/vt.c for more details. |
413 | */ | |
8419d457 | 414 | fontbuf = malloc_multiply((cfo.width + 7) / 8 * 32, cfo.charcount); |
5297577f MS |
415 | if (!fontbuf) { |
416 | log_oom(); | |
417 | return; | |
418 | } | |
1142bed2 | 419 | /* get fonts from the source console */ |
5297577f | 420 | cfo.data = fontbuf; |
1142bed2 | 421 | r = ioctl(src_fd, KDFONTOP, &cfo); |
5297577f MS |
422 | if (r < 0) |
423 | log_warning_errno(errno, "KD_FONT_OP_GET failed while trying to read the font data: %m"); | |
424 | else { | |
425 | unimapd.entries = unipairs; | |
426 | unimapd.entry_ct = USHRT_MAX; | |
1142bed2 | 427 | r = ioctl(src_fd, GIO_UNIMAP, &unimapd); |
5297577f MS |
428 | if (r < 0) |
429 | log_warning_errno(errno, "GIO_UNIMAP failed while trying to read unicode mappings: %m"); | |
430 | else | |
431 | cfo.op = KD_FONT_OP_SET; | |
432 | } | |
433 | } | |
eb22d84b MS |
434 | } |
435 | ||
a5eebcff | 436 | if (cfo.op != KD_FONT_OP_SET) |
0ef1adf5 | 437 | log_full(log_level, "Fonts will not be copied to remaining consoles"); |
eb22d84b | 438 | |
64fe2aea | 439 | for (unsigned i = 1; i <= 63; i++) { |
1142bed2 | 440 | char ttyname[sizeof("/dev/tty63")]; |
254d1313 | 441 | _cleanup_close_ int fd_d = -EBADF; |
dd04aac9 | 442 | |
1142bed2 | 443 | if (i == src_idx || verify_vc_allocation(i) < 0) |
dd04aac9 KS |
444 | continue; |
445 | ||
eb22d84b | 446 | /* try to open terminal */ |
1142bed2 MS |
447 | xsprintf(ttyname, "/dev/tty%u", i); |
448 | fd_d = open_terminal(ttyname, O_RDWR|O_CLOEXEC|O_NOCTTY); | |
eb22d84b | 449 | if (fd_d < 0) { |
1142bed2 | 450 | log_warning_errno(fd_d, "Unable to open tty%u, fonts will not be copied: %m", i); |
dd04aac9 | 451 | continue; |
eb22d84b | 452 | } |
dd04aac9 | 453 | |
26382cab | 454 | if (verify_vc_kbmode(fd_d) < 0) |
dd04aac9 KS |
455 | continue; |
456 | ||
e8d1d6e7 | 457 | (void) toggle_utf8_vc(ttyname, fd_d, utf8); |
eb22d84b MS |
458 | |
459 | if (cfo.op != KD_FONT_OP_SET) | |
460 | continue; | |
461 | ||
462 | r = ioctl(fd_d, KDFONTOP, &cfo); | |
463 | if (r < 0) { | |
d610d201 FB |
464 | int last_errno, mode; |
465 | ||
466 | /* The fonts couldn't have been copied. It might be due to the | |
467 | * terminal being in graphical mode. In this case the kernel | |
468 | * returns -EINVAL which is too generic for distinguishing this | |
469 | * specific case. So we need to retrieve the terminal mode and if | |
470 | * the graphical mode is in used, let's assume that something else | |
471 | * is using the terminal and the failure was expected as we | |
472 | * shouldn't have tried to copy the fonts. */ | |
473 | ||
474 | last_errno = errno; | |
475 | if (ioctl(fd_d, KDGETMODE, &mode) >= 0 && mode != KD_TEXT) | |
476 | log_debug("KD_FONT_OP_SET skipped: tty%u is not in text mode", i); | |
477 | else | |
478 | log_warning_errno(last_errno, "KD_FONT_OP_SET failed, fonts will not be copied to tty%u: %m", i); | |
479 | ||
eb22d84b MS |
480 | continue; |
481 | } | |
ff452e76 | 482 | |
64fe2aea ZJS |
483 | /* Copy unicode translation table unimapd is a ushort count and a pointer |
484 | * to an array of struct unipair { ushort, ushort }. */ | |
eb22d84b | 485 | r = ioctl(fd_d, PIO_UNIMAPCLR, &adv); |
aaa709bb | 486 | if (r < 0) { |
1142bed2 | 487 | log_warning_errno(errno, "PIO_UNIMAPCLR failed, unimaps might be incorrect for tty%u: %m", i); |
aaa709bb | 488 | continue; |
ff452e76 | 489 | } |
aaa709bb ZJS |
490 | |
491 | r = ioctl(fd_d, PIO_UNIMAP, &unimapd); | |
492 | if (r < 0) { | |
1142bed2 | 493 | log_warning_errno(errno, "PIO_UNIMAP failed, unimaps might be incorrect for tty%u: %m", i); |
aaa709bb ZJS |
494 | continue; |
495 | } | |
496 | ||
497 | log_debug("Font and unimap successfully copied to %s", ttyname); | |
dd04aac9 KS |
498 | } |
499 | } | |
500 | ||
1142bed2 | 501 | static int find_source_vc(char **ret_path, unsigned *ret_idx) { |
c10d6bdb | 502 | int r, err = 0; |
1142bed2 | 503 | |
64fe2aea | 504 | _cleanup_free_ char *path = new(char, sizeof("/dev/tty63")); |
234519ae | 505 | if (!path) |
1142bed2 MS |
506 | return log_oom(); |
507 | ||
64fe2aea | 508 | for (unsigned i = 1; i <= 63; i++) { |
254d1313 | 509 | _cleanup_close_ int fd = -EBADF; |
1142bed2 MS |
510 | |
511 | r = verify_vc_allocation(i); | |
512 | if (r < 0) { | |
513 | if (!err) | |
514 | err = -r; | |
515 | continue; | |
516 | } | |
517 | ||
518 | sprintf(path, "/dev/tty%u", i); | |
519 | fd = open_terminal(path, O_RDWR|O_CLOEXEC|O_NOCTTY); | |
520 | if (fd < 0) { | |
521 | if (!err) | |
522 | err = -fd; | |
523 | continue; | |
524 | } | |
26382cab | 525 | r = verify_vc_kbmode(fd); |
1142bed2 MS |
526 | if (r < 0) { |
527 | if (!err) | |
528 | err = -r; | |
529 | continue; | |
530 | } | |
531 | ||
532 | /* all checks passed, return this one as a source console */ | |
533 | *ret_idx = i; | |
ae2a15bc | 534 | *ret_path = TAKE_PTR(path); |
c10d6bdb | 535 | return TAKE_FD(fd); |
1142bed2 MS |
536 | } |
537 | ||
538 | return log_error_errno(err, "No usable source console found: %m"); | |
539 | } | |
540 | ||
541 | static int verify_source_vc(char **ret_path, const char *src_vc) { | |
254d1313 | 542 | _cleanup_close_ int fd = -EBADF; |
c10d6bdb LP |
543 | char *path; |
544 | int r; | |
1142bed2 MS |
545 | |
546 | fd = open_terminal(src_vc, O_RDWR|O_CLOEXEC|O_NOCTTY); | |
547 | if (fd < 0) | |
548 | return log_error_errno(fd, "Failed to open %s: %m", src_vc); | |
549 | ||
550 | r = verify_vc_device(fd); | |
551 | if (r < 0) | |
552 | return log_error_errno(r, "Device %s is not a virtual console: %m", src_vc); | |
553 | ||
554 | r = verify_vc_allocation_byfd(fd); | |
555 | if (r < 0) | |
556 | return log_error_errno(r, "Virtual console %s is not allocated: %m", src_vc); | |
557 | ||
26382cab | 558 | r = verify_vc_kbmode(fd); |
1142bed2 MS |
559 | if (r < 0) |
560 | return log_error_errno(r, "Virtual console %s is not in K_XLATE or K_UNICODE: %m", src_vc); | |
561 | ||
562 | path = strdup(src_vc); | |
234519ae | 563 | if (!path) |
1142bed2 MS |
564 | return log_oom(); |
565 | ||
566 | *ret_path = path; | |
c10d6bdb | 567 | return TAKE_FD(fd); |
1142bed2 MS |
568 | } |
569 | ||
97c4a07d | 570 | int main(int argc, char **argv) { |
8886ca62 YW |
571 | _cleanup_(context_done) Context c = {}; |
572 | _cleanup_free_ char *vc = NULL; | |
254d1313 | 573 | _cleanup_close_ int fd = -EBADF; |
1142bed2 MS |
574 | bool utf8, keyboard_ok; |
575 | unsigned idx = 0; | |
93c9a9d2 | 576 | int r; |
97c4a07d | 577 | |
d2acb93d | 578 | log_setup(); |
97c4a07d | 579 | |
4c12626c LP |
580 | umask(0022); |
581 | ||
97c4a07d | 582 | if (argv[1]) |
1142bed2 MS |
583 | fd = verify_source_vc(&vc, argv[1]); |
584 | else | |
585 | fd = find_source_vc(&vc, &idx); | |
1142bed2 | 586 | if (fd < 0) |
abee28c5 | 587 | return EXIT_FAILURE; |
03044059 | 588 | |
653ab83b | 589 | utf8 = is_locale_utf8(); |
97c4a07d | 590 | |
8886ca62 | 591 | context_load_config(&c); |
2034ec42 | 592 | |
a0043bfa ZJS |
593 | /* Take lock around the remaining operation to avoid being interrupted by a tty reset operation |
594 | * performed for services with TTYVHangup=yes. */ | |
595 | r = lock_generic(fd, LOCK_BSD, LOCK_EX); | |
596 | if (r < 0) { | |
597 | log_error_errno(r, "Failed to lock console: %m"); | |
598 | return EXIT_FAILURE; | |
599 | } | |
600 | ||
31390963 | 601 | (void) toggle_utf8_sysfs(utf8); |
e8d1d6e7 | 602 | (void) toggle_utf8_vc(vc, fd, utf8); |
93c9a9d2 | 603 | |
dfc55e34 YW |
604 | r = font_load_and_wait(vc, &c); |
605 | keyboard_ok = keyboard_load_and_wait(vc, &c, utf8) == 0; | |
97c4a07d | 606 | |
1142bed2 | 607 | if (idx > 0) { |
93c9a9d2 | 608 | if (r == 0) |
1142bed2 | 609 | setup_remaining_vcs(fd, idx, utf8); |
93c9a9d2 ZJS |
610 | else if (r == EX_OSERR) |
611 | /* setfont returns EX_OSERR when ioctl(KDFONTOP/PIO_FONTX/PIO_FONTX) fails. | |
612 | * This might mean various things, but in particular lack of a graphical | |
613 | * console. Let's be generous and not treat this as an error. */ | |
614 | log_notice("Setting fonts failed with a \"system error\", ignoring."); | |
eb22d84b | 615 | else |
aaa709bb | 616 | log_warning("Setting source virtual console failed, ignoring remaining ones"); |
eb22d84b | 617 | } |
97c4a07d | 618 | |
93c9a9d2 | 619 | return IN_SET(r, 0, EX_OSERR) && keyboard_ok ? EXIT_SUCCESS : EXIT_FAILURE; |
97c4a07d | 620 | } |