| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
/*** |
| 3 |
Copyright © 2016 Michal Soltys <[email protected]> |
| 4 |
***/ |
| 5 |
|
| 6 |
#include <fcntl.h> |
| 7 |
#include <linux/kd.h> |
| 8 |
#include <linux/tiocl.h> |
| 9 |
#include <linux/vt.h> |
| 10 |
#include <sys/ioctl.h> |
| 11 |
#include <sys/stat.h> |
| 12 |
#include <sysexits.h> |
| 13 |
#include <termios.h> |
| 14 |
#include <unistd.h> |
| 15 |
|
| 16 |
#include "alloc-util.h" |
| 17 |
#include "creds-util.h" |
| 18 |
#include "env-file.h" |
| 19 |
#include "errno-util.h" |
| 20 |
#include "fd-util.h" |
| 21 |
#include "fileio.h" |
| 22 |
#include "io-util.h" |
| 23 |
#include "locale-util.h" |
| 24 |
#include "log.h" |
| 25 |
#include "main-func.h" |
| 26 |
#include "pidref.h" |
| 27 |
#include "proc-cmdline.h" |
| 28 |
#include "process-util.h" |
| 29 |
#include "stdio-util.h" |
| 30 |
#include "string-util.h" |
| 31 |
#include "strv.h" |
| 32 |
#include "terminal-util.h" |
| 33 |
#include "vconsole-util.h" |
| 34 |
|
| 35 |
typedef struct Context { |
| 36 |
char *keymap; |
| 37 |
char *keymap_toggle; |
| 38 |
char *font; |
| 39 |
char *font_map; |
| 40 |
char *font_unimap; |
| 41 |
} Context; |
| 42 |
|
| 43 |
static void context_done(Context *c) { |
| 44 |
assert(c); |
| 45 |
|
| 46 |
free(c->keymap); |
| 47 |
free(c->keymap_toggle); |
| 48 |
free(c->font); |
| 49 |
free(c->font_map); |
| 50 |
free(c->font_unimap); |
| 51 |
} |
| 52 |
|
| 53 |
#define context_merge(dst, src, src_compat, name) \ |
| 54 |
({ \ |
| 55 |
if (src->name) \ |
| 56 |
free_and_replace(dst->name, src->name); \ |
| 57 |
else if (src_compat && src_compat->name) \ |
| 58 |
free_and_replace(dst->name, src_compat->name); \ |
| 59 |
}) |
| 60 |
|
| 61 |
static void context_merge_config( |
| 62 |
Context *dst, |
| 63 |
Context *src, |
| 64 |
Context *src_compat) { |
| 65 |
|
| 66 |
assert(dst); |
| 67 |
assert(src); |
| 68 |
|
| 69 |
context_merge(dst, src, src_compat, keymap); |
| 70 |
context_merge(dst, src, src_compat, keymap_toggle); |
| 71 |
context_merge(dst, src, src_compat, font); |
| 72 |
context_merge(dst, src, src_compat, font_map); |
| 73 |
context_merge(dst, src, src_compat, font_unimap); |
| 74 |
} |
| 75 |
|
| 76 |
static int context_read_efi(Context *c) { |
| 77 |
_cleanup_(context_done) Context v = {}; |
| 78 |
int r; |
| 79 |
|
| 80 |
assert(c); |
| 81 |
|
| 82 |
r = vconsole_keymap_from_efi(&v.keymap); |
| 83 |
if (r < 0) |
| 84 |
return r; |
| 85 |
if (r == 0) { |
| 86 |
log_debug("No vconsole keymap matches firmware-provided keyboard layout, ignoring."); |
| 87 |
return 0; |
| 88 |
} |
| 89 |
|
| 90 |
context_merge_config(c, &v, /* src_compat= */ NULL); |
| 91 |
return 0; |
| 92 |
} |
| 93 |
|
| 94 |
static int context_read_creds(Context *c) { |
| 95 |
_cleanup_(context_done) Context v = {}; |
| 96 |
int r; |
| 97 |
|
| 98 |
assert(c); |
| 99 |
|
| 100 |
r = read_credential_strings_many( |
| 101 |
"vconsole.keymap", &v.keymap, |
| 102 |
"vconsole.keymap_toggle", &v.keymap_toggle, |
| 103 |
"vconsole.font", &v.font, |
| 104 |
"vconsole.font_map", &v.font_map, |
| 105 |
"vconsole.font_unimap", &v.font_unimap); |
| 106 |
if (r < 0) |
| 107 |
log_warning_errno(r, "Failed to import credentials, ignoring: %m"); |
| 108 |
|
| 109 |
context_merge_config(c, &v, NULL); |
| 110 |
return 0; |
| 111 |
} |
| 112 |
|
| 113 |
static int context_read_env(Context *c) { |
| 114 |
_cleanup_(context_done) Context v = {}; |
| 115 |
int r; |
| 116 |
|
| 117 |
assert(c); |
| 118 |
|
| 119 |
r = parse_env_file( |
| 120 |
NULL, "/etc/vconsole.conf", |
| 121 |
"KEYMAP", &v.keymap, |
| 122 |
"KEYMAP_TOGGLE", &v.keymap_toggle, |
| 123 |
"FONT", &v.font, |
| 124 |
"FONT_MAP", &v.font_map, |
| 125 |
"FONT_UNIMAP", &v.font_unimap); |
| 126 |
if (r < 0) { |
| 127 |
if (r != -ENOENT) |
| 128 |
log_warning_errno(r, "Failed to read /etc/vconsole.conf, ignoring: %m"); |
| 129 |
return r; |
| 130 |
} |
| 131 |
|
| 132 |
context_merge_config(c, &v, NULL); |
| 133 |
return 0; |
| 134 |
} |
| 135 |
|
| 136 |
static int context_read_proc_cmdline(Context *c) { |
| 137 |
_cleanup_(context_done) Context v = {}, w = {}; |
| 138 |
int r; |
| 139 |
|
| 140 |
assert(c); |
| 141 |
|
| 142 |
r = proc_cmdline_get_key_many( |
| 143 |
PROC_CMDLINE_STRIP_RD_PREFIX, |
| 144 |
"vconsole.keymap", &v.keymap, |
| 145 |
"vconsole.keymap_toggle", &v.keymap_toggle, |
| 146 |
"vconsole.font", &v.font, |
| 147 |
"vconsole.font_map", &v.font_map, |
| 148 |
"vconsole.font_unimap", &v.font_unimap, |
| 149 |
/* compatibility with obsolete multiple-dot scheme */ |
| 150 |
"vconsole.keymap.toggle", &w.keymap_toggle, |
| 151 |
"vconsole.font.map", &w.font_map, |
| 152 |
"vconsole.font.unimap", &w.font_unimap); |
| 153 |
if (r < 0) { |
| 154 |
if (r != -ENOENT) |
| 155 |
log_warning_errno(r, "Failed to read /proc/cmdline, ignoring: %m"); |
| 156 |
return r; |
| 157 |
} |
| 158 |
|
| 159 |
context_merge_config(c, &v, &w); |
| 160 |
return 0; |
| 161 |
} |
| 162 |
|
| 163 |
static void context_load_config(Context *c) { |
| 164 |
assert(c); |
| 165 |
|
| 166 |
/* Pick up the firmware-provided keyboard layout if any (lowest priority) */ |
| 167 |
(void) context_read_efi(c); |
| 168 |
|
| 169 |
/* Load data from credentials */ |
| 170 |
(void) context_read_creds(c); |
| 171 |
|
| 172 |
/* Load data from configuration file */ |
| 173 |
(void) context_read_env(c); |
| 174 |
|
| 175 |
/* Let the kernel command line override /etc/vconsole.conf (highest priority) */ |
| 176 |
(void) context_read_proc_cmdline(c); |
| 177 |
} |
| 178 |
|
| 179 |
static int verify_vc_device(int fd) { |
| 180 |
assert(fd >= 0); |
| 181 |
|
| 182 |
unsigned char data[] = { |
| 183 |
TIOCL_GETFGCONSOLE, |
| 184 |
}; |
| 185 |
|
| 186 |
return RET_NERRNO(ioctl(fd, TIOCLINUX, data)); |
| 187 |
} |
| 188 |
|
| 189 |
static int verify_vc_allocation(unsigned idx) { |
| 190 |
char vcname[sizeof("/dev/vcs") + DECIMAL_STR_MAX(unsigned) - 2]; |
| 191 |
|
| 192 |
xsprintf(vcname, "/dev/vcs%u", idx); |
| 193 |
|
| 194 |
return RET_NERRNO(access(vcname, F_OK)); |
| 195 |
} |
| 196 |
|
| 197 |
static int verify_vc_allocation_byfd(int fd) { |
| 198 |
assert(fd >= 0); |
| 199 |
|
| 200 |
struct vt_stat vcs = {}; |
| 201 |
if (ioctl(fd, VT_GETSTATE, &vcs) < 0) |
| 202 |
return -errno; |
| 203 |
|
| 204 |
return verify_vc_allocation(vcs.v_active); |
| 205 |
} |
| 206 |
|
| 207 |
static int verify_vc_kbmode(int fd) { |
| 208 |
int curr_mode; |
| 209 |
|
| 210 |
assert(fd >= 0); |
| 211 |
|
| 212 |
/* |
| 213 |
* Make sure we only adjust consoles in K_XLATE or K_UNICODE mode. |
| 214 |
* Otherwise we would (likely) interfere with X11's processing of the |
| 215 |
* key events. |
| 216 |
* |
| 217 |
* https://lists.freedesktop.org/archives/systemd-devel/2013-February/008573.html |
| 218 |
*/ |
| 219 |
|
| 220 |
if (ioctl(fd, KDGKBMODE, &curr_mode) < 0) |
| 221 |
return -errno; |
| 222 |
|
| 223 |
return IN_SET(curr_mode, K_XLATE, K_UNICODE) ? 0 : -EBUSY; |
| 224 |
} |
| 225 |
|
| 226 |
static int verify_vc_display_mode(int fd) { |
| 227 |
int mode; |
| 228 |
|
| 229 |
assert(fd >= 0); |
| 230 |
|
| 231 |
/* Similarly the vc is likely busy if it is in KD_GRAPHICS mode. If it's not the case and it's been |
| 232 |
* left in graphics mode, the kernel will refuse to operate on the font settings anyway. */ |
| 233 |
|
| 234 |
if (ioctl(fd, KDGETMODE, &mode) < 0) |
| 235 |
return -errno; |
| 236 |
|
| 237 |
return mode != KD_TEXT ? -EBUSY : 0; |
| 238 |
} |
| 239 |
|
| 240 |
static int verify_vc_support_font(int fd) { |
| 241 |
struct console_font_op cfo = { |
| 242 |
.op = KD_FONT_OP_GET, |
| 243 |
.width = UINT_MAX, |
| 244 |
.height = UINT_MAX, |
| 245 |
.charcount = UINT_MAX, |
| 246 |
}; |
| 247 |
|
| 248 |
assert(fd >= 0); |
| 249 |
|
| 250 |
if (ioctl(fd, KDFONTOP, &cfo) < 0) |
| 251 |
return ERRNO_IS_NOT_SUPPORTED(errno) ? 0 : -errno; |
| 252 |
|
| 253 |
return 1; |
| 254 |
} |
| 255 |
|
| 256 |
static int toggle_utf8_vc(const char *name, int fd, bool utf8) { |
| 257 |
int r; |
| 258 |
struct termios tc = {}; |
| 259 |
|
| 260 |
assert(name); |
| 261 |
assert(fd >= 0); |
| 262 |
|
| 263 |
r = ioctl(fd, KDSKBMODE, utf8 ? K_UNICODE : K_XLATE); |
| 264 |
if (r < 0) |
| 265 |
return log_warning_errno(errno, "Failed to %s UTF-8 kbdmode on %s: %m", enable_disable(utf8), name); |
| 266 |
|
| 267 |
r = loop_write(fd, utf8 ? "\033%G" : "\033%@", SIZE_MAX); |
| 268 |
if (r < 0) |
| 269 |
return log_warning_errno(r, "Failed to %s UTF-8 term processing on %s: %m", enable_disable(utf8), name); |
| 270 |
|
| 271 |
r = tcgetattr(fd, &tc); |
| 272 |
if (r >= 0) { |
| 273 |
SET_FLAG(tc.c_iflag, IUTF8, utf8); |
| 274 |
r = tcsetattr(fd, TCSANOW, &tc); |
| 275 |
} |
| 276 |
if (r < 0) |
| 277 |
return log_warning_errno(errno, "Failed to %s iutf8 flag on %s: %m", enable_disable(utf8), name); |
| 278 |
|
| 279 |
log_debug("UTF-8 kbdmode %sd on %s", enable_disable(utf8), name); |
| 280 |
return 0; |
| 281 |
} |
| 282 |
|
| 283 |
static int toggle_utf8_sysfs(bool utf8) { |
| 284 |
int r; |
| 285 |
|
| 286 |
r = write_string_file("/sys/module/vt/parameters/default_utf8", one_zero(utf8), WRITE_STRING_FILE_DISABLE_BUFFER); |
| 287 |
if (r < 0) |
| 288 |
return log_warning_errno(r, "Failed to %s sysfs UTF-8 flag: %m", enable_disable(utf8)); |
| 289 |
|
| 290 |
log_debug("Sysfs UTF-8 flag %sd", enable_disable(utf8)); |
| 291 |
return 0; |
| 292 |
} |
| 293 |
|
| 294 |
/* SYSTEMD_DEFAULT_KEYMAP must not be empty */ |
| 295 |
assert_cc(STRLEN(SYSTEMD_DEFAULT_KEYMAP) > 0); |
| 296 |
|
| 297 |
static int keyboard_load_and_wait(const char *vc, Context *c, bool utf8) { |
| 298 |
const char* args[8]; |
| 299 |
unsigned i = 0; |
| 300 |
int r; |
| 301 |
|
| 302 |
assert(vc); |
| 303 |
assert(c); |
| 304 |
|
| 305 |
const char |
| 306 |
*keymap = empty_to_null(c->keymap) ?: SYSTEMD_DEFAULT_KEYMAP, |
| 307 |
*keymap_toggle = empty_to_null(c->keymap_toggle); |
| 308 |
|
| 309 |
if (streq(keymap, "@kernel")) |
| 310 |
return 0; |
| 311 |
|
| 312 |
if (access(KBD_LOADKEYS, X_OK) < 0) { |
| 313 |
if (errno != ENOENT) |
| 314 |
return log_error_errno(errno, "Failed to check if '" KBD_LOADKEYS "' is available: %m"); |
| 315 |
|
| 316 |
log_notice("'" KBD_LOADKEYS "' is not available, skipping keyboard mapping setup."); |
| 317 |
return 0; /* Report that we skipped this */ |
| 318 |
} |
| 319 |
|
| 320 |
args[i++] = KBD_LOADKEYS; |
| 321 |
args[i++] = "-q"; |
| 322 |
args[i++] = "-C"; |
| 323 |
args[i++] = vc; |
| 324 |
if (utf8) |
| 325 |
args[i++] = "-u"; |
| 326 |
args[i++] = keymap; |
| 327 |
if (keymap_toggle) |
| 328 |
args[i++] = keymap_toggle; |
| 329 |
args[i++] = NULL; |
| 330 |
|
| 331 |
if (DEBUG_LOGGING) { |
| 332 |
_cleanup_free_ char *cmd = NULL; |
| 333 |
|
| 334 |
cmd = strv_join((char**) args, " "); |
| 335 |
log_debug("Executing \"%s\"...", strnull(cmd)); |
| 336 |
} |
| 337 |
|
| 338 |
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; |
| 339 |
r = pidref_safe_fork("(loadkeys)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG, &pidref); |
| 340 |
if (r < 0) |
| 341 |
return r; |
| 342 |
if (r == 0) { |
| 343 |
execv(args[0], (char **) args); |
| 344 |
_exit(EXIT_FAILURE); |
| 345 |
} |
| 346 |
|
| 347 |
r = pidref_wait_for_terminate_and_check(KBD_LOADKEYS, &pidref, WAIT_LOG); |
| 348 |
if (r < 0) |
| 349 |
return r; |
| 350 |
if (r != EXIT_SUCCESS) |
| 351 |
return -EPROTO; |
| 352 |
|
| 353 |
return 1; /* Report that we did something */ |
| 354 |
} |
| 355 |
|
| 356 |
static int font_load_and_wait(int fd, const char *vc, Context *c) { |
| 357 |
const char* args[9]; |
| 358 |
unsigned i = 0; |
| 359 |
int r; |
| 360 |
|
| 361 |
assert(vc); |
| 362 |
assert(c); |
| 363 |
|
| 364 |
const char |
| 365 |
*font = empty_to_null(c->font), |
| 366 |
*font_map = empty_to_null(c->font_map), |
| 367 |
*font_unimap = empty_to_null(c->font_unimap); |
| 368 |
|
| 369 |
/* Any part can be set independently */ |
| 370 |
if (!font && !font_map && !font_unimap) |
| 371 |
return 0; |
| 372 |
|
| 373 |
if (access(KBD_SETFONT, X_OK) < 0) { |
| 374 |
if (errno != ENOENT) |
| 375 |
return log_error_errno(errno, "Failed to check if '" KBD_SETFONT "' is available: %m"); |
| 376 |
|
| 377 |
log_notice("'" KBD_SETFONT "' is not available, skipping console font setup."); |
| 378 |
return 0; /* Report that we skipped this */ |
| 379 |
} |
| 380 |
|
| 381 |
/* May be called on the dummy console (e.g. during keymap setup with fbcon deferred takeover). Font |
| 382 |
* changes are not supported here and will fail. */ |
| 383 |
r = verify_vc_support_font(fd); |
| 384 |
if (r < 0) |
| 385 |
return log_error_errno(r, "Failed to check '%s' has font support: %m", vc); |
| 386 |
if (r == 0) { |
| 387 |
log_notice("'%s' has no font support, skipping.", vc); |
| 388 |
return 0; /* Report that we skipped this */ |
| 389 |
} |
| 390 |
|
| 391 |
args[i++] = KBD_SETFONT; |
| 392 |
args[i++] = "-C"; |
| 393 |
args[i++] = vc; |
| 394 |
if (font_map) { |
| 395 |
args[i++] = "-m"; |
| 396 |
args[i++] = font_map; |
| 397 |
} |
| 398 |
if (font_unimap) { |
| 399 |
args[i++] = "-u"; |
| 400 |
args[i++] = font_unimap; |
| 401 |
} |
| 402 |
if (font) |
| 403 |
args[i++] = font; |
| 404 |
args[i++] = NULL; |
| 405 |
|
| 406 |
if (DEBUG_LOGGING) { |
| 407 |
_cleanup_free_ char *cmd = NULL; |
| 408 |
|
| 409 |
cmd = strv_join((char**) args, " "); |
| 410 |
log_debug("Executing \"%s\"...", strnull(cmd)); |
| 411 |
} |
| 412 |
|
| 413 |
_cleanup_(pidref_done) PidRef pidref = PIDREF_NULL; |
| 414 |
r = pidref_safe_fork("(setfont)", FORK_RESET_SIGNALS|FORK_CLOSE_ALL_FDS|FORK_RLIMIT_NOFILE_SAFE|FORK_LOG, &pidref); |
| 415 |
if (r < 0) |
| 416 |
return r; |
| 417 |
if (r == 0) { |
| 418 |
execv(args[0], (char **) args); |
| 419 |
_exit(EXIT_FAILURE); |
| 420 |
} |
| 421 |
|
| 422 |
/* setfont returns EX_OSERR when ioctl(KDFONTOP/PIO_FONTX/PIO_FONTX) fails. Let's be generous and not |
| 423 |
* treat this as an error. */ |
| 424 |
r = pidref_wait_for_terminate_and_check(KBD_SETFONT, &pidref, WAIT_LOG_ABNORMAL); |
| 425 |
if (r < 0) |
| 426 |
return r; /* WAIT_LOG_ABNORMAL means we already have logged about these kinds of errors */ |
| 427 |
if (r == EX_OSERR) { |
| 428 |
log_notice(KBD_SETFONT " failed with a \"system error\" (EX_OSERR), ignoring."); |
| 429 |
return 0; /* Report that we skipped this */ |
| 430 |
} |
| 431 |
if (r != EXIT_SUCCESS) |
| 432 |
return log_error_errno(SYNTHETIC_ERRNO(EPROTO), KBD_SETFONT " failed with exit status %i.", r); |
| 433 |
|
| 434 |
return 1; /* Report that we did something */ |
| 435 |
} |
| 436 |
|
| 437 |
/* |
| 438 |
* A newly allocated VT uses the font from the source VT. Here we update all possibly already allocated VTs |
| 439 |
* with the configured font. It also allows systemd-vconsole-setup.service to be restarted to apply a new |
| 440 |
* font to all VTs. |
| 441 |
* |
| 442 |
* We also setup per-console utf8 related stuff: kbdmode, term processing, stty iutf8. |
| 443 |
*/ |
| 444 |
static void setup_remaining_vcs(int src_fd, unsigned src_idx, bool utf8) { |
| 445 |
struct console_font_op cfo = { |
| 446 |
.op = KD_FONT_OP_GET, |
| 447 |
.width = UINT_MAX, .height = UINT_MAX, |
| 448 |
.charcount = UINT_MAX, |
| 449 |
}; |
| 450 |
struct unimapinit adv = {}; |
| 451 |
struct unimapdesc unimapd; |
| 452 |
_cleanup_free_ struct unipair* unipairs = NULL; |
| 453 |
_cleanup_free_ void *fontbuf = NULL; |
| 454 |
int r; |
| 455 |
|
| 456 |
assert(src_fd >= 0); |
| 457 |
|
| 458 |
unipairs = new(struct unipair, USHRT_MAX); |
| 459 |
if (!unipairs) |
| 460 |
return (void) log_oom(); |
| 461 |
|
| 462 |
/* get metadata of the current font (width, height, count) */ |
| 463 |
r = ioctl(src_fd, KDFONTOP, &cfo); |
| 464 |
if (r < 0) { |
| 465 |
log_warning_errno(errno, "KD_FONT_OP_GET failed while trying to get the font metadata: %m"); |
| 466 |
} else { |
| 467 |
/* verify parameter sanity first */ |
| 468 |
if (cfo.width > 32 || cfo.height > 32 || cfo.charcount > 512) |
| 469 |
log_warning("Invalid font metadata - width: %u (max 32), height: %u (max 32), count: %u (max 512)", |
| 470 |
cfo.width, cfo.height, cfo.charcount); |
| 471 |
else { |
| 472 |
/* |
| 473 |
* Console fonts supported by the kernel are limited in size to 32 x 32 and maximum 512 |
| 474 |
* characters. Thus with 1 bit per pixel it requires up to 65536 bytes. The height always |
| 475 |
* requires 32 per glyph, regardless of the actual height - see the comment above #define |
| 476 |
* max_font_size 65536 in drivers/tty/vt/vt.c for more details. |
| 477 |
*/ |
| 478 |
fontbuf = malloc_multiply((cfo.width + 7) / 8 * 32, cfo.charcount); |
| 479 |
if (!fontbuf) { |
| 480 |
log_oom(); |
| 481 |
return; |
| 482 |
} |
| 483 |
/* get fonts from the source console */ |
| 484 |
cfo.data = fontbuf; |
| 485 |
r = ioctl(src_fd, KDFONTOP, &cfo); |
| 486 |
if (r < 0) |
| 487 |
log_warning_errno(errno, "KD_FONT_OP_GET failed while trying to read the font data: %m"); |
| 488 |
else { |
| 489 |
unimapd.entries = unipairs; |
| 490 |
unimapd.entry_ct = USHRT_MAX; |
| 491 |
r = ioctl(src_fd, GIO_UNIMAP, &unimapd); |
| 492 |
if (r < 0) |
| 493 |
log_warning_errno(errno, "GIO_UNIMAP failed while trying to read unicode mappings: %m"); |
| 494 |
else |
| 495 |
cfo.op = KD_FONT_OP_SET; |
| 496 |
} |
| 497 |
} |
| 498 |
} |
| 499 |
|
| 500 |
if (cfo.op != KD_FONT_OP_SET) |
| 501 |
log_warning("Fonts will not be copied to remaining consoles"); |
| 502 |
|
| 503 |
for (unsigned i = 1; i <= 63; i++) { |
| 504 |
char ttyname[sizeof("/dev/tty63")]; |
| 505 |
_cleanup_close_ int fd_d = -EBADF; |
| 506 |
|
| 507 |
if (i == src_idx || verify_vc_allocation(i) < 0) |
| 508 |
continue; |
| 509 |
|
| 510 |
/* try to open terminal */ |
| 511 |
xsprintf(ttyname, "/dev/tty%u", i); |
| 512 |
fd_d = open_terminal(ttyname, O_RDWR|O_CLOEXEC|O_NOCTTY); |
| 513 |
if (fd_d < 0) { |
| 514 |
log_warning_errno(fd_d, "Unable to open tty%u, fonts will not be copied: %m", i); |
| 515 |
continue; |
| 516 |
} |
| 517 |
|
| 518 |
if (verify_vc_kbmode(fd_d) < 0) |
| 519 |
continue; |
| 520 |
|
| 521 |
(void) toggle_utf8_vc(ttyname, fd_d, utf8); |
| 522 |
|
| 523 |
if (cfo.op != KD_FONT_OP_SET) |
| 524 |
continue; |
| 525 |
|
| 526 |
r = verify_vc_display_mode(fd_d); |
| 527 |
if (r < 0) { |
| 528 |
log_debug_errno(r, "KD_FONT_OP_SET skipped: tty%u is not in text mode", i); |
| 529 |
continue; |
| 530 |
} |
| 531 |
|
| 532 |
if (ioctl(fd_d, KDFONTOP, &cfo) < 0) { |
| 533 |
log_warning_errno(errno, "KD_FONT_OP_SET failed, fonts will not be copied to tty%u: %m", i); |
| 534 |
continue; |
| 535 |
} |
| 536 |
|
| 537 |
/* Copy unicode translation table unimapd is a ushort count and a pointer |
| 538 |
* to an array of struct unipair { ushort, ushort }. */ |
| 539 |
r = ioctl(fd_d, PIO_UNIMAPCLR, &adv); |
| 540 |
if (r < 0) { |
| 541 |
log_warning_errno(errno, "PIO_UNIMAPCLR failed, unimaps might be incorrect for tty%u: %m", i); |
| 542 |
continue; |
| 543 |
} |
| 544 |
|
| 545 |
r = ioctl(fd_d, PIO_UNIMAP, &unimapd); |
| 546 |
if (r < 0) { |
| 547 |
log_warning_errno(errno, "PIO_UNIMAP failed, unimaps might be incorrect for tty%u: %m", i); |
| 548 |
continue; |
| 549 |
} |
| 550 |
|
| 551 |
log_debug("Font and unimap successfully copied to %s", ttyname); |
| 552 |
} |
| 553 |
} |
| 554 |
|
| 555 |
static int find_source_vc(char **ret_path, unsigned *ret_idx) { |
| 556 |
int r, err = 0; |
| 557 |
|
| 558 |
assert(ret_path); |
| 559 |
assert(ret_idx); |
| 560 |
|
| 561 |
/* This function returns an fd when it finds a candidate. When it fails, it returns the first error |
| 562 |
* that occurred when the VC was being opened or -EBUSY when it finds some VCs but all are busy |
| 563 |
* otherwise -ENOENT when there is no allocated VC. */ |
| 564 |
|
| 565 |
for (unsigned i = 1; i <= 63; i++) { |
| 566 |
_cleanup_close_ int fd = -EBADF; |
| 567 |
_cleanup_free_ char *path = NULL; |
| 568 |
|
| 569 |
/* We save the first error but we give less importance for the case where we previously fail |
| 570 |
* due to the VCs being not allocated. Similarly errors on opening a device has a higher |
| 571 |
* priority than errors due to devices either not allocated or busy. */ |
| 572 |
|
| 573 |
r = verify_vc_allocation(i); |
| 574 |
if (r < 0) { |
| 575 |
RET_GATHER(err, log_debug_errno(r, "VC %u existence check failed, skipping: %m", i)); |
| 576 |
continue; |
| 577 |
} |
| 578 |
|
| 579 |
if (asprintf(&path, "/dev/tty%u", i) < 0) |
| 580 |
return log_oom(); |
| 581 |
|
| 582 |
fd = open_terminal(path, O_RDWR|O_CLOEXEC|O_NOCTTY); |
| 583 |
if (fd < 0) { |
| 584 |
log_debug_errno(fd, "Failed to open terminal %s, ignoring: %m", path); |
| 585 |
if (IN_SET(err, 0, -EBUSY, -ENOENT)) |
| 586 |
err = fd; |
| 587 |
continue; |
| 588 |
} |
| 589 |
|
| 590 |
r = verify_vc_kbmode(fd); |
| 591 |
if (r < 0) { |
| 592 |
log_debug_errno(r, "Failed to check VC %s keyboard mode: %m", path); |
| 593 |
if (IN_SET(err, 0, -ENOENT)) |
| 594 |
err = r; |
| 595 |
continue; |
| 596 |
} |
| 597 |
|
| 598 |
r = verify_vc_display_mode(fd); |
| 599 |
if (r < 0) { |
| 600 |
log_debug_errno(r, "Failed to check VC %s display mode: %m", path); |
| 601 |
if (IN_SET(err, 0, -ENOENT)) |
| 602 |
err = r; |
| 603 |
continue; |
| 604 |
} |
| 605 |
|
| 606 |
log_debug("Selecting %s as source console", path); |
| 607 |
|
| 608 |
/* all checks passed, return this one as a source console */ |
| 609 |
*ret_idx = i; |
| 610 |
*ret_path = TAKE_PTR(path); |
| 611 |
return TAKE_FD(fd); |
| 612 |
} |
| 613 |
|
| 614 |
return err; |
| 615 |
} |
| 616 |
|
| 617 |
static int verify_source_vc(char **ret_path, const char *src_vc) { |
| 618 |
_cleanup_close_ int fd = -EBADF; |
| 619 |
char *path; |
| 620 |
int r; |
| 621 |
|
| 622 |
assert(ret_path); |
| 623 |
|
| 624 |
fd = open_terminal(src_vc, O_RDWR|O_CLOEXEC|O_NOCTTY); |
| 625 |
if (fd < 0) |
| 626 |
return log_error_errno(fd, "Failed to open %s: %m", src_vc); |
| 627 |
|
| 628 |
r = verify_vc_device(fd); |
| 629 |
if (r < 0) |
| 630 |
return log_error_errno(r, "Device %s is not a virtual console: %m", src_vc); |
| 631 |
|
| 632 |
r = verify_vc_allocation_byfd(fd); |
| 633 |
if (r < 0) |
| 634 |
return log_error_errno(r, "Virtual console %s is not allocated: %m", src_vc); |
| 635 |
|
| 636 |
r = verify_vc_kbmode(fd); |
| 637 |
if (r < 0) |
| 638 |
return log_error_errno(r, "Virtual console %s is not in K_XLATE or K_UNICODE: %m", src_vc); |
| 639 |
|
| 640 |
/* setfont(8) silently ignores when the font can't be applied due to the vc being in |
| 641 |
* KD_GRAPHICS. Hence we continue to accept this case however we now let the user know that the vc |
| 642 |
* will be initialized only partially. */ |
| 643 |
r = verify_vc_display_mode(fd); |
| 644 |
if (r < 0) |
| 645 |
log_notice_errno(r, "Virtual console %s is not in KD_TEXT, font settings likely won't be applied.", src_vc); |
| 646 |
|
| 647 |
path = strdup(src_vc); |
| 648 |
if (!path) |
| 649 |
return log_oom(); |
| 650 |
|
| 651 |
*ret_path = path; |
| 652 |
return TAKE_FD(fd); |
| 653 |
} |
| 654 |
|
| 655 |
static int run(int argc, char **argv) { |
| 656 |
_cleanup_(context_done) Context c = {}; |
| 657 |
_cleanup_free_ char *vc = NULL; |
| 658 |
_cleanup_close_ int fd = -EBADF, lock_fd = -EBADF; |
| 659 |
bool utf8; |
| 660 |
unsigned idx = 0; |
| 661 |
|
| 662 |
log_setup(); |
| 663 |
|
| 664 |
umask(0022); |
| 665 |
|
| 666 |
if (argv[1]) { |
| 667 |
fd = verify_source_vc(&vc, argv[1]); |
| 668 |
if (fd < 0) |
| 669 |
return fd; |
| 670 |
} else { |
| 671 |
fd = find_source_vc(&vc, &idx); |
| 672 |
if (fd < 0 && fd != -EBUSY) |
| 673 |
return log_error_errno(fd, "No virtual console that can be configured found: %m"); |
| 674 |
} |
| 675 |
|
| 676 |
utf8 = is_locale_utf8(); |
| 677 |
(void) toggle_utf8_sysfs(utf8); |
| 678 |
|
| 679 |
if (fd < 0) { |
| 680 |
/* We found only busy VCs, which might happen during the boot process when the boot splash is |
| 681 |
* displayed on the only allocated VC. In this case we don't interfere and avoid initializing |
| 682 |
* the VC partially as some operations are likely to fail. */ |
| 683 |
log_notice("All allocated virtual consoles are busy, will not configure key mapping and font."); |
| 684 |
return EXIT_SUCCESS; |
| 685 |
} |
| 686 |
|
| 687 |
context_load_config(&c); |
| 688 |
|
| 689 |
/* Take lock around the remaining operation to avoid being interrupted by a tty reset operation |
| 690 |
* performed for services with TTYVHangup=yes. */ |
| 691 |
lock_fd = lock_dev_console(); |
| 692 |
if (ERRNO_IS_NEG_DEVICE_ABSENT(lock_fd)) |
| 693 |
log_debug_errno(lock_fd, "Device /dev/console does not exist, proceeding without lock: %m"); |
| 694 |
else if (lock_fd < 0) |
| 695 |
log_warning_errno(lock_fd, "Failed to lock /dev/console, proceeding without lock: %m"); |
| 696 |
|
| 697 |
(void) toggle_utf8_vc(vc, fd, utf8); |
| 698 |
|
| 699 |
int setfont_status = font_load_and_wait(fd, vc, &c); |
| 700 |
int loadkeys_status = keyboard_load_and_wait(vc, &c, utf8); |
| 701 |
|
| 702 |
if (idx > 0) { |
| 703 |
if (setfont_status == 0) |
| 704 |
log_notice("Configuration of first virtual console was skipped, ignoring remaining ones."); |
| 705 |
else if (setfont_status < 0) |
| 706 |
log_warning("Configuration of first virtual console failed, ignoring remaining ones."); |
| 707 |
else |
| 708 |
setup_remaining_vcs(fd, idx, utf8); |
| 709 |
} |
| 710 |
|
| 711 |
return (setfont_status >= 0 && loadkeys_status >= 0) ? EXIT_SUCCESS : EXIT_FAILURE; |
| 712 |
} |
| 713 |
|
| 714 |
DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run); |