Importing FreeBSD port of libgtop from Josh Sled.

This commit is contained in:
Martin Baulig
1998-08-06 23:34:50 +00:00
parent 864867aeeb
commit 79f5a9c667
20 changed files with 554 additions and 244 deletions

View File

@@ -1,15 +1,16 @@
LINK = $(LIBTOOL) --mode=link $(CC) $(CFLAGS) $(LDFLAGS) -o $@
INCLUDES = -I$(includedir) -I$(top_builddir) -I$(top_srcdir) @machine_incs@ \
-I$(top_srcdir)/include -I$(top_srcdir)/intl @GUILE_INCS@ \
-DGTOPLOCALEDIR=\"$(datadir)/locale\" -D_GNU_SOURCE
CFLAGS = -Wall -W @CFLAGS@
lib_LTLIBRARIES = libgtop_sysdeps.la libgtop_sysdeps_suid.la
lib_LTLIBRARIES = libgtop_sysdeps.la
libgtop_sysdeps_la_SOURCES = nosuid.c siglist.c
libgtop_sysdeps_suid_la_SOURCES = open.c close.c siglist.c cpu.c mem.c swap.c \
libgtop_sysdeps_la_SOURCES = init.c open.c close.c cpu.c mem.c swap.c \
uptime.c loadavg.c shm_limits.c msg_limits.c \
sem_limits.c proclist.c procstate.c procuid.c \
proctime.c procmem.c procsignal.c prockernel.c \
procsegment.c
include_HEADERS = glibtop_server.h glibtop_machine.h

View File

@@ -2,7 +2,7 @@
/* Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
This file is part of the Gnome Top Library.
Contributed by Martin Baulig <martin@home-of-linux.org>, April 1998.
Contributed by Joshua Sled <jsled@xcf.berkeley.edu>, July 1998.
The Gnome Top Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
@@ -24,5 +24,5 @@
/* Closes pipe to gtop server. */
void
glibtop_close_s (glibtop *server)
glibtop_close_l (glibtop *server)
{ }

View File

@@ -2,7 +2,7 @@
/* Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
This file is part of the Gnome Top Library.
Contributed by Martin Baulig <martin@home-of-linux.org>, April 1998.
Contributed by Joshua Sled <jsled@xcf.berkeley.edu>, July 1998.
The Gnome Top Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
@@ -22,6 +22,13 @@
#include <config.h>
#include <glibtop/cpu.h>
#include <nlist.h>
#include <kvm.h>
#include <sys/dkstat.h>
#include <time.h>
#include <sys/types.h>
#include <sys/sysctl.h>
static const unsigned long _glibtop_sysdeps_cpu =
(1 << GLIBTOP_CPU_TOTAL) + (1 << GLIBTOP_CPU_USER) +
(1 << GLIBTOP_CPU_NICE) + (1 << GLIBTOP_CPU_SYS) +
@@ -30,41 +37,66 @@ static const unsigned long _glibtop_sysdeps_cpu =
/* Provides information about cpu usage. */
void
glibtop_get_cpu_p (glibtop *server, glibtop_cpu *buf)
glibtop_get_cpu_s (glibtop *server, glibtop_cpu *buf)
{
long cp_time [CPUSTATES];
int i;
/* kvm_* vars */
kvm_t *kd;
static const int nlst_length=2;
long cpts[CPUSTATES];
/* sysctl vars*/
static const int mib_length=2;
int mib[mib_length];
struct clockinfo ci;
size_t length;
glibtop_init_p (server, 0, 0);
/* nlist structure for kernel access */
struct nlist nlst[nlst_length] = {
{ "_cp_time" },
{ 0 }
};
memset (buf, 0, sizeof (glibtop_cpu));
/* MIB array for sysctl(3) use */
mib[0] = CTL_KERN;
mib[1] = KERN_CLOCKRATE;
/* !!! THE FOLLOWING CODE RUNS SGID KMEM - CHANGE WITH CAUTION !!! */
glibtop_init_r (& server);
setregid (server->machine.gid, server->machine.egid);
memset (buf, 0, sizeof (glibtop_cpu));
/* get the cp_time array */
/* Get the value out of the kernel */
if (kvm_open(NULL, NULL, NULL, O_RDONLY, "kvm_open") == NULL) {
/* If we can't get at the kernel, return the null values we just
gave the array. */
return;
}
kvm_nlist(kd, &nlst);
kvm_read(kd, nlst[0].n_value, &cpts, sizeof(cpts));
kvm_close(kd);
(void) _glibtop_getkval (server, _glibtop_nlist [X_CP_TIME].n_value,
(int *) cp_time, sizeof (cp_time),
_glibtop_nlist [X_CP_TIME].n_name);
/* Get the clockrate data */
length = sizeof(clockinfo);
sysctl(mib, mib_length, &ci, &len, NULL, 0);
if (setregid (server->machine.egid, server->machine.gid))
_exit (1);
buf->flags = _glibtop_sysdeps_cpu;
/* !!! END OF SUID ROOT PART !!! */
/* set user time */
buf->user = cpts[CP_USER];
/* set nice time */
buf->nice = cpts[CP_NICE];
/* set sys time */
buf->sys = cpts[CP_SYS];
/* set idle time */
buf->idle = cpts[CP_IDLE];
/* set frequency */
/*
FIXME -- is hz, tick, profhz or stathz wanted?
buf->frequency = sysctl("kern.clockrate", ...);
buf->user = cp_time [CP_USER];
buf->nice = cp_time [CP_NICE];
buf->sys = cp_time [CP_SYS];
buf->idle = cp_time [CP_IDLE];
buf->frequency = server->machine.hz;
struct clockinfo
*/
buf->frequency = ci.hz;
/* set total */
buf->total = cpts[CP_USER] + cpts[CP_NICE]
+ cpts[CP_SYS] + cpts[CP_IDLE];
/* Calculate total time. */
buf->total = buf->user + buf->nice + buf->sys + buf->idle;
/* Now we can set the flags. */
buf->flags = _glibtop_sysdeps_cpu;
}

