mirror of
https://github.com/tio/tio.git
synced 2026-05-01 14:57:59 +02:00
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:
parent
8be401559d
commit
ef98fc7fa2
1 changed files with 10 additions and 0 deletions
10
src/tty.c
10
src/tty.c
|
|
@ -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);
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue