From 67798e0d7d631f030db33147ef06d986bba1a5bb Mon Sep 17 00:00:00 2001 From: Martin Baulig Date: Mon, 1 Jun 1998 15:09:03 +0000 Subject: [PATCH] removed #if's - we now decide at runtime whether to use the server or to * lib/*.c: removed #if's - we now decide at runtime whether to use the server or to call the appropriate function for the sysdeps directory. --- lib/command.c | 2 +- lib/cpu.c | 15 ++++----- lib/init.c | 6 ++-- lib/loadavg.c | 15 ++++----- lib/mem.c | 15 ++++----- lib/msg_limits.c | 15 ++++----- lib/open.c | 78 +++++++++++++++++++++++++++++++++++++++++++++-- lib/prockernel.c | 17 ++++++----- lib/proclist.c | 15 ++++----- lib/procmem.c | 17 ++++++----- lib/procsegment.c | 17 ++++++----- lib/procsignal.c | 17 ++++++----- lib/procstate.c | 15 ++++----- lib/proctime.c | 17 ++++++----- lib/procuid.c | 15 ++++----- lib/read.c | 2 +- lib/read_data.c | 4 +-- lib/sem_limits.c | 15 ++++----- lib/shm_limits.c | 15 ++++----- lib/swap.c | 15 ++++----- lib/uptime.c | 15 ++++----- lib/write.c | 2 +- 22 files changed, 217 insertions(+), 127 deletions(-) diff --git a/lib/command.c b/lib/command.c index eb74183b..1bc67b05 100644 --- a/lib/command.c +++ b/lib/command.c @@ -33,7 +33,7 @@ glibtop_call__l (glibtop *server, unsigned command, size_t send_size, void *send glibtop_command *cmnd; void *ptr; - glibtop_init__r (&server); + glibtop_init__r (&server, 0, 0); cmnd = glibtop_calloc__r (server, 1, sizeof (glibtop_command)); diff --git a/lib/cpu.c b/lib/cpu.c index 04d3bb9d..94ffcdab 100644 --- a/lib/cpu.c +++ b/lib/cpu.c @@ -23,16 +23,17 @@ #include #include -#if GLIBTOP_SUID_CPU - /* Provides information about cpu usage. */ void glibtop_get_cpu__l (glibtop *server, glibtop_cpu *buf) { - glibtop_init__r (&server); - glibtop_call__l (server, GLIBTOP_CMND_CPU, 0, NULL, - sizeof (glibtop_cpu), buf); -} + glibtop_init__r (&server, GLIBTOP_SYSDEPS_CPU, 0); -#endif + if (server->features & GLIBTOP_SYSDEPS_CPU) { + glibtop_call__l (server, GLIBTOP_CMND_CPU, 0, NULL, + sizeof (glibtop_cpu), buf); + } else { + glibtop_get_cpu__r (server, buf); + } +} diff --git a/lib/init.c b/lib/init.c index f6a9a506..a3cb49ef 100644 --- a/lib/init.c +++ b/lib/init.c @@ -26,14 +26,16 @@ static glibtop _glibtop_global_server; glibtop *glibtop_global_server = NULL; glibtop * -glibtop_init__r (glibtop **server) +glibtop_init__r (glibtop **server, const unsigned long features, + const unsigned flags) { if (*server != NULL) return *server; if (glibtop_global_server == NULL) { glibtop_global_server = &_glibtop_global_server; - glibtop_open (glibtop_global_server, "glibtop"); + glibtop_open (glibtop_global_server, "glibtop", + features, flags); } return *server = glibtop_global_server; diff --git a/lib/loadavg.c b/lib/loadavg.c index 7d571218..0704f1bc 100644 --- a/lib/loadavg.c +++ b/lib/loadavg.c @@ -23,16 +23,17 @@ #include #include -#if GLIBTOP_SUID_LOADAVG - /* Provides load averange. */ void glibtop_get_loadavg__l (glibtop *server, glibtop_loadavg *buf) { - glibtop_init__r (&server); - glibtop_call__l (server, GLIBTOP_CMND_LOADAVG, 0, NULL, - sizeof (glibtop_loadavg), buf); -} + glibtop_init__r (&server, GLIBTOP_SYSDEPS_LOADAVG, 0); -#endif + if (server->features & GLIBTOP_SYSDEPS_LOADAVG) { + glibtop_call__l (server, GLIBTOP_CMND_LOADAVG, 0, NULL, + sizeof (glibtop_loadavg), buf); + } else { + glibtop_get_loadavg__r (server, buf); + } +} diff --git a/lib/mem.c b/lib/mem.c index a5dd8a65..ea8f2c6b 100644 --- a/lib/mem.c +++ b/lib/mem.c @@ -22,16 +22,17 @@ #include #include -#if GLIBTOP_SUID_MEM - /* Provides information about memory usage. */ void glibtop_get_mem__l (glibtop *server, glibtop_mem *buf) { - glibtop_init__r (&server); - glibtop_call__l (server, GLIBTOP_CMND_MEM, 0, NULL, - sizeof (glibtop_mem), buf); -} + glibtop_init__r (&server, GLIBTOP_SYSDEPS_MEM, 0); -#endif + if (server->features & GLIBTOP_SYSDEPS_MEM) { + glibtop_call__l (server, GLIBTOP_CMND_MEM, 0, NULL, + sizeof (glibtop_mem), buf); + } else { + glibtop_get_mem__r (server, buf); + } +} diff --git a/lib/msg_limits.c b/lib/msg_limits.c index 176a8a4f..9fc82c12 100644 --- a/lib/msg_limits.c +++ b/lib/msg_limits.c @@ -22,16 +22,17 @@ #include #include -#if GLIBTOP_SUID_MSG_LIMITS - /* Provides information about sysv ipc limits. */ void glibtop_get_msg_limits__l (glibtop *server, glibtop_msg_limits *buf) { - glibtop_init__r (&server); - glibtop_call__l (server, GLIBTOP_CMND_MSG_LIMITS, 0, NULL, - sizeof (glibtop_msg_limits), buf); -} + glibtop_init__r (&server, GLIBTOP_SYSDEPS_MSG_LIMITS, 0); -#endif + if (server->features & GLIBTOP_SYSDEPS_MSG_LIMITS) { + glibtop_call__l (server, GLIBTOP_CMND_MSG_LIMITS, 0, NULL, + sizeof (glibtop_msg_limits), buf); + } else { + glibtop_get_msg_limits__r (server, buf); + } +} diff --git a/lib/open.c b/lib/open.c index ed55b431..7dbe3131 100644 --- a/lib/open.c +++ b/lib/open.c @@ -19,19 +19,80 @@ write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +#include #include +#include /* Opens pipe to gtop server. Returns 0 on success and -1 on error. */ void -glibtop_open (glibtop *server, const char *program_name) +glibtop_open (glibtop *server, const char *program_name, + const unsigned long features, const unsigned flags) { char version [BUFSIZ], buffer [BUFSIZ]; + char *server_command, *server_rsh, *temp; + char *server_host, *server_user; memset (server, 0, sizeof (glibtop)); server->name = program_name; + /* Try to get data from environment. */ + + temp = getenv ("LIBGTOP_SERVER") ? + getenv ("LIBGTOP_SERVER") : GTOP_SERVER; + + server_command = glibtop_malloc__r (server, strlen (temp) + 1); + + strcpy (server_command, temp); + + temp = getenv ("LIBGTOP_RSH") ? + getenv ("LIBGTOP_RSH") : "rsh"; + + server_rsh = glibtop_malloc__r (server, strlen (temp) + 1); + + strcpy (server_rsh, temp); + + /* Extract host and user information. */ + + temp = strstr (server_command, ":"); + + if (temp) { + *temp = 0; + server_host = server_command; + server_command = temp+1; + + temp = strstr (server_host, "@"); + + if (temp) { + *temp = 0; + server_user = server_host; + server_host = temp+1; + } else { + server_user = NULL; + } + + } else { + server_host = NULL; + server_user = NULL; + } + + /* Store everything in `server'. */ + + server->server_command = server_command; + server->server_host = server_host; + server->server_user = server_user; + server->server_rsh = server_rsh; + + /* Get server features. */ + + if (server->server_host) + glibtop_error__r (server, _("Remote server not yet supported by library\n")); + + server->features = glibtop_server_features; + + /* Fork and exec server. */ + if (pipe (server->input) || pipe (server->output)) glibtop_error__r (server, _("cannot make a pipe: %s\n"), strerror (errno)); @@ -44,7 +105,20 @@ glibtop_open (glibtop *server, const char *program_name) close (server->input [0]); close (server->output [1]); dup2 (server->input [1], 1); /* dup2 (server->input [1], 2); */ dup2 (server->output [0], 0); - execl (GTOP_SERVER, NULL); + + if (server_host) { + if (server_user) { + execl (server->server_rsh, "gtop_server", "-l", + server->server_user, server->server_host, + server->server_command, NULL); + } else { + execl (server->server_rsh, "gtop_server", + server->server_host, server_command, NULL); + } + } else { + execl (server->server_command, "gtop_server", NULL); + } + _exit (2); } diff --git a/lib/prockernel.c b/lib/prockernel.c index d93add72..0c66d230 100644 --- a/lib/prockernel.c +++ b/lib/prockernel.c @@ -23,17 +23,18 @@ #include #include -#if GLIBTOP_SUID_PROC_KERNEL - /* Provides detailed information about a process. */ void glibtop_get_proc_kernel__l (glibtop *server, glibtop_proc_kernel *buf, - pid_t pid) + pid_t pid) { - glibtop_init__r (&server); - glibtop_call__l (server, GLIBTOP_CMND_PROC_KERNEL, sizeof (pid_t), - &pid, sizeof (glibtop_proc_kernel), buf); -} + glibtop_init__r (&server, GLIBTOP_SYSDEPS_PROC_KERNEL, 0); -#endif + if (server->features & GLIBTOP_SYSDEPS_PROC_KERNEL) { + glibtop_call__l (server, GLIBTOP_CMND_PROC_KERNEL, sizeof (pid_t), + &pid, sizeof (glibtop_proc_kernel), buf); + } else { + glibtop_get_proc_kernel__r (server, buf, pid); + } +} diff --git a/lib/proclist.c b/lib/proclist.c index 3c541129..0a40f825 100644 --- a/lib/proclist.c +++ b/lib/proclist.c @@ -22,16 +22,17 @@ #include #include -#if GLIBTOP_SUID_PROCLIST - /* Fetch list of currently running processes. */ unsigned * glibtop_get_proclist__l (glibtop *server, glibtop_proclist *buf) { - glibtop_init__r (&server); - return glibtop_call__l (server, GLIBTOP_CMND_PROCLIST, 0, NULL, - sizeof (glibtop_proclist), buf); -} + glibtop_init__r (&server, GLIBTOP_SYSDEPS_PROCLIST, 0); -#endif + if (server->features & GLIBTOP_SYSDEPS_PROCLIST) { + return glibtop_call__l (server, GLIBTOP_CMND_PROCLIST, 0, NULL, + sizeof (glibtop_proclist), buf); + } else { + return glibtop_get_proclist__r (server, buf); + } +} diff --git a/lib/procmem.c b/lib/procmem.c index 7dd06ba9..575c59a9 100644 --- a/lib/procmem.c +++ b/lib/procmem.c @@ -23,17 +23,18 @@ #include #include -#if GLIBTOP_SUID_PROC_MEM - /* Provides detailed information about a process. */ void glibtop_get_proc_mem__l (glibtop *server, glibtop_proc_mem *buf, - pid_t pid) + pid_t pid) { - glibtop_init__r (&server); - glibtop_call__l (server, GLIBTOP_CMND_PROC_MEM, sizeof (pid_t), - &pid, sizeof (glibtop_proc_mem), buf); -} + glibtop_init__r (&server, GLIBTOP_SYSDEPS_PROC_MEM, 0); -#endif + if (server->features & GLIBTOP_SYSDEPS_PROC_MEM) { + glibtop_call__l (server, GLIBTOP_CMND_PROC_MEM, sizeof (pid_t), + &pid, sizeof (glibtop_proc_mem), buf); + } else { + glibtop_get_proc_mem__r (server, buf, pid); + } +} diff --git a/lib/procsegment.c b/lib/procsegment.c index f144094a..a485dc30 100644 --- a/lib/procsegment.c +++ b/lib/procsegment.c @@ -23,17 +23,18 @@ #include #include -#if GLIBTOP_SUID_PROC_SEGMENT - /* Provides detailed information about a process. */ void glibtop_get_proc_segment__l (glibtop *server, glibtop_proc_segment *buf, - pid_t pid) + pid_t pid) { - glibtop_init__r (&server); - glibtop_call__l (server, GLIBTOP_CMND_PROC_SEGMENT, sizeof (pid_t), - &pid, sizeof (glibtop_proc_segment), buf); -} + glibtop_init__r (&server, GLIBTOP_SYSDEPS_PROC_SEGMENT, 0); -#endif + if (server->features & GLIBTOP_SYSDEPS_PROC_SEGMENT) { + glibtop_call__l (server, GLIBTOP_CMND_PROC_SEGMENT, sizeof (pid_t), + &pid, sizeof (glibtop_proc_segment), buf); + } else { + glibtop_get_proc_segment__r (server, buf, pid); + } +} diff --git a/lib/procsignal.c b/lib/procsignal.c index d1ed2dd8..89c23ed3 100644 --- a/lib/procsignal.c +++ b/lib/procsignal.c @@ -23,17 +23,18 @@ #include #include -#if GLIBTOP_SUID_PROC_SIGNAL - /* Provides detailed information about a process. */ void glibtop_get_proc_signal__l (glibtop *server, glibtop_proc_signal *buf, - pid_t pid) + pid_t pid) { - glibtop_init__r (&server); - glibtop_call__l (server, GLIBTOP_CMND_PROC_SIGNAL, sizeof (pid_t), - &pid, sizeof (glibtop_proc_signal), buf); -} + glibtop_init__r (&server, GLIBTOP_SYSDEPS_PROC_SIGNAL, 0); -#endif + if (server->features & GLIBTOP_SYSDEPS_PROC_SIGNAL) { + glibtop_call__l (server, GLIBTOP_CMND_PROC_SIGNAL, sizeof (pid_t), + &pid, sizeof (glibtop_proc_signal), buf); + } else { + glibtop_get_proc_signal__r (server, buf, pid); + } +} diff --git a/lib/procstate.c b/lib/procstate.c index 3c986c3e..a5625ac4 100644 --- a/lib/procstate.c +++ b/lib/procstate.c @@ -23,17 +23,18 @@ #include #include -#if GLIBTOP_SUID_PROC_STATE - /* Provides detailed information about a process. */ void glibtop_get_proc_state__l (glibtop *server, glibtop_proc_state *buf, pid_t pid) { - glibtop_init__r (&server); - glibtop_call__l (server, GLIBTOP_CMND_PROC_STATE, sizeof (pid_t), - &pid, sizeof (glibtop_proc_state), buf); -} + glibtop_init__r (&server, GLIBTOP_SYSDEPS_PROC_STATE, 0); -#endif + if (server->features & GLIBTOP_SYSDEPS_PROC_STATE) { + glibtop_call__l (server, GLIBTOP_CMND_PROC_STATE, sizeof (pid_t), + &pid, sizeof (glibtop_proc_state), buf); + } else { + glibtop_get_proc_state__r (server, buf, pid); + } +} diff --git a/lib/proctime.c b/lib/proctime.c index 41413cbb..40c0e62c 100644 --- a/lib/proctime.c +++ b/lib/proctime.c @@ -23,17 +23,18 @@ #include #include -#if GLIBTOP_SUID_PROC_TIME - /* Provides detailed information about a process. */ void glibtop_get_proc_time__l (glibtop *server, glibtop_proc_time *buf, - pid_t pid) + pid_t pid) { - glibtop_init__r (&server); - glibtop_call__l (server, GLIBTOP_CMND_PROC_TIME, sizeof (pid_t), - &pid, sizeof (glibtop_proc_time), buf); -} + glibtop_init__r (&server, GLIBTOP_SYSDEPS_PROC_TIME, 0); -#endif + if (server->features & GLIBTOP_SYSDEPS_PROC_TIME) { + glibtop_call__l (server, GLIBTOP_CMND_PROC_TIME, sizeof (pid_t), + &pid, sizeof (glibtop_proc_time), buf); + } else { + glibtop_get_proc_time__r (server, buf, pid); + } +} diff --git a/lib/procuid.c b/lib/procuid.c index 93e6ada3..563242a8 100644 --- a/lib/procuid.c +++ b/lib/procuid.c @@ -23,17 +23,18 @@ #include #include -#if GLIBTOP_SUID_PROC_UID - /* Provides detailed information about a process. */ void glibtop_get_proc_uid__l (glibtop *server, glibtop_proc_uid *buf, pid_t pid) { - glibtop_init__r (&server); - glibtop_call__l (server, GLIBTOP_CMND_PROC_UID, sizeof (pid_t), - &pid, sizeof (glibtop_proc_uid), buf); -} + glibtop_init__r (&server, GLIBTOP_SYSDEPS_PROC_UID, 0); -#endif + if (server->features & GLIBTOP_SYSDEPS_PROC_UID) { + glibtop_call__l (server, GLIBTOP_CMND_PROC_UID, sizeof (pid_t), + &pid, sizeof (glibtop_proc_uid), buf); + } else { + glibtop_get_proc_uid__r (server, buf, pid); + } +} diff --git a/lib/read.c b/lib/read.c index c8ff2eec..d50b443e 100644 --- a/lib/read.c +++ b/lib/read.c @@ -28,7 +28,7 @@ glibtop_read__l (glibtop *server, size_t size, void *buf) { size_t ssize; - glibtop_init__r (&server); + glibtop_init__r (&server, 0, 0); if (read (server->input [0], &ssize, sizeof (size_t)) < 0) glibtop_error__r (server, _("read size: %s"), strerror (errno)); diff --git a/lib/read_data.c b/lib/read_data.c index acfe0add..20e0b0e0 100644 --- a/lib/read_data.c +++ b/lib/read_data.c @@ -30,7 +30,7 @@ glibtop_read_data__l (glibtop *server) size_t size; void *ptr; - glibtop_init__r (&server); + glibtop_init__r (&server, 0, 0); if (read (server->input [0], &size, sizeof (size_t)) < 0) glibtop_error__r (server, _("read data size: %s"), @@ -38,8 +38,6 @@ glibtop_read_data__l (glibtop *server) if (!size) return NULL; - fprintf (stderr, "Server has %d bytes of data.\n", size); - ptr = glibtop_malloc__r (server, size); if (read (server->input [0], ptr, size) < 0) diff --git a/lib/sem_limits.c b/lib/sem_limits.c index 2ce853a3..193b6f19 100644 --- a/lib/sem_limits.c +++ b/lib/sem_limits.c @@ -22,16 +22,17 @@ #include #include -#if GLIBTOP_SUID_SEM_LIMITS - /* Provides information about sysv ipc limits. */ void glibtop_get_sem_limits__l (glibtop *server, glibtop_sem_limits *buf) { - glibtop_init__r (&server); - glibtop_call__l (server, GLIBTOP_CMND_SEM_LIMITS, 0, NULL, - sizeof (glibtop_sem_limits), buf); -} + glibtop_init__r (&server, GLIBTOP_SYSDEPS_SEM_LIMITS, 0); -#endif + if (server->features & GLIBTOP_SYSDEPS_SEM_LIMITS) { + glibtop_call__l (server, GLIBTOP_CMND_SEM_LIMITS, 0, NULL, + sizeof (glibtop_sem_limits), buf); + } else { + glibtop_get_sem_limits__r (server, buf); + } +} diff --git a/lib/shm_limits.c b/lib/shm_limits.c index 9d3aee22..8b226400 100644 --- a/lib/shm_limits.c +++ b/lib/shm_limits.c @@ -22,16 +22,17 @@ #include #include -#if GLIBTOP_SUID_SHM_LIMITS - /* Provides information about sysv ipc limits. */ void glibtop_get_shm_limits__l (glibtop *server, glibtop_shm_limits *buf) { - glibtop_init__r (&server); - glibtop_call__l (server, GLIBTOP_CMND_SHM_LIMITS, 0, NULL, - sizeof (glibtop_shm_limits), buf); -} + glibtop_init__r (&server, GLIBTOP_SYSDEPS_SHM_LIMITS, 0); -#endif + if (server->features & GLIBTOP_SYSDEPS_SHM_LIMITS) { + glibtop_call__l (server, GLIBTOP_CMND_SHM_LIMITS, 0, NULL, + sizeof (glibtop_shm_limits), buf); + } else { + glibtop_get_shm_limits__r (server, buf); + } +} diff --git a/lib/swap.c b/lib/swap.c index e4972869..f6d7aeae 100644 --- a/lib/swap.c +++ b/lib/swap.c @@ -22,16 +22,17 @@ #include #include -#if GLIBTOP_SUID_SWAP - /* Provides information about swap usage. */ void glibtop_get_swap__l (glibtop *server, glibtop_swap *buf) { - glibtop_init__r (&server); - glibtop_call__l (server, GLIBTOP_CMND_SWAP, 0, NULL, - sizeof (glibtop_swap), buf); -} + glibtop_init__r (&server, GLIBTOP_SYSDEPS_SWAP, 0); -#endif + if (server->features & GLIBTOP_SYSDEPS_SWAP) { + glibtop_call__l (server, GLIBTOP_CMND_SWAP, 0, NULL, + sizeof (glibtop_swap), buf); + } else { + glibtop_get_swap__r (server, buf); + } +} diff --git a/lib/uptime.c b/lib/uptime.c index 597a50b8..a6ce1195 100644 --- a/lib/uptime.c +++ b/lib/uptime.c @@ -23,16 +23,17 @@ #include #include -#if GLIBTOP_SUID_UPTIME - /* Provides uptime and idle time. */ void glibtop_get_uptime__l (glibtop *server, glibtop_uptime *buf) { - glibtop_init__r (&server); - glibtop_call__l (server, GLIBTOP_CMND_UPTIME, 0, NULL, - sizeof (glibtop_uptime), buf); -} + glibtop_init__r (&server, GLIBTOP_SYSDEPS_UPTIME, 0); -#endif + if (server->features & GLIBTOP_SYSDEPS_UPTIME) { + glibtop_call__l (server, GLIBTOP_CMND_UPTIME, 0, NULL, + sizeof (glibtop_uptime), buf); + } else { + glibtop_get_uptime__r (server, buf); + } +} diff --git a/lib/write.c b/lib/write.c index 2d3984d0..e7913ef0 100644 --- a/lib/write.c +++ b/lib/write.c @@ -26,7 +26,7 @@ void glibtop_write__l (glibtop *server, size_t size, void *buf) { - glibtop_init__r (&server); + glibtop_init__r (&server, 0, 0); if (write (server->output [1], &size, sizeof (size_t)) < 0) glibtop_error__r (server, _("write size: %s"), strerror (errno));