xref: /aosp_15_r20/system/libbase/include/android-base/unique_fd.h (revision 8f0ba417480079999ba552f1087ae592091b9d02)
1*8f0ba417SAndroid Build Coastguard Worker /*
2*8f0ba417SAndroid Build Coastguard Worker  * Copyright (C) 2015 The Android Open Source Project
3*8f0ba417SAndroid Build Coastguard Worker  *
4*8f0ba417SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*8f0ba417SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*8f0ba417SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*8f0ba417SAndroid Build Coastguard Worker  *
8*8f0ba417SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*8f0ba417SAndroid Build Coastguard Worker  *
10*8f0ba417SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*8f0ba417SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*8f0ba417SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*8f0ba417SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*8f0ba417SAndroid Build Coastguard Worker  * limitations under the License.
15*8f0ba417SAndroid Build Coastguard Worker  */
16*8f0ba417SAndroid Build Coastguard Worker 
17*8f0ba417SAndroid Build Coastguard Worker #pragma once
18*8f0ba417SAndroid Build Coastguard Worker 
19*8f0ba417SAndroid Build Coastguard Worker #include <dirent.h>
20*8f0ba417SAndroid Build Coastguard Worker #include <errno.h>
21*8f0ba417SAndroid Build Coastguard Worker #include <fcntl.h>
22*8f0ba417SAndroid Build Coastguard Worker 
23*8f0ba417SAndroid Build Coastguard Worker #include <stdio.h>
24*8f0ba417SAndroid Build Coastguard Worker #include <sys/types.h>
25*8f0ba417SAndroid Build Coastguard Worker #include <unistd.h>
26*8f0ba417SAndroid Build Coastguard Worker 
27*8f0ba417SAndroid Build Coastguard Worker // DO NOT INCLUDE OTHER LIBBASE HEADERS HERE!
28*8f0ba417SAndroid Build Coastguard Worker // This file gets used in libbinder, and libbinder is used everywhere.
29*8f0ba417SAndroid Build Coastguard Worker // Including other headers from libbase frequently results in inclusion of
30*8f0ba417SAndroid Build Coastguard Worker // android-base/macros.h, which causes macro collisions.
31*8f0ba417SAndroid Build Coastguard Worker 
32*8f0ba417SAndroid Build Coastguard Worker #if defined(__BIONIC__)
33*8f0ba417SAndroid Build Coastguard Worker #include <android/fdsan.h>
34*8f0ba417SAndroid Build Coastguard Worker #endif
35*8f0ba417SAndroid Build Coastguard Worker #if !defined(_WIN32) && !defined(__TRUSTY__)
36*8f0ba417SAndroid Build Coastguard Worker #include <sys/socket.h>
37*8f0ba417SAndroid Build Coastguard Worker #endif
38*8f0ba417SAndroid Build Coastguard Worker 
39*8f0ba417SAndroid Build Coastguard Worker namespace android {
40*8f0ba417SAndroid Build Coastguard Worker namespace base {
41*8f0ba417SAndroid Build Coastguard Worker 
42*8f0ba417SAndroid Build Coastguard Worker // Container for a file descriptor that automatically closes the descriptor as
43*8f0ba417SAndroid Build Coastguard Worker // it goes out of scope.
44*8f0ba417SAndroid Build Coastguard Worker //
45*8f0ba417SAndroid Build Coastguard Worker //      unique_fd ufd(open("/some/path", "r"));
46*8f0ba417SAndroid Build Coastguard Worker //      if (ufd.get() == -1) return error;
47*8f0ba417SAndroid Build Coastguard Worker //
48*8f0ba417SAndroid Build Coastguard Worker //      // Do something useful, possibly including 'return'.
49*8f0ba417SAndroid Build Coastguard Worker //
50*8f0ba417SAndroid Build Coastguard Worker //      return 0; // Descriptor is closed for you.
51*8f0ba417SAndroid Build Coastguard Worker //
52*8f0ba417SAndroid Build Coastguard Worker // See also the Pipe()/Socketpair()/Fdopen()/Fdopendir() functions in this file
53*8f0ba417SAndroid Build Coastguard Worker // that provide interoperability with the libc functions with the same (but
54*8f0ba417SAndroid Build Coastguard Worker // lowercase) names.
55*8f0ba417SAndroid Build Coastguard Worker //
56*8f0ba417SAndroid Build Coastguard Worker // unique_fd is also known as ScopedFd/ScopedFD/scoped_fd; mentioned here to help
57*8f0ba417SAndroid Build Coastguard Worker // you find this class if you're searching for one of those names.
58*8f0ba417SAndroid Build Coastguard Worker //
59*8f0ba417SAndroid Build Coastguard Worker // unique_fd itself is a specialization of unique_fd_impl with a default closer.
60*8f0ba417SAndroid Build Coastguard Worker template <typename Closer>
61*8f0ba417SAndroid Build Coastguard Worker class unique_fd_impl final {
62*8f0ba417SAndroid Build Coastguard Worker  public:
unique_fd_impl()63*8f0ba417SAndroid Build Coastguard Worker   unique_fd_impl() {}
64*8f0ba417SAndroid Build Coastguard Worker 
unique_fd_impl(int fd)65*8f0ba417SAndroid Build Coastguard Worker   explicit unique_fd_impl(int fd) { reset(fd); }
~unique_fd_impl()66*8f0ba417SAndroid Build Coastguard Worker   ~unique_fd_impl() { reset(); }
67*8f0ba417SAndroid Build Coastguard Worker 
68*8f0ba417SAndroid Build Coastguard Worker   unique_fd_impl(const unique_fd_impl&) = delete;
69*8f0ba417SAndroid Build Coastguard Worker   void operator=(const unique_fd_impl&) = delete;
unique_fd_impl(unique_fd_impl && other)70*8f0ba417SAndroid Build Coastguard Worker   unique_fd_impl(unique_fd_impl&& other) noexcept { reset(other.release()); }
71*8f0ba417SAndroid Build Coastguard Worker   unique_fd_impl& operator=(unique_fd_impl&& s) noexcept {
72*8f0ba417SAndroid Build Coastguard Worker     int fd = s.fd_;
73*8f0ba417SAndroid Build Coastguard Worker     s.fd_ = -1;
74*8f0ba417SAndroid Build Coastguard Worker     reset(fd, &s);
75*8f0ba417SAndroid Build Coastguard Worker     return *this;
76*8f0ba417SAndroid Build Coastguard Worker   }
77*8f0ba417SAndroid Build Coastguard Worker 
78*8f0ba417SAndroid Build Coastguard Worker   [[clang::reinitializes]] void reset(int new_value = -1) { reset(new_value, nullptr); }
79*8f0ba417SAndroid Build Coastguard Worker 
get()80*8f0ba417SAndroid Build Coastguard Worker   int get() const { return fd_; }
81*8f0ba417SAndroid Build Coastguard Worker 
82*8f0ba417SAndroid Build Coastguard Worker #if !defined(ANDROID_BASE_UNIQUE_FD_DISABLE_IMPLICIT_CONVERSION)
83*8f0ba417SAndroid Build Coastguard Worker   // unique_fd's operator int is dangerous, but we have way too much code that
84*8f0ba417SAndroid Build Coastguard Worker   // depends on it, so make this opt-in at first.
85*8f0ba417SAndroid Build Coastguard Worker   operator int() const { return get(); }  // NOLINT
86*8f0ba417SAndroid Build Coastguard Worker #endif
87*8f0ba417SAndroid Build Coastguard Worker 
88*8f0ba417SAndroid Build Coastguard Worker   bool operator>=(int rhs) const { return get() >= rhs; }
89*8f0ba417SAndroid Build Coastguard Worker   bool operator<(int rhs) const { return get() < rhs; }
90*8f0ba417SAndroid Build Coastguard Worker   bool operator==(int rhs) const { return get() == rhs; }
91*8f0ba417SAndroid Build Coastguard Worker   bool operator!=(int rhs) const { return get() != rhs; }
92*8f0ba417SAndroid Build Coastguard Worker   bool operator==(const unique_fd_impl& rhs) const { return get() == rhs.get(); }
93*8f0ba417SAndroid Build Coastguard Worker   bool operator!=(const unique_fd_impl& rhs) const { return get() != rhs.get(); }
94*8f0ba417SAndroid Build Coastguard Worker 
95*8f0ba417SAndroid Build Coastguard Worker   // Catch bogus error checks (i.e.: "!fd" instead of "fd != -1").
96*8f0ba417SAndroid Build Coastguard Worker   bool operator!() const = delete;
97*8f0ba417SAndroid Build Coastguard Worker 
ok()98*8f0ba417SAndroid Build Coastguard Worker   bool ok() const { return get() >= 0; }
99*8f0ba417SAndroid Build Coastguard Worker 
release()100*8f0ba417SAndroid Build Coastguard Worker   int release() __attribute__((warn_unused_result)) {
101*8f0ba417SAndroid Build Coastguard Worker     tag(fd_, this, nullptr);
102*8f0ba417SAndroid Build Coastguard Worker     int ret = fd_;
103*8f0ba417SAndroid Build Coastguard Worker     fd_ = -1;
104*8f0ba417SAndroid Build Coastguard Worker     return ret;
105*8f0ba417SAndroid Build Coastguard Worker   }
106*8f0ba417SAndroid Build Coastguard Worker 
107*8f0ba417SAndroid Build Coastguard Worker  private:
reset(int new_value,void * previous_tag)108*8f0ba417SAndroid Build Coastguard Worker   void reset(int new_value, void* previous_tag) {
109*8f0ba417SAndroid Build Coastguard Worker     int previous_errno = errno;
110*8f0ba417SAndroid Build Coastguard Worker 
111*8f0ba417SAndroid Build Coastguard Worker     if (fd_ != -1) {
112*8f0ba417SAndroid Build Coastguard Worker       close(fd_, this);
113*8f0ba417SAndroid Build Coastguard Worker     }
114*8f0ba417SAndroid Build Coastguard Worker 
115*8f0ba417SAndroid Build Coastguard Worker     fd_ = new_value;
116*8f0ba417SAndroid Build Coastguard Worker     if (new_value != -1) {
117*8f0ba417SAndroid Build Coastguard Worker       tag(new_value, previous_tag, this);
118*8f0ba417SAndroid Build Coastguard Worker     }
119*8f0ba417SAndroid Build Coastguard Worker 
120*8f0ba417SAndroid Build Coastguard Worker     errno = previous_errno;
121*8f0ba417SAndroid Build Coastguard Worker   }
122*8f0ba417SAndroid Build Coastguard Worker 
123*8f0ba417SAndroid Build Coastguard Worker   int fd_ = -1;
124*8f0ba417SAndroid Build Coastguard Worker 
125*8f0ba417SAndroid Build Coastguard Worker   // Template magic to use Closer::Tag if available, and do nothing if not.
126*8f0ba417SAndroid Build Coastguard Worker   // If Closer::Tag exists, this implementation is preferred, because int is a better match.
127*8f0ba417SAndroid Build Coastguard Worker   // If not, this implementation is SFINAEd away, and the no-op below is the only one that exists.
128*8f0ba417SAndroid Build Coastguard Worker   template <typename T = Closer>
129*8f0ba417SAndroid Build Coastguard Worker   static auto tag(int fd, void* old_tag, void* new_tag)
130*8f0ba417SAndroid Build Coastguard Worker       -> decltype(T::Tag(fd, old_tag, new_tag), void()) {
131*8f0ba417SAndroid Build Coastguard Worker     T::Tag(fd, old_tag, new_tag);
132*8f0ba417SAndroid Build Coastguard Worker   }
133*8f0ba417SAndroid Build Coastguard Worker 
134*8f0ba417SAndroid Build Coastguard Worker   template <typename T = Closer>
tag(long,void *,void *)135*8f0ba417SAndroid Build Coastguard Worker   static void tag(long, void*, void*) {
136*8f0ba417SAndroid Build Coastguard Worker     // No-op.
137*8f0ba417SAndroid Build Coastguard Worker   }
138*8f0ba417SAndroid Build Coastguard Worker 
139*8f0ba417SAndroid Build Coastguard Worker   // Same as above, to select between Closer::Close(int) and Closer::Close(int, void*).
140*8f0ba417SAndroid Build Coastguard Worker   template <typename T = Closer>
141*8f0ba417SAndroid Build Coastguard Worker   static auto close(int fd, void* tag_value) -> decltype(T::Close(fd, tag_value), void()) {
142*8f0ba417SAndroid Build Coastguard Worker     T::Close(fd, tag_value);
143*8f0ba417SAndroid Build Coastguard Worker   }
144*8f0ba417SAndroid Build Coastguard Worker 
145*8f0ba417SAndroid Build Coastguard Worker   template <typename T = Closer>
146*8f0ba417SAndroid Build Coastguard Worker   static auto close(int fd, void*) -> decltype(T::Close(fd), void()) {
147*8f0ba417SAndroid Build Coastguard Worker     T::Close(fd);
148*8f0ba417SAndroid Build Coastguard Worker   }
149*8f0ba417SAndroid Build Coastguard Worker };
150*8f0ba417SAndroid Build Coastguard Worker 
151*8f0ba417SAndroid Build Coastguard Worker // The actual details of closing are factored out to support unusual cases.
152*8f0ba417SAndroid Build Coastguard Worker // Almost everyone will want this DefaultCloser, which handles fdsan on bionic.
153*8f0ba417SAndroid Build Coastguard Worker struct DefaultCloser {
154*8f0ba417SAndroid Build Coastguard Worker #if defined(__BIONIC__)
TagDefaultCloser155*8f0ba417SAndroid Build Coastguard Worker   static void Tag(int fd, void* old_addr, void* new_addr) {
156*8f0ba417SAndroid Build Coastguard Worker     if (android_fdsan_exchange_owner_tag) {
157*8f0ba417SAndroid Build Coastguard Worker       uint64_t old_tag = android_fdsan_create_owner_tag(ANDROID_FDSAN_OWNER_TYPE_UNIQUE_FD,
158*8f0ba417SAndroid Build Coastguard Worker                                                         reinterpret_cast<uint64_t>(old_addr));
159*8f0ba417SAndroid Build Coastguard Worker       uint64_t new_tag = android_fdsan_create_owner_tag(ANDROID_FDSAN_OWNER_TYPE_UNIQUE_FD,
160*8f0ba417SAndroid Build Coastguard Worker                                                         reinterpret_cast<uint64_t>(new_addr));
161*8f0ba417SAndroid Build Coastguard Worker       android_fdsan_exchange_owner_tag(fd, old_tag, new_tag);
162*8f0ba417SAndroid Build Coastguard Worker     }
163*8f0ba417SAndroid Build Coastguard Worker   }
CloseDefaultCloser164*8f0ba417SAndroid Build Coastguard Worker   static void Close(int fd, void* addr) {
165*8f0ba417SAndroid Build Coastguard Worker     if (android_fdsan_close_with_tag) {
166*8f0ba417SAndroid Build Coastguard Worker       uint64_t tag = android_fdsan_create_owner_tag(ANDROID_FDSAN_OWNER_TYPE_UNIQUE_FD,
167*8f0ba417SAndroid Build Coastguard Worker                                                     reinterpret_cast<uint64_t>(addr));
168*8f0ba417SAndroid Build Coastguard Worker       android_fdsan_close_with_tag(fd, tag);
169*8f0ba417SAndroid Build Coastguard Worker     } else {
170*8f0ba417SAndroid Build Coastguard Worker       close(fd);
171*8f0ba417SAndroid Build Coastguard Worker     }
172*8f0ba417SAndroid Build Coastguard Worker   }
173*8f0ba417SAndroid Build Coastguard Worker #else
174*8f0ba417SAndroid Build Coastguard Worker   static void Close(int fd) {
175*8f0ba417SAndroid Build Coastguard Worker     // Even if close(2) fails with EINTR, the fd will have been closed.
176*8f0ba417SAndroid Build Coastguard Worker     // Using TEMP_FAILURE_RETRY will either lead to EBADF or closing someone
177*8f0ba417SAndroid Build Coastguard Worker     // else's fd.
178*8f0ba417SAndroid Build Coastguard Worker     // http://lkml.indiana.edu/hypermail/linux/kernel/0509.1/0877.html
179*8f0ba417SAndroid Build Coastguard Worker     ::close(fd);
180*8f0ba417SAndroid Build Coastguard Worker   }
181*8f0ba417SAndroid Build Coastguard Worker #endif
182*8f0ba417SAndroid Build Coastguard Worker };
183*8f0ba417SAndroid Build Coastguard Worker 
184*8f0ba417SAndroid Build Coastguard Worker using unique_fd = unique_fd_impl<DefaultCloser>;
185*8f0ba417SAndroid Build Coastguard Worker 
186*8f0ba417SAndroid Build Coastguard Worker #if !defined(_WIN32) && !defined(__TRUSTY__)
187*8f0ba417SAndroid Build Coastguard Worker 
188*8f0ba417SAndroid Build Coastguard Worker // Inline functions, so that they can be used header-only.
189*8f0ba417SAndroid Build Coastguard Worker 
190*8f0ba417SAndroid Build Coastguard Worker // See pipe(2).
191*8f0ba417SAndroid Build Coastguard Worker // This helper hides the details of converting to unique_fd, and also hides the
192*8f0ba417SAndroid Build Coastguard Worker // fact that macOS doesn't support O_CLOEXEC or O_NONBLOCK directly.
193*8f0ba417SAndroid Build Coastguard Worker template <typename Closer>
194*8f0ba417SAndroid Build Coastguard Worker inline bool Pipe(unique_fd_impl<Closer>* read, unique_fd_impl<Closer>* write,
195*8f0ba417SAndroid Build Coastguard Worker                  int flags = O_CLOEXEC) {
196*8f0ba417SAndroid Build Coastguard Worker   int pipefd[2];
197*8f0ba417SAndroid Build Coastguard Worker 
198*8f0ba417SAndroid Build Coastguard Worker #if defined(__linux__)
199*8f0ba417SAndroid Build Coastguard Worker   if (pipe2(pipefd, flags) != 0) {
200*8f0ba417SAndroid Build Coastguard Worker     return false;
201*8f0ba417SAndroid Build Coastguard Worker   }
202*8f0ba417SAndroid Build Coastguard Worker #else  // defined(__APPLE__)
203*8f0ba417SAndroid Build Coastguard Worker   if (flags & ~(O_CLOEXEC | O_NONBLOCK)) {
204*8f0ba417SAndroid Build Coastguard Worker     return false;
205*8f0ba417SAndroid Build Coastguard Worker   }
206*8f0ba417SAndroid Build Coastguard Worker   if (pipe(pipefd) != 0) {
207*8f0ba417SAndroid Build Coastguard Worker     return false;
208*8f0ba417SAndroid Build Coastguard Worker   }
209*8f0ba417SAndroid Build Coastguard Worker 
210*8f0ba417SAndroid Build Coastguard Worker   if (flags & O_CLOEXEC) {
211*8f0ba417SAndroid Build Coastguard Worker     if (fcntl(pipefd[0], F_SETFD, FD_CLOEXEC) != 0 || fcntl(pipefd[1], F_SETFD, FD_CLOEXEC) != 0) {
212*8f0ba417SAndroid Build Coastguard Worker       close(pipefd[0]);
213*8f0ba417SAndroid Build Coastguard Worker       close(pipefd[1]);
214*8f0ba417SAndroid Build Coastguard Worker       return false;
215*8f0ba417SAndroid Build Coastguard Worker     }
216*8f0ba417SAndroid Build Coastguard Worker   }
217*8f0ba417SAndroid Build Coastguard Worker   if (flags & O_NONBLOCK) {
218*8f0ba417SAndroid Build Coastguard Worker     if (fcntl(pipefd[0], F_SETFL, O_NONBLOCK) != 0 || fcntl(pipefd[1], F_SETFL, O_NONBLOCK) != 0) {
219*8f0ba417SAndroid Build Coastguard Worker       close(pipefd[0]);
220*8f0ba417SAndroid Build Coastguard Worker       close(pipefd[1]);
221*8f0ba417SAndroid Build Coastguard Worker       return false;
222*8f0ba417SAndroid Build Coastguard Worker     }
223*8f0ba417SAndroid Build Coastguard Worker   }
224*8f0ba417SAndroid Build Coastguard Worker #endif
225*8f0ba417SAndroid Build Coastguard Worker 
226*8f0ba417SAndroid Build Coastguard Worker   read->reset(pipefd[0]);
227*8f0ba417SAndroid Build Coastguard Worker   write->reset(pipefd[1]);
228*8f0ba417SAndroid Build Coastguard Worker   return true;
229*8f0ba417SAndroid Build Coastguard Worker }
230*8f0ba417SAndroid Build Coastguard Worker 
231*8f0ba417SAndroid Build Coastguard Worker // See socketpair(2).
232*8f0ba417SAndroid Build Coastguard Worker // This helper hides the details of converting to unique_fd.
233*8f0ba417SAndroid Build Coastguard Worker template <typename Closer>
Socketpair(int domain,int type,int protocol,unique_fd_impl<Closer> * left,unique_fd_impl<Closer> * right)234*8f0ba417SAndroid Build Coastguard Worker inline bool Socketpair(int domain, int type, int protocol, unique_fd_impl<Closer>* left,
235*8f0ba417SAndroid Build Coastguard Worker                        unique_fd_impl<Closer>* right) {
236*8f0ba417SAndroid Build Coastguard Worker   int sockfd[2];
237*8f0ba417SAndroid Build Coastguard Worker   if (socketpair(domain, type, protocol, sockfd) != 0) {
238*8f0ba417SAndroid Build Coastguard Worker     return false;
239*8f0ba417SAndroid Build Coastguard Worker   }
240*8f0ba417SAndroid Build Coastguard Worker   left->reset(sockfd[0]);
241*8f0ba417SAndroid Build Coastguard Worker   right->reset(sockfd[1]);
242*8f0ba417SAndroid Build Coastguard Worker   return true;
243*8f0ba417SAndroid Build Coastguard Worker }
244*8f0ba417SAndroid Build Coastguard Worker 
245*8f0ba417SAndroid Build Coastguard Worker // See socketpair(2).
246*8f0ba417SAndroid Build Coastguard Worker // This helper hides the details of converting to unique_fd.
247*8f0ba417SAndroid Build Coastguard Worker template <typename Closer>
Socketpair(int type,unique_fd_impl<Closer> * left,unique_fd_impl<Closer> * right)248*8f0ba417SAndroid Build Coastguard Worker inline bool Socketpair(int type, unique_fd_impl<Closer>* left, unique_fd_impl<Closer>* right) {
249*8f0ba417SAndroid Build Coastguard Worker   return Socketpair(AF_UNIX, type, 0, left, right);
250*8f0ba417SAndroid Build Coastguard Worker }
251*8f0ba417SAndroid Build Coastguard Worker 
252*8f0ba417SAndroid Build Coastguard Worker // See fdopen(3).
253*8f0ba417SAndroid Build Coastguard Worker // Using fdopen with unique_fd correctly is more annoying than it should be,
254*8f0ba417SAndroid Build Coastguard Worker // because fdopen doesn't close the file descriptor received upon failure.
Fdopen(unique_fd && ufd,const char * mode)255*8f0ba417SAndroid Build Coastguard Worker inline FILE* Fdopen(unique_fd&& ufd, const char* mode) {
256*8f0ba417SAndroid Build Coastguard Worker   int fd = ufd.release();
257*8f0ba417SAndroid Build Coastguard Worker   FILE* file = fdopen(fd, mode);
258*8f0ba417SAndroid Build Coastguard Worker   if (!file) {
259*8f0ba417SAndroid Build Coastguard Worker     close(fd);
260*8f0ba417SAndroid Build Coastguard Worker   }
261*8f0ba417SAndroid Build Coastguard Worker   return file;
262*8f0ba417SAndroid Build Coastguard Worker }
263*8f0ba417SAndroid Build Coastguard Worker 
264*8f0ba417SAndroid Build Coastguard Worker // See fdopendir(3).
265*8f0ba417SAndroid Build Coastguard Worker // Using fdopendir with unique_fd correctly is more annoying than it should be,
266*8f0ba417SAndroid Build Coastguard Worker // because fdopen doesn't close the file descriptor received upon failure.
Fdopendir(unique_fd && ufd)267*8f0ba417SAndroid Build Coastguard Worker inline DIR* Fdopendir(unique_fd&& ufd) {
268*8f0ba417SAndroid Build Coastguard Worker   int fd = ufd.release();
269*8f0ba417SAndroid Build Coastguard Worker   DIR* dir = fdopendir(fd);
270*8f0ba417SAndroid Build Coastguard Worker   if (dir == nullptr) {
271*8f0ba417SAndroid Build Coastguard Worker     close(fd);
272*8f0ba417SAndroid Build Coastguard Worker   }
273*8f0ba417SAndroid Build Coastguard Worker   return dir;
274*8f0ba417SAndroid Build Coastguard Worker }
275*8f0ba417SAndroid Build Coastguard Worker 
276*8f0ba417SAndroid Build Coastguard Worker #endif  // !defined(_WIN32) && !defined(__TRUSTY__)
277*8f0ba417SAndroid Build Coastguard Worker 
278*8f0ba417SAndroid Build Coastguard Worker // A wrapper type that can be implicitly constructed from either int or
279*8f0ba417SAndroid Build Coastguard Worker // unique_fd. This supports cases where you don't actually own the file
280*8f0ba417SAndroid Build Coastguard Worker // descriptor, and can't take ownership, but are temporarily acting as if
281*8f0ba417SAndroid Build Coastguard Worker // you're the owner.
282*8f0ba417SAndroid Build Coastguard Worker //
283*8f0ba417SAndroid Build Coastguard Worker // One example would be a function that needs to also allow
284*8f0ba417SAndroid Build Coastguard Worker // STDERR_FILENO, not just a newly-opened fd. Another example would be JNI code
285*8f0ba417SAndroid Build Coastguard Worker // that's using a file descriptor that's actually owned by a
286*8f0ba417SAndroid Build Coastguard Worker // ParcelFileDescriptor or whatever on the Java side, but where the JNI code
287*8f0ba417SAndroid Build Coastguard Worker // would like to enforce this weaker sense of "temporary ownership".
288*8f0ba417SAndroid Build Coastguard Worker //
289*8f0ba417SAndroid Build Coastguard Worker // If you think of unique_fd as being like std::string in that represents
290*8f0ba417SAndroid Build Coastguard Worker // ownership, borrowed_fd is like std::string_view (and int is like const
291*8f0ba417SAndroid Build Coastguard Worker // char*).
292*8f0ba417SAndroid Build Coastguard Worker struct borrowed_fd {
borrowed_fdborrowed_fd293*8f0ba417SAndroid Build Coastguard Worker   /* implicit */ borrowed_fd(int fd) : fd_(fd) {}  // NOLINT
294*8f0ba417SAndroid Build Coastguard Worker   template <typename T>
borrowed_fdborrowed_fd295*8f0ba417SAndroid Build Coastguard Worker   /* implicit */ borrowed_fd(const unique_fd_impl<T>& ufd) : fd_(ufd.get()) {}  // NOLINT
296*8f0ba417SAndroid Build Coastguard Worker 
getborrowed_fd297*8f0ba417SAndroid Build Coastguard Worker   int get() const { return fd_; }
298*8f0ba417SAndroid Build Coastguard Worker 
299*8f0ba417SAndroid Build Coastguard Worker   bool operator>=(int rhs) const { return get() >= rhs; }
300*8f0ba417SAndroid Build Coastguard Worker   bool operator<(int rhs) const { return get() < rhs; }
301*8f0ba417SAndroid Build Coastguard Worker   bool operator==(int rhs) const { return get() == rhs; }
302*8f0ba417SAndroid Build Coastguard Worker   bool operator!=(int rhs) const { return get() != rhs; }
303*8f0ba417SAndroid Build Coastguard Worker 
304*8f0ba417SAndroid Build Coastguard Worker  private:
305*8f0ba417SAndroid Build Coastguard Worker   int fd_ = -1;
306*8f0ba417SAndroid Build Coastguard Worker };
307*8f0ba417SAndroid Build Coastguard Worker }  // namespace base
308*8f0ba417SAndroid Build Coastguard Worker }  // namespace android
309*8f0ba417SAndroid Build Coastguard Worker 
310*8f0ba417SAndroid Build Coastguard Worker template <typename T>
311*8f0ba417SAndroid Build Coastguard Worker int close(const android::base::unique_fd_impl<T>&)
312*8f0ba417SAndroid Build Coastguard Worker     __attribute__((__unavailable__("close called on unique_fd")));
313*8f0ba417SAndroid Build Coastguard Worker 
314*8f0ba417SAndroid Build Coastguard Worker template <typename T>
315*8f0ba417SAndroid Build Coastguard Worker FILE* fdopen(const android::base::unique_fd_impl<T>&, const char* mode)
316*8f0ba417SAndroid Build Coastguard Worker     __attribute__((__unavailable__("fdopen takes ownership of the fd passed in; either dup the "
317*8f0ba417SAndroid Build Coastguard Worker                                    "unique_fd, or use android::base::Fdopen to pass ownership")));
318*8f0ba417SAndroid Build Coastguard Worker 
319*8f0ba417SAndroid Build Coastguard Worker template <typename T>
320*8f0ba417SAndroid Build Coastguard Worker DIR* fdopendir(const android::base::unique_fd_impl<T>&) __attribute__((
321*8f0ba417SAndroid Build Coastguard Worker     __unavailable__("fdopendir takes ownership of the fd passed in; either dup the "
322*8f0ba417SAndroid Build Coastguard Worker                     "unique_fd, or use android::base::Fdopendir to pass ownership")));
323