linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Johannes Hostert <jojohostert@gmail.com>
To: unlisted-recipients:; (no To-header on input)
Cc: Johannes Hostert <jojohostert@gmail.com>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Jiri Slaby <jslaby@suse.com>, Eric Biggers <ebiggers@google.com>,
	Eric Dumazet <edumazet@google.com>,
	linux-kernel@vger.kernel.org
Subject: [PATCH] vt: vt_ioctl: Add new ioctl to read vt switch lock state
Date: Sun, 26 Apr 2020 01:14:34 +0200	[thread overview]
Message-ID: <20200425231436.535671-1-jojohostert@gmail.com> (raw)

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;
}

Signed-off-by: Johannes Hostert <jojohostert@gmail.com>
---
 drivers/tty/vt/vt_ioctl.c | 5 +++++
 include/uapi/linux/vt.h   | 1 +
 2 files changed, 6 insertions(+)

diff --git a/drivers/tty/vt/vt_ioctl.c b/drivers/tty/vt/vt_ioctl.c
index daf61c28ba76..08b808e1fbf0 100644
--- a/drivers/tty/vt/vt_ioctl.c
+++ b/drivers/tty/vt/vt_ioctl.c
@@ -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);
diff --git a/include/uapi/linux/vt.h b/include/uapi/linux/vt.h
index e9d39c48520a..13a1e82dfb14 100644
--- a/include/uapi/linux/vt.h
+++ b/include/uapi/linux/vt.h
@@ -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 {
-- 
2.26.2


             reply	other threads:[~2020-04-25 23:34 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-04-25 23:14 Johannes Hostert [this message]
2020-04-26  7:08 ` [PATCH] vt: vt_ioctl: Add new ioctl to read vt switch lock state Jiri Slaby

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20200425231436.535671-1-jojohostert@gmail.com \
    --to=jojohostert@gmail.com \
    --cc=ebiggers@google.com \
    --cc=edumazet@google.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=jslaby@suse.com \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).