]> rtime.felk.cvut.cz Git - sojka/sterm.git/commitdiff
Use liblockdev to lock the tty device
authorMichal Sojka <sojkam1@fel.cvut.cz>
Wed, 23 Jul 2014 16:49:03 +0000 (18:49 +0200)
committerMichal Sojka <sojkam1@fel.cvut.cz>
Wed, 23 Jul 2014 16:49:03 +0000 (18:49 +0200)
This is simpler and it should also be compatible with systemd initiated
change of /var/lock permissions. See
http://lists.freedesktop.org/archives/systemd-devel/2011-March/001823.html

Makefile
debian/control
sterm.c

index 8c9a08c0a20a5fb2fe56a68882730d2f0d71cabc..18737c5b82adc1b2df31fc6cdf17370e7534d86c 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -1,4 +1,5 @@
 CFLAGS = -O2 -Wall
+LDFLAGS = -llockdev
 
 all: sterm
 
index de83d265059afd2bd063fd57fdf2673ebbe5c600..d99eebde6dcfcba06b66b6ab669f86e6465e4971 100644 (file)
@@ -2,7 +2,7 @@ Source: sterm
 Section: comm
 Priority: optional
 Maintainer: Michal Sojka <sojkam1@fel.cvut.cz>
-Build-Depends: debhelper (>= 9)
+Build-Depends: debhelper (>= 9), liblockdev1-dev
 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 0e124e5cff25ec9b8432661069d76a2d20a42906..2402c95dbd3e1c5ae19b0d7d184cb9d9fe62a075 100644 (file)
--- a/sterm.c
+++ b/sterm.c
@@ -31,6 +31,7 @@
 
 #define _BSD_SOURCE
 #include <sys/ioctl.h>
+#include <sys/types.h>
 #include <unistd.h>
 #include <fcntl.h>
 #include <termios.h>
@@ -41,6 +42,7 @@
 #include <stdbool.h>
 #include <string.h>
 #include <signal.h>
+#include <lockdev.h>
 
 #define STRINGIFY(val) #val
 #define TOSTRING(val) STRINGIFY(val)
@@ -52,9 +54,8 @@
 bool verbose = false;
 bool exit_on_escape = true;
 
-char template[] = "/var/lock/TMPXXXXXX";
-char lockfile[100];
 struct termios stdin_tio_backup;
+char *dev = NULL;
 
 void rm_file(int status, void *arg)
 {
@@ -69,6 +70,11 @@ void restore_stdin_term()
        tcsetattr(0, TCSANOW, &stdin_tio_backup);
 }
 
+void unlock()
+{
+       dev_unlock(dev, getpid());
+}
+
 void sighandler(int arg)
 {
        exit(0); /* Invoke exit handlers */
@@ -121,7 +127,6 @@ void usage(const char* argv0)
 int main(int argc, char *argv[])
 {
        int fd;
-       char *dev = NULL;
        int opt;
        speed_t speed = 0;
        int dtr = 0, rts = 0;
@@ -192,37 +197,15 @@ int main(int argc, char *argv[])
        signal(SIGTERM, sighandler);
        signal(SIGHUP, sighandler);
 
-       if (strncmp(dev, "/dev/", 5) == 0 &&
-           strrchr(dev, '/') == dev + 4 &&
-           dev[5] != 0)
-       { /* Create lock file (to be inter-operable with other programs) */
-               /* This is racy, but what we can do - see also comments in uucp / cu */
-               int tmp = CHECK(mkstemp(template));
-               on_exit(rm_file, template);
-               char pid[20];
-               snprintf(pid, sizeof(pid), "%u", getpid());
-               CHECK(write(tmp, pid, strlen(pid)));
-               close(tmp);
-               snprintf(lockfile, sizeof(lockfile), "/var/lock/LCK..%s", dev + 5);
-       retry:
-               if (link(template, lockfile) == -1) {
-                       tmp = CHECK(open(lockfile, O_RDONLY));
-                       CHECK(read(tmp, pid, sizeof(pid)));
-                       close(tmp);
-                       int p = atoi(pid);
-                       char proc[50];
-                       snprintf(proc, sizeof(proc), "/proc/%d", p);
-                       if (access(proc, F_OK) == 0) {
-                               fprintf(stderr, "%s is used by PID %d\n", dev, p);
-                               exit(1);
-                       }
-                       fprintf(stderr, "Stale lock file %s (PID %d) - removing it!\n", lockfile, p);
-                       CHECK(unlink(lockfile));
-                       goto retry;
-               }
-               rm_file(0, template);
-               on_exit(rm_file, lockfile);
+       pid_t pid = dev_lock(dev);
+       if (pid > 0) {
+               fprintf(stderr, "%s is used by PID %d\n", dev, pid);
+               exit(1);
+       } else if (pid < 0) {
+               perror("dev_lock()");
+               exit(1);
        }
+       atexit(unlock);
 
        if ((fd = open(dev, O_RDWR)) < 0) {
                perror(dev);