From 08e6b0cb8b8c27bed303503bfeec4b405b8f49b5 Mon Sep 17 00:00:00 2001 From: Doug MacEachern Date: Sat, 12 Feb 2005 01:50:56 +0000 Subject: [PATCH] check /dev/kmem perms --- src/os/darwin/darwin_sigar.c | 32 ++++++++++++++++++++++++++------ src/os/darwin/sigar_os.h | 2 ++ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/src/os/darwin/darwin_sigar.c b/src/os/darwin/darwin_sigar.c index ac95bbf8..7b3286fc 100644 --- a/src/os/darwin/darwin_sigar.c +++ b/src/os/darwin/darwin_sigar.c @@ -43,6 +43,10 @@ static int get_koffsets(sigar_t *sigar) { NULL } }; + if (!sigar->kmem) { + return SIGAR_EPERM_KMEM; + } + kvm_nlist(sigar->kmem, klist); if (klist[0].n_type == 0) { return errno; @@ -57,11 +61,9 @@ static int get_koffsets(sigar_t *sigar) static int kread(sigar_t *sigar, void *data, int size, long offset) { -#if 0 - if (sigar->kmem < 0) { + if (!sigar->kmem) { return SIGAR_EPERM_KMEM; } -#endif if (kvm_read(sigar->kmem, offset, data, size) != size) { return errno; @@ -126,7 +128,12 @@ int sigar_os_close(sigar_t *sigar) char *sigar_os_error_string(int err) { - return NULL; + switch (err) { + case SIGAR_EPERM_KMEM: + return "Failed to open /dev/kmem for reading"; + default: + return NULL; + } } int sigar_mem_get(sigar_t *sigar, sigar_mem_t *mem) @@ -251,6 +258,10 @@ int sigar_swap_get(sigar_t *sigar, sigar_swap_t *swap) #else struct kvm_swap kswap[1]; + if (!sigar->kmem) { + return SIGAR_EPERM_KMEM; + } + if (kvm_getswapinfo(sigar->kmem, kswap, 1, 0) < 0) { return errno; } @@ -375,8 +386,13 @@ int sigar_proc_list_get(sigar_t *sigar, return SIGAR_OK; #else int i, num; - struct kinfo_proc *proc = - kvm_getprocs(sigar->kmem, KERN_PROC_ALL, 0, &num); + struct kinfo_proc *proc; + + if (!sigar->kmem) { + return SIGAR_EPERM_KMEM; + } + + proc = kvm_getprocs(sigar->kmem, KERN_PROC_ALL, 0, &num); proclist->number = 0; proclist->size = num; @@ -625,6 +641,10 @@ int sigar_proc_env_get(sigar_t *sigar, sigar_pid_t pid, struct kinfo_proc *pinfo; int num; + if (!sigar->kmem) { + return SIGAR_EPERM_KMEM; + } + pinfo = kvm_getprocs(sigar->kmem, KERN_PROC_PID, pid, &num); if (!pinfo || (num < 1)) { return errno; diff --git a/src/os/darwin/sigar_os.h b/src/os/darwin/sigar_os.h index e9ca4ce3..dff54d80 100644 --- a/src/os/darwin/sigar_os.h +++ b/src/os/darwin/sigar_os.h @@ -31,4 +31,6 @@ struct sigar_t { #endif }; +#define SIGAR_EPERM_KMEM (SIGAR_OS_START_ERROR+1) + #endif /* SIGAR_OS_H */