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 |
42 |
|
#include <sys/param.h> |
43 |
|
#include <sys/sysctl.h> |
44 |
|
#endif |
45 |
+ |
#ifdef HPUX |
46 |
+ |
#include <sys/param.h> |
47 |
+ |
#include <sys/pstat.h> |
48 |
+ |
#endif |
49 |
|
|
50 |
|
#include "tools.h" |
51 |
|
#include "statgrab.h" |
234 |
|
if(strcmp(ksp->ks_module, "md")==0) continue; |
235 |
|
if((kstat_read(kc, ksp, &kios))==-1) continue; |
236 |
|
strncpy(device_name, ksp->ks_name, sizeof device_name); |
237 |
< |
for(x=0;x<(sizeof device_name);x++){ |
237 |
> |
for(x=0;x<(int)(sizeof device_name);x++){ |
238 |
|
if( isdigit((int)device_name[x]) ) break; |
239 |
|
} |
240 |
|
if(x == sizeof device_name) x--; |
269 |
|
|
270 |
|
#endif |
271 |
|
|
272 |
< |
|
270 |
< |
|
272 |
> |
#if defined(LINUX) || defined(CYGWIN) |
273 |
|
char *sg_f_read_line(FILE *f, const char *string){ |
274 |
|
/* Max line length. 8k should be more than enough */ |
275 |
|
static char line[8192]; |
280 |
|
} |
281 |
|
} |
282 |
|
|
283 |
+ |
sg_set_error(SG_ERROR_PARSE, NULL); |
284 |
|
return NULL; |
285 |
|
} |
286 |
|
|
294 |
|
return match_string; |
295 |
|
} |
296 |
|
|
297 |
< |
|
295 |
< |
|
297 |
> |
/* FIXME do Linux and Cygwin always have atoll? */ |
298 |
|
#ifndef HAVE_ATOLL |
299 |
|
static long long atoll(const char *s) { |
300 |
|
long long value = 0; |
315 |
|
} |
316 |
|
#endif |
317 |
|
|
318 |
+ |
long long sg_get_ll_match(char *line, regmatch_t *match){ |
319 |
+ |
char *ptr; |
320 |
+ |
long long num; |
321 |
+ |
|
322 |
+ |
ptr=line+match->rm_so; |
323 |
+ |
num=atoll(ptr); |
324 |
+ |
|
325 |
+ |
return num; |
326 |
+ |
} |
327 |
+ |
#endif |
328 |
+ |
|
329 |
|
/* $OpenBSD: strlcpy.c,v 1.8 2003/06/17 21:56:24 millert Exp $ */ |
330 |
|
|
331 |
|
/* |
444 |
|
return 0; |
445 |
|
} |
446 |
|
|
434 |
– |
long long sg_get_ll_match(char *line, regmatch_t *match){ |
435 |
– |
char *ptr; |
436 |
– |
long long num; |
437 |
– |
|
438 |
– |
ptr=line+match->rm_so; |
439 |
– |
num=atoll(ptr); |
440 |
– |
|
441 |
– |
return num; |
442 |
– |
} |
443 |
– |
|
447 |
|
#if (defined(FREEBSD) && !defined(FREEBSD5)) || defined(DFBSD) |
448 |
|
kvm_t *sg_get_kvm() { |
449 |
|
static kvm_t *kvmd = NULL; |
453 |
|
} |
454 |
|
|
455 |
|
kvmd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, NULL); |
456 |
+ |
if(kvmd == NULL) { |
457 |
+ |
sg_set_error(SG_ERROR_KVM_OPENFILES, NULL); |
458 |
+ |
} |
459 |
|
return kvmd; |
460 |
|
} |
461 |
|
|
468 |
|
} |
469 |
|
|
470 |
|
kvmd2 = kvm_openfiles(_PATH_DEVNULL, _PATH_DEVNULL, NULL, O_RDONLY, NULL); |
471 |
+ |
if(kvmd2 == NULL) { |
472 |
+ |
sg_set_error(SG_ERROR_KVM_OPENFILES, NULL); |
473 |
+ |
} |
474 |
|
return kvmd2; |
475 |
|
} |
476 |
|
#endif |
486 |
|
mib[1] = VM_UVMEXP; |
487 |
|
|
488 |
|
if (sysctl(mib, 2, &uvm, &size, NULL, 0) < 0) { |
489 |
+ |
sg_set_error_with_errno(SG_ERROR_SYSCTL, "CTL_VM.VM_UVMEXP"); |
490 |
|
return NULL; |
491 |
|
} |
492 |
|
|
494 |
|
} |
495 |
|
#endif |
496 |
|
|
497 |
+ |
#ifdef HPUX |
498 |
+ |
struct pst_static *sg_get_pstat_static() { |
499 |
+ |
static int got = 0; |
500 |
+ |
static struct pst_static pst; |
501 |
+ |
|
502 |
+ |
if (!got) { |
503 |
+ |
if (pstat_getstatic(&pst, sizeof pst, 1, 0) == -1) { |
504 |
+ |
sg_set_error_with_errno(SG_ERROR_PSTAT, |
505 |
+ |
"pstat_static"); |
506 |
+ |
return NULL; |
507 |
+ |
} |
508 |
+ |
got = 1; |
509 |
+ |
} |
510 |
+ |
return &pst; |
511 |
+ |
} |
512 |
+ |
#endif |
513 |
+ |
|
514 |
|
int sg_init(){ |
515 |
+ |
sg_set_error(SG_ERROR_NONE, NULL); |
516 |
+ |
|
517 |
|
#if (defined(FREEBSD) && !defined(FREEBSD5)) || defined(DFBSD) |
518 |
|
if (sg_get_kvm() == NULL) { |
519 |
|
return -1; |
535 |
|
} |
536 |
|
|
537 |
|
int sg_drop_privileges() { |
538 |
< |
if (setegid(getgid()) != 0) return -1; |
539 |
< |
if (seteuid(getuid()) != 0) return -1; |
538 |
> |
#ifdef HAVE_SETEGID |
539 |
> |
if (setegid(getgid()) != 0) { |
540 |
> |
#elif defined(HAVE_SETRESGID) |
541 |
> |
if (setresgid(getgid(), getgid(), getgid()) != 0) { |
542 |
> |
#else |
543 |
> |
{ |
544 |
> |
#endif |
545 |
> |
sg_set_error_with_errno(SG_ERROR_SETEGID, NULL); |
546 |
> |
return -1; |
547 |
> |
} |
548 |
> |
#ifdef HAVE_SETEUID |
549 |
> |
if (seteuid(getuid()) != 0) { |
550 |
> |
#elif defined(HAVE_SETRESUID) |
551 |
> |
if (setresuid(getuid(), getuid(), getuid()) != 0) { |
552 |
> |
#else |
553 |
> |
{ |
554 |
> |
#endif |
555 |
> |
sg_set_error_with_errno(SG_ERROR_SETEUID, NULL); |
556 |
> |
return -1; |
557 |
> |
} |
558 |
|
return 0; |
559 |
|
} |
560 |
|
|
562 |
|
void *tmp = NULL; |
563 |
|
tmp = realloc(ptr, size); |
564 |
|
if(tmp == NULL) { |
565 |
< |
sg_set_error(SG_ERROR_MALLOC_FAILED, NULL); |
565 |
> |
sg_set_error_with_errno(SG_ERROR_MALLOC, NULL); |
566 |
|
} |
567 |
|
return tmp; |
568 |
|
} |
522 |
– |
|