xref: /aosp_15_r20/external/cronet/base/process/process_handle.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2013 The Chromium Authors
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 
8 #include <stdint.h>
9 #include <sys/types.h>
10 
11 #include <compare>
12 #include <iosfwd>
13 
14 #include "base/base_export.h"
15 #include "build/build_config.h"
16 
17 #if BUILDFLAG(IS_WIN)
18 #include "base/win/windows_types.h"
19 #endif
20 
21 #if BUILDFLAG(IS_FUCHSIA)
22 #include <zircon/types.h>
23 #endif
24 
25 namespace base {
26 
27 class FilePath;
28 
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.
32 #if BUILDFLAG(IS_WIN)
33 typedef HANDLE ProcessHandle;
34 typedef DWORD ProcessId;
35 typedef HANDLE UserTokenHandle;
36 const ProcessHandle kNullProcessHandle = NULL;
37 const ProcessId kNullProcessId = 0;
38 #elif BUILDFLAG(IS_FUCHSIA)
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;
43 #elif BUILDFLAG(IS_POSIX)
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;
49 #endif  // BUILDFLAG(IS_WIN)
50 
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);
54 #if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_FUCHSIA)
55 #define CrPRIdPid "ld"
56 #else
57 #define CrPRIdPid "d"
58 #endif
59 
60 class UniqueProcId {
61  public:
UniqueProcId(ProcessId value)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.
GetUnsafeValue()68   ProcessId GetUnsafeValue() const { return value_; }
69 
70   friend bool operator==(const UniqueProcId&, const UniqueProcId&) = default;
71   friend auto operator<=>(const UniqueProcId&, const UniqueProcId&) = default;
72 
73  private:
74   ProcessId value_;
75 };
76 
77 std::ostream& operator<<(std::ostream& os, const UniqueProcId& obj);
78 
79 // Returns the id of the current process.
80 // Note that on some platforms, this is not guaranteed to be unique across
81 // processes (use GetUniqueIdForProcess if uniqueness is required).
82 BASE_EXPORT ProcessId GetCurrentProcId();
83 
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
86 // processes may be reused.
87 BASE_EXPORT UniqueProcId GetUniqueIdForProcess();
88 
89 #if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
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 
100 // Returns the ProcessHandle of the current process.
101 BASE_EXPORT ProcessHandle GetCurrentProcessHandle();
102 
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.
106 // DEPRECATED. New code should be using Process::Pid() instead.
107 // Note that on some platforms, this is not guaranteed to be unique across
108 // processes.
109 BASE_EXPORT ProcessId GetProcId(ProcessHandle process);
110 
111 #if !BUILDFLAG(IS_FUCHSIA)
112 // Returns the ID for the parent of the given process. Not available on Fuchsia.
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.
115 BASE_EXPORT ProcessId GetParentProcessId(ProcessHandle process);
116 #endif  // !BUILDFLAG(IS_FUCHSIA)
117 
118 #if BUILDFLAG(IS_POSIX)
119 // Returns the path to the executable of the given process.
120 BASE_EXPORT FilePath GetProcessExecutablePath(ProcessHandle process);
121 #endif
122 
123 }  // namespace base
124 
125 #endif  // BASE_PROCESS_PROCESS_HANDLE_H_
126