Debian Codegraph @

agent/mibgroup/host/data_access/swrun_cygwin.c

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