scoped_file.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Copyright 2014 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_FILES_SCOPED_FILE_H_
  5. #define BASE_FILES_SCOPED_FILE_H_
  6. #include <stdio.h>
  7. #include <memory>
  8. #include "base/base_export.h"
  9. #include "base/scoped_generic.h"
  10. #include "build/build_config.h"
  11. namespace base {
  12. namespace internal {
  13. #if BUILDFLAG(IS_ANDROID)
  14. // Use fdsan on android.
  15. struct BASE_EXPORT ScopedFDCloseTraits : public ScopedGenericOwnershipTracking {
  16. static int InvalidValue() { return -1; }
  17. static void Free(int);
  18. static void Acquire(const ScopedGeneric<int, ScopedFDCloseTraits>&, int);
  19. static void Release(const ScopedGeneric<int, ScopedFDCloseTraits>&, int);
  20. };
  21. #elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
  22. #if BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_LINUX)
  23. // On ChromeOS and Linux we guard FD lifetime with a global table and hook into
  24. // libc close() to perform checks.
  25. struct BASE_EXPORT ScopedFDCloseTraits : public ScopedGenericOwnershipTracking {
  26. #else
  27. struct BASE_EXPORT ScopedFDCloseTraits {
  28. #endif
  29. static int InvalidValue() {
  30. return -1;
  31. }
  32. static void Free(int fd);
  33. #if BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_LINUX)
  34. static void Acquire(const ScopedGeneric<int, ScopedFDCloseTraits>&, int);
  35. static void Release(const ScopedGeneric<int, ScopedFDCloseTraits>&, int);
  36. #endif
  37. };
  38. #endif
  39. // Functor for |ScopedFILE| (below).
  40. struct ScopedFILECloser {
  41. inline void operator()(FILE* x) const {
  42. if (x)
  43. fclose(x);
  44. }
  45. };
  46. } // namespace internal
  47. #if BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_LINUX)
  48. namespace subtle {
  49. // Enables or disables enforcement of FD ownership as tracked by ScopedFD
  50. // objects. Enforcement is disabled by default since it proves unwieldy in some
  51. // test environments, but tracking is always done. It's best to enable this as
  52. // early as possible in a process's lifetime.
  53. void BASE_EXPORT EnableFDOwnershipEnforcement(bool enabled);
  54. // Resets ownership state of all FDs. The only permissible use of this API is
  55. // in a forked child process between the fork() and a subsequent exec() call.
  56. //
  57. // For one issue, it is common to mass-close most open FDs before calling
  58. // exec(), to avoid leaking FDs into the new executable's environment. For
  59. // processes which have enabled FD ownership enforcement, this reset operation
  60. // is necessary before performing such closures.
  61. //
  62. // Furthermore, fork()+exec() may be used in a multithreaded context, and
  63. // because fork() is not atomic, the FD ownership state in the child process may
  64. // be inconsistent with the actual set of opened file descriptors once fork()
  65. // returns in the child process.
  66. //
  67. // It is therefore especially important to call this ASAP after fork() in the
  68. // child process if any FD manipulation will be done prior to the subsequent
  69. // exec call.
  70. void BASE_EXPORT ResetFDOwnership();
  71. } // namespace subtle
  72. #endif
  73. // -----------------------------------------------------------------------------
  74. #if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
  75. // A low-level Posix file descriptor closer class. Use this when writing
  76. // platform-specific code, especially that does non-file-like things with the
  77. // FD (like sockets).
  78. //
  79. // If you're writing low-level Windows code, see base/win/scoped_handle.h
  80. // which provides some additional functionality.
  81. //
  82. // If you're writing cross-platform code that deals with actual files, you
  83. // should generally use base::File instead which can be constructed with a
  84. // handle, and in addition to handling ownership, has convenient cross-platform
  85. // file manipulation functions on it.
  86. typedef ScopedGeneric<int, internal::ScopedFDCloseTraits> ScopedFD;
  87. #endif
  88. // Automatically closes |FILE*|s.
  89. typedef std::unique_ptr<FILE, internal::ScopedFILECloser> ScopedFILE;
  90. #if BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_LINUX)
  91. // Queries the ownership status of an FD, i.e. whether it is currently owned by
  92. // a ScopedFD in the calling process.
  93. bool BASE_EXPORT IsFDOwned(int fd);
  94. #endif // BUILDFLAG(IS_CHROMEOS) || BUILDFLAG(IS_LINUX)
  95. } // namespace base
  96. #endif // BASE_FILES_SCOPED_FILE_H_