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