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