]> rtime.felk.cvut.cz Git - sojka/sterm.git/commitdiff
Never block when opening the port
authorMichal Sojka <sojkam1@fel.cvut.cz>
Tue, 27 Jan 2015 09:19:22 +0000 (10:19 +0100)
committerMichal Sojka <sojkam1@fel.cvut.cz>
Tue, 27 Jan 2015 09:19:22 +0000 (10:19 +0100)
Whether blocking happens depends on the value of CDC signal and previously
set CLOCAL settings. Since we cannot influence CLOCAL settings before
opening the port, we have to use O_NONBLOCK.

sterm.c

diff --git a/sterm.c b/sterm.c
index e9d914abbb4fc4ae71ff56cce1cc2c432d8412fa..209f83c92df4d42ecd2a13d8c3c58d3e4a2ac73a 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
@@ -209,10 +209,14 @@ int main(int argc, char *argv[])
        }
        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));