Tomasz Wiszkowski | a449e9c | 2017-05-25 14:30:56 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 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 | */ |
Greg Hartman | 3c95aac | 2017-06-14 18:24:26 -0700 | [diff] [blame] | 16 | |
| 17 | // TODO: We can't use std::shared_ptr on the older guests due to HALs. |
| 18 | |
Tomasz Wiszkowski | a449e9c | 2017-05-25 14:30:56 -0700 | [diff] [blame] | 19 | #ifndef CUTTLEFISH_COMMON_COMMON_LIBS_FS_SHARED_FD_H_ |
| 20 | #define CUTTLEFISH_COMMON_COMMON_LIBS_FS_SHARED_FD_H_ |
| 21 | |
Tomasz Wiszkowski | 16fb174 | 2017-09-15 14:31:53 -0700 | [diff] [blame] | 22 | #include <sys/epoll.h> |
Tomasz Wiszkowski | a449e9c | 2017-05-25 14:30:56 -0700 | [diff] [blame] | 23 | #include <sys/eventfd.h> |
| 24 | #include <sys/ioctl.h> |
Greg Hartman | 40b88f5 | 2017-06-22 15:34:11 -0700 | [diff] [blame] | 25 | #include <sys/mman.h> |
Tomasz Wiszkowski | a449e9c | 2017-05-25 14:30:56 -0700 | [diff] [blame] | 26 | #include <sys/select.h> |
Tomasz Wiszkowski | 2c35922 | 2017-07-05 14:00:13 -0700 | [diff] [blame] | 27 | #include <sys/socket.h> |
Tomasz Wiszkowski | a449e9c | 2017-05-25 14:30:56 -0700 | [diff] [blame] | 28 | #include <sys/types.h> |
| 29 | #include <sys/stat.h> |
Tomasz Wiszkowski | 2c35922 | 2017-07-05 14:00:13 -0700 | [diff] [blame] | 30 | #include <sys/time.h> |
Tomasz Wiszkowski | af38c7c | 2017-08-14 12:33:11 -0700 | [diff] [blame] | 31 | #include <sys/timerfd.h> |
Tomasz Wiszkowski | a449e9c | 2017-05-25 14:30:56 -0700 | [diff] [blame] | 32 | #include <sys/uio.h> |
| 33 | #include <sys/un.h> |
| 34 | |
| 35 | #include <memory> |
Cody Schuffelen | 4ede79d | 2019-11-13 13:24:12 -0800 | [diff] [blame^] | 36 | #include <sstream> |
Tomasz Wiszkowski | a449e9c | 2017-05-25 14:30:56 -0700 | [diff] [blame] | 37 | |
| 38 | #include <errno.h> |
| 39 | #include <fcntl.h> |
| 40 | #include <string.h> |
| 41 | #include <unistd.h> |
| 42 | |
| 43 | #include "common/libs/auto_resources/auto_resources.h" |
Cody Schuffelen | fa69c38 | 2018-12-17 18:57:36 -0800 | [diff] [blame] | 44 | #include "vm_sockets.h" |
Tomasz Wiszkowski | a449e9c | 2017-05-25 14:30:56 -0700 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * Classes to to enable safe access to files. |
| 48 | * POSIX kernels have an unfortunate habit of recycling file descriptors. |
| 49 | * That can cause problems like http://b/26121457 in code that doesn't manage |
| 50 | * file lifetimes properly. These classes implement an alternate interface |
| 51 | * that has some advantages: |
| 52 | * |
| 53 | * o References to files are tightly controlled |
| 54 | * o Files are auto-closed if they go out of scope |
| 55 | * o Files are life-time aware. It is impossible to close the instance twice. |
| 56 | * o File descriptors are always initialized. By default the descriptor is |
| 57 | * set to a closed instance. |
| 58 | * |
| 59 | * These classes are designed to mimic to POSIX interface as closely as |
| 60 | * possible. Specifically, they don't attempt to track the type of file |
| 61 | * descriptors and expose only the valid operations. This is by design, since |
| 62 | * it makes it easier to convert existing code to SharedFDs and avoids the |
| 63 | * possibility that new POSIX functionality will lead to large refactorings. |
| 64 | */ |
Greg Hartman | 153b106 | 2017-11-11 12:09:21 -0800 | [diff] [blame] | 65 | namespace cvd { |
Tomasz Wiszkowski | a449e9c | 2017-05-25 14:30:56 -0700 | [diff] [blame] | 66 | |
| 67 | class FileInstance; |
| 68 | |
| 69 | /** |
Greg Hartman | 3c95aac | 2017-06-14 18:24:26 -0700 | [diff] [blame] | 70 | * Describes the fields in msghdr that are honored by the *MsgAndFDs |
| 71 | * calls. |
| 72 | */ |
| 73 | struct InbandMessageHeader { |
Tomasz Wiszkowski | 2c35922 | 2017-07-05 14:00:13 -0700 | [diff] [blame] | 74 | void* msg_name; |
| 75 | socklen_t msg_namelen; |
Greg Hartman | 3c95aac | 2017-06-14 18:24:26 -0700 | [diff] [blame] | 76 | struct iovec* msg_iov; |
Tomasz Wiszkowski | 2c35922 | 2017-07-05 14:00:13 -0700 | [diff] [blame] | 77 | size_t msg_iovlen; |
| 78 | int msg_flags; |
Greg Hartman | 3c95aac | 2017-06-14 18:24:26 -0700 | [diff] [blame] | 79 | |
| 80 | void Convert(struct msghdr* dest) const { |
| 81 | dest->msg_name = msg_name; |
| 82 | dest->msg_namelen = msg_namelen; |
| 83 | dest->msg_iov = msg_iov; |
| 84 | dest->msg_iovlen = msg_iovlen; |
| 85 | dest->msg_flags = msg_flags; |
| 86 | } |
| 87 | }; |
| 88 | |
| 89 | /** |
Tomasz Wiszkowski | a449e9c | 2017-05-25 14:30:56 -0700 | [diff] [blame] | 90 | * Counted reference to a FileInstance. |
| 91 | * |
| 92 | * This is also the place where most new FileInstances are created. The creation |
| 93 | * mehtods correspond to the underlying POSIX calls. |
| 94 | * |
| 95 | * SharedFDs can be compared and stored in STL containers. The semantics are |
| 96 | * slightly different from POSIX file descriptors: |
| 97 | * |
| 98 | * o The value of the SharedFD is the identity of its underlying FileInstance. |
| 99 | * |
| 100 | * o Each newly created SharedFD has a unique, closed FileInstance: |
| 101 | * SharedFD a, b; |
| 102 | * assert (a != b); |
| 103 | * a = b; |
| 104 | * asssert(a == b); |
| 105 | * |
| 106 | * o The identity of the FileInstance is not affected by closing the file: |
| 107 | * SharedFD a, b; |
| 108 | * set<SharedFD> s; |
| 109 | * s.insert(a); |
| 110 | * assert(s.count(a) == 1); |
| 111 | * assert(s.count(b) == 0); |
| 112 | * a->Close(); |
| 113 | * assert(s.count(a) == 1); |
| 114 | * assert(s.count(b) == 0); |
| 115 | * |
| 116 | * o FileInstances are never visibly recycled. |
| 117 | * |
| 118 | * o If all of the SharedFDs referring to a FileInstance go out of scope the |
| 119 | * file is closed and the FileInstance is recycled. |
| 120 | * |
| 121 | * Creation methods must ensure that no references to the new file descriptor |
| 122 | * escape. The underlying FileInstance should have the only reference to the |
| 123 | * file descriptor. Any method that needs to know the fd must be in either |
| 124 | * SharedFD or FileInstance. |
| 125 | * |
| 126 | * SharedFDs always have an underlying FileInstance, so all of the method |
| 127 | * calls are safe in accordance with the null object pattern. |
| 128 | * |
| 129 | * Errors on system calls that create new FileInstances, such as Open, are |
| 130 | * reported with a new, closed FileInstance with the errno set. |
| 131 | */ |
| 132 | class SharedFD { |
| 133 | public: |
| 134 | inline SharedFD(); |
Tomasz Wiszkowski | 2c35922 | 2017-07-05 14:00:13 -0700 | [diff] [blame] | 135 | SharedFD(const std::shared_ptr<FileInstance>& in) : value_(in) {} |
Tomasz Wiszkowski | a449e9c | 2017-05-25 14:30:56 -0700 | [diff] [blame] | 136 | // Reference the listener as a FileInstance to make this FD type agnostic. |
Tomasz Wiszkowski | 2c35922 | 2017-07-05 14:00:13 -0700 | [diff] [blame] | 137 | static SharedFD Accept(const FileInstance& listener, struct sockaddr* addr, |
| 138 | socklen_t* addrlen); |
Tomasz Wiszkowski | a449e9c | 2017-05-25 14:30:56 -0700 | [diff] [blame] | 139 | static SharedFD Accept(const FileInstance& listener); |
Greg Hartman | 3c95aac | 2017-06-14 18:24:26 -0700 | [diff] [blame] | 140 | static SharedFD Dup(int unmanaged_fd); |
Tomasz Wiszkowski | a449e9c | 2017-05-25 14:30:56 -0700 | [diff] [blame] | 141 | static SharedFD GetControlSocket(const char* name); |
Jorge E. Moreira | 6ffa0cb | 2018-10-24 17:19:39 -0700 | [diff] [blame] | 142 | // All SharedFDs have the O_CLOEXEC flag after creation. To remove use the |
| 143 | // Fcntl or Dup functions. |
Tomasz Wiszkowski | a449e9c | 2017-05-25 14:30:56 -0700 | [diff] [blame] | 144 | static SharedFD Open(const char* pathname, int flags, mode_t mode = 0); |
Jorge E. Moreira | 6d7753e | 2018-07-18 11:28:48 -0700 | [diff] [blame] | 145 | static SharedFD Creat(const char* pathname, mode_t mode); |
Tomasz Wiszkowski | a449e9c | 2017-05-25 14:30:56 -0700 | [diff] [blame] | 146 | static bool Pipe(SharedFD* fd0, SharedFD* fd1); |
Tomasz Wiszkowski | d88403b | 2017-07-06 14:42:14 -0700 | [diff] [blame] | 147 | static SharedFD Event(int initval = 0, int flags = 0); |
Tomasz Wiszkowski | 16fb174 | 2017-09-15 14:31:53 -0700 | [diff] [blame] | 148 | static SharedFD Epoll(int flags = 0); |
Tomasz Wiszkowski | 2c35922 | 2017-07-05 14:00:13 -0700 | [diff] [blame] | 149 | static bool SocketPair(int domain, int type, int protocol, SharedFD* fd0, |
| 150 | SharedFD* fd1); |
Tomasz Wiszkowski | a449e9c | 2017-05-25 14:30:56 -0700 | [diff] [blame] | 151 | static SharedFD Socket(int domain, int socket_type, int protocol); |
Tomasz Wiszkowski | 2c35922 | 2017-07-05 14:00:13 -0700 | [diff] [blame] | 152 | static SharedFD SocketLocalClient(const char* name, bool is_abstract, |
| 153 | int in_type); |
Ryan Haining | d5c1bde | 2018-01-29 13:34:22 -0800 | [diff] [blame] | 154 | static SharedFD SocketLocalClient(int port, int type); |
Tomasz Wiszkowski | 2c35922 | 2017-07-05 14:00:13 -0700 | [diff] [blame] | 155 | static SharedFD SocketLocalServer(const char* name, bool is_abstract, |
| 156 | int in_type, mode_t mode); |
Jorge E. Moreira | 854bcbc | 2017-09-26 16:05:10 -0700 | [diff] [blame] | 157 | static SharedFD SocketLocalServer(int port, int type); |
Tomasz Wiszkowski | a449e9c | 2017-05-25 14:30:56 -0700 | [diff] [blame] | 158 | static SharedFD SocketSeqPacketServer(const char* name, mode_t mode); |
| 159 | static SharedFD SocketSeqPacketClient(const char* name); |
Cody Schuffelen | fa69c38 | 2018-12-17 18:57:36 -0800 | [diff] [blame] | 160 | static SharedFD VsockServer(unsigned int port, int type); |
| 161 | static SharedFD VsockClient(unsigned int cid, unsigned int port, int type); |
Tomasz Wiszkowski | af38c7c | 2017-08-14 12:33:11 -0700 | [diff] [blame] | 162 | static SharedFD TimerFD(int clock, int flags); |
Tomasz Wiszkowski | a449e9c | 2017-05-25 14:30:56 -0700 | [diff] [blame] | 163 | |
Tomasz Wiszkowski | 2c35922 | 2017-07-05 14:00:13 -0700 | [diff] [blame] | 164 | bool operator==(const SharedFD& rhs) const { return value_ == rhs.value_; } |
Tomasz Wiszkowski | a449e9c | 2017-05-25 14:30:56 -0700 | [diff] [blame] | 165 | |
Tomasz Wiszkowski | 2c35922 | 2017-07-05 14:00:13 -0700 | [diff] [blame] | 166 | bool operator!=(const SharedFD& rhs) const { return value_ != rhs.value_; } |
Tomasz Wiszkowski | a449e9c | 2017-05-25 14:30:56 -0700 | [diff] [blame] | 167 | |
Tomasz Wiszkowski | 2c35922 | 2017-07-05 14:00:13 -0700 | [diff] [blame] | 168 | bool operator<(const SharedFD& rhs) const { return value_ < rhs.value_; } |
Tomasz Wiszkowski | a449e9c | 2017-05-25 14:30:56 -0700 | [diff] [blame] | 169 | |
Tomasz Wiszkowski | 2c35922 | 2017-07-05 14:00:13 -0700 | [diff] [blame] | 170 | bool operator<=(const SharedFD& rhs) const { return value_ <= rhs.value_; } |
Tomasz Wiszkowski | a449e9c | 2017-05-25 14:30:56 -0700 | [diff] [blame] | 171 | |
Tomasz Wiszkowski | 2c35922 | 2017-07-05 14:00:13 -0700 | [diff] [blame] | 172 | bool operator>(const SharedFD& rhs) const { return value_ > rhs.value_; } |
Tomasz Wiszkowski | a449e9c | 2017-05-25 14:30:56 -0700 | [diff] [blame] | 173 | |
Tomasz Wiszkowski | 2c35922 | 2017-07-05 14:00:13 -0700 | [diff] [blame] | 174 | bool operator>=(const SharedFD& rhs) const { return value_ >= rhs.value_; } |
Tomasz Wiszkowski | a449e9c | 2017-05-25 14:30:56 -0700 | [diff] [blame] | 175 | |
Tomasz Wiszkowski | 2c35922 | 2017-07-05 14:00:13 -0700 | [diff] [blame] | 176 | std::shared_ptr<FileInstance> operator->() const { return value_; } |
Tomasz Wiszkowski | a449e9c | 2017-05-25 14:30:56 -0700 | [diff] [blame] | 177 | |
Greg Hartman | 153b106 | 2017-11-11 12:09:21 -0800 | [diff] [blame] | 178 | const cvd::FileInstance& operator*() const { return *value_; } |
Tomasz Wiszkowski | a449e9c | 2017-05-25 14:30:56 -0700 | [diff] [blame] | 179 | |
Greg Hartman | 153b106 | 2017-11-11 12:09:21 -0800 | [diff] [blame] | 180 | cvd::FileInstance& operator*() { return *value_; } |
Tomasz Wiszkowski | a449e9c | 2017-05-25 14:30:56 -0700 | [diff] [blame] | 181 | |
| 182 | private: |
Cody Schuffelen | fa69c38 | 2018-12-17 18:57:36 -0800 | [diff] [blame] | 183 | static SharedFD ErrorFD(int error); |
| 184 | |
Tomasz Wiszkowski | a449e9c | 2017-05-25 14:30:56 -0700 | [diff] [blame] | 185 | std::shared_ptr<FileInstance> value_; |
| 186 | }; |
| 187 | |
| 188 | /** |
| 189 | * Tracks the lifetime of a file descriptor and provides methods to allow |
| 190 | * callers to use the file without knowledge of the underlying descriptor |
| 191 | * number. |
| 192 | * |
| 193 | * FileInstances have two states: Open and Closed. They may start in either |
| 194 | * state. However, once a FileIntance enters the Closed state it cannot be |
| 195 | * reopened. |
| 196 | * |
| 197 | * Construction of FileInstances is limited to select classes to avoid |
| 198 | * escaping file descriptors. At this point SharedFD is the only class |
| 199 | * that has access. We may eventually have ScopedFD and WeakFD. |
| 200 | */ |
| 201 | class FileInstance { |
| 202 | // Give SharedFD access to the aliasing constructor. |
| 203 | friend class SharedFD; |
Tomasz Wiszkowski | 2c35922 | 2017-07-05 14:00:13 -0700 | [diff] [blame] | 204 | |
Tomasz Wiszkowski | a449e9c | 2017-05-25 14:30:56 -0700 | [diff] [blame] | 205 | public: |
Tomasz Wiszkowski | 2c35922 | 2017-07-05 14:00:13 -0700 | [diff] [blame] | 206 | virtual ~FileInstance() { Close(); } |
Tomasz Wiszkowski | a449e9c | 2017-05-25 14:30:56 -0700 | [diff] [blame] | 207 | |
| 208 | // This can't be a singleton because our shared_ptr's aren't thread safe. |
| 209 | static std::shared_ptr<FileInstance> ClosedInstance() { |
| 210 | return std::shared_ptr<FileInstance>(new FileInstance(-1, EBADF)); |
| 211 | } |
| 212 | |
Tomasz Wiszkowski | 2c35922 | 2017-07-05 14:00:13 -0700 | [diff] [blame] | 213 | int Bind(const struct sockaddr* addr, socklen_t addrlen) { |
Tomasz Wiszkowski | a449e9c | 2017-05-25 14:30:56 -0700 | [diff] [blame] | 214 | errno = 0; |
| 215 | int rval = bind(fd_, addr, addrlen); |
| 216 | errno_ = errno; |
| 217 | return rval; |
| 218 | } |
| 219 | |
Tomasz Wiszkowski | 2c35922 | 2017-07-05 14:00:13 -0700 | [diff] [blame] | 220 | int Connect(const struct sockaddr* addr, socklen_t addrlen) { |
Tomasz Wiszkowski | a449e9c | 2017-05-25 14:30:56 -0700 | [diff] [blame] | 221 | errno = 0; |
| 222 | int rval = connect(fd_, addr, addrlen); |
| 223 | errno_ = errno; |
| 224 | return rval; |
| 225 | } |
| 226 | |
| 227 | void Close(); |
| 228 | |
| 229 | // Returns true if the entire input was copied. |
| 230 | // Otherwise an error will be set either on this file or the input. |
| 231 | // The non-const reference is needed to avoid binding this to a particular |
| 232 | // reference type. |
| 233 | bool CopyFrom(FileInstance& in); |
Jorge E. Moreira | cfbb410 | 2018-07-02 18:53:24 -0700 | [diff] [blame] | 234 | bool CopyFrom(FileInstance& in, size_t length); |
Tomasz Wiszkowski | a449e9c | 2017-05-25 14:30:56 -0700 | [diff] [blame] | 235 | |
| 236 | int UNMANAGED_Dup() { |
| 237 | errno = 0; |
| 238 | int rval = TEMP_FAILURE_RETRY(dup(fd_)); |
| 239 | errno_ = errno; |
| 240 | return rval; |
| 241 | } |
| 242 | |
Jorge E. Moreira | 6d7753e | 2018-07-18 11:28:48 -0700 | [diff] [blame] | 243 | int UNMANAGED_Dup2(int newfd) { |
| 244 | errno = 0; |
| 245 | int rval = TEMP_FAILURE_RETRY(dup2(fd_, newfd)); |
| 246 | errno_ = errno; |
| 247 | return rval; |
| 248 | } |
| 249 | |
Greg Hartman | 153b106 | 2017-11-11 12:09:21 -0800 | [diff] [blame] | 250 | int EpollCtl(int op, cvd::SharedFD new_fd, struct epoll_event* event) { |
Tomasz Wiszkowski | 16fb174 | 2017-09-15 14:31:53 -0700 | [diff] [blame] | 251 | errno = 0; |
Greg Hartman | d0f20d4 | 2017-12-19 23:47:26 -0800 | [diff] [blame] | 252 | int rval = TEMP_FAILURE_RETRY(epoll_ctl(fd_, op, new_fd->fd_, event)); |
Tomasz Wiszkowski | 16fb174 | 2017-09-15 14:31:53 -0700 | [diff] [blame] | 253 | errno_ = errno; |
| 254 | return rval; |
| 255 | } |
| 256 | |
| 257 | int EpollWait(struct epoll_event* events, int maxevents, int timeout) { |
| 258 | errno = 0; |
Greg Hartman | d0f20d4 | 2017-12-19 23:47:26 -0800 | [diff] [blame] | 259 | int rval = TEMP_FAILURE_RETRY(epoll_wait(fd_, events, maxevents, timeout)); |
Tomasz Wiszkowski | 16fb174 | 2017-09-15 14:31:53 -0700 | [diff] [blame] | 260 | errno_ = errno; |
| 261 | return rval; |
| 262 | } |
| 263 | |
Tomasz Wiszkowski | a449e9c | 2017-05-25 14:30:56 -0700 | [diff] [blame] | 264 | int Fchown(uid_t owner, gid_t group) { |
| 265 | errno = 0; |
| 266 | int rval = TEMP_FAILURE_RETRY(fchown(fd_, owner, group)); |
| 267 | errno_ = errno; |
| 268 | return rval; |
| 269 | } |
| 270 | |
| 271 | int Fcntl(int command, int value) { |
| 272 | errno = 0; |
| 273 | int rval = TEMP_FAILURE_RETRY(fcntl(fd_, command, value)); |
| 274 | errno_ = errno; |
| 275 | return rval; |
| 276 | } |
| 277 | |
| 278 | int Fstat(struct stat* buf) { |
| 279 | errno = 0; |
| 280 | int rval = TEMP_FAILURE_RETRY(fstat(fd_, buf)); |
| 281 | errno_ = errno; |
| 282 | return rval; |
| 283 | } |
| 284 | |
Tomasz Wiszkowski | 2c35922 | 2017-07-05 14:00:13 -0700 | [diff] [blame] | 285 | int GetErrno() const { return errno_; } |
Tomasz Wiszkowski | a449e9c | 2017-05-25 14:30:56 -0700 | [diff] [blame] | 286 | |
| 287 | int GetSockOpt(int level, int optname, void* optval, socklen_t* optlen) { |
| 288 | errno = 0; |
| 289 | int rval = getsockopt(fd_, level, optname, optval, optlen); |
| 290 | if (rval == -1) { |
| 291 | errno_ = errno; |
| 292 | } |
| 293 | return rval; |
| 294 | } |
| 295 | |
| 296 | void Identify(const char* identity); |
| 297 | |
Greg Hartman | 40b88f5 | 2017-06-22 15:34:11 -0700 | [diff] [blame] | 298 | int Ioctl(int request, void* val = nullptr) { |
Tomasz Wiszkowski | a449e9c | 2017-05-25 14:30:56 -0700 | [diff] [blame] | 299 | errno = 0; |
Greg Hartman | 40b88f5 | 2017-06-22 15:34:11 -0700 | [diff] [blame] | 300 | int rval = TEMP_FAILURE_RETRY(ioctl(fd_, request, val)); |
Tomasz Wiszkowski | a449e9c | 2017-05-25 14:30:56 -0700 | [diff] [blame] | 301 | errno_ = errno; |
| 302 | return rval; |
| 303 | } |
| 304 | |
Tomasz Wiszkowski | 2c35922 | 2017-07-05 14:00:13 -0700 | [diff] [blame] | 305 | bool IsOpen() const { return fd_ != -1; } |
Tomasz Wiszkowski | a449e9c | 2017-05-25 14:30:56 -0700 | [diff] [blame] | 306 | |
| 307 | // in probably isn't modified, but the API spec doesn't have const. |
| 308 | bool IsSet(fd_set* in) const; |
| 309 | |
| 310 | int Listen(int backlog) { |
| 311 | errno = 0; |
| 312 | int rval = listen(fd_, backlog); |
| 313 | errno_ = errno; |
| 314 | return rval; |
| 315 | } |
| 316 | |
| 317 | static void Log(const char* message); |
| 318 | |
| 319 | off_t LSeek(off_t offset, int whence) { |
| 320 | errno = 0; |
| 321 | off_t rval = TEMP_FAILURE_RETRY(lseek(fd_, offset, whence)); |
| 322 | errno_ = errno; |
| 323 | return rval; |
| 324 | } |
| 325 | |
Tomasz Wiszkowski | 2c35922 | 2017-07-05 14:00:13 -0700 | [diff] [blame] | 326 | void* Mmap(void* addr, size_t length, int prot, int flags, off_t offset) { |
Greg Hartman | 40b88f5 | 2017-06-22 15:34:11 -0700 | [diff] [blame] | 327 | errno = 0; |
Tomasz Wiszkowski | 2c35922 | 2017-07-05 14:00:13 -0700 | [diff] [blame] | 328 | void* rval = mmap(addr, length, prot, flags, fd_, offset); |
Greg Hartman | 40b88f5 | 2017-06-22 15:34:11 -0700 | [diff] [blame] | 329 | errno_ = errno; |
| 330 | return rval; |
| 331 | } |
| 332 | |
| 333 | ssize_t Pread(void* buf, size_t count, off_t offset) { |
| 334 | errno = 0; |
| 335 | ssize_t rval = TEMP_FAILURE_RETRY(pread(fd_, buf, count, offset)); |
| 336 | errno_ = errno; |
| 337 | return rval; |
| 338 | } |
| 339 | |
Tomasz Wiszkowski | a449e9c | 2017-05-25 14:30:56 -0700 | [diff] [blame] | 340 | ssize_t Recv(void* buf, size_t len, int flags) { |
| 341 | errno = 0; |
| 342 | ssize_t rval = TEMP_FAILURE_RETRY(recv(fd_, buf, len, flags)); |
| 343 | errno_ = errno; |
| 344 | return rval; |
| 345 | } |
| 346 | |
| 347 | ssize_t RecvFrom(void* buf, size_t len, int flags, struct sockaddr* src_addr, |
| 348 | socklen_t* addr_len) { |
| 349 | errno = 0; |
Tomasz Wiszkowski | 2c35922 | 2017-07-05 14:00:13 -0700 | [diff] [blame] | 350 | ssize_t rval = |
| 351 | TEMP_FAILURE_RETRY(recvfrom(fd_, buf, len, flags, src_addr, addr_len)); |
Tomasz Wiszkowski | a449e9c | 2017-05-25 14:30:56 -0700 | [diff] [blame] | 352 | errno_ = errno; |
| 353 | return rval; |
| 354 | } |
| 355 | |
| 356 | ssize_t RecvMsg(struct msghdr* msg, int flags) { |
| 357 | errno = 0; |
| 358 | ssize_t rval = TEMP_FAILURE_RETRY(recvmsg(fd_, msg, flags)); |
| 359 | errno_ = errno; |
| 360 | return rval; |
| 361 | } |
| 362 | |
Tomasz Wiszkowski | 2c35922 | 2017-07-05 14:00:13 -0700 | [diff] [blame] | 363 | template <size_t SZ> |
| 364 | ssize_t RecvMsgAndFDs(const struct InbandMessageHeader& msg_in, int flags, |
| 365 | SharedFD (*new_fds)[SZ]) { |
Greg Hartman | 3c95aac | 2017-06-14 18:24:26 -0700 | [diff] [blame] | 366 | // We need to make some modifications to land the fds. Make it clear |
| 367 | // that there are no updates to the msg being passed in during this call. |
| 368 | struct msghdr msg; |
| 369 | msg_in.Convert(&msg); |
| 370 | union { |
| 371 | char buffer[CMSG_SPACE(SZ * sizeof(int))]; |
| 372 | struct cmsghdr this_aligns_buffer; |
| 373 | } u; |
Tomasz Wiszkowski | 2c35922 | 2017-07-05 14:00:13 -0700 | [diff] [blame] | 374 | msg.msg_control = u.buffer; |
Greg Hartman | 3c95aac | 2017-06-14 18:24:26 -0700 | [diff] [blame] | 375 | msg.msg_controllen = sizeof(u.buffer); |
| 376 | |
| 377 | cmsghdr* cmsg = CMSG_FIRSTHDR(&msg); |
Tomasz Wiszkowski | 2c35922 | 2017-07-05 14:00:13 -0700 | [diff] [blame] | 378 | cmsg->cmsg_len = CMSG_LEN(SZ * sizeof(int)); |
Greg Hartman | 3c95aac | 2017-06-14 18:24:26 -0700 | [diff] [blame] | 379 | cmsg->cmsg_level = SOL_SOCKET; |
Tomasz Wiszkowski | 2c35922 | 2017-07-05 14:00:13 -0700 | [diff] [blame] | 380 | cmsg->cmsg_type = SCM_RIGHTS; |
Greg Hartman | 3c95aac | 2017-06-14 18:24:26 -0700 | [diff] [blame] | 381 | int* fd_array = reinterpret_cast<int*>(CMSG_DATA(cmsg)); |
Greg Hartman | ee76f04 | 2017-12-04 18:33:51 -0800 | [diff] [blame] | 382 | for (size_t i = 0; i < SZ; ++i) { |
Greg Hartman | 3c95aac | 2017-06-14 18:24:26 -0700 | [diff] [blame] | 383 | fd_array[i] = -1; |
| 384 | } |
| 385 | ssize_t rval = RecvMsg(&msg, flags); |
Greg Hartman | ee76f04 | 2017-12-04 18:33:51 -0800 | [diff] [blame] | 386 | for (size_t i = 0; i < SZ; ++i) { |
Greg Hartman | 3c95aac | 2017-06-14 18:24:26 -0700 | [diff] [blame] | 387 | (*new_fds)[i] = |
| 388 | std::shared_ptr<FileInstance>(new FileInstance(fd_array[i], errno)); |
| 389 | } |
| 390 | return rval; |
| 391 | } |
| 392 | |
Tomasz Wiszkowski | a449e9c | 2017-05-25 14:30:56 -0700 | [diff] [blame] | 393 | ssize_t Read(void* buf, size_t count) { |
| 394 | errno = 0; |
| 395 | ssize_t rval = TEMP_FAILURE_RETRY(read(fd_, buf, count)); |
| 396 | errno_ = errno; |
| 397 | return rval; |
| 398 | } |
| 399 | |
| 400 | ssize_t Send(const void* buf, size_t len, int flags) { |
| 401 | errno = 0; |
| 402 | ssize_t rval = TEMP_FAILURE_RETRY(send(fd_, buf, len, flags)); |
| 403 | errno_ = errno; |
| 404 | return rval; |
| 405 | } |
| 406 | |
| 407 | ssize_t SendMsg(const struct msghdr* msg, int flags) { |
| 408 | errno = 0; |
| 409 | ssize_t rval = TEMP_FAILURE_RETRY(sendmsg(fd_, msg, flags)); |
| 410 | errno_ = errno; |
| 411 | return rval; |
| 412 | } |
| 413 | |
Tomasz Wiszkowski | 2c35922 | 2017-07-05 14:00:13 -0700 | [diff] [blame] | 414 | template <size_t SZ> |
| 415 | ssize_t SendMsgAndFDs(const struct InbandMessageHeader& msg_in, int flags, |
| 416 | const SharedFD (&fds)[SZ]) { |
Greg Hartman | 3c95aac | 2017-06-14 18:24:26 -0700 | [diff] [blame] | 417 | struct msghdr msg; |
| 418 | msg_in.Convert(&msg); |
| 419 | union { |
| 420 | char buffer[CMSG_SPACE(SZ * sizeof(int))]; |
| 421 | struct cmsghdr this_aligns_buffer; |
| 422 | } u; |
Tomasz Wiszkowski | 2c35922 | 2017-07-05 14:00:13 -0700 | [diff] [blame] | 423 | msg.msg_control = u.buffer; |
Greg Hartman | 3c95aac | 2017-06-14 18:24:26 -0700 | [diff] [blame] | 424 | msg.msg_controllen = sizeof(u.buffer); |
| 425 | |
| 426 | cmsghdr* cmsg = CMSG_FIRSTHDR(&msg); |
Tomasz Wiszkowski | 2c35922 | 2017-07-05 14:00:13 -0700 | [diff] [blame] | 427 | cmsg->cmsg_len = CMSG_LEN(SZ * sizeof(int)); |
Greg Hartman | 3c95aac | 2017-06-14 18:24:26 -0700 | [diff] [blame] | 428 | cmsg->cmsg_level = SOL_SOCKET; |
Tomasz Wiszkowski | 2c35922 | 2017-07-05 14:00:13 -0700 | [diff] [blame] | 429 | cmsg->cmsg_type = SCM_RIGHTS; |
Greg Hartman | 3c95aac | 2017-06-14 18:24:26 -0700 | [diff] [blame] | 430 | int* fd_array = reinterpret_cast<int*>(CMSG_DATA(cmsg)); |
Greg Hartman | 7d5e0bf | 2017-12-19 23:48:34 -0800 | [diff] [blame] | 431 | for (size_t i = 0; i < SZ; ++i) { |
Greg Hartman | 3c95aac | 2017-06-14 18:24:26 -0700 | [diff] [blame] | 432 | fd_array[i] = fds[i]->fd_; |
| 433 | } |
| 434 | return SendMsg(&msg, flags); |
| 435 | } |
| 436 | |
Ryan Haining | 732b460 | 2018-02-16 16:44:47 -0800 | [diff] [blame] | 437 | int Shutdown(int how) { |
| 438 | errno = 0; |
| 439 | int rval = shutdown(fd_, how); |
| 440 | errno_ = errno; |
| 441 | return rval; |
| 442 | } |
| 443 | |
Tomasz Wiszkowski | 2c35922 | 2017-07-05 14:00:13 -0700 | [diff] [blame] | 444 | ssize_t SendTo(const void* buf, size_t len, int flags, |
| 445 | const struct sockaddr* dest_addr, socklen_t addrlen) { |
Tomasz Wiszkowski | a449e9c | 2017-05-25 14:30:56 -0700 | [diff] [blame] | 446 | errno = 0; |
Tomasz Wiszkowski | 2c35922 | 2017-07-05 14:00:13 -0700 | [diff] [blame] | 447 | ssize_t rval = |
| 448 | TEMP_FAILURE_RETRY(sendto(fd_, buf, len, flags, dest_addr, addrlen)); |
Tomasz Wiszkowski | a449e9c | 2017-05-25 14:30:56 -0700 | [diff] [blame] | 449 | errno_ = errno; |
| 450 | return rval; |
| 451 | } |
| 452 | |
| 453 | void Set(fd_set* dest, int* max_index) const; |
| 454 | |
Tomasz Wiszkowski | 2c35922 | 2017-07-05 14:00:13 -0700 | [diff] [blame] | 455 | int SetSockOpt(int level, int optname, const void* optval, socklen_t optlen) { |
Tomasz Wiszkowski | a449e9c | 2017-05-25 14:30:56 -0700 | [diff] [blame] | 456 | errno = 0; |
| 457 | int rval = setsockopt(fd_, level, optname, optval, optlen); |
| 458 | errno_ = errno; |
| 459 | return rval; |
| 460 | } |
| 461 | |
| 462 | const char* StrError() const { |
| 463 | errno = 0; |
| 464 | FileInstance* s = const_cast<FileInstance*>(this); |
| 465 | char* out = strerror_r(errno_, s->strerror_buf_, sizeof(strerror_buf_)); |
| 466 | |
| 467 | // From man page: |
| 468 | // strerror_r() returns a pointer to a string containing the error message. |
| 469 | // This may be either a pointer to a string that the function stores in |
| 470 | // buf, or a pointer to some (immutable) static string (in which case buf |
| 471 | // is unused). |
| 472 | if (out != s->strerror_buf_) { |
Greg Hartman | 40b88f5 | 2017-06-22 15:34:11 -0700 | [diff] [blame] | 473 | strncpy(s->strerror_buf_, out, sizeof(strerror_buf_)); |
Tomasz Wiszkowski | a449e9c | 2017-05-25 14:30:56 -0700 | [diff] [blame] | 474 | } |
| 475 | return strerror_buf_; |
| 476 | } |
| 477 | |
Tomasz Wiszkowski | af38c7c | 2017-08-14 12:33:11 -0700 | [diff] [blame] | 478 | int TimerGet(struct itimerspec* curr_value) { |
| 479 | errno = 0; |
| 480 | int rval = timerfd_gettime(fd_, curr_value); |
| 481 | errno_ = errno; |
| 482 | return rval; |
| 483 | } |
| 484 | |
| 485 | int TimerSet(int flags, const struct itimerspec* new_value, |
Greg Hartman | d0f20d4 | 2017-12-19 23:47:26 -0800 | [diff] [blame] | 486 | struct itimerspec* old_value) { |
Tomasz Wiszkowski | af38c7c | 2017-08-14 12:33:11 -0700 | [diff] [blame] | 487 | errno = 0; |
| 488 | int rval = timerfd_settime(fd_, flags, new_value, old_value); |
| 489 | errno_ = errno; |
| 490 | return rval; |
| 491 | } |
| 492 | |
Tomasz Wiszkowski | 2c35922 | 2017-07-05 14:00:13 -0700 | [diff] [blame] | 493 | ssize_t Truncate(off_t length) { |
| 494 | errno = 0; |
| 495 | ssize_t rval = TEMP_FAILURE_RETRY(ftruncate(fd_, length)); |
| 496 | errno_ = errno; |
| 497 | return rval; |
| 498 | } |
| 499 | |
Tomasz Wiszkowski | a449e9c | 2017-05-25 14:30:56 -0700 | [diff] [blame] | 500 | ssize_t Write(const void* buf, size_t count) { |
| 501 | errno = 0; |
| 502 | ssize_t rval = TEMP_FAILURE_RETRY(write(fd_, buf, count)); |
| 503 | errno_ = errno; |
| 504 | return rval; |
| 505 | } |
| 506 | |
| 507 | ssize_t WriteV(struct iovec* iov, int iovcount) { |
| 508 | errno = 0; |
| 509 | ssize_t rval = TEMP_FAILURE_RETRY(writev(fd_, iov, iovcount)); |
| 510 | errno_ = errno; |
| 511 | return rval; |
| 512 | } |
| 513 | |
| 514 | private: |
| 515 | FileInstance(int fd, int in_errno) : fd_(fd), errno_(in_errno) { |
Jorge E. Moreira | 6ffa0cb | 2018-10-24 17:19:39 -0700 | [diff] [blame] | 516 | // Ensure every file descriptor managed by a FileInstance has the CLOEXEC |
| 517 | // flag |
| 518 | TEMP_FAILURE_RETRY(fcntl(fd, F_SETFD, FD_CLOEXEC)); |
Cody Schuffelen | 4ede79d | 2019-11-13 13:24:12 -0800 | [diff] [blame^] | 519 | std::stringstream identity; |
| 520 | identity << "fd=" << fd << " @" << this; |
| 521 | identity_ = identity.str(); |
Tomasz Wiszkowski | a449e9c | 2017-05-25 14:30:56 -0700 | [diff] [blame] | 522 | } |
| 523 | |
Tomasz Wiszkowski | 2c35922 | 2017-07-05 14:00:13 -0700 | [diff] [blame] | 524 | FileInstance* Accept(struct sockaddr* addr, socklen_t* addrlen) const { |
Tomasz Wiszkowski | a449e9c | 2017-05-25 14:30:56 -0700 | [diff] [blame] | 525 | int fd = TEMP_FAILURE_RETRY(accept(fd_, addr, addrlen)); |
| 526 | if (fd == -1) { |
| 527 | return new FileInstance(fd, errno); |
| 528 | } else { |
| 529 | return new FileInstance(fd, 0); |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | int fd_; |
| 534 | int errno_; |
Cody Schuffelen | 4ede79d | 2019-11-13 13:24:12 -0800 | [diff] [blame^] | 535 | std::string identity_; |
Tomasz Wiszkowski | a449e9c | 2017-05-25 14:30:56 -0700 | [diff] [blame] | 536 | char strerror_buf_[160]; |
| 537 | }; |
| 538 | |
| 539 | /* Methods that need both a fully defined SharedFD and a fully defined |
| 540 | FileInstance. */ |
| 541 | |
Ryan Haining | 412a692 | 2018-12-19 14:43:09 -0800 | [diff] [blame] | 542 | inline SharedFD::SharedFD() : value_(FileInstance::ClosedInstance()) {} |
Tomasz Wiszkowski | a449e9c | 2017-05-25 14:30:56 -0700 | [diff] [blame] | 543 | |
Greg Hartman | 153b106 | 2017-11-11 12:09:21 -0800 | [diff] [blame] | 544 | } // namespace cvd |
Tomasz Wiszkowski | a449e9c | 2017-05-25 14:30:56 -0700 | [diff] [blame] | 545 | |
| 546 | #endif // CUTTLEFISH_COMMON_COMMON_LIBS_FS_SHARED_FD_H_ |