1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
|
/*
* swrun_cygwin.c:
* hrSWRunTable data access:
* Cygwin interface
*/
#include <net-snmp/net-snmp-config.h>
#include <stdio.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#include <windows.h>
#include <sys/cygwin.h>
#include <tlhelp32.h>
#include <psapi.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>
#include <net-snmp/library/container.h>
#include <net-snmp/library/snmp_debug.h>
#include <net-snmp/data_access/swrun.h>
#include <net-snmp/data_access/swrun.h>
#include "swrun_private.h"
/*
* a lot of this is "stolen" from cygwin ps.cc
*/
typedef BOOL(WINAPI * ENUMPROCESSMODULES) (HANDLE hProcess,
HMODULE * lphModule,
DWORD cb,
LPDWORD lpcbNeeded);
typedef DWORD(WINAPI * GETMODULEFILENAME) (HANDLE hProcess,
HMODULE hModule,
LPTSTR lpstrFIleName,
DWORD nSize);
typedef DWORD(WINAPI * GETPROCESSMEMORYINFO) (HANDLE hProcess,
PPROCESS_MEMORY_COUNTERS
pmc, DWORD nSize);
typedef HANDLE(WINAPI * CREATESNAPSHOT) (DWORD dwFlags,
DWORD th32ProcessID);
typedef BOOL(WINAPI * PROCESSWALK) (HANDLE hSnapshot,
LPPROCESSENTRY32 lppe);
ENUMPROCESSMODULES myEnumProcessModules;
GETMODULEFILENAME myGetModuleFileNameEx;
CREATESNAPSHOT myCreateToolhelp32Snapshot;
PROCESSWALK myProcess32First;
PROCESSWALK myProcess32Next;
GETPROCESSMEMORYINFO myGetProcessMemoryInfo = NULL;
cygwin_getinfo_types query = CW_GETPINFO;
static BOOL WINAPI
dummyprocessmodules(HANDLE hProcess,
HMODULE * lphModule, DWORD cb, LPDWORD lpcbNeeded)
{
lphModule[0] = (HMODULE) * lpcbNeeded;
*lpcbNeeded = 1;
return 1;
}
static DWORD WINAPI
GetModuleFileNameEx95(HANDLE hProcess,
HMODULE hModule, LPTSTR lpstrFileName, DWORD n)
{
HANDLE h;
DWORD pid = (DWORD) hModule;
PROCESSENTRY32 proc;
h = myCreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (!h)
return 0;
proc.dwSize = sizeof(proc);
if (myProcess32First(h, &proc))
do
if (proc.th32ProcessID == pid) {
CloseHandle(h);
strcpy(lpstrFileName, proc.szExeFile);
return 1;
}
while (myProcess32Next(h, &proc));
CloseHandle(h);
return 0;
}
#define FACTOR (0x19db1ded53ea710LL)
#define NSPERSEC 10000000LL
#define NSPERMSEC 10000LL
static time_t __stdcall
to_time_t(PFILETIME ptr)
{
long rem;
long long x =
((long long) ptr->dwHighDateTime << 32) +
((unsigned) ptr->dwLowDateTime);
x -= FACTOR;
rem = x % NSPERSEC;
rem += NSPERSEC / 2;
x /= NSPERSEC;
x += rem / NSPERSEC;
return x;
}
static long
to_msec(PFILETIME ptr)
{
long long x =
((long long) ptr->dwHighDateTime << 32) +
(unsigned) ptr->dwLowDateTime;
x /= NSPERMSEC;
return x;
}
HMODULE h;
/* ---------------------------------------------------------------------
*/
void
netsnmp_arch_swrun_init(void)
{
if ((h = LoadLibrary("psapi.dll")) != NULL) {
myEnumProcessModules = (ENUMPROCESSMODULES)
GetProcAddress(h, "EnumProcessModules");
myGetModuleFileNameEx = (GETMODULEFILENAME)
GetProcAddress(h, "GetModuleFileNameExA");
myGetProcessMemoryInfo = (GETPROCESSMEMORYINFO)
GetProcAddress(h, "GetProcessMemoryInfo");
if (myEnumProcessModules && myGetModuleFileNameEx)
query = CW_GETPINFO_FULL;
else
snmp_log(LOG_ERR, "hr_swrun failed NT init\n");
} elif ((h = GetModuleHandle("KERNEL32.DLL")) != NULL) {
myCreateToolhelp32Snapshot = (CREATESNAPSHOT)
GetProcAddress(h, "CreateToolhelp32Snapshot");
myProcess32First = (PROCESSWALK) GetProcAddress(h, "Process32First");
myProcess32Next = (PROCESSWALK) GetProcAddress(h, "Process32Next");
myEnumProcessModules = dummyprocessmodules;
myGetModuleFileNameEx = GetModuleFileNameEx95;
if (myCreateToolhelp32Snapshot && myProcess32First
&& myProcess32Next)
#if 0
/*
* This doesn't work at all on Win98 SE
*/
query = CW_GETPINFO_FULL;
#else
query = CW_GETPINFO;
#endif
else
snmp_log(LOG_ERR, "hr_swrun failed non-NT init\n");
}
}
/* ---------------------------------------------------------------------
*/
int
netsnmp_arch_swrun_container_load( netsnmp_container *container, u_int flags)
{
struct external_pinfo *curproc;
pid_t curpid = 0;
DWORD n;
HANDLE h;
FILETIME ct, et, kt, ut;
PROCESS_MEMORY_COUNTERS pmc;
char *cp1, *cp2;
netsnmp_swrun_entry *entry;
cygwin_internal(CW_LOCK_PINFO, 1000);
while (curproc = (struct external_pinfo *)
cygwin_internal(query, curpid | CW_NEXTPID)) {
curpid = curproc->pid;
if (( PID_EXITED & curproc.process_state ) ||
( ~0xffff & curproc.exitcode ))
continue;
entry = netsnmp_swrun_entry_create(curpid);
if (NULL == entry)
continue; /* error already logged by function */
rc = CONTAINER_INSERT(container, entry);
n = lowproc.dwProcessId & 0xffff;
h = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, n);
if (curproc.ppid) {
entry->hrSWRunPath_len =
sprintf(entry->hrSWRunPath, "%.*s",
(int)sizeof(entry->hrSWRunPath) - 1,
cygwin_conv_to_posix_path(curproc.progname));
} else if (query == CW_GETPINFO_FULL) {
if (h) {
HMODULE hm[1000];
if (!myEnumProcessModules(h, hm, sizeof hm, &n))
n = 0;
if (n && myGetModuleFileNameEx(h, hm[0], string,
sizeof string)) {
entry->hrSWRunPath_len =
sprintf(entry->hrSWRunPath, "%.*s",
(int)sizeof(entry->hrSWRunPath) - 1, string);
}
}
}
/*
* Set hrSWRunName to be the last component of hrSWRunPath,
* but without any file extension
*/
if ( entry->hrSWRunPath_len ) {
cp1 = strrchr( entry->hrSWRunPath, '.' );
if ( cp1 )
*cp1 = '\0'; /* Mask the file extension */
cp2 = strrchr( entry->hrSWRunPath, '/' );
if (cp2)
cp2++; /* Find the final component ... */
else
cp2 = entry->hrSWRunPath; /* ... if any */
entry->hrSWRunName_len =
sprintf(entry->hrSWRunName, "%.*s",
(int)sizeof(entry->hrSWRunName) - 1, cp2);
if ( cp1 )
*cp1 = '.'; /* Restore the file extension */
}
/*
* XXX - No information regarding process arguments
* XXX - No information regarding system processes vs applications
*/
if (PID_STOPPED & curproc.process_state ) {
entry->hrSWRunStatus = HRSWRUNSTATUS_NOTRUNNABLE;
}
/* else if (PID_ZOMBIE & curproc.process_state ) */
else if ( ~0xffff & curproc.exitcode )
{
entry->hrSWRunStatus = HRSWRUNSTATUS_INVALID;
} else {
/* entry->hrSWRunStatus = HRSWRUNSTATUS_RUNNABLE; ?? */
entry->hrSWRunStatus = HRSWRUNSTATUS_RUNNING;
}
if (h) {
if (GetProcessTimes(h, &ct, &et, &kt, &ut))
entry->hrSWRunPerfCPU = (to_msec(&kt) + to_msec(&ut)) / 10;
if (myGetProcessMemoryInfo
&& myGetProcessMemoryInfo(h, &pmc, sizeof pmc))
entry->hrSWRunPerfMem = pmc.WorkingSetSize / 1024;
CloseHandle(h);
}
}
cygwin_internal(CW_UNLOCK_PINFO);
DEBUGMSGTL(("swrun:load:arch"," loaded %d entries\n",
CONTAINER_SIZE(container)));
return 0;
}
|