diff options
author | Martin Baulig <martin@src.gnome.org> | 1998-08-07 15:12:04 +0000 |
---|---|---|
committer | Martin Baulig <martin@src.gnome.org> | 1998-08-07 15:12:04 +0000 |
commit | 6893d89fd865f226e91e9e3705bab081b1304235 (patch) | |
tree | 503e6977bd3886e6843298022c77adf991784fe6 | |
parent | 35e900692a5d1a8d74c5105bf51da329885eadb3 (diff) | |
download | libgtop-6893d89fd865f226e91e9e3705bab081b1304235.tar.gz |
Merged code from Josh Sled.
-rw-r--r-- | sysdeps/freebsd/Makefile.am | 7 | ||||
-rw-r--r-- | sysdeps/freebsd/cpu.c | 57 | ||||
-rw-r--r-- | sysdeps/freebsd/glibtop_machine.h | 29 | ||||
-rw-r--r-- | sysdeps/freebsd/glibtop_suid.h | 4 | ||||
-rw-r--r-- | sysdeps/freebsd/loadavg.c | 16 | ||||
-rw-r--r-- | sysdeps/freebsd/mem.c | 67 | ||||
-rw-r--r-- | sysdeps/freebsd/msg_limits.c | 4 | ||||
-rw-r--r-- | sysdeps/freebsd/open.c | 72 | ||||
-rw-r--r-- | sysdeps/freebsd/prockernel.c | 79 | ||||
-rw-r--r-- | sysdeps/freebsd/proclist.c | 47 | ||||
-rw-r--r-- | sysdeps/freebsd/procmem.c | 83 | ||||
-rw-r--r-- | sysdeps/freebsd/procsegment.c | 47 | ||||
-rw-r--r-- | sysdeps/freebsd/procsignal.c | 35 | ||||
-rw-r--r-- | sysdeps/freebsd/procstate.c | 14 |
14 files changed, 506 insertions, 55 deletions
diff --git a/sysdeps/freebsd/Makefile.am b/sysdeps/freebsd/Makefile.am index 9c188950..40aae758 100644 --- a/sysdeps/freebsd/Makefile.am +++ b/sysdeps/freebsd/Makefile.am @@ -4,13 +4,14 @@ 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_la_SOURCES = init.c open.c close.c cpu.c mem.c swap.c \ +libgtop_sysdeps_suid_la_SOURCES = open.c close.c siglist.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 diff --git a/sysdeps/freebsd/cpu.c b/sysdeps/freebsd/cpu.c index 25bb8c14..845f1d87 100644 --- a/sysdeps/freebsd/cpu.c +++ b/sysdeps/freebsd/cpu.c @@ -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 @@ -25,7 +25,20 @@ #include <glibtop_suid.h> -static const unsigned long _glibtop_sysdeps_cpu = 0; +static const unsigned long _glibtop_sysdeps_cpu = +(1 << GLIBTOP_CPU_TOTAL) + (1 << GLIBTOP_CPU_USER) + +(1 << GLIBTOP_CPU_NICE) + (1 << GLIBTOP_CPU_SYS) + +(1 << GLIBTOP_CPU_IDLE) + (1 << GLIBTOP_CPU_FREQUENCY); + +/* nlist structure for kernel access */ +static struct nlist nlst [] = { + { "_cp_time" }, + { 0 } +}; + +/* MIB array for sysctl */ +static const int mib_length=2; +static const int mib [] = { CTL_KERN, KERN_CLOCKRATE }; /* Init function. */ @@ -33,6 +46,9 @@ void glibtop_init_cpu_p (glibtop *server) { server->sysdeps.cpu = _glibtop_sysdeps_cpu; + + if (kvm_nlist (server->machine.kd, nlst) != 0) + glibtop_error_io_r (server, "kvm_nlist"); } /* Provides information about cpu usage. */ @@ -40,7 +56,44 @@ glibtop_init_cpu_p (glibtop *server) void glibtop_get_cpu_p (glibtop *server, glibtop_cpu *buf) { + long cpts [CPUSTATES]; + /* sysctl vars*/ + struct clockinfo ci; + size_t length; + glibtop_init_p (server, GLIBTOP_SYSDEPS_CPU, 0); memset (buf, 0, sizeof (glibtop_cpu)); + + if (kvm_read (server->machine.kd, nlst [0].n_value, + &cpts, sizeof (cpts))) + glibtop_error_io_r (server, "kvm_read (cp_time)"); + + /* Get the clockrate data */ + length = sizeof (struct clockinfo); + if (sysctl (mib, mib_length, &ci, &length, NULL, 0)) + glibtop_error_io_r (server, "sysctl"); + + buf->flags = _glibtop_sysdeps_cpu; + + /* 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", ...); + + struct clockinfo + */ + buf->frequency = ci.hz; + /* set total */ + buf->total = cpts [CP_USER] + cpts [CP_NICE] + + cpts [CP_SYS] + cpts [CP_IDLE]; } diff --git a/sysdeps/freebsd/glibtop_machine.h b/sysdeps/freebsd/glibtop_machine.h index 9fb3afdc..e2101394 100644 --- a/sysdeps/freebsd/glibtop_machine.h +++ b/sysdeps/freebsd/glibtop_machine.h @@ -22,17 +22,32 @@ #ifndef __GLIBTOP_MACHINE_H__ #define __GLIBTOP_MACHINE_H__ +#include <nlist.h> +#include <kvm.h> +#include <sys/dkstat.h> +#include <time.h> +#include <sys/user.h> +#include <sys/types.h> +#include <sys/sysctl.h> + +#include <fcntl.h> +#include <osreldate.h> + +__BEGIN_DECLS + typedef struct _glibtop_machine glibtop_machine; struct _glibtop_machine { - 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; + 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; }; +__END_DECLS + #endif __GLIBTOP_MACHINE_H__ diff --git a/sysdeps/freebsd/glibtop_suid.h b/sysdeps/freebsd/glibtop_suid.h index 349d8f2a..bff87930 100644 --- a/sysdeps/freebsd/glibtop_suid.h +++ b/sysdeps/freebsd/glibtop_suid.h @@ -25,11 +25,11 @@ __BEGIN_DECLS static inline void glibtop_suid_enter (glibtop *server) { - setreuid (server->machine.uid, server->machine.euid); + setregid (server->machine.gid, server->machine.egid); }; static inline void glibtop_suid_leave (glibtop *server) { - if (setreuid (server->machine.euid, server->machine.uid)) + if (setregid (server->machine.egid, server->machine.gid)) _exit (1); }; diff --git a/sysdeps/freebsd/loadavg.c b/sysdeps/freebsd/loadavg.c index 733e263b..5cb3bbae 100644 --- a/sysdeps/freebsd/loadavg.c +++ b/sysdeps/freebsd/loadavg.c @@ -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 @@ -25,7 +25,8 @@ #include <glibtop_suid.h> -static const unsigned long _glibtop_sysdeps_loadavg = 0; +static const unsigned long _glibtop_sysdeps_loadavg = +(1 << GLIBTOP_LOADAVG_LOADAVG); /* Init function. */ @@ -40,7 +41,18 @@ glibtop_init_loadavg_p (glibtop *server) void glibtop_get_loadavg_p (glibtop *server, glibtop_loadavg *buf) { + double ldavg[3]; + int i; + glibtop_init_p (server, GLIBTOP_SYSDEPS_LOADAVG, 0); memset (buf, 0, sizeof (glibtop_loadavg)); + + getloadavg (ldavg, 3); + + /* fill in the struct */ + buf->flags = _glibtop_sysdeps_loadavg; + for (i = 0; i < 3; i++) { + buf->loadavg [i] = ldavg [i]; + } /* end for */ } diff --git a/sysdeps/freebsd/mem.c b/sysdeps/freebsd/mem.c index f379f451..eb9a515e 100644 --- a/sysdeps/freebsd/mem.c +++ b/sysdeps/freebsd/mem.c @@ -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 @@ -25,7 +25,26 @@ #include <glibtop_suid.h> -static const unsigned long _glibtop_sysdeps_mem = 0; +#include <sys/sysctl.h> +#include <sys/vmmeter.h> +#include <vm/vm_param.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); + +/* nlist structure for kernel access */ +static struct nlist nlst [] = { + { "_cnt" }, + { "_bufspace" }, + { 0 } +}; + +/* MIB array for sysctl */ +static const int mib_length=2; +static const int mib [] = { CTL_VM, VM_METER }; /* Init function. */ @@ -33,14 +52,54 @@ void glibtop_init_mem_p (glibtop *server) { server->sysdeps.mem = _glibtop_sysdeps_mem; -} -/* Provides information about memory usage. */ + if (kvm_nlist (server->machine.kd, nlst) != 0) + glibtop_error_io_r (server, "kvm_nlist"); + +} void glibtop_get_mem_p (glibtop *server, glibtop_mem *buf) { + struct vmtotal vmt; + /* for sysctl(3) */ + size_t length_vmt; + struct vmmeter vmm; + int bufspace; + glibtop_init_p (server, GLIBTOP_SYSDEPS_MEM, 0); memset (buf, 0, sizeof (glibtop_mem)); + + /* Get the data from sysctl */ + length_vmt = sizeof (vmt); + if (sysctl (mib, 2, &vmt, &length_vmt, NULL, 0)) + glibtop_error_io_r (server, "sysctl"); + + /* Get the data from kvm_* */ + if (kvm_read (server->machine.kd, nlst[0].n_value, + &vmm, sizeof (vmm))) + glibtop_error_io_r (server, "kvm_read (cnt)"); + + if (kvm_read (server->machine.kd, nlst[1].n_value, + &bufspace, sizeof (bufspace))) + glibtop_error_io_r (server, "kvm_read (bufspace)"); + + /* Set the values to return */ + buf->flags = _glibtop_sysdeps_mem; + /* total */ + buf->total = (u_int64_t) vmt.t_vm; + /* used */ + buf->used = (u_int64_t) vmt.t_avm; + /* free */ + buf->free = (u_int64_t) vmt.t_free; + /* shared */ + buf->shared = (u_int64_t) vmt.t_vmshr; + /* buffer */ + buf->buffer = (u_int64_t) bufspace; + /* cached */ + buf->cached = (u_int64_t) (vmm.v_cache_count * vmm.v_page_size); + + /* user */ + buf->user = buf->total - buf->free - buf->shared - buf->buffer; } diff --git a/sysdeps/freebsd/msg_limits.c b/sysdeps/freebsd/msg_limits.c index ea1417f2..533363f4 100644 --- a/sysdeps/freebsd/msg_limits.c +++ b/sysdeps/freebsd/msg_limits.c @@ -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 @@ -25,6 +25,8 @@ #include <glibtop_suid.h> +#include <sys/msg.h> + static const unsigned long _glibtop_sysdeps_msg_limits = 0; /* Init function. */ diff --git a/sysdeps/freebsd/open.c b/sysdeps/freebsd/open.c index e4c0b48b..99255850 100644 --- a/sysdeps/freebsd/open.c +++ b/sysdeps/freebsd/open.c @@ -19,26 +19,72 @@ 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> -#include <kvm.h> -#include <osreldate.h> +/* !!! THIS FUNCTION RUNS SUID ROOT - CHANGE WITH CAUTION !!! */ -/* Opens pipe to gtop server. Returns 0 on success and -1 on error. */ +void +glibtop_init_p (glibtop *server, const unsigned long features, + const unsigned flags) +{ + glibtop_init_func_t *init_fkt; + + if (server == NULL) + glibtop_error_r (NULL, "glibtop_init_p (server == NULL)"); + + /* Do the initialization, but only if not already initialized. */ + + if ((server->flags & _GLIBTOP_INIT_STATE_INIT) == 0) { + glibtop_open_p (server, "glibtop", features, flags); + + for (init_fkt = _glibtop_init_hook_p; *init_fkt; init_fkt++) + (*init_fkt) (server); + + server->flags |= _GLIBTOP_INIT_STATE_INIT; + } +} void -glibtop_open (glibtop *server, const char *program_name, - const unsigned long features, const unsigned flags) +glibtop_open_p (glibtop *server, const char *program_name, + const unsigned long features, const unsigned flags) { - memset (server, 0, sizeof (glibtop)); - server->name = program_name; + 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 (); - server->os_version_code = __FreeBSD_version; + 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 */ - } + /* Setup machine-specific data */ + server->machine.kd = kvm_open (NULL, NULL, NULL, O_RDONLY, "kvm_open"); + + if (server->machine.kd == NULL) + glibtop_error_io_r (server, "kvm_open"); + + /* 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 FreeBSD, 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. */ } diff --git a/sysdeps/freebsd/prockernel.c b/sysdeps/freebsd/prockernel.c index 4f183894..ee0015a6 100644 --- a/sysdeps/freebsd/prockernel.c +++ b/sysdeps/freebsd/prockernel.c @@ -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 @@ -25,7 +25,26 @@ #include <glibtop_suid.h> -static const unsigned long _glibtop_sysdeps_proc_kernel = 0; +#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); /* Init function. */ @@ -35,13 +54,63 @@ glibtop_init_proc_kernel_p (glibtop *server) server->sysdeps.proc_kernel = _glibtop_sysdeps_proc_kernel; } -/* Provides detailed information about a process. */ - void -glibtop_get_proc_kernel_p (glibtop *server, glibtop_proc_kernel *buf, +glibtop_get_proc_kernel_p (glibtop *server, + glibtop_proc_kernel *buf, pid_t pid) { + struct kinfo_proc *pinfo; + struct i386tss *pcb_tss; + int f, count, kmem; + struct pstats ps; + struct user usr; + glibtop_init_p (server, GLIBTOP_SYSDEPS_PROC_KERNEL, 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) + glibtop_error_io_r (server, "open (/dev/kmem)"); + + /* Read the p_stats struct from kernel memory */ + lseek (f, (long) pinfo[0].kp_proc.p_stats, SEEK_SET); + if (read (f, &ps, sizeof (struct pstats))) + glibtop_error_io_r (server, "read"); + /* Read the struct at kp_proc.p_addr */ + lseek (f, (long) pinfo[0].kp_proc.p_addr, SEEK_SET); + if (read(f, &usr, sizeof (struct user))) + glibtop_error_io_r (server, "read"); + close (f); + + + /* kflags: + kinfo_proc.e_flag? + proc.p_flag + proc.p_stat + */ + buf->k_flags = 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->maj_flt = 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 = (u_int64_t) usr.u_pcb.pcb_ksp; + /* kstk_eip: pcb_tss.tss_eip */ + buf->kstk_eip = (u_int64_t) usr.u_pcb.pcb_pc; + + /* wchan : kinfo_proc.proc.p_wchan */ + buf->wchan = (u_int64_t) pinfo[0].kp_proc.p_wchan; } diff --git a/sysdeps/freebsd/proclist.c b/sysdeps/freebsd/proclist.c index d94600ca..e5bea008 100644 --- a/sysdeps/freebsd/proclist.c +++ b/sysdeps/freebsd/proclist.c @@ -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,11 +21,32 @@ #include <glibtop.h> #include <glibtop/error.h> +#include <glibtop/xmalloc.h> #include <glibtop/proclist.h> #include <glibtop_suid.h> -static const unsigned long _glibtop_sysdeps_proclist = 0; +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. + * The calling function has to free the memory to which a pointer is returned. + * + * On error, NULL is returned and buf->flags is zero. */ /* Init function. */ @@ -38,9 +59,29 @@ glibtop_init_proclist_p (glibtop *server) unsigned * glibtop_get_proclist_p (glibtop *server, glibtop_proclist *buf) { + struct kinfo_proc *pinfo; + unsigned *pids = NULL; + int count; + int i; + glibtop_init_p (server, GLIBTOP_SYSDEPS_PROCLIST, 0); memset (buf, 0, sizeof (glibtop_proclist)); - return NULL; + /* 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 pids; } diff --git a/sysdeps/freebsd/procmem.c b/sysdeps/freebsd/procmem.c index 357d9456..5e7f0660 100644 --- a/sysdeps/freebsd/procmem.c +++ b/sysdeps/freebsd/procmem.c @@ -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 @@ -25,7 +25,22 @@ #include <glibtop_suid.h> -static const unsigned long _glibtop_sysdeps_proc_mem = 0; +#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); /* Init function. */ @@ -41,7 +56,71 @@ void glibtop_get_proc_mem_p (glibtop *server, glibtop_proc_mem *buf, pid_t pid) { + struct kinfo_proc *pinfo; + struct vmspace vms; + struct vm_map vmm; + struct pstats ps; + int count, f; + glibtop_init_p (server, GLIBTOP_SYSDEPS_PROC_MEM, 0); memset (buf, 0, sizeof (glibtop_proc_mem)); + + f = open ("/dev/kmem", O_RDONLY, NULL); + if (f == NULL) + glibtop_error_io_r (server, "open (/dev/kmem)"); + + /* Read the vmspace from kernel memeory */ + lseek (f, (long)pinfo[0].kp_proc.p_vmspace, SEEK_SET); + if (read (f, &vms, sizeof (struct vmspace)) < 0) + glibtop_error_io_r (server, "read"); + /* Read the vm_map from kernel memeory */ + /* [FIXME: ] lseek (f, (long) vms.vm_map, SEEK_SET); */ + if (read (f, &vmm, sizeof (struct vm_map)) < 0) + glibtop_error_io_r (server, "read"); + /* Read the pstats [for the RSS rlimit] from kernel memory. */ + lseek (f, (long)pinfo[0].kp_proc.p_stats, SEEK_SET); + if (read (f, &ps, sizeof (struct pstats)) < 0) + glibtop_error_io_r (server, "read"); + 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 = (u_int64_t) (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 = (u_int64_t) 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 = (u_int64_t) 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 = (u_int64_t) 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; } diff --git a/sysdeps/freebsd/procsegment.c b/sysdeps/freebsd/procsegment.c index 0f087b21..584b80d8 100644 --- a/sysdeps/freebsd/procsegment.c +++ b/sysdeps/freebsd/procsegment.c @@ -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 @@ -25,7 +25,18 @@ #include <glibtop_suid.h> -static const unsigned long _glibtop_sysdeps_proc_segment = 0; +#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) */ /* Init function. */ @@ -38,10 +49,40 @@ glibtop_init_proc_segment_p (glibtop *server) /* Provides detailed information about a process. */ void -glibtop_get_proc_segment_p (glibtop *server, glibtop_proc_segment *buf, +glibtop_get_proc_segment_p (glibtop *server, + glibtop_proc_segment *buf, pid_t pid) { + struct kinfo_proc *pinfo; + int *count; + glibtop_init_p (server, GLIBTOP_SYSDEPS_PROC_SEGMENT, 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 + */ } + diff --git a/sysdeps/freebsd/procsignal.c b/sysdeps/freebsd/procsignal.c index 63b9ccad..d72f80f1 100644 --- a/sysdeps/freebsd/procsignal.c +++ b/sysdeps/freebsd/procsignal.c @@ -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 @@ -25,7 +25,11 @@ #include <glibtop_suid.h> -static const unsigned long _glibtop_sysdeps_proc_signal = 0; +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); /* Init function. */ @@ -35,13 +39,34 @@ glibtop_init_proc_signal_p (glibtop *server) server->sysdeps.proc_signal = _glibtop_sysdeps_proc_signal; } -/* Provides detailed information about a process. */ - void -glibtop_get_proc_signal_p (glibtop *server, glibtop_proc_signal *buf, +glibtop_get_proc_signal_p (glibtop *server, + glibtop_proc_signal *buf, pid_t pid) { + struct kinfo_proc *pinfo; + glibtop_init_p (server, GLIBTOP_SYSDEPS_PROC_SIGNAL, 0); memset (buf, 0, sizeof (glibtop_proc_signal)); + + /* 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; } diff --git a/sysdeps/freebsd/procstate.c b/sysdeps/freebsd/procstate.c index c33e3a9b..cf8c94d4 100644 --- a/sysdeps/freebsd/procstate.c +++ b/sysdeps/freebsd/procstate.c @@ -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 @@ -25,7 +25,11 @@ #include <glibtop_suid.h> -static const unsigned long _glibtop_sysdeps_proc_state = 0; +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); /* Init function. */ @@ -38,10 +42,14 @@ glibtop_init_proc_state_p (glibtop *server) /* Provides detailed information about a process. */ void -glibtop_get_proc_state_p (glibtop *server, glibtop_proc_state *buf, +glibtop_get_proc_state_p (glibtop *server, + glibtop_proc_state *buf, pid_t pid) { glibtop_init_p (server, GLIBTOP_SYSDEPS_PROC_STATE, 0); memset (buf, 0, sizeof (glibtop_proc_state)); + + /* Set the flags for the data we're about to return*/ + buf->flags = _glibtop_sysdeps_proc_state; } |