From eba38e7977c3e8a0c75e60448183aee8ed6c8c7e Mon Sep 17 00:00:00 2001 From: Michal Sojka Date: Fri, 15 Dec 2017 12:51:54 +0100 Subject: [PATCH] Make liblockdev optional and use flock --- Makefile | 3 +++ debian/control | 2 +- sterm.c | 9 +++++++++ 3 files changed, 13 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 5aada56..f7d0b4d 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,8 @@ CFLAGS = -O2 -Wall -g + +ifneq ($(findstring HAVE_LOCKDEV,$(CFLAGS)),) LDLIBS = -llockdev +endif PREFIX ?= /usr/local INSTALL ?= install diff --git a/debian/control b/debian/control index d99eebd..de83d26 100644 --- a/debian/control +++ b/debian/control @@ -2,7 +2,7 @@ Source: sterm Section: comm Priority: optional Maintainer: Michal Sojka -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 diff --git a/sterm.c b/sterm.c index ea86573..6ecb117 100644 --- a/sterm.c +++ b/sterm.c @@ -44,7 +44,10 @@ #include #include #include +#ifdef HAVE_LOCKDEV #include +#endif +#include #include #define STRINGIFY(val) #val @@ -74,10 +77,12 @@ void restore_stdin_term() tcsetattr(0, TCSANOW, &stdin_tio_backup); } +#ifdef HAVE_LOCKDEV void unlock() { dev_unlock(dev, getpid()); } +#endif void sighandler(int arg) { @@ -266,6 +271,7 @@ int main(int argc, char *argv[]) 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); @@ -280,6 +286,7 @@ int main(int argc, char *argv[]) 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) { @@ -290,6 +297,8 @@ int main(int argc, char *argv[]) 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)); -- 2.39.2