terminal-util: introduce vt_restore() helper

This commit is contained in:
Franck Bui 2018-08-27 22:33:44 +02:00
parent 0212126c45
commit 6179ede1c5
3 changed files with 39 additions and 12 deletions

View File

@ -1271,3 +1271,37 @@ int vt_reset_keyboard(int fd) {
return 0;
}
int vt_restore(int fd) {
static const struct vt_mode mode = {
.mode = VT_AUTO,
};
int r, q = 0;
r = ioctl(fd, KDSETMODE, KD_TEXT);
if (r < 0)
q = log_debug_errno(errno, "Failed to set VT in text mode, ignoring: %m");
r = vt_reset_keyboard(fd);
if (r < 0) {
log_debug_errno(r, "Failed to reset keyboard mode, ignoring: %m");
if (q >= 0)
q = r;
}
r = ioctl(fd, VT_SETMODE, &mode);
if (r < 0) {
log_debug_errno(errno, "Failed to set VT_AUTO mode, ignoring: %m");
if (q >= 0)
q = -errno;
}
r = fchown(fd, 0, (gid_t) -1);
if (r < 0) {
log_debug_errno(errno, "Failed to chown VT, ignoring: %m");
if (q >= 0)
q = -errno;
}
return q;
}

View File

@ -154,3 +154,4 @@ int open_terminal_in_namespace(pid_t pid, const char *name, int mode);
int vt_default_utf8(void);
int vt_reset_keyboard(int fd);
int vt_restore(int fd);

View File

@ -1225,12 +1225,7 @@ error:
}
static void session_restore_vt(Session *s) {
static const struct vt_mode mode = {
.mode = VT_AUTO,
};
int vt, old_fd;
int r, vt, old_fd;
/* We need to get a fresh handle to the virtual terminal,
* since the old file-descriptor is potentially in a hung-up
@ -1246,12 +1241,9 @@ static void session_restore_vt(Session *s) {
if (vt < 0)
return;
(void) ioctl(vt, KDSETMODE, KD_TEXT);
(void) vt_reset_keyboard(vt);
(void) ioctl(vt, VT_SETMODE, &mode);
(void) fchown(vt, 0, (gid_t) -1);
r = vt_restore(vt);
if (r < 0)
log_warning_errno(r, "Failed to restore VT, ignoring: %m");
s->vtfd = safe_close(s->vtfd);
}