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 8 #include <stdint.h> 9 #include <sys/types.h> 10 11 #include "base/base_export.h" 12 #include "base/files/file_path.h" 13 #include "build/build_config.h" 14 15 #if defined(OS_WIN) 16 #include "base/win/windows_types.h" 17 #endif 18 19 #if defined(OS_FUCHSIA) 20 #include <zircon/types.h> 21 #endif 22 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; 34 #elif defined(OS_FUCHSIA) 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; 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 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); 50 #if defined(OS_WIN) || defined(OS_FUCHSIA) 51 #define CrPRIdPid "ld" 52 #else 53 #define CrPRIdPid "d" 54 #endif 55 56 class UniqueProcId { 57 public: UniqueProcId(ProcessId value)58 explicit UniqueProcId(ProcessId value) : value_(value) {} 59 UniqueProcId(const UniqueProcId& other) = default; 60 UniqueProcId& operator=(const UniqueProcId& other) = default; 61 62 // Returns the process PID. WARNING: On some platforms, the pid may not be 63 // valid within the current process sandbox. GetUnsafeValue()64 ProcessId GetUnsafeValue() const { return value_; } 65 66 bool operator==(const UniqueProcId& other) const { 67 return value_ == other.value_; 68 } 69 70 bool operator!=(const UniqueProcId& other) const { 71 return value_ != other.value_; 72 } 73 74 bool operator<(const UniqueProcId& other) const { 75 return value_ < other.value_; 76 } 77 78 bool operator<=(const UniqueProcId& other) const { 79 return value_ <= other.value_; 80 } 81 82 bool operator>(const UniqueProcId& other) const { 83 return value_ > other.value_; 84 } 85 86 bool operator>=(const UniqueProcId& other) const { 87 return value_ >= other.value_; 88 } 89 90 private: 91 ProcessId value_; 92 }; 93 94 std::ostream& operator<<(std::ostream& os, const UniqueProcId& obj); 95 96 // Returns the id of the current process. 97 // Note that on some platforms, this is not guaranteed to be unique across 98 // processes (use GetUniqueIdForProcess if uniqueness is required). 99 BASE_EXPORT ProcessId GetCurrentProcId(); 100 101 // Returns a unique ID for the current process. The ID will be unique across all 102 // currently running processes within the chrome session, but IDs of terminated 103 // processes may be reused. 104 BASE_EXPORT UniqueProcId GetUniqueIdForProcess(); 105 106 #if defined(OS_LINUX) || defined(OS_CHROMEOS) || defined(OS_BSD) 107 // When a process is started in a different PID namespace from the browser 108 // process, this function must be called with the process's PID in the browser's 109 // PID namespace in order to initialize its unique ID. Not thread safe. 110 // WARNING: To avoid inconsistent results from GetUniqueIdForProcess, this 111 // should only be called very early after process startup - ideally as soon 112 // after process creation as possible. 113 BASE_EXPORT void InitUniqueIdForProcessInPidNamespace( 114 ProcessId pid_outside_of_namespace); 115 #endif 116 117 // Returns the ProcessHandle of the current process. 118 BASE_EXPORT ProcessHandle GetCurrentProcessHandle(); 119 120 // Returns the process ID for the specified process. This is functionally the 121 // same as Windows' GetProcessId(), but works on versions of Windows before Win 122 // XP SP1 as well. 123 // DEPRECATED. New code should be using Process::Pid() instead. 124 // Note that on some platforms, this is not guaranteed to be unique across 125 // processes. 126 BASE_EXPORT ProcessId GetProcId(ProcessHandle process); 127 128 #if !defined(OS_FUCHSIA) 129 // Returns the ID for the parent of the given process. Not available on Fuchsia. 130 // Returning a negative value indicates an error, such as if the |process| does 131 // not exist. Returns 0 when |process| has no parent process. 132 BASE_EXPORT ProcessId GetParentProcessId(ProcessHandle process); 133 #endif // !defined(OS_FUCHSIA) 134 135 #if defined(OS_POSIX) 136 // Returns the path to the executable of the given process. 137 BASE_EXPORT FilePath GetProcessExecutablePath(ProcessHandle process); 138 #endif 139 140 } // namespace base 141 142 #endif // BASE_PROCESS_PROCESS_HANDLE_H_ 143