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.
This commit is contained in:
Martin Lund 2016-05-12 15:44:59 +02:00
parent 8be401559d
commit ef98fc7fa2

View file

@ -27,6 +27,7 @@
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <sys/file.h>
#include <fcntl.h>
#include <termios.h>
#include <stdbool.h>
@ -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);