[PATCH] libosmocore[master]: serial: Open devie in non-blocking mode and then switch to b...

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

Harald Welte gerrit-no-reply at lists.osmocom.org
Fri Dec 23 22:01:34 UTC 2016


Review at  https://gerrit.osmocom.org/1500

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
---
M src/serial.c
1 file changed, 17 insertions(+), 3 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/00/1500/1

diff --git a/src/serial.c b/src/serial.c
index 66ee756..4403226 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) {

-- 
To view, visit https://gerrit.osmocom.org/1500
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I626b138574bc50f4f4b09c4d609f3623ff512dff
Gerrit-PatchSet: 1
Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Owner: Harald Welte <laforge at gnumonks.org>



More information about the gerrit-log mailing list