Moved all sysdeps code to the new libgtop-backends module.

2001-04-20  Martin Baulig  <baulig@suse.de>

	Moved all sysdeps code to the new libgtop-backends module.

	* sysdeps/freesd/*: Removed.
	* sysdeps/kernel/*: Removed.
	* sysdeps/linux/*: Removed.
	* sysdeps/osf1/*: Removed.
	* sysdeps/solaris/*: Removed.
	* sysdeps/stub/*: Removed.
	* sysdeps/stub_suid/*: Removed.
	* sysdeps/sun4/*: Removed.
This commit is contained in:
Martin Baulig
2001-04-20 21:06:35 +00:00
committed by Martin Baulig
parent 5bfb085021
commit 7a7c6baf55
254 changed files with 13 additions and 21595 deletions

View File

@@ -1,6 +0,0 @@
.deps
.libs
Makefile
Makefile.in
libgtop_sysdeps.la
*.lo

View File

@@ -1,160 +0,0 @@
2000-04-24 Martin Baulig <baulig@suse.de>
* proc_cwd.c: New file. This feature was requested by Nautilus
hacker Maciej Stachowiak and it returns the current working
directory of a process.
2000-02-22 Martin Baulig <martin@home-of-linux.org>
* Makefile.am: Only install the libs in $(LIBGTOP_BACKEND_DIR)
when we LIBGTOP_USE_GMODULE and in $(libdir) otherwise.
2000-02-06 Martin Baulig <martin@home-of-linux.org>
* ppp.c: Make this work with multiple ISDN devices.
2000-01-22 Martin Baulig <martin@home-of-linux.org>
* Makefile.am: Install the library in $(backenddir).
1999-12-11 Martin Baulig <martin@home-of-linux.org>
* cpu.c: Only set smp flags when we're really on a SMP machine.
1999-11-28 Martin Baulig <martin@home-of-linux.org>
* procstate.c (glibtop_get_procstate_s): Stat "/proc/<pid>" instead
of "/proc/<pid>/state" to get `uid' and `gid'. Thanks to Jason Becker
for pointing out that GTop reported wrong uid's.
1999-11-21 Martin Baulig <martin@home-of-linux.org>
* interfaces.c: New file.
(glibtop_init_interface_names_s): New function.
(glibtop_get_interface_names_s): New function.
1999-11-21 Martin Baulig <martin@home-of-linux.org>
* proclist.c: (glibtop_get_proclist_s): Support GLIBTOP_KERN_PROC_PPID.
1999-10-19 Martin Baulig <martin@home-of-linux.org>
* uptime.c (glibtop_get_uptime_s): Don't cast uptime and idletime
to float as suggested by John Kodis.
1999-09-28 Radek Doulik <rodo@praha.eridan.cz>
* proclist.c (glibtop_get_proclist_s): fixed idle processes filtering
1999-07-29 Martin Baulig <martin@home-of-linux.org>
* proctime.c (glibtop_get_proctime_s): Don't provide `rtime'
any longer since we must not use `rtime = utime + stime'.
1999-02-20 Martin Baulig <martin@home-of-linux.org>
* netload.c: Don't include <linux/ip_fw.h> for libc5 systems
since it is no longer required and makes trouble on Slakware.
1999-01-06 Martin Baulig <martin@home-of-linux.org>
* netload.c (glibtop_get_netload_s): Use `server->os_version_code'
instead of GLIBTOP_LINUX_VERSION_CODE.
1999-01-06 Martin Baulig <martin@home-of-linux.org>
Unconditionally enable SMP support for linux.
* open.c (_glibtop_open_s): Always determine the number of CPUs
we have here, not only when SMP support was enabled.
* cpu.c, proctime.c: Use `server->ncpu' to determine whether to
enable SMP support.
1998-12-28 Martin Baulig <martin@home-of-linux.org>
* netload.c: When using glibc, don't include <netinet/ip_fw.h>
any longer but all headers that were included there; we only
needed this file to get those headers.
1998-12-09 Martin Baulig <martin@home-of-linux.org>
* procargs.c (glibtop_get_proc_args_s): Added implementation
for this function.
1998-12-03 Martin Baulig <martin@home-of-linux.org>
* netload.c: No longer include <linux/version.h>, but use
GLIBTOP_LINUX_VERSION_CODE which is set by configure instead.
Define _GLIBTOP_IP_FW_ACCTIN and _GLIBTOP_IP_FW_ACCTOUT here
and use them instead of IP_FW_F_ACCTIN and IP_FW_F_ACCTOUT.
1998-11-30 Martin Baulig <martin@home-of-linux.org>
* netload.c: Use correct header files both for libc5 and glibc.
With Linux >= 2.1.114 we no longer use IP accounting since it
already has byte counts in /proc/net/dev.
1998-11-22 Martin Baulig <baulig@merkur.uni-trier.de>
* netload.c (glibtop_get_netload_s): Added implementation
for this function.
The code here is smart enough to use /proc/net/ip_acct if
IP accounting is enabled in the kernel and activated on the
requested device and /proc/net/dev if not.
To get separate statistics for received and transmitted
packets you need to use two accounting rules:
ipfwadm -A in -a -P all -W eth0
ipfwadm -A out -a -P all -W eth0
But before you activate IP accounting, please have a look
at /proc/net/dev - if if already contains byte counters,
then don't use IP accounting.
1998-10-28 Martin Baulig <martin@home-of-linux.org>
* ppp.c: Added code for ISDN here.
1998-10-26 Martin Baulig <martin@home-of-linux.org>
* netload.c: New file. Currently empty.
1998-10-26 Martin Baulig <martin@home-of-linux.org>
* procargs.c: New file. Currently empty.
1998-10-25 Martin Baulig <martin@home-of-linux.org>
* ppp.c: New file. Currently empty.
1998-10-11 Martin Baulig <martin@home-of-linux.org>
* procdata.c: Removed this obsolete file.
1998-10-02 Martin Baulig <martin@home-of-linux.org>
* proclist.c (glibtop_init_proclist_s): You can now also use the
`GLIBTOP_EXCLUDE_*' flags for the `which' parameter.
1998-10-01 Martin Baulig <baulig@Stud.Informatik.uni-trier.de>
* proclist.c (glibtop_init_proclist_s): Honor the `which' parameter
to select which processes to fetch.
1998-09-12 Martin Baulig <martin@home-of-linux.org>
* sysinfo.c: New file.
1998-09-09 Martin Baulig <baulig@Stud.Informatik.uni-trier.de>
* proctime.c (glibtop_get_proc_time_s): Added SMP support.
* open.c (glibtop_init_s): Initialize `ncpu' on SMP systems.
* cpu.c (glibtop_get_cpu_s): Added SMP support.
* ChangeLog: New file.

View File

@@ -1,27 +0,0 @@
LINK = $(LIBTOOL) --mode=link $(CC) $(CFLAGS) $(LDFLAGS) -o $@
INCLUDES = @INCLUDES@
dynamic_ldflags = @libgtop_dynamic_ldflags@
backenddir = @LIBGTOP_BACKEND_DIR@
if LIBGTOP_USE_GMODULE
backendlibdir = $(backenddir)
else
backendlibdir = $(libdir)
endif
backendlib_LTLIBRARIES = libgtop_sysdeps.la
libgtop_sysdeps_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 proccwd.c procargs.c procmap.c \
sysinfo.c interfaces.c netinfo.c netload.c ppp.c
libgtop_sysdeps_la_LIBADD = @GLIB_LIBS@
libgtop_sysdeps_la_LDFLAGS = $(LT_VERSION_INFO) $(dynamic_ldflags)
include_HEADERS = glibtop_server.h glibtop_machine.h

View File

@@ -1,37 +0,0 @@
<?xml version="1.0"?> <!-- -*-xml-*- -->
<!DOCTYPE libgtop:backendinfo SYSTEM 'libgtop-backend-info.dtd'>
<libgtop:BackendInfo xmlns:libgtop="http://www.home-of-linux.org/libgtop/1.1">
<name>
<short_name>
linux-sysdeps
</short_name>
<long_name>
Linux sysdeps port.
</long_name>
</name>
<description>
This is the Linux sysdeps port which uses the /proc filesystem
to get all the data.
</description>
<authors>
<author>
<author_name>
Martin Baulig
</author_name>
<author_email>
martin@home-of-linux.org
</author_email>
</author>
</authors>
<legalnotice>
<license>
GNU General Public License
</license>
</legalnotice>
</libgtop:BackendInfo>

View File

@@ -1,32 +0,0 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* $Id$ */
/* 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 <glibtop/close.h>
/* Closes pipe to gtop server. */
void
glibtop_close_s (glibtop *server)
{ }

View File

@@ -1,125 +0,0 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* $Id$ */
/* 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/error.h>
#include <glibtop/cpu.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);
static const unsigned long _glibtop_sysdeps_cpu_smp =
(1L << GLIBTOP_CPU_XCPU_TOTAL) + (1L << GLIBTOP_CPU_XCPU_USER) +
(1L << GLIBTOP_CPU_XCPU_NICE) + (1L << GLIBTOP_CPU_XCPU_SYS) +
(1L << GLIBTOP_CPU_XCPU_IDLE) + (1L << GLIBTOP_CPU_XCPU_FLAGS);
/* Init function. */
int
glibtop_init_cpu_s (glibtop *server)
{
server->sysdeps.cpu = _glibtop_sysdeps_cpu;
if (server->ncpu)
server->sysdeps.cpu |= _glibtop_sysdeps_cpu_smp;
return 0;
}
/* Provides information about cpu usage. */
#define FILENAME "/proc/stat"
int
glibtop_get_cpu_s (glibtop *server, glibtop_cpu *buf)
{
char buffer [BUFSIZ], *p;
int fd, len, i;
u_int64_t total;
glibtop_init_s (&server, GLIBTOP_SYSDEPS_CPU, 0);
memset (buf, 0, sizeof (glibtop_cpu));
fd = open (FILENAME, O_RDONLY);
if (fd < 0) {
glibtop_warn_io_r (server, "open (%s)", FILENAME);
return -1;
}
len = read (fd, buffer, BUFSIZ-1);
if (len < 0) {
close (fd);
glibtop_warn_io_r (server, "read (%s)", FILENAME);
return -1;
}
close (fd);
buffer [len] = '\0';
p = skip_token (buffer); /* "cpu" */
buf->user = strtoul (p, &p, 0);
buf->nice = strtoul (p, &p, 0);
buf->sys = strtoul (p, &p, 0);
buf->idle = strtoul (p, &p, 0);
total = buf->user;
total += buf->nice;
total += buf->sys;
total += buf->idle;
buf->total = total;
buf->frequency = 100;
buf->flags = _glibtop_sysdeps_cpu;
for (i = 0; i < server->ncpu; i++) {
if (strncmp (p+1, "cpu", 3) || !isdigit (p [4]))
break;
buf->xcpu_flags |= (1L << (u_int64_t) i);
p += 6;
buf->xcpu_user [i] = strtoul (p, &p, 0);
buf->xcpu_nice [i] = strtoul (p, &p, 0);
buf->xcpu_sys [i] = strtoul (p, &p, 0);
buf->xcpu_idle [i] = strtoul (p, &p, 0);
total = buf->xcpu_user [i];
total += buf->xcpu_nice [i];
total += buf->xcpu_sys [i];
total += buf->xcpu_idle [i];
buf->xcpu_total [i] = total;
}
if (buf->xcpu_flags)
buf->flags |= _glibtop_sysdeps_cpu_smp;
return 0;
}

View File

