[go: nahoru, domu]

blob: fbc0cf9baeeedda56636e78c7426bc1682542347 [file] [log] [blame]
Avi Drissmane4622aa2022-09-08 20:36:061// Copyright 2006-2009 The Chromium Authors
agl@chromium.org5fe733de2009-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"
rvargas@chromium.org0daaebfe2014-03-15 00:09:0510
agl@chromium.org5fe733de2009-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
agl@chromium.org5fe733de2009-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.
agl@chromium.org2749885f2009-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 Zhang20b21af2020-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.
agl@chromium.org5fe733de2009-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);
agl@chromium.org5fe733de2009-02-11 18:59:2037
Peter Kasting73ee7ef2021-07-20 19:47:5638 bool operator==(const FileDescriptor& other) const;
39 bool operator!=(const FileDescriptor& other) const;
viettrungluu@chromium.orgaf58f482014-03-19 14:50:2140
gspencer@chromium.orgd65adb12010-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;
gspencer@chromium.orgd65adb12010-04-28 17:26:4943
Peter Kasting73ee7ef2021-07-20 19:47:5644 int fd = kInvalidFd;
45
agl@chromium.org5fe733de2009-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;
agl@chromium.org5fe733de2009-02-11 18:59:2050};
51
52} // namespace base
53
54#endif // BASE_FILE_DESCRIPTOR_POSIX_H_