]> git.ipfire.org Git - thirdparty/xz.git/commitdiff
xz: Fix the fcntl() usage when creating a pipe for the self-pipe trick.
authorLasse Collin <lasse.collin@tukaani.org>
Sat, 21 Feb 2015 21:00:19 +0000 (23:00 +0200)
committerLasse Collin <lasse.collin@tukaani.org>
Sat, 21 Feb 2015 21:00:19 +0000 (23:00 +0200)
Now it reads the old flags instead of blindly setting O_NONBLOCK.
The old code may have worked correctly, but this is better.

src/xz/file_io.c

index c52656c10ef822149ecd4dc16c635bccf0955ec0..9bd515ddc894455aff411f1f6783e6160ed51bc3 100644 (file)
@@ -82,13 +82,19 @@ io_init(void)
        // we are root.
        warn_fchown = geteuid() == 0;
 
-       if (pipe(user_abort_pipe)
-                       || fcntl(user_abort_pipe[0], F_SETFL, O_NONBLOCK)
-                               == -1
-                       || fcntl(user_abort_pipe[1], F_SETFL, O_NONBLOCK)
-                               == -1)
+       // Create a pipe for the self-pipe trick.
+       if (pipe(user_abort_pipe))
                message_fatal(_("Error creating a pipe: %s"),
                                strerror(errno));
+
+       // Make both ends of the pipe non-blocking.
+       for (unsigned i = 0; i < 2; ++i) {
+               int flags = fcntl(user_abort_pipe[i], F_GETFL);
+               if (flags == -1 || fcntl(user_abort_pipe[i], F_SETFL,
+                               flags | O_NONBLOCK) == -1)
+                       message_fatal(_("Error creating a pipe: %s"),
+                                       strerror(errno));
+       }
 #endif
 
 #ifdef __DJGPP__