Android-cuttlefish cvd tool
shared_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_SHARED_FD_H_
18#define CUTTLEFISH_COMMON_COMMON_LIBS_FS_SHARED_FD_H_
19
20#include <fcntl.h>
21#include <stddef.h>
22#include <stdint.h>
23#include <string.h>
24#include <sys/inotify.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// Must be below sys/socket.h to support older libc
38#ifdef __linux__
39#include <linux/vm_sockets.h>
40#include <sys/epoll.h>
41#include <sys/eventfd.h>
42#endif
43
44#include <chrono>
45#include <memory>
46#include <string>
47#include <string_view>
48#include <utility>
49#include <vector>
50
53
73namespace cuttlefish {
74
75struct PollSharedFd;
76class Epoll;
77class Fd;
78struct VhostUserVsockCid;
79struct VsockCid;
80
124class SharedFD {
125 // Give WeakFD access to the underlying shared_ptr.
126 friend class WeakFD;
127
128 public:
129 SharedFD();
130 SharedFD(const std::shared_ptr<Fd>& in) : value_(in) {}
131 SharedFD(SharedFD const&) = default;
132 SharedFD(SharedFD&& other);
133 SharedFD(Fd other);
134 SharedFD& operator=(SharedFD const&) = default;
135 SharedFD& operator=(SharedFD&& other);
136 SharedFD& operator=(Fd other);
137 static SharedFD Dup(int unmanaged_fd);
138 // All SharedFDs have the O_CLOEXEC flag after creation. To remove use the
139 // Fcntl or Dup functions.
140 static SharedFD Open(const char* pathname, int flags, mode_t mode = 0);
141 static SharedFD Open(const std::string& pathname, int flags, mode_t mode = 0);
142 static bool Pipe(SharedFD* fd0, SharedFD* fd1);
143#ifdef __linux__
144 static SharedFD Event(int initval = 0, int flags = 0);
145 static SharedFD ShmOpen(const std::string& name, int oflag, int mode);
146#endif
147 static SharedFD MemfdCreateWithData(const std::string& name,
148 const std::string& data,
149 unsigned int flags = 0);
151 std::string_view path, int flags = O_CLOEXEC);
152 static int Poll(PollSharedFd* fds, size_t num_fds, int timeout);
153 static int Poll(std::vector<PollSharedFd>& fds, int timeout);
154 static bool SocketPair(int domain, int type, int protocol, SharedFD* fd0,
155 SharedFD* fd1);
157 int protocol);
158 static SharedFD Socket(int domain, int socket_type, int protocol);
159 static SharedFD SocketLocalClient(const std::string& name, bool is_abstract,
160 int in_type);
161 static SharedFD SocketLocalClient(const std::string& name, bool is_abstract,
162 int in_type, int timeout_seconds);
163 static SharedFD SocketLocalClient(int port, int type);
164 static SharedFD SocketClient(
165 const std::string& host, int port, int type,
166 std::chrono::seconds timeout = std::chrono::seconds(0));
167 static SharedFD Socket6Client(
168 const std::string& host, const std::string& interface, int port, int type,
169 std::chrono::seconds timeout = std::chrono::seconds(0));
170 static SharedFD SocketLocalServer(const std::string& name, bool is_abstract,
171 int in_type, mode_t mode);
172 static SharedFD SocketLocalServer(int port, int type);
173
174#ifdef __linux__
175 // For binding in vsock, svm_cid from `cid` param would be either
176 // VMADDR_CID_ANY, VMADDR_CID_LOCAL, VMADDR_CID_HOST or their own CID, and it
177 // is used for indicating connections which it accepts from.
178 // * VMADDR_CID_ANY: accept from any
179 // * VMADDR_CID_LOCAL: accept from local
180 // * VMADDR_CID_HOST: accept from child vm
181 // * their own CID: accept from parent vm
182 // With vhost-user-vsock, it is basically similar to VMADDR_CID_HOST, but for
183 // now it has limitations that it should bind to a specific socket file which
184 // is for a certain cid. So for vhost-user-vsock, we need to specify the
185 // expected client's cid. That's why vhost_user_vsock_listening_cid is
186 // necessary.
187 // TODO: combining them when vhost-user-vsock impl supports a kind of
188 // VMADDR_CID_HOST
189 static SharedFD VsockServer(unsigned int port, int type,
190 std::optional<int> vhost_user_vsock_listening_cid,
191 unsigned int cid = VMADDR_CID_ANY);
192 static SharedFD VsockServer(
193 int type, std::optional<int> vhost_user_vsock_listening_cid);
194 static SharedFD VsockClient(unsigned int cid, unsigned int port, int type,
195 bool vhost_user);
196 static std::string GetVhostUserVsockServerAddr(
197 unsigned int port, int vhost_user_vsock_listening_cid);
198 static std::string GetVhostUserVsockClientAddr(int cid);
199#endif
200
201 auto operator<=>(const SharedFD&) const = default;
202
203 std::shared_ptr<Fd> operator->() const { return value_; }
204
205 const Fd& operator*() const { return *value_; }
206
207 Fd& operator*() { return *value_; }
208
209 private:
210 static SharedFD ErrorFD(int error);
211
212 std::shared_ptr<Fd> value_;
213};
214
220class WeakFD {
221 public:
222 WeakFD(SharedFD shared_fd) : value_(shared_fd.value_) {}
223
224 // Creates a new SharedFD object that shares ownership of the underlying fd.
225 // Callers need to check that the returned SharedFD is open before using it.
226 SharedFD lock() const;
227
228 private:
229 std::weak_ptr<Fd> value_;
230};
231
234 short events;
235 short revents;
236};
237
238} // namespace cuttlefish
239
240#endif // CUTTLEFISH_COMMON_COMMON_LIBS_FS_SHARED_FD_H_
Definition: fd.h:88
Definition: shared_fd.h:124
std::shared_ptr< Fd > operator->() const
Definition: shared_fd.h:203
SharedFD & operator=(SharedFD const &)=default
static SharedFD ErrorFD(int error)
Definition: shared_fd.cpp:229
static SharedFD Open(const char *pathname, int flags, mode_t mode=0)
Definition: shared_fd.cpp:214
Fd & operator*()
Definition: shared_fd.h:207
auto operator<=>(const SharedFD &) const =default
static SharedFD Socket(int domain, int socket_type, int protocol)
Definition: shared_fd.cpp:218
SharedFD()
Definition: shared_fd.cpp:105
static Result< std::pair< SharedFD, std::string > > Mkostemp(std::string_view path, int flags=O_CLOEXEC)
Definition: shared_fd.cpp:222
static SharedFD MemfdCreateWithData(const std::string &name, const std::string &data, unsigned int flags=0)
Definition: shared_fd.cpp:174
SharedFD(const std::shared_ptr< Fd > &in)
Definition: shared_fd.h:130
static SharedFD Dup(int unmanaged_fd)
Definition: shared_fd.cpp:145
static int Poll(PollSharedFd *fds, size_t num_fds, int timeout)
Definition: shared_fd.cpp:131
static bool Pipe(SharedFD *fd0, SharedFD *fd1)
Definition: shared_fd.cpp:149
static bool SocketPair(int domain, int type, int protocol, SharedFD *fd0, SharedFD *fd1)
Definition: shared_fd.cpp:190
static SharedFD SocketLocalClient(const std::string &name, bool is_abstract, int in_type)
Definition: shared_fd.cpp:231
std::shared_ptr< Fd > value_
Definition: shared_fd.h:212
static SharedFD SocketClient(const std::string &host, int port, int type, std::chrono::seconds timeout=std::chrono::seconds(0))
Definition: shared_fd.cpp:246
const Fd & operator*() const
Definition: shared_fd.h:205
static SharedFD Socket6Client(const std::string &host, const std::string &interface, int port, int type, std::chrono::seconds timeout=std::chrono::seconds(0))
Definition: shared_fd.cpp:251
static SharedFD SocketLocalServer(const std::string &name, bool is_abstract, int in_type, mode_t mode)
Definition: shared_fd.cpp:261
SharedFD(SharedFD const &)=default
Definition: shared_fd.h:220
std::weak_ptr< Fd > value_
Definition: shared_fd.h:229
WeakFD(SharedFD shared_fd)
Definition: shared_fd.h:222
SharedFD lock() const
Definition: shared_fd.cpp:328
#define data
Definition: dict.c:56
#define error(format, args...)
Definition: fec_private.h:201
char * name
Definition: libf2fs.c:1403
Event
Definition: kernel_log_server.h:31
Definition: alloc_driver.h:20
tl::expected< T, StackTraceError > Result
Definition: result_type.h:30
uint8_t type
Definition: pairing_connection.h:0
Definition: shared_fd.h:232
short revents
Definition: shared_fd.h:235
short events
Definition: shared_fd.h:234
SharedFD fd
Definition: shared_fd.h:233