1 |
|
/* |
2 |
< |
* i-scream central monitoring system |
2 |
> |
* i-scream libstatgrab |
3 |
|
* http://www.i-scream.org |
4 |
|
* Copyright (C) 2000-2004 i-scream |
5 |
|
* |
30 |
|
#include <stdlib.h> |
31 |
|
#include <unistd.h> |
32 |
|
#include <sys/types.h> |
33 |
– |
#include <regex.h> |
33 |
|
#ifdef ALLBSD |
34 |
|
#include <fcntl.h> |
35 |
|
#endif |
36 |
< |
#if defined(FREEBSD) || defined(DFBSD) |
36 |
> |
#if (defined(FREEBSD) && !defined(FREEBSD5)) || defined(DFBSD) |
37 |
|
#include <kvm.h> |
38 |
+ |
#include <paths.h> |
39 |
|
#endif |
40 |
|
#if defined(NETBSD) || defined(OPENBSD) |
41 |
|
#include <uvm/uvm_extern.h> |
44 |
|
#endif |
45 |
|
|
46 |
|
#include "tools.h" |
47 |
+ |
#include "statgrab.h" |
48 |
|
|
49 |
|
#ifdef SOLARIS |
50 |
|
#ifdef HAVE_LIBDEVINFO_H |
74 |
|
#endif |
75 |
|
|
76 |
|
#ifdef SOLARIS |
77 |
< |
char *get_svr_from_bsd(char *bsd){ |
77 |
> |
const char *sg_get_svr_from_bsd(const char *bsd){ |
78 |
|
#ifdef HAVE_LIBDEVINFO_H |
79 |
|
mapping_t *map_ptr; |
80 |
|
for(map_ptr = mapping; map_ptr != NULL; map_ptr = map_ptr->next) |
85 |
|
#endif |
86 |
|
|
87 |
|
#if defined(SOLARIS) && defined(HAVE_LIBDEVINFO_H) |
88 |
< |
void add_mapping(char *bsd, char *svr){ |
88 |
> |
static void add_mapping(char *bsd, char *svr){ |
89 |
|
mapping_t *map_ptr; |
90 |
|
mapping_t *map_end_ptr; |
91 |
|
|
91 |
– |
bsd = strdup(bsd); |
92 |
– |
svr = strdup(svr); |
93 |
– |
|
92 |
|
if (mapping == NULL){ |
93 |
< |
mapping = malloc(sizeof(mapping_t)); |
93 |
> |
mapping = sg_malloc(sizeof(mapping_t)); |
94 |
|
if (mapping == NULL) return; |
95 |
|
map_ptr = mapping; |
96 |
|
}else{ |
105 |
|
/* We've reached end of list and not found the entry.. So we need to malloc |
106 |
|
* new mapping_t |
107 |
|
*/ |
108 |
< |
map_end_ptr->next = malloc(sizeof(mapping_t)); |
108 |
> |
map_end_ptr->next = sg_malloc(sizeof(mapping_t)); |
109 |
|
if (map_end_ptr->next == NULL) return; |
110 |
|
map_ptr = map_end_ptr->next; |
111 |
|
} |
112 |
|
|
113 |
|
map_ptr->next = NULL; |
114 |
< |
map_ptr->bsd = bsd; |
115 |
< |
map_ptr->svr = svr; |
114 |
> |
map_ptr->bsd = NULL; |
115 |
> |
map_ptr->svr = NULL; |
116 |
> |
if (sg_update_string(&map_ptr->bsd, bsd) < 0 |
117 |
> |
|| sg_update_string(&map_ptr->svr, svr) < 0) { |
118 |
> |
return; |
119 |
> |
} |
120 |
|
|
121 |
|
return; |
122 |
|
} |
123 |
|
|
124 |
< |
char *read_dir(char *disk_path){ |
124 |
> |
|
125 |
> |
static char *read_dir(char *disk_path){ |
126 |
|
DIR *dirp; |
127 |
|
struct dirent *dp; |
128 |
|
struct stat stbuf; |
129 |
< |
char *svr_name; |
129 |
> |
char *svr_name = NULL; |
130 |
|
char current_dir[MAXPATHLEN]; |
131 |
|
char file_name[MAXPATHLEN]; |
132 |
|
char temp_name[MAXPATHLEN]; |
153 |
|
x = readlink(dir_dname, file_name, sizeof(file_name)); |
154 |
|
file_name[x] = '\0'; |
155 |
|
if (strcmp(file_name, temp_name) == 0) { |
156 |
< |
svr_name = strdup(dp->d_name); |
156 |
> |
if (sg_update_string(&svr_name, |
157 |
> |
dp->d_name) < 0) { |
158 |
> |
return NULL; |
159 |
> |
} |
160 |
|
closedir(dirp); |
161 |
|
return svr_name; |
162 |
|
} |
167 |
|
} |
168 |
|
|
169 |
|
|
170 |
< |
|
165 |
< |
int get_alias(char *alias){ |
170 |
> |
static int get_alias(char *alias){ |
171 |
|
char file[MAXPATHLEN]; |
172 |
|
di_node_t root_node; |
173 |
|
di_node_t node; |
178 |
|
char *value; |
179 |
|
int instance; |
180 |
|
if ((root_node = di_init("/", DINFOCPYALL)) == DI_NODE_NIL) { |
181 |
< |
return 1; |
181 |
> |
return -1; |
182 |
|
} |
183 |
|
node = di_drv_first_node(alias, root_node); |
184 |
|
while (node != DI_NODE_NIL) { |
188 |
|
minor_name = di_minor_name(minor); |
189 |
|
strcpy(tmpnode, alias); |
190 |
|
sprintf(tmpnode, "%s%d", tmpnode, instance); |
191 |
< |
strlcpy(file, "/devices", sizeof file); |
192 |
< |
strlcat(file, phys_path, sizeof file); |
193 |
< |
strlcat(file, ":", sizeof file); |
194 |
< |
strlcat(file, minor_name, sizeof file); |
191 |
> |
sg_strlcpy(file, "/devices", sizeof file); |
192 |
> |
sg_strlcat(file, phys_path, sizeof file); |
193 |
> |
sg_strlcat(file, ":", sizeof file); |
194 |
> |
sg_strlcat(file, minor_name, sizeof file); |
195 |
|
value = read_dir(file); |
196 |
|
if (value != NULL){ |
197 |
|
add_mapping(tmpnode, value); |
208 |
|
|
209 |
|
|
210 |
|
#define BIG_ENOUGH 512 |
211 |
< |
int build_mapping(){ |
211 |
> |
static int build_mapping(){ |
212 |
|
char device_name[BIG_ENOUGH]; |
213 |
|
int x; |
214 |
|
kstat_ctl_t *kc; |
220 |
|
int found; |
221 |
|
|
222 |
|
if ((kc = kstat_open()) == NULL) { |
223 |
< |
return; |
223 |
> |
return -1; |
224 |
|
} |
225 |
|
|
226 |
|
for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) { |
242 |
|
if (x>=BIG_ENOUGH){ |
243 |
|
/* We've got bigger than we thought was massive */ |
244 |
|
/* If we hit this.. Make big enough bigger */ |
245 |
< |
return 1; |
245 |
> |
return -1; |
246 |
|
} |
247 |
|
if( !strncmp(driver_list[x], device_name, BIG_ENOUGH)){ |
248 |
|
found = 1; |
252 |
|
|
253 |
|
if(!found){ |
254 |
|
if((get_alias(device_name)) != 0){ |
255 |
< |
return 1; |
255 |
> |
return -1; |
256 |
|
} |
257 |
|
strncpy(driver_list[x], device_name, BIG_ENOUGH); |
258 |
|
list_entries++; |
265 |
|
|
266 |
|
#endif |
267 |
|
|
268 |
< |
|
269 |
< |
|
265 |
< |
char *f_read_line(FILE *f, const char *string){ |
268 |
> |
#if defined(LINUX) || defined(CYGWIN) |
269 |
> |
char *sg_f_read_line(FILE *f, const char *string){ |
270 |
|
/* Max line length. 8k should be more than enough */ |
271 |
|
static char line[8192]; |
272 |
|
|
276 |
|
} |
277 |
|
} |
278 |
|
|
279 |
+ |
sg_set_error(SG_ERROR_PARSE, NULL); |
280 |
|
return NULL; |
281 |
|
} |
282 |
|
|
283 |
< |
char *get_string_match(char *line, regmatch_t *match){ |
283 |
> |
char *sg_get_string_match(char *line, regmatch_t *match){ |
284 |
|
int len=match->rm_eo - match->rm_so; |
285 |
< |
char *match_string=malloc(len+1); |
285 |
> |
char *match_string=sg_malloc(len+1); |
286 |
|
|
287 |
|
match_string=strncpy(match_string, line+match->rm_so, len); |
288 |
|
match_string[len]='\0'; |
290 |
|
return match_string; |
291 |
|
} |
292 |
|
|
293 |
+ |
long long sg_get_ll_match(char *line, regmatch_t *match){ |
294 |
+ |
char *ptr; |
295 |
+ |
long long num; |
296 |
|
|
297 |
+ |
ptr=line+match->rm_so; |
298 |
+ |
num=atoll(ptr); |
299 |
|
|
300 |
+ |
return num; |
301 |
+ |
} |
302 |
+ |
#endif |
303 |
+ |
|
304 |
|
#ifndef HAVE_ATOLL |
305 |
|
static long long atoll(const char *s) { |
306 |
|
long long value = 0; |
321 |
|
} |
322 |
|
#endif |
323 |
|
|
310 |
– |
#ifndef HAVE_STRLCPY |
324 |
|
/* $OpenBSD: strlcpy.c,v 1.8 2003/06/17 21:56:24 millert Exp $ */ |
325 |
|
|
326 |
|
/* |
344 |
|
* will be copied. Always NUL terminates (unless siz == 0). |
345 |
|
* Returns strlen(src); if retval >= siz, truncation occurred. |
346 |
|
*/ |
347 |
< |
size_t strlcpy(char *dst, const char *src, size_t siz){ |
348 |
< |
register char *d = dst; |
349 |
< |
register const char *s = src; |
350 |
< |
register size_t n = siz; |
347 |
> |
size_t sg_strlcpy(char *dst, const char *src, size_t siz){ |
348 |
> |
register char *d = dst; |
349 |
> |
register const char *s = src; |
350 |
> |
register size_t n = siz; |
351 |
|
|
352 |
< |
/* Copy as many bytes as will fit */ |
353 |
< |
if (n != 0 && --n != 0) { |
354 |
< |
do { |
355 |
< |
if ((*d++ = *s++) == 0) |
356 |
< |
break; |
357 |
< |
} while (--n != 0); |
358 |
< |
} |
352 |
> |
/* Copy as many bytes as will fit */ |
353 |
> |
if (n != 0 && --n != 0) { |
354 |
> |
do { |
355 |
> |
if ((*d++ = *s++) == 0) |
356 |
> |
break; |
357 |
> |
} while (--n != 0); |
358 |
> |
} |
359 |
|
|
360 |
< |
/* Not enough room in dst, add NUL and traverse rest of src */ |
361 |
< |
if (n == 0) { |
362 |
< |
if (siz != 0) |
363 |
< |
*d = '\0'; /* NUL-terminate dst */ |
364 |
< |
while (*s++) |
365 |
< |
; |
366 |
< |
} |
360 |
> |
/* Not enough room in dst, add NUL and traverse rest of src */ |
361 |
> |
if (n == 0) { |
362 |
> |
if (siz != 0) |
363 |
> |
*d = '\0'; /* NUL-terminate dst */ |
364 |
> |
while (*s++) |
365 |
> |
; |
366 |
> |
} |
367 |
|
|
368 |
< |
return(s - src - 1); /* count does not include NUL */ |
368 |
> |
return(s - src - 1); /* count does not include NUL */ |
369 |
|
} |
357 |
– |
#endif |
370 |
|
|
359 |
– |
#ifndef HAVE_STRLCAT |
371 |
|
/* $OpenBSD: strlcat.c,v 1.11 2003/06/17 21:56:24 millert Exp $ */ |
372 |
|
|
373 |
|
/* |
393 |
|
* Returns strlen(src) + MIN(siz, strlen(initial dst)). |
394 |
|
* If retval >= siz, truncation occurred. |
395 |
|
*/ |
396 |
< |
size_t strlcat(char *dst, const char *src, size_t siz){ |
397 |
< |
register char *d = dst; |
398 |
< |
register const char *s = src; |
399 |
< |
register size_t n = siz; |
400 |
< |
size_t dlen; |
396 |
> |
size_t sg_strlcat(char *dst, const char *src, size_t siz){ |
397 |
> |
register char *d = dst; |
398 |
> |
register const char *s = src; |
399 |
> |
register size_t n = siz; |
400 |
> |
size_t dlen; |
401 |
|
|
402 |
< |
/* Find the end of dst and adjust bytes left but don't go past end */ |
403 |
< |
while (n-- != 0 && *d != '\0') |
404 |
< |
d++; |
405 |
< |
dlen = d - dst; |
406 |
< |
n = siz - dlen; |
402 |
> |
/* Find the end of dst and adjust bytes left but don't go past end */ |
403 |
> |
while (n-- != 0 && *d != '\0') |
404 |
> |
d++; |
405 |
> |
dlen = d - dst; |
406 |
> |
n = siz - dlen; |
407 |
|
|
408 |
< |
if (n == 0) |
409 |
< |
return(dlen + strlen(s)); |
410 |
< |
while (*s != '\0') { |
411 |
< |
if (n != 1) { |
412 |
< |
*d++ = *s; |
413 |
< |
n--; |
414 |
< |
} |
415 |
< |
s++; |
416 |
< |
} |
417 |
< |
*d = '\0'; |
408 |
> |
if (n == 0) |
409 |
> |
return(dlen + strlen(s)); |
410 |
> |
while (*s != '\0') { |
411 |
> |
if (n != 1) { |
412 |
> |
*d++ = *s; |
413 |
> |
n--; |
414 |
> |
} |
415 |
> |
s++; |
416 |
> |
} |
417 |
> |
*d = '\0'; |
418 |
|
|
419 |
< |
return(dlen + (s - src)); /* count does not include NUL */ |
419 |
> |
return(dlen + (s - src)); /* count does not include NUL */ |
420 |
|
} |
421 |
|
|
422 |
< |
#endif |
422 |
> |
int sg_update_string(char **dest, const char *src) { |
423 |
> |
char *new; |
424 |
|
|
425 |
< |
long long get_ll_match(char *line, regmatch_t *match){ |
426 |
< |
char *ptr; |
427 |
< |
long long num; |
425 |
> |
if (src == NULL) { |
426 |
> |
/* We're being told to set it to NULL. */ |
427 |
> |
free(*dest); |
428 |
> |
*dest = NULL; |
429 |
> |
return 0; |
430 |
> |
} |
431 |
|
|
432 |
< |
ptr=line+match->rm_so; |
433 |
< |
num=atoll(ptr); |
432 |
> |
new = sg_realloc(*dest, strlen(src) + 1); |
433 |
> |
if (new == NULL) { |
434 |
> |
return -1; |
435 |
> |
} |
436 |
|
|
437 |
< |
return num; |
437 |
> |
strcpy(new, src); |
438 |
> |
*dest = new; |
439 |
> |
return 0; |
440 |
|
} |
441 |
|
|
442 |
< |
#if defined(FREEBSD) || defined(DFBSD) |
443 |
< |
kvm_t *get_kvm() { |
442 |
> |
#if (defined(FREEBSD) && !defined(FREEBSD5)) || defined(DFBSD) |
443 |
> |
kvm_t *sg_get_kvm() { |
444 |
|
static kvm_t *kvmd = NULL; |
445 |
|
|
446 |
|
if (kvmd != NULL) { |
448 |
|
} |
449 |
|
|
450 |
|
kvmd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, NULL); |
451 |
+ |
if(kvmd == NULL) { |
452 |
+ |
sg_set_error(SG_ERROR_KVM_OPENFILES, NULL); |
453 |
+ |
} |
454 |
|
return kvmd; |
455 |
|
} |
456 |
+ |
|
457 |
+ |
/* Can't think of a better name for this function */ |
458 |
+ |
kvm_t *sg_get_kvm2() { |
459 |
+ |
static kvm_t *kvmd2 = NULL; |
460 |
+ |
|
461 |
+ |
if (kvmd2 != NULL) { |
462 |
+ |
return kvmd2; |
463 |
+ |
} |
464 |
+ |
|
465 |
+ |
kvmd2 = kvm_openfiles(_PATH_DEVNULL, _PATH_DEVNULL, NULL, O_RDONLY, NULL); |
466 |
+ |
if(kvmd2 == NULL) { |
467 |
+ |
sg_set_error(SG_ERROR_KVM_OPENFILES, NULL); |
468 |
+ |
} |
469 |
+ |
return kvmd2; |
470 |
+ |
} |
471 |
|
#endif |
472 |
|
|
473 |
|
#if defined(NETBSD) || defined(OPENBSD) |
474 |
< |
struct uvmexp *get_uvmexp() { |
474 |
> |
struct uvmexp *sg_get_uvmexp() { |
475 |
|
int mib[2]; |
476 |
< |
size_t size; |
477 |
< |
static struct uvmexp *uvm = NULL; |
476 |
> |
size_t size = sizeof(struct uvmexp); |
477 |
> |
static struct uvmexp uvm; |
478 |
> |
struct uvmexp *new; |
479 |
|
|
480 |
|
mib[0] = CTL_VM; |
481 |
|
mib[1] = VM_UVMEXP; |
482 |
|
|
483 |
< |
if (sysctl(mib, 2, NULL, &size, NULL, 0) < 0) { |
483 |
> |
if (sysctl(mib, 2, &uvm, &size, NULL, 0) < 0) { |
484 |
> |
sg_set_error_with_errno(SG_ERROR_SYSCTL, "CTL_VM.VM_UVMEXP"); |
485 |
|
return NULL; |
486 |
|
} |
487 |
|
|
488 |
< |
uvm = realloc(uvm, size); |
450 |
< |
if (uvm == NULL) { |
451 |
< |
return NULL; |
452 |
< |
} |
453 |
< |
|
454 |
< |
if (sysctl(mib, 2, uvm, &size, NULL, 0) < 0) { |
455 |
< |
return NULL; |
456 |
< |
} |
457 |
< |
|
458 |
< |
return uvm; |
488 |
> |
return &uvm; |
489 |
|
} |
490 |
|
#endif |
491 |
|
|
492 |
< |
int statgrab_init(){ |
493 |
< |
#if defined(FREEBSD) || defined(DFBSD) |
494 |
< |
{ |
495 |
< |
kvm_t *kvmd = get_kvm(); |
496 |
< |
if (kvmd == NULL) return 1; |
492 |
> |
int sg_init(){ |
493 |
> |
sg_set_error(SG_ERROR_NONE, NULL); |
494 |
> |
|
495 |
> |
#if (defined(FREEBSD) && !defined(FREEBSD5)) || defined(DFBSD) |
496 |
> |
if (sg_get_kvm() == NULL) { |
497 |
> |
return -1; |
498 |
|
} |
499 |
+ |
if (sg_get_kvm2() == NULL) { |
500 |
+ |
return -1; |
501 |
+ |
} |
502 |
|
#endif |
503 |
|
#ifdef SOLARIS |
504 |
|
/* On solaris 7, this will fail if you are not root. But, everything |
512 |
|
return 0; |
513 |
|
} |
514 |
|
|
515 |
< |
int statgrab_drop_privileges() { |
516 |
< |
if (setegid(getgid()) != 0) return -1; |
517 |
< |
if (seteuid(getuid()) != 0) return -1; |
515 |
> |
int sg_drop_privileges() { |
516 |
> |
#ifdef HAVE_SETEGID |
517 |
> |
if (setegid(getgid()) != 0) { |
518 |
> |
#elif defined(HAVE_SETRESGID) |
519 |
> |
if (setresgid(getgid(), getgid(), getgid()) != 0) { |
520 |
> |
#else |
521 |
> |
{ |
522 |
> |
#endif |
523 |
> |
sg_set_error_with_errno(SG_ERROR_SETEGID, NULL); |
524 |
> |
return -1; |
525 |
> |
} |
526 |
> |
#ifdef HAVE_SETEUID |
527 |
> |
if (seteuid(getuid()) != 0) { |
528 |
> |
#elif defined(HAVE_SETRESUID) |
529 |
> |
if (setresuid(getuid(), getuid(), getuid()) != 0) { |
530 |
> |
#else |
531 |
> |
{ |
532 |
> |
#endif |
533 |
> |
sg_set_error_with_errno(SG_ERROR_SETEUID, NULL); |
534 |
> |
return -1; |
535 |
> |
} |
536 |
|
return 0; |
537 |
|
} |
538 |
|
|
539 |
+ |
void *sg_realloc(void *ptr, size_t size) { |
540 |
+ |
void *tmp = NULL; |
541 |
+ |
tmp = realloc(ptr, size); |
542 |
+ |
if(tmp == NULL) { |
543 |
+ |
sg_set_error_with_errno(SG_ERROR_MALLOC, NULL); |
544 |
+ |
} |
545 |
+ |
return tmp; |
546 |
+ |
} |
547 |
+ |
|
548 |
+ |
/* If we don't have a GNU compatible realloc, fake it. */ |
549 |
+ |
#if HAVE_REALLOC == 0 |
550 |
+ |
void *rpl_realloc(void *ptr, size_t size) { |
551 |
+ |
if (ptr == NULL && size == 0) { |
552 |
+ |
return NULL; |
553 |
+ |
} |
554 |
+ |
|
555 |
+ |
if (size == 0) { |
556 |
+ |
free(ptr); |
557 |
+ |
return NULL; |
558 |
+ |
} |
559 |
+ |
|
560 |
+ |
if (ptr == NULL) { |
561 |
+ |
return malloc(size); |
562 |
+ |
} |
563 |
+ |
|
564 |
+ |
#undef realloc |
565 |
+ |
return realloc(ptr, size); |
566 |
+ |
#define realloc rpl_realloc |
567 |
+ |
} |
568 |
+ |
#endif |