2008-08-05 22:52:17 +00:00
|
|
|
// much of this code is public-domain
|
|
|
|
//
|
2008-01-31 18:34:51 +00:00
|
|
|
#include "boinc_win.h"
|
2006-11-01 22:40:30 +00:00
|
|
|
#include "error_numbers.h"
|
2006-08-28 18:22:07 +00:00
|
|
|
#include "diagnostics_win.h"
|
2008-09-29 03:04:39 +00:00
|
|
|
#include "str_util.h"
|
2010-03-16 00:07:58 +00:00
|
|
|
#include "str_replace.h"
|
2006-08-25 21:31:05 +00:00
|
|
|
#include "procinfo.h"
|
2006-08-25 21:31:04 +00:00
|
|
|
|
2006-08-28 18:22:07 +00:00
|
|
|
using std::vector;
|
|
|
|
|
|
|
|
// NtQuerySystemInformation
|
|
|
|
typedef NTSTATUS (WINAPI *tNTQSI)(
|
|
|
|
ULONG SystemInformationClass,
|
|
|
|
PVOID SystemInformation,
|
|
|
|
ULONG SystemInformationLength,
|
|
|
|
PULONG ReturnLength
|
|
|
|
);
|
|
|
|
|
2006-08-25 21:31:04 +00:00
|
|
|
static int get_process_information(PVOID* ppBuffer, PULONG pcbBuffer) {
|
|
|
|
NTSTATUS Status = STATUS_INFO_LENGTH_MISMATCH;
|
|
|
|
HANDLE hHeap = GetProcessHeap();
|
2010-08-11 16:35:06 +00:00
|
|
|
HMODULE hNTDllLib = GetModuleHandle(_T("ntdll.dll"));
|
2006-11-02 00:52:55 +00:00
|
|
|
tNTQSI pNTQSI = (tNTQSI)GetProcAddress(hNTDllLib, "NtQuerySystemInformation");
|
|
|
|
ULONG cbBuffer = 0;
|
2006-08-25 21:31:04 +00:00
|
|
|
|
2006-11-01 22:40:30 +00:00
|
|
|
while (1) {
|
2006-11-02 00:52:55 +00:00
|
|
|
// Store the buffer size since it appears that somebody is monkeying around
|
|
|
|
// with the return values on some systems.
|
|
|
|
cbBuffer = *pcbBuffer;
|
|
|
|
|
2006-08-25 21:31:04 +00:00
|
|
|
*ppBuffer = HeapAlloc(hHeap, HEAP_ZERO_MEMORY, *pcbBuffer);
|
|
|
|
if (ppBuffer == NULL) {
|
2006-11-01 22:40:30 +00:00
|
|
|
return ERR_MALLOC;
|
2006-08-25 21:31:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Status = pNTQSI(
|
|
|
|
SystemProcessAndThreadInformation,
|
|
|
|
*ppBuffer,
|
|
|
|
*pcbBuffer,
|
|
|
|
pcbBuffer
|
|
|
|
);
|
|
|
|
|
2006-11-02 00:52:55 +00:00
|
|
|
if (*pcbBuffer < cbBuffer) {
|
2008-09-29 01:55:58 +00:00
|
|
|
// Somebody is trying to screw us up,
|
|
|
|
// so set the value back to the cached size
|
|
|
|
// so we can do something smart like increase the buffer size.
|
2006-11-02 00:52:55 +00:00
|
|
|
*pcbBuffer = cbBuffer;
|
|
|
|
}
|
|
|
|
|
2006-08-25 21:31:04 +00:00
|
|
|
if (Status == STATUS_INFO_LENGTH_MISMATCH) {
|
|
|
|
HeapFree(hHeap, NULL, *ppBuffer);
|
|
|
|
*pcbBuffer *= 2;
|
|
|
|
} else if (!NT_SUCCESS(Status)) {
|
|
|
|
HeapFree(hHeap, NULL, *ppBuffer);
|
2006-11-01 22:40:30 +00:00
|
|
|
return ERR_GETRUSAGE;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0; // never reached
|
2006-08-25 21:31:04 +00:00
|
|
|
}
|
|
|
|
|
2006-09-11 17:32:18 +00:00
|
|
|
// Note: the following will work on both NT and XP,
|
|
|
|
// because the NT process structure differs only at the end
|
|
|
|
//
|
2011-09-02 17:45:29 +00:00
|
|
|
int get_procinfo_XP(PROC_MAP& pm) {
|
2006-11-02 00:52:55 +00:00
|
|
|
ULONG cbBuffer = 128*1024; // 128k initial buffer
|
2006-08-25 21:31:04 +00:00
|
|
|
PVOID pBuffer = NULL;
|
|
|
|
PSYSTEM_PROCESSES pProcesses = NULL;
|
2010-04-13 08:52:50 +00:00
|
|
|
static DWORD pid = 0;
|
2010-02-15 01:00:32 +00:00
|
|
|
|
|
|
|
if (!pid) {
|
|
|
|
pid = GetCurrentProcessId();
|
|
|
|
}
|
2007-06-01 22:34:47 +00:00
|
|
|
#if 0
|
|
|
|
printf("FILETIME: %d\n", sizeof(FILETIME));
|
|
|
|
printf("LARGE_INTEGER: %d\n", sizeof(LARGE_INTEGER));
|
|
|
|
printf("DWORD: %d\n", sizeof(DWORD));
|
|
|
|
printf("UNICODE_STRING: %d\n", sizeof(UNICODE_STRING));
|
|
|
|
printf("KPRIORITY: %d\n", sizeof(KPRIORITY));
|
|
|
|
printf("ULONG: %d\n", sizeof(ULONG));
|
|
|
|
printf("SIZE_T: %d\n", sizeof(SIZE_T));
|
|
|
|
#endif
|
2006-08-25 21:31:04 +00:00
|
|
|
|
|
|
|
get_process_information(&pBuffer, &cbBuffer);
|
|
|
|
pProcesses = (PSYSTEM_PROCESSES)pBuffer;
|
|
|
|
while (pProcesses) {
|
|
|
|
PROCINFO p;
|
2006-08-28 18:22:07 +00:00
|
|
|
p.id = pProcesses->ProcessId;
|
|
|
|
p.parentid = pProcesses->InheritedFromProcessId;
|
|
|
|
p.swap_size = pProcesses->VmCounters.PagefileUsage;
|
2006-08-25 21:31:04 +00:00
|
|
|
p.working_set_size = pProcesses->VmCounters.WorkingSetSize;
|
2006-09-08 19:27:42 +00:00
|
|
|
p.page_fault_count = pProcesses->VmCounters.PageFaultCount;
|
2006-08-28 18:22:07 +00:00
|
|
|
p.user_time = ((double) pProcesses->UserTime.QuadPart)/1e7;
|
|
|
|
p.kernel_time = ((double) pProcesses->KernelTime.QuadPart)/1e7;
|
|
|
|
p.id = pProcesses->ProcessId;
|
|
|
|
p.parentid = pProcesses->InheritedFromProcessId;
|
2010-09-29 18:15:24 +00:00
|
|
|
p.is_low_priority = (pProcesses->BasePriority <= 4);
|
2008-09-29 03:04:39 +00:00
|
|
|
WideCharToMultiByte(CP_ACP, 0,
|
|
|
|
pProcesses->ProcessName.Buffer,
|
|
|
|
pProcesses->ProcessName.Length,
|
|
|
|
p.command,
|
|
|
|
sizeof(p.command),
|
|
|
|
NULL, NULL
|
|
|
|
);
|
2010-05-18 20:16:46 +00:00
|
|
|
p.is_boinc_app = (p.id == (int)pid) || (strcasestr(p.command, "boinc") != NULL);
|
2010-04-13 08:52:50 +00:00
|
|
|
|
2010-04-13 09:40:22 +00:00
|
|
|
#ifdef _GRIDREPUBLIC
|
2010-04-15 23:03:22 +00:00
|
|
|
if (!strcmp(p.command, "gridrepublic.exe")) {
|
2010-04-13 09:40:22 +00:00
|
|
|
p.is_boinc_app = true;
|
2010-04-13 08:52:50 +00:00
|
|
|
}
|
2010-04-13 09:40:22 +00:00
|
|
|
#endif
|
2010-04-13 09:42:19 +00:00
|
|
|
#ifdef _PROGRESSTHRUPROCESSORS
|
2010-04-15 23:03:22 +00:00
|
|
|
if (!strcmp(p.command, "progressthruprocessors.exe")) {
|
2010-04-13 09:40:22 +00:00
|
|
|
p.is_boinc_app = true;
|
|
|
|
}
|
|
|
|
#endif
|
2011-09-02 17:45:29 +00:00
|
|
|
pm.insert(std::pair<int, PROCINFO>(p.id, p));
|
2006-08-25 21:31:04 +00:00
|
|
|
if (!pProcesses->NextEntryDelta) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pProcesses = (PSYSTEM_PROCESSES)(((LPBYTE)pProcesses) + pProcesses->NextEntryDelta);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pBuffer) HeapFree(GetProcessHeap(), NULL, pBuffer);
|
2011-09-02 17:45:29 +00:00
|
|
|
find_children(pm);
|
2006-08-25 21:31:04 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2006-08-28 18:22:07 +00:00
|
|
|
// get a list of all running processes.
|
|
|
|
//
|
2011-09-02 17:45:29 +00:00
|
|
|
int procinfo_setup(PROC_MAP& pm) {
|
2006-08-25 21:31:04 +00:00
|
|
|
OSVERSIONINFO osvi;
|
|
|
|
osvi.dwOSVersionInfoSize = sizeof(osvi);
|
|
|
|
GetVersionEx(&osvi);
|
|
|
|
|
|
|
|
switch(osvi.dwPlatformId) {
|
|
|
|
case VER_PLATFORM_WIN32_WINDOWS:
|
|
|
|
// Win95, Win98, WinME
|
2006-09-11 17:32:18 +00:00
|
|
|
return 0; // not supported
|
2006-08-25 21:31:04 +00:00
|
|
|
case VER_PLATFORM_WIN32_NT:
|
2011-09-02 17:45:29 +00:00
|
|
|
return get_procinfo_XP(pm);
|
2006-08-25 21:31:04 +00:00
|
|
|
}
|
2006-09-11 17:32:18 +00:00
|
|
|
return 0;
|
2006-08-28 18:22:07 +00:00
|
|
|
}
|