@@ -1,60 +0,0 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* $Id$ */
/* 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 <unistd.h>
#include <fcntl.h>
#include <ctype.h>
BEGIN_LIBGTOP_DECLS
#if _IN_LIBGTOP
static inline char *
skip_token(const char *p)
{
while (isspace(*p)) p++;
while (*p && !isspace(*p)) p++;
return (char *)p;
}
#endif
typedef struct _glibtop_machine glibtop_machine;
struct _glibtop_machine
{
pid_t last_pid;
int no_update;
int fd_stat, fd_meminfo, fd_loadavg;
char proc_stat [BUFSIZ], proc_statm [BUFSIZ];
char proc_status [BUFSIZ];
};
END_LIBGTOP_DECLS
#endif

View File

@@ -1,139 +0,0 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* $Id$ */
/* 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__
#include <fcntl.h>
#include <ctype.h>
BEGIN_LIBGTOP_DECLS
#ifdef _IN_LIBGTOP
static inline char *
skip_token (const char *p)
{
while (isspace(*p)) p++;
while (*p && !isspace(*p)) p++;
return (char *)p;
}
static inline char *
skip_multiple_token (const char *p, int count)
{
int i;
for (i = 0; i < count; i++)
p = skip_token (p);
return (char *)p;
}
static inline char *
skip_line (const char *p)
{
while (*p != '\n') p++;
return (char *) ++p;
}
static inline int
proc_file_to_buffer (char *buffer, const char *fmt, pid_t pid)
{
char filename [BUFSIZ];
int fd, len;
sprintf (filename, fmt, pid);
fd = open (filename, O_RDONLY);
if (fd < 0) return -1;
len = read (fd, buffer, BUFSIZ-1);
close (fd);
if (len < 0)
return -1;
buffer [len] = '\0';
return 0;
}
static inline int
proc_stat_to_buffer (char *buffer, pid_t pid)
{
return proc_file_to_buffer (buffer, "/proc/%d/stat", pid);
}
static inline int
proc_status_to_buffer (char *buffer, pid_t pid)
{
return proc_file_to_buffer (buffer, "/proc/%d/status", pid);
}
static inline int
proc_statm_to_buffer (char *buffer, pid_t pid)
{
return proc_file_to_buffer (buffer, "/proc/%d/statm", pid);
}
static inline char *
proc_stat_after_cmd (char *p)
{
p = strrchr (p, ')');
if (!p) return p;
*p++ = '\0';
return p;
}
#endif
#define GLIBTOP_SUID_CPU 0
#define GLIBTOP_SUID_MEM 0
#define GLIBTOP_SUID_SWAP 0
#define GLIBTOP_SUID_UPTIME 0
#define GLIBTOP_SUID_LOADAVG 0
#define GLIBTOP_SUID_SHM_LIMITS 0
#define GLIBTOP_SUID_MSG_LIMITS 0
#define GLIBTOP_SUID_SEM_LIMITS 0
#define GLIBTOP_SUID_PROCLIST 0
#define GLIBTOP_SUID_PROC_STATE 0
#define GLIBTOP_SUID_PROC_UID 0
#define GLIBTOP_SUID_PROC_MEM 0
#define GLIBTOP_SUID_PROC_TIME 0
#define GLIBTOP_SUID_PROC_SIGNAL 0
#define GLIBTOP_SUID_PROC_KERNEL 0
#define GLIBTOP_SUID_PROC_SEGMENT 0
#define GLIBTOP_SUID_PROC_ARGS 0
#define GLIBTOP_SUID_PROC_MAP 0
#define GLIBTOP_SUID_NETINFO 0
#define GLIBTOP_SUID_NETLOAD 0
#define GLIBTOP_SUID_INTERFACE_NAMES 0
#define GLIBTOP_SUID_PPP 0
END_LIBGTOP_DECLS
#endif

View File

@@ -1,131 +0,0 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* $Id$ */
/* 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 <glibtop.h>
#include <glibtop/error.h>
#include <glibtop/xmalloc.h>
#include <glibtop/interfaces.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <netinet/in.h>
#include <net/if.h>
static const unsigned long _glibtop_sysdeps_interface_names =
(1L << GLIBTOP_INTERFACE_NAMES_NUMBER) +
(1L << GLIBTOP_INTERFACE_NAMES_SIZE);
/* Init function. */
int
glibtop_init_interface_names_s (glibtop *server)
{
server->sysdeps.interface_names = _glibtop_sysdeps_interface_names;
return 0;
}
/* Provides network statistics. */
glibtop_interface *
glibtop_get_interface_names_s (glibtop *server, glibtop_array *array,
u_int64_t interface, u_int64_t number,
u_int64_t instance, u_int64_t strategy)
{
GPtrArray *parray;
u_int64_t strategy_flags;
glibtop_interface *retval = NULL;
int skfd, i, n, numreqs = 30;
struct ifconf ifc;
struct ifreq *ifr;
glibtop_init_s (&server, GLIBTOP_SYSDEPS_INTERFACE_NAMES, 0);
memset (array, 0, sizeof (glibtop_array));
strategy_flags = strategy & GLIBTOP_STRATEGY_FLAGS_MASK;
strategy &= ~GLIBTOP_STRATEGY_FLAGS_MASK;
parray = g_ptr_array_new ();
skfd = socket (AF_INET, SOCK_DGRAM, 0);
if (!skfd) {
glibtop_error_io_r (server, "socket (AF_INET, SOCK_DGRAM, 0)");
return NULL;
}
ifc.ifc_buf = NULL;
for (;;) {
ifc.ifc_len = sizeof (struct ifreq) * numreqs;
ifc.ifc_buf = glibtop_realloc_r (server, ifc.ifc_buf, ifc.ifc_len);
if (ioctl (skfd, SIOCGIFCONF, &ifc) < 0) {
glibtop_error_io_r (server, "SIOCGIFCONF");
glibtop_free_r (server, ifc.ifc_buf);
return NULL;
}
if (ifc.ifc_len == sizeof (struct ifreq) * numreqs) {
/* assume it overflowed and try again */
numreqs += 10;
continue;
}
break;
}
ifr = ifc.ifc_req;
for (n = 0; n < ifc.ifc_len; n += sizeof(struct ifreq), ifr++) {
glibtop_interface *interface_ptr;
if (strchr (ifr->ifr_name, ':')) {
/* This is a logical interface */
if (!(strategy_flags & GLIBTOP_INTERFACES_INCLUDE_LOGICAL))
continue;
}
interface_ptr = g_new0 (glibtop_interface, 1);
strcpy (interface_ptr->name, ifr->ifr_name);
g_ptr_array_add (parray, interface_ptr);
}
glibtop_free_r (server, ifc.ifc_buf);
close (skfd);
retval = glibtop_calloc_r (server, parray->len, sizeof (glibtop_interface));
for (i = 0; i < parray->len; i++)
retval [i] = *(glibtop_interface *) parray->pdata [i];
array->number = parray->len;
array->size = sizeof (glibtop_interface);
array->total = array->number * array->size;
g_ptr_array_free (parray, TRUE);
return retval;
}

View File

@@ -1,105 +0,0 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* $Id$ */
/* 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/error.h>
#include <glibtop/loadavg.h>
static const unsigned long _glibtop_sysdeps_loadavg =
(1L << GLIBTOP_LOADAVG_LOADAVG);
static const unsigned long _glibtop_sysdeps_loadavg_tasks =
(1L << GLIBTOP_LOADAVG_NR_RUNNING) +
(1L << GLIBTOP_LOADAVG_NR_TASKS) +
(1L << GLIBTOP_LOADAVG_LAST_PID);
/* Init function. */
int
glibtop_init_loadavg_s (glibtop *server)
{
server->sysdeps.loadavg = _glibtop_sysdeps_loadavg;
return 0;
}
/* Provides load load averange. */
#define FILENAME "/proc/loadavg"
int
glibtop_get_loadavg_s (glibtop *server, glibtop_loadavg *buf)
{
char buffer [BUFSIZ], *p, *old;
int fd, len;
glibtop_init_s (&server, GLIBTOP_SYSDEPS_LOADAVG, 0);
memset (buf, 0, sizeof (glibtop_loadavg));
fd = open (FILENAME, O_RDONLY);
if (fd < 0) {
glibtop_warn_io_r (server, "open (%s)", FILENAME);
return -1;
}
len = read (fd, buffer, BUFSIZ-1);
if (len < 0) {
close (fd);
glibtop_warn_io_r (server, "read (%s)", FILENAME);
return -1;
}
close (fd);
buffer [len] = '\0';
buf->loadavg [0] = (float) strtod (buffer, &p);
buf->loadavg [1] = (float) strtod (p, &p);
buf->loadavg [2] = (float) strtod (p, &p);
buf->flags = _glibtop_sysdeps_loadavg;
while (isspace(*p)) p++;
/* Older Linux versions don't have the nr_running/nr_tasks fields. */
old = p;
while (*p) {
if (*p == '/')
break;
if (!isdigit (*p))
return 0;
p++;
}
buf->nr_running = strtoul (old, &p, 0); p++;
buf->nr_tasks = strtoul (p, &p, 0);
buf->last_pid = strtoul (p, &p, 0);
buf->flags |= _glibtop_sysdeps_loadavg_tasks;
return 0;
}

View File

@@ -1,93 +0,0 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* $Id$ */
/* 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/error.h>
#include <glibtop/mem.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_CACHED) +
(1L << GLIBTOP_MEM_USER);
/* Init function. */
int
glibtop_init_mem_s (glibtop *server)
{
server->sysdeps.mem = _glibtop_sysdeps_mem;
return 0;
}
/* Provides information about memory usage. */
#define FILENAME "/proc/meminfo"
int
glibtop_get_mem_s (glibtop *server, glibtop_mem *buf)
{
char buffer [BUFSIZ], *p;
int fd, len;
glibtop_init_s (&server, GLIBTOP_SYSDEPS_MEM, 0);
memset (buf, 0, sizeof (glibtop_mem));
fd = open (FILENAME, O_RDONLY);
if (fd < 0) {
glibtop_warn_io_r (server, "open (%s)", FILENAME);
return -1;
}
len = read (fd, buffer, BUFSIZ-1);
if (len < 0) {
close (fd);
glibtop_warn_io_r (server, "read (%s)", FILENAME);
return -1;
}
close (fd);
buffer [len] = '\0';
p = skip_line (buffer);
p = skip_token (p); /* "Mem:" */
buf->total = strtoul (p, &p, 0);
buf->used = strtoul (p, &p, 0);
buf->free = strtoul (p, &p, 0);
buf->shared = strtoul (p, &p, 0);
buf->buffer = strtoul (p, &p, 0);
buf->cached = strtoul (p, &p, 0);
buf->user = buf->total - buf->free - buf->shared - buf->buffer;
buf->flags = _glibtop_sysdeps_mem;
return 0;
}

View File

@@ -1,71 +0,0 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* $Id$ */
/* 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 <glibtop/msg_limits.h>
#include <sys/ipc.h>
#include <sys/msg.h>
static const unsigned long _glibtop_sysdeps_msg_limits =
(1L << GLIBTOP_MSG_LIMITS_MSGPOOL) + (1L << GLIBTOP_MSG_LIMITS_MSGMAP) +
(1L << GLIBTOP_MSG_LIMITS_MSGMAX) + (1L << GLIBTOP_MSG_LIMITS_MSGMNB) +
(1L << GLIBTOP_MSG_LIMITS_MSGMNI) + (1L << GLIBTOP_MSG_LIMITS_MSGSSZ) +
(1L << GLIBTOP_MSG_LIMITS_MSGTQL);
/* Init function. */
int
glibtop_init_msg_limits_s (glibtop *server)
{
server->sysdeps.msg_limits = _glibtop_sysdeps_msg_limits;
return 0;
}
/* Provides information about sysv ipc limits. */
int
glibtop_get_msg_limits_s (glibtop *server, glibtop_msg_limits *buf)
{
struct msginfo msginfo;
glibtop_init_s (&server, GLIBTOP_SYSDEPS_MSG_LIMITS, 0);
memset (buf, 0, sizeof (glibtop_msg_limits));
buf->flags = _glibtop_sysdeps_msg_limits;
msgctl (0, IPC_INFO, (struct msqid_ds *) &msginfo);
buf->msgpool = msginfo.msgpool;
buf->msgmap = msginfo.msgmap;
buf->msgmax = msginfo.msgmax;
buf->msgmnb = msginfo.msgmnb;
buf->msgmni = msginfo.msgmni;
buf->msgssz = msginfo.msgssz;
buf->msgtql = msginfo.msgtql;
return 0;
}