View File

@@ -22,64 +22,17 @@
#ifndef __GLIBTOP_MACHINE_H__
#define __GLIBTOP_MACHINE_H__
#include <sys/types.h>
#include <sys/signal.h>
#include <sys/param.h>
#include <stdio.h>
#include <nlist.h>
#include <math.h>
#include <kvm.h>
#include <sys/errno.h>
#include <sys/sysctl.h>
#include <sys/dir.h>
#include <sys/dkstat.h>
#include <sys/file.h>
#include <sys/time.h>
#include <stdlib.h>
#include <sys/rlist.h>
#include <sys/conf.h>
__BEGIN_DECLS
#define X_CCPU 0
#define X_CP_TIME 1
#define X_HZ 2
#define X_STATHZ 3
#define X_AVENRUN 4
#define VM_SWAPLIST 5 /* list of free swap areas */
#define VM_SWDEVT 6 /* list of swap devices and sizes */
#define VM_NSWAP 7 /* size of largest swap device */
#define VM_NSWDEV 8 /* number of swap devices */
#define VM_DMMAX 9 /* maximum size of a swap block */
#define X_CNT 10 /* struct vmmeter cnt */
#define X_LASTPID 11
typedef struct _glibtop_machine glibtop_machine;
typedef struct _glibtop_machine glibtop_machine;
struct _glibtop_machine
{
uid_t uid, euid; /* Real and effective user id */
gid_t gid, egid; /* Real and effective group id */
int nlist_count; /* Number of symbols in the nlist */
u_int64_t hz; /* Tick frequency */
int ncpu; /* Number of CPUs we have */
kvm_t *kd;
uid_t uid, euid;
gid_t gid, egid;
/* The kernel descriptor, used by kvm_* calls. We keep and re-use
it rather than re-getting it for almost all function
invocations. */
kvm_t *kd;
};
/* Those functions are used internally in libgtop */
#ifdef _IN_LIBGTOP
extern struct nlist _glibtop_nlist[];
extern int _glibtop_check_nlist __P((void *, register struct nlist *));
extern int _glibtop_getkval __P((void *, unsigned long, int *, int, char *));
#endif
__END_DECLS
#endif
#endif __GLIBTOP_MACHINE_H__

View File

@@ -2,7 +2,7 @@
/* Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
This file is part of the Gnome Top Library.
Contributed by Martin Baulig <martin@home-of-linux.org>, April 1998.
Contributed by Joshua Sled <jsled@xcf.berkeley.edu>, July 1998.
The Gnome Top Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
@@ -22,10 +22,28 @@
#include <config.h>
#include <glibtop/loadavg.h>
#include <stdlib.h>
/* Provides load averange. */
static const unsigned long _glibtop_sysdeps_loadavg =
(1 << GLIBTOP_LOADAVG_LOADAVG);
void
glibtop_get_loadavg_p (glibtop *server, glibtop_loadavg *buf)
glibtop_get_loadavg_s (glibtop *server, glibtop_loadavg *buf)
{
memset (buf, 0, sizeof (glibtop_loadavg));
double ldavg[3];
glibtop_init_r (&server);
memset (buf, 0, sizeof (glibtop_loadavg));
getloadavg(ldavg, 3);
/* fill in the struct */
buf->flags = _glibtop_sysdeps_loadavg;
for (int i=0; i<3; i++) {
buf->loadavg[i] = ldavg[i];
} /* end for */
}

