]> rtime.felk.cvut.cz Git - sojka/sterm.git/blobdiff - sterm.c
Update changelog for 20150821 release
[sojka/sterm.git] / sterm.c
diff --git a/sterm.c b/sterm.c
index 0e124e5cff25ec9b8432661069d76a2d20a42906..c075f8b73bcfe913624fd13a1f7ca32d5cd021aa 100644 (file)
--- a/sterm.c
+++ b/sterm.c
@@ -1,7 +1,7 @@
 /*
  * Simple serial terminal
  *
- * Copyright 2014 Michal Sojka <sojkam1@fel.cvut.cz>
+ * Copyright 2014, 2015 Michal Sojka <sojkam1@fel.cvut.cz>
  *
  * This program is free software: you can redistribute it and/or
  * modify it under the terms of the GNU General Public License as
@@ -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 */
@@ -79,12 +85,17 @@ int dtr_rts_arg(const char option)
        int val = -1;
 
        if (optarg) {
-               switch (optarg[0]) {
-               case '+': val = +1; break;
-               case '-': val = -1; break;
-               default:
-                       fprintf(stderr, "Unknown -%c argument: %s", option, optarg);
-                       exit(1);
+               char *end;
+               val = strtol(optarg, &end, 10);
+               if (end == optarg) {
+                       /* Not a number */
+                       switch (optarg[0]) {
+                       case '+': val = +1; break;
+                       case '-': val = -1; break;
+                       default:
+                               fprintf(stderr, "Unknown -%c argument: %s", option, optarg);
+                               exit(1);
+                       }
                }
        }
        return val;
@@ -95,33 +106,40 @@ void exit_on_escapeseq(const char *buf, int len)
        static const char escseq[] = "\r~.";
        static const char *state = escseq+1;
        int i;
-
        for (i = 0; i < len; i++) {
                if (buf[i] == *state) {
                        state++;
                        if (*state == 0)
                                exit(0);
-               } else
+               } else {
                        state = escseq;
+                       if (buf[i] == *state)
+                               state++;
+               }
        }
 }
 
 void usage(const char* argv0)
 {
        fprintf(stderr, "Usage: %s [options] <device>\n", argv0);
-       fprintf(stderr, "Options:\n");
-       fprintf(stderr, "  -d [+|-] create short positive/negative pulse on DTR\n");
-       fprintf(stderr, "  -e       ignore '~.' escape sequence\n");
-       fprintf(stderr, "  -n       do not switch the device to raw mode\n");
-       fprintf(stderr, "  -r [+|-] create short positive/negative pulse on RTS\n");
-       fprintf(stderr, "  -s <baudrate>\n");
-       fprintf(stderr, "  -v       verbose\n");
+       fprintf(stderr,
+               "Options:\n"
+               "  -d[PULSE] make pulse on DTR\n"
+               "  -e        ignore '~.' escape sequence\n"
+               "  -n        do not switch the device to raw mode\n"
+               "  -r[PULSE] make pulse on RTS\n"
+               "  -s <baudrate>\n"
+               "\n"
+               "PULSE is a number specifying the pulse. Absolute value defines the\n"
+               "length of the pulse in milliseconds, sign determines the polarity of\n"
+               "the pulse. Alternatively, PULSE can be either '+' or '-', which\n"
+               "corresponds to +1 or -1.\n"
+               );
 }
 
 int main(int argc, char *argv[])
 {
        int fd;
-       char *dev = NULL;
        int opt;
        speed_t speed = 0;
        int dtr = 0, rts = 0;
@@ -192,42 +210,24 @@ 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) {
+       /* 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) {
                perror(dev);
                exit(1);
        }
+        /* Cancel the efect of O_NONBLOCK flag. */
+       int n = fcntl(fd, F_GETFL, 0);
+        fcntl(fd, F_SETFL, n & ~O_NDELAY);
 
        if (isatty(fd)) {
                CHECK(ioctl(fd, TIOCEXCL, NULL));
@@ -242,17 +242,30 @@ int main(int argc, char *argv[])
                }
 
                if (dtr || rts) {
-                       int status;
+                       int status, ms = 0;
                        /* tio.c_cflag &= ~HUPCL; */ /* Don't lower DTR/RTS on close */
 
                        CHECK(ioctl(fd, TIOCMGET, &status));
-                       if (dtr == -1) status &= ~TIOCM_DTR;
-                       if (dtr == +1) status |=  TIOCM_DTR;
-                       if (rts == -1) status &= ~TIOCM_RTS;
-                       if (rts == +1) status |=  TIOCM_RTS;
+                       if (dtr > 0) { status &= ~TIOCM_DTR; ms = +dtr; }
+                       if (dtr < 0) { status |=  TIOCM_DTR; ms = -dtr; }
+                       if (rts > 0) { status &= ~TIOCM_RTS; ms = +rts; }
+                       if (rts < 0) { status |=  TIOCM_RTS; ms = -rts; }
                        CHECK(ioctl(fd, TIOCMSET, &status));
+
+                       usleep(ms*1000);
+
+                       if (dtr < 0) { status &= ~TIOCM_DTR; }
+                       if (dtr > 0) { status |=  TIOCM_DTR; }
+                       if (rts < 0) { status &= ~TIOCM_RTS; }
+                       if (rts > 0) { status |=  TIOCM_RTS; }
+                       CHECK(ioctl(fd, TIOCMSET, &status));
+
                }
 
+                /* Disable flow control */
+               tio.c_cflag &= ~(CRTSCTS);
+               tio.c_iflag &= ~(IXON|IXOFF);
+
                CHECK(tcsetattr(fd, TCSANOW, &tio));
        } else if (speed || dtr || rts) {
                fprintf(stderr, "Cannot set speed, DTR or RTS on non-terminal %s\n", dev);