/
swrun_darwin.c
495 lines (439 loc) · 16.5 KB
/
swrun_darwin.c
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/*
* swrun_darwin.c:
* hrSWRunTable data access:
* Darwin
*/
/*
* Copyright (C) 2007 Apple, Inc. All rights reserved.
* Use is subject to license terms specified in the COPYING file
* distributed with the Net-SNMP package.
*/
#include <net-snmp/net-snmp-config.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 "swrun_private.h"
#include <stdlib.h>
#include <unistd.h>
#include <libproc.h>
#include <sys/proc_info.h>
#include <sys/sysctl.h> /* for sysctl() and struct kinfo_proc */
#define __APPLE_API_EVOLVING 1
#include <sys/acl.h> /* or else CoreFoundation.h barfs */
#undef __APPLE_API_EVOLVING
#include <CoreFoundation/CFBase.h>
#include <CoreFoundation/CFNumber.h>
#include <CoreFoundation/CFBundle.h>
#include <CoreServices/CoreServices.h>
#include <IOKit/IOCFBundle.h>
#include <mach/mach.h>
#include <mach/mach_time.h>
/** sigh... can't find Processes.h */
#ifndef kProcessDictionaryIncludeAllInformationMask
#define kProcessDictionaryIncludeAllInformationMask (long)0xFFFFFFFF
#endif
#ifndef procNotFound
#define procNotFound -600
#endif
/* ---------------------------------------------------------------------
*/
static int _kern_argmax;
static int _set_command_name(netsnmp_swrun_entry *entry);
/** avoid kernel bug in 10.2. 8192 oughta be enough anyways, right? */
#define MAX_KERN_ARGMAX 8192
/* ---------------------------------------------------------------------
*/
void
netsnmp_arch_swrun_init(void)
{
int mib[2] = { CTL_KERN, KERN_ARGMAX };
size_t size, mib_size = sizeof(mib)/sizeof(mib[0]);
DEBUGMSGTL(("swrun:load:arch","init\n"));
size = sizeof(_kern_argmax);
if (sysctl(mib, mib_size, &_kern_argmax, &size, NULL, 0) == -1) {
snmp_log(LOG_ERR, "Error in ARGMAX sysctl(): %s", strerror(errno));
_kern_argmax = MAX_KERN_ARGMAX;
}
else if (_kern_argmax > MAX_KERN_ARGMAX) {
DEBUGMSGTL(("swrun:load:arch",
"artificially limiting ARGMAX to %d (from %d)\n",
MAX_KERN_ARGMAX, _kern_argmax));
_kern_argmax = MAX_KERN_ARGMAX;
}
}
/* ---------------------------------------------------------------------
*/
#define SWRUNINDENT " "
int
netsnmp_arch_swrun_container_load( netsnmp_container *container, u_int flags)
{
int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL};
size_t buf_size, mib_size = sizeof(mib)/sizeof(mib[0]);
struct kinfo_proc *processes = NULL;
struct proc_taskallinfo taskinfo;
netsnmp_swrun_entry *entry;
int rc, num_entries, i;
DEBUGMSGTL(("swrun:load:arch"," load\n"));
/*
* get size to allocate. This introduces a bit of a race condition,
* as the size could change between this call and the next...
*/
rc = sysctl(mib, mib_size, NULL, &buf_size, NULL, 0);
if (rc < 0) {
snmp_log(LOG_ERR, "KERN_PROC_ALL size sysctl failed: %d\n", rc);
return -1;
}
processes = (struct kinfo_proc*) malloc(buf_size);
if (NULL == processes) {
snmp_log(LOG_ERR, "malloc failed\n");
return -1;
}
rc = sysctl(mib, mib_size, processes, &buf_size, NULL, 0);
if (rc < 0) {
snmp_log(LOG_ERR, "KERN_PROC_ALL sysctl failed: %d\n", rc);
free(processes);
return -1;
}
num_entries = buf_size / sizeof(struct kinfo_proc);
for (i = 0; i < num_entries; i++) {
/*
* skip empty names.
* p_stat = (SIDL|SRUN|SSLEEP|SSTOP|SZOMB)
*/
if (('\0' == processes[i].kp_proc.p_comm[0]) ||
(0 == processes[i].kp_proc.p_pid)) {
DEBUGMSGTL(("swrun:load:arch",
" skipping p_comm '%s', pid %5d, p_pstat %d\n",
processes[i].kp_proc.p_comm,
processes[i].kp_proc.p_pid,
processes[i].kp_proc.p_stat));
continue;
}
DEBUGMSGTL(("swrun:load:arch"," %s pid %5d\n",
processes[i].kp_proc.p_comm,
processes[i].kp_proc.p_pid));
entry = netsnmp_swrun_entry_create(processes[i].kp_proc.p_pid);
if (NULL == entry)
continue; /* error already logged by function */
rc = CONTAINER_INSERT(container, entry);
/*
* p_comm is a partial name, but it is all we have at this point.
*/
entry->hrSWRunName_len = snprintf(entry->hrSWRunName,
sizeof(entry->hrSWRunName)-1,
"%s", processes[i].kp_proc.p_comm);
/** sysctl for name, path, params */
rc = _set_command_name(entry);
/*
* map p_stat to RunStatus. Odd that there is no 'running' status.
*/
switch(processes[i].kp_proc.p_stat) {
case SRUN:
entry->hrSWRunStatus = HRSWRUNSTATUS_RUNNABLE;
break;
case SSLEEP:
case SSTOP:
entry->hrSWRunStatus = HRSWRUNSTATUS_NOTRUNNABLE;
break;
case SIDL:
case SZOMB:
default:
entry->hrSWRunStatus = HRSWRUNSTATUS_INVALID;
break;
}
/*
* check for system processes
*/
if (P_SYSTEM & processes[i].kp_proc.p_flag) {
entry->hrSWRunType = HRSWRUNTYPE_OPERATINGSYSTEM;
DEBUGMSGTL(("swrun:load:arch", SWRUNINDENT "SYSTEM\n"));
}
else entry->hrSWRunType = HRSWRUNTYPE_APPLICATION;
/*
* get mem size, run time
*/
rc = proc_pidinfo( processes[i].kp_proc.p_pid, PROC_PIDTASKALLINFO, 0,
&taskinfo, sizeof(taskinfo));
if (sizeof(taskinfo) != rc) {
DEBUGMSGTL(("swrun:load:arch", " proc_pidinfo returned %d\n", rc));
}
else {
uint64_t task_mem = taskinfo.ptinfo.pti_resident_size / 1024;
union {
u_quad_t uq; /* u_int64_t */
UnsignedWide uw; /* struct u_int32_t hi/lo */
} at, ns;
at.uq = taskinfo.ptinfo.pti_total_user +
taskinfo.ptinfo.pti_total_system;
ns = at;
ns.uq = ns.uq / 10000000LL; /* nano to deci */
if (task_mem > INT32_MAX) {
DEBUGMSGTL(("swrun:load:arch", SWRUNINDENT "mem overflow\n"));
task_mem = INT32_MAX;
}
if (ns.uq > INT32_MAX) {
DEBUGMSGTL(("swrun:load:arch", SWRUNINDENT "time overflow\n"));
ns.uq = INT32_MAX;
}
entry->hrSWRunPerfMem = task_mem;
entry->hrSWRunPerfCPU = ns.uq;
}
}
free(processes);
DEBUGMSGTL(("swrun:load:arch"," loaded %d entries\n",
(int)CONTAINER_SIZE(container)));
return 0;
}
/* ---------------------------------------------------------------------
* The following code was snagged from Darwin code, and the original
* file had the following licenses:
*/
/*
* Copyright (c) 2002-2004 Apple Computer, Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* The contents of this file constitute Original Code as defined in and
* are subject to the Apple Public Source License Version 1.1 (the
* "License"). You may not use this file except in compliance with the
* License. Please obtain a copy of the License at
* http://www.apple.com/publicsource and read it before using this file.
*
* This Original Code and all software distributed under the License are
* distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
* License for the specific language governing rights and limitations
* under the License.
*
* @APPLE_LICENSE_HEADER_END@
*/
#ifdef JAGUAR /* xxx configure test? */
static int
_set_command_name_jaguar(netsnmp_swrun_entry *entry)
{
int mib[3] = {CTL_KERN, KERN_PROCARGS, 0};
size_t procargssize, mib_size = sizeof(mib)/sizeof(mib[0]);
char *arg_end, *exec_path;
int *ip;
int len;
char *command_beg, *command;
char arg_buf[MAX_KERN_ARGMAX]; /* max to avoid kernel bug */
DEBUGMSGTL(("swrun:load:arch:_cn"," pid %d\n", entry->hrSWRunIndex));
mib[2] = entry->hrSWRunIndex;
memset(arg_buf, 0x0, sizeof(arg_buf));
procargssize = _kern_argmax;
if (sysctl(mib, mib_size, arg_buf, &procargssize, NULL, 0) == -1) {
snmp_log(LOG_ERR, "Error in PROCARGS sysctl() for %s: %s\n",
entry->hrSWRunName, strerror(errno));
entry->hrSWRunPath_len = 0;
return -1;
}
/* Set ip just above the end of arg_buf. */
arg_end = &arg_buf[procargssize];
ip = (int *)arg_end;
/*
* Skip the last 2 words, since the last is a 0 word, and
* the second to last may be as well, if there are no
* arguments.
*/
ip -= 3;
/* Iterate down the arguments until a 0 word is found. */
for (; *ip != 0; ip--) {
if (ip == (int *)arg_buf) {
DEBUGMSGTL(("swrun:load:arch:_cn"," unexpected toparg\n"));
return -1;
}
}
/* The saved exec_path is just above the 0 word. */
ip++;
exec_path = (char *)ip;
DEBUGMSGTL(("swrun:load:arch:_cn"," exec_path %s\n", exec_path));
len = strlen(exec_path);
strlcpy(entry->hrSWRunPath, exec_path, sizeof(entry->hrSWRunPath));
if (len > sizeof(entry->hrSWRunPath)-1) {
DEBUGMSGTL(("swrun:load:arch:_cn"," truncating long run path\n"));
entry->hrSWRunPath[sizeof(entry->hrSWRunPath)-2] = '$';
entry->hrSWRunPath_len = sizeof(entry->hrSWRunPath)-1;
DEBUGMSGTL(("swrun:load:arch:_cn"," exec_path %s\n",
entry->hrSWRunPath));
}
else
entry->hrSWRunPath_len = len;
/*
* Get the beginning of the first argument. It is word-aligned,
* so skip padding '\0' bytes.
*/
command_beg = exec_path + strlen(exec_path);
DEBUGMSGTL(("swrun:load:arch:_cn"," command_beg '%s'\n", command_beg));
for (; *command_beg == '\0'; command_beg++) {
if (command_beg >= arg_end)
return -1;
}
DEBUGMSGTL(("swrun:load:arch:_cn"," command_beg '%s'\n", command_beg));
/* Get the basename of command. */
command = command_beg + strlen(command_beg) + 1;
for (command--; command >= command_beg; command--) {
if (*command == '/')
break;
}
command++;
DEBUGMSGTL(("swrun:load:arch:_cn"," command '%s'\n", command));
/* Allocate space for the command and copy. */
DEBUGMSGTL(("swrun:load:arch:_cn",
SWRUNINDENT "kernel name %s\n", command));
if (strncmp(command, entry->hrSWRunName, sizeof(entry->hrSWRunName)-1)) {
strlcpy(entry->hrSWRunName, command, sizeof(entry->hrSWRunName));
entry->hrSWRunName_len = strlen(entry->hrSWRunName);
DEBUGMSGTL(("swrun:load:arch:_cn", "**"
SWRUNINDENT "updated name to %s\n", entry->hrSWRunName));
return 0;
}
/** no error, no change */
return 1;
}
#else
static int
_set_command_name(netsnmp_swrun_entry *entry)
{
int mib[3] = {CTL_KERN, 0, 0};
size_t procargssize, mib_size = sizeof(mib)/sizeof(mib[0]);
char *cp;
int len, nargs;
char *command_beg, *command, *exec_path, *argN;
char arg_buf[MAX_KERN_ARGMAX]; /* max to avoid kernel bug */
/*
* arguments
*/
mib[1] = KERN_PROCARGS2;
mib[2] = entry->hrSWRunIndex;
memset(arg_buf, 0x0, sizeof(arg_buf));
procargssize = _kern_argmax;
if (sysctl(mib, mib_size, arg_buf, &procargssize, NULL, 0) == -1) {
snmp_log(LOG_ERR, "Error in PROCARGS2 sysctl() for %s: %s\n",
entry->hrSWRunName, strerror(errno));
entry->hrSWRunPath_len = 0;
entry->hrSWRunParameters_len = 0;
return -1;
}
else {
memcpy(&nargs,arg_buf, sizeof(nargs));
}
exec_path = arg_buf + sizeof(nargs);
len = strlen(exec_path);
strlcpy(entry->hrSWRunPath, exec_path, sizeof(entry->hrSWRunPath));
if (len > sizeof(entry->hrSWRunPath)-1) {
DEBUGMSGTL(("swrun:load:arch:_cn"," truncating long run path\n"));
entry->hrSWRunPath[sizeof(entry->hrSWRunPath)-2] = '$';
entry->hrSWRunPath_len = sizeof(entry->hrSWRunPath)-1;
}
else
entry->hrSWRunPath_len = len;
/** Skip the saved exec_path. */
#if 0
cp = exec_path + len;
#else
for (cp = exec_path; cp < &arg_buf[procargssize]; cp++) {
if (*cp == '\0')
break; /* End of exec_path reached. */
}
if (cp != exec_path + len) {
DEBUGMSGTL(("swrun:load:arch:_cn", " OFF BY %d\n",
(int)((exec_path + len) - cp)));
netsnmp_assert( cp == exec_path + len );
}
#endif
if (cp == &arg_buf[procargssize]) {
DEBUGMSGTL(("swrun:load:arch:_cn"," unexpected end of buffer\n"));
return -1;
}
/** Skip trailing '\0' characters. */
for (; cp < &arg_buf[procargssize]; cp++) {
if (*cp != '\0')
break; /* Beginning of first argument reached. */
}
if (cp == &arg_buf[procargssize]) {
DEBUGMSGTL(("swrun:load:arch:_cn"," unexpected end of buffer\n"));
return -1;
}
command_beg = cp;
/*
* Make sure that the command is '\0'-terminated. This protects
* against malicious programs; under normal operation this never
* ends up being a problem..
*/
for (; cp < &arg_buf[procargssize]; cp++) {
if (*cp == '\0')
break; /* End of first argument reached. */
}
if (cp == &arg_buf[procargssize]) {
DEBUGMSGTL(("swrun:load:arch:_cn"," unexpected end of buffer\n"));
return -1;
}
command = cp;
--nargs;
/*
* save arguments
*/
while( nargs && cp < &arg_buf[procargssize] ) {
/** Skip trailing '\0' characters from prev arg. */
for (; (cp < &arg_buf[procargssize]) && (*cp == 0); cp++)
; /* noop */
if (cp == &arg_buf[procargssize])
continue; /* effectively a break */
/** save argN start */
argN = cp;
--nargs;
if (0 == nargs)
continue; /* effectively a break */
/** Skip to end of arg */
for (; (cp < &arg_buf[procargssize]) && (*cp != 0); cp++)
; /* noop */
if (cp == &arg_buf[procargssize])
continue; /* effectively a break */
/*
* check for overrun into env
*/
if ((*argN != '-') && strchr(argN,'=')) {
DEBUGMSGTL(("swrun:load:arch:_cn", " *** OVERRUN INTO ENV %d\n",nargs));
continue;
}
/*
* save arg
*/
if(entry->hrSWRunParameters_len < sizeof(entry->hrSWRunParameters)-1) {
strlcat(&entry->hrSWRunParameters[entry->hrSWRunParameters_len],
argN, sizeof(entry->hrSWRunParameters)-entry->hrSWRunParameters_len-1);
entry->hrSWRunParameters_len = strlen(entry->hrSWRunParameters);
if ((entry->hrSWRunParameters_len+2 < sizeof(entry->hrSWRunParameters)-1) && (0 != nargs)) {
/* add space between params */
entry->hrSWRunParameters[entry->hrSWRunParameters_len++] = ' ';
entry->hrSWRunParameters[entry->hrSWRunParameters_len] = 0;
} else {
DEBUGMSGTL(("swrun:load:arch:_cn"," truncating long arg list\n"));
entry->hrSWRunParameters[entry->hrSWRunParameters_len++] = '$';
entry->hrSWRunParameters[entry->hrSWRunParameters_len] = '0';
}
}
}
if (' ' == entry->hrSWRunParameters[entry->hrSWRunParameters_len])
entry->hrSWRunParameters[entry->hrSWRunParameters_len--] = 0;
/* Get the basename of command. */
for (command--; command >= command_beg; command--) {
if (*command == '/')
break;
}
command++;
/* Allocate space for the command and copy. */
if (strncmp(command, entry->hrSWRunName, sizeof(entry->hrSWRunName)-1)) {
strlcpy(entry->hrSWRunName, command, sizeof(entry->hrSWRunName));
entry->hrSWRunName_len = strlen(entry->hrSWRunName);
DEBUGMSGTL(("swrun:load:arch:_cn",
" **updated name to %s\n", entry->hrSWRunName));
}
return 0;
}
#endif