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-vt.c | 43 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 36 insertions(+), 7 deletions(-) (limited to 'src/mds-vt.c') 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); } -- cgit v1.2.3-70-g09d2