Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2006-2009 The Chromium Authors |
agl@chromium.org | 5fe733de | 2009-02-11 18:59:20 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef BASE_FILE_DESCRIPTOR_POSIX_H_ |
| 6 | #define BASE_FILE_DESCRIPTOR_POSIX_H_ |
| 7 | |
Peter Kasting | 73ee7ef | 2021-07-20 19:47:56 | [diff] [blame] | 8 | #include "base/base_export.h" |
morrita | a409ccc | 2014-10-20 23:53:25 | [diff] [blame] | 9 | #include "base/files/scoped_file.h" |
rvargas@chromium.org | 0daaebfe | 2014-03-15 00:09:05 | [diff] [blame] | 10 | |
agl@chromium.org | 5fe733de | 2009-02-11 18:59:20 | [diff] [blame] | 11 | namespace base { |
| 12 | |
Peter Kasting | 73ee7ef | 2021-07-20 19:47:56 | [diff] [blame] | 13 | class File; |
| 14 | |
Lei Zhang | 26f40fd | 2019-01-22 23:57:22 | [diff] [blame] | 15 | constexpr int kInvalidFd = -1; |
| 16 | |
agl@chromium.org | 5fe733de | 2009-02-11 18:59:20 | [diff] [blame] | 17 | // ----------------------------------------------------------------------------- |
| 18 | // We introduct a special structure for file descriptors in order that we are |
| 19 | // able to use template specialisation to special-case their handling. |
agl@chromium.org | 2749885f | 2009-03-05 21:40:11 | [diff] [blame] | 20 | // |
dcheng | 33ee80f | 2015-08-19 08:37:38 | [diff] [blame] | 21 | // IMPORTANT: This is primarily intended for use when sending file descriptors |
| 22 | // over IPC. Even if |auto_close| is true, base::FileDescriptor does NOT close() |
| 23 | // |fd| when going out of scope. Instead, a consumer of a base::FileDescriptor |
| 24 | // must invoke close() on |fd| if |auto_close| is true. |
| 25 | // |
Lei Zhang | 20b21af | 2020-08-10 18:31:58 | [diff] [blame] | 26 | // In the case of IPC, the IPC subsystem knows to close() |fd| after sending |
dcheng | 33ee80f | 2015-08-19 08:37:38 | [diff] [blame] | 27 | // a message that contains a base::FileDescriptor if auto_close == true. On the |
| 28 | // other end, the receiver must make sure to close() |fd| after it has finished |
| 29 | // processing the IPC message. See the IPC::ParamTraits<> specialization in |
| 30 | // ipc/ipc_message_utils.h for all the details. |
agl@chromium.org | 5fe733de | 2009-02-11 18:59:20 | [diff] [blame] | 31 | // ----------------------------------------------------------------------------- |
Peter Kasting | 73ee7ef | 2021-07-20 19:47:56 | [diff] [blame] | 32 | struct BASE_EXPORT FileDescriptor { |
| 33 | FileDescriptor(); |
| 34 | FileDescriptor(int ifd, bool iauto_close); |
| 35 | explicit FileDescriptor(File file); |
| 36 | explicit FileDescriptor(ScopedFD fd); |
agl@chromium.org | 5fe733de | 2009-02-11 18:59:20 | [diff] [blame] | 37 | |
Peter Kasting | 73ee7ef | 2021-07-20 19:47:56 | [diff] [blame] | 38 | bool operator==(const FileDescriptor& other) const; |
| 39 | bool operator!=(const FileDescriptor& other) const; |
viettrungluu@chromium.org | af58f48 | 2014-03-19 14:50:21 | [diff] [blame] | 40 | |
gspencer@chromium.org | d65adb1 | 2010-04-28 17:26:49 | [diff] [blame] | 41 | // A comparison operator so that we can use these as keys in a std::map. |
Peter Kasting | 73ee7ef | 2021-07-20 19:47:56 | [diff] [blame] | 42 | bool operator<(const FileDescriptor& other) const; |
gspencer@chromium.org | d65adb1 | 2010-04-28 17:26:49 | [diff] [blame] | 43 | |
Peter Kasting | 73ee7ef | 2021-07-20 19:47:56 | [diff] [blame] | 44 | int fd = kInvalidFd; |
| 45 | |
agl@chromium.org | 5fe733de | 2009-02-11 18:59:20 | [diff] [blame] | 46 | // If true, this file descriptor should be closed after it has been used. For |
| 47 | // example an IPC system might interpret this flag as indicating that the |
| 48 | // file descriptor it has been given should be closed after use. |
Peter Kasting | 73ee7ef | 2021-07-20 19:47:56 | [diff] [blame] | 49 | bool auto_close = false; |
agl@chromium.org | 5fe733de | 2009-02-11 18:59:20 | [diff] [blame] | 50 | }; |
| 51 | |
| 52 | } // namespace base |
| 53 | |
| 54 | #endif // BASE_FILE_DESCRIPTOR_POSIX_H_ |