diff --git a/ChangeLog b/ChangeLog index 22fb4b80..87de4161 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,24 @@ +1998-07-03 Martin baulig + + * macros/gnome-libgtop-sysdeps.m4: No longer use + `glibtop_machine.h' for Linux. + + * sysdeps/linux/glibtop_machine.h: Removed. + + * sysdeps/guile/guile.awk: Using `scm_append' + instead of `gh_append'. + + * sysdeps/guile/names/guile-names.awk: dito. + + * sysdeps/linux/*.c: Using code from stable branch again. + + * include/glibtop/parameter.h: New file. + + * lib/parameter.c: New file. + + * lib/{open, init}.c: Done some more work on server + initialization + 1998-06-21 Martin Baulig * perl/*: Added perl interface. diff --git a/examples/first.c b/examples/first.c index d6031e9a..79180446 100644 --- a/examples/first.c +++ b/examples/first.c @@ -26,6 +26,8 @@ #include #include +#include + #include #include @@ -38,7 +40,8 @@ main (int argc, char *argv []) { glibtop_union data; glibtop_sysdeps sysdeps; - unsigned c, count, i, *ptr; + unsigned c, method, count, port, i, *ptr; + char buffer [BUFSIZ]; pid_t pid, ppid; count = PROFILE_COUNT; @@ -47,7 +50,23 @@ main (int argc, char *argv []) bindtextdomain (PACKAGE, GTOPLOCALEDIR); textdomain (PACKAGE); - glibtop_init (); + glibtop_init_r (&glibtop_global_server, 0, GLIBTOP_INIT_NO_OPEN); + + glibtop_get_parameter (GLIBTOP_PARAM_METHOD, &method, sizeof (method)); + + printf ("Method = %d\n", method); + + count = glibtop_get_parameter (GLIBTOP_PARAM_COMMAND, buffer, BUFSIZ); + buffer [count] = 0; + + printf ("Command = '%s'\n", buffer); + + count = glibtop_get_parameter (GLIBTOP_PARAM_HOST, buffer, BUFSIZ); + buffer [count] = 0; + + glibtop_get_parameter (GLIBTOP_PARAM_PORT, &port, sizeof (port)); + + printf ("Host = '%s' - %u\n\n", buffer, port); for (c = 0; c < count; c++) glibtop_get_cpu (&data.cpu); diff --git a/glibtop.h b/glibtop.h index 2ac3a5ec..9db6c74c 100644 --- a/glibtop.h +++ b/glibtop.h @@ -35,7 +35,8 @@ typedef struct _glibtop glibtop; struct _glibtop { - int fd; /* Server file descriptor */ + unsigned flags; + unsigned method; /* Server Method */ #ifdef HAVE_GLIBTOP_MACHINE_H glibtop_machine machine; /* Machine dependent data */ #endif diff --git a/include/glibtop/Makefile.am b/include/glibtop/Makefile.am index 53ee5c51..a0157346 100644 --- a/include/glibtop/Makefile.am +++ b/include/glibtop/Makefile.am @@ -5,4 +5,5 @@ glibtop_HEADERS = close.h loadavg.h prockernel.h procstate.h \ proctime.h shm_limits.h version.h cpu.h msg_limits.h \ procmem.h procuid.h swap.h write.h error.h open.h \ procsegment.h read.h sysdeps.h xmalloc.h global.h \ - output.h procsignal.h read_data.h union.h types.h + output.h procsignal.h read_data.h union.h types.h \ + parameter.h diff --git a/include/glibtop/open.h b/include/glibtop/open.h index 4e4e5408..a891727d 100644 --- a/include/glibtop/open.h +++ b/include/glibtop/open.h @@ -26,8 +26,19 @@ __BEGIN_DECLS +#define _GLIBTOP_INIT_STATE_INIT 0x10000 +#define _GLIBTOP_INIT_STATE_OPEN 0x20000 +#define _GLIBTOP_INIT_STATE_SERVER 0x40000 + +#define GLIBTOP_INIT_NO_OPEN 1 +#define GLIBTOP_INIT_NO_INIT 2 + #define GLIBTOP_OPEN_NO_OVERRIDE 1 +#define GLIBTOP_METHOD_DIRECT 1 +#define GLIBTOP_METHOD_PIPE 2 +#define GLIBTOP_METHOD_INET 3 + extern void glibtop_open_l __P((glibtop *, const char *, const unsigned long, const unsigned)); extern void glibtop_open_r __P((glibtop *, const char *, const unsigned long, const unsigned)); diff --git a/include/glibtop/parameter.h b/include/glibtop/parameter.h new file mode 100644 index 00000000..7614d3e5 --- /dev/null +++ b/include/glibtop/parameter.h @@ -0,0 +1,42 @@ +/* Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc. + This file is part of the Gnome Top Library. + Contributed by Martin Baulig , April 1998. + + The Gnome Top Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + The Gnome Top Library 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 + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with the GNU C Library; see the file COPYING.LIB. If not, + write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. */ + +#ifndef __GLIBTOP_PARAMETER_H__ +#define __GLIBTOP_PARAMETER_H__ + +#include +#include + +__BEGIN_DECLS + +#define GLIBTOP_PARAM_METHOD 1 +#define GLIBTOP_PARAM_FEATURES 2 +#define GLIBTOP_PARAM_COMMAND 3 +#define GLIBTOP_PARAM_HOST 4 +#define GLIBTOP_PARAM_PORT 5 + +#define glibtop_get_parameter(p1,p2,p3) glibtop_get_parameter_l(glibtop_global_server,p1,p2,p3) +#define glibtop_set_parameter(p1,p2,p3) glibtop_set_parameter_l(glibtop_global_server,p1,p2,p3) + +extern size_t glibtop_get_parameter_l __P((glibtop *, const unsigned, void *, size_t)); +extern void glibtop_set_parameter_l __P((glibtop *, const unsigned, const void *, size_t)); + +__END_DECLS + +#endif diff --git a/lib/Makefile.am b/lib/Makefile.am index 1e9e7c28..87d16c29 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -8,7 +8,8 @@ CFLAGS = -Wall -W @CFLAGS@ -DGTOP_SERVER=\""@LIBGTOP_SERVER@"\" -DDEBUG lib_LTLIBRARIES = libgtop.la -libgtop_la_SOURCES = init.c open.c close.c command.c read.c read_data.c write.c lib.c +libgtop_la_SOURCES = init.c open.c close.c command.c read.c read_data.c \ + write.c lib.c parameter.c BUILT_SOURCES = lib.c diff --git a/lib/init.c b/lib/init.c index ceefe35e..bc202c62 100644 --- a/lib/init.c +++ b/lib/init.c @@ -20,23 +20,168 @@ Boston, MA 02111-1307, USA. */ #include -#include +#include +#include -static glibtop _glibtop_global_server; -glibtop *glibtop_global_server = NULL; +#ifndef DEFAULT_PORT +#define DEFAULT_PORT 42800 +#endif + +static glibtop _glibtop_global_server; +glibtop *glibtop_global_server = &_glibtop_global_server; + + +void +_init_server (glibtop *server, const unsigned features) +{ + char *command, *temp; + + /* Try to get server command, but don't override if already + * set via glibtop_set_parameter () */ + + if (server->server_command == NULL) { + const char *temp = getenv ("LIBGTOP_SERVER") ? + getenv ("LIBGTOP_SERVER") : GTOP_SERVER; + + server->server_command = glibtop_strdup_r (server, temp); + } + + if (server->server_rsh == NULL) { + const char *temp = getenv ("LIBGTOP_RSH") ? + getenv ("LIBGTOP_RSH") : "/usr/bin/ssh"; + + server->server_rsh = glibtop_strdup_r (server, temp); + } + + /* Try to get server method, but don't override if already + * set via glibtop_set_parameter () */ + + if (server->method) return; + + /* If server->command doesn't start with a colon, then it is + * the full pathname of the server executable. */ + + if (server->server_command [0] != ':') { + if (features & glibtop_server_features) { + + /* We really need the server. */ + + server->method = GLIBTOP_METHOD_PIPE; + + } else { + + /* Fine. No server is needed, so we call the + * sysdeps functions directly. */ + + server->method = GLIBTOP_METHOD_DIRECT; + } + + return; + } + + + /* If the first character of 'server_command' is a colon, + * the first field is the method to connect to the server. */ + + /* Everything up to the next colon is the method. */ + + command = glibtop_strdup_r (server, server->server_command+1); + temp = strstr (command, ":"); + if (temp) *temp = 0; + + /* Dispatch method. */ + + if (!strcmp (command, "direct")) { + + /* Use sysdeps dir instead of connecting to server + * even if using the server would be required on + * the current system. */ + + server->method = GLIBTOP_METHOD_DIRECT; + + } else if (!strcmp (command, "inet")) { + + server->method = GLIBTOP_METHOD_INET; + + /* Connect to internet server. */ + + if (temp == NULL) { + /* If no value was set, we use 'localhost'. */ + if (server->server_host == NULL) + server->server_host = glibtop_strdup_r + (server, "localhost"); + } else { + char *temp2 = strstr (temp+1, ":"); + if (temp2) *temp2 = 0; + + /* Override default. */ + if (server->server_host) + glibtop_free_r (server, (char *) server->server_host); + + server->server_host = glibtop_strdup_r + (server, temp+1); + + temp = temp2; + } + + if (temp == NULL) { + /* If no value was set, we use DEFAULT_PORT. */ + if (server->server_port == 0) + server->server_port = DEFAULT_PORT; + } else { + char *temp2 = strstr (temp+1, ":"); + if (temp2) *temp2 = 0; + + if (sscanf (temp+1, "%ld", &server->server_port) != 1) + server->server_port = DEFAULT_PORT; + + temp = temp2 ? temp2 + 1 : temp2; + } + } else { + glibtop_error_r (server, "Unknown server method '%s'", + server->server_command+1); + } + + glibtop_free_r (server, command); +} glibtop * -glibtop_init_r (glibtop **server, const unsigned long features, +glibtop_init_r (glibtop **server_ptr, const unsigned long features, const unsigned flags) { - if (*server != NULL) - return *server; + glibtop *server; - if (glibtop_global_server == NULL) { - glibtop_global_server = &_glibtop_global_server; + if (server_ptr == NULL) + return NULL; + + if (*server_ptr == NULL) + *server_ptr = glibtop_global_server; + + server = *server_ptr; + + /* Should we do the initialization? */ + + if (flags & GLIBTOP_INIT_NO_INIT) + return server; + + /* Do the initialization, but only if not already initialized. */ + + if ((server->flags & _GLIBTOP_INIT_STATE_INIT) == 0) { + _init_server (server, features); + + server->flags |= _GLIBTOP_INIT_STATE_INIT; + } + + /* Should we open the server? */ + + if (flags & GLIBTOP_INIT_NO_OPEN) + return server; + + /* Open server, but only if not already opened. */ + + if ((server->flags & _GLIBTOP_INIT_STATE_OPEN) == 0) glibtop_open_l (glibtop_global_server, "glibtop", features, flags); - } - return *server = glibtop_global_server; + return server; } diff --git a/lib/lib.awk b/lib/lib.awk index fc901bcd..7498b841 100644 --- a/lib/lib.awk +++ b/lib/lib.awk @@ -4,6 +4,7 @@ BEGIN { print ""; print "#include "; + print "#include "; print "#include "; print "#include "; @@ -29,7 +30,9 @@ function output(feature) { print "{"; print "\tglibtop_init_r (&server, GLIBTOP_SYSDEPS_"toupper(feature)", 0);"; print ""; - print "\tif (server->features & GLIBTOP_SYSDEPS_"toupper(feature)") {"; + print "\tif ((server->flags & _GLIBTOP_INIT_STATE_SERVER) &&"; + print "\t (server->features & GLIBTOP_SYSDEPS_"toupper(feature)"))"; + print "\t{"; if (feature ~ /^proc_/) { print "\t\t"prefix"glibtop_call_l (server, GLIBTOP_CMND_"toupper(feature)", sizeof (pid_t),"; diff --git a/lib/open.c b/lib/open.c index fccf3635..1bf95032 100644 --- a/lib/open.c +++ b/lib/open.c @@ -33,97 +33,36 @@ void glibtop_open_l (glibtop *server, const char *program_name, const unsigned long features, const unsigned flags) { - char version [BUFSIZ], buffer [BUFSIZ], *temp, *temp2; - glibtop_sysdeps sysdeps; - int connect_type, ret; - - memset (server, 0, sizeof (glibtop)); - server->name = program_name; - /* Is the user allowed to override the server ? */ + /* It is important to set _GLIBTOP_INIT_STATE_OPEN here when we + * do recursive calls to glibtop_init_r (). */ + + server->flags |= _GLIBTOP_INIT_STATE_OPEN; + + if (server->method == GLIBTOP_METHOD_INET) { + int connect_type; + + fprintf (stderr, "Connecting to '%s' port %d.\n", + server->server_host, server->server_port); + + connect_type = glibtop_make_connection + (server->server_host, server->server_port, + &server->socket); + + server->features = -1; - if (flags & GLIBTOP_OPEN_NO_OVERRIDE) return; - - /* Try to get data from environment. */ - - temp = getenv ("LIBGTOP_SERVER") ? - getenv ("LIBGTOP_SERVER") : GTOP_SERVER; - - server->server_command = glibtop_strdup_r (server, temp); - - temp = getenv ("LIBGTOP_RSH") ? - getenv ("LIBGTOP_RSH") : "rsh"; - - server->server_rsh = glibtop_strdup_r (server, temp); - - /* If the first character of 'server_command' is a colon, - * the first field is the method to connect to the server. */ - - if (server->server_command [0] == ':') { - - /* Everything up to the next colon is the method. */ - - temp = strstr (server->server_command+1, ":"); - if (temp) *temp = 0; - - /* Dispatch method. */ - - if (!strcmp (server->server_command+1, "direct")) { - - /* Use sysdeps dir instead of connecting to server - * even if using the server would be required on - * the current system. */ - - return; - - } else if (!strcmp (server->server_command+1, "inet")) { - - /* Connect to internet server. */ - - if (temp == NULL) { - server->server_host = glibtop_strdup_r - (server, "localhost"); - } else { - temp2 = strstr (temp+1, ":"); - if (temp2) *temp2 = 0; - - server->server_host = glibtop_strdup_r - (server, temp+1); - - temp = temp2; - } - - if (temp == NULL) { - server->server_port = DEFAULT_PORT; - } else { - temp2 = strstr (temp+1, ":"); - if (temp2) *temp2 = 0; - - ret = sscanf (temp+1, "%d", &server->server_port); - - if (ret != 1) - server->server_port = DEFAULT_PORT; - - temp = temp2 ? temp2 + 1 : temp2; - } - - fprintf (stderr, "Connecting to '%s' port %d.\n", - server->server_host, server->server_port); - - connect_type = glibtop_make_connection - (server->server_host, server->server_port, - &server->socket); - - server->features = -1; - - return; - } } - glibtop_call_l (server, GLIBTOP_CMND_SYSDEPS, 0, NULL, - sizeof (glibtop_sysdeps), &sysdeps); - - server->features = sysdeps.features; + /* If the server has been started, ask it for its features. */ + + if (server->flags & _GLIBTOP_INIT_STATE_SERVER) { + glibtop_sysdeps sysdeps; + + glibtop_call_l (server, GLIBTOP_CMND_SYSDEPS, 0, NULL, + sizeof (glibtop_sysdeps), &sysdeps); + + server->features = sysdeps.features; + } } diff --git a/lib/parameter.c b/lib/parameter.c new file mode 100644 index 00000000..ca3c5d0c --- /dev/null +++ b/lib/parameter.c @@ -0,0 +1,62 @@ +/* $Id$ */ + +/* Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc. + This file is part of the Gnome Top Library. + Contributed by Martin Baulig , April 1998. + + The Gnome Top Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + The Gnome Top Library 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 + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with the GNU C Library; see the file COPYING.LIB. If not, + write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. */ + +#include +#include + +#define _write_data(ptr,size) \ + if ((data_ptr == NULL) || (data_size < size)) return -size; \ + if (ptr == NULL) { strcpy (data_ptr, ""); return 1; } \ + memcpy (data_ptr, ptr, size); \ + return size; + +#define _strlen(ptr) (ptr ? strlen (ptr) : 0) + +size_t +glibtop_get_parameter_l (glibtop *server, const unsigned parameter, + void *data_ptr, size_t data_size) +{ + switch (parameter) { + case GLIBTOP_PARAM_METHOD: + _write_data (&server->method, + sizeof (server->method)); + case GLIBTOP_PARAM_FEATURES: + _write_data (&server->features, + sizeof (server->features)); + case GLIBTOP_PARAM_COMMAND: + _write_data (server->server_command, + _strlen(server->server_command)); + case GLIBTOP_PARAM_HOST: + _write_data (server->server_host, + _strlen(server->server_host)); + case GLIBTOP_PARAM_PORT: + _write_data (&server->server_port, + sizeof (server->server_port)); + } + + return 0; +} + +void +glibtop_set_parameter_l (glibtop *server, const unsigned parameter, + const void *data_ptr, size_t data_size) +{ +} diff --git a/src/daemon/.cvsignore b/src/daemon/.cvsignore index 3dda7298..e86ad538 100644 --- a/src/daemon/.cvsignore +++ b/src/daemon/.cvsignore @@ -1,2 +1,3 @@ Makefile.in Makefile +gnuserv diff --git a/sysdeps/guile/guile.awk b/sysdeps/guile/guile.awk index 25d293f0..8722a6e3 100644 --- a/sysdeps/guile/guile.awk +++ b/sysdeps/guile/guile.awk @@ -55,7 +55,7 @@ BEGIN { print ""; print "\tif (ptr) {"; print "\t\tfor (i = 0; i < proclist.number; i++)"; - print "\t\t\tlist = gh_append"; + print "\t\t\tlist = scm_append"; print "\t\t\t\t(gh_list (list,"; print "\t\t\t\t\t gh_list (gh_ulong2scm ((unsigned long) ptr [i])),"; print "\t\t\t\t\t SCM_UNDEFINED));"; diff --git a/sysdeps/guile/names/guile-names.awk b/sysdeps/guile/names/guile-names.awk index 11693343..c76f3b60 100644 --- a/sysdeps/guile/names/guile-names.awk +++ b/sysdeps/guile/names/guile-names.awk @@ -22,7 +22,7 @@ function output(feature) { print "\tlist = gh_list (SCM_UNDEFINED);"; print ""; print "\tfor (i = 0; i < GLIBTOP_MAX_"toupper(feature)"; i++)"; - print "\t\tlist = gh_append"; + print "\t\tlist = scm_append"; print "\t\t\t(gh_list (list,"; print "\t\t\t\t gh_list (gh_str02scm ((char *) glibtop_names_"feature" [i])),"; print "\t\t\t\t SCM_UNDEFINED));"; @@ -41,7 +41,7 @@ function output(feature) { print "\tlist = gh_list (SCM_UNDEFINED);"; print ""; print "\tfor (i = 0; i < GLIBTOP_MAX_"toupper(feature)"; i++)"; - print "\t\tlist = gh_append"; + print "\t\tlist = scm_append"; print "\t\t\t(gh_list (list,"; print "\t\t\t\t gh_list (gh_ulong2scm (glibtop_types_"feature" [i])),"; print "\t\t\t\t SCM_UNDEFINED));"; @@ -60,7 +60,7 @@ function output(feature) { print "\tlist = gh_list (SCM_UNDEFINED);"; print ""; print "\tfor (i = 0; i < GLIBTOP_MAX_"toupper(feature)"; i++)"; - print "\t\tlist = gh_append"; + print "\t\tlist = scm_append"; print "\t\t\t(gh_list (list,"; print "\t\t\t\t gh_list (gh_str02scm (gettext"; print "\t\t\t\t\t\t\t(glibtop_labels_"feature" [i]))),"; @@ -80,7 +80,7 @@ function output(feature) { print "\tlist = gh_list (SCM_UNDEFINED);"; print ""; print "\tfor (i = 0; i < GLIBTOP_MAX_"toupper(feature)"; i++)"; - print "\t\tlist = gh_append"; + print "\t\tlist = scm_append"; print "\t\t\t(gh_list (list,"; print "\t\t\t\t gh_list (gh_str02scm (gettext"; print "\t\t\t\t\t\t\t(glibtop_descriptions_"feature" [i]))),"; diff --git a/sysdeps/linux/cpu.c b/sysdeps/linux/cpu.c index c6ce8976..750161a6 100644 --- a/sysdeps/linux/cpu.c +++ b/sysdeps/linux/cpu.c @@ -35,8 +35,7 @@ static const unsigned long _glibtop_sysdeps_cpu = void glibtop_get_cpu_s (glibtop *server, glibtop_cpu *buf) { - char buffer [BUFSIZ], *tmp; - int fd = 0, ret; + FILE *f; glibtop_init_r (&server, 0, 0); @@ -44,25 +43,15 @@ glibtop_get_cpu_s (glibtop *server, glibtop_cpu *buf) buf->flags = _glibtop_sysdeps_cpu; - fd = open (FILENAME, O_RDONLY); - if (fd == -1) - glibtop_error_r (server, "open (%s): %s", - FILENAME, strerror (errno)); + f = fopen ("/proc/stat", "r"); + if (!f) return; - ret = read (fd, buffer, BUFSIZ); - if (ret == -1) - glibtop_error_r (server, "read (%s): %s", - FILENAME, strerror (errno)); - - tmp = skip_token (buffer); /* "cpu" */ - - buf->user = strtoul (tmp, &tmp, 10); - buf->nice = strtoul (tmp, &tmp, 10); - buf->sys = strtoul (tmp, &tmp, 10); - buf->idle = strtoul (tmp, &tmp, 10); + fscanf (f, "cpu %lu %lu %lu %lu\n", + &buf->user, &buf->nice, &buf->sys, &buf->idle); buf->total = buf->user + buf->nice + buf->sys + buf->idle; buf->frequency = 100; - close (fd); + + fclose (f); } diff --git a/sysdeps/linux/loadavg.c b/sysdeps/linux/loadavg.c index 57717205..dbda3e6d 100644 --- a/sysdeps/linux/loadavg.c +++ b/sysdeps/linux/loadavg.c @@ -33,8 +33,7 @@ static const unsigned long _glibtop_sysdeps_loadavg = void glibtop_get_loadavg_s (glibtop *server, glibtop_loadavg *buf) { - char buffer [BUFSIZ], *tmp; - int fd = 0, ret; + FILE *f; glibtop_init_r (&server, 0, 0); @@ -42,19 +41,11 @@ glibtop_get_loadavg_s (glibtop *server, glibtop_loadavg *buf) buf->flags = _glibtop_sysdeps_loadavg; - fd = open (FILENAME, O_RDONLY); - if (fd == -1) - glibtop_error_r (server, "open (%s): %s", - FILENAME, strerror (errno)); + f = fopen ("/proc/loadavg", "r"); + if (!f) return; - ret = read (fd, buffer, BUFSIZ); - if (ret == -1) - glibtop_error_r (server, "read (%s): %s", - FILENAME, strerror (errno)); - - buf->loadavg [0] = strtod (buffer, &tmp); - buf->loadavg [1] = strtod (tmp, &tmp); - buf->loadavg [2] = strtod (tmp, &tmp); - - close (fd); + fscanf (f, "%lf %lf %lf\n", + &buf->loadavg [0], &buf->loadavg [1], &buf->loadavg [2]); + + fclose (f); } diff --git a/sysdeps/linux/mem.c b/sysdeps/linux/mem.c index b605a35d..4d6d7f72 100644 --- a/sysdeps/linux/mem.c +++ b/sysdeps/linux/mem.c @@ -36,8 +36,7 @@ static const unsigned long _glibtop_sysdeps_mem = void glibtop_get_mem_s (glibtop *server, glibtop_mem *buf) { - char buffer [BUFSIZ], *tmp; - int fd = 0, ret; + FILE *f; glibtop_init_r (&server, 0, 0); @@ -45,28 +44,13 @@ glibtop_get_mem_s (glibtop *server, glibtop_mem *buf) buf->flags = _glibtop_sysdeps_mem; - fd = open (FILENAME, O_RDONLY); - if (fd == -1) - glibtop_error_r (server, "open (%s): %s", - FILENAME, strerror (errno)); + f = fopen ("/proc/meminfo", "r"); + if (!f) return; - ret = read (fd, buffer, BUFSIZ); - if (ret == -1) - glibtop_error_r (server, "read (%s): %s", - FILENAME, strerror (errno)); - - tmp = strchr (buffer, '\n'); - tmp = skip_token (tmp); /* "Mem:" */ - tmp = skip_token (tmp); /* total memory */ - - buf->total = strtoul (tmp, &tmp, 10); - buf->used = strtoul (tmp, &tmp, 10); - buf->free = strtoul (tmp, &tmp, 10); - buf->shared = strtoul (tmp, &tmp, 10); - buf->buffer = strtoul (tmp, &tmp, 10); - buf->cached = strtoul (tmp, &tmp, 10); + fscanf (f, "%*[^\n]\nMem: %lu %lu %lu %lu %lu %lu\n", + &buf->total, &buf->used, &buf->free, &buf->shared, &buf->buffer, &buf->cached); buf->user = buf->total - buf->free - buf->shared - buf->buffer; - close (fd); + fclose (f); } diff --git a/sysdeps/linux/prockernel.c b/sysdeps/linux/prockernel.c index 7784eac3..777af80d 100644 --- a/sysdeps/linux/prockernel.c +++ b/sysdeps/linux/prockernel.c @@ -34,8 +34,9 @@ static const unsigned long _glibtop_sysdeps_proc_kernel = void glibtop_get_proc_kernel_s (glibtop *server, glibtop_proc_kernel *buf, pid_t pid) { - char buffer [BUFSIZ], input [BUFSIZ], *tmp; - int fd = 0, nread; + char input [BUFSIZ], *tmp; + int nread; + FILE *f; glibtop_init_r (&server, 0, 0); @@ -48,24 +49,23 @@ glibtop_get_proc_kernel_s (glibtop *server, glibtop_proc_kernel *buf, pid_t pid) } sprintf (input, "/proc/%d/stat", pid); - - fd = open (input, O_RDONLY); - if (fd == -1) - glibtop_error_r (server, "open (%s): %s", - input, strerror (errno)); - - nread = read (fd, buffer, BUFSIZ); - if (nread == -1) - glibtop_error_r (server, "read (%s): %s", - input, strerror (errno)); - - buffer [nread] = 0; - close (fd); - + + f = fopen (input, "r"); + if (!f) return; + + nread = fread (input, 1, BUFSIZ, f); + + if (nread < 0) { + fclose (f); + return; + } + + input [nread] = 0; + /* This is from guile-utils/gtop/proc/readproc.c */ /* split into "PID (cmd" and "" */ - tmp = strrchr (buffer, ')'); + tmp = strrchr (input, ')'); *tmp = '\0'; /* replace trailing ')' with NUL */ /* parse these two strings separately, skipping the leading "(". */ @@ -76,6 +76,8 @@ glibtop_get_proc_kernel_s (glibtop *server, glibtop_proc_kernel *buf, pid_t pid) &buf->k_flags, &buf->min_flt, &buf->cmin_flt, &buf->maj_flt, &buf->cmaj_flt, &buf->kstk_esp, &buf->kstk_eip, &buf->wchan); + + fclose (f); buf->flags = _glibtop_sysdeps_proc_kernel; } diff --git a/sysdeps/linux/procmem.c b/sysdeps/linux/procmem.c index f1108eb3..a39a0553 100644 --- a/sysdeps/linux/procmem.c +++ b/sysdeps/linux/procmem.c @@ -33,8 +33,9 @@ static const unsigned long _glibtop_sysdeps_proc_mem = void glibtop_get_proc_mem_s (glibtop *server, glibtop_proc_mem *buf, pid_t pid) { - char buffer [BUFSIZ], input [BUFSIZ], *tmp; - int fd = 0, nread; + char input [BUFSIZ], *tmp; + int nread; + FILE *f; glibtop_init_r (&server, 0, 0); @@ -47,24 +48,23 @@ glibtop_get_proc_mem_s (glibtop *server, glibtop_proc_mem *buf, pid_t pid) } sprintf (input, "/proc/%d/stat", pid); - - fd = open (input, O_RDONLY); - if (fd == -1) - glibtop_error_r (server, "open (%s): %s", - input, strerror (errno)); - - nread = read (fd, buffer, BUFSIZ); - if (nread == -1) - glibtop_error_r (server, "read (%s): %s", - input, strerror (errno)); - - buffer [nread] = 0; - close (fd); + + f = fopen (input, "r"); + if (!f) return; + + nread = fread (input, 1, BUFSIZ, f); + + if (nread < 0) { + fclose (f); + return; + } + + input [nread] = 0; /* This is from guile-utils/gtop/proc/readproc.c */ /* split into "PID (cmd" and "" */ - tmp = strrchr (buffer, ')'); + tmp = strrchr (input, ')'); *tmp = '\0'; /* replace trailing ')' with NUL */ /* parse these two strings separately, skipping the leading "(". */ sscanf(tmp + 2, /* skip space after ')' too */ @@ -72,23 +72,26 @@ glibtop_get_proc_mem_s (glibtop *server, glibtop_proc_mem *buf, pid_t pid) "%*d %*d %*d %*d %*d %*d %*u %*u %*d %lu " "%lu %lu", &buf->vsize, &buf->rss, &buf->rss_rlim); - sprintf (input, "/proc/%d/statm", pid); - - fd = open (input, O_RDONLY); - if (fd == -1) - glibtop_error_r (server, "open (%s): %s", - input, strerror (errno)); - - nread = read (fd, buffer, BUFSIZ); - if (nread == -1) - glibtop_error_r (server, "read (%s): %s", - input, strerror (errno)); + fclose (f); - buffer [nread] = 0; - close (fd); - - sscanf (buffer, "%ld %ld %ld", + sprintf (input, "/proc/%d/statm", pid); + + f = fopen (input, "r"); + if (!f) return; + + nread = fread (input, 1, BUFSIZ, f); + + if (nread < 0) { + fclose (f); + return; + } + + input [nread] = 0; + + sscanf (input, "%ld %ld %ld", &buf->size, &buf->resident, &buf->share); + fclose (f); + buf->flags = _glibtop_sysdeps_proc_mem; } diff --git a/sysdeps/linux/procsegment.c b/sysdeps/linux/procsegment.c index 3c4ea965..b5b37ba4 100644 --- a/sysdeps/linux/procsegment.c +++ b/sysdeps/linux/procsegment.c @@ -35,8 +35,9 @@ void glibtop_get_proc_segment_s (glibtop *server, glibtop_proc_segment *buf, pid_t pid) { - char buffer [BUFSIZ], input [BUFSIZ], *tmp; - int fd = 0, nread; + char input [BUFSIZ], *tmp; + int nread; + FILE *f; glibtop_init_r (&server, 0, 0); @@ -49,24 +50,23 @@ glibtop_get_proc_segment_s (glibtop *server, glibtop_proc_segment *buf, } sprintf (input, "/proc/%d/stat", pid); - - fd = open (input, O_RDONLY); - if (fd == -1) - glibtop_error_r (server, "open (%s): %s", - input, strerror (errno)); - - nread = read (fd, buffer, BUFSIZ); - if (nread == -1) - glibtop_error_r (server, "read (%s): %s", - input, strerror (errno)); - - buffer [nread] = 0; - close (fd); + f = fopen (input, "r"); + if (!f) return; + + nread = fread (input, 1, BUFSIZ, f); + + if (nread < 0) { + fclose (f); + return; + } + + input [nread] = 0; + /* This is from guile-utils/gtop/proc/readproc.c */ /* split into "PID (cmd" and "" */ - tmp = strrchr (buffer, ')'); + tmp = strrchr (input, ')'); *tmp = '\0'; /* replace trailing ')' with NUL */ /* parse these two strings separately, skipping the leading "(". */ sscanf(tmp + 2, /* skip space after ')' too */ @@ -75,23 +75,26 @@ glibtop_get_proc_segment_s (glibtop *server, glibtop_proc_segment *buf, "%*u %*u %lu %lu %lu", &buf->start_code, &buf->end_code, &buf->start_stack); - sprintf (input, "/proc/%d/statm", pid); - - fd = open (input, O_RDONLY); - if (fd == -1) - glibtop_error_r (server, "open (%s): %s", - input, strerror (errno)); - - nread = read (fd, buffer, BUFSIZ); - if (nread == -1) - glibtop_error_r (server, "read (%s): %s", - input, strerror (errno)); - - buffer [nread] = 0; - close (fd); + fclose (f); - sscanf (buffer, "%*d %*d %*d %ld %ld %ld %ld", + sprintf (input, "/proc/%d/statm", pid); + + f = fopen (input, "r"); + if (!f) return; + + nread = fread (input, 1, BUFSIZ, f); + + if (nread < 0) { + fclose (f); + return; + } + + input [nread] = 0; + + sscanf (input, "%*d %*d %*d %ld %ld %ld %ld", &buf->trs, &buf->lrs, &buf->drs, &buf->dt); + fclose (f); + buf->flags = _glibtop_sysdeps_proc_segment; } diff --git a/sysdeps/linux/procsignal.c b/sysdeps/linux/procsignal.c index 14bdb353..3b409e8b 100644 --- a/sysdeps/linux/procsignal.c +++ b/sysdeps/linux/procsignal.c @@ -32,8 +32,9 @@ static const unsigned long _glibtop_sysdeps_proc_signal = void glibtop_get_proc_signal_s (glibtop *server, glibtop_proc_signal *buf, pid_t pid) { - char buffer [BUFSIZ], input [BUFSIZ], *tmp; - int fd = 0, nread; + char input [BUFSIZ], *tmp; + int nread; + FILE *f; glibtop_init_r (&server, 0, 0); @@ -46,24 +47,23 @@ glibtop_get_proc_signal_s (glibtop *server, glibtop_proc_signal *buf, pid_t pid) } sprintf (input, "/proc/%d/stat", pid); - - fd = open (input, O_RDONLY); - if (fd == -1) - glibtop_error_r (server, "open (%s): %s", - input, strerror (errno)); - - nread = read (fd, buffer, BUFSIZ); - if (nread == -1) - glibtop_error_r (server, "read (%s): %s", - input, strerror (errno)); - - buffer [nread] = 0; - close (fd); + f = fopen (input, "r"); + if (!f) return; + + nread = fread (input, 1, BUFSIZ, f); + + if (nread < 0) { + fclose (f); + return; + } + + input [nread] = 0; + /* This is from guile-utils/gtop/proc/readproc.c */ /* split into "PID (cmd" and "" */ - tmp = strrchr (buffer, ')'); + tmp = strrchr (input, ')'); *tmp = '\0'; /* replace trailing ')' with NUL */ /* parse these two strings separately, skipping the leading "(". */ sscanf(tmp + 2, /* skip space after ')' too */ @@ -73,5 +73,7 @@ glibtop_get_proc_signal_s (glibtop *server, glibtop_proc_signal *buf, pid_t pid) &buf->signal, &buf->blocked, &buf->sigignore, &buf->sigcatch); + fclose (f); + buf->flags = _glibtop_sysdeps_proc_signal; } diff --git a/sysdeps/linux/procstate.c b/sysdeps/linux/procstate.c index 03432969..bcf55662 100644 --- a/sysdeps/linux/procstate.c +++ b/sysdeps/linux/procstate.c @@ -34,9 +34,10 @@ static const unsigned long _glibtop_sysdeps_proc_state = void glibtop_get_proc_state_s (glibtop *server, glibtop_proc_state *buf, pid_t pid) { - char buffer [BUFSIZ], input [BUFSIZ], *tmp; + char input [BUFSIZ], *tmp; struct stat statb; - int fd, nread; + int nread; + FILE *f; glibtop_init_r (&server, 0, 0); @@ -61,30 +62,28 @@ glibtop_get_proc_state_s (glibtop *server, glibtop_proc_state *buf, pid_t pid) buf->uid = statb.st_uid; buf->gid = statb.st_gid; - - fd = open (input, O_RDONLY); - if (fd == -1) - glibtop_error_r (server, "open (%s): %s", - input, strerror (errno)); - - nread = read (fd, buffer, BUFSIZ); - if (nread == -1) - glibtop_error_r (server, "read (%s): %s", - input, strerror (errno)); - buffer [nread] = 0; - + f = fopen (input, "r"); + if (!f) return; + + nread = fread (input, 1, BUFSIZ, f); + + if (nread < 0) { + fclose (f); + return; + } + + input [nread] = 0; + /* This is from guile-utils/gtop/proc/readproc.c */ /* split into "PID (cmd" and "" */ - tmp = strrchr (buffer, ')'); + tmp = strrchr (input, ')'); *tmp = '\0'; /* replace trailing ')' with NUL */ /* parse these two strings separately, skipping the leading "(". */ memset (buf->cmd, 0, sizeof (buf->cmd)); - sscanf (buffer, "%d (%39c", &pid, buf->cmd); + sscanf (input, "%d (%39c", &pid, buf->cmd); sscanf(tmp + 2, "%c", &buf->state); /* skip space after ')' too */ - close (fd); - buf->flags = _glibtop_sysdeps_proc_state; } diff --git a/sysdeps/linux/proctime.c b/sysdeps/linux/proctime.c index 0d740bf0..0d0eaeda 100644 --- a/sysdeps/linux/proctime.c +++ b/sysdeps/linux/proctime.c @@ -34,8 +34,9 @@ static const unsigned long _glibtop_sysdeps_proc_time = void glibtop_get_proc_time_s (glibtop *server, glibtop_proc_time *buf, pid_t pid) { - char buffer [BUFSIZ], input [BUFSIZ], *tmp; - int fd = 0, nread; + char input [BUFSIZ], *tmp; + int nread; + FILE *f; glibtop_init_r (&server, 0, 0); @@ -48,24 +49,23 @@ glibtop_get_proc_time_s (glibtop *server, glibtop_proc_time *buf, pid_t pid) } sprintf (input, "/proc/%d/stat", pid); - - fd = open (input, O_RDONLY); - if (fd == -1) - glibtop_error_r (server, "open (%s): %s", - input, strerror (errno)); - - nread = read (fd, buffer, BUFSIZ); - if (nread == -1) - glibtop_error_r (server, "read (%s): %s", - input, strerror (errno)); - - buffer [nread] = 0; - close (fd); + f = fopen (input, "r"); + if (!f) return; + + nread = fread (input, 1, BUFSIZ, f); + + if (nread < 0) { + fclose (f); + return; + } + + input [nread] = 0; + /* This is from guile-utils/gtop/proc/readproc.c */ /* split into "PID (cmd" and "" */ - tmp = strrchr (buffer, ')'); + tmp = strrchr (input, ')'); *tmp = '\0'; /* replace trailing ')' with NUL */ /* parse these two strings separately, skipping the leading "(". */ sscanf(tmp + 2, /* skip space after ')' too */ @@ -74,5 +74,7 @@ glibtop_get_proc_time_s (glibtop *server, glibtop_proc_time *buf, pid_t pid) &buf->utime, &buf->stime, &buf->cutime, &buf->cstime, &buf->timeout, &buf->it_real_value, &buf->start_time); + fclose (f); + buf->flags = _glibtop_sysdeps_proc_time; } diff --git a/sysdeps/linux/procuid.c b/sysdeps/linux/procuid.c index ff2afb19..582a9ebd 100644 --- a/sysdeps/linux/procuid.c +++ b/sysdeps/linux/procuid.c @@ -38,8 +38,9 @@ static const unsigned long _glibtop_sysdeps_proc_uid = void glibtop_get_proc_uid_s (glibtop *server, glibtop_proc_uid *buf, pid_t pid) { - char buffer [BUFSIZ], input [BUFSIZ], *tmp; - int fd = 0, nread; + char input [BUFSIZ], *tmp; + int nread; + FILE *f; glibtop_init_r (&server, 0, 0); @@ -52,23 +53,22 @@ glibtop_get_proc_uid_s (glibtop *server, glibtop_proc_uid *buf, pid_t pid) } sprintf (input, "/proc/%d/status", pid); - - fd = open (input, O_RDONLY); - if (fd == -1) - glibtop_error_r (server, "open (%s): %s", - input, strerror (errno)); - - nread = read (fd, buffer, BUFSIZ); - if (nread == -1) - glibtop_error_r (server, "read (%s): %s", - input, strerror (errno)); - buffer [nread] = 0; - close (fd); + f = fopen (input, "r"); + if (!f) return; + + nread = fread (input, 1, BUFSIZ, f); + + if (nread < 0) { + fclose (f); + return; + } + + input [nread] = 0; /* Search substring 'Pid:' */ - tmp = strstr (buffer, "Pid:"); + tmp = strstr (input, "Pid:"); if (tmp == NULL) return; @@ -76,25 +76,26 @@ glibtop_get_proc_uid_s (glibtop *server, glibtop_proc_uid *buf, pid_t pid) "Gid: %u %u %*u %*u\n", &buf->pid, &buf->ppid, &buf->uid, &buf->euid, &buf->gid, &buf->egid); + fclose (f); + sprintf (input, "/proc/%d/stat", pid); - - fd = open (input, O_RDONLY); - if (fd == -1) - glibtop_error_r (server, "open (%s): %s", - input, strerror (errno)); - - nread = read (fd, buffer, BUFSIZ); - if (nread == -1) - glibtop_error_r (server, "read (%s): %s", - input, strerror (errno)); - - buffer [nread] = 0; - close (fd); + + f = fopen (input, "r"); + if (!f) return; + + nread = fread (input, 1, BUFSIZ, f); + + if (nread < 0) { + fclose (f); + return; + } + + input [nread] = 0; /* This is from guile-utils/gtop/proc/readproc.c */ /* split into "PID (cmd" and "" */ - tmp = strrchr (buffer, ')'); + tmp = strrchr (input, ')'); *tmp = '\0'; /* replace trailing ')' with NUL */ /* parse these two strings separately, skipping the leading "(". */ sscanf(tmp + 2, /* skip space after ')' too */ @@ -116,5 +117,7 @@ glibtop_get_proc_uid_s (glibtop *server, glibtop_proc_uid *buf, pid_t pid) /* when tty wasn't full devno */ buf->tty = 4*0x100 + buf->tty; + fclose (f); + buf->flags = _glibtop_sysdeps_proc_uid; } diff --git a/sysdeps/linux/swap.c b/sysdeps/linux/swap.c index 57f159dd..dd54e1bd 100644 --- a/sysdeps/linux/swap.c +++ b/sysdeps/linux/swap.c @@ -34,8 +34,7 @@ static unsigned long _glibtop_sysdeps_swap = void glibtop_get_swap_s (glibtop *server, glibtop_swap *buf) { - char buffer [BUFSIZ], *tmp; - int fd = 0, ret; + FILE *f; glibtop_init_r (&server, 0, 0); @@ -43,24 +42,11 @@ glibtop_get_swap_s (glibtop *server, glibtop_swap *buf) buf->flags = _glibtop_sysdeps_swap; - fd = open (FILENAME, O_RDONLY); - if (fd == -1) - glibtop_error_r (server, "open (%s): %s", - FILENAME, strerror (errno)); + f = fopen ("/proc/meminfo", "r"); + if (!f) return; - ret = read (fd, buffer, BUFSIZ); - if (ret == -1) - glibtop_error_r (server, "read (%s): %s", - FILENAME, strerror (errno)); + fscanf (f, "%*[^\n]\n%*[^\n]\nSwap: %lu %lu %lu\n", + &buf->total, &buf->used, &buf->free); - tmp = strchr (buffer, '\n'); - tmp = strchr (tmp+1, '\n'); - - tmp = skip_token (tmp); /* "Swap:" */ - - buf->total = strtoul (tmp, &tmp, 10); - buf->used = strtoul (tmp, &tmp, 10); - buf->free = strtoul (tmp, &tmp, 10); - - close (fd); + fclose (f); } diff --git a/sysdeps/linux/uptime.c b/sysdeps/linux/uptime.c index 17b263cb..8233e1ff 100644 --- a/sysdeps/linux/uptime.c +++ b/sysdeps/linux/uptime.c @@ -33,8 +33,7 @@ static unsigned long _glibtop_sysdeps_uptime = void glibtop_get_uptime_s (glibtop *server, glibtop_uptime *buf) { - int fd, ret; - char buffer [BUFSIZ]; + FILE *f; glibtop_init_r (&server, 0, 0); @@ -42,17 +41,10 @@ glibtop_get_uptime_s (glibtop *server, glibtop_uptime *buf) buf->flags = _glibtop_sysdeps_uptime; - fd = open (FILENAME, O_RDONLY); - if (fd == -1) - glibtop_error_r (server, "open (%s): %s", - FILENAME, strerror (errno)); + f = fopen ("/proc/uptime", "r"); + if (!f) return; - ret = read (fd, buffer, BUFSIZ); - if (ret == -1) - glibtop_error_r (server, "read (%s): %s", - FILENAME, strerror (errno)); + fscanf (f, "%lf %lf\n", &buf->uptime, &buf->idletime); - sscanf (buffer, "%lf %lf\n", &buf->uptime, &buf->idletime); - - close (fd); + fclose (f); }