]> sigrok.org Git - libserialport.git/blobdiff - serialport.c
Handle EINTR from tcdrain() in sp_drain().
[libserialport.git] / serialport.c
index 0a467dea1a74732cde11d144ae564d2b13fe516f..bbb53bdc64941d9aac6bab6a484d031a4653303b 100644 (file)
@@ -799,13 +799,23 @@ enum sp_return sp_drain(struct sp_port *port)
        /* Returns non-zero upon success, 0 upon failure. */
        if (FlushFileBuffers(port->hdl) == 0)
                RETURN_FAIL("FlushFileBuffers() failed");
+       RETURN_OK();
 #else
-       /* Returns 0 upon success, -1 upon failure. */
-       if (tcdrain(port->fd) < 0)
-               RETURN_FAIL("tcdrain() failed");
+       int result;
+       while (1) {
+               result = tcdrain(port->fd);
+               if (result < 0) {
+                       if (errno == EINTR) {
+                               DEBUG("tcdrain() was interrupted");
+                               continue;
+                       } else {
+                               RETURN_FAIL("tcdrain() failed");
+                       }
+               } else {
+                       RETURN_OK();
+               }
+       }
 #endif
-
-       RETURN_OK();
 }
 
 enum sp_return sp_blocking_write(struct sp_port *port, const void *buf, size_t count, unsigned int timeout)
@@ -890,9 +900,14 @@ enum sp_return sp_blocking_write(struct sp_port *port, const void *buf, size_t c
                        timersub(&end, &now, &delta);
                }
                result = select(port->fd + 1, NULL, &fds, NULL, timeout ? &delta : NULL);
-               if (result < 0)
-                       RETURN_FAIL("select() failed");
-               if (result == 0) {
+               if (result < 0) {
+                       if (errno == EINTR) {
+                               DEBUG("select() call was interrupted, repeating");
+                               continue;
+                       } else {
+                               RETURN_FAIL("select() failed");
+                       }
+               } else if (result == 0) {
                        DEBUG("write timed out");
                        RETURN_VALUE("%d", bytes_written);
                }
@@ -1060,9 +1075,14 @@ enum sp_return sp_blocking_read(struct sp_port *port, void *buf, size_t count, u
                        timersub(&end, &now, &delta);
                }
                result = select(port->fd + 1, &fds, NULL, NULL, timeout ? &delta : NULL);
-               if (result < 0)
-                       RETURN_FAIL("select() failed");
-               if (result == 0) {
+               if (result < 0) {
+                       if (errno == EINTR) {
+                               DEBUG("select() call was interrupted, repeating");
+                               continue;
+                       } else {
+                               RETURN_FAIL("select() failed");
+                       }
+               } else if (result == 0) {
                        DEBUG("read timed out");
                        RETURN_VALUE("%d", bytes_read);
                }