Section: comm
Priority: optional
Maintainer: Michal Sojka <sojkam1@fel.cvut.cz>
-Build-Depends: debhelper (>= 9), liblockdev1-dev
+Build-Depends: debhelper (>= 9)
Standards-Version: 3.9.5
Homepage: https://rtime.felk.cvut.cz/gitweb/sojka/sterm.git
Vcs-Git: git://rtime.felk.cvut.cz/sojka/sterm.git
#include <stdbool.h>
#include <string.h>
#include <signal.h>
+#ifdef HAVE_LOCKDEV
#include <lockdev.h>
+#endif
+#include <sys/file.h>
#include <errno.h>
#define STRINGIFY(val) #val
tcsetattr(0, TCSANOW, &stdin_tio_backup);
}
+#ifdef HAVE_LOCKDEV
void unlock()
{
dev_unlock(dev, getpid());
}
+#endif
void sighandler(int arg)
{
signal(SIGTERM, sighandler);
signal(SIGHUP, sighandler);
+#ifdef HAVE_LOCKDEV
pid_t pid = dev_lock(dev);
if (pid > 0) {
fprintf(stderr, "%s is used by PID %d\n", dev, pid);
exit(1);
}
atexit(unlock);
+#endif
/* O_NONBLOCK is needed to not wait for the CDC signal. See tty_ioctl(4). */
if ((fd = open(dev, O_RDWR|O_NOCTTY|O_NONBLOCK)) < 0) {
int n = fcntl(fd, F_GETFL, 0);
fcntl(fd, F_SETFL, n & ~O_NDELAY);
+ flock(fd, LOCK_EX);
+
if (isatty(fd)) {
CHECK(ioctl(fd, TIOCEXCL, NULL));