[SIGAR-89] libproc.dylib does not exist on ppc

This commit is contained in:
Doug MacEachern 2008-02-23 08:18:12 +00:00
parent 4ecf30d8e9
commit e08e3a8bcc
2 changed files with 54 additions and 28 deletions

View File

@ -27,6 +27,7 @@
#include <nfs/nfsproto.h> #include <nfs/nfsproto.h>
#ifdef DARWIN #ifdef DARWIN
#include <dlfcn.h>
#include <mach/mach_init.h> #include <mach/mach_init.h>
#include <mach/message.h> #include <mach/message.h>
#include <mach/kern_return.h> #include <mach/kern_return.h>
@ -38,6 +39,7 @@
#include <mach/thread_info.h> #include <mach/thread_info.h>
#include <mach/vm_map.h> #include <mach/vm_map.h>
#include <mach/shared_memory_server.h> #include <mach/shared_memory_server.h>
#define __OPENTRANSPORTPROVIDERS__
#include <Gestalt.h> #include <Gestalt.h>
#include <CFString.h> #include <CFString.h>
#include <CoreFoundation/CoreFoundation.h> #include <CoreFoundation/CoreFoundation.h>
@ -45,9 +47,6 @@
#include <IOKit/IOKitLib.h> #include <IOKit/IOKitLib.h>
#include <IOKit/IOTypes.h> #include <IOKit/IOTypes.h>
#include <IOKit/storage/IOBlockStorageDriver.h> #include <IOKit/storage/IOBlockStorageDriver.h>
#ifdef DARWIN_HAS_LIBPROC_H
#include <sys/libproc.h>
#endif
#else #else
#include <sys/dkstat.h> #include <sys/dkstat.h>
#include <sys/types.h> #include <sys/types.h>
@ -222,6 +221,12 @@ int sigar_os_open(sigar_t **sigar)
#ifdef DARWIN #ifdef DARWIN
(*sigar)->mach_port = mach_host_self(); (*sigar)->mach_port = mach_host_self();
# ifdef DARWIN_HAS_LIBPROC_H
if (((*sigar)->libproc = dlopen("/usr/lib/libproc.dylib", 0))) {
(*sigar)->proc_pidinfo = dlsym((*sigar)->libproc, "proc_pidinfo");
(*sigar)->proc_pidfdinfo = dlsym((*sigar)->libproc, "proc_pidfdinfo");
}
# endif
#else #else
(*sigar)->kmem = kvm_open(NULL, NULL, NULL, O_RDONLY, NULL); (*sigar)->kmem = kvm_open(NULL, NULL, NULL, O_RDONLY, NULL);
if (stat("/proc/curproc", &sb) < 0) { if (stat("/proc/curproc", &sb) < 0) {
@ -737,13 +742,17 @@ static int proc_fdinfo_get(sigar_t *sigar, sigar_pid_t pid, int *num)
int rsize; int rsize;
const int init_size = PROC_PIDLISTFD_SIZE * 32; const int init_size = PROC_PIDLISTFD_SIZE * 32;
if (!sigar->libproc) {
return SIGAR_ENOTIMPL;
}
if (sigar->ifconf_len == 0) { if (sigar->ifconf_len == 0) {
sigar->ifconf_len = init_size; sigar->ifconf_len = init_size;
sigar->ifconf_buf = malloc(sigar->ifconf_len); sigar->ifconf_buf = malloc(sigar->ifconf_len);
} }
while (1) { while (1) {
rsize = proc_pidinfo(pid, PROC_PIDLISTFDS, 0, rsize = sigar->proc_pidinfo(pid, PROC_PIDLISTFDS, 0,
sigar->ifconf_buf, sigar->ifconf_len); sigar->ifconf_buf, sigar->ifconf_len);
if (rsize <= 0) { if (rsize <= 0) {
return errno; return errno;
@ -868,8 +877,9 @@ int sigar_proc_mem_get(sigar_t *sigar, sigar_pid_t pid,
mach_msg_type_number_t count; mach_msg_type_number_t count;
# ifdef DARWIN_HAS_LIBPROC_H # ifdef DARWIN_HAS_LIBPROC_H
struct proc_taskinfo pti; struct proc_taskinfo pti;
if (sigar->libproc) {
int sz = int sz =
proc_pidinfo(pid, PROC_PIDTASKINFO, 0, &pti, sizeof(pti)); sigar->proc_pidinfo(pid, PROC_PIDTASKINFO, 0, &pti, sizeof(pti));
if (sz == sizeof(pti)) { if (sz == sizeof(pti)) {
procmem->size = pti.pti_virtual_size; procmem->size = pti.pti_virtual_size;
@ -880,6 +890,7 @@ int sigar_proc_mem_get(sigar_t *sigar, sigar_pid_t pid,
procmem->share = SIGAR_FIELD_NOTIMPL; procmem->share = SIGAR_FIELD_NOTIMPL;
return SIGAR_OK; return SIGAR_OK;
} }
}
# endif # endif
status = task_for_pid(self, pid, &task); status = task_for_pid(self, pid, &task);
@ -990,7 +1001,7 @@ int sigar_proc_cred_get(sigar_t *sigar, sigar_pid_t pid,
#define tval2nsec(tval) \ #define tval2nsec(tval) \
(SIGAR_SEC2NANO((tval).seconds) + SIGAR_MICROSEC2NANO((tval).microseconds)) (SIGAR_SEC2NANO((tval).seconds) + SIGAR_MICROSEC2NANO((tval).microseconds))
static int get_proc_times(sigar_pid_t pid, sigar_proc_time_t *time) static int get_proc_times(sigar_t *sigar, sigar_pid_t pid, sigar_proc_time_t *time)
{ {
unsigned int count; unsigned int count;
time_value_t utime = {0, 0}, stime = {0, 0}; time_value_t utime = {0, 0}, stime = {0, 0};
@ -999,9 +1010,10 @@ static int get_proc_times(sigar_pid_t pid, sigar_proc_time_t *time)
task_port_t task, self; task_port_t task, self;
kern_return_t status; kern_return_t status;
# ifdef DARWIN_HAS_LIBPROC_H # ifdef DARWIN_HAS_LIBPROC_H
if (sigar->libproc) {
struct proc_taskinfo pti; struct proc_taskinfo pti;
int sz = int sz =
proc_pidinfo(pid, PROC_PIDTASKINFO, 0, &pti, sizeof(pti)); sigar->proc_pidinfo(pid, PROC_PIDTASKINFO, 0, &pti, sizeof(pti));
if (sz == sizeof(pti)) { if (sz == sizeof(pti)) {
time->user = SIGAR_NSEC2MSEC(pti.pti_total_user); time->user = SIGAR_NSEC2MSEC(pti.pti_total_user);
@ -1009,6 +1021,7 @@ static int get_proc_times(sigar_pid_t pid, sigar_proc_time_t *time)
time->total = time->user + time->sys; time->total = time->user + time->sys;
return SIGAR_OK; return SIGAR_OK;
} }
}
# endif # endif
self = mach_task_self(); self = mach_task_self();
@ -1064,7 +1077,7 @@ int sigar_proc_time_get(sigar_t *sigar, sigar_pid_t pid,
} }
#if defined(DARWIN) #if defined(DARWIN)
if ((status = get_proc_times(pid, proctime)) != SIGAR_OK) { if ((status = get_proc_times(sigar, pid, proctime)) != SIGAR_OK) {
return status; return status;
} }
proctime->start_time = tv2msec(pinfo->KI_START); proctime->start_time = tv2msec(pinfo->KI_START);
@ -2935,13 +2948,17 @@ int sigar_proc_port_get(sigar_t *sigar, int protocol,
sigar_proc_list_t pids; sigar_proc_list_t pids;
int i, status, found=0; int i, status, found=0;
if (!sigar->libproc) {
return SIGAR_ENOTIMPL;
}
status = sigar_proc_list_get(sigar, &pids); status = sigar_proc_list_get(sigar, &pids);
if (status != SIGAR_OK) { if (status != SIGAR_OK) {
return status; return status;
} }
for (i=0; i<pids.number; i++) { for (i=0; i<pids.number; i++) {
int n, num; int n, num=0;
struct proc_fdinfo *fdinfo; struct proc_fdinfo *fdinfo;
status = proc_fdinfo_get(sigar, pids.data[i], &num); status = proc_fdinfo_get(sigar, pids.data[i], &num);
@ -2959,7 +2976,7 @@ int sigar_proc_port_get(sigar_t *sigar, int protocol,
if (fdp->proc_fdtype != PROX_FDTYPE_SOCKET) { if (fdp->proc_fdtype != PROX_FDTYPE_SOCKET) {
continue; continue;
} }
rsize = proc_pidfdinfo(pids.data[i], fdp->proc_fd, rsize = sigar->proc_pidfdinfo(pids.data[i], fdp->proc_fd,
PROC_PIDFDSOCKETINFO, &si, sizeof(si)); PROC_PIDFDSOCKETINFO, &si, sizeof(si));
if (rsize != sizeof(si)) { if (rsize != sizeof(si)) {
continue; continue;

View File

@ -22,6 +22,12 @@
#ifdef DARWIN #ifdef DARWIN
#include <mach/port.h> #include <mach/port.h>
#include <mach/host_info.h> #include <mach/host_info.h>
#ifdef DARWIN_HAS_LIBPROC_H
#include <mach-o/dyld.h>
#include <sys/libproc.h>
typedef int (*proc_pidinfo_func_t)(int, int, uint64_t, void *, int);
typedef int (*proc_pidfdinfo_func_t)(int, int, int, void *, int);
#endif
#else #else
#include <kvm.h> #include <kvm.h>
#endif #endif
@ -52,6 +58,9 @@ struct sigar_t {
bsd_pinfo_t *pinfo; bsd_pinfo_t *pinfo;
#ifdef DARWIN #ifdef DARWIN
mach_port_t mach_port; mach_port_t mach_port;
void *libproc;
proc_pidinfo_func_t proc_pidinfo;
proc_pidfdinfo_func_t proc_pidfdinfo;
#else #else
kvm_t *kmem; kvm_t *kmem;
/* offsets for seeking on kmem */ /* offsets for seeking on kmem */