From a08120c3236f2d2f8241615523afb9ac98d5cae3 Mon Sep 17 00:00:00 2001 From: Doug MacEachern Date: Sat, 7 Mar 2009 06:37:36 -0800 Subject: [PATCH 1/5] 10.6 == Snow Leopard --- src/os/darwin/darwin_sigar.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/os/darwin/darwin_sigar.c b/src/os/darwin/darwin_sigar.c index a72b5e99..0ca030b7 100644 --- a/src/os/darwin/darwin_sigar.c +++ b/src/os/darwin/darwin_sigar.c @@ -3410,6 +3410,9 @@ int sigar_os_sys_info_get(sigar_t *sigar, case 5: codename = "Leopard"; break; + case 6: + codename = "Snow Leopard"; + break; default: codename = "Unknown"; break; From 0b335d9025b28d47ff39ecd7d3ab34d8f5b25db1 Mon Sep 17 00:00:00 2001 From: Doug MacEachern Date: Sat, 7 Mar 2009 06:40:23 -0800 Subject: [PATCH 2/5] 6.1 == Windows 7 --- src/os/win32/win32_sigar.c | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/os/win32/win32_sigar.c b/src/os/win32/win32_sigar.c index 9879c701..224edd40 100644 --- a/src/os/win32/win32_sigar.c +++ b/src/os/win32/win32_sigar.c @@ -3465,8 +3465,16 @@ int sigar_os_sys_info_get(sigar_t *sigar, } else if (version.dwMajorVersion == 6) { if (version.sigar_wProductType == VER_NT_WORKSTATION) { - vendor_name = "Windows Vista"; - vendor_version = "Vista"; + if (version.dwMinorVersion == 0) { + vendor_name = "Windows Vista"; + vendor_version = "Vista"; + code_name = "Longhorn"; + } + else { + vendor_name = "Windows 7"; + vendor_version = "7"; + code_name = "Vienna"; + } } else { vendor_name = "Windows 2008"; From c88b49abe230b380d6c1ede47b5dabb8d626d1ad Mon Sep 17 00:00:00 2001 From: Doug MacEachern Date: Tue, 1 Sep 2009 14:09:46 -0700 Subject: [PATCH 3/5] (SIGAR-168) always fallback to wmi when peb fails for proc_args --- src/os/win32/peb.c | 11 ++++++----- src/os/win32/win32_sigar.c | 22 ++++++++++------------ 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/os/win32/peb.c b/src/os/win32/peb.c index 153effe9..ed0147d1 100644 --- a/src/os/win32/peb.c +++ b/src/os/win32/peb.c @@ -171,15 +171,16 @@ int sigar_proc_args_peb_get(sigar_t *sigar, HANDLE proc, } size = rtl_bufsize(buf, rtl.CommandLine); + if (size <= 0) { + return ERROR_DATATYPE_MISMATCH; /* fallback to wmi */ + } memset(buf, '\0', sizeof(buf)); - - if ((size > 0) && - ReadProcessMemory(proc, rtl.CommandLine.Buffer, buf, size, NULL)) - { + + if (ReadProcessMemory(proc, rtl.CommandLine.Buffer, buf, size, NULL)) { return sigar_parse_proc_args(sigar, buf, procargs); } else { - return SIGAR_OK; + return GetLastError(); } } diff --git a/src/os/win32/win32_sigar.c b/src/os/win32/win32_sigar.c index 224edd40..ccbd4456 100644 --- a/src/os/win32/win32_sigar.c +++ b/src/os/win32/win32_sigar.c @@ -1434,20 +1434,18 @@ static int sigar_remote_proc_args_get(sigar_t *sigar, sigar_pid_t pid, char cmdline[SIGAR_CMDLINE_MAX], *ptr = cmdline, *arg; HANDLE proc = open_process(pid); - if (!proc) { - return GetLastError(); + if (proc) { + status = sigar_proc_args_peb_get(sigar, proc, procargs); + + CloseHandle(proc); + + if (status == SIGAR_OK) { + return status; + } } - status = sigar_proc_args_peb_get(sigar, proc, procargs); - - CloseHandle(proc); - - if (status == ERROR_DATATYPE_MISMATCH) { - /* we are 32-bit, pid process is 64-bit */ - status = sigar_proc_args_wmi_get(sigar, pid, procargs); - } - - return status; + /* likely we are 32-bit, pid process is 64-bit */ + return sigar_proc_args_wmi_get(sigar, pid, procargs); } int sigar_os_proc_args_get(sigar_t *sigar, sigar_pid_t pid, From c6b35167c8839dc64312f8ec2084a5e586737597 Mon Sep 17 00:00:00 2001 From: Doug MacEachern Date: Tue, 1 Sep 2009 14:31:22 -0700 Subject: [PATCH 4/5] fix proc_exe fallback to wmi --- src/os/win32/peb.c | 9 +++------ src/os/win32/win32_sigar.c | 6 +++--- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/src/os/win32/peb.c b/src/os/win32/peb.c index ed0147d1..4fdbfcb8 100644 --- a/src/os/win32/peb.c +++ b/src/os/win32/peb.c @@ -99,6 +99,9 @@ int sigar_proc_exe_peb_get(sigar_t *sigar, HANDLE proc, RTL_USER_PROCESS_PARAMETERS rtl; DWORD size; + procexe->name[0] = '\0'; + procexe->cwd[0] = '\0'; + if ((status = sigar_rtl_get(sigar, proc, &rtl)) != SIGAR_OK) { return status; } @@ -111,9 +114,6 @@ int sigar_proc_exe_peb_get(sigar_t *sigar, HANDLE proc, { SIGAR_W2A(buf, procexe->name, sizeof(procexe->name)); } - else { - procexe->name[0] = '\0'; - } size = rtl_bufsize(buf, rtl.CurrentDirectoryName); memset(buf, '\0', sizeof(buf)); @@ -123,9 +123,6 @@ int sigar_proc_exe_peb_get(sigar_t *sigar, HANDLE proc, { SIGAR_W2A(buf, procexe->cwd, sizeof(procexe->cwd)); } - else { - procexe->cwd[0] = '\0'; - } return SIGAR_OK; } diff --git a/src/os/win32/win32_sigar.c b/src/os/win32/win32_sigar.c index ccbd4456..c98af479 100644 --- a/src/os/win32/win32_sigar.c +++ b/src/os/win32/win32_sigar.c @@ -1605,9 +1605,9 @@ SIGAR_DECLARE(int) sigar_proc_exe_get(sigar_t *sigar, sigar_pid_t pid, } status = sigar_proc_exe_peb_get(sigar, proc, procexe); - if (status == ERROR_DATATYPE_MISMATCH) { - /* we are 32-bit, pid process is 64-bit */ - procexe->cwd[0] = '\0'; /* XXX where else can we try? */ + if (procexe->name[0] == '\0') { + /* likely we are 32-bit, pid process is 64-bit */ + /* procexe->cwd[0] = XXX where else can we try? */ status = sigar_proc_exe_wmi_get(sigar, pid, procexe); } From fdc0cf5c1f1da3d085e00ac534cd9d6460f3f9d5 Mon Sep 17 00:00:00 2001 From: Doug MacEachern Date: Tue, 1 Sep 2009 15:39:33 -0700 Subject: [PATCH 5/5] add mapping from wmi HRESULT to NTRESULT --- src/os/win32/win32_sigar.c | 9 ++++++++- src/os/win32/wmi.cpp | 34 +++++++++++++++++++++++++++------- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/src/os/win32/win32_sigar.c b/src/os/win32/win32_sigar.c index c98af479..5ac57b64 100644 --- a/src/os/win32/win32_sigar.c +++ b/src/os/win32/win32_sigar.c @@ -1445,7 +1445,11 @@ static int sigar_remote_proc_args_get(sigar_t *sigar, sigar_pid_t pid, } /* likely we are 32-bit, pid process is 64-bit */ - return sigar_proc_args_wmi_get(sigar, pid, procargs); + status = sigar_proc_args_wmi_get(sigar, pid, procargs); + if (status == ERROR_NOT_FOUND) { + status = SIGAR_NO_SUCH_PROCESS; + } + return status; } int sigar_os_proc_args_get(sigar_t *sigar, sigar_pid_t pid, @@ -1609,6 +1613,9 @@ SIGAR_DECLARE(int) sigar_proc_exe_get(sigar_t *sigar, sigar_pid_t pid, /* likely we are 32-bit, pid process is 64-bit */ /* procexe->cwd[0] = XXX where else can we try? */ status = sigar_proc_exe_wmi_get(sigar, pid, procexe); + if (status == ERROR_NOT_FOUND) { + status = SIGAR_NO_SUCH_PROCESS; + } } if (procexe->cwd[0] != '\0') { diff --git a/src/os/win32/wmi.cpp b/src/os/win32/wmi.cpp index a816bbd9..82229cab 100644 --- a/src/os/win32/wmi.cpp +++ b/src/os/win32/wmi.cpp @@ -42,15 +42,18 @@ class WMI { HRESULT GetProcStringProperty(DWORD pid, TCHAR *name, TCHAR *value, DWORD len); HRESULT GetProcExecutablePath(DWORD pid, TCHAR *value); HRESULT GetProcCommandLine(DWORD pid, TCHAR *value); + int GetLastError(); private: IWbemServices *wbem; + HRESULT result; BSTR GetProcQuery(DWORD pid); }; WMI::WMI() { wbem = NULL; + result = S_OK; CoInitializeEx(NULL, COINIT_MULTITHREADED); } @@ -60,14 +63,31 @@ WMI::~WMI() CoUninitialize(); } +/* XXX must be a better way to map HRESULT */ +int WMI::GetLastError() +{ + switch (result) { + case S_OK: + return ERROR_SUCCESS; + case WBEM_E_NOT_FOUND: + return ERROR_NOT_FOUND; + case WBEM_E_ACCESS_DENIED: + return ERROR_ACCESS_DENIED; + case WBEM_E_NOT_SUPPORTED: + return SIGAR_ENOTIMPL; + default: + return ERROR_INVALID_FUNCTION; + } +} + HRESULT WMI::Open(LPCTSTR machine, LPCTSTR user, LPCTSTR pass) { - HRESULT result; IWbemLocator *locator; wchar_t path[MAX_PATH]; if (wbem) { - return S_OK; + result = S_OK; + return result; } result = @@ -116,6 +136,7 @@ void WMI::Close() if (wbem) { wbem->Release(); wbem = NULL; + result = S_OK; } } @@ -128,7 +149,6 @@ BSTR WMI::GetProcQuery(DWORD pid) HRESULT WMI::GetProcStringProperty(DWORD pid, TCHAR *name, TCHAR *value, DWORD len) { - HRESULT result; IWbemClassObject *obj; VARIANT var; @@ -177,11 +197,11 @@ extern "C" int sigar_proc_args_wmi_get(sigar_t *sigar, sigar_pid_t pid, WMI *wmi = new WMI(); if (FAILED(wmi->Open())) { - return GetLastError(); + return wmi->GetLastError(); } if (FAILED(wmi->GetProcCommandLine(pid, buf))) { - status = GetLastError(); + status = wmi->GetLastError(); } else { status = sigar_parse_proc_args(sigar, buf, procargs); @@ -201,13 +221,13 @@ extern "C" int sigar_proc_exe_wmi_get(sigar_t *sigar, sigar_pid_t pid, WMI *wmi = new WMI(); if (FAILED(wmi->Open())) { - return GetLastError(); + return wmi->GetLastError(); } procexe->name[0] = '\0'; if (FAILED(wmi->GetProcExecutablePath(pid, buf))) { - status = GetLastError(); + status = wmi->GetLastError(); } else { status = SIGAR_OK;