summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHarald Welte <laforge@gnumonks.org>2016-12-23 22:49:39 +0100
committerHarald Welte <laforge@gnumonks.org>2016-12-23 22:50:10 +0100
commit1db37820aaa0e83cfc7f26270f5bb3a6d9a27189 (patch)
tree3130be4e016f99888a5df02f463e570576a88c96
parenta0f74f218bd5c805d1c190af54d80ed781241148 (diff)
serial: Open devie in non-blocking mode and then switch to blocking
There are some serial ports that apparently block during the open in some circumstances. We don't want that. We want to either open it immediately, or fail fast. Change-Id: I626b138574bc50f4f4b09c4d609f3623ff512dff
-rw-r--r--src/serial.c20
1 files changed, 17 insertions, 3 deletions
diff --git a/src/serial.c b/src/serial.c
index 66ee7564..44032263 100644
--- a/src/serial.c
+++ b/src/serial.c
@@ -59,16 +59,30 @@
int
osmo_serial_init(const char *dev, speed_t baudrate)
{
- int rc, fd=0, v24;
+ int rc, fd=0, v24, flags;
struct termios tio;
- /* Open device */
- fd = open(dev, O_RDWR | O_NOCTTY);
+ /* Use nonblock as the device might block otherwise */
+ fd = open(dev, O_RDWR | O_NOCTTY | O_SYNC | O_NONBLOCK);
if (fd < 0) {
dbg_perror("open");
return -errno;
}
+ /* now put it into blcoking mode */
+ flags = fcntl(fd, F_GETFL, 0);
+ if (flags < 0) {
+ dbg_perror("fcntl get flags");
+ return -1;
+ }
+
+ flags &= ~O_NONBLOCK;
+ rc = fcntl(fd, F_SETFL, flags);
+ if (rc != 0) {
+ dbg_perror("fcntl set flags");
+ return -1;
+ }
+
/* Configure serial interface */
rc = tcgetattr(fd, &tio);
if (rc < 0) {