OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/process_util.h" | 5 #include "base/process_util.h" |
6 | 6 |
7 #include <windows.h> | 7 #include <windows.h> |
8 #include <winternl.h> | 8 #include <winternl.h> |
9 #include <psapi.h> | 9 #include <psapi.h> |
10 | 10 |
11 #include "base/histogram.h" | 11 #include "base/histogram.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/scoped_handle_win.h" | 13 #include "base/scoped_handle_win.h" |
14 #include "base/scoped_ptr.h" | 14 #include "base/scoped_ptr.h" |
15 | 15 |
16 namespace { | 16 namespace { |
17 | 17 |
18 // System pagesize. This value remains constant on x86/64 architectures. | 18 // System pagesize. This value remains constant on x86/64 architectures. |
19 const int PAGESIZE_KB = 4; | 19 const int PAGESIZE_KB = 4; |
20 | 20 |
21 // HeapSetInformation function pointer. | 21 // HeapSetInformation function pointer. |
22 typedef BOOL (WINAPI* HeapSetFn)(HANDLE, HEAP_INFORMATION_CLASS, PVOID, SIZE_T); | 22 typedef BOOL (WINAPI* HeapSetFn)(HANDLE, HEAP_INFORMATION_CLASS, PVOID, SIZE_T); |
23 | 23 |
24 } // namespace | 24 } // namespace |
25 | 25 |
26 namespace base { | 26 namespace base { |
27 | 27 |
28 int GetCurrentProcId() { | 28 ProcessId GetCurrentProcId() { |
29 return ::GetCurrentProcessId(); | 29 return ::GetCurrentProcessId(); |
30 } | 30 } |
31 | 31 |
32 ProcessHandle GetCurrentProcessHandle() { | 32 ProcessHandle GetCurrentProcessHandle() { |
33 return ::GetCurrentProcess(); | 33 return ::GetCurrentProcess(); |
34 } | 34 } |
35 | 35 |
36 ProcessHandle OpenProcessHandle(int pid) { | 36 ProcessHandle OpenProcessHandle(ProcessId pid) { |
37 return OpenProcess(PROCESS_DUP_HANDLE | PROCESS_TERMINATE, FALSE, pid); | 37 return OpenProcess(PROCESS_DUP_HANDLE | PROCESS_TERMINATE, FALSE, pid); |
38 } | 38 } |
39 | 39 |
40 void CloseProcessHandle(ProcessHandle process) { | 40 void CloseProcessHandle(ProcessHandle process) { |
41 CloseHandle(process); | 41 CloseHandle(process); |
42 } | 42 } |
43 | 43 |
44 // Helper for GetProcId() | 44 // Helper for GetProcId() |
45 bool GetProcIdViaGetProcessId(ProcessHandle process, DWORD* id) { | 45 bool GetProcIdViaGetProcessId(ProcessHandle process, DWORD* id) { |
46 // Dynamically get a pointer to GetProcessId(). | 46 // Dynamically get a pointer to GetProcessId(). |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 ProcessBasicInformation, | 93 ProcessBasicInformation, |
94 &info, sizeof info, | 94 &info, sizeof info, |
95 &bytes_returned); | 95 &bytes_returned); |
96 if (!SUCCEEDED(status) || (bytes_returned != (sizeof info))) | 96 if (!SUCCEEDED(status) || (bytes_returned != (sizeof info))) |
97 return false; | 97 return false; |
98 | 98 |
99 *id = static_cast<DWORD>(info.UniqueProcessId); | 99 *id = static_cast<DWORD>(info.UniqueProcessId); |
100 return true; | 100 return true; |
101 } | 101 } |
102 | 102 |
103 int GetProcId(ProcessHandle process) { | 103 ProcessId GetProcId(ProcessHandle process) { |
104 // Get a handle to |process| that has PROCESS_QUERY_INFORMATION rights. | 104 // Get a handle to |process| that has PROCESS_QUERY_INFORMATION rights. |
105 HANDLE current_process = GetCurrentProcess(); | 105 HANDLE current_process = GetCurrentProcess(); |
106 HANDLE process_with_query_rights; | 106 HANDLE process_with_query_rights; |
107 if (DuplicateHandle(current_process, process, current_process, | 107 if (DuplicateHandle(current_process, process, current_process, |
108 &process_with_query_rights, PROCESS_QUERY_INFORMATION, | 108 &process_with_query_rights, PROCESS_QUERY_INFORMATION, |
109 false, 0)) { | 109 false, 0)) { |
110 // Try to use GetProcessId(), if it exists. Fall back on | 110 // Try to use GetProcessId(), if it exists. Fall back on |
111 // NtQueryInformationProcess() otherwise (< Win XP SP1). | 111 // NtQueryInformationProcess() otherwise (< Win XP SP1). |
112 DWORD id; | 112 DWORD id; |
113 bool success = | 113 bool success = |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 | 153 |
154 bool LaunchApp(const CommandLine& cl, | 154 bool LaunchApp(const CommandLine& cl, |
155 bool wait, bool start_hidden, ProcessHandle* process_handle) { | 155 bool wait, bool start_hidden, ProcessHandle* process_handle) { |
156 return LaunchApp(cl.command_line_string(), wait, | 156 return LaunchApp(cl.command_line_string(), wait, |
157 start_hidden, process_handle); | 157 start_hidden, process_handle); |
158 } | 158 } |
159 | 159 |
160 // Attempts to kill the process identified by the given process | 160 // Attempts to kill the process identified by the given process |
161 // entry structure, giving it the specified exit code. | 161 // entry structure, giving it the specified exit code. |
162 // Returns true if this is successful, false otherwise. | 162 // Returns true if this is successful, false otherwise. |
163 bool KillProcessById(DWORD process_id, int exit_code, bool wait) { | 163 bool KillProcessById(ProcessId process_id, int exit_code, bool wait) { |
164 HANDLE process = OpenProcess(PROCESS_TERMINATE | SYNCHRONIZE, | 164 HANDLE process = OpenProcess(PROCESS_TERMINATE | SYNCHRONIZE, |
165 FALSE, // Don't inherit handle | 165 FALSE, // Don't inherit handle |
166 process_id); | 166 process_id); |
167 if (!process) | 167 if (!process) |
168 return false; | 168 return false; |
169 | 169 |
170 bool ret = KillProcess(process, exit_code, wait); | 170 bool ret = KillProcess(process, exit_code, wait); |
171 CloseHandle(process); | 171 CloseHandle(process); |
172 return ret; | 172 return ret; |
173 } | 173 } |
(...skipping 468 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
642 void EnableTerminationOnHeapCorruption() { | 642 void EnableTerminationOnHeapCorruption() { |
643 // Ignore the result code. Supported on XP SP3 and Vista. | 643 // Ignore the result code. Supported on XP SP3 and Vista. |
644 HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0); | 644 HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0); |
645 } | 645 } |
646 | 646 |
647 void RaiseProcessToHighPriority() { | 647 void RaiseProcessToHighPriority() { |
648 SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS); | 648 SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS); |
649 } | 649 } |
650 | 650 |
651 } // namespace base | 651 } // namespace base |
OLD | NEW |