Skip to content
Permalink
Browse files

Merge pull request #12378 from rbalint/vt-kbd-reset-check

VT kbd reset check
  • Loading branch information...
poettering committed May 16, 2019
2 parents a32a008 + 13a43c7 commit 9725f1a10f80f5e0ae7d9b60547458622aeb322f
Showing with 36 additions and 21 deletions.
  1. +25 −1 src/basic/terminal-util.c
  2. +1 −0 src/basic/terminal-util.h
  3. +10 −20 src/vconsole/vconsole-setup.c
@@ -1249,12 +1249,36 @@ int vt_default_utf8(void) {
return parse_boolean(b);
}

int vt_verify_kbmode(int fd) {
int curr_mode;

/*
* Make sure we only adjust consoles in K_XLATE or K_UNICODE mode.
* Otherwise we would (likely) interfere with X11's processing of the
* key events.
*
* http://lists.freedesktop.org/archives/systemd-devel/2013-February/008573.html
*/

if (ioctl(fd, KDGKBMODE, &curr_mode) < 0)
return -errno;

return IN_SET(curr_mode, K_XLATE, K_UNICODE) ? 0 : -EBUSY;
}

int vt_reset_keyboard(int fd) {
int kb;
int kb, r;

/* If we can't read the default, then default to unicode. It's 2017 after all. */
kb = vt_default_utf8() != 0 ? K_UNICODE : K_XLATE;

r = vt_verify_kbmode(fd);
if (r == -EBUSY) {
log_debug_errno(r, "Keyboard is not in XLATE or UNICODE mode, not resetting: %m");
return 0;
} else if (r < 0)
return r;

if (ioctl(fd, KDSKBMODE, kb) < 0)
return -errno;

@@ -156,6 +156,7 @@ int openpt_in_namespace(pid_t pid, int flags);
int open_terminal_in_namespace(pid_t pid, const char *name, int mode);

int vt_default_utf8(void);
int vt_verify_kbmode(int fd);
int vt_reset_keyboard(int fd);
int vt_restore(int fd);
int vt_release(int fd, bool restore_vt);
@@ -70,29 +70,19 @@ static int verify_vc_allocation_byfd(int fd) {
return verify_vc_allocation(vcs.v_active);
}

static int verify_vc_kbmode(int fd) {
int curr_mode;

/*
* Make sure we only adjust consoles in K_XLATE or K_UNICODE mode.
* Otherwise we would (likely) interfere with X11's processing of the
* key events.
*
* http://lists.freedesktop.org/archives/systemd-devel/2013-February/008573.html
*/

if (ioctl(fd, KDGKBMODE, &curr_mode) < 0)
return -errno;

return IN_SET(curr_mode, K_XLATE, K_UNICODE) ? 0 : -EBUSY;
}

static int toggle_utf8(const char *name, int fd, bool utf8) {
int r;
struct termios tc = {};

assert(name);

r = vt_verify_kbmode(fd);
if (r == -EBUSY) {
log_warning_errno(r, "Virtual console %s is not in K_XLATE or K_UNICODE: %m", name);
return 0;
} else if (r < 0)
return log_warning_errno(r, "Failed to verify kbdmode on %s: %m", name);

r = ioctl(fd, KDSKBMODE, utf8 ? K_UNICODE : K_XLATE);
if (r < 0)
return log_warning_errno(errno, "Failed to %s UTF-8 kbdmode on %s: %m", enable_disable(utf8), name);
@@ -290,7 +280,7 @@ static void setup_remaining_vcs(int src_fd, unsigned src_idx, bool utf8) {
continue;
}

if (verify_vc_kbmode(fd_d) < 0)
if (vt_verify_kbmode(fd_d) < 0)
continue;

toggle_utf8(ttyname, fd_d, utf8);
@@ -365,7 +355,7 @@ static int find_source_vc(char **ret_path, unsigned *ret_idx) {
err = -fd;
continue;
}
r = verify_vc_kbmode(fd);
r = vt_verify_kbmode(fd);
if (r < 0) {
if (!err)
err = -r;
@@ -398,7 +388,7 @@ static int verify_source_vc(char **ret_path, const char *src_vc) {
if (r < 0)
return log_error_errno(r, "Virtual console %s is not allocated: %m", src_vc);

r = verify_vc_kbmode(fd);
r = vt_verify_kbmode(fd);
if (r < 0)
return log_error_errno(r, "Virtual console %s is not in K_XLATE or K_UNICODE: %m", src_vc);

0 comments on commit 9725f1a

Please sign in to comment.
You can’t perform that action at this time.