use PEB to get the proc_exe.cwd

This commit is contained in:
Doug MacEachern 2004-08-01 20:31:42 +00:00
parent 3401932ba5
commit d939d1763c
3 changed files with 31 additions and 31 deletions

View File

@ -8,7 +8,10 @@
#include "sigar_private.h" #include "sigar_private.h"
#include "sigar_os.h" #include "sigar_os.h"
#define START_ADDRESS (LPVOID)0x00020498 #define PAGE_START 0x00020000
#define CWD_OFFSET PAGE_START + 0x0290
#define PATH_OFFSET PAGE_START + 0x0498
#define START_ADDRESS PAGE_START + 0x0498
static int sigar_peb_get(sigar_t *sigar, HANDLE proc, DWORD *base) static int sigar_peb_get(sigar_t *sigar, HANDLE proc, DWORD *base)
{ {
@ -19,7 +22,7 @@ static int sigar_peb_get(sigar_t *sigar, HANDLE proc, DWORD *base)
sigar->peb = malloc(sigar->pagesize); sigar->peb = malloc(sigar->pagesize);
} }
if (!VirtualQueryEx(proc, START_ADDRESS, &mbi, sizeof(mbi))) { if (!VirtualQueryEx(proc, (char*)START_ADDRESS, &mbi, sizeof(mbi))) {
return GetLastError(); return GetLastError();
} }
@ -34,15 +37,19 @@ static int sigar_peb_get(sigar_t *sigar, HANDLE proc, DWORD *base)
return SIGAR_OK; return SIGAR_OK;
} }
//point scratch to env block #define SKIP_NULL(scratch) \
#define PEB_FIRST(scratch, base) \ if (*scratch == '\0') scratch += sizeof(WCHAR)
scratch = sigar->peb + ((DWORD)START_ADDRESS - base)
#define PEB_START(scratch, base, offset) \
scratch = sigar->peb + ((DWORD)offset - base)
//point scratch to next string (assumes PEB_FIRST) //point scratch to next string (assumes PEB_FIRST)
#define PEB_NEXT(scratch) \ #define PEB_NEXT(scratch) \
scratch = scratch + (wcslen((LPWSTR)scratch) + 1) * sizeof(WCHAR) scratch = scratch + (wcslen((LPWSTR)scratch) + 1) * sizeof(WCHAR); \
SKIP_NULL(scratch)
int sigar_proc_exe_name_get(sigar_t *sigar, HANDLE proc, char *name) int sigar_proc_exe_peb_get(sigar_t *sigar, HANDLE proc,
sigar_proc_exe_t *procexe)
{ {
int status; int status;
LPBYTE scratch; LPBYTE scratch;
@ -53,20 +60,21 @@ int sigar_proc_exe_name_get(sigar_t *sigar, HANDLE proc, char *name)
return status; return status;
} }
//skip env PATH PEB_START(scratch, base, CWD_OFFSET);
PEB_FIRST(scratch, base);
PEB_NEXT(scratch);
//seems common, reason unknown.
if (*scratch == '\0') {
scratch += sizeof(WCHAR);
}
wcsncpy(buf, (LPWSTR)scratch, MAX_PATH); wcsncpy(buf, (LPWSTR)scratch, MAX_PATH);
buf[MAX_PATH-1] = L'\0'; buf[MAX_PATH-1] = L'\0';
SIGAR_W2A(buf, name, MAX_PATH); SIGAR_W2A(buf, procexe->cwd, sizeof(procexe->cwd));
PEB_START(scratch, base, PATH_OFFSET);
PEB_NEXT(scratch); //skip PATH
wcsncpy(buf, (LPWSTR)scratch, MAX_PATH);
buf[MAX_PATH-1] = L'\0';
SIGAR_W2A(buf, procexe->name, sizeof(procexe->name));
return SIGAR_OK; return SIGAR_OK;
} }
@ -82,19 +90,11 @@ int sigar_proc_cmdline_get(sigar_t *sigar, HANDLE proc, char *cmdline)
return status; return status;
} }
//skip env block PEB_START(scratch, base, PATH_OFFSET);
PEB_FIRST(scratch, base);
PEB_NEXT(scratch); PEB_NEXT(scratch); //skip PATH
//seems common, reason unknown.
if (*scratch == '\0') {
scratch += sizeof(WCHAR);
}
PEB_NEXT(scratch); PEB_NEXT(scratch); //skip exe name
if (*scratch == '\0') {
scratch += sizeof(WCHAR);
}
wcsncpy(buf, (LPWSTR)scratch, MAX_PATH); wcsncpy(buf, (LPWSTR)scratch, MAX_PATH);
buf[MAX_PATH-1] = L'\0'; buf[MAX_PATH-1] = L'\0';

View File

@ -119,7 +119,8 @@ struct sigar_t {
int sigar_wsa_init(sigar_t *sigar); int sigar_wsa_init(sigar_t *sigar);
int sigar_proc_exe_name_get(sigar_t *sigar, HANDLE proc, char *name); int sigar_proc_exe_peb_get(sigar_t *sigar, HANDLE proc,
sigar_proc_exe_t *procexe);
int sigar_proc_cmdline_get(sigar_t *sigar, HANDLE proc, char *cmdline); int sigar_proc_cmdline_get(sigar_t *sigar, HANDLE proc, char *cmdline);

View File

@ -1192,10 +1192,9 @@ SIGAR_DECLARE(int) sigar_proc_exe_get(sigar_t *sigar, sigar_pid_t pid,
return GetLastError(); return GetLastError();
} }
procexe->cwd[0] = '\0';
procexe->root[0] = '\0'; procexe->root[0] = '\0';
status = sigar_proc_exe_name_get(sigar, proc, procexe->name); status = sigar_proc_exe_peb_get(sigar, proc, procexe);
return status; return status;
} }