Android-cuttlefish cvd tool
fd.h
Go to the documentation of this file.
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 */
16
17#ifndef CUTTLEFISH_COMMON_COMMON_LIBS_FS_FILE_INSTANCE_H_
18#define CUTTLEFISH_COMMON_COMMON_LIBS_FS_FILE_INSTANCE_H_
19
20#include <errno.h>
21#include <fcntl.h>
22#include <stddef.h>
23#include <stdint.h>
24#include <string.h>
25#include <sys/ioctl.h>
26#include <sys/mman.h>
27#include <sys/select.h>
28#include <sys/socket.h>
29#include <sys/stat.h>
30#include <sys/time.h>
31#include <sys/types.h>
32#include <sys/uio.h>
33#include <sys/un.h>
34#include <termios.h>
35#include <unistd.h>
36
37#ifdef __linux__
38#include <sys/epoll.h>
39#include <sys/eventfd.h>
40#include <sys/inotify.h>
41#endif
42
43#include <chrono>
44#include <string>
45#include <vector>
46
47#include "absl/strings/string_view.h"
48#include "android-base/cmsg.h"
49
55
75namespace cuttlefish {
87class Fd : public ReaderWriterSeeker {
88 // Give SharedFD access to the aliasing constructor.
89 friend class SharedFD;
90 friend class Fd;
91 friend class Epoll;
92
93 public:
94 Fd();
95 Fd(Fd&) = delete;
96 Fd(Fd&&);
97 ~Fd();
98 Fd& operator=(Fd&) = delete;
99 Fd& operator=(Fd&&);
100
101 // All Fds have the O_CLOEXEC flag after creation. To remove use the
102 // Fcntl or Dup functions.
103
104 static Result<Fd, int> Accept(const Fd& listener);
105 static Result<Fd, int> Creat(absl::string_view path, mode_t mode);
106 static Result<Fd, int> Dup(int unmanaged_fd);
107 static Result<Fd, int> Fifo(absl::string_view pathname, mode_t mode);
108 static Result<Fd, int> MemfdCreate(absl::string_view name,
109 unsigned int flags = 0);
111 absl::string_view path, int flags = O_CLOEXEC);
112 static Result<Fd, int> Open(absl::string_view pathname, int flags,
113 mode_t mode = 0);
114 static Result<std::pair<Fd, Fd>, int> Pipe();
115 static Result<std::pair<Fd, Fd>, int> SocketPair(int domain, int type,
116 int protocol);
117 static Result<Fd, int> Socket(int domain, int socket_type, int protocol);
119 absl::string_view host, absl::string_view interface, int port, int type,
120 std::chrono::seconds timeout = std::chrono::seconds(0));
122 absl::string_view host, int port, int type,
123 std::chrono::seconds timeout = std::chrono::seconds(0));
124 static Result<Fd, int> SocketLocalClient(absl::string_view name,
125 bool is_abstract, int in_type);
126 static Result<Fd, int> SocketLocalClient(absl::string_view name,
127 bool is_abstract, int in_type,
128 int timeout_seconds);
129 static Result<Fd, int> SocketLocalClient(int port, int type);
130 static Result<Fd, int> SocketLocalServer(absl::string_view name,
131 bool is_abstract, int in_type,
132 mode_t mode);
133 static Result<Fd, int> SocketLocalServer(int port, int type);
134 static Result<Fd, int> ShmOpen(absl::string_view name, int oflag, int mode);
135
136#ifdef __linux__
137 static Result<Fd, int> Event(int initval = 0, int flags = 0);
138 static Result<Fd, int> InotifyFd();
139#endif
140 // Usages outside of Linux without vhost-user-vsock use AF_VSOCK and fail
141 // with EAFNOSUPPORT.
142 // For binding in vsock, svm_cid from `cid` param would be either
143 // VMADDR_CID_ANY, VMADDR_CID_LOCAL, VMADDR_CID_HOST or their own CID, and it
144 // is used for indicating connections which it accepts from.
145 // * VMADDR_CID_ANY: accept from any
146 // * VMADDR_CID_LOCAL: accept from local
147 // * VMADDR_CID_HOST: accept from child vm
148 // * their own CID: accept from parent vm
149 // With vhost-user-vsock, it is basically similar to VMADDR_CID_HOST, but for
150 // now it has limitations that it should bind to a specific socket file which
151 // is for a certain cid. So for vhost-user-vsock, we need to specify the
152 // expected client's cid. That's why vhost_user_vsock_listening_cid is
153 // necessary.
154 // TODO: combining them when vhost-user-vsock impl supports a kind of
155 // VMADDR_CID_HOST
156 static std::string GetVhostUserVsockServerAddr(
157 unsigned int port, int vhost_user_vsock_listening_cid);
158 static std::string GetVhostUserVsockClientAddr(int cid);
160 unsigned int port, int type,
161 std::optional<int> vhost_user_vsock_listening_cid,
162 unsigned int cid = VMADDR_CID_ANY);
164 int type, std::optional<int> vhost_user_vsock_listening_cid);
165
166 Result<void> Visit(IoVisitor&) override;
167
168 Result<void, int> Bind(const struct sockaddr* addr, socklen_t addrlen);
169 Result<void, int> Connect(const struct sockaddr* addr, socklen_t addrlen);
170 int ConnectWithTimeout(const struct sockaddr* addr, socklen_t addrlen,
171 struct timeval* timeout);
172 void Close();
173
174 bool Chmod(mode_t mode);
175
176 // Returns true if the entire input was copied.
177 // Otherwise an error will be set either on this file or the input.
178 // The non-const reference is needed to avoid binding this to a particular
179 // reference type.
180 bool CopyFrom(Fd& in, size_t length, Fd* stop = nullptr);
181 // Same as CopyFrom, but reads from input until EOF is reached.
182 bool CopyAllFrom(Fd& in, Fd* stop = nullptr);
183 bool SendFile(Fd& in, off_t* offset, size_t count);
184
185 int UNMANAGED_Dup();
186 int UNMANAGED_Dup2(int newfd);
188 int Fcntl(int command, int value);
190
191 Result<void, int> Flock(int operation);
192
194
195 int GetErrno() const { return errno_; }
196 int GetSockName(struct sockaddr* addr, socklen_t* addrlen);
197
198#ifdef __linux__
199 unsigned int VsockServerPort();
200#endif
201
202 int Ioctl(int request, void* val = nullptr);
203 bool IsOpen() const { return fd_ != -1; }
204
205 // in probably isn't modified, but the API spec doesn't have const.
206 bool IsSet(fd_set* in) const;
207
208 // whether this is a regular file or not
209 bool IsRegular() const { return is_regular_file_; }
210
211 static void Log(const char* message);
212 off_t LSeek(off_t offset, int whence);
213 ssize_t Recv(void* buf, size_t len, int flags);
214 ssize_t RecvMsg(struct msghdr* msg, int flags);
215 Result<uint64_t, int> Read(void* buf, uint64_t count) override;
216 Result<uint64_t, int> PRead(void* buf, uint64_t count,
217 uint64_t offset) const override;
218#ifdef __linux__
219 int EventfdRead(eventfd_t* value);
220#endif
221 ssize_t Send(const void* buf, size_t len, int flags);
222 ssize_t SendMsg(const struct msghdr* msg, int flags);
223
224 Result<uint64_t, int> SeekSet(uint64_t) override;
225 Result<uint64_t, int> SeekCur(int64_t) override;
226 Result<uint64_t, int> SeekEnd(int64_t) override;
227
228 template <typename... Args>
229 ssize_t SendFileDescriptors(const void* buf, size_t len, Args&&... sent_fds) {
230 std::vector<int> fds;
231 (fds.push_back(sent_fds->fd_), ...);
232 errno = 0;
233 auto ret = android::base::SendFileDescriptorVector(fd_, buf, len, fds);
234 errno_ = errno;
235 return ret;
236 }
237
238 int Shutdown(int how);
239 void Set(fd_set* dest, int* max_index) const;
240 int SetSockOpt(int level, int optname, const void* optval, socklen_t optlen);
241 int GetSockOpt(int level, int optname, void* optval, socklen_t* optlen);
242 int SetTerminalRaw();
243 std::string StrError() const;
244 ScopedMMap MMap(void* addr, size_t length, int prot, int flags, off_t offset);
245 Result<void> Truncate(uint64_t length) override;
246 /*
247 * If the file is a regular file and the count is 0, Write() may detect
248 * error(s) by calling write(fd, buf, 0) declared in <unistd.h>. If detected,
249 * it will return -1. If not, 0 will be returned. For non-regular files such
250 * as socket or pipe, write(fd, buf, 0) is not specified. Write(), however,
251 * will do nothing and just return 0.
252 *
253 */
254 Result<uint64_t, int> Write(const void* buf, uint64_t count) override;
255 Result<uint64_t, int> PWrite(const void* buf, uint64_t count,
256 uint64_t offset) override;
257#ifdef __linux__
258 int EventfdWrite(eventfd_t value);
259#endif
260 bool IsATTY();
261
262 int Futimens(const struct timespec times[2]);
263
264#ifdef __linux__
265 // inotify related functions
266 int InotifyAddWatch(const std::string& pathname, uint32_t mask);
267 void InotifyRmWatch(int watch);
268#endif
269
270 private:
271 Fd(int fd, int in_errno);
272
273 Result<void, int> Listen(int backlog);
274
275 static Fd ErrorFD(int error);
276
277 int fd_;
279 std::string identity_;
281};
282
283} // namespace cuttlefish
284
285#endif // CUTTLEFISH_COMMON_COMMON_LIBS_FS_FILE_INSTANCE_H_
Definition: epoll.h:35
Definition: fd.h:87
static Result< Fd, int > Open(absl::string_view pathname, int flags, mode_t mode=0)
Definition: fd.cc:207
Result< void > Visit(IoVisitor &) override
Definition: fd.cc:468
Result< void, int > Flock(int operation)
Definition: fd.cc:753
Result< uint64_t, int > PRead(void *buf, uint64_t count, uint64_t offset) const override
Definition: fd.cc:828
bool IsRegular() const
Definition: fd.h:209
static Result< Fd, int > Creat(absl::string_view path, mode_t mode)
Definition: fd.cc:224
static Result< Fd, int > MemfdCreate(absl::string_view name, unsigned int flags=0)
Definition: fd.cc:186
static Result< std::pair< Fd, Fd >, int > SocketPair(int domain, int type, int protocol)
Definition: fd.cc:198
static std::string GetVhostUserVsockServerAddr(unsigned int port, int vhost_user_vsock_listening_cid)
Definition: fd.cc:455
int errno_
Definition: fd.h:278
int Shutdown(int how)
Definition: fd.cc:880
int Ioctl(int request, void *val=nullptr)
Definition: fd.cc:786
ssize_t Send(const void *buf, size_t len, int flags)
Definition: fd.cc:847
static Fd ErrorFD(int error)
Definition: fd.cc:259
int Fcntl(int command, int value)
Definition: fd.cc:738
int Futimens(const struct timespec times[2])
Definition: fd.cc:985
static Result< std::pair< Fd, std::string >, int > Mkostemp(absl::string_view path, int flags=O_CLOEXEC)
Definition: fd.cc:249
int UNMANAGED_Dup2(int newfd)
Definition: fd.cc:723
static void Log(const char *message)
Definition: fd.cc:688
int ConnectWithTimeout(const struct sockaddr *addr, socklen_t addrlen, struct timeval *timeout)
Definition: fd.cc:615
static Result< Fd, int > Socket(int domain, int socket_type, int protocol)
Definition: fd.cc:242
bool CopyFrom(Fd &in, size_t length, Fd *stop=nullptr)
Definition: fd.cc:473
Result< uint64_t, int > PWrite(const void *buf, uint64_t count, uint64_t offset) override
Definition: fd.cc:960
Result< void, int > Listen(int backlog)
Definition: fd.cc:792
int fd_
Definition: fd.h:277
ssize_t SendMsg(const struct msghdr *msg, int flags)
Definition: fd.cc:853
Fd(Fd &)=delete
Result< uint64_t, int > Write(const void *buf, uint64_t count) override
Definition: fd.cc:946
int SetTerminalRaw()
Definition: fd.cc:899
int SetSockOpt(int level, int optname, const void *optval, socklen_t optlen)
Definition: fd.cc:886
static Result< Fd, int > VsockServer(unsigned int port, int type, std::optional< int > vhost_user_vsock_listening_cid, unsigned int cid=VMADDR_CID_ANY)
Definition: fd.cc:418
bool SendFile(Fd &in, off_t *offset, size_t count)
Definition: fd.cc:539
void Close()
Definition: fd.cc:587
std::string identity_
Definition: fd.h:279
std::string StrError() const
Definition: fd.cc:924
static Result< Fd, int > SocketClient(absl::string_view host, int port, int type, std::chrono::seconds timeout=std::chrono::seconds(0))
Definition: fd.cc:297
friend class Fd
Definition: fd.h:90
bool IsOpen() const
Definition: fd.h:203
Result< void, int > Bind(const struct sockaddr *addr, socklen_t addrlen)
Definition: fd.cc:701
static Result< Fd, int > ShmOpen(absl::string_view name, int oflag, int mode)
Definition: fd.cc:178
Result< uint64_t, int > SeekCur(int64_t) override
Definition: fd.cc:866
int UNMANAGED_Dup()
Definition: fd.cc:717
bool IsATTY()
Definition: fd.cc:979
Fd & operator=(Fd &)=delete
Result< void, int > Fsync()
Definition: fd.cc:744
bool is_regular_file_
Definition: fd.h:280
ScopedMMap MMap(void *addr, size_t length, int prot, int flags, off_t offset)
Definition: fd.cc:929
bool Chmod(mode_t mode)
Definition: fd.cc:609
static Result< Fd, int > Dup(int unmanaged_fd)
Definition: fd.cc:148
int GetSockOpt(int level, int optname, void *optval, socklen_t *optlen)
Definition: fd.cc:893
Result< void > Truncate(uint64_t length) override
Definition: fd.cc:937
int GetSockName(struct sockaddr *addr, socklen_t *addrlen)
Definition: fd.cc:771
Result< uint64_t, int > SeekEnd(int64_t) override
Definition: fd.cc:873
Result< uint64_t, int > SeekSet(uint64_t) override
Definition: fd.cc:859
bool IsSet(fd_set *in) const
Definition: fd.cc:678
static Result< Fd, int > Accept(const Fd &listener)
Definition: fd.cc:141
Result< uint64_t, int > Read(void *buf, uint64_t count) override
Definition: fd.cc:818
static Result< Fd, int > Socket6Client(absl::string_view host, absl::string_view interface, int port, int type, std::chrono::seconds timeout=std::chrono::seconds(0))
Definition: fd.cc:315
off_t LSeek(off_t offset, int whence)
Definition: fd.cc:800
ssize_t SendFileDescriptors(const void *buf, size_t len, Args &&... sent_fds)
Definition: fd.h:229
Result< void, int > Connect(const struct sockaddr *addr, socklen_t addrlen)
Definition: fd.cc:709
void Set(fd_set *dest, int *max_index) const
Definition: fd.cc:691
ssize_t RecvMsg(struct msghdr *msg, int flags)
Definition: fd.cc:812
static std::string GetVhostUserVsockClientAddr(int cid)
Definition: fd.cc:463
Result< struct stat > Fstat()
Definition: fd.cc:762
Result< void, int > Fchdir()
Definition: fd.cc:729
static Result< Fd, int > SocketLocalClient(absl::string_view name, bool is_abstract, int in_type)
Definition: fd.cc:261
~Fd()
Definition: fd.cc:132
static Result< Fd, int > Fifo(absl::string_view pathname, mode_t mode)
Definition: fd.cc:228
static Result< Fd, int > SocketLocalServer(absl::string_view name, bool is_abstract, int in_type, mode_t mode)
Definition: fd.cc:380
static Result< std::pair< Fd, Fd >, int > Pipe()
Definition: fd.cc:155
ssize_t Recv(void *buf, size_t len, int flags)
Definition: fd.cc:806
bool CopyAllFrom(Fd &in, Fd *stop=nullptr)
Definition: fd.cc:528
int GetErrno() const
Definition: fd.h:195
Definition: io.h:45
Definition: reader_writer_seeker.h:27
Definition: scoped_mmap.h:28
Definition: shared_fd.h:125
#define error(format, args...)
Definition: fec_private.h:199
Event
Definition: kernel_log_server.h:31
Definition: alloc_driver.h:21
tl::expected< T, StackTraceError< E > > Result
Definition: result_type.h:30
std::vector< std::string_view > Args
Definition: incremental.h:28
__u32 mask
Definition: nl80211.h:0
uint8_t type
Definition: pairing_connection.h:0
#define VMADDR_CID_ANY
Definition: vm_sockets.h:28