]> git.ipfire.org Git - thirdparty/plymouth.git/commitdiff
terminal: always open terminal in non-blocking mode
authorRay Strode <rstrode@redhat.com>
Thu, 16 Jun 2016 19:23:15 +0000 (15:23 -0400)
committerRay Strode <rstrode@redhat.com>
Thu, 16 Jun 2016 19:23:15 +0000 (15:23 -0400)
In theory this is safer since open calls can block
indefinitely without O_NONBLOCK.

src/libply-splash-core/ply-terminal.c
src/libply/ply-utils.c
src/libply/ply-utils.h

index 1bbeb4261bca2e192cd83fafcb9e98da69f00b94..602ff49f3d16e2c68188c0d6ef949d22dd94f458 100644 (file)
@@ -551,7 +551,7 @@ ply_terminal_open_device (ply_terminal_t *terminal)
         assert (terminal->fd < 0);
         assert (terminal->fd_watch == NULL);
 
-        terminal->fd = open (terminal->name, O_RDWR | O_NOCTTY);
+        terminal->fd = open (terminal->name, O_RDWR | O_NOCTTY | O_NONBLOCK);
 
         if (terminal->fd < 0) {
                 ply_trace ("Unable to open terminal device '%s': %m", terminal->name);
@@ -570,6 +570,8 @@ ply_terminal_open_device (ply_terminal_t *terminal)
                 return PLY_TERMINAL_OPEN_RESULT_FAILURE;
         }
 
+        ply_set_fd_as_blocking (terminal->fd);
+
         terminal->fd_watch = ply_event_loop_watch_fd (terminal->loop, terminal->fd,
                                                       PLY_EVENT_LOOP_FD_STATUS_HAS_DATA,
                                                       (ply_event_handler_t) on_tty_input,
index feee6e6695d65de553a5659ee250c8800e24812d..31b642eb29626be6a5197ac04f4d2152f476245c 100644 (file)
@@ -421,6 +421,29 @@ ply_fd_may_block (int fd)
         return (flags & O_NONBLOCK) != 0;
 }
 
+bool
+ply_set_fd_as_blocking (int fd)
+{
+        int flags;
+        int ret = 0;
+
+        assert (fd >= 0);
+
+        flags = fcntl (fd, F_GETFL);
+
+        if (flags == -1) {
+                return false;
+        }
+
+        if (flags & O_NONBLOCK) {
+                flags &= ~O_NONBLOCK;
+
+                ret = fcntl (fd, F_SETFL, flags);
+        }
+
+        return ret == 0;
+}
+
 char **
 ply_copy_string_array (const char *const *array)
 {
index 10bdb5d0f497ebcdc3504f83994e220642cd5125..2f4ce81864107c1f36f3e58f65aab0475c6c0d5b 100644 (file)
@@ -82,6 +82,7 @@ bool ply_read_uint32 (int       fd,
 bool ply_fd_has_data (int fd);
 bool ply_fd_can_take_data (int fd);
 bool ply_fd_may_block (int fd);
+bool ply_set_fd_as_blocking (int fd);
 char **ply_copy_string_array (const char *const *array);
 void ply_free_string_array (char **array);
 bool ply_string_has_prefix (const char *string,