View File

@@ -2,7 +2,7 @@
/* Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
This file is part of the Gnome Top Library.
Contributed by Martin Baulig <martin@home-of-linux.org>, April 1998.
Contributed by Joshua Sled <jsled@xcf.berkeley.edu>, July 1998.
The Gnome Top Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
@@ -22,10 +22,77 @@
#include <config.h>
#include <glibtop/mem.h>
#include <sys/types.h>
#include <sys/sysctl.h>
#include <sys/vmmeter.h>
static const unsigned long _glibtop_sysdeps_mem =
(1 << GLIBTOP_MEM_TOTAL) + (1 << GLIBTOP_MEM_USED) +
(1 << GLIBTOP_MEM_FREE) + (1 << GLIBTOP_MEM_SHARED) +
(1 << GLIBTOP_MEM_BUFFER) + (1 << GLIBTOP_MEM_CACHED);
/* + (1 << GLIBTOP_MEM_USER); */
/* Provides information about memory usage. */
void
glibtop_get_mem_p (glibtop *server, glibtop_mem *buf)
glibtop_get_mem_s (glibtop *server, glibtop_mem *buf)
{
memset (buf, 0, sizeof (glibtop_mem));
/* for kvm_*, which is needed to get cache and buffer data */
kvm_t *kd;
struct nlist nlst[3];
static unsigned long cnt_offset;
static unsigned long bufsize_offset;
struct vmtotal vmt;
static int bufspace = 0;
/* for sysctl(3) */
size_t length_vmt;
int mib_vm[2];
struct vmmeter vmm;
/* Setup nlist array */
nlst = {
{ "_cnt" },
{ "_bufspace" },
{ 0 }
}
/* Setup MIB array for sysctl */
mib_vm[0] = CTL_VM;
mib_vm[1] = VM_METER;
glibtop_init_r (&server, 0, 0);
memset (buf, 0, sizeof (glibtop_mem));
/* Get the data from sysctl */
length_vmt = sizeof(vmt);
sysctl(mib, 2, &vmt, &length_vmt, NULL, 0);
/* Get the data from kvm_* */
kd = kvm_open(NULL, NULL, NULL, OD_RDONLY, "kvm_open");
kvm_nlist(kd, &nlst);
cnt_offset = nlst[0].n_value;
bufspace_offset = nlst[1].n_value;
kvm_read(kd, cnt_offset, &vmm, sizeof(vmm));
kvm_read(kd, bufspace_offset, &bufspace, sizeof(bufspace));
kvm_close(kd);
/* Set the values to return */
buf->flags = _glibtop_sysdeps_mem;
/* total */
buf->total = (unsigned long)vmt.t_vm;
/* used */
buf->used = (unsigned long)vmt.t_avm;
/* free */
buf->free = (unsigned long)vmt.t_free;
/* shared */
buf->shared = (unsigned long)vmt.t_vmshr;
/* buffer */
buf->buffer = (unsigned long)bufspace;
/* cached */
buf->cache = (unsigned long)(vmm.v_cache_count * vmm.v_page_size);
/* user */
/* FIXME: Any way to get or calculate this? */
}

View File

@@ -2,7 +2,7 @@
/* Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
This file is part of the Gnome Top Library.
Contributed by Martin Baulig <martin@home-of-linux.org>, April 1998.
Contributed by Joshua Sled <jsled@xcf.berkeley.edu>, July 1998.
The Gnome Top Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
@@ -21,10 +21,41 @@
#include <glibtop/msg_limits.h>
#include <sys/msg.h>
static const unsigned long _glibtop_sysdeps_msg_limits =
(1 << GLIBTOP_IPC_MSGMAX) + (1 << GLIBTOP_IPC_MSGMNB) +
(1 << GLIBTOP_IPC_MSGMNI) + (1 << GLIBTOP_IPC_MSGSSZ) +
(1 << GLIBTOP_IPC_MSGTQL);
/* Provides information about sysv ipc limits. */
void
glibtop_get_msg_limits_p (glibtop *server, glibtop_msg_limits *buf)
glibtop_get_msg_limits_s (glibtop *server, glibtop_msg_limits *buf)
{
memset (buf, 0, sizeof (glibtop_msg_limits));
glibtop_init_r(&server, 0, 0);
memset (buf, 0, sizeof (glibtop_msg_limits));
/* Set values */
/* msgpool */
/* Any way to get this?
Seems to be MSGMAX-bytes long.*/
/* buf->msgpool = MSGMAX; */
/* msgmap: sys/msg.h ? */
/* Different type in /usr/include/sys/msg.h */
/* msgmax: sys/msg.h:MSGMAX*/
buf->msgmax = MSGMAX;
/* msgmnb: sys/msg.h:MSGMNB */
buf->msgmnb = MSGMNB;
/* msgmni: sys/msg.h:MSGMNI */
buf->msgmni = MSGMNI;
/* msgssz: sys/msg.h:MSGSSZ */
buf->msgssz = MSGSSZ;
/* msgtql: sys/msg.h:MSGTQL */
buf->msgtql = MSGTQL;
}

