internal: ioctl use /dev/tty instead of fd 0 for VT_GETSTATE (#2989)

* fix: ioctl use /dev/tty instead of fd 0 for VT_GETSTATE

ioctl VT_GETSTATE on stdin fails if it is not console, such as when
using GDM.  `/dev/tty` should be used instead.

- `/dev/tty` is a synonym for the controlling terminal of a process, if
there is one.

---------

Co-authored-by: Jan Beich <jbeich@FreeBSD.org>
This commit is contained in:
memchr 2023-08-16 08:51:08 +00:00 committed by GitHub
parent 4986d74ef2
commit 63b266cf65
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -3,6 +3,7 @@
#include <regex> #include <regex>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <fcntl.h>
#if defined(__linux__) #if defined(__linux__)
#include <linux/vt.h> #include <linux/vt.h>
#elif defined(__NetBSD__) || defined(__OpenBSD__) #elif defined(__NetBSD__) || defined(__OpenBSD__)
@ -518,15 +519,19 @@ bool CKeybindManager::handleVT(xkb_keysym_t keysym) {
// vtnr is bugged for some reason. // vtnr is bugged for some reason.
unsigned int ttynum = 0; unsigned int ttynum = 0;
int fd;
if ((fd = open("/dev/tty", O_RDONLY | O_NOCTTY)) >= 0) {
#if defined(VT_GETSTATE) #if defined(VT_GETSTATE)
struct vt_stat st; struct vt_stat st;
if (!ioctl(0, VT_GETSTATE, &st)) if (!ioctl(fd, VT_GETSTATE, &st))
ttynum = st.v_active; ttynum = st.v_active;
#elif defined(VT_GETACTIVE) #elif defined(VT_GETACTIVE)
int vt; int vt;
if (!ioctl(0, VT_GETACTIVE, &vt)) if (!ioctl(fd, VT_GETACTIVE, &vt))
ttynum = vt; ttynum = vt;
#endif #endif
close(fd);
}
if (ttynum == TTY) if (ttynum == TTY)
return true; return true;