From 2348f71aea0c53216da99f08fc86ff231b9edac1 Mon Sep 17 00:00:00 2001 From: janwas Date: Sat, 26 May 2007 22:42:08 +0000 Subject: [PATCH] mahaf: don't bring up a warning when starting the driver fails (expected case on non-admin accounts) wcpu: detect values once during init (simplifies code a bit) This was SVN commit r5100. --- source/lib/sysdep/win/mahaf.cpp | 22 +++++++--- source/lib/sysdep/win/wcpu.cpp | 76 ++++++++++++++++++++++++--------- 2 files changed, 71 insertions(+), 27 deletions(-) diff --git a/source/lib/sysdep/win/mahaf.cpp b/source/lib/sysdep/win/mahaf.cpp index 3a92e26b94..218f881bd6 100644 --- a/source/lib/sysdep/win/mahaf.cpp +++ b/source/lib/sysdep/win/mahaf.cpp @@ -150,13 +150,21 @@ static SC_HANDLE OpenServiceControlManager() LPCSTR machineName = 0; // local LPCSTR databaseName = 0; // default SC_HANDLE hSCM = OpenSCManager(machineName, databaseName, SC_MANAGER_ALL_ACCESS); - // non-admin account => we can't start the driver. note that installing - // the driver and having it start with Windows would allow access to - // the service even from least-permission accounts. if(!hSCM) - return 0; + { + // administrator privileges are required. note: installing the + // service and having it start automatically would allow + // Least-Permission accounts to use it, but is too invasive and + // thus out of the question. - return hSCM; + // make sure the error is as expected, otherwise something is afoot. + if(GetLastError() != ERROR_ACCESS_DENIED) + debug_assert(0); + + return 0; + } + + return hSCM; // success } @@ -186,6 +194,8 @@ static void UninstallDriver() static void StartDriver(const char* driverPathname) { const SC_HANDLE hSCM = OpenServiceControlManager(); + if(!hSCM) + return; // create service (note: this just enters the service into SCM's DB; // no error is raised if the driver binary doesn't exist etc.) @@ -212,7 +222,7 @@ create: #endif } else - WARN_IF_FALSE(0); // creating actually failed + debug_assert(0); // creating actually failed } } diff --git a/source/lib/sysdep/win/wcpu.cpp b/source/lib/sysdep/win/wcpu.cpp index 36831a9e4f..32e9912b08 100644 --- a/source/lib/sysdep/win/wcpu.cpp +++ b/source/lib/sysdep/win/wcpu.cpp @@ -15,39 +15,47 @@ #include "lib/posix/posix_time.h" #include "win.h" #include "wutil.h" +#include "winit.h" + +#pragma SECTION_PRE_LIBC(B) // early; whrt depends on us +WIN_REGISTER_FUNC(wcpu_Init); +#pragma FORCE_INCLUDE(wcpu_Init) +#pragma SECTION_RESTORE + // limit allows statically allocated per-CPU structures (for simplicity). // WinAPI only supports max. 32 CPUs anyway (due to DWORD bitfields). static const uint MAX_CPUS = 32; -// note: the below routines should cache their results to simplify things -// for callers. however, we don't want to set the variables during init -// because that would create init order dependencies (if other modules -// use this from e.g. their PreLibcInit). hence, each routine must check -// if they've been called for the first time. + +static uint numProcessors = 0; /// get number of CPUs (can't fail) uint wcpu_NumProcessors() { - static uint numProcessors = 0; - if(numProcessors) - return numProcessors; + debug_assert(numProcessors != 0); + return numProcessors; +} +static void DetectNumProcessors() +{ SYSTEM_INFO si; - GetSystemInfo(&si); + GetSystemInfo(&si); // can't fail numProcessors = (uint)si.dwNumberOfProcessors; - - return numProcessors; } +static double clockFrequency = -1.0; + double wcpu_ClockFrequency() { - static double clockFrequency = -1.0; - if(clockFrequency > 0.0) - return clockFrequency; + debug_assert(clockFrequency > 0.0); + return clockFrequency; +} - // read CPU frequency from registry +static void DetectClockFrequency() +{ + // read from registry HKEY hKey; if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, "HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0", 0, KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS) { @@ -62,17 +70,19 @@ double wcpu_ClockFrequency() } else debug_assert(0); - - return clockFrequency; } +static int isThrottlingPossible = -1; + int wcpu_IsThrottlingPossible() { - static int isThrottlingPossible = -1; - if(isThrottlingPossible != -1) - return isThrottlingPossible; + debug_assert(isThrottlingPossible != -1); + return isThrottlingPossible; +} +static void CheckIfThrottlingPossible() +{ WIN_SAVE_LAST_ERROR; // CallNtPowerInformation @@ -138,7 +148,6 @@ int wcpu_IsThrottlingPossible() WIN_RESTORE_LAST_ERROR; debug_assert(isThrottlingPossible == 0 || isThrottlingPossible == 1); - return isThrottlingPossible; } @@ -187,6 +196,31 @@ LibError wcpu_CallByEachCPU(CpuCallback cb, void* param) return INFO::OK; } +//----------------------------------------------------------------------------- + +static LibError wcpu_Init() +{ + DetectNumProcessors(); + DetectClockFrequency(); + CheckIfThrottlingPossible(); + + return INFO::OK; +} + + + + + + + + + + + + + + + ////////////////////////////////////////////////////////////////////////////// //