View File

@@ -1,383 +0,0 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* $Id$ */
/* 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 <glibtop.h>
#include <glibtop/error.h>
#include <glibtop/xmalloc.h>
#include <glibtop/netinfo.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/sysctl.h>
#ifndef HAVE_AFINET
#define HAVE_AFINET 1
#endif
#ifndef HAVE_AFINET6
#define HAVE_AFINET6 1
#endif
#ifdef HAVE_AFINET6
#define _PATH_PROCNET_IFINET6 "/proc/net/if_inet6"
#define IPV6_ADDR_LOOPBACK 0x0010U
#define IPV6_ADDR_LINKLOCAL 0x0020U
#define IPV6_ADDR_SITELOCAL 0x0040U
#define IPV6_ADDR_COMPATv4 0x0080U
#endif /* HAVE_AFINET6 */
#if !defined (_LIBC) && defined (__GNU_LIBRARY__) && __GNU_LIBRARY__ > 1
/* GNU LibC */
#include <net/if.h>
#include <netinet/ip_icmp.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include <net/if.h>
#else /* Libc 5 */
#include <linux/if.h>
#include <linux/in.h>
#include <linux/ip.h>
#include <linux/icmp.h>
#include <linux/tcp.h>
#include <linux/udp.h>
#endif
static const unsigned long _glibtop_sysdeps_netinfo =
(1L << GLIBTOP_NETINFO_TRANSPORT) +
(1L << GLIBTOP_NETINFO_IF_FLAGS) +
(1L << GLIBTOP_NETINFO_ADDRESS) +
(1L << GLIBTOP_NETINFO_SUBNET) +
(1L << GLIBTOP_NETINFO_MTU);
/* Init function. */
int
glibtop_init_netinfo_s (glibtop *server)
{
server->sysdeps.netinfo = _glibtop_sysdeps_netinfo;
return 0;
}
#ifdef HAVE_AFINET
static int
_netinfo_ipv4 (glibtop *server, glibtop_netinfo *buf,
const char *interface, glibtop_ifaddr *address)
{
int skfd;
skfd = socket (AF_INET, SOCK_DGRAM, 0);
if (skfd) {
struct ifreq ifr;
unsigned flags;
if (address) {
address->transport = GLIBTOP_TRANSPORT_IPV4;
address->flags |= (1L << GLIBTOP_IFADDR_TRANSPORT);
buf->transport = GLIBTOP_TRANSPORT_IPV4;
buf->flags |= (1L << GLIBTOP_NETINFO_TRANSPORT);
}
strcpy (ifr.ifr_name, interface);
if (!ioctl (skfd, SIOCGIFFLAGS, &ifr)) {
buf->flags |= (1L << GLIBTOP_NETINFO_IF_FLAGS);
flags = ifr.ifr_flags;
} else
flags = 0;
if (flags & IFF_UP)
buf->if_flags |= (1L << GLIBTOP_IF_FLAGS_UP);
if (flags & IFF_BROADCAST)
buf->if_flags |= (1L << GLIBTOP_IF_FLAGS_BROADCAST);
if (flags & IFF_DEBUG)
buf->if_flags |= (1L << GLIBTOP_IF_FLAGS_DEBUG);
if (flags & IFF_LOOPBACK)
buf->if_flags |= (1L << GLIBTOP_IF_FLAGS_LOOPBACK);
if (flags & IFF_POINTOPOINT)
buf->if_flags |= (1L << GLIBTOP_IF_FLAGS_POINTOPOINT);
if (flags & IFF_RUNNING)
buf->if_flags |= (1L << GLIBTOP_IF_FLAGS_RUNNING);
if (flags & IFF_NOARP)
buf->if_flags |= (1L << GLIBTOP_IF_FLAGS_NOARP);
if (flags & IFF_PROMISC)
buf->if_flags |= (1L << GLIBTOP_IF_FLAGS_PROMISC);
if (flags & IFF_ALLMULTI)
buf->if_flags |= (1L << GLIBTOP_IF_FLAGS_ALLMULTI);
if (flags & IFF_MULTICAST)
buf->if_flags |= (1L << GLIBTOP_IF_FLAGS_MULTICAST);
if (address) {
strcpy (ifr.ifr_name, interface);
if (!ioctl (skfd, SIOCGIFADDR, &ifr)) {
struct sockaddr_in addr =
*(struct sockaddr_in *) &ifr.ifr_addr;
address->addr_len = 4;
memcpy (&address->address, &addr.sin_addr.s_addr, 4);
address->flags |= (1L << GLIBTOP_IFADDR_ADDRESS);
address->flags |= (1L << GLIBTOP_IFADDR_ADDR_LEN);
}
strcpy (ifr.ifr_name, interface);
if (!ioctl (skfd, SIOCGIFNETMASK, &ifr)) {
struct sockaddr_in addr =
*(struct sockaddr_in *) &ifr.ifr_addr;
memcpy (&address->subnet, &addr.sin_addr.s_addr, 4);
address->flags |= (1L << GLIBTOP_IFADDR_SUBNET);
}
}
strcpy (ifr.ifr_name, interface);
if (!ioctl (skfd, SIOCGIFMTU, &ifr)) {
buf->mtu = ifr.ifr_mtu;
buf->flags |= (1L << GLIBTOP_NETINFO_MTU);
}
close (skfd);
}
return 0;
}
#endif /* HAVE_AFINET */
#ifdef HAVE_AFINET6
static int
_parse_ipv6_address (const char *addr_string, u_int8_t *dest)
{
int i;
if (strlen (addr_string) != 32)
return -1;
for (i = 0; i < 16; i++) {
char c1, c2;
int d1, d2;
c1 = tolower (addr_string [(i<<1)]);
c2 = tolower (addr_string [(i<<1)+1]);
if ((c1 >= '0') && (c1 <= '9'))
d1 = c1-'0';
else if ((c1 >= 'a') && (c1 <= 'f'))
d1 = c1-'a'+10;
else
return -1;
if ((c2 >= '0') && (c2 <= '9'))
d2 = c2-'0';
else if ((c2 >= 'a') && (c2 <= 'f'))
d2 = c2-'a'+10;
else
return -1;
dest [i] = (d1 << 4) + d2;
}
return 0;
}
static int
_netinfo_ipv6 (glibtop *server, glibtop_netinfo *buf,
const char *interface, GPtrArray *parray)
{
FILE *f;
char addr6[40], devname[20];
int plen, scope, dad_status, if_idx;
#ifdef HAVE_AFINET6
/* get common things such as mtu and if_flags */
_netinfo_ipv4 (server, buf, interface, NULL);
#endif
buf->transport = GLIBTOP_TRANSPORT_IPV6;
buf->flags |= (1L << GLIBTOP_NETINFO_TRANSPORT);
f = fopen (_PATH_PROCNET_IFINET6, "r");
if (f != NULL) {
while (fscanf (f, "%64s %02x %02x %02x %02x %20s\n",
addr6, &if_idx, &plen, &scope, &dad_status,
devname) != EOF) {
glibtop_ifaddr address;
memset (&address, 0, sizeof (glibtop_ifaddr));
if (strcmp (devname, interface))
continue;
if (!_parse_ipv6_address (addr6, address.address))
address.flags |= (1L << GLIBTOP_IFADDR_ADDRESS);
address.transport = GLIBTOP_TRANSPORT_IPV6;
address.subnet = plen;
switch (scope) {
case 0:
address.scope = GLIBTOP_IPV6_SCOPE_GLOBAL;
break;
case IPV6_ADDR_LINKLOCAL:
address.scope = GLIBTOP_IPV6_SCOPE_LINKLOCAL;
break;
case IPV6_ADDR_SITELOCAL:
address.scope = GLIBTOP_IPV6_SCOPE_SITELOCAL;
break;
case IPV6_ADDR_COMPATv4:
address.scope = GLIBTOP_IPV6_SCOPE_COMPATv4;
break;
case IPV6_ADDR_LOOPBACK:
address.scope = GLIBTOP_IPV6_SCOPE_LOOPBACK;
break;
default:
address.scope = GLIBTOP_IPV6_SCOPE_UNKNOWN;
break;
}
address.addr_len = 8;
address.flags |= (1L << GLIBTOP_IFADDR_TRANSPORT) |
(1L << GLIBTOP_IFADDR_ADDR_LEN) |
(1L << GLIBTOP_IFADDR_SUBNET) |
(1L << GLIBTOP_IFADDR_SCOPE);
g_ptr_array_add (parray, g_memdup (&address, sizeof (address)));
}
}
fclose (f);
return 0;
}
#endif /* HAVE_AFINET6 */
/* Provides network statistics. */
glibtop_ifaddr *
glibtop_get_netinfo_s (glibtop *server, glibtop_array *array,
glibtop_netinfo *buf, const char *interface,
u_int64_t transport)
{
GPtrArray *parray;
glibtop_ifaddr *retval = NULL;
int i;
memset (buf, 0, sizeof (glibtop_netinfo));
if (strlen (interface) >= GLIBTOP_INTERFACE_LEN)
return NULL;
/* Assume IPv4 is the standard until IPv6 becomes more popular. */
if (transport == GLIBTOP_TRANSPORT_DEFAULT)
transport = GLIBTOP_TRANSPORT_ALL;
/* Get information about all possible transport methods. */
if (transport == GLIBTOP_TRANSPORT_ALL) {
char buffer [BUFSIZ];
struct stat statb;
/* We may get a little speed improvement when we use sysctl ()
* directly, but the following piece of code seems very stable
* and reliable to me.
*
* The first stat() on "/proc/sys/net" is done to find out whether
* the kernel has sysctl support.
*
* January 23, 1999
* Martin
*/
if (!stat ("/proc/sys/net", &statb) && S_ISDIR (statb.st_mode)) {
buf->flags |= (1L << GLIBTOP_NETINFO_TRANSPORT);
sprintf (buffer, "/proc/sys/net/ipv4/conf/%s", interface);
if (!stat (buffer, &statb) && S_ISDIR (statb.st_mode))
buf->transport |= GLIBTOP_TRANSPORT_IPV4;
sprintf (buffer, "/proc/sys/net/ipv6/conf/%s", interface);
if (!stat (buffer, &statb) && S_ISDIR (statb.st_mode))
buf->transport |= GLIBTOP_TRANSPORT_IPV6;
}
}
parray = g_ptr_array_new ();
#ifdef HAVE_AFINET
if (transport & GLIBTOP_TRANSPORT_IPV4) {
glibtop_ifaddr address;
memset (&address, 0, sizeof (glibtop_ifaddr));
_netinfo_ipv4 (server, buf, interface, &address);
if (address.flags & (1L << GLIBTOP_IFADDR_ADDRESS))
g_ptr_array_add (parray, g_memdup (&address, sizeof (address)));
}
#endif /* HAVE_AFINET */
#ifdef HAVE_AFINET6
if (transport & GLIBTOP_TRANSPORT_IPV6)
_netinfo_ipv6 (server, buf, interface, parray);
#endif /* HAVE_AFINET6 */
if (!parray->len) {
g_ptr_array_free (parray, TRUE);
return NULL;
}
retval = glibtop_calloc_r (server, parray->len, sizeof (glibtop_ifaddr));
for (i = 0; i < parray->len; i++)
retval [i] = *(glibtop_ifaddr *) parray->pdata [i];
array->number = parray->len;
array->size = sizeof (glibtop_ifaddr);
array->total = array->number * array->size;
g_ptr_array_free (parray, TRUE);
return retval;
}

View File

