From 629a59ca698a0e3d3a0a29d5f2245c0e19808849 Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Fri, 22 Aug 2014 16:54:56 +0200 Subject: vt protocols MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/mds-kkbd.c | 3 +++ src/mds-vt.c | 43 ++++++++++++++++++++++++++++++++++++------- src/mds-vt.h | 16 +++++++++++++--- 3 files changed, 52 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/mds-kkbd.c b/src/mds-kkbd.c index b699ea0..842da90 100644 --- a/src/mds-kkbd.c +++ b/src/mds-kkbd.c @@ -16,6 +16,9 @@ * along with this program. If not, see . */ #include "mds-kkbd.h" +/* TODO: This server should wait for `Command: get-vt` to be available, + query the active VT and connect to that TTY instead of stdin. */ +/* TODO: Release control on `Command: switching-vt`. */ #include #include diff --git a/src/mds-vt.c b/src/mds-vt.c index 2167fb1..a6124c9 100644 --- a/src/mds-vt.c +++ b/src/mds-vt.c @@ -50,7 +50,7 @@ server_characteristics_t server_characteristics = { .require_privileges = 1, /* we want to open the new tty (the rest is fine without root for some reason) */ .require_display = 1, - .require_respawn_info = 0, + .require_respawn_info = 1, .sanity_check_argc = 1, .fork_for_safety = 0, .danger_is_deadly = 0 @@ -98,9 +98,10 @@ int initialise_server(void) const char* const message = "Command: intercept\n" "Message ID: 0\n" - "Length: 14\n" + "Length: 38\n" "\n" - "Command: echo\n"; + "Command: get-vt\n"; + "Command: configure-vt\n"; if (full_send(message, strlen(message))) return 1; @@ -337,16 +338,44 @@ int vt_set_active(int vt) /** * Open a virtual terminal * - * @param vt The index of the terminal - * @return The file descriptor for the terminal, -1 on error + * @param vt The index of the terminal + * @param old_stat Output parameter for the old file stat for the terminal + * @return The file descriptor for the terminal, -1 on error */ -int vt_open(int vt) +int vt_open(int vt, struct stat* restrict old_stat) { char vtpath[64]; /* Should be small enought and large enought for any lunatic alternative to /dev/ttyNNN, if not you will need to apply a patch (or fix your system.) */ + int fd; sprintf(vtpath, VT_PATH_PATTERN, vt); - return open(vtpath, O_RDWR); + fd = open(vtpath, O_RDWR); + if (fd < 0) + return -1; + if ((fstat(fd, old_stat) < 0) || + (fchown(fd, getuid(), getgid()) < 0)) + { + close(fd); + return -1; + } + return fd; +} + + +/** + * Close a virtual terminal + * + * @param vt The index of the terminal + * @param old_stat The old file stat for the terminal + */ +void vt_close(int fd, struct stat* restrict old_stat) +{ + if (fchown(fd, old_stat->st_uid, old_stat->st_gid) < 0) + { + xperror(*argv); + eprint("while resetting TTY ownership."); + } + close(fd); } diff --git a/src/mds-vt.h b/src/mds-vt.h index 1f87726..60204fc 100644 --- a/src/mds-vt.h +++ b/src/mds-vt.h @@ -62,10 +62,20 @@ int vt_set_active(int vt); /** * Open a virtual terminal * - * @param vt The index of the terminal - * @return The file descriptor for the terminal, -1 on error + * @param vt The index of the terminal + * @param old_stat Output parameter for the old file stat for the terminal + * @return The file descriptor for the terminal, -1 on error + */ +int vt_open(int vt, struct stat* restrict old_stat); + + +/** + * Close a virtual terminal + * + * @param vt The index of the terminal + * @param old_stat The old file stat for the terminal */ -int vt_open(int vt); +void vt_close(int fd, struct stat* restrict old_stat); -- cgit v1.2.3-70-g09d2