summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--sysdeps/linux/ChangeLog14
-rw-r--r--sysdeps/linux/cpu.c13
-rw-r--r--sysdeps/linux/glibtop_server.c48
-rw-r--r--sysdeps/linux/glibtop_server.h31
-rw-r--r--sysdeps/linux/loadavg.c13
-rw-r--r--sysdeps/linux/mem.c19
-rw-r--r--sysdeps/linux/open.c13
-rw-r--r--sysdeps/linux/swap.c25
-rw-r--r--sysdeps/linux/sysinfo.c21
-rw-r--r--sysdeps/linux/uptime.c13
10 files changed, 92 insertions, 118 deletions
diff --git a/sysdeps/linux/ChangeLog b/sysdeps/linux/ChangeLog
index d2e807aa..d2fa822b 100644
--- a/sysdeps/linux/ChangeLog
+++ b/sysdeps/linux/ChangeLog
@@ -1,3 +1,17 @@
+2004-07-03 Benoît Dejean <tazforever@dlfp.org>
+
+ * glibtop_server.h:
+ * glibtop_server.c: (try_file_to_buffer), (file_to_buffer): Added. These
+ functions are commonly used. Misc cleanups.
+
+ * cpu.c: (glibtop_get_cpu_s):
+ * loadavg.c: (glibtop_get_loadavg_s):
+ * mem.c: (glibtop_get_mem_s):
+ * open.c: (glibtop_open_s):
+ * swap.c: (glibtop_get_swap_s):
+ * sysinfo.c: (init_sysinfo):
+ * uptime.c: (glibtop_get_uptime_s): Replaced open/read/close by file_to_buffer().
+
2004-06-18 Benoît Dejean <tazforever@dlfp.org>
* loadavg.c: (glibtop_get_loadavg_s): Whitespace cleanup.
diff --git a/sysdeps/linux/cpu.c b/sysdeps/linux/cpu.c
index 87f923c5..e6fedfde 100644
--- a/sysdeps/linux/cpu.c
+++ b/sysdeps/linux/cpu.c
@@ -54,24 +54,13 @@ void
glibtop_get_cpu_s (glibtop *server, glibtop_cpu *buf)
{
char buffer [BUFSIZ], *p;
- int fd, len;
int i;
glibtop_init_s (&server, GLIBTOP_SYSDEPS_CPU, 0);
memset (buf, 0, sizeof (glibtop_cpu));
- 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';
+ file_to_buffer(server, buffer, FILENAME);
/*
* GLOBAL
diff --git a/sysdeps/linux/glibtop_server.c b/sysdeps/linux/glibtop_server.c
index 8260a5c7..7d1bd232 100644
--- a/sysdeps/linux/glibtop_server.c
+++ b/sysdeps/linux/glibtop_server.c
@@ -1,9 +1,11 @@
#include <glibtop.h>
+#include <glibtop/error.h>
#include <glib.h>
#include <string.h>
#include <stdlib.h>
+#include <stdarg.h>
#include <fcntl.h>
@@ -31,28 +33,58 @@ get_scaled(const char *buffer, const char *key)
}
-int
-proc_file_to_buffer (char *buffer, const char *fmt, pid_t pid)
+/*
+ * Read functions
+ */
+enum TRY_FILE_TO_BUFFER
{
- char filename [256];
+ TRY_FILE_TO_BUFFER_OK = 0,
+ TRY_FILE_TO_BUFFER_OPEN = -1,
+ TRY_FILE_TO_BUFFER_READ = -2
+};
+
+int try_file_to_buffer(char *buffer, const char *format, ...)
+{
+ char path[4096];
int fd;
ssize_t len;
+ va_list pa;
+
+ va_start(pa, format);
- g_snprintf (filename, sizeof filename, fmt, pid);
+ /* C99 also provides vsnprintf */
+ g_vsnprintf(path, sizeof path, format, pa);
- fd = open (filename, O_RDONLY);
- if (fd < 0) return -1;
+ va_end(pa);
+
+ if((fd = open (path, O_RDONLY)) < 0)
+ return TRY_FILE_TO_BUFFER_OPEN;
len = read (fd, buffer, BUFSIZ-1);
close (fd);
- if (len < 0) return -1;
+ if (len < 0)
+ return TRY_FILE_TO_BUFFER_READ;
buffer [len] = '\0';
- return 0;
+ return TRY_FILE_TO_BUFFER_OK;
}
+
+void
+file_to_buffer(glibtop *server, char *buffer, const char *filename)
+{
+ switch(try_file_to_buffer(buffer, filename))
+ {
+ case TRY_FILE_TO_BUFFER_OPEN:
+ glibtop_error_io_r (server, "open (%s)", filename);
+ case TRY_FILE_TO_BUFFER_READ:
+ glibtop_error_io_r (server, "read (%s)", filename);
+ }
+}
+
+
#warning "Ignore the following warning"
unsigned get_pageshift()
{
diff --git a/sysdeps/linux/glibtop_server.h b/sysdeps/linux/glibtop_server.h
index a522cefb..ab1d32aa 100644
--- a/sysdeps/linux/glibtop_server.h
+++ b/sysdeps/linux/glibtop_server.h
@@ -21,8 +21,10 @@
Boston, MA 02111-1307, USA.
*/
-#ifndef __GLIBTOP_SERVER_H__
-#define __GLIBTOP_SERVER_H__
+#ifndef __LINUX__GLIBTOP_SERVER_H__
+#define __LINUX__GLIBTOP_SERVER_H__
+
+#include <glibtop.h>
#include <glib.h>
@@ -36,8 +38,10 @@ G_BEGIN_DECLS
#define LINUX_VERSION(x,y,z) (0x10000*(x) + 0x100*(y) + z)
+
unsigned get_pageshift();
+
static inline char *
skip_token (const char *p)
{
@@ -55,6 +59,7 @@ skip_multiple_token (const char *p, size_t count)
return (char *)p;
}
+
static inline char *
skip_line (const char *p)
{
@@ -62,12 +67,29 @@ skip_line (const char *p)
return (char *) (*p ? p+1 : p);
}
+
unsigned long long
get_scaled(const char *buffer, const char *key);
+/* aborts on error */
+void
+file_to_buffer(glibtop *server, char *buffer, const char *filename);
+
+/* return < 0 on error, otherwise 0 on success */
int
-proc_file_to_buffer (char *buffer, const char *fmt, pid_t pid);
+try_file_to_buffer(char *buffer, const char *format, ...);
+
+
+/* some inline functions that wrap proc path
+ * as fast as macros :)
+ */
+
+static inline int
+proc_file_to_buffer (char *buffer, const char *fmt, pid_t pid)
+{
+ return try_file_to_buffer(buffer, fmt, pid);
+}
static inline int
proc_stat_to_buffer (char *buffer, pid_t pid)
@@ -87,6 +109,7 @@ 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)
{
@@ -121,4 +144,4 @@ proc_stat_after_cmd (char *p)
G_END_DECLS
-#endif
+#endif /* __LINUX__GLIBTOP_SERVER_H__ */
diff --git a/sysdeps/linux/loadavg.c b/sysdeps/linux/loadavg.c
index ea639b6e..61b5006d 100644
--- a/sysdeps/linux/loadavg.c
+++ b/sysdeps/linux/loadavg.c
@@ -49,23 +49,12 @@ void
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_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';
+ file_to_buffer(server, buffer, FILENAME);
buf->loadavg [0] = strtod (buffer, &p);
buf->loadavg [1] = strtod (p, &p);
diff --git a/sysdeps/linux/mem.c b/sysdeps/linux/mem.c
index 330fdfac..175dbcdb 100644
--- a/sysdeps/linux/mem.c
+++ b/sysdeps/linux/mem.c
@@ -47,23 +47,10 @@ void
glibtop_get_mem_s (glibtop *server, glibtop_mem *buf)
{
char buffer [BUFSIZ];
- 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_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';
+ file_to_buffer(server, buffer, FILENAME);
buf->total = get_scaled(buffer, "MemTotal:");
buf->free = get_scaled(buffer, "MemFree:");
@@ -72,6 +59,6 @@ glibtop_get_mem_s (glibtop *server, glibtop_mem *buf)
buf->buffer = get_scaled(buffer, "Buffers:");
buf->cached = get_scaled(buffer, "Cached:");
- buf->user = buf->total - buf->free - buf->cached - buf->buffer;
- buf->flags = _glibtop_sysdeps_mem;
+ buf->user = buf->total - buf->free - buf->cached - buf->buffer;
+ buf->flags = _glibtop_sysdeps_mem;
}
diff --git a/sysdeps/linux/open.c b/sysdeps/linux/open.c
index b9c594d0..b6d61f38 100644
--- a/sysdeps/linux/open.c
+++ b/sysdeps/linux/open.c
@@ -66,23 +66,12 @@ glibtop_open_s (glibtop *server, const char *program_name,
const unsigned flags)
{
char buffer [BUFSIZ], *p = buffer;
- int fd, len;
server->name = program_name;
server->os_version_code = get_linux_version();
- 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';
+ file_to_buffer(server, buffer, FILENAME);
for (server->ncpu = 0; server->ncpu < GLIBTOP_NCPU; server->ncpu++) {
diff --git a/sysdeps/linux/swap.c b/sysdeps/linux/swap.c
index c9a42e5e..5299c65a 100644
--- a/sysdeps/linux/swap.c
+++ b/sysdeps/linux/swap.c
@@ -52,23 +52,12 @@ void
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_error_io_r (server, "open (%s)", MEMINFO);
-
- len = read (fd, buffer, BUFSIZ-1);
- if (len < 0)
- glibtop_error_io_r (server, "read (%s)", MEMINFO);
-
- close (fd);
-
- buffer [len] = '\0';
+ file_to_buffer(server, buffer, MEMINFO);
/* Kernel 2.6 with multiple lines */
@@ -78,17 +67,7 @@ glibtop_get_swap_s (glibtop *server, glibtop_swap *buf)
buf->flags = _glibtop_sysdeps_swap;
- fd = open (PROC_STAT, O_RDONLY);
- if (fd < 0)
- glibtop_error_io_r (server, "open (%s)", PROC_STAT);
-
- len = read (fd, buffer, BUFSIZ-1);
- if (len < 0)
- glibtop_error_io_r (server, "read (%s)", PROC_STAT);
-
- close (fd);
-
- buffer [len] = '\0';
+ file_to_buffer(server, buffer, PROC_STAT);
p = strstr (buffer, "\nswap");
if (p == NULL) return;
diff --git a/sysdeps/linux/sysinfo.c b/sysdeps/linux/sysinfo.c
index 667a1b51..fbbed5ff 100644
--- a/sysdeps/linux/sysinfo.c
+++ b/sysdeps/linux/sysinfo.c
@@ -36,8 +36,6 @@ static glibtop_sysinfo sysinfo = { .flags = 0 };
static void
init_sysinfo (glibtop *server)
{
- int fd;
- ssize_t len;
char buffer [BUFSIZ];
if(G_LIKELY(sysinfo.flags)) return;
@@ -46,21 +44,7 @@ init_sysinfo (glibtop *server)
memset (&sysinfo, 0, sizeof (glibtop_sysinfo));
-
- /* load the file */
-
- 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';
-
+ file_to_buffer(server, buffer, FILENAME);
/* cpuinfo records are seperated by a blank line */
gchar ** const processors = g_strsplit(buffer, "\n\n", 0);
@@ -97,8 +81,7 @@ init_sysinfo (glibtop *server)
/* the last key has no value and has not been added */
- if(*p)
- g_free(*p);
+ if(*p) g_free(*p);
/* just g_free instead of g_strvfree because we stole
the memory*/
diff --git a/sysdeps/linux/uptime.c b/sysdeps/linux/uptime.c
index 74d3d7f3..8e73c478 100644
--- a/sysdeps/linux/uptime.c
+++ b/sysdeps/linux/uptime.c
@@ -45,23 +45,12 @@ void
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_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';
+ file_to_buffer(server, buffer, FILENAME);
buf->uptime = strtod (buffer, &p);
buf->idletime = strtod (p, &p);