]> rtime.felk.cvut.cz Git - sojka/sterm.git/commitdiff
Make liblockdev optional and use flock
authorMichal Sojka <michal.sojka@cvut.cz>
Fri, 15 Dec 2017 11:51:54 +0000 (12:51 +0100)
committerMichal Sojka <michal.sojka@cvut.cz>
Fri, 15 Dec 2017 12:03:05 +0000 (13:03 +0100)
Makefile
debian/control
sterm.c

index 5aada56a6871227d28ff8a9cdebf2064baaa7787..f7d0b4dd1881bcad927e7cc340492dfe7f7c3b1f 100644 (file)
--- 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
index d99eebde6dcfcba06b66b6ab669f86e6465e4971..de83d265059afd2bd063fd57fdf2673ebbe5c600 100644 (file)
@@ -2,7 +2,7 @@ Source: sterm
 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
diff --git a/sterm.c b/sterm.c
index ea8657326d84f61f69b5d9a06626efc01ab64641..6ecb117926ee89dd3e3b4a03ccfb8c315a42b76d 100644 (file)
--- a/sterm.c
+++ b/sterm.c
 #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
@@ -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));