View File

@@ -2,7 +2,7 @@
/* Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
This file is part of the Gnome Top Library.
Contributed by Martin Baulig <martin@home-of-linux.org>, April 1998.
Contributed by Joshua Sled <jsled@xcf.berkeley.edu>, July 1998.
The Gnome Top Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
@@ -19,126 +19,26 @@
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA. */
#include <glibtop.h>
#include <glibtop/open.h>
#include <glibtop/xmalloc.h>
struct nlist _glibtop_nlist[] = {
{ "_ccpu" }, /* 0 */
{ "_cp_time" }, /* 1 */
{ "_hz" }, /* 2 */
{ "_stathz" }, /* 3 */
{ "_averunnable" }, /* 4 */
{ "_swaplist" }, /* 5 */
{ "_swdevt" }, /* 6 */
{ "_nswap" }, /* 7 */
{ "_nswdev" }, /* 8 */
{ "_dmmax" }, /* 9 */
{ "_cnt" }, /* 10 */
{ "_nextpid" }, /* 11 */
{ 0 }
};
#include <kvm.h>
#include <osreldate.h>
/* Opens pipe to gtop server. Returns 0 on success and -1 on error. */
/* !!! THIS FUNCTION RUNS SUID ROOT - CHANGE WITH CAUTION !!! */
void
glibtop_init_p (glibtop *server, const unsigned long features,
const unsigned flags)
glibtop_open (glibtop *server, const char *program_name,
const unsigned long features, const unsigned flags)
{
if (server == NULL)
glibtop_error_r (NULL, "glibtop_init_p (server == NULL)");
memset (server, 0, sizeof (glibtop));
server->name = program_name;
server->os_version_code = __FreeBSD_version;
/* Setup machine-specific data */
server->machine.kd = kvm_open(NULL, NULL, NULL, O_RDONLY, "kvm_open");
if (server->machine.kd == NULL) {
/* FIXME: error */
}
glibtop_open_p (server, "glibtop", features, flags);
}
void
glibtop_open_p (glibtop *server, const char *program_name,
const unsigned long features, const unsigned flags)
{
fprintf (stderr, "DEBUG (%d): glibtop_open_p ()\n", getpid ());
/* !!! WE ARE ROOT HERE - CHANGE WITH CAUTION !!! */
server->name = program_name;
server->machine.uid = getuid ();
server->machine.euid = geteuid ();
server->machine.gid = getgid ();
server->machine.egid = getegid ();
/* initialize the kernel interface */
server->machine.kd = kvm_open (NULL, NULL, NULL, O_RDONLY, "libgtop");
if (server->machine.kd == NULL)
glibtop_error_io_r (server, "kvm_open");
/* get the list of symbols we want to access in the kernel */
server->machine.nlist_count = kvm_nlist
(server->machine.kd, _glibtop_nlist);
/* On FreeBSD, kvm_nlist () returns the number of invalid
* entries in the nlist. */
if (server->machine.nlist_count)
glibtop_error_io_r (server, "nlist");
/* Get tick frequency. */
(void) _glibtop_getkval (server, _glibtop_nlist [X_STATHZ].n_value,
(int *) &server->machine.hz,
sizeof (server->machine.hz),
_glibtop_nlist [X_STATHZ].n_name);
if (!server->machine.hz)
(void) _glibtop_getkval
(server, _glibtop_nlist [X_HZ].n_value,
(int *) &server->machine.hz,
sizeof (server->machine.hz),
_glibtop_nlist [X_STATHZ].n_name);
/* Drop priviledges. */
if (setreuid (server->machine.euid, server->machine.uid))
_exit (1);
if (setregid (server->machine.egid, server->machine.gid))
_exit (1);
/* !!! END OF SUID ROOT PART !!! */
/* Our effective uid is now those of the user invoking the server,
* so we do no longer have any priviledges. */
/* NOTE: On SunOS, we do not need to be suid root, we just need to
* be sgid kmem.
*
* The server will only use setegid() to get back it's priviledges,
* so it will fail if it is suid root and not sgid kmem. */
}
/* Used internally. Fetches value from kernel. */
int
_glibtop_getkval (void *void_server, unsigned long offset, int *ptr,
int size, char *refstr)
{
glibtop *server = (glibtop *) void_server;
fprintf (stderr, "DEBUG: kvm_read: %d - %lu - %p - %lu\n",
server->machine.kd, offset, ptr, size);
if (kvm_read (server->machine.kd, offset, ptr, size) != size)
{
if (*refstr == '!') return 0;
glibtop_error_r (server, "kvm_read(%s): %s",
refstr, strerror (errno));
}
return 1;
}

