Support marking a file-descriptor as blocking via fcntl
authorChristopher Dykes <cdykes@fb.com>
Mon, 26 Jun 2017 21:39:26 +0000 (14:39 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Mon, 26 Jun 2017 21:51:49 +0000 (14:51 -0700)
Summary: Previously, only marking it as non-blocking was supported.

Reviewed By: simpkins

Differential Revision: D5307769

fbshipit-source-id: 03ed7c35632dbc9551552b21c401ea709bd6eaaa

folly/portability/Fcntl.cpp

index 30bab8fbd8c37176239f9805972a681123cce976..5d8e7ec7ed0db8d1dff35346c7e212bc2677ad4a 100644 (file)
@@ -61,21 +61,20 @@ int fcntl(int fd, int cmd, ...) {
     }
     case F_SETFL: {
       int flags = va_arg(args, int);
-      if (flags & O_NONBLOCK) {
-        // If it's not a socket, it's probably a pipe.
-        if (folly::portability::sockets::is_fh_socket(fd)) {
-          SOCKET s = (SOCKET)_get_osfhandle(fd);
-          if (s != INVALID_SOCKET) {
-            u_long nonBlockingEnabled = 1;
-            res = ioctlsocket(s, FIONBIO, &nonBlockingEnabled);
-          }
-        } else {
-          HANDLE p = (HANDLE)_get_osfhandle(fd);
-          if (GetFileType(p) == FILE_TYPE_PIPE) {
-            DWORD newMode = PIPE_READMODE_BYTE | PIPE_NOWAIT;
-            if (SetNamedPipeHandleState(p, &newMode, nullptr, nullptr)) {
-              res = 0;
-            }
+      // If it's not a socket, it's probably a pipe.
+      if (folly::portability::sockets::is_fh_socket(fd)) {
+        SOCKET s = (SOCKET)_get_osfhandle(fd);
+        if (s != INVALID_SOCKET) {
+          u_long nonBlockingEnabled = (flags & O_NONBLOCK) ? 1 : 0;
+          res = ioctlsocket(s, FIONBIO, &nonBlockingEnabled);
+        }
+      } else {
+        HANDLE p = (HANDLE)_get_osfhandle(fd);
+        if (GetFileType(p) == FILE_TYPE_PIPE) {
+          DWORD newMode = PIPE_READMODE_BYTE;
+          newMode |= (flags & O_NONBLOCK) ? PIPE_NOWAIT : PIPE_WAIT;
+          if (SetNamedPipeHandleState(p, &newMode, nullptr, nullptr)) {
+            res = 0;
           }
         }
       }