diff options
author | Jasper Lievisse Adriaanse <jasper@humppa.nl> | 2011-06-16 12:27:00 +0200 |
---|---|---|
committer | Jasper Lievisse Adriaanse <jasper@humppa.nl> | 2011-06-16 12:27:00 +0200 |
commit | 46706961ac8dcb27a063506b4a178419bb1fd1e3 (patch) | |
tree | 63395d38098970e57541a13708cfbe47f8ea2bdc | |
parent | edda30a4adf4f2501ee0de00c7df7341240bd630 (diff) | |
download | libgtop-46706961ac8dcb27a063506b4a178419bb1fd1e3.tar.gz |
Split off OpenBSD code into a separate implementation
Move OpenBSD code into its own sysdeps directory. The changes from
bsd/ were becoming to hard to maintain as a #ifdef tree. This is
what's currently being distributed as part of OpenBSD ports.
40 files changed, 3600 insertions, 2 deletions
diff --git a/configure.in b/configure.in index cb87a2bf..ad20de2f 100644 --- a/configure.in +++ b/configure.in @@ -385,6 +385,7 @@ sysdeps/aix/Makefile sysdeps/bsd/Makefile sysdeps/darwin/Makefile sysdeps/cygwin/Makefile +sysdeps/openbsd/Makefile src/Makefile src/daemon/Makefile lib/Makefile diff --git a/libgtop-sysdeps.m4 b/libgtop-sysdeps.m4 index e960caee..7ef66395 100644 --- a/libgtop-sysdeps.m4 +++ b/libgtop-sysdeps.m4 @@ -69,12 +69,18 @@ AC_DEFUN([GNOME_LIBGTOP_SYSDEPS],[ libgtop_sysdeps_private_mountlist=yes libgtop_sysdeps_private_fsusage=yes ;; - netbsd*|openbsd*|bsdi*) + netbsd*|bsdi*) libgtop_sysdeps_dir=bsd libgtop_use_machine_h=yes libgtop_need_server=yes libgtop_postinstall='chgrp kmem $(bindir)/libgtop_server2 && chmod 2755 $(bindir)/libgtop_server2' ;; + openbsd*) + libgtop_sysdeps_dir=openbsd + libgtop_use_machine_h=yes + libgtop_need_server=yes + libgtop_postinstall='chgrp kmem $(bindir)/libgtop_server2 && chmod 2555 $(bindir)/libgtop_server2' + ;; freebsd*|kfreebsd*) libgtop_sysdeps_dir=freebsd libgtop_use_machine_h=yes diff --git a/sysdeps/Makefile.am b/sysdeps/Makefile.am index 7057f1f3..163c0087 100644 --- a/sysdeps/Makefile.am +++ b/sysdeps/Makefile.am @@ -2,4 +2,5 @@ SUBDIRS = common @sysdeps_dir@ DIST_SUBDIRS = bsd common linux osf1 \ - stub stub_suid sun4 freebsd solaris aix darwin cygwin + stub stub_suid sun4 freebsd solaris aix darwin \ + cygwin openbsd diff --git a/sysdeps/openbsd/AUTHORS b/sysdeps/openbsd/AUTHORS new file mode 100644 index 00000000..2b0f17f1 --- /dev/null +++ b/sysdeps/openbsd/AUTHORS @@ -0,0 +1,7 @@ +Martin Baulig (martin@home-of-linux.org) +Josh Sled (jsled@scam.XCF.Berkeley.EDU) + +Contributions/bugfixes by: +Antoine Jacoutot <ajacoutot@openbsd.org> +Ariane van der Steldt <ariane@stack.nl> +Jasper Lievisse Adriaanse <jasper@openbsd.org> diff --git a/sysdeps/openbsd/Makefile.am b/sysdeps/openbsd/Makefile.am new file mode 100644 index 00000000..c21ac2fc --- /dev/null +++ b/sysdeps/openbsd/Makefile.am @@ -0,0 +1,25 @@ +# $OpenBSD: Makefile.am,v 1.2 2011/05/23 19:35:52 jasper Exp $ + +INCLUDES = @INCLUDES@ + +noinst_LTLIBRARIES = libgtop_sysdeps-2.0.la libgtop_sysdeps_suid-2.0.la + +libgtop_sysdeps_2_0_la_SOURCES = nosuid.c siglist.c sysinfo.c + +libgtop_sysdeps_2_0_la_LDFLAGS = $(LT_VERSION_INFO) + +libgtop_sysdeps_suid_2_0_la_LIBADD = $(KVM_LIBS) +libgtop_sysdeps_suid_2_0_la_SOURCES = 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 procargs.c procmap.c netlist.c \ + netload.c ppp.c procopenfiles.c fsusage.c \ + procwd.c procaffinity.c glibtop_private.c + +libgtop_sysdeps_suid_2_0_la_LDFLAGS = $(LT_VERSION_INFO) + +libgtopinclude_HEADERS = glibtop_server.h glibtop_machine.h \ + glibtop_suid.h glibtop_private.h + +libgtopincludedir = $(includedir)/libgtop-2.0 diff --git a/sysdeps/openbsd/close.c b/sysdeps/openbsd/close.c new file mode 100644 index 00000000..627cb2f5 --- /dev/null +++ b/sysdeps/openbsd/close.c @@ -0,0 +1,32 @@ +/* $OpenBSD: close.c,v 1.2 2011/05/23 19:35:53 jasper Exp $ */ + +/* Copyright (C) 1998 Joshua Sled + This file is part of LibGTop 1.0. + + Contributed by Joshua Sled <jsled@xcf.berkeley.edu>, July 1998. + + LibGTop is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, + or (at your option) any later version. + + LibGTop is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with LibGTop; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +#include <config.h> +#include <glibtop.h> +#include <glibtop/close.h> + +/* Closes pipe to gtop server. */ + +void +glibtop_close_p (glibtop *server) +{ } diff --git a/sysdeps/openbsd/cpu.c b/sysdeps/openbsd/cpu.c new file mode 100644 index 00000000..a018b726 --- /dev/null +++ b/sysdeps/openbsd/cpu.c @@ -0,0 +1,109 @@ +/* $OpenBSD: cpu.c,v 1.4 2011/05/24 12:37:15 jasper Exp $ */ + +/* Copyright (C) 1998 Joshua Sled + This file is part of LibGTop 1.0. + + Contributed by Joshua Sled <jsled@xcf.berkeley.edu>, July 1998. + + LibGTop is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, + or (at your option) any later version. + + LibGTop is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with LibGTop; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +#include <config.h> +#include <glibtop.h> +#include <glibtop/error.h> +#include <glibtop/cpu.h> + +#include <glibtop_suid.h> + +#include <sys/sched.h> + +static const unsigned long _glibtop_sysdeps_cpu = +(1L << GLIBTOP_CPU_TOTAL) + (1L << GLIBTOP_CPU_USER) + +(1L << GLIBTOP_CPU_NICE) + (1L << GLIBTOP_CPU_SYS) + +(1L << GLIBTOP_CPU_IDLE) + (1L << GLIBTOP_CPU_FREQUENCY) + +(1L << GLIBTOP_CPU_IOWAIT); + +/* MIB array for sysctl */ +static int mib_length=2; +static int mib [] = { CTL_KERN, KERN_CLOCKRATE }; +static int mib2 [] = { CTL_KERN, KERN_CPTIME }; + +/* Init function. */ + +void +_glibtop_init_cpu_p (glibtop *server) +{ + server->sysdeps.cpu = _glibtop_sysdeps_cpu; +} + +/* Provides information about cpu usage. */ + +void +glibtop_get_cpu_p (glibtop *server, glibtop_cpu *buf) +{ + guint64 cpts [CPUSTATES]; + + /* sysctl vars*/ + struct clockinfo ci; + size_t length; + + glibtop_init_p (server, (1L << GLIBTOP_SYSDEPS_CPU), 0); + + memset (buf, 0, sizeof (glibtop_cpu)); + + /* If this fails, the nlist may not be valid. */ + if (server->sysdeps.cpu == 0) + return; + + length = sizeof (cpts); + if (sysctl (mib2, mib_length, cpts, &length, NULL, 0)) { + glibtop_warn_io_r (server, "sysctl"); + return; + } + + /* Get the clockrate data */ + length = sizeof (struct clockinfo); + if (sysctl (mib, mib_length, &ci, &length, NULL, 0)) { + glibtop_warn_io_r (server, "sysctl"); + return; + } + + /* 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 iowait (really just interrupt) time */ + buf->iowait = cpts [CP_INTR]; + + /* 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]; + + /* Set the flags last. */ + buf->flags = _glibtop_sysdeps_cpu; +} diff --git a/sysdeps/openbsd/fsusage.c b/sysdeps/openbsd/fsusage.c new file mode 100644 index 00000000..5ffc4333 --- /dev/null +++ b/sysdeps/openbsd/fsusage.c @@ -0,0 +1,58 @@ +/* $OpenBSD: fsusage.c,v 1.3 2011/05/23 19:35:53 jasper Exp $ */ + +#include <config.h> + +#undef HAVE_SYS_STATVFS_H +#undef STAT_STATVFS + +#include <glibtop.h> +#include <glibtop/error.h> +#include <glibtop/fsusage.h> + +#include <glibtop_suid.h> + +#include <glib.h> + +#include <unistd.h> +#include <sys/param.h> +#if defined (HAVE_SYS_STATVFS_H) +#include <sys/statvfs.h> +#else +#include <sys/mount.h> +#endif + +#include <stdio.h> +#include <string.h> +#include <stdlib.h> + +void +_glibtop_bsd_get_fsusage_read_write(glibtop *server, + glibtop_fsusage *buf, + const char *path); + +void +_glibtop_bsd_get_fsusage_read_write(glibtop *server, + glibtop_fsusage *buf, + const char *path) +{ + int result; +#if defined (STAT_STATVFS) + struct statvfs sfs; +#else + struct statfs sfs; +#endif + +#if defined (STAT_STATVFS) + result = statvfs (path, &sfs); +#else + result = statfs (path, &sfs); +#endif + + if (result == -1) { + return; + } + + buf->read = sfs.f_syncreads + sfs.f_asyncreads; + buf->write = sfs.f_syncwrites + sfs.f_asyncwrites; + buf->flags |= (1 << GLIBTOP_FSUSAGE_READ) | (1 << GLIBTOP_FSUSAGE_WRITE); +} diff --git a/sysdeps/openbsd/glibtop_machine.h b/sysdeps/openbsd/glibtop_machine.h new file mode 100644 index 00000000..6d31bb9b --- /dev/null +++ b/sysdeps/openbsd/glibtop_machine.h @@ -0,0 +1,55 @@ +/* $OpenBSD: glibtop_machine.h,v 1.3 2011/05/23 19:35:53 jasper Exp $ */ + +/* Copyright (C) 1998-99 Martin Baulig + This file is part of LibGTop 1.0. + + Contributed by Martin Baulig <martin@home-of-linux.org>, April 1998. + + LibGTop is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, + or (at your option) any later version. + + LibGTop is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with LibGTop; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +#ifndef __GLIBTOP_MACHINE_H__ +#define __GLIBTOP_MACHINE_H__ + +#include <sys/param.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> + +G_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; +}; + +G_END_DECLS + +#endif /* __GLIBTOP_MACHINE_H__ */ diff --git a/sysdeps/openbsd/glibtop_private.c b/sysdeps/openbsd/glibtop_private.c new file mode 100644 index 00000000..9c0b9067 --- /dev/null +++ b/sysdeps/openbsd/glibtop_private.c @@ -0,0 +1,56 @@ +/* $OpenBSD: glibtop_private.c,v 1.2 2011/05/23 19:35:53 jasper Exp $ */ + +#include <config.h> +#include <glibtop.h> +#include <glibtop/error.h> + +#include "glibtop_private.h" + +#include <sys/types.h> +#include <unistd.h> +#include <string.h> +#include <glib.h> +#include <errno.h> + +char * +execute_lsof(pid_t pid) { + char *output = NULL; + char *lsof; + char *command; + int exit_status; + + lsof = g_find_program_in_path("lsof"); + if (lsof == NULL) + return NULL; + + command = g_strdup_printf("%s -n -P -Fftn -p %d", lsof, pid); + g_free(lsof); + + if (g_spawn_command_line_sync (command, &output, NULL, &exit_status, NULL)) { + if (exit_status != 0) { + g_warning("Could not execute \"%s\" (%i)\nMake sure lsof(8) is installed sgid kmem.", + command, exit_status); + output = NULL; + } + } + + g_free(command); + return output; +} + +/* Ported from linux/glibtop_private.c */ +gboolean +safe_readlink(const char *path, char *buf, int bufsiz) +{ + int ret; + + ret = readlink(path, buf, bufsiz - 1); + + if (ret == -1) { + g_warning("Could not read link %s : %s", path, strerror(errno)); + return FALSE; + } + + buf[ret] = '\0'; + return TRUE; +} diff --git a/sysdeps/openbsd/glibtop_private.h b/sysdeps/openbsd/glibtop_private.h new file mode 100644 index 00000000..5e546382 --- /dev/null +++ b/sysdeps/openbsd/glibtop_private.h @@ -0,0 +1,42 @@ +/* $OpenBSD: glibtop_private.h,v 1.2 2011/05/23 19:35:53 jasper Exp $ */ + +/* Copyright (C) 2007 Joe Marcus Clarke + This file is part of LibGTop 2.0. + + LibGTop is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, + or (at your option) any later version. + + LibGTop is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with LibGTop; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +#ifndef __OPENBSD__GLIBTOP_PRIVATE_H__ +#define __OPENBSD__GLIBTOP_PRIVATE_H__ + +#include <glibtop.h> +#include <glibtop/error.h> + +#include <glib.h> + +#include <sys/types.h> +#include <unistd.h> +#include <string.h> +#include <errno.h> + +G_BEGIN_DECLS + +char *execute_lsof(pid_t pid); +gboolean safe_readlink(const char *path, char *buf, int bufsiz); + +G_END_DECLS + +#endif /* __OPENBSD__GLIBTOP_PRIVATE_H__ */ diff --git a/sysdeps/openbsd/glibtop_server.h b/sysdeps/openbsd/glibtop_server.h new file mode 100644 index 00000000..8d6392d3 --- /dev/null +++ b/sysdeps/openbsd/glibtop_server.h @@ -0,0 +1,55 @@ +/* $OpenBSD: glibtop_server.h,v 1.2 2011/05/23 19:35:53 jasper Exp $ */ + +/* Copyright (C) 1998-99 Martin Baulig + This file is part of LibGTop 1.0. + + Contributed by Martin Baulig <martin@home-of-linux.org>, April 1998. + + LibGTop is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, + or (at your option) any later version. + + LibGTop is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with LibGTop; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +#ifndef __GLIBTOP_SERVER_H__ +#define __GLIBTOP_SERVER_H__ + +G_BEGIN_DECLS + +#define GLIBTOP_SUID_CPU (1 << GLIBTOP_SYSDEPS_CPU) +#define GLIBTOP_SUID_MEM (1 << GLIBTOP_SYSDEPS_MEM) +#define GLIBTOP_SUID_SWAP (1 << GLIBTOP_SYSDEPS_SWAP) +#define GLIBTOP_SUID_UPTIME (1 << GLIBTOP_SYSDEPS_UPTIME) +#define GLIBTOP_SUID_LOADAVG (1 << GLIBTOP_SYSDEPS_LOADAVG) +#define GLIBTOP_SUID_SHM_LIMITS (1 << GLIBTOP_SYSDEPS_SHM_LIMITS) +#define GLIBTOP_SUID_MSG_LIMITS (1 << GLIBTOP_SYSDEPS_MSG_LIMITS) +#define GLIBTOP_SUID_SEM_LIMITS (1 << GLIBTOP_SYSDEPS_SEM_LIMITS) +#define GLIBTOP_SUID_PROCLIST (1 << GLIBTOP_SYSDEPS_PROCLIST) +#define GLIBTOP_SUID_PROC_STATE (1 << GLIBTOP_SYSDEPS_PROC_STATE) +#define GLIBTOP_SUID_PROC_UID (1 << GLIBTOP_SYSDEPS_PROC_UID) +#define GLIBTOP_SUID_PROC_MEM (1 << GLIBTOP_SYSDEPS_PROC_MEM) +#define GLIBTOP_SUID_PROC_TIME (1 << GLIBTOP_SYSDEPS_PROC_TIME) +#define GLIBTOP_SUID_PROC_SIGNAL (1 << GLIBTOP_SYSDEPS_PROC_SIGNAL) +#define GLIBTOP_SUID_PROC_KERNEL (1 << GLIBTOP_SYSDEPS_PROC_KERNEL) +#define GLIBTOP_SUID_PROC_SEGMENT (1 << GLIBTOP_SYSDEPS_PROC_SEGMENT) +#define GLIBTOP_SUID_PROC_ARGS (1 << GLIBTOP_SYSDEPS_PROC_ARGS) +#define GLIBTOP_SUID_PROC_MAP (1 << GLIBTOP_SYSDEPS_PROC_MAP) +#define GLIBTOP_SUID_NETLOAD (1 << GLIBTOP_SYSDEPS_NETLOAD) +#define GLIBTOP_SUID_NETLIST 0 +#define GLIBTOP_SUID_PPP (1 << GLIBTOP_SYSDEPS_PPP) +#define GLIBTOP_SUID_PROC_WD 0 +#define GLIBTOP_SUID_PROC_AFFINITY 0 + +G_END_DECLS + +#endif diff --git a/sysdeps/openbsd/glibtop_suid.h b/sysdeps/openbsd/glibtop_suid.h new file mode 100644 index 00000000..91157190 --- /dev/null +++ b/sysdeps/openbsd/glibtop_suid.h @@ -0,0 +1,58 @@ +/* $OpenBSD: glibtop_suid.h,v 1.2 2011/05/23 19:35:53 jasper Exp $ */ + +/* Copyright (C) 1998-99 Martin Baulig + This file is part of LibGTop 1.0. + + Contributed by Martin Baulig <martin@home-of-linux.org>, April 1998. + + LibGTop is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, + or (at your option) any later version. + + LibGTop is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with LibGTop; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +#ifndef __GLIBTOP_SUID_H__ +#define __GLIBTOP_SUID_H__ + +G_BEGIN_DECLS + +#if _IN_LIBGTOP +#include <sys/param.h> +#endif + +#define KI_PROC(ki) (&(ki))->kp_proc) +#define KI_EPROC(ki) (&(ki))->kp_eproc) + +#define FORCEUREAD 1 +#define UREADOK(ki) (FORCEUREAD || (KI_PROC(ki)->p_flag & P_INMEM)) + +static inline void glibtop_suid_enter (glibtop *server) { + setregid (server->machine.gid, server->machine.egid); +}; + +static inline void glibtop_suid_leave (glibtop *server) { + if (setregid (server->machine.egid, server->machine.gid)) + _exit (1); +}; + +void +glibtop_init_p (glibtop *server, const unsigned long features, + const unsigned flags); +void +glibtop_open_p (glibtop *server, const char *program_name, + const unsigned long features, + const unsigned flags); + +G_END_DECLS + +#endif diff --git a/sysdeps/openbsd/loadavg.c b/sysdeps/openbsd/loadavg.c new file mode 100644 index 00000000..5d20aee1 --- /dev/null +++ b/sysdeps/openbsd/loadavg.c @@ -0,0 +1,61 @@ +/* $OpenBSD: loadavg.c,v 1.2 2011/05/23 19:35:53 jasper Exp $ */ + +/* Copyright (C) 1998 Joshua Sled + This file is part of LibGTop 1.0. + + Contributed by Joshua Sled <jsled@xcf.berkeley.edu>, July 1998. + + LibGTop is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, + or (at your option) any later version. + + LibGTop is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with LibGTop; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +#include <config.h> +#include <glibtop.h> +#include <glibtop/error.h> +#include <glibtop/loadavg.h> + +#include <glibtop_suid.h> + +static const unsigned long _glibtop_sysdeps_loadavg = +(1L << GLIBTOP_LOADAVG_LOADAVG); + +/* Init function. */ + +void +_glibtop_init_loadavg_p (glibtop *server) +{ + server->sysdeps.loadavg = _glibtop_sysdeps_loadavg; +} + +/* Provides load averange. */ + +void +glibtop_get_loadavg_p (glibtop *server, glibtop_loadavg *buf) +{ + double ldavg[3]; + int i; + + glibtop_init_p (server, (1L << 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/openbsd/mem.c b/sysdeps/openbsd/mem.c new file mode 100644 index 00000000..3d90b97f --- /dev/null +++ b/sysdeps/openbsd/mem.c @@ -0,0 +1,141 @@ +/* $OpenBSD: mem.c,v 1.8 2011/05/26 08:07:22 jasper Exp $ */ + +/* Copyright (C) 1998 Joshua Sled + This file is part of LibGTop 1.0. + + Contributed by Joshua Sled <jsled@xcf.berkeley.edu>, July 1998. + + LibGTop is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, + or (at your option) any later version. + + LibGTop is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with LibGTop; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +#include <config.h> +#include <glibtop.h> +#include <glibtop/error.h> +#include <glibtop/mem.h> + +#include <glibtop_suid.h> + +#include <sys/sysctl.h> +#include <sys/vmmeter.h> +#include <uvm/uvm_extern.h> +#include <uvm/uvm_param.h> + +static const unsigned long _glibtop_sysdeps_mem = +(1L << GLIBTOP_MEM_TOTAL) + (1L << GLIBTOP_MEM_USED) + +(1L << GLIBTOP_MEM_FREE) + +(1L << GLIBTOP_MEM_SHARED) + +(1L << GLIBTOP_MEM_BUFFER) + +(1L << GLIBTOP_MEM_USER) + (1L << GLIBTOP_MEM_LOCKED); + +#ifndef LOG1024 +#define LOG1024 10 +#endif + +/* these are for getting the memory statistics */ +static int pageshift; /* log base 2 of the pagesize */ + +/* define pagetok in terms of pageshift */ +#define pagetok(size) ((size) << pageshift) + +/* nlist structure for kernel access */ +static struct nlist nlst [] = { + { "_bufpages" }, + { 0 } +}; + +/* MIB array for sysctl */ +static int mib [] = { CTL_VM, VM_METER }; +static int mib_uvmexp [] = { CTL_VM, VM_UVMEXP }; + +/* Init function. */ + +void +_glibtop_init_mem_p (glibtop *server) +{ + register int pagesize; + + if (kvm_nlist (server->machine.kd, nlst) < 0) { + glibtop_warn_io_r (server, "kvm_nlist (mem)"); + return; + } + + /* get the page size and calculate pageshift from it */ + pagesize = sysconf(_SC_PAGESIZE); + pageshift = 0; + while (pagesize > 1) { + pageshift++; + pagesize >>= 1; + } + + /* we only need the amount of log(2)1024 for our conversion */ + pageshift -= LOG1024; + + server->sysdeps.mem = _glibtop_sysdeps_mem; +} + +void +glibtop_get_mem_p (glibtop *server, glibtop_mem *buf) +{ + struct vmtotal vmt; + size_t length_vmt; + struct uvmexp uvmexp; + size_t length_uvmexp; + u_int v_used_count; + u_int v_total_count; + u_int v_free_count; + + glibtop_init_p (server, (1L << GLIBTOP_SYSDEPS_MEM), 0); + + memset (buf, 0, sizeof (glibtop_mem)); + + if (server->sysdeps.mem == 0) + return; + + /* Get the data from sysctl */ + length_vmt = sizeof (vmt); + if (sysctl (mib, 2, &vmt, &length_vmt, NULL, 0)) { + glibtop_warn_io_r (server, "sysctl (vm.meter)"); + return; + } + + length_uvmexp = sizeof (uvmexp); + if (sysctl (mib_uvmexp, 2, &uvmexp, &length_uvmexp, NULL, 0)) { + glibtop_warn_io_r (server, "sysctl (uvmexp)"); + return; + } + + /* + * t_arm = active real memory + * t_rm = total real memory in use + * t_free = free memory pages + */ + v_total_count = vmt.t_rm + vmt.t_free; + v_used_count = vmt.t_rm; + v_free_count = vmt.t_free; + + /* convert memory stats to Kbytes */ + buf->total = (guint64) pagetok (v_total_count) << LOG1024; + buf->used = (guint64) pagetok (v_used_count) << LOG1024; + buf->free = (guint64) pagetok (v_free_count) << LOG1024; + buf->locked = (guint64) pagetok (uvmexp.wired) << LOG1024; + buf->shared = (guint64) pagetok (vmt.t_rmshr) << LOG1024; + buf->buffer = 0; + + buf->user = buf->total - buf->free - buf->shared - buf->buffer; + + /* Set the values to return */ + buf->flags = _glibtop_sysdeps_mem; +} diff --git a/sysdeps/openbsd/msg_limits.c b/sysdeps/openbsd/msg_limits.c new file mode 100644 index 00000000..6bc122c2 --- /dev/null +++ b/sysdeps/openbsd/msg_limits.c @@ -0,0 +1,96 @@ +/* $OpenBSD: msg_limits.c,v 1.3 2011/05/23 19:35:54 jasper Exp $ */ + +/* Copyright (C) 1998-99 Martin Baulig + This file is part of LibGTop 1.0. + + Contributed by Martin Baulig <martin@home-of-linux.org>, August 1998. + + LibGTop is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, + or (at your option) any later version. + + LibGTop is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with LibGTop; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +#include <config.h> +#include <glibtop.h> +#include <glibtop/error.h> +#include <glibtop/msg_limits.h> + +#include <glibtop_suid.h> + +/* Define the appropriate macro (if any) to get declaration of `struct + * msginfo'. Needed on, at least, FreeBSD. */ +#if defined (STRUCT_MSGINFO_NEEDS_KERNEL) +#define KERNEL 1 +#elif defined (STRUCT_MSGINFO_NEEDS__KERNEL) +#define _KERNEL 1 +#endif + +#include <sys/ipc.h> +#include <sys/msg.h> + +static const unsigned long _glibtop_sysdeps_msg_limits = +(1L << GLIBTOP_IPC_MSGMAX) + (1L << GLIBTOP_IPC_MSGMNI) + +(1L << GLIBTOP_IPC_MSGMNB) + (1L << GLIBTOP_IPC_MSGTQL) + +(1L << GLIBTOP_IPC_MSGSSZ); + +/* The values in this structure never change at runtime, so we only + * read it once during initialization. We have to use the name `_msginfo' + * since `msginfo' is already declared external in <sys/msg.h>. */ +static struct msginfo _msginfo; + +/* nlist structure for kernel access */ +static struct nlist nlst [] = { + { "_msginfo" }, + { 0 } +}; + +/* Init function. */ + +void +_glibtop_init_msg_limits_p (glibtop *server) +{ + if (kvm_nlist (server->machine.kd, nlst) < 0) { + glibtop_warn_io_r (server, "kvm_nlist (msg_limits)"); + return; + } + + if (kvm_read (server->machine.kd, nlst [0].n_value, + &_msginfo, sizeof (_msginfo)) != sizeof (_msginfo)) { + glibtop_warn_io_r (server, "kvm_read (msginfo)"); + return; + } + + server->sysdeps.msg_limits = _glibtop_sysdeps_msg_limits; +} + +/* Provides information about sysv ipc limits. */ + +void +glibtop_get_msg_limits_p (glibtop *server, glibtop_msg_limits *buf) +{ + glibtop_init_p (server, (1L << GLIBTOP_SYSDEPS_MSG_LIMITS), 0); + + memset (buf, 0, sizeof (glibtop_msg_limits)); + + if (server->sysdeps.msg_limits == 0) + return; + + buf->msgmax = _msginfo.msgmax; + buf->msgmni = _msginfo.msgmni; + buf->msgmnb = _msginfo.msgmnb; + buf->msgtql = _msginfo.msgtql; + buf->msgssz = _msginfo.msgtql; + + buf->flags = _glibtop_sysdeps_msg_limits; +} diff --git a/sysdeps/openbsd/netlist.c b/sysdeps/openbsd/netlist.c new file mode 100644 index 00000000..cb584c21 --- /dev/null +++ b/sysdeps/openbsd/netlist.c @@ -0,0 +1,68 @@ +/* $OpenBSD: netlist.c,v 1.2 2011/05/23 19:35:54 jasper Exp $ */ + +/* + This file is part of LibGTop 2.0. + + LibGTop is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, + or (at your option) any later version. + + LibGTop is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with LibGTop; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +#include <config.h> +#include <glibtop/netlist.h> +#include <glibtop/error.h> + +#include <net/if.h> + + +static const unsigned long _glibtop_sysdeps_netlist = (1 << GLIBTOP_NETLIST_NUMBER); + +/* Init function. */ + +void +_glibtop_init_netlist_s (glibtop *server) +{ + server->sysdeps.netlist = _glibtop_sysdeps_netlist; +} + + +char** +glibtop_get_netlist_s (glibtop *server, glibtop_netlist *buf) +{ + struct if_nameindex *ifstart, *ifs; + GPtrArray *devices; + + glibtop_init_s (&server, GLIBTOP_SYSDEPS_NETLIST, 0); + + memset (buf, 0, sizeof (glibtop_netlist)); + + ifs = ifstart = if_nameindex(); + + devices = g_ptr_array_new(); + + while(ifs && ifs->if_name) { + g_ptr_array_add(devices, g_strdup(ifs->if_name)); + buf->number++; + ifs++; + } + + if_freenameindex(ifstart); + + buf->flags = _glibtop_sysdeps_netlist; + + g_ptr_array_add(devices, NULL); + + return (char **) g_ptr_array_free(devices, FALSE); +} + diff --git a/sysdeps/openbsd/netload.c b/sysdeps/openbsd/netload.c new file mode 100644 index 00000000..579f951a --- /dev/null +++ b/sysdeps/openbsd/netload.c @@ -0,0 +1,210 @@ +/* $OpenBSD: netload.c,v 1.3 2011/05/23 19:35:54 jasper Exp $ */ + +/* Copyright (C) 1998-99 Martin Baulig + This file is part of LibGTop 1.0. + + Contributed by Martin Baulig <martin@home-of-linux.org>, October 1998. + + LibGTop is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, + or (at your option) any later version. + + LibGTop is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with LibGTop; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +#include <config.h> +#include <glibtop.h> +#include <glibtop/error.h> +#include <glibtop/netload.h> + +#include <glibtop_suid.h> + +#include <string.h> + +#include <net/if.h> +#include <net/if_dl.h> +#include <net/if_types.h> + +#ifdef HAVE_NET_IF_VAR_H +#include <net/if_var.h> +#endif + +#include <netinet/in.h> +#include <netinet/in_var.h> + +static const unsigned long _glibtop_sysdeps_netload = +(1L << GLIBTOP_NETLOAD_IF_FLAGS) + +(1L << GLIBTOP_NETLOAD_PACKETS_IN) + +(1L << GLIBTOP_NETLOAD_PACKETS_OUT) + +(1L << GLIBTOP_NETLOAD_PACKETS_TOTAL) + +(1L << GLIBTOP_NETLOAD_BYTES_IN) + +(1L << GLIBTOP_NETLOAD_BYTES_OUT) + +(1L << GLIBTOP_NETLOAD_BYTES_TOTAL) + +(1L << GLIBTOP_NETLOAD_ERRORS_IN) + +(1L << GLIBTOP_NETLOAD_ERRORS_OUT) + +(1L << GLIBTOP_NETLOAD_ERRORS_TOTAL) + +(1L << GLIBTOP_NETLOAD_COLLISIONS); + +static const unsigned _glibtop_sysdeps_netload_data = +(1L << GLIBTOP_NETLOAD_ADDRESS) + +(1L << GLIBTOP_NETLOAD_SUBNET) + +(1L << GLIBTOP_NETLOAD_MTU); + +/* nlist structure for kernel access */ +static struct nlist nlst [] = { + { "_ifnet" }, + { 0 } +}; + +/* Init function. */ + +void +_glibtop_init_netload_p (glibtop *server) +{ + server->sysdeps.netload = _glibtop_sysdeps_netload; + + if (kvm_nlist (server->machine.kd, nlst) < 0) + glibtop_error_io_r (server, "kvm_nlist"); +} + +/* Provides Network statistics. */ + +void +glibtop_get_netload_p (glibtop *server, glibtop_netload *buf, + const char *interface) +{ + struct ifnet ifnet; + u_long ifnetaddr, ifnetfound; + struct sockaddr *sa = NULL; + char name [32]; + + union { + struct ifaddr ifa; + struct in_ifaddr in; + } ifaddr; + + glibtop_init_p (server, (1L << GLIBTOP_SYSDEPS_NETLOAD), 0); + + memset (buf, 0, sizeof (glibtop_netload)); + + if (kvm_read (server->machine.kd, nlst [0].n_value, + &ifnetaddr, sizeof (ifnetaddr)) != sizeof (ifnetaddr)) + glibtop_error_io_r (server, "kvm_read (ifnet)"); + + while (ifnetaddr) { + struct sockaddr_in *sin; + register char *cp; + u_long ifaddraddr; + + { + ifnetfound = ifnetaddr; + + if (kvm_read (server->machine.kd, ifnetaddr, &ifnet, + sizeof (ifnet)) != sizeof (ifnet)) + glibtop_error_io_r (server, "kvm_read (ifnetaddr)"); + + g_strlcpy (name, ifnet.if_xname, sizeof(name)); + ifnetaddr = (u_long) ifnet.if_list.tqe_next; + + if (strcmp (name, interface) != 0) + continue; + + ifaddraddr = (u_long) ifnet.if_addrlist.tqh_first; + } + if (ifnet.if_flags & IFF_UP) + buf->if_flags |= (1L << GLIBTOP_IF_FLAGS_UP); + if (ifnet.if_flags & IFF_BROADCAST) + buf->if_flags |= (1L << GLIBTOP_IF_FLAGS_BROADCAST); + if (ifnet.if_flags & IFF_DEBUG) + buf->if_flags |= (1L << GLIBTOP_IF_FLAGS_DEBUG); + if (ifnet.if_flags & IFF_LOOPBACK) + buf->if_flags |= (1L << GLIBTOP_IF_FLAGS_LOOPBACK); + if (ifnet.if_flags & IFF_POINTOPOINT) + buf->if_flags |= (1L << GLIBTOP_IF_FLAGS_POINTOPOINT); +#ifdef IFF_DRV_RUNNING + if (ifnet.if_drv_flags & IFF_DRV_RUNNING) +#else + if (ifnet.if_flags & IFF_RUNNING) +#endif + buf->if_flags |= (1L << GLIBTOP_IF_FLAGS_RUNNING); + if (ifnet.if_flags & IFF_NOARP) + buf->if_flags |= (1L << GLIBTOP_IF_FLAGS_NOARP); + if (ifnet.if_flags & IFF_PROMISC) + buf->if_flags |= (1L << GLIBTOP_IF_FLAGS_PROMISC); + if (ifnet.if_flags & IFF_ALLMULTI) + buf->if_flags |= (1L << GLIBTOP_IF_FLAGS_ALLMULTI); +#ifdef IFF_DRV_OACTIVE + if (ifnet.if_drv_flags & IFF_DRV_OACTIVE) +#else + if (ifnet.if_flags & IFF_OACTIVE) +#endif + buf->if_flags |= (1L << GLIBTOP_IF_FLAGS_OACTIVE); + if (ifnet.if_flags & IFF_SIMPLEX) + buf->if_flags |= (1L << GLIBTOP_IF_FLAGS_SIMPLEX); + if (ifnet.if_flags & IFF_LINK0) + buf->if_flags |= (1L << GLIBTOP_IF_FLAGS_LINK0); + if (ifnet.if_flags & IFF_LINK1) + buf->if_flags |= (1L << GLIBTOP_IF_FLAGS_LINK1); + if (ifnet.if_flags & IFF_LINK2) + buf->if_flags |= (1L << GLIBTOP_IF_FLAGS_LINK2); + if (ifnet.if_flags & IFF_MULTICAST) + buf->if_flags |= (1L << GLIBTOP_IF_FLAGS_MULTICAST); + + buf->packets_in = ifnet.if_ipackets; + buf->packets_out = ifnet.if_opackets; + buf->packets_total = buf->packets_in + buf->packets_out; + + buf->bytes_in = ifnet.if_ibytes; + buf->bytes_out = ifnet.if_obytes; + buf->bytes_total = buf->bytes_in + buf->bytes_out; + + buf->errors_in = ifnet.if_ierrors; + buf->errors_out = ifnet.if_oerrors; + buf->errors_total = buf->errors_in + buf->errors_out; + + buf->collisions = ifnet.if_collisions; + buf->flags = _glibtop_sysdeps_netload; + + while (ifaddraddr) { + if ((kvm_read (server->machine.kd, ifaddraddr, &ifaddr, + sizeof (ifaddr)) != sizeof (ifaddr))) + glibtop_error_io_r (server, "kvm_read (ifaddraddr)"); + +#define CP(x) ((char *)(x)) + cp = (CP(ifaddr.ifa.ifa_addr) - CP(ifaddraddr)) + + CP(&ifaddr); + sa = (struct sockaddr *)cp; + + if (sa->sa_family == AF_LINK) { + struct sockaddr_dl *dl = (struct sockaddr_dl *) sa; + + memcpy (buf->hwaddress, LLADDR (dl), sizeof (buf->hwaddress)); + buf->flags |= GLIBTOP_NETLOAD_HWADDRESS; + } else if (sa->sa_family == AF_INET) { + sin = (struct sockaddr_in *)sa; + buf->subnet = ifaddr.in.ia_netmask; + buf->address = sin->sin_addr.s_addr; + buf->mtu = ifnet.if_mtu; + + buf->flags |= _glibtop_sysdeps_netload_data; + } else if (sa->sa_family == AF_INET6) { + struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *) sa; + + memcpy (buf->address6, &sin6->sin6_addr, sizeof (buf->address6)); + buf->flags |= GLIBTOP_NETLOAD_ADDRESS6; + } + /* FIXME prefix6, scope6 */ + ifaddraddr = (u_long) ifaddr.ifa.ifa_list.tqe_next; + } + return; + } +} diff --git a/sysdeps/openbsd/nosuid.c b/sysdeps/openbsd/nosuid.c new file mode 100644 index 00000000..81eccb60 --- /dev/null +++ b/sysdeps/openbsd/nosuid.c @@ -0,0 +1,38 @@ +/* $OpenBSD: nosuid.c,v 1.2 2011/05/23 19:35:54 jasper Exp $ */ + +/* Copyright (C) 1998-99 Martin Baulig + This file is part of LibGTop 1.0. + + Contributed by Martin Baulig <martin@home-of-linux.org>, April 1998. + + LibGTop is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, + or (at your option) any later version. + + LibGTop is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with LibGTop; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +#include <config.h> +#include <glibtop.h> +#include <glibtop/open.h> +#include <glibtop/close.h> + +void +glibtop_open_s (glibtop *server, + const char *program_name, + const unsigned long features, + const unsigned flags) +{ } + +void +glibtop_close_s (glibtop *server) +{ } diff --git a/sysdeps/openbsd/open.c b/sysdeps/openbsd/open.c new file mode 100644 index 00000000..d24bdd19 --- /dev/null +++ b/sysdeps/openbsd/open.c @@ -0,0 +1,95 @@ +/* $OpenBSD: open.c,v 1.4 2011/05/23 19:35:54 jasper Exp $ */ + +/* Copyright (C) 1998 Joshua Sled + This file is part of LibGTop 1.0. + + Contributed by Joshua Sled <jsled@xcf.berkeley.edu>, July 1998. + + LibGTop is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, + or (at your option) any later version. + + LibGTop is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with LibGTop; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +#include <config.h> +#include <glibtop.h> +#include <glibtop/error.h> +#include <glibtop/open.h> +#include <glibtop/init_hooks.h> + +/* !!! THIS FUNCTION RUNS SUID ROOT - CHANGE WITH CAUTION !!! */ + +void +glibtop_init_p (glibtop *server, const unsigned long features, + const unsigned flags) +{ + const _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_SYSDEPS) == 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_SYSDEPS; + } +} + +void +glibtop_open_p (glibtop *server, const char *program_name, + const unsigned long features, + const unsigned flags) +{ +#ifdef DEBUG + fprintf (stderr, "DEBUG (%d): glibtop_open_p ()\n", getpid ()); +#endif + + /* !!! WE ARE ROOT HERE - CHANGE WITH CAUTION !!! */ + + server->machine.uid = getuid (); + server->machine.euid = geteuid (); + server->machine.gid = getgid (); + server->machine.egid = getegid (); + + server->os_version_code = OpenBSD; + + /* 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/openbsd/ppp.c b/sysdeps/openbsd/ppp.c new file mode 100644 index 00000000..e1ac9887 --- /dev/null +++ b/sysdeps/openbsd/ppp.c @@ -0,0 +1,141 @@ +/* $OpenBSD: ppp.c,v 1.3 2011/05/23 19:35:54 jasper Exp $ */ + +/* Copyright (C) 1998-99 Martin Baulig + This file is part of LibGTop 1.0. + + Contributed by Martin Baulig <martin@home-of-linux.org>, October 1998. + + LibGTop is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, + or (at your option) any later version. + + LibGTop is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with LibGTop; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +#include <config.h> +#include <glibtop.h> +#include <glibtop/error.h> +#include <glibtop/ppp.h> + +#include <glibtop_suid.h> + +#ifdef HAVE_I4B + +#include <net/if.h> +#include <net/if_types.h> + +#ifdef HAVE_NET_IF_VAR_H +#include <net/if_var.h> +#endif + +#include <net/netisr.h> +#include <net/route.h> + +#include <net/if_sppp.h> + +/* Read `misc/i4b_acct.txt' for details ... */ +#ifdef HAVE_I4B_ACCT +#include <machine/i4b_acct.h> +#endif + +static const unsigned long _glibtop_sysdeps_ppp = +(1L << GLIBTOP_PPP_STATE); + +#ifdef HAVE_I4B_ACCT +static const unsigned long _glibtop_sysdeps_ppp_acct = +(1L << GLIBTOP_PPP_BYTES_IN) + (1L << GLIBTOP_PPP_BYTES_OUT); +#endif + +#endif /* HAVE_I4B */ + +/* nlist structure for kernel access */ +static struct nlist nlst [] = { +#ifdef HAVE_I4B + { "_i4bisppp_softc" }, +#endif + { 0 } +}; + +/* Init function. */ + +void +_glibtop_init_ppp_p (glibtop *server) +{ +#ifdef HAVE_I4B +#ifdef HAVE_I4B_ACCT + server->sysdeps.ppp = _glibtop_sysdeps_ppp | + _glibtop_sysdeps_ppp_acct; +#else + server->sysdeps.ppp = _glibtop_sysdeps_ppp; +#endif +#endif /* HAVE_I4B */ + + if (kvm_nlist (server->machine.kd, nlst) < 0) + glibtop_error_io_r (server, "kvm_nlist"); +} + +/* Provides information about ppp usage. */ + +void +glibtop_get_ppp_p (glibtop *server, glibtop_ppp *buf, unsigned short device) +{ +#ifdef HAVE_I4B +#ifdef HAVE_I4B_ACCT + struct i4bisppp_softc data; +#else + struct sppp data; +#endif + int phase; + + glibtop_init_p (server, (1L << GLIBTOP_SYSDEPS_PPP), 0); + + memset (buf, 0, sizeof (glibtop_ppp)); + + if (kvm_read (server->machine.kd, nlst [0].n_value, + &data, sizeof (data)) != sizeof (data)) + glibtop_error_io_r (server, "kvm_read (i4bisppp_softc)"); + +#ifdef HAVE_I4B_ACCT + phase = data.sc_if_un.scu_sp.pp_phase; +#else + /* FIXME: Which FreeBSD version have this field and + * which not. */ +#if 0 + phase = data.pp_phase; +#endif +#endif + + switch (phase) { +#ifdef HAVE_I4B_ACCT + case PHASE_DEAD: + case PHASE_TERMINATE: + buf->state = GLIBTOP_PPP_STATE_HANGUP; + break; + case PHASE_ESTABLISH: + case PHASE_NETWORK: + buf->state = GLIBTOP_PPP_STATE_ONLINE; + break; +#endif + default: + buf->state = GLIBTOP_PPP_STATE_UNKNOWN; + break; + } + + buf->flags = _glibtop_sysdeps_ppp; + +#ifdef HAVE_I4B_ACCT + buf->bytes_in = data.sc_inb; + buf->bytes_out = data.sc_outb; + buf->flags |= _glibtop_sysdeps_ppp_acct; +#endif +#endif /* HAVE_I4B */ +} diff --git a/sysdeps/openbsd/procaffinity.c b/sysdeps/openbsd/procaffinity.c new file mode 100644 index 00000000..dcb27eff --- /dev/null +++ b/sysdeps/openbsd/procaffinity.c @@ -0,0 +1,27 @@ +/* + * $OpenBSD: procaffinity.c,v 1.1 2009/10/16 10:56:04 jasper Exp $ + * procaffinity stub. + */ + +#include <config.h> +#include <glibtop/procaffinity.h> +#include <glibtop/error.h> + + +void +_glibtop_init_proc_affinity_s(glibtop *server) +{ + server->sysdeps.proc_affinity = + (1 << GLIBTOP_PROC_AFFINITY_NUMBER) | + (1 << GLIBTOP_PROC_AFFINITY_ALL); + +} + + +guint16 * +glibtop_get_proc_affinity_s(glibtop *server, glibtop_proc_affinity *buf, pid_t pid) +{ + memset(buf, 0, sizeof *buf); + + return NULL; +} diff --git a/sysdeps/openbsd/procargs.c b/sysdeps/openbsd/procargs.c new file mode 100644 index 00000000..900a3a2b --- /dev/null +++ b/sysdeps/openbsd/procargs.c @@ -0,0 +1,101 @@ +/* $OpenBSD: procargs.c,v 1.3 2011/05/23 19:35:54 jasper Exp $ */ + +/* Copyright (C) 1998 Joshua Sled + This file is part of LibGTop 1.0. + + Contributed by Joshua Sled <jsled@xcf.berkeley.edu>, July 1998. + + LibGTop is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, + or (at your option) any later version. + + LibGTop is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with LibGTop; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +#include <config.h> +#include <glibtop.h> +#include <glibtop/error.h> +#include <glibtop/procargs.h> + +#include <glibtop_suid.h> + +#include <kvm.h> +#include <sys/param.h> +#include <sys/proc.h> + +static const unsigned long _glibtop_sysdeps_proc_args = +(1L << GLIBTOP_PROC_ARGS_SIZE); + +/* Init function. */ + +void +_glibtop_init_proc_args_p (glibtop *server) +{ + server->sysdeps.proc_args = _glibtop_sysdeps_proc_args; +} + +/* Provides detailed information about a process. */ + +char * +glibtop_get_proc_args_p (glibtop *server, glibtop_proc_args *buf, + pid_t pid, unsigned max_len) +{ + struct kinfo_proc2 *pinfo; + char *retval, **args, **ptr; + size_t size = 0, pos = 0; + int count; + + glibtop_init_p (server, (1L << GLIBTOP_SYSDEPS_PROC_ARGS), 0); + + memset (buf, 0, sizeof (glibtop_proc_args)); + + /* swapper, init, pagedaemon, vmdaemon, update - this doen't work. */ + if (pid < 5) return NULL; + + glibtop_suid_enter (server); + + /* Get the process data */ + pinfo = kvm_getproc2 (server->machine.kd, KERN_PROC_PID, pid, + sizeof (*pinfo), &count); + if ((pinfo == NULL) || (count < 1)) { + glibtop_suid_leave (server); + glibtop_warn_io_r (server, "kvm_getprocs (%d)", pid); + return NULL; + } + + args = kvm_getargv2 (server->machine.kd, pinfo, max_len); + if (args == NULL) { + glibtop_suid_leave (server); + glibtop_warn_io_r (server, "kvm_getargv (%d)", pid); + return NULL; + } + + glibtop_suid_leave (server); + + for (ptr = args; *ptr; ptr++) + size += strlen (*ptr)+1; + + size += 2; + retval = g_malloc0 (size); + + for (ptr = args; *ptr; ptr++) { + const size_t len = strlen (*ptr)+1; + memcpy (retval+pos, *ptr, len); + pos += len; + } + + buf->size = pos ? pos-1 : 0; + + buf->flags = _glibtop_sysdeps_proc_args; + + return retval; +} diff --git a/sysdeps/openbsd/prockernel.c b/sysdeps/openbsd/prockernel.c new file mode 100644 index 00000000..1031e8c1 --- /dev/null +++ b/sysdeps/openbsd/prockernel.c @@ -0,0 +1,95 @@ +/* $OpenBSD: prockernel.c,v 1.3 2011/05/23 19:35:55 jasper Exp $ */ + +/* Copyright (C) 1998 Joshua Sled + This file is part of LibGTop 1.0. + + Contributed by Joshua Sled <jsled@xcf.berkeley.edu>, July 1998. + + LibGTop is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, + or (at your option) any later version. + + LibGTop is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with LibGTop; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +#include <config.h> +#include <glibtop.h> +#include <glibtop/error.h> +#include <glibtop/prockernel.h> + +#include <glibtop_suid.h> + +#include <kvm.h> +#include <sys/param.h> +#include <sys/sysctl.h> +#include <sys/proc.h> +#include <unistd.h> +#include <fcntl.h> + +static const unsigned long _glibtop_sysdeps_proc_kernel_pstats = +(1L << GLIBTOP_PROC_KERNEL_MIN_FLT) + +(1L << GLIBTOP_PROC_KERNEL_MAJ_FLT); + +static const unsigned long _glibtop_sysdeps_proc_kernel_wchan = +(1L << GLIBTOP_PROC_KERNEL_NWCHAN) + +(1L << GLIBTOP_PROC_KERNEL_WCHAN); + +/* Init function. */ + +void +_glibtop_init_proc_kernel_p (glibtop *server) +{ + server->sysdeps.proc_kernel = _glibtop_sysdeps_proc_kernel_pstats | + _glibtop_sysdeps_proc_kernel_wchan; +} + +void +glibtop_get_proc_kernel_p (glibtop *server, + glibtop_proc_kernel *buf, + pid_t pid) +{ + struct kinfo_proc2 *pinfo; + int count; + + char filename [BUFSIZ]; + struct stat statb; + + glibtop_init_p (server, (1L << GLIBTOP_SYSDEPS_PROC_KERNEL), 0); + + memset (buf, 0, sizeof (glibtop_proc_kernel)); + + if (server->sysdeps.proc_time == 0) + return; + + /* It does not work for the swapper task. */ + if (pid == 0) return; + + /* Get the process information */ + pinfo = kvm_getproc2 (server->machine.kd, KERN_PROC_PID, pid, + sizeof(*pinfo), &count); + if ((pinfo == NULL) || (count != 1)) { + glibtop_warn_io_r (server, "kvm_getprocs (%d)", pid); + return; + } + + buf->nwchan = pinfo[0].p_wchan; + if (pinfo[0].p_wchan && pinfo[0].p_wmesg) + g_strlcpy(buf->wchan, pinfo[0].p_wmesg, + sizeof buf->wchan); + + buf->min_flt = pinfo[0].p_uru_minflt; + buf->maj_flt = pinfo[0].p_uru_majflt; + + buf->flags |= (_glibtop_sysdeps_proc_kernel_wchan + | _glibtop_sysdeps_proc_kernel_pstats); + +} diff --git a/sysdeps/openbsd/proclist.c b/sysdeps/openbsd/proclist.c new file mode 100644 index 00000000..b63c032b --- /dev/null +++ b/sysdeps/openbsd/proclist.c @@ -0,0 +1,108 @@ +/* $OpenBSD: proclist.c,v 1.3 2011/05/23 19:35:55 jasper Exp $ */ + +/* Copyright (C) 1998 Joshua Sled + This file is part of LibGTop 1.0. + + Contributed by Joshua Sled <jsled@xcf.berkeley.edu>, July 1998. + + LibGTop is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, + or (at your option) any later version. + + LibGTop is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with LibGTop; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +#include <config.h> +#include <glibtop.h> +#include <glibtop/error.h> +#include <glibtop/proclist.h> + +#include <glibtop_suid.h> + +static const unsigned long _glibtop_sysdeps_proclist = +(1L << GLIBTOP_PROCLIST_TOTAL) + (1L << GLIBTOP_PROCLIST_NUMBER) + +(1L << 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. */ + +void +_glibtop_init_proclist_p (glibtop *server) +{ + server->sysdeps.proclist = _glibtop_sysdeps_proclist; +} + +pid_t * +glibtop_get_proclist_p (glibtop *server, glibtop_proclist *buf, + gint64 real_which, gint64 arg) +{ + struct kinfo_proc2 *pinfo; + unsigned *pids = NULL; + int which, count; + int i,j; + + glibtop_init_p (server, (1L << GLIBTOP_SYSDEPS_PROCLIST), 0); + + memset (buf, 0, sizeof (glibtop_proclist)); + + which = (int)(real_which & GLIBTOP_KERN_PROC_MASK); + + /* Get the process data */ + pinfo = kvm_getproc2 (server->machine.kd, which, arg, + sizeof (*pinfo), &count); + if ((pinfo == NULL) || (count < 1)) { + glibtop_warn_io_r (server, "kvm_getprocs (proclist)"); + return NULL; + } + count--; + + /* Allocate count objects in the pids_chain array + * Same as malloc is pids is NULL, which it is. */ + pids = g_realloc (pids, count * sizeof (unsigned)); + /* Copy the pids over to this chain */ + for (i=j=0; i < count; i++) { +#define PROC_STAT p_stat +#define PROC_RUID p_ruid +#define PROC_PID p_pid + + if ((real_which & GLIBTOP_EXCLUDE_IDLE) && + (pinfo[i].PROC_STAT != SRUN)) + continue; + else if ((real_which & GLIBTOP_EXCLUDE_SYSTEM) && + (pinfo[i].PROC_RUID == 0)) + continue; + pids [j++] = (unsigned) pinfo[i].PROC_PID; + } /* end for */ + /* Set the fields in buf */ + buf->number = j; + buf->size = sizeof (unsigned); + buf->total = j * sizeof (unsigned); + buf->flags = _glibtop_sysdeps_proclist; + return pids; +} diff --git a/sysdeps/openbsd/procmap.c b/sysdeps/openbsd/procmap.c new file mode 100644 index 00000000..acbd0fda --- /dev/null +++ b/sysdeps/openbsd/procmap.c @@ -0,0 +1,296 @@ +/* $OpenBSD: procmap.c,v 1.4 2011/05/26 17:47:25 jasper Exp $ */ + +/* Copyright (C) 1998 Joshua Sled + This file is part of LibGTop 1.0. + + Contributed by Joshua Sled <jsled@xcf.berkeley.edu>, July 1998. + + LibGTop is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, + or (at your option) any later version. + + LibGTop is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with LibGTop; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +#include <config.h> +#include <glibtop.h> +#include <glibtop/error.h> +#include <glibtop/procmap.h> + +#include <glibtop_suid.h> + +#include <kvm.h> +#include <stdlib.h> +#include <sys/param.h> +#include <sys/proc.h> +#include <sys/resource.h> +#include <uvm/uvm_extern.h> + +#include <sys/vnode.h> +#include <sys/mount.h> +#include <ufs/ufs/quota.h> +#include <ufs/ufs/inode.h> + +#include <sys/ucred.h> +#include <sys/sysctl.h> + +#undef _KERNEL +#define _UVM_UVM_AMAP_I_H_ 1 +#define _UVM_UVM_MAP_I_H_ 1 +#include <uvm/uvm.h> + +static const unsigned long _glibtop_sysdeps_proc_map = +(1L << GLIBTOP_PROC_MAP_TOTAL) + (1L << GLIBTOP_PROC_MAP_NUMBER) + +(1L << GLIBTOP_PROC_MAP_SIZE); + +static const unsigned long _glibtop_sysdeps_map_entry = +(1L << GLIBTOP_MAP_ENTRY_START) + (1L << GLIBTOP_MAP_ENTRY_END) + +(1L << GLIBTOP_MAP_ENTRY_OFFSET) + (1L << GLIBTOP_MAP_ENTRY_PERM) + +(1L << GLIBTOP_MAP_ENTRY_INODE) + (1L << GLIBTOP_MAP_ENTRY_DEVICE); + +/* Local helper functions. */ + +ssize_t load_vmmap_entries(glibtop*, unsigned long, struct vm_map_entry**, + struct vm_map_entry*); +void unload_vmmap_entries(struct vm_map_entry *); + +/* Init function. */ + +void +_glibtop_init_proc_map_p (glibtop *server) +{ + server->sysdeps.proc_map = _glibtop_sysdeps_proc_map; +} + +/* + * Download vmmap_entries from the kernel into our address space. + * We fix up the addr tree while downloading. + * + * Returns: the size of the tree on succes, or -1 on failure. + * On failure, *rptr needs to be passed to unload_vmmap_entries to free + * the lot. + */ +ssize_t +load_vmmap_entries(glibtop *server, unsigned long kptr, + struct vm_map_entry **rptr, struct vm_map_entry *parent) +{ + struct vm_map_entry *entry; + unsigned long left_kptr, right_kptr; + ssize_t left_sz; + ssize_t right_sz; + + if (kptr == 0) + return 0; + + /* Need space. */ + entry = malloc(sizeof(*entry)); + if (entry == NULL) + return -1; + + /* Download entry at kptr. */ + if (kvm_read (server->machine.kd, kptr, + (char *)entry, sizeof(*entry)) != sizeof(*entry)) { + free(entry); + return -1; + } + + /* + * Update addr pointers to have sane values in this address space. + * We save the kernel pointers in {left,right}_kptr, so we have them + * available to download children. + */ + left_kptr = (unsigned long) RB_LEFT(entry, daddrs.addr_entry); + right_kptr = (unsigned long) RB_RIGHT(entry, daddrs.addr_entry); + RB_LEFT(entry, daddrs.addr_entry) = + RB_RIGHT(entry, daddrs.addr_entry) = NULL; + /* Fill in parent pointer. */ + RB_PARENT(entry, daddrs.addr_entry) = parent; + + /* + * Consistent state reached, fill in *rptr. + */ + *rptr = entry; + + /* + * Download left, right. + * On failure, our map is in a state that can be handled by + * unload_vmmap_entries. + */ + left_sz = load_vmmap_entries(server, left_kptr, + &RB_LEFT(entry, daddrs.addr_entry), entry); + if (left_sz == -1) + return -1; + right_sz = load_vmmap_entries(server, right_kptr, + &RB_RIGHT(entry, daddrs.addr_entry), entry); + if (right_sz == -1) + return -1; + + return 1 + left_sz + right_sz; +} + +/* + * Free the vmmap entries in the given tree. + */ +void +unload_vmmap_entries(struct vm_map_entry *entry) +{ + if (entry == NULL) + return; + + unload_vmmap_entries(RB_LEFT(entry, daddrs.addr_entry)); + unload_vmmap_entries(RB_RIGHT(entry, daddrs.addr_entry)); + free(entry); +} + +/* Provides detailed information about a process. */ + +glibtop_map_entry * +glibtop_get_proc_map_p (glibtop *server, glibtop_proc_map *buf, + pid_t pid) +{ + struct kinfo_proc2 *pinfo; + struct vm_map_entry *entry; + struct uvm_map_addr root; + struct vmspace vmspace; + struct vnode vnode; + struct inode inode; + ssize_t nentries; + GArray *maps = g_array_sized_new(FALSE, FALSE, + sizeof(glibtop_map_entry), + 100); + int count, i = 0; + + glibtop_init_p (server, (1L << GLIBTOP_SYSDEPS_PROC_MAP), 0); + + memset (buf, 0, sizeof (glibtop_proc_map)); + + /* It does not work for the swapper task. */ + if (pid == 0) return (glibtop_map_entry*) g_array_free(maps, TRUE); + + glibtop_suid_enter (server); + + /* Get the process data */ + pinfo = kvm_getproc2 (server->machine.kd, KERN_PROC_PID, pid, sizeof(struct kinfo_proc2), &count); + if ((pinfo == NULL) || (count < 1)) { + glibtop_warn_io_r (server, "kvm_getprocs (%d)", pid); + return (glibtop_map_entry*) g_array_free(maps, TRUE); + } + + /* Now we get the memory maps. */ + + if (kvm_read (server->machine.kd, + (unsigned long) pinfo [0].p_vmspace, + (char *) &vmspace, sizeof (vmspace)) != sizeof (vmspace)) + glibtop_error_io_r (server, "kvm_read (vmspace)"); + + RB_INIT(&root); + nentries = load_vmmap_entries(server, + (unsigned long) RB_ROOT(&vmspace.vm_map.addr), + &RB_ROOT(&root), NULL); + if (nentries == -1) { + unload_vmmap_entries(RB_ROOT(&root)); + glibtop_error_io_r (server, "kvm_read (entry)"); + } + + /* Allocate space. */ + + buf->number = nentries; + buf->size = sizeof (glibtop_map_entry); + + buf->total = buf->number * buf->size; + + buf->flags = _glibtop_sysdeps_proc_map; + + /* Walk through the `vm_map_entry' list ... */ + + /* I tested this a few times with `mmap'; as soon as you write + * to the mmap'ed area, the object type changes from OBJT_VNODE + * to OBJT_DEFAULT so it seems this really works. */ + + RB_FOREACH(entry, uvm_map_addr, &root) { + glibtop_map_entry *mentry; + unsigned long inum, dev; + guint len; + + if (UVM_ET_ISSUBMAP(entry)) + continue; + if (!entry->object.uvm_obj) + continue; + + /* We're only interested in vnodes */ + + if (kvm_read (server->machine.kd, + (unsigned long) entry->object.uvm_obj, + &vnode, sizeof (vnode)) != sizeof (vnode)) { + glibtop_warn_io_r (server, "kvm_read (vnode)"); + unload_vmmap_entries(RB_ROOT(&root)); + return (glibtop_map_entry*) g_array_free(maps, TRUE); + } + +#if defined(UVM_VNODE_VALID) + if (!vnode.v_uvm.u_flags & UVM_VNODE_VALID) + continue; +#endif + if ((vnode.v_type != VREG) || (vnode.v_tag != VT_UFS) || + !vnode.v_data) continue; + + if (kvm_read (server->machine.kd, + (unsigned long) vnode.v_data, + &inode, sizeof (inode)) != sizeof (inode)) + glibtop_error_io_r (server, "kvm_read (inode)"); + + inum = inode.i_number; + dev = inode.i_dev; + + len = maps->len; + g_array_set_size(maps, len + 1); + mentry = &g_array_index(maps, glibtop_map_entry, len); + + mentry->flags = _glibtop_sysdeps_map_entry; + + mentry->start = (guint64) entry->start; + mentry->end = (guint64) entry->end; + mentry->offset = (guint64) entry->offset; + mentry->device = (guint64) dev; + mentry->inode = (guint64) inum; + + mentry->perm = (guint64) 0; + + if (entry->protection & VM_PROT_READ) + mentry->perm |= GLIBTOP_MAP_PERM_READ; + if (entry->protection & VM_PROT_WRITE) + mentry->perm |= GLIBTOP_MAP_PERM_WRITE; + if (entry->protection & VM_PROT_EXECUTE) + mentry->perm |= GLIBTOP_MAP_PERM_EXECUTE; + } + + buf->flags = _glibtop_sysdeps_proc_map; + + buf->number = maps->len; + buf->size = sizeof (glibtop_map_entry); + buf->total = buf->number * buf->size; + + unload_vmmap_entries(RB_ROOT(&root)); + return (glibtop_map_entry*) g_array_free(maps, FALSE); +} + +/* + * Don't implement address comparison. + */ +static __inline int +no_impl(void *p, void *q) +{ + abort(); /* Should not be called. */ + return 0; +} + +RB_GENERATE(uvm_map_addr, vm_map_entry, daddrs.addr_entry, no_impl); diff --git a/sysdeps/openbsd/procmem.c b/sysdeps/openbsd/procmem.c new file mode 100644 index 00000000..80d6c1c5 --- /dev/null +++ b/sysdeps/openbsd/procmem.c @@ -0,0 +1,134 @@ +/* $OpenBSD: procmem.c,v 1.4 2011/05/24 10:40:47 jasper Exp $ */ + +/* Copyright (C) 1998 Joshua Sled + This file is part of LibGTop 1.0. + + Contributed by Joshua Sled <jsled@xcf.berkeley.edu>, July 1998. + + LibGTop is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, + or (at your option) any later version. + + LibGTop is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with LibGTop; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +#include <config.h> +#include <glibtop.h> +#include <glibtop/error.h> +#include <glibtop/procmem.h> + +#include <glibtop_suid.h> + +#include <kvm.h> +#include <sys/param.h> +#include <sys/proc.h> +#include <sys/resource.h> +#include <uvm/uvm_extern.h> + +#include <sys/vnode.h> +#include <ufs/ufs/quota.h> +#include <ufs/ufs/inode.h> + +#include <sys/ucred.h> +#include <sys/sysctl.h> +#include <uvm/uvm.h> + +/* Fixme ... */ +#undef _KERNEL +#define _UVM_UVM_AMAP_I_H_ 1 +#define _UVM_UVM_MAP_I_H_ 1 +#include <uvm/uvm.h> + +static const unsigned long _glibtop_sysdeps_proc_mem = +(1L << GLIBTOP_PROC_MEM_SIZE) + +(1L << GLIBTOP_PROC_MEM_VSIZE) + +(1L << GLIBTOP_PROC_MEM_RESIDENT) + +(1L << GLIBTOP_PROC_MEM_RSS) + +(1L << GLIBTOP_PROC_MEM_RSS_RLIM); + +static const unsigned long _glibtop_sysdeps_proc_mem_share = +(1L << GLIBTOP_PROC_MEM_SHARE); + +#ifndef LOG1024 +#define LOG1024 10 +#endif + +/* these are for getting the memory statistics */ +static int pageshift; /* log base 2 of the pagesize */ + +/* define pagetok in terms of pageshift */ +#define pagetok(size) ((size) << pageshift) + +/* Init function. */ + +void +_glibtop_init_proc_mem_p (glibtop *server) +{ + register int pagesize; + + /* get the page size and calculate pageshift from it */ + pagesize = sysconf(_SC_PAGESIZE); + pageshift = 0; + while (pagesize > 1) { + pageshift++; + pagesize >>= 1; + } + + /* we only need the amount of log(2)1024 for our conversion */ + pageshift -= LOG1024; + + server->sysdeps.proc_mem = _glibtop_sysdeps_proc_mem | + _glibtop_sysdeps_proc_mem_share; +} + +/* Provides detailed information about a process. */ + +void +glibtop_get_proc_mem_p (glibtop *server, glibtop_proc_mem *buf, + pid_t pid) +{ + struct kinfo_proc2 *pinfo; + + int count; + + glibtop_init_p (server, (1L << GLIBTOP_SYSDEPS_PROC_MEM), 0); + + memset (buf, 0, sizeof (glibtop_proc_mem)); + + if (server->sysdeps.proc_mem == 0) + return; + + /* It does not work for the swapper task. */ + if (pid == 0) return; + + /* Get the process data */ + pinfo = kvm_getproc2 (server->machine.kd, KERN_PROC_PID, pid, + sizeof (*pinfo), &count); + if ((pinfo == NULL) || (count < 1)) { + glibtop_warn_io_r (server, "kvm_getprocs (%d)", pid); + return; + } + + buf->rss_rlim = pinfo[0].p_uru_maxrss; + buf->vsize = buf->size = (guint64)pagetok + (pinfo[0].p_vm_tsize + pinfo[0].p_vm_dsize + pinfo[0].p_vm_ssize) + << LOG1024; + buf->resident = buf->rss = (guint64)pagetok + (pinfo[0].p_vm_rssize) << LOG1024; + + /* Now we get the shared memory. */ + + buf->share = pinfo[0].p_uru_ixrss; + + buf->flags = _glibtop_sysdeps_proc_mem | + _glibtop_sysdeps_proc_mem_share; +} diff --git a/sysdeps/openbsd/procopenfiles.c b/sysdeps/openbsd/procopenfiles.c new file mode 100644 index 00000000..25f9157e --- /dev/null +++ b/sysdeps/openbsd/procopenfiles.c @@ -0,0 +1,225 @@ +/* $OpenBSD: procopenfiles.c,v 1.2 2011/05/23 19:35:55 jasper Exp $ */ + +/* Copyright (C) 1998-99 Martin Baulig + Copyright (C) 2004 Nicol\ufffds Lichtmaier + Copyright (C) 2007 Joe Marcus Clarke + This file is part of LibGTop 1.0. + + Modified by Nicol\ufffds Lichtmaier to give a process open files. + + Contributed by Martin Baulig <martin@home-of-linux.org>, April 1998. + + LibGTop is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, + or (at your option) any later version. + + LibGTop is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with LibGTop; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +#include <config.h> +#include <glibtop.h> +#include <glibtop/error.h> +#include <glibtop/procopenfiles.h> +#include <sys/types.h> +#include <sys/socket.h> +#include <sys/param.h> +#include <sys/sysctl.h> +#include <sys/un.h> +#include <netinet/in.h> +#include <arpa/inet.h> +#include <string.h> +#include <stdlib.h> + +#include "glibtop_private.h" + +static const unsigned long _glibtop_sysdeps_proc_open_files = +(1L << GLIBTOP_PROC_OPEN_FILES_NUMBER)| +(1L << GLIBTOP_PROC_OPEN_FILES_TOTAL)| +(1L << GLIBTOP_PROC_OPEN_FILES_SIZE); + +/* Init function. */ + +void +_glibtop_init_proc_open_files_s (glibtop *server) +{ + server->sysdeps.proc_open_files = _glibtop_sysdeps_proc_open_files; +} + +static GArray * +parse_output(const char *output) { + GArray *entries; + char **lines; + char *ftype = NULL; + char *fname = NULL; + guint i; + guint len; + int fd = -1; + + entries = g_array_new(FALSE, FALSE, sizeof(glibtop_open_files_entry)); + + lines = g_strsplit(output, "\n", 0); + len = g_strv_length(lines); + + for (i = 0; i < len && lines[i]; i++) { + glibtop_open_files_entry entry = {0}; + + if (strlen(lines[i]) < 2) + continue; + + if (!g_str_has_prefix(lines[i], "f") && + !g_str_has_prefix(lines[i], "t") && + !g_str_has_prefix(lines[i], "n")) + continue; + + if (g_str_has_prefix(lines[i], "f")) { + if (!g_ascii_isdigit(*(lines[i] + 1))) + i += 2; + else + fd = atoi(lines[i] + 1); + continue; + } + + if (g_str_has_prefix(lines[i], "t")) { + ftype = lines[i]; + ftype++; + continue; + } else { + fname = lines[i]; + fname++; + } + + if (ftype == NULL || fname == NULL) + continue; + + if (!strcmp(ftype, "unix")) { + entry.type = GLIBTOP_FILE_TYPE_LOCALSOCKET; + g_strlcpy(entry.info.localsock.name, fname, + sizeof(entry.info.localsock.name)); + } else if (!strcmp(ftype, "PIPE")) { + entry.type = GLIBTOP_FILE_TYPE_PIPE; + } else if (!strcmp(ftype, "VREG") || + !strcmp(ftype, "GDIR") || + !strcmp(ftype, "GREG") || + !strcmp(ftype, "VCHR") || + !strcmp(ftype, "VBLK") || + !strcmp(ftype, "DIR") || + !strcmp(ftype, "LINK") || + !strcmp(ftype, "REG") || + !strcmp(ftype, "VDIR")) { + entry.type = GLIBTOP_FILE_TYPE_FILE; + g_strlcpy(entry.info.file.name, fname, + sizeof(entry.info.file.name)); + } else if (!strcmp(ftype, "IPv4")) { + char **hosts; + char **remote_host; + + if (!strstr(fname, "->")) { + remote_host = g_strsplit(fname, ":", 0); + } else { + hosts = g_strsplit(fname, "->", 0); + if (g_strv_length(hosts) < 2) { + g_strfreev(hosts); + continue; + } + + remote_host = g_strsplit(hosts[1], ":", 0); + g_strfreev(hosts); + } + + if (g_strv_length(remote_host) < 2) { + g_strfreev(remote_host); + continue; + } + + entry.type = GLIBTOP_FILE_TYPE_INETSOCKET; + if (!strcmp(remote_host[0], "*")) + g_strlcpy(entry.info.sock.dest_host, "0.0.0.0", + sizeof(entry.info.sock.dest_host)); + else + g_strlcpy(entry.info.sock.dest_host, + remote_host[0], + sizeof(entry.info.sock.dest_host)); + entry.info.sock.dest_port = atoi(remote_host[1]); + + g_strfreev(remote_host); + } else if (!strcmp(ftype, "IPv6")) { + char **hosts; + char **remote_host; + + if (!strstr(fname, "->")) { + remote_host = g_strsplit(fname, ":", 0); + } else { + hosts = g_strsplit(fname, "->", 0); + if (g_strv_length(hosts) < 2) { + g_strfreev(hosts); + continue; + } + + remote_host = g_strsplit(hosts[1], "]", 0); + g_strfreev(hosts); + } + + if (g_strv_length(remote_host) < 2) { + g_strfreev(remote_host); + continue; + } + + entry.type = GLIBTOP_FILE_TYPE_INET6SOCKET; + if (!strcmp(remote_host[0], "*")) + g_strlcpy(entry.info.sock.dest_host, "0.0.0.0", + sizeof(entry.info.sock.dest_host)); + else + g_strlcpy(entry.info.sock.dest_host, + remote_host[0] + 1, + sizeof(entry.info.sock.dest_host)); + entry.info.sock.dest_port = atoi(remote_host[1] + 1); + + g_strfreev(remote_host); + } else + continue; + + entry.fd = fd; + + fd = -1; + ftype = NULL; + fname = NULL; + + g_array_append_val(entries, entry); + } + + g_strfreev(lines); + + return entries; +} + +glibtop_open_files_entry * +glibtop_get_proc_open_files_s (glibtop *server, glibtop_proc_open_files *buf, pid_t pid) +{ + char *output; + GArray *entries; + + memset(buf, 0, sizeof (glibtop_proc_open_files)); + + output = execute_lsof(pid); + if (output == NULL) return NULL; + + entries = parse_output(output); + + g_free(output); + + buf->flags = _glibtop_sysdeps_proc_open_files; + buf->number = entries->len; + buf->size = sizeof(glibtop_open_files_entry); + buf->total = buf->number * buf->size; + + return (glibtop_open_files_entry*)g_array_free(entries, FALSE); +} diff --git a/sysdeps/openbsd/procsegment.c b/sysdeps/openbsd/procsegment.c new file mode 100644 index 00000000..01fd3712 --- /dev/null +++ b/sysdeps/openbsd/procsegment.c @@ -0,0 +1,83 @@ +/* $OpenBSD: procsegment.c,v 1.2 2011/05/23 19:35:55 jasper Exp $ */ + +/* Copyright (C) 1998 Joshua Sled + This file is part of LibGTop 1.0. + + Contributed by Joshua Sled <jsled@xcf.berkeley.edu>, July 1998. + + LibGTop is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, + or (at your option) any later version. + + LibGTop is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with LibGTop; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +#include <config.h> +#include <glibtop.h> +#include <glibtop/error.h> +#include <glibtop/procsegment.h> + +#include <glibtop_suid.h> + +#include <kvm.h> +#include <sys/param.h> +#include <sys/sysctl.h> + +static const unsigned long _glibtop_sysdeps_proc_segment = 0; + +/* Init function. */ + +void +_glibtop_init_proc_segment_p (glibtop *server) +{ + server->sysdeps.proc_segment = _glibtop_sysdeps_proc_segment; +} + +/* Provides detailed information about a process. */ + +void +glibtop_get_proc_segment_p (glibtop *server, + glibtop_proc_segment *buf, + pid_t pid) +{ + glibtop_init_p (server, (1L << GLIBTOP_SYSDEPS_PROC_SEGMENT), 0); + + memset (buf, 0, sizeof (glibtop_proc_segment)); + +#if 0 + /* 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 + */ +#endif +} + diff --git a/sysdeps/openbsd/procsignal.c b/sysdeps/openbsd/procsignal.c new file mode 100644 index 00000000..3492129c --- /dev/null +++ b/sysdeps/openbsd/procsignal.c @@ -0,0 +1,91 @@ +/* $OpenBSD: procsignal.c,v 1.3 2011/05/23 19:35:55 jasper Exp $ */ + +/* Copyright (C) 1998 Joshua Sled + This file is part of LibGTop 1.0. + + Contributed by Joshua Sled <jsled@xcf.berkeley.edu>, July 1998. + + LibGTop is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, + or (at your option) any later version. + + LibGTop is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with LibGTop; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +#include <config.h> +#include <glibtop.h> +#include <glibtop/error.h> +#include <glibtop/procsignal.h> + +#include <glibtop_suid.h> + +#include <sys/param.h> + +static const unsigned long _glibtop_sysdeps_proc_signal = +(1L << GLIBTOP_PROC_SIGNAL_SIGNAL) + +(1L << GLIBTOP_PROC_SIGNAL_BLOCKED) + +(1L << GLIBTOP_PROC_SIGNAL_SIGIGNORE) + +(1L << GLIBTOP_PROC_SIGNAL_SIGCATCH); + +/* Init function. */ + +void +_glibtop_init_proc_signal_p (glibtop *server) +{ + server->sysdeps.proc_signal = _glibtop_sysdeps_proc_signal; +} + +void +glibtop_get_proc_signal_p (glibtop *server, + glibtop_proc_signal *buf, + pid_t pid) +{ + struct kinfo_proc2 *pinfo; + int count = 0; + + glibtop_init_p (server, (1L << GLIBTOP_SYSDEPS_PROC_SIGNAL), 0); + + memset (buf, 0, sizeof (glibtop_proc_signal)); + + /* It does not work for the swapper task. */ + if (pid == 0) return; + + /* Get the process information */ + pinfo = kvm_getproc2 (server->machine.kd, KERN_PROC_PID, pid, + sizeof (*pinfo), &count); + if ((pinfo == NULL) || (count != 1)) { + glibtop_warn_io_r (server, "kvm_getprocs (%d)", pid); + return; + } + + /* signal: mask of pending signals. + * pinfo [0].kp_proc.p_siglist + */ + buf->signal [0] = pinfo [0].p_siglist; + + /* blocked: mask of blocked signals. + * pinfo [0].kp_proc.p_sigmask + */ + buf->blocked [0] = pinfo [0].p_sigmask; + + /* sigignore: mask of ignored signals. + * pinfo [0].kp_proc.p_sigignore + */ + buf->sigignore [0] = pinfo [0].p_sigignore; + + /* sigcatch: mask of caught signals. + * pinfo [0].kp_proc.p_sigcatch + */ + buf->sigcatch [0] = pinfo [0].p_sigcatch; + + buf->flags = _glibtop_sysdeps_proc_signal; +} diff --git a/sysdeps/openbsd/procstate.c b/sysdeps/openbsd/procstate.c new file mode 100644 index 00000000..27442b14 --- /dev/null +++ b/sysdeps/openbsd/procstate.c @@ -0,0 +1,133 @@ +/* $OpenBSD: procstate.c,v 1.3 2011/05/23 19:35:55 jasper Exp $ */ + +/* Copyright (C) 1998 Joshua Sled + This file is part of LibGTop 1.0. + + Contributed by Joshua Sled <jsled@xcf.berkeley.edu>, July 1998. + + LibGTop is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, + or (at your option) any later version. + + LibGTop is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with LibGTop; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +#include <config.h> +#include <glibtop.h> +#include <glibtop/error.h> +#include <glibtop/procstate.h> + +#include <glibtop_suid.h> + +static const unsigned long _glibtop_sysdeps_proc_state = +(1L << GLIBTOP_PROC_STATE_CMD) + (1L << GLIBTOP_PROC_STATE_UID) + +(1L << GLIBTOP_PROC_STATE_GID); + +static const unsigned long _glibtop_sysdeps_proc_state_new = +0; + +/* Init function. */ + +void +_glibtop_init_proc_state_p (glibtop *server) +{ + server->sysdeps.proc_state = _glibtop_sysdeps_proc_state | + _glibtop_sysdeps_proc_state_new; +} + +/* Provides detailed information about a process. */ + +void +glibtop_get_proc_state_p (glibtop *server, + glibtop_proc_state *buf, + pid_t pid) +{ + struct kinfo_proc2 *pinfo; + int count = 0; + + glibtop_init_p (server, (1L << GLIBTOP_SYSDEPS_PROC_STATE), 0); + + memset (buf, 0, sizeof (glibtop_proc_state)); + + /* It does not work for the swapper task. */ + if (pid == 0) return; + + /* Get the process information */ + pinfo = kvm_getproc2 (server->machine.kd, KERN_PROC_PID, pid, + sizeof (*pinfo), &count); + if ((pinfo == NULL) || (count != 1)) { + glibtop_warn_io_r (server, "kvm_getprocs (%d)", pid); + return; + } + +#define PROC_COMM p_comm +#define PROC_SVUID p_svuid +#define PROC_SVGID p_svgid +#define PROC_STAT p_stat + + g_strlcpy (buf->cmd, pinfo [0].PROC_COMM, sizeof buf->cmd); + + buf->uid = pinfo [0].PROC_SVUID; + buf->gid = pinfo [0].PROC_SVGID; + + /* Set the flags for the data we're about to return*/ + buf->flags = _glibtop_sysdeps_proc_state | + _glibtop_sysdeps_proc_state_new; + +#if LIBGTOP_VERSION_CODE >= 1001000 + switch (pinfo [0].PROC_STAT) { + case SIDL: + buf->state = 0; + break; + case SRUN: + buf->state = GLIBTOP_PROCESS_RUNNING; + break; +#ifdef SSLEEP + case SSLEEP: + buf->state = GLIBTOP_PROCESS_INTERRUPTIBLE; + break; +#endif + case SSTOP: + buf->state = GLIBTOP_PROCESS_STOPPED; + break; + case SZOMB: + buf->state = GLIBTOP_PROCESS_ZOMBIE; + break; + default: + return; + } +#else + switch (pinfo [0].PROC_STAT) { + case SIDL: + buf->state = 'D'; + break; + case SRUN: + buf->state = 'R'; + break; +#ifdef SSLEEP + case SSLEEP: + buf->state = 'S'; + break; +#endif + case SSTOP: + buf->state = 'T'; + break; + case SZOMB: + buf->state = 'Z'; + break; + default: + return; + } +#endif + + buf->flags |= (1L << GLIBTOP_PROC_STATE_STATE); +} diff --git a/sysdeps/openbsd/proctime.c b/sysdeps/openbsd/proctime.c new file mode 100644 index 00000000..c4aeb6fc --- /dev/null +++ b/sysdeps/openbsd/proctime.c @@ -0,0 +1,159 @@ +/* $OpenBSD: proctime.c,v 1.3 2011/05/23 19:35:56 jasper Exp $ */ + +/* Copyright (C) 1998-99 Martin Baulig + This file is part of LibGTop 1.0. + + Contributed by Martin Baulig <martin@home-of-linux.org>, April 1998. + + LibGTop is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, + or (at your option) any later version. + + LibGTop is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with LibGTop; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +#include <config.h> +#include <glibtop.h> +#include <glibtop/error.h> +#include <glibtop/proctime.h> + +#include <glibtop_suid.h> + +static const unsigned long _glibtop_sysdeps_proc_time = +(1L << GLIBTOP_PROC_TIME_RTIME) + (1L << GLIBTOP_PROC_TIME_FREQUENCY); + +static const unsigned long _glibtop_sysdeps_proc_time_user = +(1L << GLIBTOP_PROC_TIME_UTIME) + (1L << GLIBTOP_PROC_TIME_STIME) + +(1L << GLIBTOP_PROC_TIME_CUTIME) + (1L << GLIBTOP_PROC_TIME_CSTIME) + +(1L << GLIBTOP_PROC_TIME_START_TIME); + +#define tv2sec(tv) (((guint64) tv.tv_sec * 1000000) + (guint64) tv.tv_usec) + +static unsigned int clockrate; +static const int mib [] = { CTL_KERN, KERN_CLOCKRATE }; + +/* Init function. */ + +void +_glibtop_init_proc_time_p (glibtop *server) +{ + struct clockinfo ci; + size_t length; + length = sizeof (ci); + if (sysctl (mib, 2, &ci, &length, NULL, 0) == 0) + clockrate = ci.hz; + if (!clockrate) + clockrate = 1; /* XXX avoid div by 0 later */ + + server->sysdeps.proc_time = _glibtop_sysdeps_proc_time | + _glibtop_sysdeps_proc_time_user; +} + +/* Taken from /usr/src/sys/kern/kern_resource.c */ + +/* + * Transform the running time and tick information in proc p into user, + * system, and interrupt time usage. + */ + +static void +calcru(p, up, sp, ip) + struct proc *p; + struct timeval *up; + struct timeval *sp; + struct timeval *ip; +{ + quad_t totusec; + u_quad_t u, st, ut, it, tot; + long sec, usec; + struct timeval tv; + + st = p->p_sticks; + ut = p->p_uticks; + it = p->p_iticks; + + tot = st + ut + it; + if (tot == 0) { + st = 1; + tot = 1; + } + + sec = p->p_rtime.tv_sec; + usec = p->p_rtime.tv_usec; + + totusec = (quad_t)sec * 1000000 + usec; + + if (totusec < 0) { + /* XXX no %qd in kernel. Truncate. */ + fprintf (stderr, "calcru: negative time: %ld usec\n", + (long)totusec); + totusec = 0; + } + + + u = totusec; + st = (u * st) / tot; + sp->tv_sec = st / 1000000; + sp->tv_usec = st % 1000000; + ut = (u * ut) / tot; + up->tv_sec = ut / 1000000; + up->tv_usec = ut % 1000000; + if (ip != NULL) { + it = (u * it) / tot; + ip->tv_sec = it / 1000000; + ip->tv_usec = it % 1000000; + } +} + +/* Provides detailed information about a process. */ + +void +glibtop_get_proc_time_p (glibtop *server, glibtop_proc_time *buf, + pid_t pid) +{ + struct kinfo_proc2 *pinfo; + int count; + + glibtop_init_p (server, (1L << GLIBTOP_SYSDEPS_PROC_TIME), 0); + + memset (buf, 0, sizeof (glibtop_proc_time)); + + /* It does not work for the swapper task. */ + if (pid == 0) return; + + + /* Get the process information */ + pinfo = kvm_getproc2 (server->machine.kd, KERN_PROC_PID, pid, + sizeof (*pinfo), &count); + if ((pinfo == NULL) || (count != 1)) { + glibtop_warn_io_r (server, "kvm_getprocs (%d)", pid); + return; + } + + buf->rtime = pinfo[0].p_rtime_sec * clockrate + + pinfo[0].p_rtime_usec * clockrate / 1000000; + buf->frequency = clockrate; + + buf->flags = _glibtop_sysdeps_proc_time; + + buf->utime = pinfo[0].p_uutime_sec * 1000000 + + pinfo[0].p_uutime_usec; + buf->stime = pinfo[0].p_ustime_sec * 1000000 + + pinfo[0].p_ustime_usec; + buf->cutime = pinfo[0].p_uctime_sec * 1000000 + + pinfo[0].p_uctime_usec; /* XXX is u+s */ + buf->cstime = 0; /* XXX */ + buf->start_time = pinfo[0].p_ustart_sec; + + buf->flags |= _glibtop_sysdeps_proc_time_user; +} + diff --git a/sysdeps/openbsd/procuid.c b/sysdeps/openbsd/procuid.c new file mode 100644 index 00000000..c3b4ec48 --- /dev/null +++ b/sysdeps/openbsd/procuid.c @@ -0,0 +1,104 @@ +/* $OpenBSD: procuid.c,v 1.3 2011/05/23 19:35:56 jasper Exp $ */ + +/* Copyright (C) 1998-99 Martin Baulig + This file is part of LibGTop 1.0. + + Contributed by Martin Baulig <martin@home-of-linux.org>, April 1998. + + LibGTop is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, + or (at your option) any later version. + + LibGTop is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with LibGTop; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +#include <config.h> +#include <glibtop.h> +#include <glibtop/error.h> +#include <glibtop/procuid.h> + +#include <glibtop_suid.h> + +static const unsigned long _glibtop_sysdeps_proc_uid = +(1L << GLIBTOP_PROC_UID_UID) + (1L << GLIBTOP_PROC_UID_EUID) + +(1L << GLIBTOP_PROC_UID_GID) + +(1L << GLIBTOP_PROC_UID_EGID) + (1L << GLIBTOP_PROC_UID_PID) + +(1L << GLIBTOP_PROC_UID_PPID) + (1L << GLIBTOP_PROC_UID_PGRP) + +(1L << GLIBTOP_PROC_UID_TPGID) + (1L << GLIBTOP_PROC_UID_PRIORITY) + +(1L << GLIBTOP_PROC_UID_NICE); + +static const unsigned long _glibtop_sysdeps_proc_uid_groups = +0L; + +/* Init function. */ + +void +_glibtop_init_proc_uid_p (glibtop *server) +{ + server->sysdeps.proc_uid = _glibtop_sysdeps_proc_uid | + _glibtop_sysdeps_proc_uid_groups; +} + +/* Provides detailed information about a process. */ + +void +glibtop_get_proc_uid_p (glibtop *server, glibtop_proc_uid *buf, + pid_t pid) +{ + struct kinfo_proc2 *pinfo; + int count = 0; + + glibtop_init_p (server, (1L << GLIBTOP_SYSDEPS_PROC_UID), 0); + + memset (buf, 0, sizeof (glibtop_proc_uid)); + + /* It does not work for the swapper task. */ + if (pid == 0) return; + + /* Get the process information */ + pinfo = kvm_getproc2 (server->machine.kd, KERN_PROC_PID, pid, + sizeof (*pinfo), &count); + if ((pinfo == NULL) || (count != 1)) { + glibtop_warn_io_r (server, "kvm_getprocs (%d)", pid); + return; + } + +#define PROC_RUID p_ruid +#define PROC_SVUID p_svuid +#define PROC_RGID p_rgid +#define PROC_SVGID p_svgid +#define PROC_PID p_pid +#define PROC_PPID p_ppid +#define PROC_PGID p__pgid +#define PROC_TPGID p_tpgid +#define PROC_NICE p_nice +#define PROC_PRIORITY p_priority + + buf->uid = pinfo [0].PROC_RUID; + buf->euid = pinfo [0].PROC_SVUID; + buf->gid = pinfo [0].PROC_RGID; + buf->egid = pinfo [0].PROC_SVGID; + + buf->pid = pinfo [0].PROC_PID; + buf->ppid = pinfo [0].PROC_PPID; + buf->pgrp = pinfo [0].PROC_PGID; + buf->tpgid = pinfo [0].PROC_TPGID; + + buf->nice = pinfo [0].PROC_NICE; + buf->priority = pinfo [0].PROC_PRIORITY; + + /* Set the flags for the data we're about to return*/ + buf->flags = _glibtop_sysdeps_proc_uid; + + /* Use LibGTop conditionals here so we can more easily merge this + * code into the LIBGTOP_STABLE_1_0 branch. */ +} diff --git a/sysdeps/openbsd/procwd.c b/sysdeps/openbsd/procwd.c new file mode 100644 index 00000000..8f6e6605 --- /dev/null +++ b/sysdeps/openbsd/procwd.c @@ -0,0 +1,124 @@ +/* $OpenBSD: procwd.c,v 1.2 2011/05/23 19:35:56 jasper Exp $ */ + +/* Copyright (C) 2007 Joe Marcus Clarke + This file is part of LibGTop 2. + + LibGTop is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, + or (at your option) any later version. + + LibGTop is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with LibGTop; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +#include <config.h> +#include <glibtop/procwd.h> +#include <glibtop/error.h> + +#include <glibtop_private.h> + +#include <sys/types.h> +#include <sys/sysctl.h> +#include <sys/param.h> +#include <string.h> + +static const unsigned long _glibtop_sysdeps_proc_wd = +(1 << GLIBTOP_PROC_WD_EXE) | +(1 << GLIBTOP_PROC_WD_ROOT) | +(1 << GLIBTOP_PROC_WD_NUMBER); + +void +_glibtop_init_proc_wd_s(glibtop *server) +{ + server->sysdeps.proc_wd = _glibtop_sysdeps_proc_wd; +} + +static GPtrArray * +parse_output(const char *output, glibtop_proc_wd *buf) +{ + GPtrArray *dirs; + char **lines; + gboolean nextwd = FALSE; + gboolean nextrtd = FALSE; + gboolean havertd = FALSE; + guint i; + guint len; + + dirs = g_ptr_array_sized_new(1); + + lines = g_strsplit(output, "\n", 0); + len = g_strv_length(lines); + + for (i = 0; i < len && lines[i]; i++) { + if (strlen(lines[i]) < 2) + continue; + + if (!strcmp(lines[i], "fcwd")) { + nextwd = TRUE; + continue; + } + + if (!strcmp(lines[i], "frtd")) { + nextrtd = TRUE; + continue; + } + + if (!g_str_has_prefix(lines[i], "n")) + continue; + + if (nextwd) { + g_ptr_array_add(dirs, g_strdup(lines[i] + 1)); + nextwd = FALSE; + } + + if (nextrtd && !havertd) { + g_strlcpy(buf->root, lines[i] + 1, + sizeof(buf->root)); + buf->flags |= (1 << GLIBTOP_PROC_WD_ROOT); + nextrtd = FALSE; + havertd = TRUE; + } + } + + g_strfreev(lines); + + return dirs; +} + +char** +glibtop_get_proc_wd_s(glibtop *server, glibtop_proc_wd *buf, pid_t pid) +{ + char path[MAXPATHLEN]; + char *output; + + memset (buf, 0, sizeof (glibtop_proc_wd)); + + g_snprintf(path, sizeof(path), "/proc/%u/file", pid); + if (safe_readlink(path, buf->exe, sizeof(buf->exe))) + buf->flags |= (1 << GLIBTOP_PROC_WD_EXE); + + output = execute_lsof(pid); + if (output != NULL) { + GPtrArray *dirs; + + dirs = parse_output(output, buf); + g_free(output); + + buf->number = dirs->len; + buf->flags |= (1 << GLIBTOP_PROC_WD_NUMBER); + + g_ptr_array_add(dirs, NULL); + + return (char **)g_ptr_array_free(dirs, FALSE); + } + + return NULL; +} diff --git a/sysdeps/openbsd/sem_limits.c b/sysdeps/openbsd/sem_limits.c new file mode 100644 index 00000000..2232c4bf --- /dev/null +++ b/sysdeps/openbsd/sem_limits.c @@ -0,0 +1,96 @@ +/* $OpenBSD: sem_limits.c,v 1.3 2011/05/23 19:35:56 jasper Exp $ */ + +/* Copyright (C) 1998-99 Martin Baulig + This file is part of LibGTop 1.0. + + Contributed by Martin Baulig <martin@home-of-linux.org>, August 1998. + + LibGTop is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, + or (at your option) any later version. + + LibGTop is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with LibGTop; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +#include <config.h> +#include <glibtop.h> +#include <glibtop/error.h> +#include <glibtop/sem_limits.h> + +#include <glibtop_suid.h> + +/* #define _KERNEL to get declaration of `struct seminfo'. */ + +#define _KERNEL 1 + +#include <sys/ipc.h> +#include <sys/sem.h> + +static unsigned long _glibtop_sysdeps_sem_limits = +(1L << GLIBTOP_IPC_SEMMNI) + +(1L << GLIBTOP_IPC_SEMMNS) + (1L << GLIBTOP_IPC_SEMMNU) + +(1L << GLIBTOP_IPC_SEMMSL) + (1L << GLIBTOP_IPC_SEMOPM) + +(1L << GLIBTOP_IPC_SEMUME) + (1L << GLIBTOP_IPC_SEMUSZ) + +(1L << GLIBTOP_IPC_SEMVMX) + (1L << GLIBTOP_IPC_SEMAEM); + +/* The values in this structure never change at runtime, so we only + * read it once during initialization. We have to use the name `_seminfo' + * since `seminfo' is already declared external in <sys/sem.h>. */ +static struct seminfo _seminfo; + +/* nlist structure for kernel access */ +static struct nlist nlst [] = { + { "_seminfo" }, + { 0 } +}; + +/* Init function. */ + +void +_glibtop_init_sem_limits_p (glibtop *server) +{ + if (kvm_nlist (server->machine.kd, nlst) < 0) { + glibtop_warn_io_r (server, "kvm_nlist (sem_limits)"); + return; + } + + if (kvm_read (server->machine.kd, nlst [0].n_value, + &_seminfo, sizeof (_seminfo)) != sizeof (_seminfo)) { + glibtop_warn_io_r (server, "kvm_read (seminfo)"); + return; + } + + server->sysdeps.sem_limits = _glibtop_sysdeps_sem_limits; +} + +/* Provides information about sysv sem limits. */ + +void +glibtop_get_sem_limits_p (glibtop *server, glibtop_sem_limits *buf) +{ + glibtop_init_p (server, (1L << GLIBTOP_SYSDEPS_SEM_LIMITS), 0); + + memset (buf, 0, sizeof (glibtop_sem_limits)); + + if (server->sysdeps.sem_limits == 0) + return; + + buf->semmni = _seminfo.semmni; + buf->semmns = _seminfo.semmns; + buf->semmnu = _seminfo.semmnu; + buf->semmsl = _seminfo.semmsl; + buf->semopm = _seminfo.semopm; + buf->semvmx = _seminfo.semvmx; + buf->semaem = _seminfo.semaem; + + buf->flags = _glibtop_sysdeps_sem_limits; +} diff --git a/sysdeps/openbsd/shm_limits.c b/sysdeps/openbsd/shm_limits.c new file mode 100644 index 00000000..cd36cfc8 --- /dev/null +++ b/sysdeps/openbsd/shm_limits.c @@ -0,0 +1,88 @@ +/* $OpenBSD: shm_limits.c,v 1.3 2011/05/23 19:35:56 jasper Exp $ */ + +/* Copyright (C) 1998-99 Martin Baulig + This file is part of LibGTop 1.0. + + Contributed by Martin Baulig <martin@home-of-linux.org>, August 1998. + + LibGTop is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, + or (at your option) any later version. + + LibGTop is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with LibGTop; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +#include <config.h> +#include <glibtop.h> +#include <glibtop/error.h> +#include <glibtop/shm_limits.h> + +#include <glibtop_suid.h> + +#include <sys/ipc.h> +#include <sys/shm.h> + +static unsigned long _glibtop_sysdeps_shm_limits = +(1L << GLIBTOP_IPC_SHMMAX) + (1L << GLIBTOP_IPC_SHMMIN) + +(1L << GLIBTOP_IPC_SHMMNI) + (1L << GLIBTOP_IPC_SHMSEG) + +(1L << GLIBTOP_IPC_SHMALL); + +/* The values in this structure never change at runtime, so we only + * read it once during initialization. We have to use the name `_shminfo' + * since `shminfo' is already declared external in <sys/shm.h>. */ +static struct shminfo _shminfo; + +/* nlist structure for kernel access */ +static struct nlist nlst [] = { + { "_shminfo" }, + { 0 } +}; + +/* Init function. */ + +void +_glibtop_init_shm_limits_p (glibtop *server) +{ + if (kvm_nlist (server->machine.kd, nlst) < 0) { + glibtop_warn_io_r (server, "kvm_nlist (shm_limits)"); + return; + } + + if (kvm_read (server->machine.kd, nlst [0].n_value, + &_shminfo, sizeof (_shminfo)) != sizeof (_shminfo)) { + glibtop_warn_io_r (server, "kvm_read (shminfo)"); + return; + } + + server->sysdeps.shm_limits = _glibtop_sysdeps_shm_limits; +} + +/* Provides information about sysv ipc limits. */ + +void +glibtop_get_shm_limits_p (glibtop *server, glibtop_shm_limits *buf) +{ + glibtop_init_p (server, (1L << GLIBTOP_SYSDEPS_SHM_LIMITS), 0); + + memset (buf, 0, sizeof (glibtop_shm_limits)); + + if (server->sysdeps.shm_limits == 0) + return; + + buf->shmmax = _shminfo.shmmax; + buf->shmmin = _shminfo.shmmin; + buf->shmmni = _shminfo.shmmni; + buf->shmseg = _shminfo.shmseg; + buf->shmall = _shminfo.shmall; + + buf->flags = _glibtop_sysdeps_shm_limits; +} diff --git a/sysdeps/openbsd/siglist.c b/sysdeps/openbsd/siglist.c new file mode 100644 index 00000000..f23c61c8 --- /dev/null +++ b/sysdeps/openbsd/siglist.c @@ -0,0 +1,62 @@ +/* $OpenBSD: siglist.c,v 1.2 2011/05/23 19:35:56 jasper Exp $ */ + +/* Copyright (C) 1998-99 Martin Baulig + This file is part of LibGTop 1.0. + + Contributed by Martin Baulig <martin@home-of-linux.org>, April 1998. + + LibGTop is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, + or (at your option) any later version. + + LibGTop is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with LibGTop; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +#include <config.h> +#include <glibtop.h> +#include <glibtop/signal.h> + +const glibtop_signame glibtop_sys_siglist [] = +{ { 1, "SIGHUP", "Hangup" }, + { 2, "SIGINT", "Interrupt" }, + { 3, "SIGQUIT", "Quit" }, + { 4, "SIGILL", "Illegal Instruction" }, + { 5, "SIGTRAP", "Trace/Breakpoint Trap" }, + { 6, "SIGABRT", "Abort" }, + { 7, "SIGEMT", "Emulation Trap" }, + { 8, "SIGFPE", "Arithmetic Exception" }, + { 9, "SIGKILL", "Killed" }, + { 10, "SIGBUS", "Bus Error" }, + { 11, "SIGSEGV", "Segmentation Fault" }, + { 12, "SIGSYS", "Bad System Call" }, + { 13, "SIGPIPE", "Broken Pipe" }, + { 14, "SIGALRM", "Alarm Clock" }, + { 15, "SIGTERM", "Terminated" }, + { 16, "SIGURG", "Urgent Condition Present On Socket" }, + { 17, "SIGSTOP", "Stop (cannot be caught or ignored)" }, + { 18, "SIGTSTP", "Stop Signal Generated From Keyboard" }, + { 19, "SIGCONT", "Continue After Stop" }, + { 20, "SIGCHLD", "Child Status Has Changed" }, + { 21, "SIGTTIN", "Background Read Attempted From Control Terminal" }, + { 22, "SIGTTOU", "Background Write Attempted To Control Terminal" }, + { 23, "SIGIO", "I/O Is Possible On A Descriptor" }, + { 24, "SIGXCPU", "CPU Time Limit Exceeded" }, + { 25, "SIGXFSZ", "File Size Limit Exceeded" }, + { 26, "SIGVTALRM","Virtual Time Alarm" }, + { 27, "SIGPROF", "Profiling Timer Alarm" }, + { 28, "SIGWINCH","Window Size Change" }, + { 29, "SIGINFO", "Status Request From Keyboard" }, + { 30, "SIGUSR1", "User Defined Signal 1" }, + { 31, "SIGUSR2", "User Defined Signal 2" }, + { 32, "SIGTHR", "Thread Interrupt" }, + { 0, NULL, NULL } +}; diff --git a/sysdeps/openbsd/swap.c b/sysdeps/openbsd/swap.c new file mode 100644 index 00000000..7f24365f --- /dev/null +++ b/sysdeps/openbsd/swap.c @@ -0,0 +1,131 @@ +/* $OpenBSD: swap.c,v 1.6 2011/05/25 10:44:34 jasper Exp $ */ + +/* Copyright (C) 1998-99 Martin Baulig + This file is part of LibGTop 1.0. + + Contributed by Martin Baulig <martin@home-of-linux.org>, April 1998. + + LibGTop is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, + or (at your option) any later version. + + LibGTop is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with LibGTop; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +#include <config.h> +#include <glibtop.h> +#include <glibtop/error.h> +#include <glibtop/swap.h> + +#include <glibtop_suid.h> + +static const unsigned long _glibtop_sysdeps_swap = +(1L << GLIBTOP_SWAP_TOTAL) + (1L << GLIBTOP_SWAP_USED) + +(1L << GLIBTOP_SWAP_FREE) + (1L << GLIBTOP_SWAP_PAGEIN) + +(1L << GLIBTOP_SWAP_PAGEOUT); + +#include <sys/vmmeter.h> +#include <uvm/uvm_extern.h> +#include <sys/swap.h> + +static int mib_uvmexp [] = { CTL_VM, VM_UVMEXP }; + +/* Init function. */ + +void +_glibtop_init_swap_p (glibtop *server) +{ + server->sysdeps.swap = _glibtop_sysdeps_swap; +} + +/* Provides information about swap usage. */ + +/* + * This function is based on a program called swapinfo written + * by Kevin Lahey <kml@rokkaku.atl.ga.us>. + */ + +void +glibtop_get_swap_p (glibtop *server, glibtop_swap *buf) +{ + struct swapent *swaplist; + + int nswap, i; + guint64 avail = 0, inuse = 0; + + int blocksize = 512; /* Default blocksize, use getbize() ? */ + int blockdiv = blocksize / DEV_BSIZE; + + struct uvmexp uvmexp; + size_t length_uvmexp; + static int swappgsin = -1; + static int swappgsout = -1; + + glibtop_init_p (server, (1L << GLIBTOP_SYSDEPS_SWAP), 0); + + memset (buf, 0, sizeof (glibtop_swap)); + + if (server->sysdeps.swap == 0) + return; + + length_uvmexp = sizeof (uvmexp); + if (sysctl (mib_uvmexp, 2, &uvmexp, &length_uvmexp, NULL, 0)) { + glibtop_warn_io_r (server, "sysctl (uvmexp)"); + return; + } + + if (swappgsin < 0) { + buf->pagein = 0; + buf->pageout = 0; + } else { + buf->pagein = uvmexp.swapins - swappgsin; + buf->pageout = uvmexp.swapouts - swappgsout; + } + + swappgsin = uvmexp.swapins; + swappgsout = uvmexp.swapouts; + + nswap = swapctl (SWAP_NSWAP, 0, 0); + if (nswap < 0) { + glibtop_warn_io_r (server, "swapctl (SWAP_NSWAP)"); + return; + } + + swaplist = g_malloc (nswap * sizeof (struct swapent)); + + if (swapctl (SWAP_STATS, swaplist, nswap) != nswap) { + glibtop_warn_io_r (server, "swapctl (SWAP_STATS)"); + g_free (swaplist); + return; + } + + /* Total things up, returns in 512 bytes blocks! */ + for (i = 0; i < nswap; i++) { + if (swaplist[i].se_flags & SWF_ENABLE) { + avail += (swaplist[i].se_nblks / blockdiv); + inuse += (swaplist[i].se_inuse / blockdiv); + } + } + + /* Convert back to bytes, the libgtop2 is not clear about unites... */ + avail *= 512; + inuse *= 512; + + g_free (swaplist); + + buf->flags = _glibtop_sysdeps_swap; + + buf->used = inuse; + buf->free = avail; + + buf->total = inuse + avail; +} diff --git a/sysdeps/openbsd/sysinfo.c b/sysdeps/openbsd/sysinfo.c new file mode 100644 index 00000000..9b748ba0 --- /dev/null +++ b/sysdeps/openbsd/sysinfo.c @@ -0,0 +1,116 @@ +/* $OpenBSD: sysinfo.c,v 1.3 2011/05/23 19:35:56 jasper Exp $ */ + +/* Copyright (C) 1998-99 Martin Baulig + This file is part of LibGTop 1.0. + + Contributed by Martin Baulig <martin@home-of-linux.org>, April 1998. + + LibGTop is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, + or (at your option) any later version. + + LibGTop is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with LibGTop; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +#include <config.h> +#include <sys/param.h> +#include <sys/types.h> +#include <sys/sysctl.h> +#include <glibtop/error.h> +#include <glibtop/cpu.h> +#include <glibtop/sysinfo.h> + +static const unsigned long _glibtop_sysdeps_sysinfo = +(1L << GLIBTOP_SYSINFO_CPUINFO); + +static glibtop_sysinfo sysinfo = { .flags = 0 }; + +static void +init_sysinfo (glibtop *server) +{ + char *model; + int mib[2]; + int ncpus = 1; + int mhz = 0; + size_t len; + + if (G_LIKELY (sysinfo.flags)) + return; + + glibtop_init_s (&server, GLIBTOP_SYSDEPS_CPU, 0); + + mib[0] = CTL_HW; + + /* Get the number of CPU's present */ + mib[1] = HW_NCPU; + + len = sizeof(ncpus); + if (sysctl(mib, 2, &ncpus, &len, NULL, 0) != 0) + printf("Couldn't determine hw.ncpu.\n"); + + /* Get the CPU model */ + mib[1] = HW_MODEL; + len = 0; + + if (sysctl(mib, 2, NULL, &len, NULL, 0) != -1) { + model = g_malloc (len); + sysctl(mib, 2, model, &len, NULL, 0); + } else { + printf("Couldn't determine hw.model.\n"); + } + + /* Get the clockrate */ + mib[1] = HW_CPUSPEED; + len = sizeof(mhz); + + if (sysctl(mib, 2, &mhz, &len, NULL, 0) != 0) + printf("Couldn't determine hw.cpuspeed.\n"); + + for (sysinfo.ncpu = 0; + sysinfo.ncpu < GLIBTOP_NCPU && sysinfo.ncpu < ncpus; + sysinfo.ncpu++) { + glibtop_entry * const cpuinfo = &sysinfo.cpuinfo[sysinfo.ncpu]; + + cpuinfo->labels = g_ptr_array_new (); + + cpuinfo->values = g_hash_table_new_full(g_str_hash, + g_str_equal, + NULL, g_free); + + g_ptr_array_add (cpuinfo->labels, "processor"); + g_hash_table_insert (cpuinfo->values, "processor", + g_strdup_printf("%u", (guint)sysinfo.ncpu)); + + g_ptr_array_add (cpuinfo->labels, "vendor_id"); + g_hash_table_insert (cpuinfo->values, "vendor_id", + g_strdup(model)); + + g_ptr_array_add (cpuinfo->labels, "model name"); + g_hash_table_insert (cpuinfo->values, "model name", + g_strdup(model)); + + g_ptr_array_add (cpuinfo->labels, "cpu MHz"); + g_hash_table_insert (cpuinfo->values, "cpu MHz", + g_strdup_printf("%d", mhz)); + } + + g_free (model); + + sysinfo.flags = _glibtop_sysdeps_sysinfo; +} + +const glibtop_sysinfo * +glibtop_get_sysinfo_s (glibtop *server) +{ + init_sysinfo (server); + return &sysinfo; +} diff --git a/sysdeps/openbsd/uptime.c b/sysdeps/openbsd/uptime.c new file mode 100644 index 00000000..fdfd44c6 --- /dev/null +++ b/sysdeps/openbsd/uptime.c @@ -0,0 +1,70 @@ +/* $OpenBSD: uptime.c,v 1.3 2011/05/23 19:35:57 jasper Exp $ */ + +/* Copyright (C) 1998-99 Martin Baulig + This file is part of LibGTop 1.0. + + Contributed by Martin Baulig <martin@home-of-linux.org>, April 1998. + + LibGTop is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, + or (at your option) any later version. + + LibGTop is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with LibGTop; see the file COPYING. If not, write to the + Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. +*/ + +#include <config.h> +#include <glibtop.h> +#include <glibtop/error.h> +#include <glibtop/uptime.h> + +#include <glibtop/cpu.h> + +#include <glibtop_suid.h> + +static const unsigned long _glibtop_sysdeps_uptime = +(1L << GLIBTOP_UPTIME_UPTIME) + (1L << GLIBTOP_UPTIME_IDLETIME); + +static const unsigned long _required_cpu_flags = +(1L << GLIBTOP_CPU_TOTAL) + (1L << GLIBTOP_CPU_IDLE) + +(1L << GLIBTOP_CPU_FREQUENCY); + +/* Init function. */ + +void +_glibtop_init_uptime_p (glibtop *server) +{ + server->sysdeps.uptime = _glibtop_sysdeps_uptime; +} + +/* Provides uptime and idle time. */ + +void +glibtop_get_uptime_p (glibtop *server, glibtop_uptime *buf) +{ + time_t now; + time_t uptime; + int mib[2]; + struct timeval boottime; + size_t size; + + mib[0] = CTL_KERN; + mib[1] = KERN_BOOTTIME; + size = sizeof(boottime); + if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 && + boottime.tv_sec != 0) { + time(&now); + buf->uptime = now - boottime.tv_sec; + /* XXX: don't know a useful value to put here. */ + buf->idletime = 0; + buf->flags = _glibtop_sysdeps_uptime; + } +} |