blob: b64ad091b15ad0be0689f3332e811dab0b9c6bb9 [file] [log] [blame]
Tomasz Wiszkowskia449e9c2017-05-25 14:30:56 -07001/*
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 Hartman3c95aac2017-06-14 18:24:26 -070016
17// TODO: We can't use std::shared_ptr on the older guests due to HALs.
18
Tomasz Wiszkowskia449e9c2017-05-25 14:30:56 -070019#ifndef CUTTLEFISH_COMMON_COMMON_LIBS_FS_SHARED_FD_H_
20#define CUTTLEFISH_COMMON_COMMON_LIBS_FS_SHARED_FD_H_
21
Tomasz Wiszkowski16fb1742017-09-15 14:31:53 -070022#include <sys/epoll.h>
Tomasz Wiszkowskia449e9c2017-05-25 14:30:56 -070023#include <sys/eventfd.h>
24#include <sys/ioctl.h>
Greg Hartman40b88f52017-06-22 15:34:11 -070025#include <sys/mman.h>
Tomasz Wiszkowskia449e9c2017-05-25 14:30:56 -070026#include <sys/select.h>
Tomasz Wiszkowski2c359222017-07-05 14:00:13 -070027#include <sys/socket.h>
Tomasz Wiszkowskia449e9c2017-05-25 14:30:56 -070028#include <sys/types.h>
29#include <sys/stat.h>
Tomasz Wiszkowski2c359222017-07-05 14:00:13 -070030#include <sys/time.h>
Tomasz Wiszkowskiaf38c7c2017-08-14 12:33:11 -070031#include <sys/timerfd.h>
Tomasz Wiszkowskia449e9c2017-05-25 14:30:56 -070032#include <sys/uio.h>
33#include <sys/un.h>
34
35#include <memory>
Cody Schuffelen4ede79d2019-11-13 13:24:12 -080036#include <sstream>
Tomasz Wiszkowskia449e9c2017-05-25 14:30:56 -070037
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 Schuffelenfa69c382018-12-17 18:57:36 -080044#include "vm_sockets.h"
Tomasz Wiszkowskia449e9c2017-05-25 14:30:56 -070045
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 Hartman153b1062017-11-11 12:09:21 -080065namespace cvd {
Tomasz Wiszkowskia449e9c2017-05-25 14:30:56 -070066
67class FileInstance;
68
69/**
Greg Hartman3c95aac2017-06-14 18:24:26 -070070 * Describes the fields in msghdr that are honored by the *MsgAndFDs
71 * calls.
72 */
73struct InbandMessageHeader {
Tomasz Wiszkowski2c359222017-07-05 14:00:13 -070074 void* msg_name;
75 socklen_t msg_namelen;
Greg Hartman3c95aac2017-06-14 18:24:26 -070076 struct iovec* msg_iov;
Tomasz Wiszkowski2c359222017-07-05 14:00:13 -070077 size_t msg_iovlen;
78 int msg_flags;
Greg Hartman3c95aac2017-06-14 18:24:26 -070079
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 Wiszkowskia449e9c2017-05-25 14:30:56 -070090 * 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 */
132class SharedFD {
133 public:
134 inline SharedFD();
Tomasz Wiszkowski2c359222017-07-05 14:00:13 -0700135 SharedFD(const std::shared_ptr<FileInstance>& in) : value_(in) {}
Tomasz Wiszkowskia449e9c2017-05-25 14:30:56 -0700136 // Reference the listener as a FileInstance to make this FD type agnostic.
Tomasz Wiszkowski2c359222017-07-05 14:00:13 -0700137 static SharedFD Accept(const FileInstance& listener, struct sockaddr* addr,
138 socklen_t* addrlen);
Tomasz Wiszkowskia449e9c2017-05-25 14:30:56 -0700139 static SharedFD Accept(const FileInstance& listener);
Greg Hartman3c95aac2017-06-14 18:24:26 -0700140 static SharedFD Dup(int unmanaged_fd);
Tomasz Wiszkowskia449e9c2017-05-25 14:30:56 -0700141 static SharedFD GetControlSocket(const char* name);
Jorge E. Moreira6ffa0cb2018-10-24 17:19:39 -0700142 // All SharedFDs have the O_CLOEXEC flag after creation. To remove use the
143 // Fcntl or Dup functions.
Tomasz Wiszkowskia449e9c2017-05-25 14:30:56 -0700144 static SharedFD Open(const char* pathname, int flags, mode_t mode = 0);
Jorge E. Moreira6d7753e2018-07-18 11:28:48 -0700145 static SharedFD Creat(const char* pathname, mode_t mode);
Tomasz Wiszkowskia449e9c2017-05-25 14:30:56 -0700146 static bool Pipe(SharedFD* fd0, SharedFD* fd1);
Tomasz Wiszkowskid88403b2017-07-06 14:42:14 -0700147 static SharedFD Event(int initval = 0, int flags = 0);
Tomasz Wiszkowski16fb1742017-09-15 14:31:53 -0700148 static SharedFD Epoll(int flags = 0);
Tomasz Wiszkowski2c359222017-07-05 14:00:13 -0700149 static bool SocketPair(int domain, int type, int protocol, SharedFD* fd0,
150 SharedFD* fd1);
Tomasz Wiszkowskia449e9c2017-05-25 14:30:56 -0700151 static SharedFD Socket(int domain, int socket_type, int protocol);
Tomasz Wiszkowski2c359222017-07-05 14:00:13 -0700152 static SharedFD SocketLocalClient(const char* name, bool is_abstract,
153 int in_type);
Ryan Hainingd5c1bde2018-01-29 13:34:22 -0800154 static SharedFD SocketLocalClient(int port, int type);
Tomasz Wiszkowski2c359222017-07-05 14:00:13 -0700155 static SharedFD SocketLocalServer(const char* name, bool is_abstract,
156 int in_type, mode_t mode);
Jorge E. Moreira854bcbc2017-09-26 16:05:10 -0700157 static SharedFD SocketLocalServer(int port, int type);
Tomasz Wiszkowskia449e9c2017-05-25 14:30:56 -0700158 static SharedFD SocketSeqPacketServer(const char* name, mode_t mode);
159 static SharedFD SocketSeqPacketClient(const char* name);
Cody Schuffelenfa69c382018-12-17 18:57:36 -0800160 static SharedFD VsockServer(unsigned int port, int type);
161 static SharedFD VsockClient(unsigned int cid, unsigned int port, int type);
Tomasz Wiszkowskiaf38c7c2017-08-14 12:33:11 -0700162 static SharedFD TimerFD(int clock, int flags);
Tomasz Wiszkowskia449e9c2017-05-25 14:30:56 -0700163
Tomasz Wiszkowski2c359222017-07-05 14:00:13 -0700164 bool operator==(const SharedFD& rhs) const { return value_ == rhs.value_; }
Tomasz Wiszkowskia449e9c2017-05-25 14:30:56 -0700165
Tomasz Wiszkowski2c359222017-07-05 14:00:13 -0700166 bool operator!=(const SharedFD& rhs) const { return value_ != rhs.value_; }
Tomasz Wiszkowskia449e9c2017-05-25 14:30:56 -0700167
Tomasz Wiszkowski2c359222017-07-05 14:00:13 -0700168 bool operator<(const SharedFD& rhs) const { return value_ < rhs.value_; }
Tomasz Wiszkowskia449e9c2017-05-25 14:30:56 -0700169
Tomasz Wiszkowski2c359222017-07-05 14:00:13 -0700170 bool operator<=(const SharedFD& rhs) const { return value_ <= rhs.value_; }
Tomasz Wiszkowskia449e9c2017-05-25 14:30:56 -0700171
Tomasz Wiszkowski2c359222017-07-05 14:00:13 -0700172 bool operator>(const SharedFD& rhs) const { return value_ > rhs.value_; }
Tomasz Wiszkowskia449e9c2017-05-25 14:30:56 -0700173
Tomasz Wiszkowski2c359222017-07-05 14:00:13 -0700174 bool operator>=(const SharedFD& rhs) const { return value_ >= rhs.value_; }
Tomasz Wiszkowskia449e9c2017-05-25 14:30:56 -0700175
Tomasz Wiszkowski2c359222017-07-05 14:00:13 -0700176 std::shared_ptr<FileInstance> operator->() const { return value_; }
Tomasz Wiszkowskia449e9c2017-05-25 14:30:56 -0700177
Greg Hartman153b1062017-11-11 12:09:21 -0800178 const cvd::FileInstance& operator*() const { return *value_; }
Tomasz Wiszkowskia449e9c2017-05-25 14:30:56 -0700179
Greg Hartman153b1062017-11-11 12:09:21 -0800180 cvd::FileInstance& operator*() { return *value_; }
Tomasz Wiszkowskia449e9c2017-05-25 14:30:56 -0700181
182 private:
Cody Schuffelenfa69c382018-12-17 18:57:36 -0800183 static SharedFD ErrorFD(int error);
184
Tomasz Wiszkowskia449e9c2017-05-25 14:30:56 -0700185 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 */
201class FileInstance {
202 // Give SharedFD access to the aliasing constructor.
203 friend class SharedFD;
Tomasz Wiszkowski2c359222017-07-05 14:00:13 -0700204
Tomasz Wiszkowskia449e9c2017-05-25 14:30:56 -0700205 public:
Tomasz Wiszkowski2c359222017-07-05 14:00:13 -0700206 virtual ~FileInstance() { Close(); }
Tomasz Wiszkowskia449e9c2017-05-25 14:30:56 -0700207
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 Wiszkowski2c359222017-07-05 14:00:13 -0700213 int Bind(const struct sockaddr* addr, socklen_t addrlen) {
Tomasz Wiszkowskia449e9c2017-05-25 14:30:56 -0700214 errno = 0;
215 int rval = bind(fd_, addr, addrlen);
216 errno_ = errno;
217 return rval;
218 }
219
Tomasz Wiszkowski2c359222017-07-05 14:00:13 -0700220 int Connect(const struct sockaddr* addr, socklen_t addrlen) {
Tomasz Wiszkowskia449e9c2017-05-25 14:30:56 -0700221 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. Moreiracfbb4102018-07-02 18:53:24 -0700234 bool CopyFrom(FileInstance& in, size_t length);
Tomasz Wiszkowskia449e9c2017-05-25 14:30:56 -0700235
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. Moreira6d7753e2018-07-18 11:28:48 -0700243 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 Hartman153b1062017-11-11 12:09:21 -0800250 int EpollCtl(int op, cvd::SharedFD new_fd, struct epoll_event* event) {
Tomasz Wiszkowski16fb1742017-09-15 14:31:53 -0700251 errno = 0;
Greg Hartmand0f20d42017-12-19 23:47:26 -0800252 int rval = TEMP_FAILURE_RETRY(epoll_ctl(fd_, op, new_fd->fd_, event));
Tomasz Wiszkowski16fb1742017-09-15 14:31:53 -0700253 errno_ = errno;
254 return rval;
255 }
256
257 int EpollWait(struct epoll_event* events, int maxevents, int timeout) {
258 errno = 0;
Greg Hartmand0f20d42017-12-19 23:47:26 -0800259 int rval = TEMP_FAILURE_RETRY(epoll_wait(fd_, events, maxevents, timeout));
Tomasz Wiszkowski16fb1742017-09-15 14:31:53 -0700260 errno_ = errno;
261 return rval;
262 }
263
Tomasz Wiszkowskia449e9c2017-05-25 14:30:56 -0700264 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 Wiszkowski2c359222017-07-05 14:00:13 -0700285 int GetErrno() const { return errno_; }
Tomasz Wiszkowskia449e9c2017-05-25 14:30:56 -0700286
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 Hartman40b88f52017-06-22 15:34:11 -0700298 int Ioctl(int request, void* val = nullptr) {
Tomasz Wiszkowskia449e9c2017-05-25 14:30:56 -0700299 errno = 0;
Greg Hartman40b88f52017-06-22 15:34:11 -0700300 int rval = TEMP_FAILURE_RETRY(ioctl(fd_, request, val));
Tomasz Wiszkowskia449e9c2017-05-25 14:30:56 -0700301 errno_ = errno;
302 return rval;
303 }
304
Tomasz Wiszkowski2c359222017-07-05 14:00:13 -0700305 bool IsOpen() const { return fd_ != -1; }
Tomasz Wiszkowskia449e9c2017-05-25 14:30:56 -0700306
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 Wiszkowski2c359222017-07-05 14:00:13 -0700326 void* Mmap(void* addr, size_t length, int prot, int flags, off_t offset) {
Greg Hartman40b88f52017-06-22 15:34:11 -0700327 errno = 0;
Tomasz Wiszkowski2c359222017-07-05 14:00:13 -0700328 void* rval = mmap(addr, length, prot, flags, fd_, offset);
Greg Hartman40b88f52017-06-22 15:34:11 -0700329 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 Wiszkowskia449e9c2017-05-25 14:30:56 -0700340 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 Wiszkowski2c359222017-07-05 14:00:13 -0700350 ssize_t rval =
351 TEMP_FAILURE_RETRY(recvfrom(fd_, buf, len, flags, src_addr, addr_len));
Tomasz Wiszkowskia449e9c2017-05-25 14:30:56 -0700352 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 Wiszkowski2c359222017-07-05 14:00:13 -0700363 template <size_t SZ>
364 ssize_t RecvMsgAndFDs(const struct InbandMessageHeader& msg_in, int flags,
365 SharedFD (*new_fds)[SZ]) {
Greg Hartman3c95aac2017-06-14 18:24:26 -0700366 // 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 Wiszkowski2c359222017-07-05 14:00:13 -0700374 msg.msg_control = u.buffer;
Greg Hartman3c95aac2017-06-14 18:24:26 -0700375 msg.msg_controllen = sizeof(u.buffer);
376
377 cmsghdr* cmsg = CMSG_FIRSTHDR(&msg);
Tomasz Wiszkowski2c359222017-07-05 14:00:13 -0700378 cmsg->cmsg_len = CMSG_LEN(SZ * sizeof(int));
Greg Hartman3c95aac2017-06-14 18:24:26 -0700379 cmsg->cmsg_level = SOL_SOCKET;
Tomasz Wiszkowski2c359222017-07-05 14:00:13 -0700380 cmsg->cmsg_type = SCM_RIGHTS;
Greg Hartman3c95aac2017-06-14 18:24:26 -0700381 int* fd_array = reinterpret_cast<int*>(CMSG_DATA(cmsg));
Greg Hartmanee76f042017-12-04 18:33:51 -0800382 for (size_t i = 0; i < SZ; ++i) {
Greg Hartman3c95aac2017-06-14 18:24:26 -0700383 fd_array[i] = -1;
384 }
385 ssize_t rval = RecvMsg(&msg, flags);
Greg Hartmanee76f042017-12-04 18:33:51 -0800386 for (size_t i = 0; i < SZ; ++i) {
Greg Hartman3c95aac2017-06-14 18:24:26 -0700387 (*new_fds)[i] =
388 std::shared_ptr<FileInstance>(new FileInstance(fd_array[i], errno));
389 }
390 return rval;
391 }
392
Tomasz Wiszkowskia449e9c2017-05-25 14:30:56 -0700393 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 Wiszkowski2c359222017-07-05 14:00:13 -0700414 template <size_t SZ>
415 ssize_t SendMsgAndFDs(const struct InbandMessageHeader& msg_in, int flags,
416 const SharedFD (&fds)[SZ]) {
Greg Hartman3c95aac2017-06-14 18:24:26 -0700417 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 Wiszkowski2c359222017-07-05 14:00:13 -0700423 msg.msg_control = u.buffer;
Greg Hartman3c95aac2017-06-14 18:24:26 -0700424 msg.msg_controllen = sizeof(u.buffer);
425
426 cmsghdr* cmsg = CMSG_FIRSTHDR(&msg);
Tomasz Wiszkowski2c359222017-07-05 14:00:13 -0700427 cmsg->cmsg_len = CMSG_LEN(SZ * sizeof(int));
Greg Hartman3c95aac2017-06-14 18:24:26 -0700428 cmsg->cmsg_level = SOL_SOCKET;
Tomasz Wiszkowski2c359222017-07-05 14:00:13 -0700429 cmsg->cmsg_type = SCM_RIGHTS;
Greg Hartman3c95aac2017-06-14 18:24:26 -0700430 int* fd_array = reinterpret_cast<int*>(CMSG_DATA(cmsg));
Greg Hartman7d5e0bf2017-12-19 23:48:34 -0800431 for (size_t i = 0; i < SZ; ++i) {
Greg Hartman3c95aac2017-06-14 18:24:26 -0700432 fd_array[i] = fds[i]->fd_;
433 }
434 return SendMsg(&msg, flags);
435 }
436
Ryan Haining732b4602018-02-16 16:44:47 -0800437 int Shutdown(int how) {
438 errno = 0;
439 int rval = shutdown(fd_, how);
440 errno_ = errno;
441 return rval;
442 }
443
Tomasz Wiszkowski2c359222017-07-05 14:00:13 -0700444 ssize_t SendTo(const void* buf, size_t len, int flags,
445 const struct sockaddr* dest_addr, socklen_t addrlen) {
Tomasz Wiszkowskia449e9c2017-05-25 14:30:56 -0700446 errno = 0;
Tomasz Wiszkowski2c359222017-07-05 14:00:13 -0700447 ssize_t rval =
448 TEMP_FAILURE_RETRY(sendto(fd_, buf, len, flags, dest_addr, addrlen));
Tomasz Wiszkowskia449e9c2017-05-25 14:30:56 -0700449 errno_ = errno;
450 return rval;
451 }
452
453 void Set(fd_set* dest, int* max_index) const;
454
Tomasz Wiszkowski2c359222017-07-05 14:00:13 -0700455 int SetSockOpt(int level, int optname, const void* optval, socklen_t optlen) {
Tomasz Wiszkowskia449e9c2017-05-25 14:30:56 -0700456 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 Hartman40b88f52017-06-22 15:34:11 -0700473 strncpy(s->strerror_buf_, out, sizeof(strerror_buf_));
Tomasz Wiszkowskia449e9c2017-05-25 14:30:56 -0700474 }
475 return strerror_buf_;
476 }
477
Tomasz Wiszkowskiaf38c7c2017-08-14 12:33:11 -0700478 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 Hartmand0f20d42017-12-19 23:47:26 -0800486 struct itimerspec* old_value) {
Tomasz Wiszkowskiaf38c7c2017-08-14 12:33:11 -0700487 errno = 0;
488 int rval = timerfd_settime(fd_, flags, new_value, old_value);
489 errno_ = errno;
490 return rval;
491 }
492
Tomasz Wiszkowski2c359222017-07-05 14:00:13 -0700493 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 Wiszkowskia449e9c2017-05-25 14:30:56 -0700500 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. Moreira6ffa0cb2018-10-24 17:19:39 -0700516 // Ensure every file descriptor managed by a FileInstance has the CLOEXEC
517 // flag
518 TEMP_FAILURE_RETRY(fcntl(fd, F_SETFD, FD_CLOEXEC));
Cody Schuffelen4ede79d2019-11-13 13:24:12 -0800519 std::stringstream identity;
520 identity << "fd=" << fd << " @" << this;
521 identity_ = identity.str();
Tomasz Wiszkowskia449e9c2017-05-25 14:30:56 -0700522 }
523
Tomasz Wiszkowski2c359222017-07-05 14:00:13 -0700524 FileInstance* Accept(struct sockaddr* addr, socklen_t* addrlen) const {
Tomasz Wiszkowskia449e9c2017-05-25 14:30:56 -0700525 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 Schuffelen4ede79d2019-11-13 13:24:12 -0800535 std::string identity_;
Tomasz Wiszkowskia449e9c2017-05-25 14:30:56 -0700536 char strerror_buf_[160];
537};
538
539/* Methods that need both a fully defined SharedFD and a fully defined
540 FileInstance. */
541
Ryan Haining412a6922018-12-19 14:43:09 -0800542inline SharedFD::SharedFD() : value_(FileInstance::ClosedInstance()) {}
Tomasz Wiszkowskia449e9c2017-05-25 14:30:56 -0700543
Greg Hartman153b1062017-11-11 12:09:21 -0800544} // namespace cvd
Tomasz Wiszkowskia449e9c2017-05-25 14:30:56 -0700545
546#endif // CUTTLEFISH_COMMON_COMMON_LIBS_FS_SHARED_FD_H_