[email protected] | 992a6065 | 2013-07-15 18:29:35 | [diff] [blame] | 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 | |
| 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 | |
[email protected] | 992a6065 | 2013-07-15 18:29:35 | [diff] [blame] | 11 | #include "base/base_export.h" |
[email protected] | 992a6065 | 2013-07-15 18:29:35 | [diff] [blame] | 12 | #include "base/files/file_path.h" |
| 13 | #include "build/build_config.h" |
| 14 | |
[email protected] | 992a6065 | 2013-07-15 18:29:35 | [diff] [blame] | 15 | #if defined(OS_WIN) |
| 16 | #include <windows.h> |
| 17 | #endif |
| 18 | |
scottmg | 297cc93 | 2017-05-24 03:45:58 | [diff] [blame] | 19 | #if defined(OS_FUCHSIA) |
Scott Graham | fe0e9f46 | 2017-09-18 21:25:04 | [diff] [blame^] | 20 | #include <zircon/types.h> |
scottmg | 297cc93 | 2017-05-24 03:45:58 | [diff] [blame] | 21 | #endif |
| 22 | |
[email protected] | 992a6065 | 2013-07-15 18:29:35 | [diff] [blame] | 23 | namespace base { |
| 24 | |
| 25 | // ProcessHandle is a platform specific type which represents the underlying OS |
| 26 | // handle to a process. |
| 27 | // ProcessId is a number which identifies the process in the OS. |
| 28 | #if defined(OS_WIN) |
| 29 | typedef HANDLE ProcessHandle; |
| 30 | typedef DWORD ProcessId; |
| 31 | typedef HANDLE UserTokenHandle; |
| 32 | const ProcessHandle kNullProcessHandle = NULL; |
| 33 | const ProcessId kNullProcessId = 0; |
scottmg | 297cc93 | 2017-05-24 03:45:58 | [diff] [blame] | 34 | #elif defined(OS_FUCHSIA) |
Scott Graham | fe0e9f46 | 2017-09-18 21:25:04 | [diff] [blame^] | 35 | typedef zx_handle_t ProcessHandle; |
| 36 | typedef zx_koid_t ProcessId; |
| 37 | const ProcessHandle kNullProcessHandle = ZX_HANDLE_INVALID; |
| 38 | const ProcessId kNullProcessId = ZX_KOID_INVALID; |
[email protected] | 992a6065 | 2013-07-15 18:29:35 | [diff] [blame] | 39 | #elif defined(OS_POSIX) |
| 40 | // On POSIX, our ProcessHandle will just be the PID. |
| 41 | typedef pid_t ProcessHandle; |
| 42 | typedef pid_t ProcessId; |
| 43 | const ProcessHandle kNullProcessHandle = 0; |
| 44 | const ProcessId kNullProcessId = 0; |
| 45 | #endif // defined(OS_WIN) |
| 46 | |
Bruce Dawson | 3859fd8f | 2017-08-08 15:57:17 | [diff] [blame] | 47 | // To print ProcessIds portably use CrPRIdPid (based on PRIuS and friends from |
| 48 | // C99 and format_macros.h) like this: |
| 49 | // base::StringPrintf("PID is %" CrPRIdPid ".\n", pid); |
Kevin Marshall | b73a98f | 2017-08-10 05:39:04 | [diff] [blame] | 50 | #if defined(OS_WIN) || defined(OS_FUCHSIA) |
Bruce Dawson | 3859fd8f | 2017-08-08 15:57:17 | [diff] [blame] | 51 | #define CrPRIdPid "ld" |
| 52 | #else |
| 53 | #define CrPRIdPid "d" |
| 54 | #endif |
| 55 | |
[email protected] | 992a6065 | 2013-07-15 18:29:35 | [diff] [blame] | 56 | // Returns the id of the current process. |
rickyz | 5b937a3 | 2015-10-21 23:52:20 | [diff] [blame] | 57 | // Note that on some platforms, this is not guaranteed to be unique across |
| 58 | // processes (use GetUniqueIdForProcess if uniqueness is required). |
[email protected] | 992a6065 | 2013-07-15 18:29:35 | [diff] [blame] | 59 | BASE_EXPORT ProcessId GetCurrentProcId(); |
| 60 | |
rickyz | 5b937a3 | 2015-10-21 23:52:20 | [diff] [blame] | 61 | // Returns a unique ID for the current process. The ID will be unique across all |
| 62 | // currently running processes within the chrome session, but IDs of terminated |
| 63 | // processes may be reused. This returns an opaque value that is different from |
| 64 | // a process's PID. |
| 65 | BASE_EXPORT uint32_t GetUniqueIdForProcess(); |
| 66 | |
| 67 | #if defined(OS_LINUX) |
| 68 | // When a process is started in a different PID namespace from the browser |
| 69 | // process, this function must be called with the process's PID in the browser's |
| 70 | // PID namespace in order to initialize its unique ID. Not thread safe. |
| 71 | // WARNING: To avoid inconsistent results from GetUniqueIdForProcess, this |
| 72 | // should only be called very early after process startup - ideally as soon |
| 73 | // after process creation as possible. |
| 74 | BASE_EXPORT void InitUniqueIdForProcessInPidNamespace( |
| 75 | ProcessId pid_outside_of_namespace); |
| 76 | #endif |
| 77 | |
[email protected] | 992a6065 | 2013-07-15 18:29:35 | [diff] [blame] | 78 | // Returns the ProcessHandle of the current process. |
| 79 | BASE_EXPORT ProcessHandle GetCurrentProcessHandle(); |
| 80 | |
rickyz | 5b937a3 | 2015-10-21 23:52:20 | [diff] [blame] | 81 | // Returns the process ID for the specified process. This is functionally the |
| 82 | // same as Windows' GetProcessId(), but works on versions of Windows before Win |
| 83 | // XP SP1 as well. |
rvargas | 6c690f1 | 2015-02-13 18:07:59 | [diff] [blame] | 84 | // DEPRECATED. New code should be using Process::Pid() instead. |
rickyz | 5b937a3 | 2015-10-21 23:52:20 | [diff] [blame] | 85 | // Note that on some platforms, this is not guaranteed to be unique across |
| 86 | // processes. |
[email protected] | 992a6065 | 2013-07-15 18:29:35 | [diff] [blame] | 87 | BASE_EXPORT ProcessId GetProcId(ProcessHandle process); |
| 88 | |
Scott Graham | e160c7f | 2017-05-25 23:07:14 | [diff] [blame] | 89 | #if !defined(OS_FUCHSIA) |
| 90 | // Returns the ID for the parent of the given process. Not available on Fuchsia. |
jam | 1288a4e | 2015-12-09 02:11:53 | [diff] [blame] | 91 | BASE_EXPORT ProcessId GetParentProcessId(ProcessHandle process); |
Scott Graham | e160c7f | 2017-05-25 23:07:14 | [diff] [blame] | 92 | #endif // !defined(OS_FUCHSIA) |
jam | 1288a4e | 2015-12-09 02:11:53 | [diff] [blame] | 93 | |
[email protected] | c17ecbe | 2014-05-01 10:50:05 | [diff] [blame] | 94 | #if defined(OS_POSIX) |
[email protected] | 992a6065 | 2013-07-15 18:29:35 | [diff] [blame] | 95 | // Returns the path to the executable of the given process. |
| 96 | BASE_EXPORT FilePath GetProcessExecutablePath(ProcessHandle process); |
[email protected] | 992a6065 | 2013-07-15 18:29:35 | [diff] [blame] | 97 | #endif |
| 98 | |
| 99 | } // namespace base |
| 100 | |
| 101 | #endif // BASE_PROCESS_PROCESS_HANDLE_H_ |