check /dev/kmem perms

This commit is contained in:
Doug MacEachern 2005-02-12 01:50:56 +00:00
parent 066ef754c4
commit 08e6b0cb8b
2 changed files with 28 additions and 6 deletions

View File

@ -43,6 +43,10 @@ static int get_koffsets(sigar_t *sigar)
{ NULL } { NULL }
}; };
if (!sigar->kmem) {
return SIGAR_EPERM_KMEM;
}
kvm_nlist(sigar->kmem, klist); kvm_nlist(sigar->kmem, klist);
if (klist[0].n_type == 0) { if (klist[0].n_type == 0) {
return errno; 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) static int kread(sigar_t *sigar, void *data, int size, long offset)
{ {
#if 0 if (!sigar->kmem) {
if (sigar->kmem < 0) {
return SIGAR_EPERM_KMEM; return SIGAR_EPERM_KMEM;
} }
#endif
if (kvm_read(sigar->kmem, offset, data, size) != size) { if (kvm_read(sigar->kmem, offset, data, size) != size) {
return errno; return errno;
@ -126,8 +128,13 @@ int sigar_os_close(sigar_t *sigar)
char *sigar_os_error_string(int err) char *sigar_os_error_string(int err)
{ {
switch (err) {
case SIGAR_EPERM_KMEM:
return "Failed to open /dev/kmem for reading";
default:
return NULL; return NULL;
} }
}
int sigar_mem_get(sigar_t *sigar, sigar_mem_t *mem) 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 #else
struct kvm_swap kswap[1]; struct kvm_swap kswap[1];
if (!sigar->kmem) {
return SIGAR_EPERM_KMEM;
}
if (kvm_getswapinfo(sigar->kmem, kswap, 1, 0) < 0) { if (kvm_getswapinfo(sigar->kmem, kswap, 1, 0) < 0) {
return errno; return errno;
} }
@ -375,8 +386,13 @@ int sigar_proc_list_get(sigar_t *sigar,
return SIGAR_OK; return SIGAR_OK;
#else #else
int i, num; int i, num;
struct kinfo_proc *proc = struct kinfo_proc *proc;
kvm_getprocs(sigar->kmem, KERN_PROC_ALL, 0, &num);
if (!sigar->kmem) {
return SIGAR_EPERM_KMEM;
}
proc = kvm_getprocs(sigar->kmem, KERN_PROC_ALL, 0, &num);
proclist->number = 0; proclist->number = 0;
proclist->size = num; proclist->size = num;
@ -625,6 +641,10 @@ int sigar_proc_env_get(sigar_t *sigar, sigar_pid_t pid,
struct kinfo_proc *pinfo; struct kinfo_proc *pinfo;
int num; int num;
if (!sigar->kmem) {
return SIGAR_EPERM_KMEM;
}
pinfo = kvm_getprocs(sigar->kmem, KERN_PROC_PID, pid, &num); pinfo = kvm_getprocs(sigar->kmem, KERN_PROC_PID, pid, &num);
if (!pinfo || (num < 1)) { if (!pinfo || (num < 1)) {
return errno; return errno;

View File

@ -31,4 +31,6 @@ struct sigar_t {
#endif #endif
}; };
#define SIGAR_EPERM_KMEM (SIGAR_OS_START_ERROR+1)
#endif /* SIGAR_OS_H */ #endif /* SIGAR_OS_H */