process_handle.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright (c) 2013 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_PROCESS_PROCESS_HANDLE_H_
  5. #define BASE_PROCESS_PROCESS_HANDLE_H_
  6. #include <stdint.h>
  7. #include <sys/types.h>
  8. #include "base/base_export.h"
  9. #include "base/files/file_path.h"
  10. #include "build/build_config.h"
  11. #if defined(OS_WIN)
  12. #include "base/win/windows_types.h"
  13. #endif
  14. #if defined(OS_FUCHSIA)
  15. #include <zircon/types.h>
  16. #endif
  17. namespace base {
  18. // ProcessHandle is a platform specific type which represents the underlying OS
  19. // handle to a process.
  20. // ProcessId is a number which identifies the process in the OS.
  21. #if defined(OS_WIN)
  22. typedef HANDLE ProcessHandle;
  23. typedef DWORD ProcessId;
  24. typedef HANDLE UserTokenHandle;
  25. const ProcessHandle kNullProcessHandle = NULL;
  26. const ProcessId kNullProcessId = 0;
  27. #elif defined(OS_FUCHSIA)
  28. typedef zx_handle_t ProcessHandle;
  29. typedef zx_koid_t ProcessId;
  30. const ProcessHandle kNullProcessHandle = ZX_HANDLE_INVALID;
  31. const ProcessId kNullProcessId = ZX_KOID_INVALID;
  32. #elif defined(OS_POSIX)
  33. // On POSIX, our ProcessHandle will just be the PID.
  34. typedef pid_t ProcessHandle;
  35. typedef pid_t ProcessId;
  36. const ProcessHandle kNullProcessHandle = 0;
  37. const ProcessId kNullProcessId = 0;
  38. #endif // defined(OS_WIN)
  39. // To print ProcessIds portably use CrPRIdPid (based on PRIuS and friends from
  40. // C99 and format_macros.h) like this:
  41. // base::StringPrintf("PID is %" CrPRIdPid ".\n", pid);
  42. #if defined(OS_WIN) || defined(OS_FUCHSIA)
  43. #define CrPRIdPid "ld"
  44. #else
  45. #define CrPRIdPid "d"
  46. #endif
  47. class UniqueProcId {
  48. public:
  49. explicit UniqueProcId(ProcessId value) : value_(value) {}
  50. UniqueProcId(const UniqueProcId& other) = default;
  51. UniqueProcId& operator=(const UniqueProcId& other) = default;
  52. // Returns the process PID. WARNING: On some platforms, the pid may not be
  53. // valid within the current process sandbox.
  54. ProcessId GetUnsafeValue() const { return value_; }
  55. bool operator==(const UniqueProcId& other) const {
  56. return value_ == other.value_;
  57. }
  58. bool operator!=(const UniqueProcId& other) const {
  59. return value_ != other.value_;
  60. }
  61. bool operator<(const UniqueProcId& other) const {
  62. return value_ < other.value_;
  63. }
  64. bool operator<=(const UniqueProcId& other) const {
  65. return value_ <= other.value_;
  66. }
  67. bool operator>(const UniqueProcId& other) const {
  68. return value_ > other.value_;
  69. }
  70. bool operator>=(const UniqueProcId& other) const {
  71. return value_ >= other.value_;
  72. }
  73. private:
  74. ProcessId value_;
  75. };
  76. std::ostream& operator<<(std::ostream& os, const UniqueProcId& obj);
  77. // Returns the id of the current process.
  78. // Note that on some platforms, this is not guaranteed to be unique across
  79. // processes (use GetUniqueIdForProcess if uniqueness is required).
  80. BASE_EXPORT ProcessId GetCurrentProcId();
  81. // Returns a unique ID for the current process. The ID will be unique across all
  82. // currently running processes within the chrome session, but IDs of terminated
  83. // processes may be reused.
  84. BASE_EXPORT UniqueProcId GetUniqueIdForProcess();
  85. #if defined(OS_LINUX) || defined(OS_CHROMEOS)
  86. // When a process is started in a different PID namespace from the browser
  87. // process, this function must be called with the process's PID in the browser's
  88. // PID namespace in order to initialize its unique ID. Not thread safe.
  89. // WARNING: To avoid inconsistent results from GetUniqueIdForProcess, this
  90. // should only be called very early after process startup - ideally as soon
  91. // after process creation as possible.
  92. BASE_EXPORT void InitUniqueIdForProcessInPidNamespace(
  93. ProcessId pid_outside_of_namespace);
  94. #endif
  95. // Returns the ProcessHandle of the current process.
  96. BASE_EXPORT ProcessHandle GetCurrentProcessHandle();
  97. // Returns the process ID for the specified process. This is functionally the
  98. // same as Windows' GetProcessId(), but works on versions of Windows before Win
  99. // XP SP1 as well.
  100. // DEPRECATED. New code should be using Process::Pid() instead.
  101. // Note that on some platforms, this is not guaranteed to be unique across
  102. // processes.
  103. BASE_EXPORT ProcessId GetProcId(ProcessHandle process);
  104. #if !defined(OS_FUCHSIA)
  105. // Returns the ID for the parent of the given process. Not available on Fuchsia.
  106. // Returning a negative value indicates an error, such as if the |process| does
  107. // not exist. Returns 0 when |process| has no parent process.
  108. BASE_EXPORT ProcessId GetParentProcessId(ProcessHandle process);
  109. #endif // !defined(OS_FUCHSIA)
  110. #if defined(OS_POSIX)
  111. // Returns the path to the executable of the given process.
  112. BASE_EXPORT FilePath GetProcessExecutablePath(ProcessHandle process);
  113. #endif
  114. } // namespace base
  115. #endif // BASE_PROCESS_PROCESS_HANDLE_H_