From ef98fc7fa2c51cd97b5aa4708a55dfa79ea2d128 Mon Sep 17 00:00:00 2001 From: Martin Lund Date: Thu, 12 May 2016 15:44:59 +0200 Subject: [PATCH] Introduced lock on device file Tio will now test for and obtain an advisory lock on the tty device file to prevent starting multiple sessions on the same tty device. --- src/tty.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/tty.c b/src/tty.c index a8a7cf8..7618666 100644 --- a/src/tty.c +++ b/src/tty.c @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -146,6 +147,7 @@ void disconnect_tty(void) if (connected) { color_printf("[tio %s] Disconnected", current_time()); + flock(fd, LOCK_UN); close(fd); connected = false; } @@ -184,6 +186,14 @@ int connect_tty(void) goto error_isatty; } + /* Lock device file */ + status = flock(fd, LOCK_EX | LOCK_NB); + if ((status == -1) && (errno == EWOULDBLOCK)) + { + printf("Error: Device file is locked by another process\r\n"); + exit(EXIT_FAILURE); + } + /* Flush stale I/O data (if any) */ tcflush(fd, TCIOFLUSH);