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 |
265 |
|
|
266 |
|
#endif |
267 |
|
|
268 |
< |
|
270 |
< |
|
268 |
> |
#if defined(LINUX) || defined(CYGWIN) |
269 |
|
char *sg_f_read_line(FILE *f, const char *string){ |
270 |
|
/* Max line length. 8k should be more than enough */ |
271 |
|
static char line[8192]; |
276 |
|
} |
277 |
|
} |
278 |
|
|
279 |
+ |
sg_set_error(SG_ERROR_PARSE, NULL); |
280 |
|
return NULL; |
281 |
|
} |
282 |
|
|
290 |
|
return match_string; |
291 |
|
} |
292 |
|
|
293 |
+ |
long long sg_get_ll_match(char *line, regmatch_t *match){ |
294 |
+ |
char *ptr; |
295 |
+ |
long long num; |
296 |
|
|
297 |
+ |
ptr=line+match->rm_so; |
298 |
+ |
num=atoll(ptr); |
299 |
|
|
300 |
+ |
return num; |
301 |
+ |
} |
302 |
+ |
#endif |
303 |
+ |
|
304 |
|
#ifndef HAVE_ATOLL |
305 |
|
static long long atoll(const char *s) { |
306 |
|
long long value = 0; |
439 |
|
return 0; |
440 |
|
} |
441 |
|
|
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 |
– |
|
442 |
|
#if (defined(FREEBSD) && !defined(FREEBSD5)) || defined(DFBSD) |
443 |
|
kvm_t *sg_get_kvm() { |
444 |
|
static kvm_t *kvmd = NULL; |
448 |
|
} |
449 |
|
|
450 |
|
kvmd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, NULL); |
451 |
+ |
if(kvmd == NULL) { |
452 |
+ |
sg_set_error(SG_ERROR_KVM_OPENFILES, NULL); |
453 |
+ |
} |
454 |
|
return kvmd; |
455 |
|
} |
456 |
|
|
463 |
|
} |
464 |
|
|
465 |
|
kvmd2 = kvm_openfiles(_PATH_DEVNULL, _PATH_DEVNULL, NULL, O_RDONLY, NULL); |
466 |
+ |
if(kvmd2 == NULL) { |
467 |
+ |
sg_set_error(SG_ERROR_KVM_OPENFILES, NULL); |
468 |
+ |
} |
469 |
|
return kvmd2; |
470 |
|
} |
471 |
|
#endif |
481 |
|
mib[1] = VM_UVMEXP; |
482 |
|
|
483 |
|
if (sysctl(mib, 2, &uvm, &size, NULL, 0) < 0) { |
484 |
+ |
sg_set_error_with_errno(SG_ERROR_SYSCTL, "CTL_VM.VM_UVMEXP"); |
485 |
|
return NULL; |
486 |
|
} |
487 |
|
|
490 |
|
#endif |
491 |
|
|
492 |
|
int sg_init(){ |
493 |
+ |
sg_set_error(SG_ERROR_NONE, NULL); |
494 |
+ |
|
495 |
|
#if (defined(FREEBSD) && !defined(FREEBSD5)) || defined(DFBSD) |
496 |
|
if (sg_get_kvm() == NULL) { |
497 |
|
return -1; |
513 |
|
} |
514 |
|
|
515 |
|
int sg_drop_privileges() { |
516 |
< |
if (setegid(getgid()) != 0) return -1; |
517 |
< |
if (seteuid(getuid()) != 0) return -1; |
516 |
> |
#ifdef HAVE_SETEGID |
517 |
> |
if (setegid(getgid()) != 0) { |
518 |
> |
#elif defined(HAVE_SETRESGID) |
519 |
> |
if (setresgid(getgid(), getgid(), getgid()) != 0) { |
520 |
> |
#else |
521 |
> |
{ |
522 |
> |
#endif |
523 |
> |
sg_set_error_with_errno(SG_ERROR_SETEGID, NULL); |
524 |
> |
return -1; |
525 |
> |
} |
526 |
> |
#ifdef HAVE_SETEUID |
527 |
> |
if (seteuid(getuid()) != 0) { |
528 |
> |
#elif defined(HAVE_SETRESUID) |
529 |
> |
if (setresuid(getuid(), getuid(), getuid()) != 0) { |
530 |
> |
#else |
531 |
> |
{ |
532 |
> |
#endif |
533 |
> |
sg_set_error_with_errno(SG_ERROR_SETEUID, NULL); |
534 |
> |
return -1; |
535 |
> |
} |
536 |
|
return 0; |
537 |
|
} |
538 |
|
|
540 |
|
void *tmp = NULL; |
541 |
|
tmp = realloc(ptr, size); |
542 |
|
if(tmp == NULL) { |
543 |
< |
sg_set_error(SG_ERROR_MALLOC_FAILED, NULL); |
543 |
> |
sg_set_error_with_errno(SG_ERROR_MALLOC, NULL); |
544 |
|
} |
545 |
|
return tmp; |
546 |
|
} |
522 |
– |
|