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/ioctl.h>
25#include <sys/mman.h>
26#include <sys/select.h>
27#include <sys/socket.h>
28#include <sys/stat.h>
29#include <sys/time.h>
30#include <sys/types.h>
31#include <sys/uio.h>
32#include <sys/un.h>
33#include <termios.h>
34#include <unistd.h>
35
36// Must be below sys/socket.h to support older libc
37#ifdef __linux__
38#include <linux/vm_sockets.h>
39#include <sys/epoll.h>
40#include <sys/eventfd.h>
41#include <sys/inotify.h>
42#endif
43
44#include <chrono>
45#include <memory>
46#include <string>
47#include <utility>
48#include <vector>
49
50#include "absl/strings/string_view.h"
51
54
74namespace cuttlefish {
75
76struct PollSharedFd;
77class Epoll;
78class Fd;
79struct VhostUserVsockCid;
80struct VsockCid;
81
125class SharedFD {
126 // Give WeakFD access to the underlying shared_ptr.
127 friend class WeakFD;
128
129 public:
130 SharedFD();
131 SharedFD(const std::shared_ptr<Fd>& in) : value_(in) {}
132 SharedFD(SharedFD const&) = default;
133 SharedFD(SharedFD&& other);
134 SharedFD(Fd other);
135 SharedFD& operator=(SharedFD const&) = default;
136 SharedFD& operator=(SharedFD&& other);
137 SharedFD& operator=(Fd other);
138 static SharedFD Dup(int unmanaged_fd);
139 // All SharedFDs have the O_CLOEXEC flag after creation. To remove use the
140 // Fcntl or Dup functions.
141 static bool Pipe(SharedFD* fd0, SharedFD* fd1);
143 absl::string_view path, int flags = O_CLOEXEC);
144 static int Poll(PollSharedFd* fds, size_t num_fds, int timeout);
145 static int Poll(std::vector<PollSharedFd>& fds, int timeout);
146 static bool SocketPair(int domain, int type, int protocol, SharedFD* fd0,
147 SharedFD* fd1);
149 int protocol);
150 static SharedFD Socket(int domain, int socket_type, int protocol);
151 static SharedFD SocketLocalClient(const std::string& name, bool is_abstract,
152 int in_type);
153 static SharedFD SocketLocalClient(const std::string& name, bool is_abstract,
154 int in_type, int timeout_seconds);
155 static SharedFD SocketLocalClient(int port, int type);
156 static SharedFD SocketClient(
157 const std::string& host, int port, int type,
158 std::chrono::seconds timeout = std::chrono::seconds(0));
159 static SharedFD Socket6Client(
160 const std::string& host, const std::string& interface, int port, int type,
161 std::chrono::seconds timeout = std::chrono::seconds(0));
162 static SharedFD SocketLocalServer(const std::string& name, bool is_abstract,
163 int in_type, mode_t mode);
164 static SharedFD SocketLocalServer(int port, int type);
165
166#ifdef __linux__
167 // For binding in vsock, svm_cid from `cid` param would be either
168 // VMADDR_CID_ANY, VMADDR_CID_LOCAL, VMADDR_CID_HOST or their own CID, and it
169 // is used for indicating connections which it accepts from.
170 // * VMADDR_CID_ANY: accept from any
171 // * VMADDR_CID_LOCAL: accept from local
172 // * VMADDR_CID_HOST: accept from child vm
173 // * their own CID: accept from parent vm
174 // With vhost-user-vsock, it is basically similar to VMADDR_CID_HOST, but for
175 // now it has limitations that it should bind to a specific socket file which
176 // is for a certain cid. So for vhost-user-vsock, we need to specify the
177 // expected client's cid. That's why vhost_user_vsock_listening_cid is
178 // necessary.
179 // TODO: combining them when vhost-user-vsock impl supports a kind of
180 // VMADDR_CID_HOST
181 static SharedFD VsockClient(unsigned int cid, unsigned int port, int type,
182 bool vhost_user);
183#endif
184
185 auto operator<=>(const SharedFD&) const = default;
186
187 std::shared_ptr<Fd> operator->() const { return value_; }
188
189 const Fd& operator*() const { return *value_; }
190
191 Fd& operator*() { return *value_; }
192
193 private:
194 static SharedFD ErrorFD(int error);
195
196 std::shared_ptr<Fd> value_;
197};
198
204class WeakFD {
205 public:
206 WeakFD(SharedFD shared_fd) : value_(shared_fd.value_) {}
207
208 // Creates a new SharedFD object that shares ownership of the underlying fd.
209 // Callers need to check that the returned SharedFD is open before using it.
210 SharedFD lock() const;
211
212 private:
213 std::weak_ptr<Fd> value_;
214};
215
218 short events;
219 short revents;
220};
221
222} // namespace cuttlefish
223
224#endif // CUTTLEFISH_COMMON_COMMON_LIBS_FS_SHARED_FD_H_
Definition: fd.h:87
Definition: shared_fd.h:125
static Result< std::pair< SharedFD, std::string > > Mkostemp(absl::string_view path, int flags=O_CLOEXEC)
Definition: shared_fd.cpp:189
std::shared_ptr< Fd > operator->() const
Definition: shared_fd.h:187
SharedFD & operator=(SharedFD const &)=default
static SharedFD ErrorFD(int error)
Definition: shared_fd.cpp:196
Fd & operator*()
Definition: shared_fd.h:191
auto operator<=>(const SharedFD &) const =default
static SharedFD Socket(int domain, int socket_type, int protocol)
Definition: shared_fd.cpp:185
SharedFD()
Definition: shared_fd.cpp:105
SharedFD(const std::shared_ptr< Fd > &in)
Definition: shared_fd.h:131
static SharedFD Dup(int unmanaged_fd)
Definition: shared_fd.cpp:146
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:150
static bool SocketPair(int domain, int type, int protocol, SharedFD *fd0, SharedFD *fd1)
Definition: shared_fd.cpp:165
static SharedFD SocketLocalClient(const std::string &name, bool is_abstract, int in_type)
Definition: shared_fd.cpp:198
std::shared_ptr< Fd > value_
Definition: shared_fd.h:196
static SharedFD SocketClient(const std::string &host, int port, int type, std::chrono::seconds timeout=std::chrono::seconds(0))
Definition: shared_fd.cpp:213
const Fd & operator*() const
Definition: shared_fd.h:189
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:218
static SharedFD SocketLocalServer(const std::string &name, bool is_abstract, int in_type, mode_t mode)
Definition: shared_fd.cpp:228
SharedFD(SharedFD const &)=default
Definition: shared_fd.h:204
std::weak_ptr< Fd > value_
Definition: shared_fd.h:213
WeakFD(SharedFD shared_fd)
Definition: shared_fd.h:206
SharedFD lock() const
Definition: shared_fd.cpp:275
#define error(format, args...)
Definition: fec_private.h:199
Definition: alloc_driver.h:21
tl::expected< T, StackTraceError< E > > Result
Definition: result_type.h:30
uint8_t type
Definition: pairing_connection.h:0
Definition: shared_fd.h:216
short revents
Definition: shared_fd.h:219
short events
Definition: shared_fd.h:218
SharedFD fd
Definition: shared_fd.h:217