/*
- * Copyright 2016 Facebook, Inc.
+ * Copyright 2017 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
#include <stdio.h>
#include <folly/ScopeGuard.h>
+#include <folly/portability/Sockets.h>
#include <folly/portability/SysFile.h>
#include <folly/portability/Unistd.h>
return res;
}
-#if defined(FOLLY_HAVE_PREADV) && !FOLLY_HAVE_PREADV
+#if !FOLLY_HAVE_PREADV
extern "C" ssize_t preadv(int fd, const iovec* iov, int count, off_t offset) {
return wrapPositional(readv, fd, offset, iov, count);
}
#endif
-#if defined(FOLLY_HAVE_PWRITEV) && !FOLLY_HAVE_PWRITEV
+#if !FOLLY_HAVE_PWRITEV
extern "C" ssize_t pwritev(int fd, const iovec* iov, int count, off_t offset) {
return wrapPositional(writev, fd, offset, iov, 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); };
+ SCOPE_EXIT {
+ if (shouldLock) {
+ lockf(fd, F_ULOCK, 0);
+ }
+ };
ssize_t bytesProcessed = 0;
int curIov = 0;
void* curBase = iov[0].iov_base;
size_t curLen = iov[0].iov_len;
while (curIov < count) {
- int res = 0;
+ ssize_t res = 0;
if (isRead) {
res = read(fd, curBase, (unsigned int)curLen);
if (res == 0 && curLen != 0) {
return -1;
}
- if (res == curLen) {
+ if (size_t(res) == curLen) {
curIov++;
if (curIov < count) {
curBase = iov[curIov].iov_base;
curLen = iov[curIov].iov_len;
}
} else {
- curBase += (void*)((char*)curBase + res);
+ curBase = (void*)((char*)curBase + res);
curLen -= res;
}