Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2013 The Chromium Authors |
[email protected] | 992a6065 | 2013-07-15 18:29:35 | [diff] [blame] | 2 | // 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_PROCESS_PROCESS_HANDLE_H_ |
| 6 | #define BASE_PROCESS_PROCESS_HANDLE_H_ |
| 7 | |
rickyz | 5b937a3 | 2015-10-21 23:52:20 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | #include <sys/types.h> |
| 10 | |
kylechar | d917b98 | 2023-11-30 22:52:44 | [diff] [blame] | 11 | #include <compare> |
Lei Zhang | 1757508 | 2021-05-10 20:19:18 | [diff] [blame] | 12 | #include <iosfwd> |
| 13 | |
[email protected] | 992a6065 | 2013-07-15 18:29:35 | [diff] [blame] | 14 | #include "base/base_export.h" |
[email protected] | 992a6065 | 2013-07-15 18:29:35 | [diff] [blame] | 15 | #include "build/build_config.h" |
| 16 | |
Xiaohan Wang | 37e8161 | 2022-01-15 18:27:00 | [diff] [blame] | 17 | #if BUILDFLAG(IS_WIN) |
Bruce Dawson | bfdc3fd | 2018-01-03 20:32:36 | [diff] [blame] | 18 | #include "base/win/windows_types.h" |
[email protected] | 992a6065 | 2013-07-15 18:29:35 | [diff] [blame] | 19 | #endif |
| 20 | |
Xiaohan Wang | 37e8161 | 2022-01-15 18:27:00 | [diff] [blame] | 21 | #if BUILDFLAG(IS_FUCHSIA) |
Scott Graham | fe0e9f46 | 2017-09-18 21:25:04 | [diff] [blame] | 22 | #include <zircon/types.h> |
scottmg | 297cc93 | 2017-05-24 03:45:58 | [diff] [blame] | 23 | #endif |
| 24 | |
[email protected] | 992a6065 | 2013-07-15 18:29:35 | [diff] [blame] | 25 | namespace base { |
| 26 | |
Lei Zhang | 1757508 | 2021-05-10 20:19:18 | [diff] [blame] | 27 | class FilePath; |
| 28 | |
[email protected] | 992a6065 | 2013-07-15 18:29:35 | [diff] [blame] | 29 | // ProcessHandle is a platform specific type which represents the underlying OS |
| 30 | // handle to a process. |
| 31 | // ProcessId is a number which identifies the process in the OS. |
Xiaohan Wang | 37e8161 | 2022-01-15 18:27:00 | [diff] [blame] | 32 | #if BUILDFLAG(IS_WIN) |
[email protected] | 992a6065 | 2013-07-15 18:29:35 | [diff] [blame] | 33 | typedef HANDLE ProcessHandle; |
| 34 | typedef DWORD ProcessId; |
| 35 | typedef HANDLE UserTokenHandle; |
| 36 | const ProcessHandle kNullProcessHandle = NULL; |
| 37 | const ProcessId kNullProcessId = 0; |
Xiaohan Wang | 37e8161 | 2022-01-15 18:27:00 | [diff] [blame] | 38 | #elif BUILDFLAG(IS_FUCHSIA) |
Scott Graham | fe0e9f46 | 2017-09-18 21:25:04 | [diff] [blame] | 39 | typedef zx_handle_t ProcessHandle; |
| 40 | typedef zx_koid_t ProcessId; |
| 41 | const ProcessHandle kNullProcessHandle = ZX_HANDLE_INVALID; |
| 42 | const ProcessId kNullProcessId = ZX_KOID_INVALID; |
Xiaohan Wang | 37e8161 | 2022-01-15 18:27:00 | [diff] [blame] | 43 | #elif BUILDFLAG(IS_POSIX) |
[email protected] | 992a6065 | 2013-07-15 18:29:35 | [diff] [blame] | 44 | // On POSIX, our ProcessHandle will just be the PID. |
| 45 | typedef pid_t ProcessHandle; |
| 46 | typedef pid_t ProcessId; |
| 47 | const ProcessHandle kNullProcessHandle = 0; |
| 48 | const ProcessId kNullProcessId = 0; |
Xiaohan Wang | 37e8161 | 2022-01-15 18:27:00 | [diff] [blame] | 49 | #endif // BUILDFLAG(IS_WIN) |
[email protected] | 992a6065 | 2013-07-15 18:29:35 | [diff] [blame] | 50 | |
Bruce Dawson | 3859fd8f | 2017-08-08 15:57:17 | [diff] [blame] | 51 | // To print ProcessIds portably use CrPRIdPid (based on PRIuS and friends from |
| 52 | // C99 and format_macros.h) like this: |
| 53 | // base::StringPrintf("PID is %" CrPRIdPid ".\n", pid); |
Xiaohan Wang | 37e8161 | 2022-01-15 18:27:00 | [diff] [blame] | 54 | #if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_FUCHSIA) |
Bruce Dawson | 3859fd8f | 2017-08-08 15:57:17 | [diff] [blame] | 55 | #define CrPRIdPid "ld" |
| 56 | #else |
| 57 | #define CrPRIdPid "d" |
| 58 | #endif |
| 59 | |
Chris Findeisen | 2a373a2d | 2019-08-15 19:44:30 | [diff] [blame] | 60 | class UniqueProcId { |
| 61 | public: |
| 62 | explicit UniqueProcId(ProcessId value) : value_(value) {} |
| 63 | UniqueProcId(const UniqueProcId& other) = default; |
| 64 | UniqueProcId& operator=(const UniqueProcId& other) = default; |
| 65 | |
| 66 | // Returns the process PID. WARNING: On some platforms, the pid may not be |
| 67 | // valid within the current process sandbox. |
| 68 | ProcessId GetUnsafeValue() const { return value_; } |
| 69 | |
kylechar | d917b98 | 2023-11-30 22:52:44 | [diff] [blame] | 70 | friend bool operator==(const UniqueProcId&, const UniqueProcId&) = default; |
| 71 | friend auto operator<=>(const UniqueProcId&, const UniqueProcId&) = default; |
Chris Findeisen | 2a373a2d | 2019-08-15 19:44:30 | [diff] [blame] | 72 | |
| 73 | private: |
| 74 | ProcessId value_; |
| 75 | }; |
| 76 | |
| 77 | std::ostream& operator<<(std::ostream& os, const UniqueProcId& obj); |
| 78 | |
[email protected] | 992a6065 | 2013-07-15 18:29:35 | [diff] [blame] | 79 | // Returns the id of the current process. |
rickyz | 5b937a3 | 2015-10-21 23:52:20 | [diff] [blame] | 80 | // Note that on some platforms, this is not guaranteed to be unique across |
| 81 | // processes (use GetUniqueIdForProcess if uniqueness is required). |
[email protected] | 992a6065 | 2013-07-15 18:29:35 | [diff] [blame] | 82 | BASE_EXPORT ProcessId GetCurrentProcId(); |
| 83 | |
rickyz | 5b937a3 | 2015-10-21 23:52:20 | [diff] [blame] | 84 | // Returns a unique ID for the current process. The ID will be unique across all |
| 85 | // currently running processes within the chrome session, but IDs of terminated |
Chris Findeisen | 2a373a2d | 2019-08-15 19:44:30 | [diff] [blame] | 86 | // processes may be reused. |
| 87 | BASE_EXPORT UniqueProcId GetUniqueIdForProcess(); |
rickyz | 5b937a3 | 2015-10-21 23:52:20 | [diff] [blame] | 88 | |
Xiaohan Wang | 37e8161 | 2022-01-15 18:27:00 | [diff] [blame] | 89 | #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) |
rickyz | 5b937a3 | 2015-10-21 23:52:20 | [diff] [blame] | 90 | // When a process is started in a different PID namespace from the browser |
| 91 | // process, this function must be called with the process's PID in the browser's |
| 92 | // PID namespace in order to initialize its unique ID. Not thread safe. |
| 93 | // WARNING: To avoid inconsistent results from GetUniqueIdForProcess, this |
| 94 | // should only be called very early after process startup - ideally as soon |
| 95 | // after process creation as possible. |
| 96 | BASE_EXPORT void InitUniqueIdForProcessInPidNamespace( |
| 97 | ProcessId pid_outside_of_namespace); |
| 98 | #endif |
| 99 | |
[email protected] | 992a6065 | 2013-07-15 18:29:35 | [diff] [blame] | 100 | // Returns the ProcessHandle of the current process. |
| 101 | BASE_EXPORT ProcessHandle GetCurrentProcessHandle(); |
| 102 | |
rickyz | 5b937a3 | 2015-10-21 23:52:20 | [diff] [blame] | 103 | // Returns the process ID for the specified process. This is functionally the |
| 104 | // same as Windows' GetProcessId(), but works on versions of Windows before Win |
| 105 | // XP SP1 as well. |
rvargas | 6c690f1 | 2015-02-13 18:07:59 | [diff] [blame] | 106 | // DEPRECATED. New code should be using Process::Pid() instead. |
rickyz | 5b937a3 | 2015-10-21 23:52:20 | [diff] [blame] | 107 | // Note that on some platforms, this is not guaranteed to be unique across |
| 108 | // processes. |
[email protected] | 992a6065 | 2013-07-15 18:29:35 | [diff] [blame] | 109 | BASE_EXPORT ProcessId GetProcId(ProcessHandle process); |
| 110 | |
Xiaohan Wang | 37e8161 | 2022-01-15 18:27:00 | [diff] [blame] | 111 | #if !BUILDFLAG(IS_FUCHSIA) |
Scott Graham | e160c7f | 2017-05-25 23:07:14 | [diff] [blame] | 112 | // Returns the ID for the parent of the given process. Not available on Fuchsia. |
Zijie He | 4dd88ae42 | 2017-09-20 01:30:18 | [diff] [blame] | 113 | // Returning a negative value indicates an error, such as if the |process| does |
| 114 | // not exist. Returns 0 when |process| has no parent process. |
jam | 1288a4e | 2015-12-09 02:11:53 | [diff] [blame] | 115 | BASE_EXPORT ProcessId GetParentProcessId(ProcessHandle process); |
Xiaohan Wang | 37e8161 | 2022-01-15 18:27:00 | [diff] [blame] | 116 | #endif // !BUILDFLAG(IS_FUCHSIA) |
jam | 1288a4e | 2015-12-09 02:11:53 | [diff] [blame] | 117 | |
Xiaohan Wang | 37e8161 | 2022-01-15 18:27:00 | [diff] [blame] | 118 | #if BUILDFLAG(IS_POSIX) |
[email protected] | 992a6065 | 2013-07-15 18:29:35 | [diff] [blame] | 119 | // Returns the path to the executable of the given process. |
| 120 | BASE_EXPORT FilePath GetProcessExecutablePath(ProcessHandle process); |
[email protected] | 992a6065 | 2013-07-15 18:29:35 | [diff] [blame] | 121 | #endif |
| 122 | |
| 123 | } // namespace base |
| 124 | |
| 125 | #endif // BASE_PROCESS_PROCESS_HANDLE_H_ |