file_descriptor_posix.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_FILE_DESCRIPTOR_POSIX_H_
  5. #define BASE_FILE_DESCRIPTOR_POSIX_H_
  6. #include "base/base_export.h"
  7. #include "base/files/scoped_file.h"
  8. namespace base {
  9. class File;
  10. constexpr int kInvalidFd = -1;
  11. // -----------------------------------------------------------------------------
  12. // We introduct a special structure for file descriptors in order that we are
  13. // able to use template specialisation to special-case their handling.
  14. //
  15. // IMPORTANT: This is primarily intended for use when sending file descriptors
  16. // over IPC. Even if |auto_close| is true, base::FileDescriptor does NOT close()
  17. // |fd| when going out of scope. Instead, a consumer of a base::FileDescriptor
  18. // must invoke close() on |fd| if |auto_close| is true.
  19. //
  20. // In the case of IPC, the IPC subsystem knows to close() |fd| after sending
  21. // a message that contains a base::FileDescriptor if auto_close == true. On the
  22. // other end, the receiver must make sure to close() |fd| after it has finished
  23. // processing the IPC message. See the IPC::ParamTraits<> specialization in
  24. // ipc/ipc_message_utils.h for all the details.
  25. // -----------------------------------------------------------------------------
  26. struct BASE_EXPORT FileDescriptor {
  27. FileDescriptor();
  28. FileDescriptor(int ifd, bool iauto_close);
  29. explicit FileDescriptor(File file);
  30. explicit FileDescriptor(ScopedFD fd);
  31. bool operator==(const FileDescriptor& other) const;
  32. bool operator!=(const FileDescriptor& other) const;
  33. // A comparison operator so that we can use these as keys in a std::map.
  34. bool operator<(const FileDescriptor& other) const;
  35. int fd = kInvalidFd;
  36. // If true, this file descriptor should be closed after it has been used. For
  37. // example an IPC system might interpret this flag as indicating that the
  38. // file descriptor it has been given should be closed after use.
  39. bool auto_close = false;
  40. };
  41. } // namespace base
  42. #endif // BASE_FILE_DESCRIPTOR_POSIX_H_