32#if defined(__BIONIC__)
33#include <android/fdsan.h>
35#if !defined(_WIN32) && !defined(__TRUSTY__)
36#include <sys/socket.h>
60template <
typename Closer>
78 [[clang::reinitializes]]
void reset(
int new_value = -1) {
reset(new_value,
nullptr); }
82#if !defined(ANDROID_BASE_UNIQUE_FD_DISABLE_IMPLICIT_CONVERSION)
85 operator int()
const {
return get(); }
98 bool ok()
const {
return get() >= 0; }
108 void reset(
int new_value,
void* previous_tag) {
109 int previous_errno = errno;
116 if (new_value != -1) {
117 tag(new_value, previous_tag,
this);
120 errno = previous_errno;
128 template <
typename T = Closer>
129 static auto tag(
int fd,
void* old_tag,
void* new_tag)
130 ->
decltype(T::Tag(fd, old_tag, new_tag), void()) {
131 T::Tag(fd, old_tag, new_tag);
134 template <
typename T = Closer>
135 static void tag(
long,
void*,
void*) {
140 template <
typename T = Closer>
141 static auto close(
int fd,
void* tag_value) ->
decltype(T::Close(fd, tag_value), void()) {
142 T::Close(fd, tag_value);
145 template <
typename T = Closer>
146 static auto close(
int fd,
void*) ->
decltype(T::Close(fd), void()) {
154#if defined(__BIONIC__)
155 static void Tag(
int fd,
void* old_addr,
void* new_addr) {
156 if (android_fdsan_exchange_owner_tag) {
157 uint64_t old_tag = android_fdsan_create_owner_tag(ANDROID_FDSAN_OWNER_TYPE_UNIQUE_FD,
158 reinterpret_cast<uint64_t
>(old_addr));
159 uint64_t new_tag = android_fdsan_create_owner_tag(ANDROID_FDSAN_OWNER_TYPE_UNIQUE_FD,
160 reinterpret_cast<uint64_t
>(new_addr));
161 android_fdsan_exchange_owner_tag(fd, old_tag, new_tag);
164 static void Close(
int fd,
void* addr) {
165 if (android_fdsan_close_with_tag) {
166 uint64_t tag = android_fdsan_create_owner_tag(ANDROID_FDSAN_OWNER_TYPE_UNIQUE_FD,
167 reinterpret_cast<uint64_t
>(addr));
168 android_fdsan_close_with_tag(fd, tag);
186#if !defined(_WIN32) && !defined(__TRUSTY__)
193template <
typename Closer>
195 int flags = O_CLOEXEC) {
198#if defined(__linux__)
199 if (pipe2(pipefd, flags) != 0) {
203 if (flags & ~(O_CLOEXEC | O_NONBLOCK)) {
206 if (pipe(pipefd) != 0) {
210 if (flags & O_CLOEXEC) {
211 if (fcntl(pipefd[0], F_SETFD, FD_CLOEXEC) != 0 || fcntl(pipefd[1], F_SETFD, FD_CLOEXEC) != 0) {
217 if (flags & O_NONBLOCK) {
218 if (fcntl(pipefd[0], F_SETFL, O_NONBLOCK) != 0 || fcntl(pipefd[1], F_SETFL, O_NONBLOCK) != 0) {
226 read->reset(pipefd[0]);
227 write->reset(pipefd[1]);
233template <
typename Closer>
240 left->
reset(sockfd[0]);
241 right->
reset(sockfd[1]);
247template <
typename Closer>
256 int fd = ufd.release();
257 FILE* file =
fdopen(fd, mode);
268 int fd = ufd.release();
270 if (dir ==
nullptr) {
294 template <
typename T>
312 __attribute__((__unavailable__(
"close called on unique_fd")));
316 __attribute__((__unavailable__(
"fdopen takes ownership of the fd passed in; either dup the "
317 "unique_fd, or use android::base::Fdopen to pass ownership")));
321 __unavailable__(
"fdopendir takes ownership of the fd passed in; either dup the "
322 "unique_fd, or use android::base::Fdopendir to pass ownership")));
Definition: unique_fd.h:61
bool operator==(int rhs) const
Definition: unique_fd.h:90
bool ok() const
Definition: unique_fd.h:98
~unique_fd_impl()
Definition: unique_fd.h:66
unique_fd_impl(const unique_fd_impl &)=delete
void reset(int new_value=-1)
Definition: unique_fd.h:78
static auto tag(int fd, void *old_tag, void *new_tag) -> decltype(T::Tag(fd, old_tag, new_tag), void())
Definition: unique_fd.h:129
bool operator>=(int rhs) const
Definition: unique_fd.h:88
unique_fd_impl(int fd)
Definition: unique_fd.h:65
static auto close(int fd, void *tag_value) -> decltype(T::Close(fd, tag_value), void())
Definition: unique_fd.h:141
void reset(int new_value, void *previous_tag)
Definition: unique_fd.h:108
bool operator==(const unique_fd_impl &rhs) const
Definition: unique_fd.h:92
bool operator!=(int rhs) const
Definition: unique_fd.h:91
bool operator!() const =delete
bool operator<(int rhs) const
Definition: unique_fd.h:89
static auto close(int fd, void *) -> decltype(T::Close(fd), void())
Definition: unique_fd.h:146
void operator=(const unique_fd_impl &)=delete
int fd_
Definition: unique_fd.h:123
unique_fd_impl(unique_fd_impl &&other) noexcept
Definition: unique_fd.h:70
static void tag(long, void *, void *)
Definition: unique_fd.h:135
unique_fd_impl()
Definition: unique_fd.h:63
bool operator!=(const unique_fd_impl &rhs) const
Definition: unique_fd.h:93
int release() __attribute__((warn_unused_result))
Definition: unique_fd.h:100
unique_fd_impl & operator=(unique_fd_impl &&s) noexcept
Definition: unique_fd.h:71
int get() const
Definition: unique_fd.h:80
DIR * Fdopendir(unique_fd &&ufd)
Definition: unique_fd.h:267
FILE * Fdopen(unique_fd &&ufd, const char *mode)
Definition: unique_fd.h:255
bool Pipe(unique_fd_impl< Closer > *read, unique_fd_impl< Closer > *write, int flags=O_CLOEXEC)
Definition: unique_fd.h:194
bool Socketpair(int domain, int type, int protocol, unique_fd_impl< Closer > *left, unique_fd_impl< Closer > *right)
Definition: unique_fd.h:234
class incremental::File __attribute__
uint8_t type
Definition: pairing_connection.h:0
Definition: unique_fd.h:153
static void Close(int fd)
Definition: unique_fd.h:174
Definition: unique_fd.h:292
bool operator>=(int rhs) const
Definition: unique_fd.h:299
borrowed_fd(const unique_fd_impl< T > &ufd)
Definition: unique_fd.h:295
int fd_
Definition: unique_fd.h:305
int get() const
Definition: unique_fd.h:297
borrowed_fd(int fd)
Definition: unique_fd.h:293
bool operator<(int rhs) const
Definition: unique_fd.h:300
bool operator!=(int rhs) const
Definition: unique_fd.h:302
bool operator==(int rhs) const
Definition: unique_fd.h:301
#define read
Definition: sysdeps.h:543
#define socketpair
Definition: sysdeps.h:697
#define write
Definition: sysdeps.h:560
DIR * fdopendir(const android::base::unique_fd_impl< T > &) __attribute__((__unavailable__("fdopendir takes ownership of the fd passed in
FILE * fdopen(const android::base::unique_fd_impl< T > &, const char *mode) __attribute__((__unavailable__("fdopen takes ownership of the fd passed in
int close(const android::base::unique_fd_impl< T > &) __attribute__((__unavailable__("close called on unique_fd")))