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 |
|
* |
33 |
|
#include <regex.h> |
34 |
|
#ifdef ALLBSD |
35 |
|
#include <fcntl.h> |
36 |
+ |
#endif |
37 |
+ |
#if (defined(FREEBSD) && !defined(FREEBSD5)) || defined(DFBSD) |
38 |
|
#include <kvm.h> |
39 |
+ |
#include <paths.h> |
40 |
|
#endif |
41 |
< |
#ifdef NETBSD |
41 |
> |
#if defined(NETBSD) || defined(OPENBSD) |
42 |
|
#include <uvm/uvm_extern.h> |
43 |
+ |
#include <sys/param.h> |
44 |
+ |
#include <sys/sysctl.h> |
45 |
|
#endif |
46 |
|
|
47 |
|
#include "tools.h" |
48 |
+ |
#include "statgrab.h" |
49 |
|
|
50 |
|
#ifdef SOLARIS |
51 |
+ |
#ifdef HAVE_LIBDEVINFO_H |
52 |
|
#include <libdevinfo.h> |
53 |
+ |
#endif |
54 |
|
#include <kstat.h> |
55 |
|
#include <unistd.h> |
56 |
|
#include <ctype.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 |
|
|
85 |
– |
bsd = strdup(bsd); |
86 |
– |
svr = strdup(svr); |
87 |
– |
|
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 = 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 |
|
} |
124 |
|
|
125 |
< |
char *read_dir(char *disk_path){ |
125 |
> |
|
126 |
> |
static char *read_dir(char *disk_path){ |
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 |
|
} |
168 |
|
} |
169 |
|
|
170 |
|
|
171 |
< |
|
159 |
< |
int get_alias(char *alias){ |
171 |
> |
static int get_alias(char *alias){ |
172 |
|
char file[MAXPATHLEN]; |
173 |
|
di_node_t root_node; |
174 |
|
di_node_t node; |
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) { |
189 |
|
minor_name = di_minor_name(minor); |
190 |
|
strcpy(tmpnode, alias); |
191 |
|
sprintf(tmpnode, "%s%d", tmpnode, instance); |
192 |
< |
strlcpy(file, "/devices", sizeof file); |
193 |
< |
strlcat(file, phys_path, sizeof file); |
194 |
< |
strlcat(file, ":", sizeof file); |
195 |
< |
strlcat(file, minor_name, sizeof file); |
192 |
> |
sg_strlcpy(file, "/devices", sizeof file); |
193 |
> |
sg_strlcat(file, phys_path, sizeof file); |
194 |
> |
sg_strlcat(file, ":", sizeof file); |
195 |
> |
sg_strlcat(file, minor_name, sizeof file); |
196 |
|
value = read_dir(file); |
197 |
|
if (value != NULL){ |
198 |
|
add_mapping(tmpnode, value); |
209 |
|
|
210 |
|
|
211 |
|
#define BIG_ENOUGH 512 |
212 |
< |
int build_mapping(){ |
212 |
> |
static int build_mapping(){ |
213 |
|
char device_name[BIG_ENOUGH]; |
214 |
|
int x; |
215 |
|
kstat_ctl_t *kc; |
221 |
|
int found; |
222 |
|
|
223 |
|
if ((kc = kstat_open()) == NULL) { |
224 |
< |
return; |
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++; |
268 |
|
|
269 |
|
|
270 |
|
|
271 |
< |
char *f_read_line(FILE *f, const char *string){ |
271 |
> |
char *sg_f_read_line(FILE *f, const char *string){ |
272 |
|
/* Max line length. 8k should be more than enough */ |
273 |
|
static char line[8192]; |
274 |
|
|
281 |
|
return NULL; |
282 |
|
} |
283 |
|
|
284 |
< |
char *get_string_match(char *line, regmatch_t *match){ |
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); |
287 |
|
|
313 |
|
} |
314 |
|
#endif |
315 |
|
|
304 |
– |
#ifndef HAVE_STRLCPY |
316 |
|
/* $OpenBSD: strlcpy.c,v 1.8 2003/06/17 21:56:24 millert Exp $ */ |
317 |
|
|
318 |
|
/* |
336 |
|
* will be copied. Always NUL terminates (unless siz == 0). |
337 |
|
* Returns strlen(src); if retval >= siz, truncation occurred. |
338 |
|
*/ |
339 |
< |
size_t strlcpy(char *dst, const char *src, size_t siz){ |
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; |
359 |
|
|
360 |
|
return(s - src - 1); /* count does not include NUL */ |
361 |
|
} |
351 |
– |
#endif |
362 |
|
|
353 |
– |
#ifndef HAVE_STRLCAT |
363 |
|
/* $OpenBSD: strlcat.c,v 1.11 2003/06/17 21:56:24 millert Exp $ */ |
364 |
|
|
365 |
|
/* |
385 |
|
* Returns strlen(src) + MIN(siz, strlen(initial dst)). |
386 |
|
* If retval >= siz, truncation occurred. |
387 |
|
*/ |
388 |
< |
size_t strlcat(char *dst, const char *src, size_t siz){ |
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; |
411 |
|
return(dlen + (s - src)); /* count does not include NUL */ |
412 |
|
} |
413 |
|
|
414 |
< |
#endif |
414 |
> |
int sg_update_string(char **dest, const char *src) { |
415 |
> |
char *new; |
416 |
|
|
417 |
< |
long long get_ll_match(char *line, regmatch_t *match){ |
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 = realloc(*dest, strlen(src) + 1); |
425 |
> |
if (new == NULL) { |
426 |
> |
return -1; |
427 |
> |
} |
428 |
> |
|
429 |
> |
strcpy(new, src); |
430 |
> |
*dest = new; |
431 |
> |
return 0; |
432 |
> |
} |
433 |
> |
|
434 |
> |
long long sg_get_ll_match(char *line, regmatch_t *match){ |
435 |
|
char *ptr; |
436 |
|
long long num; |
437 |
|
|
441 |
|
return num; |
442 |
|
} |
443 |
|
|
444 |
< |
#ifdef ALLBSD |
445 |
< |
kvm_t *get_kvm() { |
444 |
> |
#if (defined(FREEBSD) && !defined(FREEBSD5)) || defined(DFBSD) |
445 |
> |
kvm_t *sg_get_kvm() { |
446 |
|
static kvm_t *kvmd = NULL; |
447 |
|
|
448 |
|
if (kvmd != NULL) { |
452 |
|
kvmd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, NULL); |
453 |
|
return kvmd; |
454 |
|
} |
428 |
– |
#endif |
455 |
|
|
456 |
< |
#ifdef NETBSD |
457 |
< |
struct uvmexp *get_uvmexp() { |
458 |
< |
static u_long addr = 0; |
433 |
< |
static struct uvmexp uvm; |
434 |
< |
kvm_t *kvmd = get_kvm(); |
456 |
> |
/* Can't think of a better name for this function */ |
457 |
> |
kvm_t *sg_get_kvm2() { |
458 |
> |
static kvm_t *kvmd2 = NULL; |
459 |
|
|
460 |
< |
if (kvmd == NULL) { |
461 |
< |
return NULL; |
460 |
> |
if (kvmd2 != NULL) { |
461 |
> |
return kvmd2; |
462 |
|
} |
463 |
|
|
464 |
< |
if (addr == 0) { |
465 |
< |
struct nlist symbols[] = { |
466 |
< |
{ "_uvmexp" }, |
467 |
< |
{ NULL } |
444 |
< |
}; |
464 |
> |
kvmd2 = kvm_openfiles(_PATH_DEVNULL, _PATH_DEVNULL, NULL, O_RDONLY, NULL); |
465 |
> |
return kvmd2; |
466 |
> |
} |
467 |
> |
#endif |
468 |
|
|
469 |
< |
if (kvm_nlist(kvmd, symbols) != 0) { |
470 |
< |
return NULL; |
471 |
< |
} |
472 |
< |
addr = symbols[0].n_value; |
473 |
< |
} |
469 |
> |
#if defined(NETBSD) || defined(OPENBSD) |
470 |
> |
struct uvmexp *sg_get_uvmexp() { |
471 |
> |
int mib[2]; |
472 |
> |
size_t size = sizeof(struct uvmexp); |
473 |
> |
static struct uvmexp uvm; |
474 |
> |
struct uvmexp *new; |
475 |
|
|
476 |
< |
if (kvm_read(kvmd, addr, &uvm, sizeof uvm) != sizeof uvm) { |
476 |
> |
mib[0] = CTL_VM; |
477 |
> |
mib[1] = VM_UVMEXP; |
478 |
> |
|
479 |
> |
if (sysctl(mib, 2, &uvm, &size, NULL, 0) < 0) { |
480 |
|
return NULL; |
481 |
|
} |
482 |
+ |
|
483 |
|
return &uvm; |
484 |
|
} |
485 |
|
#endif |
486 |
|
|
487 |
< |
int statgrab_init(){ |
488 |
< |
#ifdef ALLBSD |
489 |
< |
{ |
490 |
< |
kvm_t *kvmd = get_kvm(); |
463 |
< |
if (kvmd == NULL) return 1; |
487 |
> |
int sg_init(){ |
488 |
> |
#if (defined(FREEBSD) && !defined(FREEBSD5)) || defined(DFBSD) |
489 |
> |
if (sg_get_kvm() == NULL) { |
490 |
> |
return -1; |
491 |
|
} |
492 |
< |
#endif |
493 |
< |
#ifdef NETBSD |
467 |
< |
{ |
468 |
< |
struct uvmexp *uvm = get_uvmexp(); |
469 |
< |
if (uvm == NULL) return 1; |
492 |
> |
if (sg_get_kvm2() == NULL) { |
493 |
> |
return -1; |
494 |
|
} |
495 |
|
#endif |
496 |
|
#ifdef SOLARIS |
505 |
|
return 0; |
506 |
|
} |
507 |
|
|
508 |
< |
int statgrab_drop_privileges() { |
508 |
> |
int sg_drop_privileges() { |
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_FAILED, NULL); |
519 |
+ |
} |
520 |
+ |
return tmp; |
521 |
|
} |
522 |
|
|