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 |
75 |
|
#endif |
76 |
|
|
77 |
|
#ifdef SOLARIS |
78 |
< |
char *get_svr_from_bsd(char *bsd){ |
78 |
> |
const char *sg_get_svr_from_bsd(const char *bsd){ |
79 |
|
#ifdef HAVE_LIBDEVINFO_H |
80 |
|
mapping_t *map_ptr; |
81 |
|
for(map_ptr = mapping; map_ptr != NULL; map_ptr = map_ptr->next) |
86 |
|
#endif |
87 |
|
|
88 |
|
#if defined(SOLARIS) && defined(HAVE_LIBDEVINFO_H) |
89 |
< |
void add_mapping(char *bsd, char *svr){ |
89 |
> |
static void add_mapping(char *bsd, char *svr){ |
90 |
|
mapping_t *map_ptr; |
91 |
|
mapping_t *map_end_ptr; |
92 |
|
|
92 |
– |
bsd = strdup(bsd); |
93 |
– |
svr = strdup(svr); |
94 |
– |
|
93 |
|
if (mapping == NULL){ |
94 |
|
mapping = malloc(sizeof(mapping_t)); |
95 |
|
if (mapping == NULL) return; |
112 |
|
} |
113 |
|
|
114 |
|
map_ptr->next = NULL; |
115 |
< |
map_ptr->bsd = bsd; |
116 |
< |
map_ptr->svr = svr; |
115 |
> |
map_ptr->bsd = strdup(bsd); |
116 |
> |
map_ptr->svr = strdup(svr); |
117 |
|
|
118 |
|
return; |
119 |
|
} |
120 |
|
|
121 |
< |
char *read_dir(char *disk_path){ |
121 |
> |
|
122 |
> |
static char *read_dir(char *disk_path){ |
123 |
|
DIR *dirp; |
124 |
|
struct dirent *dp; |
125 |
|
struct stat stbuf; |
161 |
|
} |
162 |
|
|
163 |
|
|
164 |
< |
|
166 |
< |
int get_alias(char *alias){ |
164 |
> |
static int get_alias(char *alias){ |
165 |
|
char file[MAXPATHLEN]; |
166 |
|
di_node_t root_node; |
167 |
|
di_node_t node; |
172 |
|
char *value; |
173 |
|
int instance; |
174 |
|
if ((root_node = di_init("/", DINFOCPYALL)) == DI_NODE_NIL) { |
175 |
< |
return 1; |
175 |
> |
return -1; |
176 |
|
} |
177 |
|
node = di_drv_first_node(alias, root_node); |
178 |
|
while (node != DI_NODE_NIL) { |
182 |
|
minor_name = di_minor_name(minor); |
183 |
|
strcpy(tmpnode, alias); |
184 |
|
sprintf(tmpnode, "%s%d", tmpnode, instance); |
185 |
< |
strlcpy(file, "/devices", sizeof file); |
186 |
< |
strlcat(file, phys_path, sizeof file); |
187 |
< |
strlcat(file, ":", sizeof file); |
188 |
< |
strlcat(file, minor_name, sizeof file); |
185 |
> |
sg_strlcpy(file, "/devices", sizeof file); |
186 |
> |
sg_strlcat(file, phys_path, sizeof file); |
187 |
> |
sg_strlcat(file, ":", sizeof file); |
188 |
> |
sg_strlcat(file, minor_name, sizeof file); |
189 |
|
value = read_dir(file); |
190 |
|
if (value != NULL){ |
191 |
|
add_mapping(tmpnode, value); |
202 |
|
|
203 |
|
|
204 |
|
#define BIG_ENOUGH 512 |
205 |
< |
int build_mapping(){ |
205 |
> |
static int build_mapping(){ |
206 |
|
char device_name[BIG_ENOUGH]; |
207 |
|
int x; |
208 |
|
kstat_ctl_t *kc; |
214 |
|
int found; |
215 |
|
|
216 |
|
if ((kc = kstat_open()) == NULL) { |
217 |
< |
return; |
217 |
> |
return -1; |
218 |
|
} |
219 |
|
|
220 |
|
for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) { |
236 |
|
if (x>=BIG_ENOUGH){ |
237 |
|
/* We've got bigger than we thought was massive */ |
238 |
|
/* If we hit this.. Make big enough bigger */ |
239 |
< |
return 1; |
239 |
> |
return -1; |
240 |
|
} |
241 |
|
if( !strncmp(driver_list[x], device_name, BIG_ENOUGH)){ |
242 |
|
found = 1; |
246 |
|
|
247 |
|
if(!found){ |
248 |
|
if((get_alias(device_name)) != 0){ |
249 |
< |
return 1; |
249 |
> |
return -1; |
250 |
|
} |
251 |
|
strncpy(driver_list[x], device_name, BIG_ENOUGH); |
252 |
|
list_entries++; |
261 |
|
|
262 |
|
|
263 |
|
|
264 |
< |
char *f_read_line(FILE *f, const char *string){ |
264 |
> |
char *sg_f_read_line(FILE *f, const char *string){ |
265 |
|
/* Max line length. 8k should be more than enough */ |
266 |
|
static char line[8192]; |
267 |
|
|
274 |
|
return NULL; |
275 |
|
} |
276 |
|
|
277 |
< |
char *get_string_match(char *line, regmatch_t *match){ |
277 |
> |
char *sg_get_string_match(char *line, regmatch_t *match){ |
278 |
|
int len=match->rm_eo - match->rm_so; |
279 |
|
char *match_string=malloc(len+1); |
280 |
|
|
306 |
|
} |
307 |
|
#endif |
308 |
|
|
311 |
– |
#ifndef HAVE_STRLCPY |
309 |
|
/* $OpenBSD: strlcpy.c,v 1.8 2003/06/17 21:56:24 millert Exp $ */ |
310 |
|
|
311 |
|
/* |
329 |
|
* will be copied. Always NUL terminates (unless siz == 0). |
330 |
|
* Returns strlen(src); if retval >= siz, truncation occurred. |
331 |
|
*/ |
332 |
< |
size_t strlcpy(char *dst, const char *src, size_t siz){ |
332 |
> |
size_t sg_strlcpy(char *dst, const char *src, size_t siz){ |
333 |
|
register char *d = dst; |
334 |
|
register const char *s = src; |
335 |
|
register size_t n = siz; |
352 |
|
|
353 |
|
return(s - src - 1); /* count does not include NUL */ |
354 |
|
} |
358 |
– |
#endif |
355 |
|
|
360 |
– |
#ifndef HAVE_STRLCAT |
356 |
|
/* $OpenBSD: strlcat.c,v 1.11 2003/06/17 21:56:24 millert Exp $ */ |
357 |
|
|
358 |
|
/* |
378 |
|
* Returns strlen(src) + MIN(siz, strlen(initial dst)). |
379 |
|
* If retval >= siz, truncation occurred. |
380 |
|
*/ |
381 |
< |
size_t strlcat(char *dst, const char *src, size_t siz){ |
381 |
> |
size_t sg_strlcat(char *dst, const char *src, size_t siz){ |
382 |
|
register char *d = dst; |
383 |
|
register const char *s = src; |
384 |
|
register size_t n = siz; |
404 |
|
return(dlen + (s - src)); /* count does not include NUL */ |
405 |
|
} |
406 |
|
|
407 |
< |
#endif |
407 |
> |
int sg_update_string(char **dest, const char *src) { |
408 |
> |
char *new; |
409 |
|
|
410 |
< |
long long get_ll_match(char *line, regmatch_t *match){ |
410 |
> |
if (src == NULL) { |
411 |
> |
/* We're being told to set it to NULL. */ |
412 |
> |
free(*dest); |
413 |
> |
*dest = NULL; |
414 |
> |
return 0; |
415 |
> |
} |
416 |
> |
|
417 |
> |
new = realloc(*dest, strlen(src) + 1); |
418 |
> |
if (new == NULL) { |
419 |
> |
return -1; |
420 |
> |
} |
421 |
> |
|
422 |
> |
strcpy(new, src); |
423 |
> |
*dest = new; |
424 |
> |
return 0; |
425 |
> |
} |
426 |
> |
|
427 |
> |
long long sg_get_ll_match(char *line, regmatch_t *match){ |
428 |
|
char *ptr; |
429 |
|
long long num; |
430 |
|
|
435 |
|
} |
436 |
|
|
437 |
|
#if (defined(FREEBSD) && !defined(FREEBSD5)) || defined(DFBSD) |
438 |
< |
kvm_t *get_kvm() { |
438 |
> |
kvm_t *sg_get_kvm() { |
439 |
|
static kvm_t *kvmd = NULL; |
440 |
|
|
441 |
|
if (kvmd != NULL) { |
447 |
|
} |
448 |
|
|
449 |
|
/* Can't think of a better name for this function */ |
450 |
< |
kvm_t *get_kvm2() { |
450 |
> |
kvm_t *sg_get_kvm2() { |
451 |
|
static kvm_t *kvmd2 = NULL; |
452 |
|
|
453 |
|
if (kvmd2 != NULL) { |
460 |
|
#endif |
461 |
|
|
462 |
|
#if defined(NETBSD) || defined(OPENBSD) |
463 |
< |
struct uvmexp *get_uvmexp() { |
463 |
> |
struct uvmexp *sg_get_uvmexp() { |
464 |
|
int mib[2]; |
465 |
< |
size_t size; |
466 |
< |
static struct uvmexp *uvm = NULL; |
465 |
> |
size_t size = sizeof(struct uvmexp); |
466 |
> |
static struct uvmexp uvm; |
467 |
> |
struct uvmexp *new; |
468 |
|
|
469 |
|
mib[0] = CTL_VM; |
470 |
|
mib[1] = VM_UVMEXP; |
471 |
|
|
472 |
< |
if (sysctl(mib, 2, NULL, &size, NULL, 0) < 0) { |
472 |
> |
if (sysctl(mib, 2, &uvm, &size, NULL, 0) < 0) { |
473 |
|
return NULL; |
474 |
|
} |
475 |
|
|
476 |
< |
uvm = realloc(uvm, size); |
463 |
< |
if (uvm == NULL) { |
464 |
< |
return NULL; |
465 |
< |
} |
466 |
< |
|
467 |
< |
if (sysctl(mib, 2, uvm, &size, NULL, 0) < 0) { |
468 |
< |
return NULL; |
469 |
< |
} |
470 |
< |
|
471 |
< |
return uvm; |
476 |
> |
return &uvm; |
477 |
|
} |
478 |
|
#endif |
479 |
|
|
480 |
< |
int statgrab_init(){ |
480 |
> |
int sg_init(){ |
481 |
|
#if (defined(FREEBSD) && !defined(FREEBSD5)) || defined(DFBSD) |
482 |
< |
{ |
483 |
< |
kvm_t *kvmd = get_kvm(); |
479 |
< |
if (kvmd == NULL) return 1; |
482 |
> |
if (sg_get_kvm() == NULL) { |
483 |
> |
return -1; |
484 |
|
} |
485 |
< |
{ |
486 |
< |
kvm_t *kvmd2 = get_kvm2(); |
483 |
< |
if (kvmd2 == NULL) return 1; |
485 |
> |
if (sg_get_kvm2() == NULL) { |
486 |
> |
return -1; |
487 |
|
} |
488 |
|
#endif |
486 |
– |
#if defined(NETBSD) || defined(OPENBSD) |
487 |
– |
{ |
488 |
– |
/* This should always succeed, but it seems that on some |
489 |
– |
* versions of NetBSD the first call to get_uvmexp will return |
490 |
– |
* a non-filled-in structure; this is a workaround for that. |
491 |
– |
*/ |
492 |
– |
struct uvmexp *uvm = get_uvmexp(); |
493 |
– |
if (uvm == NULL) return 1; |
494 |
– |
} |
495 |
– |
#endif |
489 |
|
#ifdef SOLARIS |
490 |
|
/* On solaris 7, this will fail if you are not root. But, everything |
491 |
|
* will still work, just no disk mappings. So we will ignore the exit |
498 |
|
return 0; |
499 |
|
} |
500 |
|
|
501 |
< |
int statgrab_drop_privileges() { |
501 |
> |
int sg_drop_privileges() { |
502 |
|
if (setegid(getgid()) != 0) return -1; |
503 |
|
if (seteuid(getuid()) != 0) return -1; |
504 |
|
return 0; |
505 |
+ |
} |
506 |
+ |
|
507 |
+ |
void *sg_realloc(void *ptr, size_t size) { |
508 |
+ |
void *tmp = NULL; |
509 |
+ |
tmp = realloc(ptr, size); |
510 |
+ |
if(tmp == NULL) { |
511 |
+ |
sg_set_error(SG_ERROR_MALLOC_FAILED, NULL); |
512 |
+ |
} |
513 |
+ |
return tmp; |
514 |
|
} |
515 |
|
|