vt: vt_ioctl: Add new ioctl to read vt switch lock state
PATCH 1/1 vt: vt_ioctl: Add new ioctl to read vt switch lock state
The existing ioctls VT_LOCKSWITCH and VT_UNLOCKSWITCH can be used to allow/disallow switching the virtual terminal. However, no mechanism exists that allows software to read this lock state.
Userspace programs that try to switch to another virtual terminal like chvt have no mechanism to figure out whether they will be able to actually switch the terminal. When eg. chvt is run while terminal switching is disabled, it simply sleeps forever waiting for the target terminal to become active.
This commit introduces a new ioctl VT_GETLOCKSWITCH that allows reading the current state of the switch lock flag. Userspace software can then use that flag and handle not being able to switch virtual terminals.
Example program using this:
#include <linux/vt.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#include <error.h>
#include <unistd.h>
const char* console_device = "/dev/tty0";
int main(int argc, char* argv[]) {
int fd;
while ((fd = open(console_device, O_RDWR)) == -1 && errno == EINTR);
if (fd < 0)
error(1, errno, "Opening %s", console_device);
int ret;
while ((ret = ioctl(fd, VT_GETLOCKSWITCH, 1)) == -1 && errno == EINTR);
if (ret == -1)
error(1, errno, "%s: VT_GETLOCKSWITCH", console_device);
printf("VT switching is %s\n", ret == 1 ? "locked" : "unlocked");
close(fd);
return 0;
}--- drivers/tty/vt/vt_ioctl.c | 5 +++++ include/uapi/linux/vt.h | 1 + 2 files changed, 6 insertions(+)
drivers/tty/vt/vt_ioctl.c +5 -0
@@ -1028,6 +1028,11 @@ int vt_ioctl(struct tty_struct *tty,return -EPERM ; vt_dont_switch = false;break ; +case VT_GETLOCKSWITCH : +if (!capable (CAP_SYS_TTY_CONFIG )) +return -EPERM ; + ret = vt_dont_switch ?1 :0 ; +break ;case VT_GETHIFONTMASK : ret =put_user (vc->vc_hi_font_mask, (unsigned short __user *)arg);
include/uapi/linux/vt.h +1 -0
@@ -61,6 +61,7 @@ struct vt_consize {#define VT_RESIZEX 0x560A/* set kernel's idea of screensize + more */ #define VT_LOCKSWITCH 0x560B/* disallow vt switching */ #define VT_UNLOCKSWITCH 0x560C/* allow vt switching */ +#define VT_GETLOCKSWITCH 0x5610/* return vt switch lock state */ #define VT_GETHIFONTMASK 0x560D/* return hi font mask */ struct vt_event {
Signature · 1 line
2.26.2
Quoted 25 lines · Johannes Hostert
The existing ioctls
VT_LOCKSWITCHandVT_UNLOCKSWITCHcan be used to allow/disallow switching the virtual terminal. However, no mechanism exists that allows software to read this lock state.Userspace programs that try to switch to another virtual terminal like chvt have no mechanism to figure out whether they will be able to actually switch the terminal. When eg. chvt is run while terminal switching is disabled, it simply sleeps forever waiting for the target terminal to become active.
This commit introduces a new ioctl
VT_GETLOCKSWITCHthat allows reading the current state of the switch lock flag. Userspace software can then use that flag and handle not being able to switch virtual terminals.Example program using this:
#include <
linux/vt.h>
#include <sys/ioctl.h>
#include <fcntl.h>
#include <stdio.h>
#include <errno.h>
#include <error.h>
#include <unistd.h>
const char*console_device= "/dev/tty0";
Quoted 9 lines
int main(int argc, char* argv[]) {
int fd;
while ((fd = open(console_device, O_RDWR)) == -1 && errno == EINTR);
if (fd < 0)
error(1, errno, "Opening %s", console_device);
int ret;
while ((ret = ioctl(fd, VT_GETLOCKSWITCH, 1)) == -1 && errno == EINTR);
if (ret == -1)
error(1, errno, "%s: VT_GETLOCKSWITCH", console_device);And now if you do VT_LOCKSWITCH even when VT_GETLOCKSWITCH returns 0, vt_dont_switch can be 1 already.
So NACK.
Please don't add another racy ioctl. Introduce VT_LOCKSWITCH_NONBLOCK or something like that instead (like respecting O_NONBLOCK).
thanks,
Signature · 2 lines
js suse labs