Merge branch 'sigar-1.6'

This commit is contained in:
Doug MacEachern 2009-09-01 15:39:42 -07:00
commit 1676d54123
3 changed files with 54 additions and 31 deletions

View File

@ -99,6 +99,9 @@ int sigar_proc_exe_peb_get(sigar_t *sigar, HANDLE proc,
RTL_USER_PROCESS_PARAMETERS rtl; RTL_USER_PROCESS_PARAMETERS rtl;
DWORD size; DWORD size;
procexe->name[0] = '\0';
procexe->cwd[0] = '\0';
if ((status = sigar_rtl_get(sigar, proc, &rtl)) != SIGAR_OK) { if ((status = sigar_rtl_get(sigar, proc, &rtl)) != SIGAR_OK) {
return status; 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)); SIGAR_W2A(buf, procexe->name, sizeof(procexe->name));
} }
else {
procexe->name[0] = '\0';
}
size = rtl_bufsize(buf, rtl.CurrentDirectoryName); size = rtl_bufsize(buf, rtl.CurrentDirectoryName);
memset(buf, '\0', sizeof(buf)); 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)); SIGAR_W2A(buf, procexe->cwd, sizeof(procexe->cwd));
} }
else {
procexe->cwd[0] = '\0';
}
return SIGAR_OK; return SIGAR_OK;
} }
@ -171,15 +168,16 @@ int sigar_proc_args_peb_get(sigar_t *sigar, HANDLE proc,
} }
size = rtl_bufsize(buf, rtl.CommandLine); size = rtl_bufsize(buf, rtl.CommandLine);
if (size <= 0) {
return ERROR_DATATYPE_MISMATCH; /* fallback to wmi */
}
memset(buf, '\0', sizeof(buf)); memset(buf, '\0', sizeof(buf));
if ((size > 0) && if (ReadProcessMemory(proc, rtl.CommandLine.Buffer, buf, size, NULL)) {
ReadProcessMemory(proc, rtl.CommandLine.Buffer, buf, size, NULL))
{
return sigar_parse_proc_args(sigar, buf, procargs); return sigar_parse_proc_args(sigar, buf, procargs);
} }
else { else {
return SIGAR_OK; return GetLastError();
} }
} }

View File

@ -1435,19 +1435,21 @@ static int sigar_remote_proc_args_get(sigar_t *sigar, sigar_pid_t pid,
char cmdline[SIGAR_CMDLINE_MAX], *ptr = cmdline, *arg; char cmdline[SIGAR_CMDLINE_MAX], *ptr = cmdline, *arg;
HANDLE proc = open_process(pid); HANDLE proc = open_process(pid);
if (!proc) { if (proc) {
return GetLastError();
}
status = sigar_proc_args_peb_get(sigar, proc, procargs); status = sigar_proc_args_peb_get(sigar, proc, procargs);
CloseHandle(proc); CloseHandle(proc);
if (status == ERROR_DATATYPE_MISMATCH) { if (status == SIGAR_OK) {
/* we are 32-bit, pid process is 64-bit */ return status;
status = sigar_proc_args_wmi_get(sigar, pid, procargs); }
} }
/* likely we are 32-bit, pid process is 64-bit */
status = sigar_proc_args_wmi_get(sigar, pid, procargs);
if (status == ERROR_NOT_FOUND) {
status = SIGAR_NO_SUCH_PROCESS;
}
return status; return status;
} }
@ -1608,10 +1610,13 @@ SIGAR_DECLARE(int) sigar_proc_exe_get(sigar_t *sigar, sigar_pid_t pid,
} }
status = sigar_proc_exe_peb_get(sigar, proc, procexe); status = sigar_proc_exe_peb_get(sigar, proc, procexe);
if (status == ERROR_DATATYPE_MISMATCH) { if (procexe->name[0] == '\0') {
/* we are 32-bit, pid process is 64-bit */ /* likely we are 32-bit, pid process is 64-bit */
procexe->cwd[0] = '\0'; /* XXX where else can we try? */ /* procexe->cwd[0] = XXX where else can we try? */
status = sigar_proc_exe_wmi_get(sigar, pid, procexe); status = sigar_proc_exe_wmi_get(sigar, pid, procexe);
if (status == ERROR_NOT_FOUND) {
status = SIGAR_NO_SUCH_PROCESS;
}
} }
if (procexe->cwd[0] != '\0') { if (procexe->cwd[0] != '\0') {

View File

@ -42,15 +42,18 @@ class WMI {
HRESULT GetProcStringProperty(DWORD pid, TCHAR *name, TCHAR *value, DWORD len); HRESULT GetProcStringProperty(DWORD pid, TCHAR *name, TCHAR *value, DWORD len);
HRESULT GetProcExecutablePath(DWORD pid, TCHAR *value); HRESULT GetProcExecutablePath(DWORD pid, TCHAR *value);
HRESULT GetProcCommandLine(DWORD pid, TCHAR *value); HRESULT GetProcCommandLine(DWORD pid, TCHAR *value);
int GetLastError();
private: private:
IWbemServices *wbem; IWbemServices *wbem;
HRESULT result;
BSTR GetProcQuery(DWORD pid); BSTR GetProcQuery(DWORD pid);
}; };
WMI::WMI() WMI::WMI()
{ {
wbem = NULL; wbem = NULL;
result = S_OK;
CoInitializeEx(NULL, COINIT_MULTITHREADED); CoInitializeEx(NULL, COINIT_MULTITHREADED);
} }
@ -60,14 +63,31 @@ WMI::~WMI()
CoUninitialize(); 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 WMI::Open(LPCTSTR machine, LPCTSTR user, LPCTSTR pass)
{ {
HRESULT result;
IWbemLocator *locator; IWbemLocator *locator;
wchar_t path[MAX_PATH]; wchar_t path[MAX_PATH];
if (wbem) { if (wbem) {
return S_OK; result = S_OK;
return result;
} }
result = result =
@ -116,6 +136,7 @@ void WMI::Close()
if (wbem) { if (wbem) {
wbem->Release(); wbem->Release();
wbem = NULL; 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 WMI::GetProcStringProperty(DWORD pid, TCHAR *name, TCHAR *value, DWORD len)
{ {
HRESULT result;
IWbemClassObject *obj; IWbemClassObject *obj;
VARIANT var; 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(); WMI *wmi = new WMI();
if (FAILED(wmi->Open())) { if (FAILED(wmi->Open())) {
return GetLastError(); return wmi->GetLastError();
} }
if (FAILED(wmi->GetProcCommandLine(pid, buf))) { if (FAILED(wmi->GetProcCommandLine(pid, buf))) {
status = GetLastError(); status = wmi->GetLastError();
} }
else { else {
status = sigar_parse_proc_args(sigar, buf, procargs); 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(); WMI *wmi = new WMI();
if (FAILED(wmi->Open())) { if (FAILED(wmi->Open())) {
return GetLastError(); return wmi->GetLastError();
} }
procexe->name[0] = '\0'; procexe->name[0] = '\0';
if (FAILED(wmi->GetProcExecutablePath(pid, buf))) { if (FAILED(wmi->GetProcExecutablePath(pid, buf))) {
status = GetLastError(); status = wmi->GetLastError();
} }
else { else {
status = SIGAR_OK; status = SIGAR_OK;