]> rtime.felk.cvut.cz Git - sojka/sterm.git/blobdiff - sterm.c
Handle hang-up event on stdin
[sojka/sterm.git] / sterm.c
diff --git a/sterm.c b/sterm.c
index 3e1a5736cbd5d58f406d8bb73359a8857ee5ee97..040c22f29c9a5ec163c4b944862087f5069068ca 100644 (file)
--- a/sterm.c
+++ b/sterm.c
@@ -1,7 +1,7 @@
 /*
  * Simple serial terminal
  *
- * Copyright 2014, 2015 Michal Sojka <sojkam1@fel.cvut.cz>
+ * Copyright 2014, 2015, 2016, 2017, 2019 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
@@ -30,6 +30,8 @@
  */
 
 #define _BSD_SOURCE
+#define _DEFAULT_SOURCE
+#define _GNU_SOURCE
 #include <sys/ioctl.h>
 #include <sys/types.h>
 #include <unistd.h>
 #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
 #define TOSTRING(val) STRINGIFY(val)
@@ -71,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)
 {
@@ -125,12 +133,14 @@ void usage(const char* argv0)
        fprintf(stderr, "Usage: %s [options] <device>\n", argv0);
        fprintf(stderr,
                "Options:\n"
+               "  -b <duration> send break signal\n"
                "  -c        enter command mode\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"
+               "  -v        verbose mode\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"
@@ -167,6 +177,7 @@ void handle_commands(int fd)
 
        while (!go) {
                char *p1 = NULL;
+               int num;
                if (fgets(command, sizeof(command), stdin) == NULL) {
                        if (!feof(stdin))
                            perror("Command read");
@@ -176,8 +187,12 @@ void handle_commands(int fd)
                        pulse(fd, dtr_rts_arg('d', p1), 0);
                else if (sscanf(command, "rts %ms", &p1) == 1)
                        pulse(fd, 0, dtr_rts_arg('r', p1));
+               else if (sscanf(command, "break %d", &num) == 1)
+                       CHECK(tcsendbreak(fd, num));
                else if (strcmp(command, "go\n") == 0)
                        break;
+               else if (strcmp(command, "exit\n") == 0)
+                       exit(0);
                else {
                        fprintf(stderr, "Unknown command: %s\n", command);
                        exit(1);
@@ -197,14 +212,16 @@ int main(int argc, char *argv[])
        bool stdin_tty;
        bool raw = true;
        bool cmd = false;
+       int break_dur = -1;
 
        if ((stdin_tty = isatty(0))) {
                CHECK(tcgetattr(0, &stdin_tio_backup));
                atexit(restore_stdin_term);
        }
 
-       while ((opt = getopt(argc, argv, "cnd::er::s:v")) != -1) {
+       while ((opt = getopt(argc, argv, "b:cnd::er::s:v")) != -1) {
                switch (opt) {
+               case 'b': break_dur = atoi(optarg); break;
                case 'c': cmd = true; break;
                case 'd': dtr = dtr_rts_arg(opt, optarg); break;
                case 'e': exit_on_escape = false; break;
@@ -262,15 +279,22 @@ 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);
                exit(1);
        } else if (pid < 0) {
-               perror("dev_lock()");
+               char *msg;
+               asprintf(&msg, "dev_lock('%s')", dev); /* No free() because we exit() immediately */
+               if (errno)
+                       perror(msg);
+               else
+                       fprintf(stderr, "%s: Error\n", msg);
                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) {
@@ -281,6 +305,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));
 
@@ -296,6 +322,9 @@ int main(int argc, char *argv[])
                if (dtr || rts)
                        pulse(fd, dtr, rts);
 
+               if (break_dur != -1)
+                       CHECK(tcsendbreak(fd, break_dur));
+
                 /* Disable flow control */
                tio.c_cflag &= ~(CRTSCTS);
                tio.c_iflag &= ~(IXON|IXOFF);
@@ -344,6 +373,10 @@ int main(int argc, char *argv[])
                                exit(1);
                        }
                }
+               if (fds[0].revents & POLLHUP) {
+                       VERBOSE("HUP on stdin\r\n");
+                       break;
+               }
                if (fds[1].revents & POLLIN) {
                        r1 = CHECK(read(fd, buf, sizeof(buf)));
                        if (r1 == 0) {