Debian Codegraph @

agent/mibgroup/host/data_access/swrun_kinfo.c

raw
1 /*
2 * swrun_kinfo.c:
3 * hrSWRunTable data access:
4 * kvm_getprocs() interface - FreeBSD, NetBSD, OpenBSD
5 *
6 * NB: later FreeBSD and OpenBSD use different kinfo_proc structures
7 */
8 #include <net-snmp/net-snmp-config.h>
9
10 #include <stdio.h>
11 #ifdef HAVE_STDLIB_H
12 #include <stdlib.h>
13 #endif
14 #ifdef HAVE_UNISTD_H
15 #include <unistd.h>
16 #endif
17 #ifdef HAVE_SYS_TYPES_H
18 #include <sys/types.h>
19 #endif
20 #ifdef HAVE_STRING_H
21 #include <string.h>
22 #else
23 #include <strings.h>
24 #endif
25
26 #ifdef HAVE_KVM_H
27 #include <kvm.h>
28 #endif
29 #ifdef HAVE_SYS_PARAM_H
30 #include <sys/param.h>
31 #endif
32 #ifdef HAVE_SYS_SYSCTL_H
33 #include <sys/sysctl.h>
34 #endif
35 #ifdef HAVE_SYS_PROC_H
36 #include <sys/proc.h>
37 #endif
38 #ifdef HAVE_SYS_USER_H
39 #include <sys/user.h>
40 #endif
41 #ifdef HAVE_UVM_UVM_EXTERNAL_H
42 #include <uvm/uvm_external.h>
43 #endif
44
45 #include <net-snmp/net-snmp-includes.h>
46 #include <net-snmp/agent/net-snmp-agent-includes.h>
47 #include <net-snmp/library/container.h>
48 #include <net-snmp/library/snmp_debug.h>
49 #include <net-snmp/data_access/swrun.h>
50 #include "swrun_private.h"
51 #include "../../../kernel.h"
52
53 #if defined(freebsd5) && __FreeBSD_version >= 500014
54 /*
55 * later FreeBSD kinfo_proc field names
56 */
57 #define SWRUN_TABLE kinfo_proc
58 #define SWRUN_K_STAT ki_stat
59 #define SWRUN_K_PID ki_pid
60 #define SWRUN_K_PPID ki_ppid
61 #define SWRUN_K_TID ki_tid
62 #define SWRUN_K_COMM ki_comm
63 #define SWRUN_K_FLAG ki_flag
64 #define SWRUN_K_CLASS ki_pri.pri_class
65
66 #elif defined(HAVE_KVM_GETPROC2) || defined(openbsd5)
67 /*
68 * newer NetBSD, OpenBSD kinfo_proc2 field names
69 */
70 #if defined(openbsd5)
71 #define SWRUN_TABLE kinfo_proc
72 #else
73 #define SWRUN_TABLE kinfo_proc2
74 #endif
75 #define SWRUN_K_STAT p_stat
76 #define SWRUN_K_PID p_pid
77 #define SWRUN_K_COMM p_comm
78 #define SWRUN_K_FLAG p_flag
79 /* SWRUN_K_CLASS not defined */
80
81 #elif defined(dragonfly)
82 /*
83 * DragonFly is different ...
84 */
85 #define SWRUN_TABLE kinfo_proc
86 #define SWRUN_K_STAT kp_stat
87 #define SWRUN_K_PID kp_pid
88 #define SWRUN_K_COMM kp_comm
89 #define SWRUN_K_FLAG kp_flags
90 /* SWRUN_K_CLASS not defined */
91
92 #else
93 /*
94 * early FreeBSD, NetBSD, OpenBSD kinfo_proc field names
95 */
96 #define SWRUN_TABLE kinfo_proc
97 #define SWRUN_K_STAT kp_proc.p_stat
98 #define SWRUN_K_PID kp_proc.p_pid
99 #define SWRUN_K_COMM kp_proc.p_comm
100 #define SWRUN_K_FLAG kp_proc.p_flag
101 /* SWRUN_K_CLASS not defined */
102 #endif
103
104 /*
105 * Define dummy values if not already provided by the system
106 */
107
108 #ifdef dragonfly
109
110 /*
111 * DragonFly is special. p_stat is an enum!
112 */
113
114 #define SRUN 200
115 #define SSLEEP 202
116 #define SWAIT 203
117 #define SLOCK 205
118 #define SDEAD 208
119 #define SONPROC 209
120
121
122 #else
123
124 #ifndef SRUN
125 #define SRUN 200 /* Defined by FreeBSD/OpenBSD, missing in NetBSD */
126 #endif
127 #ifndef SACTIVE
128 #define SACTIVE 201 /* Defined by NetBSD, missing in FreeBSD/OpenBSD */
129 #endif
130 #ifndef SSLEEP
131 #define SSLEEP 202 /* Defined by FreeBSD/OpenBSD, missing in NetBSD */
132 #endif
133 #ifndef SWAIT
134 #define SWAIT 203 /* Defined by FreeBSD, missing in NetBSD/OpenBSD */
135 #endif
136 #ifndef SSTOP
137 #define SSTOP 204 /* Defined by FreeBSD/NetBSD/OpenBSD */
138 #endif
139 #ifndef SLOCK
140 #define SLOCK 205 /* Defined by FreeBSD, missing in NetBSD/OpenBSD */
141 #endif
142 #ifndef SIDL
143 #define SIDL 206 /* Defined by FreeBSD/NetBSD/OpenBSD */
144 #endif
145 #ifndef SZOMB
146 #define SZOMB 207 /* Defined by FreeBSD/NetBSD/OpenBSD */
147 #endif
148 #ifndef SDEAD
149 #define SDEAD 208 /* Defined by OpenBSD, missing in FreeBSD/NetBSD */
150 #endif
151 #ifndef SONPROC
152 #define SONPROC 209 /* Defined by OpenBSD, missing in FreeBSD/NetBSD */
153 #endif
154
155 #endif
156
157 /* ---------------------------------------------------------------------
158 */
159 void
160 netsnmp_arch_swrun_init(void)
161 {
162 #if defined(NETSNMP_CAN_USE_SYSCTL) && defined(CTL_KERN) && defined(KERN_MAXPROC)
163 extern int _swrun_max;
164 size_t max_size = sizeof(_swrun_max);
165 int maxproc_mib[] = { CTL_KERN, KERN_MAXPROC };
166 sysctl(maxproc_mib, 2, &_swrun_max, &max_size, NULL, 0);
167 #endif
168
169 return;
170 }
171
172 /* ---------------------------------------------------------------------
173 */
174 int
175 netsnmp_arch_swrun_container_load( netsnmp_container *container, u_int flags)
176 {
177 struct SWRUN_TABLE *proc_table;
178 int nprocs, i, rc;
179 char buf[BUFSIZ+1], **argv;
180 netsnmp_swrun_entry *entry;
181
182 if ( 0 == kd ) {
183 DEBUGMSGTL(("swrun:load:arch"," Can't query kvm info\n"));
184 return 1; /* No handle for retrieving process table */
185 }
186 #if defined(openbsd5)
187 proc_table = kvm_getprocs(kd, KERN_PROC_KTHREAD, 0, sizeof(struct kinfo_proc), &nprocs );
188 #elif defined(HAVE_KVM_GETPROC2)
189 proc_table = kvm_getproc2(kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc2), &nprocs );
190 #elif defined(KERN_PROC_PROC)
191 proc_table = kvm_getprocs(kd, KERN_PROC_PROC, 0, &nprocs);
192 #else
193 proc_table = kvm_getprocs(kd, KERN_PROC_ALL, 0, &nprocs);
194 #endif
195 for ( i=0 ; i<nprocs; i++ ) {
196 if ( 0 == proc_table[i].SWRUN_K_STAT )
197 continue;
198 if ( -1 == proc_table[i].SWRUN_K_PID )
199 continue;
200 #ifdef SWRUN_K_TID
201 if ( 0 == proc_table[i].SWRUN_K_PPID )
202 proc_table[i].SWRUN_K_PID = proc_table[i].SWRUN_K_TID;
203 #endif
204 entry = netsnmp_swrun_entry_create(proc_table[i].SWRUN_K_PID);
205 if (NULL == entry)
206 continue; /* error already logged by function */
207 rc = CONTAINER_INSERT(container, entry);
208 if (rc < 0) {
209 netsnmp_swrun_entry_free(entry);
210 continue;
211 }
212
213 /*
214 * There are two possible sources for the command being run:
215 * - SWRUN_K_COMM (from the proc_table entry directly)
216 * - running kvm_getargv on the process entry.
217 *
218 * We'll use argv[0] (if set) for hrSWRunPath,
219 * since that might potentially contain the
220 * absolute path to the running binary.
221 * We'll use SWRUN_K_COMM for hrSWRunName,
222 * and as an alternative for hrSWRunPath
223 */
224 #ifdef HAVE_KVM_GETPROC2
225 argv = kvm_getargv2( kd, &(proc_table[i]), 0);
226 #else
227 argv = kvm_getargv( kd, &(proc_table[i]), BUFSIZ);
228 #endif
229
230 entry->hrSWRunName_len = sprintf(entry->hrSWRunName, "%.*s",
231 (int)sizeof(entry->hrSWRunName) - 1,
232 proc_table[i].SWRUN_K_COMM);
233
234 if (argv && *argv) {
235 entry->hrSWRunPath_len = sprintf(entry->hrSWRunPath, "%.*s",
236 (int)sizeof(entry->hrSWRunPath)-1,
237 argv[0]);
238 } else {
239 memcpy( entry->hrSWRunPath, entry->hrSWRunName,
240 entry->hrSWRunName_len );
241 entry->hrSWRunPath_len = entry->hrSWRunName_len;
242 }
243
244 /*
245 * Stitch together the rest of argv[] to build hrSWRunParameters
246 *
247 * Note:
248 * We add a separating space before each argv[] parameter,
249 * *including* the first one. So we need to skip this
250 * leading space (buf[0]) when setting hrSWRunParameters.
251 * This is also why we cleared the first *two* characters
252 * in the buffer initially. If there were no command-line
253 * arguments, then buf[1] would still be a null string.
254 */
255 buf[0] = '\0';
256 buf[1] = '\0';
257 if (argv)
258 argv++; /* Skip argv[0] */
259 while ( argv && *argv ) {
260 strlcat(buf, " ", sizeof(buf));
261 strlcat(buf, *argv, sizeof(buf));
262 argv++;
263 }
264 if (strlen(buf) >= BUFSIZ-10)
265 snmp_log(LOG_ERR, "params %lu/%d %s\n", strlen(buf), BUFSIZ, buf);
266 entry->hrSWRunParameters_len =
267 sprintf(entry->hrSWRunParameters, "%.*s",
268 (int)sizeof(entry->hrSWRunParameters) - 1, buf+1);
269
270 entry->hrSWRunType = (P_SYSTEM & proc_table[i].SWRUN_K_FLAG)
271 #ifdef SWRUN_K_CLASS
272 ? ((PRI_ITHD == proc_table[i].SWRUN_K_CLASS)
273 ? 3 /* device driver */
274 : 2 /* operating system */
275 )
276 : 4 /* application */
277 #else
278 ? 2 /* operating system */
279 : 4 /* application */
280 #endif
281 ;
282
283 #ifdef netbsd5
284 switch (proc_table[i].SWRUN_K_STAT) {
285 case LSONPROC:
286 case LSRUN: entry->hrSWRunStatus = HRSWRUNSTATUS_RUNNING;
287 break;
288 case LSSLEEP: entry->hrSWRunStatus = HRSWRUNSTATUS_RUNNABLE;
289 break;
290 case LSIDL:
291 case LSSUSPENDED:
292 case LSSTOP: entry->hrSWRunStatus = HRSWRUNSTATUS_NOTRUNNABLE;
293 break;
294 #ifdef LSDEAD
295 case LSDEAD:
296 #endif
297 case LSZOMB: entry->hrSWRunStatus = HRSWRUNSTATUS_INVALID;
298 break;
299 default:
300 entry->hrSWRunStatus = HRSWRUNSTATUS_INVALID;
301 snmp_log(LOG_ERR, "Bad process status %c (0x%x)\n", proc_table[i].SWRUN_K_STAT, proc_table[i].SWRUN_K_STAT);
302 break;
303 }
304 #else
305 switch (proc_table[i].SWRUN_K_STAT & 0xFF) {
306 case SONPROC:
307 case SRUN: entry->hrSWRunStatus = HRSWRUNSTATUS_RUNNING;
308 break;
309 case SSLEEP:
310 case SACTIVE:
311 case SWAIT: entry->hrSWRunStatus = HRSWRUNSTATUS_RUNNABLE;
312 break;
313 case SIDL:
314 case SSTOP:
315 case SLOCK: entry->hrSWRunStatus = HRSWRUNSTATUS_NOTRUNNABLE;
316 break;
317
318 case SDEAD:
319 case SZOMB: entry->hrSWRunStatus = HRSWRUNSTATUS_INVALID; /* i.e. "not loaded" */
320 break;
321
322 default: entry->hrSWRunStatus = HRSWRUNSTATUS_INVALID; /* Actually invalid */
323 snmp_log(LOG_ERR, "Bad process status %c (0x%x)\n", proc_table[i].SWRUN_K_STAT, proc_table[i].SWRUN_K_STAT);
324 break;
325 }
326 #endif
327
328 #if defined(freebsd5) && __FreeBSD_version >= 500014
329 entry->hrSWRunPerfCPU = (proc_table[i].ki_rusage.ru_utime.tv_sec*1000000 + proc_table[i].ki_rusage.ru_utime.tv_usec) / 10000;
330 entry->hrSWRunPerfCPU += (proc_table[i].ki_rusage.ru_stime.tv_sec*1000000 + proc_table[i].ki_rusage.ru_stime.tv_usec) / 10000;
331 entry->hrSWRunPerfCPU += (proc_table[i].ki_rusage_ch.ru_utime.tv_sec*1000000 + proc_table[i].ki_rusage_ch.ru_utime.tv_usec) / 10000;
332 entry->hrSWRunPerfCPU += (proc_table[i].ki_rusage_ch.ru_stime.tv_sec*1000000 + proc_table[i].ki_rusage_ch.ru_stime.tv_usec) / 10000;
333 entry->hrSWRunPerfMem = proc_table[i].ki_rssize * (getpagesize()/1024); /* in kB */
334 #elif defined(HAVE_KVM_GETPROC2) || defined(openbsd5)
335 /*
336 * newer NetBSD, OpenBSD
337 */
338 entry->hrSWRunPerfCPU = proc_table[i].p_rtime_sec*100;
339 entry->hrSWRunPerfCPU += proc_table[i].p_rtime_usec / 10000;
340
341 entry->hrSWRunPerfMem = proc_table[i].p_vm_rssize;
342 entry->hrSWRunPerfMem *= (getpagesize() / 1024);
343 #elif defined(dragonfly) && __DragonFly_version >= 190000
344 entry->hrSWRunPerfCPU = proc_table[i].kp_lwp.kl_uticks;
345 entry->hrSWRunPerfCPU += proc_table[i].kp_lwp.kl_sticks;
346 entry->hrSWRunPerfCPU += proc_table[i].kp_lwp.kl_iticks;
347 entry->hrSWRunPerfCPU = entry->hrSWRunPerfCPU / 10000;
348
349 entry->hrSWRunPerfMem = proc_table[i].kp_vm_map_size / 1024;
350 #elif defined(dragonfly)
351 entry->hrSWRunPerfCPU = proc_table[i].kp_eproc.e_uticks;
352 entry->hrSWRunPerfCPU += proc_table[i].kp_eproc.e_sticks;
353 entry->hrSWRunPerfCPU += proc_table[i].kp_eproc.e_iticks;
354
355 entry->hrSWRunPerfMem = proc_table[i].kp_vm_map_size / 1024;
356
357 #else
358 /*
359 * early FreeBSD, NetBSD, OpenBSD
360 */
361 entry->hrSWRunPerfCPU = proc_table[i].kp_proc.p_uticks;
362 entry->hrSWRunPerfCPU += proc_table[i].kp_proc.p_sticks;
363 entry->hrSWRunPerfCPU += proc_table[i].kp_proc.p_iticks;
364 entry->hrSWRunPerfMem = proc_table[i].kp_eproc.e_vm.vm_rssize;
365 entry->hrSWRunPerfMem *= (getpagesize() / 1024);
366 #endif
367 }
368 /*
369 * 'proc_table' is owned by the kvm library,
370 * so shouldn't be freed here.
371 */
372
373 DEBUGMSGTL(("swrun:load:arch","loaded %d entries\n",
374 (int)CONTAINER_SIZE(container)));
375
376 return 0;
377 }