View File

@@ -2,7 +2,7 @@
/* Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
This file is part of the Gnome Top Library.
Contributed by Martin Baulig <martin@home-of-linux.org>, April 1998.
Contributed by Joshua Sled <jsled@xcf.berkeley.edu>, July 1998.
The Gnome Top Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
@@ -22,11 +22,86 @@
#include <config.h>
#include <glibtop/prockernel.h>
#include <kvm.h>
#include <sys/param.h>
#include <sys/sysctl.h>
#include <sys/proc.h>
#include <sys/user.h>
#include <machine/pcb.h>
#include <machine/tss.h>
#include <unistd.h>
#include <fcntl.h>
static const unsigned long _glibtop_sysdeps_proc_kernel =
(1 << GLIBTOP_PROC_KERNEL_K_FLAGS) +
(1 << GLIBTOP_PROC_KERNEL_MIN_FLT) +
(1 << GLIBTOP_PROC_KERNEL_MAJ_FLT) +
(1 << GLIBTOP_PROC_KERNEL_CMIN_FLT) +
(1 << GLIBTOP_PROC_KERNEL_CMAJ_FLT) +
(1 << GLIBTOP_PROC_KERNEL_KSTK_ESP) +
(1 << GLIBTOP_PROC_KERNEL_KSTK_EIP) +
(1 << GLIBTOP_PROC_KERNEL_WCHAN);
/* Provides detailed information about a process. */
void
glibtop_get_proc_kernel_p (glibtop *server, glibtop_proc_kernel *buf,
glibtop_get_proc_kernel_s (glibtop *server,
glibtop_proc_kernel *buf,
pid_t pid)
{
memset (buf, 0, sizeof (glibtop_proc_kernel));
struct kinfo_proc *pinfo;
int count;
struct i386tss *pcb_tss;
int kmem;
struct pstats ps;
struct user usr;
glibtop_init_r (&server, 0, 0);
memset (buf, 0, sizeof (glibtop_proc_kernel));
/* Get the information pertaining to the given PID */
pinfo = kvm_getprocs(server->machine.kd, KERN_PROC_PID, pid, &count);
if (count != 1) {
return; /* the 0-filled struct, since we can't get any info */
}
kmem = open("/dev/kmem", O_RDONLY, NULL);
if (f == NULL) {
/* FIXME: error */
}
/* Read the p_stats struct from kernel memory */
lseek(f, (long)pinfo[0].kp_proc.p_stats, SEEK_SET);
read(f, &ps, sizeof(pstats));
/* Read the struct at kp_proc.p_addr */
lseek(f, (long)pinfo[0].kp_proc.p_addr, SEEK_SET);
read(f, &usr, sizeof(user));
close(f);
/* kflags:
kinfo_proc.e_flag?
proc.p_flag
proc.p_stat
*/
buf->kflags = pinfo[0].kp_eproc.e_flag;
/* min_flt: rusage.ru_minflt */
buf->min_flt = ps.p_ru.ru_minflt;
/* maj_flt: rusage.ru_majflt */
buf->majflt = ps.p_ru.ru_majflt;
/* cmin_flt: */
buf->cmin_flt = ps.p_cru.ru_minflt;
/* cmaj_flt: */
buf->cmaj_flt = ps.p_cru.ru_majflt;
/* kstk_esp: pcb_tss.tss_esp */
buf->kstk_esp = (unsigned long)usr.u_pcb.pcb_ksp;
/* kstk_eip: pcb_tss.tss_eip */
buf->kstk_eip = (unsigned long)usr.u_pcb.pcb_pc;
/* wchan : kinfo_proc.proc.p_wchan */
buf->wchan = (unsigned long)pinfo[0].kp_proc.p_wchan;
}

View File

@@ -2,7 +2,7 @@
/* Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
This file is part of the Gnome Top Library.
Contributed by Martin Baulig <martin@home-of-linux.org>, April 1998.
Contributed by Joshua Sled <jsled@xcf.berkeley.edu>, July 1998.
The Gnome Top Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
@@ -22,18 +22,60 @@
#include <config.h>
#include <glibtop/proclist.h>
#include <kvm.h>
#include <sys/param.h>
#include <sys/sysctl.h>
#define GLIBTOP_PROCLIST_FLAGS 3
static const unsigned long _glibtop_sysdeps_proclist =
(1 << GLIBTOP_PROCLIST_TOTAL) + (1 << GLIBTOP_PROCLIST_NUMBER) +
(1 << GLIBTOP_PROCLIST_SIZE);
/* Fetch list of currently running processes.
* The interface of this function is a little bit different from the others:
* buf->flags is only set if the call succeeded, in this case pids_chain,
* a list of the pids of all currently running processes is returned,
* buf->number is the number of elements of this list and buf->size is
* the size of one single element (sizeof (unsigned)). The total size is
* stored in buf->total.
*
* The calling function has to free the memory to which a pointer is returned.
*
* IMPORTANT NOTE:
* On error, this function MUST return NULL and set buf->flags to zero !
* On success, it returnes a pointer to a list of buf->number elements
* each buf->size big. The total size is stored in buf->total. */
* each buf->size big. The total size is stored in buf->total.
* The calling function has to free the memory to which a pointer is returned.
*
* On error, NULL is returned and buf->flags is zero. */
unsigned *
glibtop_get_proclist_p (glibtop *server, glibtop_proclist *buf)
glibtop_get_proclist_s (glibtop *server, glibtop_proclist *buf)
{
memset (buf, 0, sizeof (glibtop_proclist));
return NULL;
struct kinfo_proc *pinfo;
unsigned *pids=NULL;
int count;
int i;
glibtop_init_r (&server, 0, 0);
memset (buf, 0, sizeof (glibtop_proclist));
/* Get the process data */
pinfo = kvm_getprocs(server->machine.kd, KERN_PROC_ALL, NULL, &count);
/* Allocate count objects in the pids_chain array
Same as malloc is pids is NULL, which it is. */
pids = glibtop_realloc__r(server, pids, count * sizeof(unsigned));
/* Copy the pids over to this chain */
for (i=0; i < count; i++) {
pids[i] = (unsigned)pinfo[i].kp_proc.p_pid;
} /* end for */
/* Set the fields in buf */
buf->number = count;
buf->size = sizeof(unsigned);
buf->total = count * sizeof(unsigned);
buf->flags = _glibtop_sysdeps_proclist;
/* return the data */
return pids;
}

View File

@@ -2,7 +2,7 @@
/* Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
This file is part of the Gnome Top Library.
Contributed by Martin Baulig <martin@home-of-linux.org>, April 1998.
Contributed by Joshua Sled <jsled@xcf.berkeley.edu>, July 1998.
The Gnome Top Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
@@ -22,11 +22,99 @@
#include <config.h>
#include <glibtop/procmem.h>
#include <kvm.h>
#include <sys/param.h>
#include <sys/sysctl.h>
#include <sys/user.h>
#include <sys/proc.h>
#include <sys/resource.h>
#include <vm/vm_map.h>
#include <machine/pmap.h>
static const unsigned long _glibtop_sysdeps_proc_mem =
(1 << GLIBTOP_PROC_MEM_SIZE) +
(1 << GLIBTOP_PROC_MEM_VSIZE) +
(1 << GLIBTOP_PROC_MEM_RESIDENT) +
/* (1 << GLIBTOP_PROC_MEM_SHARE) + */
(1 << GLIBTOP_PROC_MEM_RSS) +
(1 << GLIBTOP_PROC_MEM_RSS_RLIM);
/* Provides detailed information about a process. */
void
glibtop_get_proc_mem_p (glibtop *server, glibtop_proc_mem *buf,
glibtop_get_proc_mem_s (glibtop *server, glibtop_proc_mem *buf,
pid_t pid)
{
memset (buf, 0, sizeof (glibtop_proc_mem));
struct kinfo_proc *pinfo;
int count;
struct vmspace vms;
struct vm_map vmm;
struct pstats ps;
int f;
glibtop_init_r(&server, 0, 0);
memset (buf, 0, sizeof (glibtop_proc_mem));
if (pid == 0) {
/* Client is only interested in the flags. */
buf->flags = _glibtop_sysdeps_proc_mem;
return;
}
f = open("/dev/kmem", O_ORDONLY, NULL);
if (f == NULL) {
/* FIXME: error */
}
/* Read the vmspace from kernel memeory */
lseek(f, (long)pinfo[0].kp_proc.p_vmspace, SEEK_SET);
read(f, &vms, sizeof(vmspace));
/* Read the vm_map from kernel memeory */
lseek(f, (long)vms.vm_map, SEEK_SET);
read(f, &vmm, sizeof(vm_map));
/* Read the pstats [for the RSS rlimit] from kernel memory. */
lseek(f, (long)pinfo[0].kp_proc.p_stats, SEEK_SET);
read(f, &ps, sizeof(pstats));
close(f);
/* Get the process information */
kvm_getprocs(server->machine.kd, KERN_PROC_PID, pid, &count);
if (count != 1) {
/* Return no information */
return;
}
/* size: total # of pages in memory
(segsz_t)pinfo[0].kp_proc.p_vmspace.(vm_tsize + vm_dsize + vm_ssize)
*/
buf->size = (unsigned long)(vms.vm_tsize + vms.vm_dsize + vms.vm_ssize);
/* vsize: number of pages of VM
(vm_size_t)pinfo[0].kp_proc.p_vmspace.vm_map.size
*/
buf->vsize = (unsigned long)vmm.size;
/* resident: number of resident (non-swapped) pages [4k]
(long)pmap_resident_count(pinfo[0]->kp_proc.p_vmspace.vm_map.pmap);
*/
buf->resident = (unsigned long)pmap_resident_count(vmm.pmap);
/* share: number of pages shared (mmap'd) memory
??? vm_object has this info, but how to get it?
Even if we could, it's not reachable information.
*/
/* rss: resident set size
(segsz_t)kp_proc.p_vmspace.vm_rssize
*/
buf->rss = (unsigned long)vms.vm_rssize;
/* rss_rlim: current rss limit [bytes]
(rlim_t)kp_proc.p_limit.pl_rlimit[RLIMIT_RSS].rlim_cur
or
(long)kp_proc.p_stats->p_ru.ru_maxrss */
buf->rss_rlim = ps.p_ru.ru_maxrss;
/* Set the flags */
buf->flags = _glibtop_sysdeps_proc_mem;
}

View File

@@ -2,7 +2,7 @@
/* Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
This file is part of the Gnome Top Library.
Contributed by Martin Baulig <martin@home-of-linux.org>, April 1998.
Contributed by Joshua Sled <jsled@xcf.berkeley.edu>, July 1998.
The Gnome Top Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
@@ -22,11 +22,57 @@
#include <config.h>
#include <glibtop/procsegment.h>
#include <kvm.h>
#include <sys/param.h>
#include <sys/sysctl.h>
static const unsigned long _glibtop_sysdeps_proc_segment =
(1 << GLIBTOP_PROC_SEGMENT_TRS);
/* (1 << GLIBTOP_PROC_SEGMENT_LRS) +
(1 << GLIBTOP_PROC_SEGMENT_DRS) +
(1 << GLIBTOP_PROC_SEGMENT_DT) +
(1 << GLIBTOP_PROC_SEGMENT_START_CODE) +
(1 << GLIBTOP_PROC_SEGMENT_END_CODE) +
(1 << GLIBTOP_PROC_SEGMENT_START_STACK) */
/* Provides detailed information about a process. */
void
glibtop_get_proc_segment_p (glibtop *server, glibtop_proc_segment *buf,
glibtop_get_proc_segment_s (glibtop *server,
glibtop_proc_segment *buf,
pid_t pid)
{
memset (buf, 0, sizeof (glibtop_proc_segment));
struct kinfo_proc *pinfo;
int *count;
glibtop_init_r(&server, 0, 0);
memset (buf, 0, sizeof (glibtop_proc_segment));
/* Get the process info from the kernel */
kvm_getprocs(server->machine.kd, KERN_PROC_PID, pid, count);
if (*count != 1) {
return; /* the zeroed-out buffer indicating no data */
}
/* trs: text resident set size
pinfo[0]->kp_eproc.e_xrssize;
*/
buf->trs = pinfo[0]->kp_eproc.e_xrssize;
/* lrs: shared-lib resident set size
? */
/* drs: data resident set size
pinfo[0]->kp_eproc.e_vm.vm_map.vm_dsize;
*/
/* dt: dirty pages
*/
/* start_code: address of beginning of code segment
*/
/* end_code: address of end of code segment
*/
/* start_stack: address of the bottom of stack segment
*/
}

View File

@@ -2,7 +2,7 @@
/* Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
This file is part of the Gnome Top Library.
Contributed by Martin Baulig <martin@home-of-linux.org>, April 1998.
Contributed by Joshua Sled <jsled@xcf.berkeley.edu>, July 1998.
The Gnome Top Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
@@ -22,11 +22,48 @@
#include <config.h>
#include <glibtop/procsignal.h>
static const unsigned long _glibtop_sysdeps_proc_signal =
(1 << GLIBTOP_PROC_SIGNAL_SIGNAL) +
(1 << GLIBTOP_PROC_SIGNAL_BLOCKED) +
(1 << GLIBTOP_PROC_SIGNAL_SIGIGNORE) +
(1 << GLIBTOP_PROC_SIGNAL_SIGCATCH);
/* Provides detailed information about a process. */
void
glibtop_get_proc_signal_p (glibtop *server, glibtop_proc_signal *buf,
glibtop_get_proc_signal_s (glibtop *server,
glibtop_proc_signal *buf,
pid_t pid)
{
memset (buf, 0, sizeof (glibtop_proc_signal));
glibtop_init_r(&server, 0, 0);
memset (buf, 0, sizeof (glibtop_proc_signal));
if (pid == 0) {
/* Client is only interested in the flags. */
buf->flags = _glibtop_sysdeps_proc_signal;
return;
}
/* signal: mask of pending signals.
pinfo[0].kp_proc.p_siglist
*/
buf->signal = pinfo[0].kp_proc.p_siglist;
/* blocked: mask of blocked signals.
pinfo[0].kp_proc.p_sigmask
*/
buf->blocked = pinfo[0].kp_proc.p_sigmask;
/* sigignore: mask of ignored signals.
pinfo[0].kp_proc.p_sigignore
*/
buf->sigignore = pinfo[0].kp_proc.p_sigignore;
/* sigcatch: mask of caught signals.
pinfo[0].kp_proc.p_sigcatch
*/
buf->sigcatch = pinfo[0].kp_proc.p_sigcatch;
}

View File

@@ -2,7 +2,7 @@
/* Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc.
This file is part of the Gnome Top Library.
Contributed by Martin Baulig <martin@home-of-linux.org>, April 1998.
Contributed by Joshua Sled <jsled@xcf.berkeley.edu, July 1998.
The Gnome Top Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public License as
@@ -22,11 +22,31 @@
#include <config.h>
#include <glibtop/procstate.h>
static const unsigned long _glibtop_sysdeps_proc_state =
(1 << GLIBTOP_PROC_STATE_CMD) +
(1 << GLIBTOP_PROC_STATE_STATE) +
(1 << GLIBTOP_PROC_STATE_UID) +
(1 << GLIBTOP_PROC_STATE_GID);
/* Provides detailed information about a process. */
void
glibtop_get_proc_state_p (glibtop *server, glibtop_proc_state *buf,
glibtop_get_proc_state_s (glibtop *server,
glibtop_proc_state *buf,
pid_t pid)
{
memset (buf, 0, sizeof (glibtop_proc_state));
glibtop_init_r (&server, 0, 0);
memset (buf, 0, sizeof (glibtop_proc_state));
if (pid == INVALID_PID) {
/* Client is only interested in the flags. */
buf->flags = _glibtop_sysdeps_proc_state;
return;
}
/* Set the flags for the data we're about to return*/
buf->flags = _glibtop_sysdeps_proc_state;
}

View File

@@ -25,8 +25,8 @@
/* Provides detailed information about a process. */
void
glibtop_get_proc_time_p (glibtop *server, glibtop_proc_time *buf,
pid_t pid)
glibtop_get_proc_time__r (glibtop *server, glibtop_proc_time *buf,
pid_t pid)
{
memset (buf, 0, sizeof (glibtop_proc_time));
}

View File

@@ -25,8 +25,8 @@
/* Provides detailed information about a process. */
void
glibtop_get_proc_uid_p (glibtop *server, glibtop_proc_uid *buf,
pid_t pid)
glibtop_get_proc_uid__r (glibtop *server, glibtop_proc_uid *buf,
pid_t pid)
{
memset (buf, 0, sizeof (glibtop_proc_uid));
}

View File

@@ -24,7 +24,7 @@
/* Provides information about sysv sem limits. */
void
glibtop_get_sem_limits_p (glibtop *server, glibtop_sem_limits *buf)
glibtop_get_sem_limits__r (glibtop *server, glibtop_sem_limits *buf)
{
memset (buf, 0, sizeof (glibtop_sem_limits));
}

View File

@@ -24,7 +24,7 @@
/* Provides information about sysv ipc limits. */
void
glibtop_get_shm_limits_p (glibtop *server, glibtop_shm_limits *buf)
glibtop_get_shm_limits__r (glibtop *server, glibtop_shm_limits *buf)
{
memset (buf, 0, sizeof (glibtop_shm_limits));
}

View File

@@ -25,7 +25,7 @@
/* Provides information about swap usage. */
void
glibtop_get_swap_p (glibtop *server, glibtop_swap *buf)
glibtop_get_swap__r (glibtop *server, glibtop_swap *buf)
{
memset (buf, 0, sizeof (glibtop_swap));
}

View File

@@ -25,7 +25,7 @@
/* Provides uptime and idle time. */
void
glibtop_get_uptime_p (glibtop *server, glibtop_uptime *buf)
glibtop_get_uptime__r (glibtop *server, glibtop_uptime *buf)
{
memset (buf, 0, sizeof (glibtop_uptime));
}