| 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 |
|
* |
| 45 |
|
#endif |
| 46 |
|
|
| 47 |
|
#include "tools.h" |
| 48 |
+ |
#include "statgrab.h" |
| 49 |
|
|
| 50 |
|
#ifdef SOLARIS |
| 51 |
|
#ifdef HAVE_LIBDEVINFO_H |
| 91 |
|
mapping_t *map_end_ptr; |
| 92 |
|
|
| 93 |
|
if (mapping == NULL){ |
| 94 |
< |
mapping = malloc(sizeof(mapping_t)); |
| 94 |
> |
mapping = sg_malloc(sizeof(mapping_t)); |
| 95 |
|
if (mapping == NULL) return; |
| 96 |
|
map_ptr = mapping; |
| 97 |
|
}else{ |
| 106 |
|
/* We've reached end of list and not found the entry.. So we need to malloc |
| 107 |
|
* new mapping_t |
| 108 |
|
*/ |
| 109 |
< |
map_end_ptr->next = malloc(sizeof(mapping_t)); |
| 109 |
> |
map_end_ptr->next = sg_malloc(sizeof(mapping_t)); |
| 110 |
|
if (map_end_ptr->next == NULL) return; |
| 111 |
|
map_ptr = map_end_ptr->next; |
| 112 |
|
} |
| 113 |
|
|
| 114 |
|
map_ptr->next = NULL; |
| 115 |
< |
map_ptr->bsd = strdup(bsd); |
| 116 |
< |
map_ptr->svr = strdup(svr); |
| 115 |
> |
map_ptr->bsd = NULL; |
| 116 |
> |
map_ptr->svr = NULL; |
| 117 |
> |
if (sg_update_string(&map_ptr->bsd, bsd) < 0 |
| 118 |
> |
|| sg_update_string(&map_ptr->svr, svr) < 0) { |
| 119 |
> |
return; |
| 120 |
> |
} |
| 121 |
|
|
| 122 |
|
return; |
| 123 |
|
} |
| 127 |
|
DIR *dirp; |
| 128 |
|
struct dirent *dp; |
| 129 |
|
struct stat stbuf; |
| 130 |
< |
char *svr_name; |
| 130 |
> |
char *svr_name = NULL; |
| 131 |
|
char current_dir[MAXPATHLEN]; |
| 132 |
|
char file_name[MAXPATHLEN]; |
| 133 |
|
char temp_name[MAXPATHLEN]; |
| 154 |
|
x = readlink(dir_dname, file_name, sizeof(file_name)); |
| 155 |
|
file_name[x] = '\0'; |
| 156 |
|
if (strcmp(file_name, temp_name) == 0) { |
| 157 |
< |
svr_name = strdup(dp->d_name); |
| 157 |
> |
if (sg_update_string(&svr_name, |
| 158 |
> |
dp->d_name) < 0) { |
| 159 |
> |
return NULL; |
| 160 |
> |
} |
| 161 |
|
closedir(dirp); |
| 162 |
|
return svr_name; |
| 163 |
|
} |
| 179 |
|
char *value; |
| 180 |
|
int instance; |
| 181 |
|
if ((root_node = di_init("/", DINFOCPYALL)) == DI_NODE_NIL) { |
| 182 |
< |
return 1; |
| 182 |
> |
return -1; |
| 183 |
|
} |
| 184 |
|
node = di_drv_first_node(alias, root_node); |
| 185 |
|
while (node != DI_NODE_NIL) { |
| 221 |
|
int found; |
| 222 |
|
|
| 223 |
|
if ((kc = kstat_open()) == NULL) { |
| 224 |
< |
return 1; |
| 224 |
> |
return -1; |
| 225 |
|
} |
| 226 |
|
|
| 227 |
|
for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) { |
| 243 |
|
if (x>=BIG_ENOUGH){ |
| 244 |
|
/* We've got bigger than we thought was massive */ |
| 245 |
|
/* If we hit this.. Make big enough bigger */ |
| 246 |
< |
return 1; |
| 246 |
> |
return -1; |
| 247 |
|
} |
| 248 |
|
if( !strncmp(driver_list[x], device_name, BIG_ENOUGH)){ |
| 249 |
|
found = 1; |
| 253 |
|
|
| 254 |
|
if(!found){ |
| 255 |
|
if((get_alias(device_name)) != 0){ |
| 256 |
< |
return 1; |
| 256 |
> |
return -1; |
| 257 |
|
} |
| 258 |
|
strncpy(driver_list[x], device_name, BIG_ENOUGH); |
| 259 |
|
list_entries++; |
| 283 |
|
|
| 284 |
|
char *sg_get_string_match(char *line, regmatch_t *match){ |
| 285 |
|
int len=match->rm_eo - match->rm_so; |
| 286 |
< |
char *match_string=malloc(len+1); |
| 286 |
> |
char *match_string=sg_malloc(len+1); |
| 287 |
|
|
| 288 |
|
match_string=strncpy(match_string, line+match->rm_so, len); |
| 289 |
|
match_string[len]='\0'; |
| 337 |
|
* Returns strlen(src); if retval >= siz, truncation occurred. |
| 338 |
|
*/ |
| 339 |
|
size_t sg_strlcpy(char *dst, const char *src, size_t siz){ |
| 340 |
< |
register char *d = dst; |
| 341 |
< |
register const char *s = src; |
| 342 |
< |
register size_t n = siz; |
| 340 |
> |
register char *d = dst; |
| 341 |
> |
register const char *s = src; |
| 342 |
> |
register size_t n = siz; |
| 343 |
|
|
| 344 |
< |
/* Copy as many bytes as will fit */ |
| 345 |
< |
if (n != 0 && --n != 0) { |
| 346 |
< |
do { |
| 347 |
< |
if ((*d++ = *s++) == 0) |
| 348 |
< |
break; |
| 349 |
< |
} while (--n != 0); |
| 350 |
< |
} |
| 344 |
> |
/* Copy as many bytes as will fit */ |
| 345 |
> |
if (n != 0 && --n != 0) { |
| 346 |
> |
do { |
| 347 |
> |
if ((*d++ = *s++) == 0) |
| 348 |
> |
break; |
| 349 |
> |
} while (--n != 0); |
| 350 |
> |
} |
| 351 |
|
|
| 352 |
< |
/* Not enough room in dst, add NUL and traverse rest of src */ |
| 353 |
< |
if (n == 0) { |
| 354 |
< |
if (siz != 0) |
| 355 |
< |
*d = '\0'; /* NUL-terminate dst */ |
| 356 |
< |
while (*s++) |
| 357 |
< |
; |
| 358 |
< |
} |
| 352 |
> |
/* Not enough room in dst, add NUL and traverse rest of src */ |
| 353 |
> |
if (n == 0) { |
| 354 |
> |
if (siz != 0) |
| 355 |
> |
*d = '\0'; /* NUL-terminate dst */ |
| 356 |
> |
while (*s++) |
| 357 |
> |
; |
| 358 |
> |
} |
| 359 |
|
|
| 360 |
< |
return(s - src - 1); /* count does not include NUL */ |
| 360 |
> |
return(s - src - 1); /* count does not include NUL */ |
| 361 |
|
} |
| 362 |
|
|
| 363 |
|
/* $OpenBSD: strlcat.c,v 1.11 2003/06/17 21:56:24 millert Exp $ */ |
| 386 |
|
* If retval >= siz, truncation occurred. |
| 387 |
|
*/ |
| 388 |
|
size_t sg_strlcat(char *dst, const char *src, size_t siz){ |
| 389 |
< |
register char *d = dst; |
| 390 |
< |
register const char *s = src; |
| 391 |
< |
register size_t n = siz; |
| 392 |
< |
size_t dlen; |
| 389 |
> |
register char *d = dst; |
| 390 |
> |
register const char *s = src; |
| 391 |
> |
register size_t n = siz; |
| 392 |
> |
size_t dlen; |
| 393 |
|
|
| 394 |
< |
/* Find the end of dst and adjust bytes left but don't go past end */ |
| 395 |
< |
while (n-- != 0 && *d != '\0') |
| 396 |
< |
d++; |
| 397 |
< |
dlen = d - dst; |
| 398 |
< |
n = siz - dlen; |
| 394 |
> |
/* Find the end of dst and adjust bytes left but don't go past end */ |
| 395 |
> |
while (n-- != 0 && *d != '\0') |
| 396 |
> |
d++; |
| 397 |
> |
dlen = d - dst; |
| 398 |
> |
n = siz - dlen; |
| 399 |
|
|
| 400 |
< |
if (n == 0) |
| 401 |
< |
return(dlen + strlen(s)); |
| 402 |
< |
while (*s != '\0') { |
| 403 |
< |
if (n != 1) { |
| 404 |
< |
*d++ = *s; |
| 405 |
< |
n--; |
| 406 |
< |
} |
| 407 |
< |
s++; |
| 408 |
< |
} |
| 409 |
< |
*d = '\0'; |
| 400 |
> |
if (n == 0) |
| 401 |
> |
return(dlen + strlen(s)); |
| 402 |
> |
while (*s != '\0') { |
| 403 |
> |
if (n != 1) { |
| 404 |
> |
*d++ = *s; |
| 405 |
> |
n--; |
| 406 |
> |
} |
| 407 |
> |
s++; |
| 408 |
> |
} |
| 409 |
> |
*d = '\0'; |
| 410 |
|
|
| 411 |
< |
return(dlen + (s - src)); /* count does not include NUL */ |
| 411 |
> |
return(dlen + (s - src)); /* count does not include NUL */ |
| 412 |
|
} |
| 413 |
|
|
| 414 |
< |
char *sg_update_string(char **dest, const char *src) { |
| 414 |
> |
int sg_update_string(char **dest, const char *src) { |
| 415 |
|
char *new; |
| 416 |
|
|
| 417 |
< |
new = realloc(*dest, strlen(src) + 1); |
| 417 |
> |
if (src == NULL) { |
| 418 |
> |
/* We're being told to set it to NULL. */ |
| 419 |
> |
free(*dest); |
| 420 |
> |
*dest = NULL; |
| 421 |
> |
return 0; |
| 422 |
> |
} |
| 423 |
> |
|
| 424 |
> |
new = sg_realloc(*dest, strlen(src) + 1); |
| 425 |
|
if (new == NULL) { |
| 426 |
< |
return NULL; |
| 426 |
> |
return -1; |
| 427 |
|
} |
| 428 |
|
|
| 429 |
|
strcpy(new, src); |
| 430 |
|
*dest = new; |
| 431 |
< |
return new; |
| 431 |
> |
return 0; |
| 432 |
|
} |
| 433 |
|
|
| 434 |
|
long long sg_get_ll_match(char *line, regmatch_t *match){ |
| 469 |
|
#if defined(NETBSD) || defined(OPENBSD) |
| 470 |
|
struct uvmexp *sg_get_uvmexp() { |
| 471 |
|
int mib[2]; |
| 472 |
< |
size_t size; |
| 473 |
< |
static struct uvmexp *uvm = NULL; |
| 472 |
> |
size_t size = sizeof(struct uvmexp); |
| 473 |
> |
static struct uvmexp uvm; |
| 474 |
|
struct uvmexp *new; |
| 475 |
|
|
| 476 |
|
mib[0] = CTL_VM; |
| 477 |
|
mib[1] = VM_UVMEXP; |
| 478 |
|
|
| 479 |
< |
if (sysctl(mib, 2, NULL, &size, NULL, 0) < 0) { |
| 479 |
> |
if (sysctl(mib, 2, &uvm, &size, NULL, 0) < 0) { |
| 480 |
|
return NULL; |
| 481 |
|
} |
| 482 |
|
|
| 483 |
< |
new = realloc(uvm, size); |
| 469 |
< |
if (new == NULL) { |
| 470 |
< |
return NULL; |
| 471 |
< |
} |
| 472 |
< |
uvm = new; |
| 473 |
< |
|
| 474 |
< |
if (sysctl(mib, 2, uvm, &size, NULL, 0) < 0) { |
| 475 |
< |
return NULL; |
| 476 |
< |
} |
| 477 |
< |
|
| 478 |
< |
return uvm; |
| 483 |
> |
return &uvm; |
| 484 |
|
} |
| 485 |
|
#endif |
| 486 |
|
|
| 487 |
|
int sg_init(){ |
| 488 |
|
#if (defined(FREEBSD) && !defined(FREEBSD5)) || defined(DFBSD) |
| 489 |
|
if (sg_get_kvm() == NULL) { |
| 490 |
< |
return 1; |
| 490 |
> |
return -1; |
| 491 |
|
} |
| 492 |
|
if (sg_get_kvm2() == NULL) { |
| 493 |
< |
return 1; |
| 493 |
> |
return -1; |
| 494 |
|
} |
| 495 |
|
#endif |
| 491 |
– |
#if defined(NETBSD) || defined(OPENBSD) |
| 492 |
– |
/* This should always succeed, but it seems that on some |
| 493 |
– |
* versions of NetBSD the first call to get_uvmexp will return |
| 494 |
– |
* a non-filled-in structure; this is a workaround for that. |
| 495 |
– |
*/ |
| 496 |
– |
if (sg_get_uvmexp() == NULL) return 1; |
| 497 |
– |
#endif |
| 496 |
|
#ifdef SOLARIS |
| 497 |
|
/* On solaris 7, this will fail if you are not root. But, everything |
| 498 |
|
* will still work, just no disk mappings. So we will ignore the exit |
| 509 |
|
if (setegid(getgid()) != 0) return -1; |
| 510 |
|
if (seteuid(getuid()) != 0) return -1; |
| 511 |
|
return 0; |
| 512 |
+ |
} |
| 513 |
+ |
|
| 514 |
+ |
void *sg_realloc(void *ptr, size_t size) { |
| 515 |
+ |
void *tmp = NULL; |
| 516 |
+ |
tmp = realloc(ptr, size); |
| 517 |
+ |
if(tmp == NULL) { |
| 518 |
+ |
sg_set_error(SG_ERROR_MALLOC, NULL); |
| 519 |
+ |
} |
| 520 |
+ |
return tmp; |
| 521 |
|
} |
| 522 |
|
|