@@ -1,288 +0,0 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* $Id$ */
/* 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 <glibtop.h>
#include <glibtop/error.h>
#include <glibtop/netload.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#if !defined (_LIBC) && defined (__GNU_LIBRARY__) && __GNU_LIBRARY__ > 1
/* GNU LibC */
#include <net/if.h>
#include <netinet/ip_icmp.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include <net/if.h>
#else /* Libc 5 */
#include <linux/if.h>
#include <linux/in.h>
#include <linux/ip.h>
#include <linux/icmp.h>
#include <linux/tcp.h>
#include <linux/udp.h>
#endif
#define _GLIBTOP_IP_FW_ACCTIN 0x1000 /* Account incoming packets only. */
#define _GLIBTOP_IP_FW_ACCTOUT 0x2000 /* Account outgoing packets only. */
static const unsigned long _glibtop_sysdeps_netload =
(1L << GLIBTOP_NETLOAD_ERRORS_IN) +
(1L << GLIBTOP_NETLOAD_ERRORS_OUT) +
(1L << GLIBTOP_NETLOAD_COLLISIONS);
static const unsigned long _glibtop_sysdeps_netload_bytes =
(1L << GLIBTOP_NETLOAD_BYTES_IN) +
(1L << GLIBTOP_NETLOAD_BYTES_OUT) +
(1L << GLIBTOP_NETLOAD_BYTES_TOTAL);
static const unsigned long _glibtop_sysdeps_netload_packets =
(1L << GLIBTOP_NETLOAD_PACKETS_IN) +
(1L << GLIBTOP_NETLOAD_PACKETS_OUT) +
(1L << GLIBTOP_NETLOAD_PACKETS_TOTAL);
static const unsigned long _glibtop_sysdeps_netload_total =
(1L << GLIBTOP_NETLOAD_PACKETS_TOTAL) +
(1L << GLIBTOP_NETLOAD_BYTES_TOTAL);
static const unsigned long _glibtop_sysdeps_netload_in =
(1L << GLIBTOP_NETLOAD_PACKETS_TOTAL) +
(1L << GLIBTOP_NETLOAD_BYTES_TOTAL) +
(1L << GLIBTOP_NETLOAD_PACKETS_IN) +
(1L << GLIBTOP_NETLOAD_BYTES_IN);
static const unsigned long _glibtop_sysdeps_netload_out =
(1L << GLIBTOP_NETLOAD_PACKETS_TOTAL) +
(1L << GLIBTOP_NETLOAD_BYTES_TOTAL) +
(1L << GLIBTOP_NETLOAD_PACKETS_OUT) +
(1L << GLIBTOP_NETLOAD_BYTES_OUT);
/* Init function. */
int
glibtop_init_netload_s (glibtop *server)
{
server->sysdeps.netload = _glibtop_sysdeps_netload |
_glibtop_sysdeps_netload_bytes |
_glibtop_sysdeps_netload_packets;
return 0;
}
/* Provides network statistics. */
int
glibtop_get_netload_s (glibtop *server, glibtop_netload *buf,
const char *interface, unsigned transport,
unsigned protocol)
{
char buffer [BUFSIZ], *p;
int have_bytes, fields;
FILE *f;
memset (buf, 0, sizeof (glibtop_netload));
/* Linux 2.1.114 - don't know where exactly this was added, but
* recent kernels have byte count in /proc/net/dev so we don't
* need IP accounting.
*/
if (server->os_version_code < 131442) {
/* If IP accounting is enabled in the kernel and it is
* enabled for the requested interface, we use it to
* get the data. In this case, we don't use /proc/net/dev
* to get errors and collisions.
*/
f = fopen ("/proc/net/ip_acct", "r");
if (f) {
int success = 0;
/* Skip over the header line. */
fgets (buffer, BUFSIZ-1, f);
while (fgets (buffer, BUFSIZ-1, f)) {
unsigned long flags, packets, bytes;
char *p, *dev;
/* Skip over the network thing. */
dev = skip_token (buffer) + 1;
p = skip_token (dev);
*p++ = 0;
if (strcmp (dev, interface))
continue;
success = 1;
p = skip_token (p);
flags = strtoul (p, &p, 16);
p = skip_multiple_token (p, 2);
packets = strtoul (p, &p, 0);
bytes = strtoul (p, &p, 0);
if (flags & _GLIBTOP_IP_FW_ACCTIN) {
/* Incoming packets only. */
buf->packets_total += packets;
buf->packets_in += packets;
buf->bytes_total += bytes;
buf->bytes_in += bytes;
buf->flags |= _glibtop_sysdeps_netload_in;
} else if (flags & _GLIBTOP_IP_FW_ACCTOUT) {
/* Outgoing packets only. */
buf->packets_total += packets;
buf->packets_out += packets;
buf->bytes_total += bytes;
buf->bytes_out += bytes;
buf->flags |= _glibtop_sysdeps_netload_out;
} else {
/* Only have total values. */
buf->packets_total += packets;
buf->bytes_total += bytes;
buf->flags |= _glibtop_sysdeps_netload_total;
}
}
fclose (f);
if (success) return 0;
}
}
/* Ok, either IP accounting is not enabled in the kernel or
* it was not enabled for the requested interface. */
f = fopen ("/proc/net/dev", "r");
if (!f) return -1;
/* Skip over the header line. */
fgets (buffer, BUFSIZ-1, f);
fgets (buffer, BUFSIZ-1, f);
/* Starting with 2.1.xx (don't know exactly which version)
* /proc/net/dev contains both byte and package counters. */
p = strchr (buffer, '|');
if (!p) {
fclose (f);
return -1;
}
/* Do we already have byte counters ? */
have_bytes = strncmp (++p, "bytes", 5) == 0;
/* Count remaining 'Receive' fields so we know where
* the first 'Transmit' field starts. */
fields = 0;
while (*p != '|') {
if (!isspace (*p++)) continue;
while (isspace (*p++)) ;
fields++;
}
/* Should never happen. */
if (fields < 2) {
fclose (f);
return -1;
}
fields--;
while (fgets (buffer, BUFSIZ-1, f)) {
char *p, *dev;
dev = buffer;
while (isspace (*dev)) dev++;
p = strchr (dev, ':');
if (!p) continue;
*p++ = 0;
/* If it's not a digit, then it's most likely an error
* message like 'No statistics available'. */
while (isspace (*p)) p++;
if (!isdigit (*p)) continue;
if (strcmp (dev, interface))
continue;
/* Only read byte counts if we really have them. */
if (have_bytes) {
buf->bytes_in = strtoul (p, &p, 0);
fields--;
}
buf->packets_in = strtoul (p, &p, 0);
buf->errors_in = strtoul (p, &p, 0);
p = skip_multiple_token (p, fields);
if (have_bytes)
buf->bytes_out = strtoul (p, &p, 0);
buf->packets_out = strtoul (p, &p, 0);
buf->errors_out = strtoul (p, &p, 0);
p = skip_multiple_token (p, 2);
buf->collisions = strtoul (p, &p, 0);
/* Compute total valules. */
buf->bytes_total = buf->bytes_in + buf->bytes_out;
buf->packets_total = buf->packets_in + buf->packets_out;
/* And now the flags. */
buf->flags |= _glibtop_sysdeps_netload;
buf->flags |= _glibtop_sysdeps_netload_packets;
if (have_bytes)
buf->flags |= _glibtop_sysdeps_netload_bytes;
}
fclose (f);
return 0;
}

View File

@@ -1,103 +0,0 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* $Id$ */
/* 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 <glibtop.h>
#include <glibtop/cpu.h>
#include <glibtop/open.h>
/* =====================================================
* Linux kernel version information for procps utilities
* Copyright (c) 1996 Charles Blake <cblake@bbn.com>
*/
#include <sys/utsname.h>
#define LINUX_VERSION(x,y,z) (0x10000*(x) + 0x100*(y) + z)
static int linux_version_code = 0;
static void set_linux_version(void) {
static struct utsname uts;
int x = 0, y = 0, z = 0; /* cleared in case sscanf() < 3 */
if (linux_version_code) return;
if (uname(&uts) == -1) /* failure most likely implies impending death */
exit(1);
if (sscanf(uts.release, "%d.%d.%d", &x, &y, &z) < 3)
fprintf(stderr, /* *very* unlikely to happen by accident */
"Non-standard uts for running kernel:\n"
"release %s=%d.%d.%d gives version code %d\n",
uts.release, x, y, z, LINUX_VERSION(x,y,z));
linux_version_code = LINUX_VERSION(x, y, z);
}
/* ======================================================= */
/* Opens pipe to gtop server. Returns 0 on success and -1 on error. */
#define FILENAME "/proc/stat"
void
glibtop_open_s (glibtop_server *server, const char *program_name,
const unsigned long features,
const unsigned flags)
{
char buffer [BUFSIZ], *p;
int fd, len, i;
server->name = program_name;
set_linux_version ();
server->_priv->os_version_code = (unsigned long) linux_version_code;
server->ncpu = 0;
fd = open (FILENAME, O_RDONLY);
if (fd < 0)
glibtop_error_io_r (server, "open (%s)", FILENAME);
len = read (fd, buffer, BUFSIZ-1);
if (len < 0)
glibtop_error_io_r (server, "read (%s)", FILENAME);
close (fd);
buffer [len] = '\0';
p = skip_multiple_token (buffer, 5) + 1;
for (i = 0; i < GLIBTOP_NCPU; i++) {
if (strncmp (p, "cpu", 3) || !isdigit (p [3]))
break;
server->ncpu++;
p = skip_multiple_token (p, 5) + 1;
}
#if DEBUG
printf ("\nThis machine has %d CPUs.\n\n", server->ncpu);
#endif
}

View File

