Don't attempt to lock the file descriptor in readv/writev if it's a socket
authorChristopher Dykes <cdykes@fb.com>
Sun, 2 Jul 2017 06:50:37 +0000 (23:50 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Sun, 2 Jul 2017 06:57:43 +0000 (23:57 -0700)
Summary:
There is no way to lock a socket on Windows, so these calls will always fail. Just don't do the lock on Windows.
To do it properly with sockets, this should probably be forwarding to `sendmsg`/`recvmsg`, but this is good enough for now.

Reviewed By: simpkins

Differential Revision: D5307901

fbshipit-source-id: 9855274e932a3e2ec3cacae10363200187e0c01b

folly/portability/SysUio.cpp

index 9a80d27c93a9555adfb4b7ec889e2302fc1a6ba0..74196c0029af1b08bb2fecf3b7c31d9e736a97ef 100644 (file)
@@ -70,11 +70,18 @@ static ssize_t doVecOperation(int fd, const iovec* iov, int count) {
     return -1;
   }
 
-  if (lockf(fd, F_LOCK, 0) == -1) {
+  // We only need to worry about locking if the file descriptor is
+  // not a socket. We have no way of locking sockets :(
+  // The correct way to do this for sockets is via sendmsg/recvmsg,
+  // but this is good enough for now.
+  bool shouldLock = !folly::portability::sockets::is_fh_socket(fd);
+  if (shouldLock && lockf(fd, F_LOCK, 0) == -1) {
     return -1;
   }
   SCOPE_EXIT {
-    lockf(fd, F_ULOCK, 0);
+    if (shouldLock) {
+      lockf(fd, F_ULOCK, 0);
+    }
   };
 
   ssize_t bytesProcessed = 0;