Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | #ifndef BASE_PROCESS_PROCESS_HANDLE_H_ |
michael@0 | 6 | #define BASE_PROCESS_PROCESS_HANDLE_H_ |
michael@0 | 7 | |
michael@0 | 8 | #include "base/base_export.h" |
michael@0 | 9 | #include "base/basictypes.h" |
michael@0 | 10 | #include "base/files/file_path.h" |
michael@0 | 11 | #include "build/build_config.h" |
michael@0 | 12 | |
michael@0 | 13 | #include <sys/types.h> |
michael@0 | 14 | #if defined(OS_WIN) |
michael@0 | 15 | #include <windows.h> |
michael@0 | 16 | #endif |
michael@0 | 17 | |
michael@0 | 18 | namespace base { |
michael@0 | 19 | |
michael@0 | 20 | // ProcessHandle is a platform specific type which represents the underlying OS |
michael@0 | 21 | // handle to a process. |
michael@0 | 22 | // ProcessId is a number which identifies the process in the OS. |
michael@0 | 23 | #if defined(OS_WIN) |
michael@0 | 24 | typedef HANDLE ProcessHandle; |
michael@0 | 25 | typedef DWORD ProcessId; |
michael@0 | 26 | typedef HANDLE UserTokenHandle; |
michael@0 | 27 | const ProcessHandle kNullProcessHandle = NULL; |
michael@0 | 28 | const ProcessId kNullProcessId = 0; |
michael@0 | 29 | #elif defined(OS_POSIX) |
michael@0 | 30 | // On POSIX, our ProcessHandle will just be the PID. |
michael@0 | 31 | typedef pid_t ProcessHandle; |
michael@0 | 32 | typedef pid_t ProcessId; |
michael@0 | 33 | const ProcessHandle kNullProcessHandle = 0; |
michael@0 | 34 | const ProcessId kNullProcessId = 0; |
michael@0 | 35 | #endif // defined(OS_WIN) |
michael@0 | 36 | |
michael@0 | 37 | // Returns the id of the current process. |
michael@0 | 38 | BASE_EXPORT ProcessId GetCurrentProcId(); |
michael@0 | 39 | |
michael@0 | 40 | // Returns the ProcessHandle of the current process. |
michael@0 | 41 | BASE_EXPORT ProcessHandle GetCurrentProcessHandle(); |
michael@0 | 42 | |
michael@0 | 43 | // Converts a PID to a process handle. This handle must be closed by |
michael@0 | 44 | // CloseProcessHandle when you are done with it. Returns true on success. |
michael@0 | 45 | BASE_EXPORT bool OpenProcessHandle(ProcessId pid, ProcessHandle* handle); |
michael@0 | 46 | |
michael@0 | 47 | // Converts a PID to a process handle. On Windows the handle is opened |
michael@0 | 48 | // with more access rights and must only be used by trusted code. |
michael@0 | 49 | // You have to close returned handle using CloseProcessHandle. Returns true |
michael@0 | 50 | // on success. |
michael@0 | 51 | // TODO(sanjeevr): Replace all calls to OpenPrivilegedProcessHandle with the |
michael@0 | 52 | // more specific OpenProcessHandleWithAccess method and delete this. |
michael@0 | 53 | BASE_EXPORT bool OpenPrivilegedProcessHandle(ProcessId pid, |
michael@0 | 54 | ProcessHandle* handle); |
michael@0 | 55 | |
michael@0 | 56 | // Converts a PID to a process handle using the desired access flags. Use a |
michael@0 | 57 | // combination of the kProcessAccess* flags defined above for |access_flags|. |
michael@0 | 58 | BASE_EXPORT bool OpenProcessHandleWithAccess(ProcessId pid, |
michael@0 | 59 | uint32 access_flags, |
michael@0 | 60 | ProcessHandle* handle); |
michael@0 | 61 | |
michael@0 | 62 | // Closes the process handle opened by OpenProcessHandle. |
michael@0 | 63 | BASE_EXPORT void CloseProcessHandle(ProcessHandle process); |
michael@0 | 64 | |
michael@0 | 65 | // Returns the unique ID for the specified process. This is functionally the |
michael@0 | 66 | // same as Windows' GetProcessId(), but works on versions of Windows before |
michael@0 | 67 | // Win XP SP1 as well. |
michael@0 | 68 | BASE_EXPORT ProcessId GetProcId(ProcessHandle process); |
michael@0 | 69 | |
michael@0 | 70 | #if defined(OS_WIN) |
michael@0 | 71 | enum IntegrityLevel { |
michael@0 | 72 | INTEGRITY_UNKNOWN, |
michael@0 | 73 | LOW_INTEGRITY, |
michael@0 | 74 | MEDIUM_INTEGRITY, |
michael@0 | 75 | HIGH_INTEGRITY, |
michael@0 | 76 | }; |
michael@0 | 77 | // Determine the integrity level of the specified process. Returns false |
michael@0 | 78 | // if the system does not support integrity levels (pre-Vista) or in the case |
michael@0 | 79 | // of an underlying system failure. |
michael@0 | 80 | BASE_EXPORT bool GetProcessIntegrityLevel(ProcessHandle process, |
michael@0 | 81 | IntegrityLevel* level); |
michael@0 | 82 | #endif |
michael@0 | 83 | |
michael@0 | 84 | #if defined(OS_LINUX) || defined(OS_ANDROID) || defined(OS_BSD) |
michael@0 | 85 | // Returns the path to the executable of the given process. |
michael@0 | 86 | BASE_EXPORT FilePath GetProcessExecutablePath(ProcessHandle process); |
michael@0 | 87 | #endif |
michael@0 | 88 | |
michael@0 | 89 | #if defined(OS_POSIX) |
michael@0 | 90 | // Returns the ID for the parent of the given process. |
michael@0 | 91 | BASE_EXPORT ProcessId GetParentProcessId(ProcessHandle process); |
michael@0 | 92 | #endif |
michael@0 | 93 | |
michael@0 | 94 | } // namespace base |
michael@0 | 95 | |
michael@0 | 96 | #endif // BASE_PROCESS_PROCESS_HANDLE_H_ |