allow AsyncSignalHandler to attach and detach from an EventBase
[folly.git] / folly / portability / SysUio.cpp
1 /*
2  * Copyright 2017 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/SysFile.h>
24 #include <folly/portability/Unistd.h>
25
26 template <class F, class... Args>
27 static int wrapPositional(F f, int fd, off_t offset, Args... args) {
28   off_t origLoc = lseek(fd, 0, SEEK_CUR);
29   if (origLoc == off_t(-1)) {
30     return -1;
31   }
32   if (lseek(fd, offset, SEEK_SET) == off_t(-1)) {
33     return -1;
34   }
35
36   int res = (int)f(fd, args...);
37
38   int curErrNo = errno;
39   if (lseek(fd, origLoc, SEEK_SET) == off_t(-1)) {
40     if (res == -1) {
41       errno = curErrNo;
42     }
43     return -1;
44   }
45   errno = curErrNo;
46
47   return res;
48 }
49
50 #if !FOLLY_HAVE_PREADV
51 extern "C" ssize_t preadv(int fd, const iovec* iov, int count, off_t offset) {
52   return wrapPositional(readv, fd, offset, iov, count);
53 }
54 #endif
55
56 #if !FOLLY_HAVE_PWRITEV
57 extern "C" ssize_t pwritev(int fd, const iovec* iov, int count, off_t offset) {
58   return wrapPositional(writev, fd, offset, iov, count);
59 }
60 #endif
61
62 #ifdef _WIN32
63 template <bool isRead>
64 static ssize_t doVecOperation(int fd, const iovec* iov, int count) {
65   if (!count) {
66     return 0;
67   }
68   if (count < 0 || count > folly::kIovMax) {
69     errno = EINVAL;
70     return -1;
71   }
72
73   if (lockf(fd, F_LOCK, 0) == -1) {
74     return -1;
75   }
76   SCOPE_EXIT {
77     lockf(fd, F_ULOCK, 0);
78   };
79
80   ssize_t bytesProcessed = 0;
81   int curIov = 0;
82   void* curBase = iov[0].iov_base;
83   size_t curLen = iov[0].iov_len;
84   while (curIov < count) {
85     ssize_t res = 0;
86     if (isRead) {
87       res = read(fd, curBase, (unsigned int)curLen);
88       if (res == 0 && curLen != 0) {
89         break; // End of File
90       }
91     } else {
92       res = write(fd, curBase, (unsigned int)curLen);
93       // Write of zero bytes is fine.
94     }
95
96     if (res == -1) {
97       return -1;
98     }
99
100     if (size_t(res) == curLen) {
101       curIov++;
102       if (curIov < count) {
103         curBase = iov[curIov].iov_base;
104         curLen = iov[curIov].iov_len;
105       }
106     } else {
107       curBase = (void*)((char*)curBase + res);
108       curLen -= res;
109     }
110
111     if (bytesProcessed + res < 0) {
112       // Overflow
113       errno = EINVAL;
114       return -1;
115     }
116     bytesProcessed += res;
117   }
118
119   return bytesProcessed;
120 }
121
122 extern "C" ssize_t readv(int fd, const iovec* iov, int count) {
123   return doVecOperation<true>(fd, iov, count);
124 }
125
126 extern "C" ssize_t writev(int fd, const iovec* iov, int count) {
127   return doVecOperation<false>(fd, iov, count);
128 }
129 #endif