summaryrefslogtreecommitdiffstats
path: root/base/process_util_win.cc
diff options
context:
space:
mode:
authorjamesr@chromium.org <jamesr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-12-07 21:00:01 +0000
committerjamesr@chromium.org <jamesr@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2009-12-07 21:00:01 +0000
commitf161af217a213baca993696b83d351471b9f4105 (patch)
tree62fc7aea7ca428a6ddab3b38cdc2a8016774194e /base/process_util_win.cc
parent1c9735aa8747af43bddbe8c785af05f0d55538b1 (diff)
downloadchromium_src-f161af217a213baca993696b83d351471b9f4105.zip
chromium_src-f161af217a213baca993696b83d351471b9f4105.tar.gz
chromium_src-f161af217a213baca993696b83d351471b9f4105.tar.bz2
Make base::GetProcId() thread-safe on windows and drop w2k support
base::GetProcId() was previously not threadsafe since it relied upon threads observing reads between two different static variables in the same order that they were written without any explicit synchronization. This probably is responsible for a small number of our startup crashes (http://crash/search?query=product:%22Chrome%22+base::GetProcIdViaGetProcessId). This fixes the race by dropping w2k support and directly calling the function. TEST=start up without crashing BUG=none Review URL: http://codereview.chromium.org/460063 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@33987 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/process_util_win.cc')
-rw-r--r--base/process_util_win.cc70
1 files changed, 2 insertions, 68 deletions
diff --git a/base/process_util_win.cc b/base/process_util_win.cc
index 18de37f..4d74226 100644
--- a/base/process_util_win.cc
+++ b/base/process_util_win.cc
@@ -7,7 +7,6 @@
#include <fcntl.h>
#include <io.h>
#include <windows.h>
-#include <winternl.h>
#include <psapi.h>
#include <ios>
@@ -118,65 +117,6 @@ void CloseProcessHandle(ProcessHandle process) {
CloseHandle(process);
}
-// Helper for GetProcId()
-bool GetProcIdViaGetProcessId(ProcessHandle process, DWORD* id) {
- // Dynamically get a pointer to GetProcessId().
- typedef DWORD (WINAPI *GetProcessIdFunction)(HANDLE);
- static GetProcessIdFunction GetProcessIdPtr = NULL;
- static bool initialize_get_process_id = true;
- if (initialize_get_process_id) {
- initialize_get_process_id = false;
- HMODULE kernel32_handle = GetModuleHandle(L"kernel32.dll");
- if (!kernel32_handle) {
- NOTREACHED();
- return false;
- }
- GetProcessIdPtr = reinterpret_cast<GetProcessIdFunction>(GetProcAddress(
- kernel32_handle, "GetProcessId"));
- }
- if (!GetProcessIdPtr)
- return false;
- // Ask for the process ID.
- *id = (*GetProcessIdPtr)(process);
- return true;
-}
-
-// Helper for GetProcId()
-bool GetProcIdViaNtQueryInformationProcess(ProcessHandle process, DWORD* id) {
- // Dynamically get a pointer to NtQueryInformationProcess().
- typedef NTSTATUS (WINAPI *NtQueryInformationProcessFunction)(
- HANDLE, PROCESSINFOCLASS, PVOID, ULONG, PULONG);
- static NtQueryInformationProcessFunction NtQueryInformationProcessPtr = NULL;
- static bool initialize_query_information_process = true;
- if (initialize_query_information_process) {
- initialize_query_information_process = false;
- // According to nsylvain, ntdll.dll is guaranteed to be loaded, even though
- // the Windows docs seem to imply that you should LoadLibrary() it.
- HMODULE ntdll_handle = GetModuleHandle(L"ntdll.dll");
- if (!ntdll_handle) {
- NOTREACHED();
- return false;
- }
- NtQueryInformationProcessPtr =
- reinterpret_cast<NtQueryInformationProcessFunction>(GetProcAddress(
- ntdll_handle, "NtQueryInformationProcess"));
- }
- if (!NtQueryInformationProcessPtr)
- return false;
- // Ask for the process ID.
- PROCESS_BASIC_INFORMATION info;
- ULONG bytes_returned;
- NTSTATUS status = (*NtQueryInformationProcessPtr)(process,
- ProcessBasicInformation,
- &info, sizeof info,
- &bytes_returned);
- if (!SUCCEEDED(status) || (bytes_returned != (sizeof info)))
- return false;
-
- *id = static_cast<DWORD>(info.UniqueProcessId);
- return true;
-}
-
ProcessId GetProcId(ProcessHandle process) {
// Get a handle to |process| that has PROCESS_QUERY_INFORMATION rights.
HANDLE current_process = GetCurrentProcess();
@@ -184,15 +124,9 @@ ProcessId GetProcId(ProcessHandle process) {
if (DuplicateHandle(current_process, process, current_process,
&process_with_query_rights, PROCESS_QUERY_INFORMATION,
false, 0)) {
- // Try to use GetProcessId(), if it exists. Fall back on
- // NtQueryInformationProcess() otherwise (< Win XP SP1).
- DWORD id;
- bool success =
- GetProcIdViaGetProcessId(process_with_query_rights, &id) ||
- GetProcIdViaNtQueryInformationProcess(process_with_query_rights, &id);
+ DWORD id = GetProcessId(process_with_query_rights);
CloseHandle(process_with_query_rights);
- if (success)
- return id;
+ return id;
}
// We're screwed.