@@ -1,324 +0,0 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* $Id$ */
/* 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 <glibtop.h>
#include <glibtop/error.h>
#include <glibtop/xmalloc.h>
#include <glibtop/ppp.h>
#include <linux/isdn.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <ctype.h>
#include <glib.h>
#if !defined (_LIBC) && defined (__GNU_LIBRARY__) && __GNU_LIBRARY__ > 1
/* GNU LibC */
#include <net/if.h>
#include <netinet/ip_icmp.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include <net/if.h>
#include <net/if_ppp.h>
#else /* Libc 5 */
#include <linux/if.h>
#include <linux/in.h>
#include <linux/ip.h>
#include <linux/icmp.h>
#include <linux/tcp.h>
#include <linux/udp.h>
#include <linux/isdn.h>
#endif
static const unsigned long _glibtop_sysdeps_ppp =
(1L << GLIBTOP_PPP_STATE) + (1L << GLIBTOP_PPP_BYTES_IN) +
(1L << GLIBTOP_PPP_BYTES_OUT);
#ifdef SIOCDEVPRIVATE
static int ip_socket;
#endif
/* Init function. */
int
glibtop_init_ppp_s (glibtop *server)
{
server->sysdeps.ppp = _glibtop_sysdeps_ppp;
#ifdef SIOCDEVPRIVATE
/* open ip socket */
if ((ip_socket = socket (AF_INET, SOCK_DGRAM, 0)) < 0)
return -GLIBTOP_ERROR_NO_KERNEL_SUPPORT; /* should never happen */
#endif
return 0;
}
static int
get_ISDN_stats (glibtop *server, int *in, int *out)
{
unsigned long *isdn_stats, *ptr;
int fd, i;
*in = *out = 0;
isdn_stats = glibtop_calloc_r (server, ISDN_MAX_CHANNELS * 2,
sizeof (unsigned long));
fd = open ("/dev/isdninfo", O_RDONLY);
if (fd < 0) {
glibtop_free_r (server, isdn_stats);
return FALSE;
}
if ((ioctl (fd, IIOCGETCPS, isdn_stats) < 0) && (errno != 0)) {
glibtop_free_r (server, isdn_stats);
close (fd);
return FALSE;
}
for (i = 0, ptr = isdn_stats; i < ISDN_MAX_CHANNELS; i++) {
*in += *ptr++; *out += *ptr++;
}
glibtop_free_r (server, isdn_stats);
close (fd);
return TRUE;
}
static int
is_ISDN_on (glibtop *server, int device, int *online)
{
FILE *f = 0;
char buffer [BUFSIZ], *p;
int i;
/* Perhaps I should try to explain this code a little bit.
*
* ------------------------------------------------------------
* This is from the manpage of isdninfo(4):
*
* DESCRIPTION
* /dev/isdninfo is a character device with major number 45
* and minor number 255. It delivers status information from
* the Linux ISDN subsystem to user level.
*
* DATA FORMAT
* When reading from this device, the current status of the
* Linux ISDN subsystem is delivered in 6 lines of text. Each
* line starts with a tag string followed by a colon and
* whitespace. After that the status values are appended sep-
* arated by whitespace.
*
* flags is the tag of line 5. In this line for every driver
* slot, it's B-Channel status is shown. If no driver
* is registered in a slot, a ? is shown. For every
* established B-Channel of the driver, a bit is set
* in the shown value. The driver's first channel is
* mapped to bit 0, the second channel to bit 1 and so
* on.
* ------------------------------------------------------------
*
* So we open /dev/isdninfo, discard the first four lines of text
* and then check whether we have something that is not `0' or `?'
* in one of the flags fields.
*
* Sounds complicated, but I don't see any other way to check whether
* we are connected. Also, this is the method some other ISDN tools
* for Linux use.
*
* Martin
*/
f = fopen ("/dev/isdninfo", "r");
if (!f) return FALSE;
for (i = 0; i < 5; i++) {
if (fgets (buffer, BUFSIZ, f) == NULL) {
fclose (f);
return FALSE;
}
}
if (strncmp (buffer, "flags:", 6)) {
fclose (f);
return FALSE;
}
p = buffer+6;
for (i = 0; i <= device; i++) {
char *end = p;
while (isspace (*p))
p++;
for (end = p; *end && !isspace (*end); end++)
;
if (*end == 0)
break;
else
*end++ = 0;
if (i < device) {
p = end;
continue;
}
fclose (f);
if (strlen (p) != 1)
return FALSE;
if (*p == '0') {
*online = FALSE;
return TRUE;
} else if (*p == '1') {
*online = TRUE;
return TRUE;
}
return FALSE;
}
fclose (f);
return FALSE;
}
static int
is_Modem_on (glibtop *server, int device)
{
gchar buf[64], lock_file [BUFSIZ];
pid_t pid = -1;
FILE *f = 0;
sprintf (lock_file, LIBGTOP_MODEM_LOCKFILE, device);
f = fopen (lock_file, "r");
if(!f) return FALSE;
if (fgets (buf, sizeof(buf), f) == NULL) {
fclose (f);
return FALSE;
}
fclose (f);
pid = (pid_t) strtol (buf, NULL, 10);
if (pid < 1 || (kill (pid, 0) == -1 && errno != EPERM)) return FALSE;
return TRUE;
}
static int
get_Modem_stats (int device, int *in, int *out)
{
struct ifreq ifreq;
struct ppp_stats stats;
char device_name [IFNAMSIZ];
sprintf (device_name, "ppp%d", device);
memset (&ifreq, 0, sizeof(ifreq));
strncpy (ifreq.ifr_ifrn.ifrn_name, device_name, IFNAMSIZ);
ifreq.ifr_ifru.ifru_data = (caddr_t)&stats;
#ifdef SIOCDEVPRIVATE
/* open ip socket */
if ((ip_socket = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
{
g_print("could not open an ip socket\n");
return 1;
}
if ((ioctl (ip_socket, SIOCDEVPRIVATE, (caddr_t)&ifreq) < 0)) {
*in = *out = 0; /* failure means ppp is not up */
return FALSE;
} else {
*in = stats.p.ppp_ibytes;
*out = stats.p.ppp_obytes;
return TRUE;
}
#else /* not SIOCDEVPRIVATE */
*in = *out = 0;
return FALSE;
#endif /* not SIOCDEVPRIVATE */
}
/* Provides PPP/ISDN information. */
int
glibtop_get_ppp_s (glibtop *server, glibtop_ppp *buf, unsigned short device,
unsigned short use_isdn)
{
int in, out, online;
glibtop_init_s (&server, GLIBTOP_SYSDEPS_PPP, 0);
memset (buf, 0, sizeof (glibtop_ppp));
if (use_isdn) {
/* ISDN */
if (is_ISDN_on (server, device, &online)) {
buf->state = online ? GLIBTOP_PPP_STATE_ONLINE :
GLIBTOP_PPP_STATE_HANGUP;
buf->flags |= (1L << GLIBTOP_PPP_STATE);
}
if (get_ISDN_stats (server, &in, &out)) {
buf->bytes_in = in;
buf->bytes_out = out;
buf->flags |= (1L << GLIBTOP_PPP_BYTES_IN) |
(1L << GLIBTOP_PPP_BYTES_OUT);
}
} else {
/* Modem */
buf->state = is_Modem_on (server, device) ?
GLIBTOP_PPP_STATE_ONLINE : GLIBTOP_PPP_STATE_HANGUP;
buf->flags |= (1L << GLIBTOP_PPP_STATE);
if (get_Modem_stats (device, &in, &out)) {
buf->bytes_in = in;
buf->bytes_out = out;
buf->flags |= (1L << GLIBTOP_PPP_BYTES_IN) |
(1L << GLIBTOP_PPP_BYTES_OUT);
}
}
return 0;
}

View File

@@ -1,107 +0,0 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* $Id$ */
/* 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 <glibtop.h>
#include <glibtop/error.h>
#include <glibtop/xmalloc.h>
#include <glibtop/procargs.h>
static const unsigned long _glibtop_sysdeps_proc_args =
(1L << GLIBTOP_ARRAY_NUMBER) + (1L << GLIBTOP_ARRAY_SIZE);
/* Init function. */
int
glibtop_init_proc_args_s (glibtop *server)
{
server->sysdeps.proc_args = _glibtop_sysdeps_proc_args;
return 0;
}
/* Provides detailed information about a process. */
char **
glibtop_get_proc_args_s (glibtop *server, glibtop_array *array, pid_t pid)
{
char fn [BUFSIZ], buffer [BUFSIZ], *ptr = NULL, *pos, **ptrlist;
size_t count = 0, max_len, total = 0, len;
int cmdline, i;
glibtop_init_s (&server, GLIBTOP_SYSDEPS_PROC_ARGS, 0);
memset (array, 0, sizeof (glibtop_array));
max_len = BUFSIZ;
ptr = glibtop_malloc_r (server, max_len + 1);
sprintf (fn, "/proc/%d/cmdline", pid);
cmdline = open (fn, O_RDONLY);
if (cmdline < 0) return NULL;
while (1) {
len = read (cmdline, buffer, BUFSIZ-1);
if (len < 0) {
close (cmdline);
glibtop_free_r (server, ptr);
return NULL;
}
if (len == 0)
break;
ptr = glibtop_realloc_r (server, ptr, total+len+1);
memcpy (ptr+total, buffer, len);
*(ptr+total+len) = 0;
total += len;
}
close (cmdline);
ptr [total] = '\0';
for (i = 0; i <= total; i++) {
if (ptr [i]) continue;
count++;
}
ptrlist = glibtop_calloc_r (server, count+1, sizeof (char *));
for (i = 0, pos = ptr; i < count; i++) {
ptrlist [i] = glibtop_strdup_r (server, pos);
pos += strlen (pos) + 1;
}
ptrlist [count] = NULL;
glibtop_free_r (server, ptr);
array->number = count;
array->size = sizeof (char *);
array->flags = _glibtop_sysdeps_proc_args;
return ptrlist;
}

View File

@@ -1,91 +0,0 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* $Id$ */
/* 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 <glibtop.h>
#include <glibtop/error.h>
#include <glibtop/xmalloc.h>
#include <glibtop/proccwd.h>
static const unsigned long _glibtop_sysdeps_proc_cwd =
(1L << GLIBTOP_PROC_CWD_SIZE);
static const unsigned long _glibtop_sysdeps_proc_cwd_stat =
(1L << GLIBTOP_PROC_CWD_DEVICE) + (1L << GLIBTOP_PROC_CWD_INODE);
/* Init function. */
int
glibtop_init_proc_cwd_s (glibtop *server)
{
server->sysdeps.proc_cwd = _glibtop_sysdeps_proc_cwd |
_glibtop_sysdeps_proc_cwd_stat;
return 0;
}
/* Provides detailed information about a process. */
char *
glibtop_get_proc_cwd_s (glibtop *server, glibtop_proc_cwd *buf, pid_t pid)
{
char fn [BUFSIZ], buffer [BUFSIZ], *retval = NULL;
struct stat statb;
int len;
glibtop_init_s (&server, GLIBTOP_SYSDEPS_PROC_CWD, 0);
memset (buf, 0, sizeof (glibtop_proc_cwd));
sprintf (fn, "/proc/%d/cwd", pid);
if (stat (fn, &statb)) {
/* If we can't even stat () than we can't readlink (). */
if (errno != EACCES) {
/* Don't make too much unnecessary noise here. */
glibtop_warn_io_r (server, "stat (%s)", fn);
}
return retval;
}
buf->device = statb.st_dev;
buf->inode = statb.st_ino;
buf->flags = _glibtop_sysdeps_proc_cwd_stat;
len = readlink (fn, buffer, BUFSIZ-1);
if (len < 0) {
glibtop_warn_io_r (server, "readlink (%s)", fn);
return NULL;
}
buffer [len] = '\0';
retval = glibtop_strdup_r (server, buffer);
buf->size = len+1;
buf->flags |= _glibtop_sysdeps_proc_cwd;
return retval;
}

View File

@@ -1,83 +0,0 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* $Id$ */
/* 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 <glibtop.h>
#include <glibtop/error.h>
#include <glibtop/prockernel.h>
static const unsigned long _glibtop_sysdeps_proc_kernel =
(1L << GLIBTOP_PROC_KERNEL_K_FLAGS) + (1L << GLIBTOP_PROC_KERNEL_MIN_FLT) +
(1L << GLIBTOP_PROC_KERNEL_MAJ_FLT) + (1L << GLIBTOP_PROC_KERNEL_CMIN_FLT) +
(1L << GLIBTOP_PROC_KERNEL_CMAJ_FLT) + (1L << GLIBTOP_PROC_KERNEL_KSTK_ESP) +
(1L << GLIBTOP_PROC_KERNEL_KSTK_EIP) + (1L << GLIBTOP_PROC_KERNEL_WCHAN);
/* Init function. */
int
glibtop_init_proc_kernel_s (glibtop *server)
{
server->sysdeps.proc_kernel = _glibtop_sysdeps_proc_kernel;
return 0;
}
/* Provides detailed information about a process. */
int
glibtop_get_proc_kernel_s (glibtop *server, glibtop_proc_kernel *buf, pid_t pid)
{
char buffer [BUFSIZ], *p;
glibtop_init_s (&server, GLIBTOP_SYSDEPS_PROC_KERNEL, 0);
memset (buf, 0, sizeof (glibtop_proc_kernel));
if (proc_stat_to_buffer (buffer, pid))
return -1;
p = proc_stat_after_cmd (buffer);
if (!p) return -1;
p = skip_multiple_token (p, 6);
buf->k_flags = strtoul (p, &p, 0);
buf->min_flt = strtoul (p, &p, 0);
buf->cmin_flt = strtoul (p, &p, 0);
buf->maj_flt = strtoul (p, &p, 0);
buf->cmaj_flt = strtoul (p, &p, 0);
p = skip_multiple_token (p, 15);
buf->kstk_esp = strtoul (p, &p, 0);
buf->kstk_eip = strtoul (p, &p, 0);
p = skip_multiple_token (p, 4);
buf->nwchan = strtoul (p, &p, 0);
buf->flags = _glibtop_sysdeps_proc_kernel;
return 0;
}

View File

@@ -1,244 +0,0 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* $Id$ */
/* 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/xmalloc.h>
#include <glibtop/proclist.h>
#include <glibtop/procuid.h>
#include <glibtop/procstate.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <ctype.h>
static const unsigned long _glibtop_sysdeps_proclist =
(1L << GLIBTOP_PROCLIST_TOTAL) + (1L << GLIBTOP_PROCLIST_NUMBER) +
(1L << GLIBTOP_PROCLIST_SIZE);
/* Init function. */
int
glibtop_init_proclist_s (glibtop *server)
{
server->sysdeps.proclist = _glibtop_sysdeps_proclist;
return 0;
}
#define BLOCK_COUNT 256
#define BLOCK_SIZE (BLOCK_COUNT * sizeof (unsigned))
/* 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.
*
* On error, NULL is returned and buf->flags is zero. */
unsigned *
glibtop_get_proclist_s (glibtop *server, glibtop_proclist *buf,
int64_t which, int64_t arg)
{
DIR *proc;
struct dirent *entry;
char buffer [BUFSIZ];
unsigned count, total, pid;
unsigned pids [BLOCK_COUNT], *pids_chain = NULL;
unsigned pids_size = 0, pids_offset = 0, new_size;
struct stat statb;
int len, i, ok;
glibtop_proc_uid procuid;
glibtop_proc_state procstate;
glibtop_init_s (&server, GLIBTOP_SYSDEPS_PROCLIST, 0);
memset (buf, 0, sizeof (glibtop_proclist));
proc = opendir ("/proc");
if (!proc) return NULL;
/* read every every entry in /proc */
for (count = total = 0, entry = readdir (proc);
entry; entry = readdir (proc)) {
ok = 1; len = strlen (entry->d_name);
/* does it consist entirely of digits? */
for (i = 0; i < len; i++)
if (!isdigit (entry->d_name [i])) ok = 0;
if (!ok) continue;
/* convert it in a number */
if (sscanf (entry->d_name, "%u", &pid) != 1) continue;
/* is it really a directory? */
sprintf (buffer, "/proc/%d", pid);
if (stat (buffer, &statb)) continue;
if (!S_ISDIR (statb.st_mode)) continue;
switch (which & GLIBTOP_KERN_PROC_MASK) {
case GLIBTOP_KERN_PROC_ALL:
break;
case GLIBTOP_KERN_PROC_PID:
if ((unsigned) arg != pid)
continue;
break;
case GLIBTOP_KERN_PROC_UID:
if ((uid_t) arg != statb.st_uid)
continue;
break;
case GLIBTOP_KERN_PROC_PGRP:
/* Do you really, really need this ? */
glibtop_get_proc_uid_s (server, &procuid, pid);
if (procuid.flags & (1L << GLIBTOP_PROC_UID_PGRP))
if ((int) arg != procuid.pgrp)
continue;
break;
case GLIBTOP_KERN_PROC_PPID:
/* Do you really, really need this ? */
glibtop_get_proc_uid_s (server, &procuid, pid);
if (procuid.flags & (1L << GLIBTOP_PROC_UID_PPID))
if ((int) arg != procuid.ppid)
continue;
break;
case GLIBTOP_KERN_PROC_SESSION:
/* Do you really, really need this ? */
glibtop_get_proc_uid_s (server, &procuid, pid);
if (procuid.flags & (1L << GLIBTOP_PROC_UID_SESSION))
if ((int) arg != procuid.session)
continue;
break;
case GLIBTOP_KERN_PROC_TTY:
/* Do you really, really need this ? */
glibtop_get_proc_uid_s (server, &procuid, pid);
if (procuid.flags & (1L << GLIBTOP_PROC_UID_TTY))
if ((int) arg != procuid.tty)
continue;
break;
case GLIBTOP_KERN_PROC_RUID:
/* Do you really, really need this ? */
glibtop_get_proc_uid_s (server, &procuid, pid);
if (procuid.flags & (1L << GLIBTOP_PROC_UID_EUID))
if ((int) arg != procuid.euid)
continue;
break;
}
if (which & GLIBTOP_EXCLUDE_NOTTY) {
glibtop_get_proc_uid_s (server, &procuid, pid);
if (procuid.flags & (1L << GLIBTOP_PROC_UID_TTY))
if (procuid.tty == -1) continue;
}
if (which & GLIBTOP_EXCLUDE_IDLE) {
glibtop_get_proc_state_s (server, &procstate, pid);
if (procstate.flags & (1L << GLIBTOP_PROC_STATE_STATE))
if (!(procstate.state & GLIBTOP_PROCESS_RUNNING)) continue;
}
if (which & GLIBTOP_EXCLUDE_SYSTEM) {
glibtop_get_proc_uid_s (server, &procuid, pid);
if (procuid.flags & (1L << GLIBTOP_PROC_UID_UID))
if (procuid.uid == 0) continue;
}
/* Fine. Now we first try to store it in pids. If this buffer is
* full, we copy it to the pids_chain. */
if (count >= BLOCK_COUNT) {
/* The following call to glibtop_realloc will be
* equivalent to glibtop_malloc () if `pids_chain' is
* NULL. We just calculate the new size and copy `pids'
* to the beginning of the newly allocated block. */
new_size = pids_size + BLOCK_SIZE;
pids_chain = glibtop_realloc_r
(server, pids_chain, new_size);
memcpy (pids_chain + pids_offset, pids, BLOCK_SIZE);
pids_size = new_size;
pids_offset += BLOCK_COUNT;
count = 0;
}
/* pids is now big enough to hold at least one single pid. */
pids [count++] = pid;
total++;
}
closedir (proc);
/* count is only zero if an error occured (one a running Linux system,
* we have at least one single process). */
if (!count) return NULL;
/* The following call to glibtop_realloc will be equivalent to
* glibtop_malloc if pids_chain is NULL. We just calculate the
* new size and copy pids to the beginning of the newly allocated
* block. */
new_size = pids_size + count * sizeof (unsigned);
pids_chain = glibtop_realloc_r (server, pids_chain, new_size);
memcpy (pids_chain + pids_offset, pids, count * sizeof (unsigned));
pids_size = new_size;
pids_offset += BLOCK_COUNT;
/* Since everything is ok now, we can set buf->flags, fill in the
* remaining fields and return the `pids_chain'. */
buf->flags = _glibtop_sysdeps_proclist;
buf->size = sizeof (unsigned);
buf->number = total;
buf->total = total * sizeof (unsigned);
return pids_chain;
}

View File

@@ -1,142 +0,0 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* $Id$ */
/* 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 <glibtop.h>
#include <glibtop/error.h>
#include <glibtop/xmalloc.h>
#include <glibtop/procmap.h>
static const unsigned long _glibtop_sysdeps_proc_map =
(1L << GLIBTOP_PROC_MAP_NUMBER) + (1L << GLIBTOP_PROC_MAP_TOTAL) +
(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) +
(1L << GLIBTOP_MAP_ENTRY_FILENAME);
/* Init function. */
int
glibtop_init_proc_map_s (glibtop *server)
{
server->sysdeps.proc_map = _glibtop_sysdeps_proc_map;
return 0;
}
/* Provides detailed information about a process. */
glibtop_map_entry *
glibtop_get_proc_map_s (glibtop *server, glibtop_proc_map *buf, pid_t pid)
{
char fn [BUFSIZ];
glibtop_map_entry *entry_list = NULL;
int rv, n = 0;
FILE *maps;
glibtop_init_s (&server, GLIBTOP_SYSDEPS_PROC_MAP, 0);
memset (buf, 0, sizeof (glibtop_proc_map));
sprintf (fn, "/proc/%d/maps", pid);
maps = fopen (fn, "r");
if (!maps) return NULL;
do {
short dev_major, dev_minor;
unsigned long start, end, offset, inode, perm;
char flags [5], *format;
size_t size;
if (sizeof (void*) == 8)
format = "%16lx-%16lx %4c\n %16lx %02hx:%02hx %ld";
else
format = "%08lx-%08lx %4c\n %08lx %02hx:%02hx %ld";
rv = fscanf (maps, format,
&start, &end, flags, &offset,
&dev_major, &dev_minor, &inode);
flags [4] = 0;
/* Compute access permissions. */
perm = 0;
if (flags [0] == 'r')
perm |= GLIBTOP_MAP_PERM_READ;
if (flags [1] == 'w')
perm |= GLIBTOP_MAP_PERM_WRITE;
if (flags [2] == 'x')
perm |= GLIBTOP_MAP_PERM_EXECUTE;
if (flags [3] == 's')
perm |= GLIBTOP_MAP_PERM_SHARED;
if (flags [3] == 'p')
perm |= GLIBTOP_MAP_PERM_PRIVATE;
/* Read filename. */
fn [0] = fgetc (maps);
if (fn [0] != '\n' && fn [0] != EOF) {
fscanf (maps, "%*[ ]%[^\n]\n", fn);
} else fn [0] = 0;
size = (n+1) * sizeof (glibtop_map_entry);
entry_list = glibtop_realloc_r (server, entry_list, size);
memset (&(entry_list [n]), 0, sizeof (glibtop_map_entry));
entry_list [n].flags = _glibtop_sysdeps_map_entry;
entry_list [n].start = (u_int64_t) start;
entry_list [n].end = (u_int64_t) end;
entry_list [n].offset = (u_int64_t) offset;
entry_list [n].perm = (u_int64_t) perm;
entry_list [n].device = (u_int64_t) (dev_major << 8) +
(u_int64_t) dev_minor;
entry_list [n].inode = (u_int64_t) inode;
strncpy (entry_list [n].filename, fn, GLIBTOP_MAP_FILENAME_LEN);
entry_list [n].filename [GLIBTOP_MAP_FILENAME_LEN] = 0;
n++;
} while (rv != EOF && rv && fn [0] != EOF);
fclose (maps);
buf->number = n;
buf->size = sizeof (glibtop_map_entry);
buf->total = n * sizeof (glibtop_map_entry);
return entry_list;
}

View File

@@ -1,110 +0,0 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* $Id$ */
/* 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 <glibtop.h>
#include <glibtop/error.h>
#include <glibtop/procmem.h>
static const unsigned long _glibtop_sysdeps_proc_mem =
(1L << GLIBTOP_PROC_MEM_VSIZE) + (1L << GLIBTOP_PROC_MEM_RSS) +
(1L << GLIBTOP_PROC_MEM_RSS_RLIM);
static const unsigned long _glibtop_sysdeps_proc_mem_statm =
(1L << GLIBTOP_PROC_MEM_SIZE) + (1L << GLIBTOP_PROC_MEM_RESIDENT) +
(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. */
int
glibtop_init_proc_mem_s (glibtop *server)
{
register int pagesize;
server->sysdeps.proc_mem = _glibtop_sysdeps_proc_mem |
_glibtop_sysdeps_proc_mem_statm;
/* get the page size with "getpagesize" and calculate pageshift
* from it */
pagesize = getpagesize ();
pageshift = 0;
while (pagesize > 1) {
pageshift++;
pagesize >>= 1;
}
return 0;
}
/* Provides detailed information about a process. */
int
glibtop_get_proc_mem_s (glibtop *server, glibtop_proc_mem *buf, pid_t pid)
{
char buffer [BUFSIZ], *p;
glibtop_init_s (&server, GLIBTOP_SYSDEPS_MEM, 0);
memset (buf, 0, sizeof (glibtop_proc_mem));
if (proc_stat_to_buffer (buffer, pid))
return -1;
p = proc_stat_after_cmd (buffer);
if (!p) return -1;
p = skip_multiple_token (p, 20);
buf->vsize = strtoul (p, &p, 0);
buf->rss = strtoul (p, &p, 0);
buf->rss_rlim = strtoul (p, &p, 0);
buf->flags = _glibtop_sysdeps_proc_mem;
if (proc_statm_to_buffer (buffer, pid))
return -1;
buf->size = strtoul (buffer, &p, 0);
buf->resident = strtoul (p, &p, 0);
buf->share = strtoul (p, &p, 0);
buf->size <<= pageshift;
buf->resident <<= pageshift;
buf->share <<= pageshift;
buf->rss <<= pageshift;
buf->flags |= _glibtop_sysdeps_proc_mem_statm;
return 0;
}

View File

@@ -1,121 +0,0 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* $Id$ */
/* 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/error.h>
#include <glibtop/procsegment.h>
static const unsigned long _glibtop_sysdeps_proc_segment =
(1L << GLIBTOP_PROC_SEGMENT_START_CODE) +
(1L << GLIBTOP_PROC_SEGMENT_END_CODE) +
(1L << GLIBTOP_PROC_SEGMENT_START_STACK);
static const unsigned long _glibtop_sysdeps_proc_segment_statm =
(1L << GLIBTOP_PROC_SEGMENT_TEXT_RSS) +
/* Disabled due to bug in the Linux Kernel. */
/* (1L << GLIBTOP_PROC_SEGMENT_SHLIB_RSS) + */
(1L << GLIBTOP_PROC_SEGMENT_DATA_RSS) +
(1L << GLIBTOP_PROC_SEGMENT_DIRTY_SIZE);
#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. */
int
glibtop_init_proc_segment_s (glibtop *server)
{
register int pagesize;
server->sysdeps.proc_segment = _glibtop_sysdeps_proc_segment |
_glibtop_sysdeps_proc_segment_statm;
/* get the page size with "getpagesize" and calculate pageshift
* from it */
pagesize = getpagesize ();
pageshift = 0;
while (pagesize > 1) {
pageshift++;
pagesize >>= 1;
}
return 0;
}
/* Provides detailed information about a process. */
int
glibtop_get_proc_segment_s (glibtop *server, glibtop_proc_segment *buf,
pid_t pid)
{
char buffer [BUFSIZ], *p;
glibtop_init_s (&server, GLIBTOP_SYSDEPS_PROC_SEGMENT, 0);
memset (buf, 0, sizeof (glibtop_proc_segment));
if (proc_stat_to_buffer (buffer, pid))
return -1;
p = proc_stat_after_cmd (buffer);
if (!p) return -1;
p = skip_multiple_token (p, 23);
buf->start_code = strtoul (p, &p, 0);
buf->end_code = strtoul (p, &p, 0);
buf->start_stack = strtoul (p, &p, 0);
buf->flags = _glibtop_sysdeps_proc_segment;
if (proc_statm_to_buffer (buffer, pid))
return 0;
p = skip_multiple_token (buffer, 3);
/* This doesn't work very well due to a bug in the Linux kernel.
* I'll submit a patch to the kernel mailing list soon. */
buf->text_rss = strtoul (p, &p, 0);
buf->shlib_rss = strtoul (p, &p, 0);
buf->data_rss = strtoul (p, &p, 0);
buf->dirty_size = strtoul (p, &p, 0);
buf->text_rss <<= pageshift;
buf->shlib_rss <<= pageshift;
buf->data_rss <<= pageshift;
buf->dirty_size <<= pageshift;
buf->flags |= _glibtop_sysdeps_proc_segment_statm;
return 0;
}

View File

@@ -1,71 +0,0 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* $Id$ */
/* 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 <glibtop.h>
#include <glibtop/error.h>
#include <glibtop/procsignal.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. */
int
glibtop_init_proc_signal_s (glibtop *server)
{
server->sysdeps.proc_signal = _glibtop_sysdeps_proc_signal;
return 0;
}
/* Provides detailed information about a process. */
int
glibtop_get_proc_signal_s (glibtop *server, glibtop_proc_signal *buf, pid_t pid)
{
char buffer [BUFSIZ], *p;
glibtop_init_s (&server, GLIBTOP_SYSDEPS_PROC_SIGNAL, 0);
memset (buf, 0, sizeof (glibtop_proc_signal));
if (proc_stat_to_buffer (buffer, pid))
return -1;
p = proc_stat_after_cmd (buffer);
if (!p) return -1;
p = skip_multiple_token (p, 28);
buf->signal [0] = strtoul (p, &p, 0);
buf->blocked [0] = strtoul (p, &p, 0);
buf->sigignore [0] = strtoul (p, &p, 0);
buf->sigcatch [0] = strtoul (p, &p, 0);
buf->flags = _glibtop_sysdeps_proc_signal;
return 0;
}

View File

@@ -1,124 +0,0 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* $Id$ */
/* 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 <glibtop.h>
#include <glibtop/error.h>
#include <glibtop/procstate.h>
#include <sys/stat.h>
static const unsigned long _glibtop_sysdeps_proc_state =
(1L << GLIBTOP_PROC_STATE_CMD);
static const unsigned long _glibtop_sysdeps_proc_state_uid =
(1L << GLIBTOP_PROC_STATE_UID) + (1L << GLIBTOP_PROC_STATE_GID);
/* Init function. */
int
glibtop_init_proc_state_s (glibtop *server)
{
server->sysdeps.proc_state = _glibtop_sysdeps_proc_state |
_glibtop_sysdeps_proc_state_uid;
return 0;
}
/* Provides detailed information about a process. */
int
glibtop_get_proc_state_s (glibtop *server, glibtop_proc_state *buf, pid_t pid)
{
char buffer [BUFSIZ], state, *p;
struct stat statb;
glibtop_init_s (&server, GLIBTOP_SYSDEPS_PROC_STATE, 0);
memset (buf, 0, sizeof (glibtop_proc_state));
/* IMPORTANT NOTICE: For security reasons it is extremely important
* that the 'uid' and 'gid' fields have correct
* values; NEVER set their flags values if this
* is not the case !!! */
sprintf (buffer, "/proc/%d", pid);
if (stat (buffer, &statb))
return -1;
/* For security reasons we use stat () since it is
* more failsafe than parsing the file. */
buf->uid = statb.st_uid;
buf->gid = statb.st_gid;
buf->flags = _glibtop_sysdeps_proc_state_uid;
sprintf (buffer, "/proc/%d", pid);
/* Now we read the remaining fields. */
if (proc_stat_to_buffer (buffer, pid))
return -1;
p = strrchr (buffer, ')'); *p = '\0';
state = p [2];
p = skip_token (buffer); p++; /* pid */
if (*p++ != '(')
glibtop_error_r (server, "Bad data in /proc/%d/stat", pid);
strncpy (buf->cmd, p, sizeof (buf->cmd)-1);
buf->cmd [sizeof (buf->cmd)-1] = 0;
buf->flags |= _glibtop_sysdeps_proc_state;
switch (state) {
case 'R':
buf->state = GLIBTOP_PROCESS_RUNNING;
break;
case 'S':
buf->state = GLIBTOP_PROCESS_INTERRUPTIBLE;
break;
case 'D':
buf->state = GLIBTOP_PROCESS_UNINTERRUPTIBLE;
break;
case 'Z':
buf->state = GLIBTOP_PROCESS_ZOMBIE;
break;
case 'T':
buf->state = GLIBTOP_PROCESS_STOPPED;
break;
case 'W':
buf->state = GLIBTOP_PROCESS_SWAPPING;
break;
default:
return -1;
}
buf->flags |= (1L << GLIBTOP_PROC_STATE_STATE);
return 0;
}

View File

@@ -1,109 +0,0 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* $Id$ */
/* 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 <glibtop.h>
#include <glibtop/error.h>
#include <glibtop/proctime.h>
static const unsigned long _glibtop_sysdeps_proc_time =
(1L << GLIBTOP_PROC_TIME_UTIME) + (1L << GLIBTOP_PROC_TIME_CUTIME) +
(1L << GLIBTOP_PROC_TIME_STIME) + (1L << GLIBTOP_PROC_TIME_CSTIME) +
(1L << GLIBTOP_PROC_TIME_FREQUENCY) + (1L << GLIBTOP_PROC_TIME_TIMEOUT) +
(1L << GLIBTOP_PROC_TIME_IT_REAL_VALUE) + (1L << GLIBTOP_PROC_TIME_START_TIME);
static const unsigned long _glibtop_sysdeps_proc_time_smp =
(1L << GLIBTOP_PROC_TIME_XCPU_UTIME) + (1L << GLIBTOP_PROC_TIME_XCPU_STIME);
/* Init function. */
int
glibtop_init_proc_time_s (glibtop *server)
{
server->sysdeps.proc_time = _glibtop_sysdeps_proc_time;
if (server->ncpu)
server->sysdeps.proc_time |= _glibtop_sysdeps_proc_time_smp;
return 0;
}
/* Provides detailed information about a process. */
int
glibtop_get_proc_time_s (glibtop *server, glibtop_proc_time *buf, pid_t pid)
{
char buffer [BUFSIZ], *p;
int i;
glibtop_init_s (&server, GLIBTOP_SYSDEPS_PROC_TIME, 0);
memset (buf, 0, sizeof (glibtop_proc_time));
if (proc_stat_to_buffer (buffer, pid))
return -1;
p = proc_stat_after_cmd (buffer);
if (!p) return -1;
p = skip_multiple_token (p, 11);
buf->utime = strtoul (p, &p, 0);
buf->stime = strtoul (p, &p, 0);
buf->cutime = strtoul (p, &p, 0);
buf->cstime = strtoul (p, &p, 0);
p = skip_multiple_token (p, 2);
buf->timeout = strtoul (p, &p, 0);
buf->it_real_value = strtoul (p, &p, 0);
buf->start_time = strtoul (p, &p, 0);
buf->frequency = 100;
buf->flags = _glibtop_sysdeps_proc_time;
if (!server->ncpu)
return 0;
if (proc_file_to_buffer (buffer, "/proc/%d/cpu", pid))
return -1;
p = skip_token (buffer);
buf->utime = strtoul (p, &p, 0);
buf->stime = strtoul (p, &p, 0);
for (i = 0; i < GLIBTOP_NCPU; i++) {
if (strncmp (p+1, "cpu", 3) || !isdigit (p [4]))
break;
p += 6;
buf->xcpu_utime [i] = strtoul (p, &p, 0);
buf->xcpu_stime [i] = strtoul (p, &p, 0);
}
buf->flags |= _glibtop_sysdeps_proc_time_smp;
return 0;
}

View File

@@ -1,132 +0,0 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* $Id$ */
/* 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 <glibtop.h>
#include <glibtop/error.h>
#include <glibtop/procuid.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);
static const unsigned long _glibtop_sysdeps_proc_uid_stat =
(1L << GLIBTOP_PROC_UID_PID) + (1L << GLIBTOP_PROC_UID_PPID) +
(1L << GLIBTOP_PROC_UID_PGRP) + (1L << GLIBTOP_PROC_UID_SESSION) +
(1L << GLIBTOP_PROC_UID_TTY) + (1L << GLIBTOP_PROC_UID_TPGID) +
(1L << GLIBTOP_PROC_UID_PRIORITY) + (1L << GLIBTOP_PROC_UID_NICE);
#define LINUX_VERSION(x,y,z) (0x10000*(x) + 0x100*(y) + z)
/* Init function. */
int
glibtop_init_proc_uid_s (glibtop *server)
{
server->sysdeps.proc_uid = _glibtop_sysdeps_proc_uid |
_glibtop_sysdeps_proc_uid_stat;
return 0;
}
/* Provides detailed information about a process. */
int
glibtop_get_proc_uid_s (glibtop *server, glibtop_proc_uid *buf, pid_t pid)
{
char buffer [BUFSIZ], *p;
glibtop_init_s (&server, GLIBTOP_SYSDEPS_PROC_UID, 0);
memset (buf, 0, sizeof (glibtop_proc_uid));
if (proc_status_to_buffer (buffer, pid))
return -1;
/* Search substring 'Pid:' */
p = strstr (buffer, "\nPid:");
if (!p) return -1;
p = skip_token (p); /* "Pid:" */
buf->pid = strtoul (p, &p, 0);
p = skip_token (p); /* "PPid:" */
buf->ppid = strtoul (p, &p, 0);
/* Maybe future Linux versions place something between
* "PPid" and "Uid", so we catch this here. */
p = strstr (p, "\nUid:");
if (!p) return -1;
p = skip_token (p); /* "Uid:" */
buf->uid = strtoul (p, &p, 0);
buf->euid = strtoul (p, &p, 0);
/* We don't know how many entries on the "Uid:" line
* future Linux version will have, so we catch this here. */
p = strstr (p, "\nGid:");
if (!p) return -1;
p = skip_token (p); /* "Gid:" */
buf->gid = strtoul (p, &p, 0);
buf->egid = strtoul (p, &p, 0);
buf->flags = _glibtop_sysdeps_proc_uid;
if (proc_stat_to_buffer (buffer, pid))
return -1;
p = proc_stat_after_cmd (buffer);
if (!p) return -1;
p = skip_multiple_token (p, 2);
buf->pgrp = strtoul (p, &p, 0);
buf->session = strtoul (p, &p, 0);
buf->tty = strtoul (p, &p, 0);
buf->tpgid = strtoul (p, &p, 0);
p = skip_multiple_token (p, 9);
buf->priority = strtoul (p, &p, 0);
buf->nice = strtoul (p, &p, 0);
if (buf->tty == 0)
/* the old notty val, update elsewhere bef. moving to 0 */
buf->tty = -1;
if (server->_param.os_version_code < LINUX_VERSION(1,3,39)) {
/* map old meanings to new */
buf->priority = 2*15 - buf->priority;
buf->nice = 15 - buf->nice;
}
if (server->os_version_code < LINUX_VERSION(1,1,30) && buf->tty != -1)
/* when tty wasn't full devno */
buf->tty = 4*0x100 + buf->tty;
buf->flags |= _glibtop_sysdeps_proc_uid_stat;
return 0;
}

View File

@@ -1,92 +0,0 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* $Id$ */
/* 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 <glibtop/sem_limits.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#ifdef _SEM_SEMUN_UNDEFINED
/* glibc 2.1 will no longer defines semun, instead it defines
* _SEM_SEMUN_UNDEFINED so users can define semun on their own.
* Thanks to Albert K T Hui <avatar@deva.net>. */
union semun
{
int val;
struct semid_ds *buf;
unsigned short int *array;
struct seminfo *__buf;
};
#endif
static unsigned long _glibtop_sysdeps_sem_limits =
(1L << GLIBTOP_SEM_LIMITS_SEMMAP) + (1L << GLIBTOP_SEM_LIMITS_SEMMNI) +
(1L << GLIBTOP_SEM_LIMITS_SEMMNS) + (1L << GLIBTOP_SEM_LIMITS_SEMMNU) +
(1L << GLIBTOP_SEM_LIMITS_SEMMSL) + (1L << GLIBTOP_SEM_LIMITS_SEMOPM) +
(1L << GLIBTOP_SEM_LIMITS_SEMUME) + (1L << GLIBTOP_SEM_LIMITS_SEMUSZ) +
(1L << GLIBTOP_SEM_LIMITS_SEMVMX) + (1L << GLIBTOP_SEM_LIMITS_SEMAEM);
/* Init function. */
int
glibtop_init_sem_limits_s (glibtop *server)
{
server->sysdeps.sem_limits = _glibtop_sysdeps_sem_limits;
return 0;
}
/* Provides information about sysv ipc limits. */
int
glibtop_get_sem_limits_s (glibtop *server, glibtop_sem_limits *buf)
{
struct seminfo seminfo;
union semun arg;
glibtop_init_s (&server, GLIBTOP_SYSDEPS_SEM_LIMITS, 0);
memset (buf, 0, sizeof (glibtop_sem_limits));
buf->flags = _glibtop_sysdeps_sem_limits;
arg.array = (ushort *) &seminfo;
semctl (0, 0, IPC_INFO, arg);
buf->semmap = seminfo.semmap;
buf->semmni = seminfo.semmni;
buf->semmns = seminfo.semmns;
buf->semmnu = seminfo.semmnu;
buf->semmsl = seminfo.semmsl;
buf->semopm = seminfo.semopm;
buf->semume = seminfo.semume;
buf->semusz = seminfo.semusz;
buf->semvmx = seminfo.semvmx;
buf->semaem = seminfo.semaem;
return 0;
}

View File

@@ -1,68 +0,0 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* $Id$ */
/* 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 <glibtop/shm_limits.h>
#include <sys/ipc.h>
#include <sys/shm.h>
static unsigned long _glibtop_sysdeps_shm_limits =
(1L << GLIBTOP_SHM_LIMITS_SHMMAX) + (1L << GLIBTOP_SHM_LIMITS_SHMMIN) +
(1L << GLIBTOP_SHM_LIMITS_SHMMNI) + (1L << GLIBTOP_SHM_LIMITS_SHMSEG) +
(1L << GLIBTOP_SHM_LIMITS_SHMALL);
/* Init function. */
int
glibtop_init_shm_limits_s (glibtop *server)
{
server->sysdeps.shm_limits = _glibtop_sysdeps_shm_limits;
return 0;
}
/* Provides information about sysv ipc limits. */
int
glibtop_get_shm_limits_s (glibtop *server, glibtop_shm_limits *buf)
{
struct shminfo shminfo;
glibtop_init_s (&server, GLIBTOP_SYSDEPS_SHM_LIMITS, 0);
memset (buf, 0, sizeof (glibtop_shm_limits));
buf->flags = _glibtop_sysdeps_shm_limits;
shmctl (0, IPC_INFO, (struct shmid_ds *) &shminfo);
buf->shmmax = shminfo.shmmax;
buf->shmmin = shminfo.shmmin;
buf->shmmni = shminfo.shmmni;
buf->shmseg = shminfo.shmseg;
buf->shmall = shminfo.shmall;
return 0;
}

View File

@@ -1,120 +0,0 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* $Id$ */
/* 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/error.h>
#include <glibtop/swap.h>
#include <fcntl.h>
static unsigned long _glibtop_sysdeps_swap =
(1L << GLIBTOP_SWAP_TOTAL) + (1L << GLIBTOP_SWAP_USED) +
(1L << GLIBTOP_SWAP_FREE);
static unsigned long _glibtop_sysdeps_swap_paging =
(1L << GLIBTOP_SWAP_PAGEIN) + (1L << GLIBTOP_SWAP_PAGEOUT);
/* Init function. */
int
glibtop_init_swap_s (glibtop *server)
{
server->sysdeps.swap = _glibtop_sysdeps_swap |
_glibtop_sysdeps_swap_paging;
return 0;
}
/* Provides information about swap usage. */
#define MEMINFO "/proc/meminfo"
#define PROC_STAT "/proc/stat"
int
glibtop_get_swap_s (glibtop *server, glibtop_swap *buf)
{
char buffer [BUFSIZ], *p;
int fd, len;
glibtop_init_s (&server, GLIBTOP_SYSDEPS_SWAP, 0);
memset (buf, 0, sizeof (glibtop_swap));
fd = open (MEMINFO, O_RDONLY);
if (fd < 0) {
glibtop_warn_io_r (server, "open (%s)", MEMINFO);
return -1;
}
len = read (fd, buffer, BUFSIZ-1);
if (len < 0) {
close (fd);
glibtop_warn_io_r (server, "read (%s)", MEMINFO);
return -1;
}
close (fd);
buffer [len] = '\0';
p = skip_line (buffer);
p = skip_line (p);
p = skip_token (p); /* "Swap:" */
buf->total = strtoul (p, &p, 0);
buf->used = strtoul (p, &p, 0);
buf->free = strtoul (p, &p, 0);
buf->flags = _glibtop_sysdeps_swap;
fd = open (PROC_STAT, O_RDONLY);
if (fd < 0) {
glibtop_warn_io_r (server, "open (%s)", PROC_STAT);
return -1;
}
len = read (fd, buffer, BUFSIZ-1);
if (len < 0) {
close (fd);
glibtop_warn_io_r (server, "read (%s)", PROC_STAT);
return -1;
}
close (fd);
buffer [len] = '\0';
p = strstr (buffer, "\nswap");
if (p == NULL) return 0;
p = skip_token (p);
buf->pagein = strtoul (p, &p, 0);
buf->pageout = strtoul (p, &p, 0);
buf->flags |= _glibtop_sysdeps_swap_paging;
return 0;
}

View File

@@ -1,96 +0,0 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* $Id$ */
/* 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/cpu.h>
#include <glibtop/sysinfo.h>
static const unsigned long _glibtop_sysdeps_sysinfo =
(1L << GLIBTOP_SYSINFO_CPUINFO);
static glibtop_sysinfo sysinfo;
static void
init_sysinfo (glibtop *server)
{
char buffer [BUFSIZ];
static int init = 0;
glibtop_entry *cpuinfo = NULL;
FILE *f;
if (init) return;
init = TRUE;
glibtop_init_s (&server, GLIBTOP_SYSDEPS_CPU, 0);
memset (&sysinfo, 0, sizeof (glibtop_sysinfo));
g_return_if_fail (f = fopen ("/proc/cpuinfo", "r"));
while (fgets (buffer, BUFSIZ, f)) {
char *p, *start, *key, *value;
if (cpuinfo == NULL) {
cpuinfo = &sysinfo.cpuinfo [sysinfo.ncpu++];
cpuinfo->labels = g_ptr_array_new ();
cpuinfo->values = g_hash_table_new (NULL, NULL);
if (sysinfo.ncpu > GLIBTOP_NCPU)
sysinfo.ncpu = GLIBTOP_NCPU;
}
p = strchr (buffer, ':');
if (!p) continue;
/* Remove leading spaces from `p'. */
*p = '\0'; start = p; p++;
while (isspace (*p)) p++;
/* Remove trailing spaces from `buffer'. */
while ((start > buffer) && (*start) && isspace (*start))
*start-- = '\0';
key = g_strdup (buffer);
value = g_strdup (p);
g_ptr_array_add (cpuinfo->labels, key);
g_hash_table_insert (cpuinfo->values, key, value);
}
fclose (f);
sysinfo.flags = _glibtop_sysdeps_sysinfo;
}
glibtop_sysinfo *
glibtop_get_sysinfo_s (glibtop *server)
{
init_sysinfo (server);
return &sysinfo;
}

