blob: 7c25a02f870a938719c623d86b5292b4fd640107 [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2006-2009 The Chromium Authors
[email protected]5fe733de2009-02-11 18:59:202// 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 Kasting73ee7ef2021-07-20 19:47:568#include "base/base_export.h"
morritaa409ccc2014-10-20 23:53:259#include "base/files/scoped_file.h"
[email protected]0daaebfe2014-03-15 00:09:0510
[email protected]5fe733de2009-02-11 18:59:2011namespace base {
12
Peter Kasting73ee7ef2021-07-20 19:47:5613class File;
14
Lei Zhang26f40fd2019-01-22 23:57:2215constexpr int kInvalidFd = -1;
16
[email protected]5fe733de2009-02-11 18:59:2017// -----------------------------------------------------------------------------
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.
[email protected]2749885f2009-03-05 21:40:1120//
dcheng33ee80f2015-08-19 08:37:3821// 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 Zhang20b21af82020-08-10 18:31:5826// In the case of IPC, the IPC subsystem knows to close() |fd| after sending
dcheng33ee80f2015-08-19 08:37:3827// 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.
[email protected]5fe733de2009-02-11 18:59:2031// -----------------------------------------------------------------------------
Peter Kasting73ee7ef2021-07-20 19:47:5632struct BASE_EXPORT FileDescriptor {
33 FileDescriptor();
34 FileDescriptor(int ifd, bool iauto_close);
35 explicit FileDescriptor(File file);
36 explicit FileDescriptor(ScopedFD fd);
[email protected]5fe733de2009-02-11 18:59:2037
Jan Keitelbb39ff72025-05-12 19:23:1038 friend bool operator==(const FileDescriptor&,
39 const FileDescriptor&) = default;
[email protected]af58f482014-03-19 14:50:2140
[email protected]d65adb12010-04-28 17:26:4941 // A comparison operator so that we can use these as keys in a std::map.
Peter Kasting73ee7ef2021-07-20 19:47:5642 bool operator<(const FileDescriptor& other) const;
[email protected]d65adb12010-04-28 17:26:4943
Peter Kasting73ee7ef2021-07-20 19:47:5644 int fd = kInvalidFd;
45
[email protected]5fe733de2009-02-11 18:59:2046 // 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 Kasting73ee7ef2021-07-20 19:47:5649 bool auto_close = false;
[email protected]5fe733de2009-02-11 18:59:2050};
51
52} // namespace base
53
54#endif // BASE_FILE_DESCRIPTOR_POSIX_H_