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" |
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 |
|
|
87 |
– |
bsd = strdup(bsd); |
88 |
– |
svr = strdup(svr); |
89 |
– |
|
92 |
|
if (mapping == NULL){ |
93 |
|
mapping = malloc(sizeof(mapping_t)); |
94 |
|
if (mapping == NULL) return; |
111 |
|
} |
112 |
|
|
113 |
|
map_ptr->next = NULL; |
114 |
< |
map_ptr->bsd = bsd; |
115 |
< |
map_ptr->svr = svr; |
114 |
> |
map_ptr->bsd = strdup(bsd); |
115 |
> |
map_ptr->svr = strdup(svr); |
116 |
|
|
117 |
|
return; |
118 |
|
} |
119 |
|
|
120 |
< |
char *read_dir(char *disk_path){ |
120 |
> |
|
121 |
> |
static char *read_dir(char *disk_path){ |
122 |
|
DIR *dirp; |
123 |
|
struct dirent *dp; |
124 |
|
struct stat stbuf; |
160 |
|
} |
161 |
|
|
162 |
|
|
163 |
< |
|
161 |
< |
int get_alias(char *alias){ |
163 |
> |
static int get_alias(char *alias){ |
164 |
|
char file[MAXPATHLEN]; |
165 |
|
di_node_t root_node; |
166 |
|
di_node_t node; |
181 |
|
minor_name = di_minor_name(minor); |
182 |
|
strcpy(tmpnode, alias); |
183 |
|
sprintf(tmpnode, "%s%d", tmpnode, instance); |
184 |
< |
strlcpy(file, "/devices", sizeof file); |
185 |
< |
strlcat(file, phys_path, sizeof file); |
186 |
< |
strlcat(file, ":", sizeof file); |
187 |
< |
strlcat(file, minor_name, sizeof file); |
184 |
> |
sg_strlcpy(file, "/devices", sizeof file); |
185 |
> |
sg_strlcat(file, phys_path, sizeof file); |
186 |
> |
sg_strlcat(file, ":", sizeof file); |
187 |
> |
sg_strlcat(file, minor_name, sizeof file); |
188 |
|
value = read_dir(file); |
189 |
|
if (value != NULL){ |
190 |
|
add_mapping(tmpnode, value); |
201 |
|
|
202 |
|
|
203 |
|
#define BIG_ENOUGH 512 |
204 |
< |
int build_mapping(){ |
204 |
> |
static int build_mapping(){ |
205 |
|
char device_name[BIG_ENOUGH]; |
206 |
|
int x; |
207 |
|
kstat_ctl_t *kc; |
213 |
|
int found; |
214 |
|
|
215 |
|
if ((kc = kstat_open()) == NULL) { |
216 |
< |
return; |
216 |
> |
return 1; |
217 |
|
} |
218 |
|
|
219 |
|
for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) { |
260 |
|
|
261 |
|
|
262 |
|
|
263 |
< |
char *f_read_line(FILE *f, const char *string){ |
263 |
> |
char *sg_f_read_line(FILE *f, const char *string){ |
264 |
|
/* Max line length. 8k should be more than enough */ |
265 |
|
static char line[8192]; |
266 |
|
|
273 |
|
return NULL; |
274 |
|
} |
275 |
|
|
276 |
< |
char *get_string_match(char *line, regmatch_t *match){ |
276 |
> |
char *sg_get_string_match(char *line, regmatch_t *match){ |
277 |
|
int len=match->rm_eo - match->rm_so; |
278 |
|
char *match_string=malloc(len+1); |
279 |
|
|
305 |
|
} |
306 |
|
#endif |
307 |
|
|
306 |
– |
#ifndef HAVE_STRLCPY |
308 |
|
/* $OpenBSD: strlcpy.c,v 1.8 2003/06/17 21:56:24 millert Exp $ */ |
309 |
|
|
310 |
|
/* |
328 |
|
* will be copied. Always NUL terminates (unless siz == 0). |
329 |
|
* Returns strlen(src); if retval >= siz, truncation occurred. |
330 |
|
*/ |
331 |
< |
size_t strlcpy(char *dst, const char *src, size_t siz){ |
331 |
> |
size_t sg_strlcpy(char *dst, const char *src, size_t siz){ |
332 |
|
register char *d = dst; |
333 |
|
register const char *s = src; |
334 |
|
register size_t n = siz; |
351 |
|
|
352 |
|
return(s - src - 1); /* count does not include NUL */ |
353 |
|
} |
353 |
– |
#endif |
354 |
|
|
355 |
– |
#ifndef HAVE_STRLCAT |
355 |
|
/* $OpenBSD: strlcat.c,v 1.11 2003/06/17 21:56:24 millert Exp $ */ |
356 |
|
|
357 |
|
/* |
377 |
|
* Returns strlen(src) + MIN(siz, strlen(initial dst)). |
378 |
|
* If retval >= siz, truncation occurred. |
379 |
|
*/ |
380 |
< |
size_t strlcat(char *dst, const char *src, size_t siz){ |
380 |
> |
size_t sg_strlcat(char *dst, const char *src, size_t siz){ |
381 |
|
register char *d = dst; |
382 |
|
register const char *s = src; |
383 |
|
register size_t n = siz; |
403 |
|
return(dlen + (s - src)); /* count does not include NUL */ |
404 |
|
} |
405 |
|
|
406 |
< |
#endif |
406 |
> |
int sg_update_string(char **dest, const char *src) { |
407 |
> |
char *new; |
408 |
|
|
409 |
< |
long long get_ll_match(char *line, regmatch_t *match){ |
409 |
> |
if (src == NULL) { |
410 |
> |
/* We're being told to set it to NULL. */ |
411 |
> |
free(*dest); |
412 |
> |
*dest = NULL; |
413 |
> |
return 0; |
414 |
> |
} |
415 |
> |
|
416 |
> |
new = realloc(*dest, strlen(src) + 1); |
417 |
> |
if (new == NULL) { |
418 |
> |
return -1; |
419 |
> |
} |
420 |
> |
|
421 |
> |
strcpy(new, src); |
422 |
> |
*dest = new; |
423 |
> |
return 0; |
424 |
> |
} |
425 |
> |
|
426 |
> |
long long sg_get_ll_match(char *line, regmatch_t *match){ |
427 |
|
char *ptr; |
428 |
|
long long num; |
429 |
|
|
433 |
|
return num; |
434 |
|
} |
435 |
|
|
436 |
< |
#ifdef ALLBSD |
437 |
< |
kvm_t *get_kvm() { |
436 |
> |
#if (defined(FREEBSD) && !defined(FREEBSD5)) || defined(DFBSD) |
437 |
> |
kvm_t *sg_get_kvm() { |
438 |
|
static kvm_t *kvmd = NULL; |
439 |
|
|
440 |
|
if (kvmd != NULL) { |
444 |
|
kvmd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, NULL); |
445 |
|
return kvmd; |
446 |
|
} |
430 |
– |
#endif |
447 |
|
|
448 |
< |
#ifdef NETBSD |
449 |
< |
struct uvmexp *get_uvmexp() { |
450 |
< |
static u_long addr = 0; |
435 |
< |
static struct uvmexp uvm; |
436 |
< |
kvm_t *kvmd = get_kvm(); |
448 |
> |
/* Can't think of a better name for this function */ |
449 |
> |
kvm_t *sg_get_kvm2() { |
450 |
> |
static kvm_t *kvmd2 = NULL; |
451 |
|
|
452 |
< |
if (kvmd == NULL) { |
453 |
< |
return NULL; |
452 |
> |
if (kvmd2 != NULL) { |
453 |
> |
return kvmd2; |
454 |
|
} |
455 |
|
|
456 |
< |
if (addr == 0) { |
457 |
< |
struct nlist symbols[] = { |
458 |
< |
{ "_uvmexp" }, |
459 |
< |
{ NULL } |
446 |
< |
}; |
456 |
> |
kvmd2 = kvm_openfiles(_PATH_DEVNULL, _PATH_DEVNULL, NULL, O_RDONLY, NULL); |
457 |
> |
return kvmd2; |
458 |
> |
} |
459 |
> |
#endif |
460 |
|
|
461 |
< |
if (kvm_nlist(kvmd, symbols) != 0) { |
462 |
< |
return NULL; |
463 |
< |
} |
464 |
< |
addr = symbols[0].n_value; |
465 |
< |
} |
461 |
> |
#if defined(NETBSD) || defined(OPENBSD) |
462 |
> |
struct uvmexp *sg_get_uvmexp() { |
463 |
> |
int mib[2]; |
464 |
> |
size_t size = sizeof(struct uvmexp); |
465 |
> |
static struct uvmexp uvm; |
466 |
> |
struct uvmexp *new; |
467 |
|
|
468 |
< |
if (kvm_read(kvmd, addr, &uvm, sizeof uvm) != sizeof uvm) { |
468 |
> |
mib[0] = CTL_VM; |
469 |
> |
mib[1] = VM_UVMEXP; |
470 |
> |
|
471 |
> |
if (sysctl(mib, 2, &uvm, &size, NULL, 0) < 0) { |
472 |
|
return NULL; |
473 |
|
} |
474 |
+ |
|
475 |
|
return &uvm; |
476 |
|
} |
477 |
|
#endif |
478 |
|
|
479 |
< |
int statgrab_init(){ |
480 |
< |
#ifdef ALLBSD |
481 |
< |
{ |
482 |
< |
kvm_t *kvmd = get_kvm(); |
465 |
< |
if (kvmd == NULL) return 1; |
479 |
> |
int sg_init(){ |
480 |
> |
#if (defined(FREEBSD) && !defined(FREEBSD5)) || defined(DFBSD) |
481 |
> |
if (sg_get_kvm() == NULL) { |
482 |
> |
return 1; |
483 |
|
} |
484 |
< |
#endif |
485 |
< |
#ifdef NETBSD |
469 |
< |
{ |
470 |
< |
struct uvmexp *uvm = get_uvmexp(); |
471 |
< |
if (uvm == NULL) return 1; |
484 |
> |
if (sg_get_kvm2() == NULL) { |
485 |
> |
return 1; |
486 |
|
} |
487 |
|
#endif |
488 |
|
#ifdef SOLARIS |
497 |
|
return 0; |
498 |
|
} |
499 |
|
|
500 |
< |
int statgrab_drop_privileges() { |
500 |
> |
int sg_drop_privileges() { |
501 |
|
if (setegid(getgid()) != 0) return -1; |
502 |
|
if (seteuid(getuid()) != 0) return -1; |
503 |
|
return 0; |