View File

@@ -1,80 +0,0 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 4 -*- */
/* $Id$ */
/* 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/error.h>
#include <glibtop/uptime.h>
static unsigned long _glibtop_sysdeps_uptime =
(1L << GLIBTOP_UPTIME_UPTIME) + (1L << GLIBTOP_UPTIME_IDLETIME);
/* Init function. */
int
glibtop_init_uptime_s (glibtop *server)
{
server->sysdeps.uptime = _glibtop_sysdeps_uptime;
return 0;
}
/* Provides uptime and idle time. */
#define FILENAME "/proc/uptime"
int
glibtop_get_uptime_s (glibtop *server, glibtop_uptime *buf)
{
char buffer [BUFSIZ], *p;
int fd, len;
glibtop_init_s (&server, GLIBTOP_SYSDEPS_UPTIME, 0);
memset (buf, 0, sizeof (glibtop_uptime));
fd = open (FILENAME, O_RDONLY);
if (fd < 0) {
glibtop_warn_io_r (server, "open (%s)", FILENAME);
return -1;
}
len = read (fd, buffer, BUFSIZ-1);
if (len < 0) {
close (fd);
glibtop_warn_io_r (server, "read (%s)", FILENAME);
return -1;
}
close (fd);
buffer [len] = '\0';
buf->uptime = strtod (buffer, &p);
buf->idletime = strtod (p, &p);
buf->flags = _glibtop_sysdeps_uptime;
return 0;
}