1/*
2 * Copyright 2016-present Facebook, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <folly/portability/SysUio.h>
18
19#include <errno.h>
20#include <stdio.h>
21
22#include <folly/ScopeGuard.h>
23#include <folly/portability/Sockets.h>
24#include <folly/portability/SysFile.h>
25#include <folly/portability/Unistd.h>
26
27template <class F, class... Args>
28static int wrapPositional(F f, int fd, off_t offset, Args... args) {
29 off_t origLoc = lseek(fd, 0, SEEK_CUR);
30 if (origLoc == off_t(-1)) {
31 return -1;
32 }
33 if (lseek(fd, offset, SEEK_SET) == off_t(-1)) {
34 return -1;
35 }
36
37 int res = (int)f(fd, args...);
38
39 int curErrNo = errno;
40 if (lseek(fd, origLoc, SEEK_SET) == off_t(-1)) {
41 if (res == -1) {
42 errno = curErrNo;
43 }
44 return -1;
45 }
46 errno = curErrNo;
47
48 return res;
49}
50
51#if !FOLLY_HAVE_PREADV
52extern "C" ssize_t preadv(int fd, const iovec* iov, int count, off_t offset) {
53 return wrapPositional(readv, fd, offset, iov, count);
54}
55#endif
56
57#if !FOLLY_HAVE_PWRITEV
58extern "C" ssize_t pwritev(int fd, const iovec* iov, int count, off_t offset) {
59 return wrapPositional(writev, fd, offset, iov, count);
60}
61#endif
62
63#ifdef _WIN32
64template <bool isRead>
65static ssize_t doVecOperation(int fd, const iovec* iov, int count) {
66 if (!count) {
67 return 0;
68 }
69 if (count < 0 || count > folly::kIovMax) {
70 errno = EINVAL;
71 return -1;
72 }
73
74 // We only need to worry about locking if the file descriptor is
75 // not a socket. We have no way of locking sockets :(
76 // The correct way to do this for sockets is via sendmsg/recvmsg,
77 // but this is good enough for now.
78 bool shouldLock = !folly::portability::sockets::is_fh_socket(fd);
79 if (shouldLock && lockf(fd, F_LOCK, 0) == -1) {
80 return -1;
81 }
82 SCOPE_EXIT {
83 if (shouldLock) {
84 lockf(fd, F_ULOCK, 0);
85 }
86 };
87
88 ssize_t bytesProcessed = 0;
89 int curIov = 0;
90 void* curBase = iov[0].iov_base;
91 size_t curLen = iov[0].iov_len;
92 while (curIov < count) {
93 ssize_t res = 0;
94 if (isRead) {
95 res = read(fd, curBase, (unsigned int)curLen);
96 if (res == 0 && curLen != 0) {
97 break; // End of File
98 }
99 } else {
100 res = write(fd, curBase, (unsigned int)curLen);
101 // Write of zero bytes is fine.
102 }
103
104 if (res == -1) {
105 return -1;
106 }
107
108 if (size_t(res) == curLen) {
109 curIov++;
110 if (curIov < count) {
111 curBase = iov[curIov].iov_base;
112 curLen = iov[curIov].iov_len;
113 }
114 } else {
115 curBase = (void*)((char*)curBase + res);
116 curLen -= res;
117 }
118
119 if (bytesProcessed + res < 0) {
120 // Overflow
121 errno = EINVAL;
122 return -1;
123 }
124 bytesProcessed += res;
125 }
126
127 return bytesProcessed;
128}
129
130extern "C" ssize_t readv(int fd, const iovec* iov, int count) {
131 return doVecOperation<true>(fd, iov, count);
132}
133
134extern "C" ssize_t writev(int fd, const iovec* iov, int count) {
135 return doVecOperation<false>(fd, iov, count);
136}
137#endif
138