diff --git a/ChangeLog b/ChangeLog index 9307741c..596cd3ec 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,13 @@ +1998-06-18 Martin Baulig + + * lib/{command, write, read}.c: Changed client <-> server + interface to make less system calls. + + * src/daemon/main.c: Changed server side of interface. + + * include/glibtop/command.h (struct _glibtop_response): New + structure to return data from the server to the client. + 1998-06-14 Martin Baulig * glibtop.h (struct _glibtop): Added `socket' field. diff --git a/include/glibtop/command.h b/include/glibtop/command.h index 8314699f..259bdec0 100644 --- a/include/glibtop/command.h +++ b/include/glibtop/command.h @@ -50,13 +50,29 @@ __BEGIN_DECLS #define GLIBTOP_MAX_CMND 18 -typedef struct _glibtop_command glibtop_command; +#define _GLIBTOP_PARAM_SIZE 16 + +typedef struct _glibtop_command glibtop_command; +typedef struct _glibtop_response glibtop_response; struct _glibtop_command { - glibtop server; - unsigned command; - size_t size; + glibtop server; + unsigned command; + size_t size, data_size; + char parameter [_GLIBTOP_PARAM_SIZE]; +}; + +union _glibtop_response_union +{ + glibtop_union data; + glibtop_sysdeps sysdeps; +}; + +struct _glibtop_response +{ + size_t data_size; + union _glibtop_response_union u; }; #define glibtop_call(p1, p2, p3, p4) glibtop_call_r(glibtop_global_server, p1, p2, p3, p4) diff --git a/lib/Makefile.am b/lib/Makefile.am index 6f2a9116..c2835361 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -4,7 +4,7 @@ INCLUDES = -I$(top_builddir) -I$(top_srcdir) @machine_incs@ \ -I$(top_srcdir)/include -I$(top_srcdir)/intl @GUILE_INCS@ \ -DGTOPLOCALEDIR=\"$(datadir)/locale\" -D_GNU_SOURCE -CFLAGS = -Wall -W @CFLAGS@ -DGTOP_SERVER=\""@LIBGTOP_SERVER@"\" +CFLAGS = -Wall -W @CFLAGS@ -DGTOP_SERVER=\""@LIBGTOP_SERVER@"\" -DDEBUG lib_LTLIBRARIES = libgtop.la diff --git a/lib/command.c b/lib/command.c index e8d2dc97..b7f6ccd4 100644 --- a/lib/command.c +++ b/lib/command.c @@ -28,27 +28,43 @@ void * glibtop_call_l (glibtop *server, unsigned command, size_t send_size, void *send_buf, - size_t recv_size, void *recv_buf) + size_t recv_size, void *recv_buf) { - glibtop_command *cmnd; + glibtop_command cmnd; + glibtop_response response; void *ptr; glibtop_init_r (&server, 0, 0); - cmnd = glibtop_calloc_r (server, 1, sizeof (glibtop_command)); + memset (&cmnd, 0, sizeof (glibtop_command)); + + memcpy (&cmnd.server, server, sizeof (glibtop)); - memcpy (&cmnd->server, server, sizeof (glibtop)); + cmnd.command = command; + + /* If send_size is less than _GLIBTOP_PARAM_SIZE (normally 16 Bytes), we + * send it together with command, so we only need one system call instead + * of two. */ + +#ifdef DEBUG + fprintf (stderr, "COMMAND: send_size = %d; command = %d; sizeof (cmnd) = %d\n", + send_size, command, sizeof (glibtop_command)); +#endif + + + if (send_size <= _GLIBTOP_PARAM_SIZE) + memcpy (cmnd.parameter, send_buf, send_size); + else + cmnd.size = send_size; - cmnd->command = command; - cmnd->size = send_size; - - glibtop_write_l (server, sizeof (glibtop_command), cmnd); + glibtop_write_l (server, sizeof (glibtop_command), &cmnd); glibtop_write_l (server, send_size, send_buf); - glibtop_read_l (server, recv_size, recv_buf); + + glibtop_read_l (server, sizeof (glibtop_response), &response); + + /* glibtop_read_l (server, recv_size, recv_buf); */ - ptr = glibtop_read_data_l (server); + /* ptr = glibtop_read_data_l (server); */ - glibtop_free_r (server, cmnd); - - return ptr; + return NULL; } diff --git a/lib/read.c b/lib/read.c index 361bb5a4..55fb922e 100644 --- a/lib/read.c +++ b/lib/read.c @@ -26,27 +26,10 @@ void glibtop_read_l (glibtop *server, size_t size, void *buf) { - size_t ssize; int ret; glibtop_init_r (&server, 0, 0); -#ifdef DEBUG - fprintf (stderr, "LIBRARY: reading %d bytes, should be %d.\n", sizeof (size_t), size); -#endif - - if (server->socket) { - ret = recv (server->socket, &ssize, sizeof (size_t), 0); - } else { - ret = read (server->input [0], &ssize, sizeof (size_t)); - } - - if (ret < 0) - glibtop_error_io_r (server, _("read size")); - - if (size != ssize) - glibtop_error_r (server, _("got %d bytes but requested %d (ret = %d)"), ssize, size, ret); - #ifdef DEBUG fprintf (stderr, "LIBRARY: really reading %d bytes.\n", size); #endif diff --git a/lib/write.c b/lib/write.c index f91858ee..b786c0e7 100644 --- a/lib/write.c +++ b/lib/write.c @@ -30,20 +30,7 @@ glibtop_write_l (glibtop *server, size_t size, void *buf) glibtop_init_r (&server, 0, 0); -#ifdef DEBUG - fprintf (stderr, "LIBRARY: writing %d bytes = %d.\n", sizeof (size_t), size); -#endif - - if (server->socket) { - ret = send (server->socket, &size, sizeof (size_t), 0); - } else { - ret = write (server->output [1], &size, sizeof (size_t)); - } - - if (ret < 0) - glibtop_error_io_r (server, _("write size")); - - if (!size) return; + if (size == 0) return; #ifdef DEBUG fprintf (stderr, "LIBRARY: really writing %d bytes.\n", size); diff --git a/src/daemon/Makefile.am b/src/daemon/Makefile.am index 7ac13ca7..fd221379 100644 --- a/src/daemon/Makefile.am +++ b/src/daemon/Makefile.am @@ -4,7 +4,7 @@ INCLUDES = -I$(top_builddir) -I$(top_srcdir) @machine_incs@ \ -I$(top_srcdir)/include -I$(top_srcdir)/intl @GUILE_INCS@ \ -DGTOPLOCALEDIR=\"$(datadir)/locale\" -D_GNU_SOURCE -CFLAGS = -Wall -W @CFLAGS@ -DGTOP_SERVER=\""@LIBGTOP_SERVER@"\" +CFLAGS = -Wall -W @CFLAGS@ -DGTOP_SERVER=\""@LIBGTOP_SERVER@"\" -DDEBUG -DREAL_DEBUG bin_PROGRAMS = gnuserv diff --git a/src/daemon/main.c b/src/daemon/main.c index 050de8bf..f0696f0d 100644 --- a/src/daemon/main.c +++ b/src/daemon/main.c @@ -39,221 +39,177 @@ #endif static void -do_output (int s, size_t size, const void *buf) +do_output (int s, glibtop_response *response, size_t data_size, const void *data) { #ifdef REAL_DEBUG - fprintf (stderr, "Writing %d bytes = %d.\n", sizeof (size_t), size); + fprintf (stderr, "Really writing %d bytes.\n", sizeof (glibtop_response)); #endif - if (send (s, &size, sizeof (size_t), 0) < 0) - glibtop_warn_io ("send"); - - if (!size) - return; + response->data_size = data_size; + + if (send (s, response, sizeof (glibtop_response), 0) < 0) + glibtop_warn_io ("send"); + if (response->data_size) { #ifdef REAL_DEBUG - fprintf (stderr, "Really writing %d bytes.\n", size); + fprintf (stderr, "Writing %d bytes of data.\n", response->data_size); #endif - if (send (s, buf, size, 0) < 0) - glibtop_warn_io ("send"); + if (send (s, data, response->data_size, 0) , 0) + glibtop_warn_io ("send"); + } } void handle_socket_connection (int s) { - pid_t pid; - int nread; - size_t size, dsize; - char parameter [BUFSIZ]; - struct timeval tv; - glibtop_union data; - glibtop_command cmnd; - glibtop_sysdeps sysdeps; - glibtop server; - void *ptr; + pid_t pid; + int nread; + size_t size; + char parameter [BUFSIZ]; + struct timeval tv; + glibtop_response response; + glibtop_command cmnd; + glibtop server; + void *ptr; - tv.tv_sec = 5; - tv.tv_usec = 0; + tv.tv_sec = 5; + tv.tv_usec = 0; - while(1) { - nread = recv (s, &size, sizeof (size_t), 0); + while(1) { + nread = recv (s, &cmnd, sizeof (glibtop_command), 0); - if (nread < 0) { - glibtop_warn_io ("recv"); - return; - } + if (nread < 0) { + glibtop_warn_io ("recv"); + return; + } - if (nread == 0) - return; + if (nread == 0) + return; - if (nread != sizeof (size_t)) { - glibtop_warn ("Expected %d bytes but got %d", sizeof (size_t), nread); - return; - } - - if (size != sizeof (glibtop_command)) { - glibtop_warn ("Expected %d bytes but got %d", sizeof (size_t), nread); - return; - } - - nread = recv (s, &cmnd, size, 0); - - if (nread < 0) { - glibtop_warn_io ("recv"); - return; - } - - if (nread == 0) - return; + if (nread != sizeof (glibtop_command)) + glibtop_warn ("Received %d bytes, but expected %d\n", + nread, sizeof (glibtop_command)); #ifdef REAL_DEBUG - fprintf (stderr, "Received command %d from client.\n", cmnd.command); + fprintf (stderr, "Received command %d from client.\n", cmnd.command); #endif - nread = recv (s, &dsize, sizeof (size_t), 0); - - /* will return 0 if parent exits. */ - - if (nread < 0) { - glibtop_warn_io ("recv"); - return; - } - - if (nread == 0) - return; + if (cmnd.size >= BUFSIZ) { + glibtop_warn ("Client sent %d bytes, but buffer is %d", cmnd.size, BUFSIZ); + return; + } - if (dsize >= BUFSIZ) { - glibtop_warn ("Client sent %d bytes, but buffer is %d", dsize, BUFSIZ); - return; - } - - memset (parameter, 0, sizeof (parameter)); + memset (parameter, 0, sizeof (parameter)); - if (dsize) { + if (cmnd.size) { #ifdef REAL_DEBUG - fprintf (stderr, "Client has %d bytes of data.\n", dsize); + fprintf (stderr, "Client has %d bytes of data.\n", cmnd.size); #endif - nread = recv (s, parameter, dsize, 0); + nread = recv (s, parameter, cmnd.size, 0); - /* will return 0 if parent exits. */ + /* will return 0 if parent exits. */ - if (nread < 0) { - glibtop_warn_io ("recv"); - return; - } + if (nread < 0) { + glibtop_warn_io ("recv"); + return; + } - if (nread == 0) - return; + if (nread == 0) + return; - if (nread != (int) dsize) { - glibtop_warn ("Expected %d bytes but got %d", dsize, nread); - return; - } - } + if (nread != (int) cmnd.size) { + glibtop_warn ("Expected %d bytes but got %d", + cmnd.size, nread); + return; + } + } - switch (cmnd.command) { - case GLIBTOP_CMND_SYSDEPS: - sysdeps.features = GLIBTOP_SYSDEPS_ALL; - do_output (s, sizeof (glibtop_sysdeps), &sysdeps); - do_output (s, 0, NULL); - break; - case GLIBTOP_CMND_CPU: - glibtop_get_cpu_l (&server, &data.cpu); - do_output (s, sizeof (glibtop_cpu), &data.cpu); - do_output (s, 0, NULL); - break; - case GLIBTOP_CMND_MEM: - glibtop_get_mem_l (&server, &data.mem); - do_output (s, sizeof (glibtop_mem), &data.mem); - do_output (s, 0, NULL); - break; - case GLIBTOP_CMND_SWAP: - glibtop_get_swap_l (&server, &data.swap); - do_output (s, sizeof (glibtop_swap), &data.swap); - do_output (s, 0, NULL); - break; - case GLIBTOP_CMND_UPTIME: - glibtop_get_uptime_l (&server, &data.uptime); - do_output (s, sizeof (glibtop_uptime), &data.uptime); - do_output (s, 0, NULL); - break; - case GLIBTOP_CMND_LOADAVG: - glibtop_get_loadavg_l (&server, &data.loadavg); - do_output (s, sizeof (glibtop_loadavg), &data.loadavg); - do_output (s, 0, NULL); - break; - case GLIBTOP_CMND_SHM_LIMITS: - glibtop_get_shm_limits_l (&server, &data.shm_limits); - do_output (s, sizeof (glibtop_shm_limits), &data.shm_limits); - do_output (s, 0, NULL); - break; - case GLIBTOP_CMND_MSG_LIMITS: - glibtop_get_msg_limits_l (&server, &data.msg_limits); - do_output (s, sizeof (glibtop_msg_limits), &data.msg_limits); - do_output (s, 0, NULL); - break; - case GLIBTOP_CMND_SEM_LIMITS: - glibtop_get_sem_limits_l (&server, &data.sem_limits); - do_output (s, sizeof (glibtop_sem_limits), &data.sem_limits); - do_output (s, 0, NULL); - break; - case GLIBTOP_CMND_PROCLIST: - ptr = glibtop_get_proclist_l (&server, &data.proclist); - do_output (s, sizeof (glibtop_proclist), &data.proclist); - do_output (s, data.proclist.total, ptr); - glibtop_free_r (&server, ptr); - break; - case GLIBTOP_CMND_PROC_STATE: - memcpy (&pid, parameter, sizeof (pid_t)); - glibtop_get_proc_state_l - (&server, &data.proc_state, pid); - do_output (s, sizeof (glibtop_proc_state), &data.proc_state); - do_output (s, 0, NULL); - break; - case GLIBTOP_CMND_PROC_UID: - memcpy (&pid, parameter, sizeof (pid_t)); - glibtop_get_proc_uid_l - (&server, &data.proc_uid, pid); - do_output (s, sizeof (glibtop_proc_uid), &data.proc_uid); - do_output (s, 0, NULL); - break; - case GLIBTOP_CMND_PROC_MEM: - memcpy (&pid, parameter, sizeof (pid_t)); - glibtop_get_proc_mem_l - (&server, &data.proc_mem, pid); - do_output (s, sizeof (glibtop_proc_mem), &data.proc_mem); - do_output (s, 0, NULL); - break; - case GLIBTOP_CMND_PROC_TIME: - memcpy (&pid, parameter, sizeof (pid_t)); - glibtop_get_proc_time_l - (&server, &data.proc_time, pid); - do_output (s, sizeof (glibtop_proc_time), &data.proc_time); - do_output (s, 0, NULL); - break; - case GLIBTOP_CMND_PROC_SIGNAL: - memcpy (&pid, parameter, sizeof (pid_t)); - glibtop_get_proc_signal_l - (&server, &data.proc_signal, pid); - do_output (s, sizeof (glibtop_proc_signal), &data.proc_signal); - do_output (s, 0, NULL); - break; - case GLIBTOP_CMND_PROC_KERNEL: - memcpy (&pid, parameter, sizeof (pid_t)); - glibtop_get_proc_kernel_l - (&server, &data.proc_kernel, pid); - do_output (s, sizeof (glibtop_proc_kernel), &data.proc_kernel); - do_output (s, 0, NULL); - break; - case GLIBTOP_CMND_PROC_SEGMENT: - memcpy (&pid, parameter, sizeof (pid_t)); - glibtop_get_proc_segment_l - (&server, &data.proc_segment, pid); - do_output (s, sizeof (glibtop_proc_segment), &data.proc_segment); - do_output (s, 0, NULL); - break; - } - } + switch (cmnd.command) { + case GLIBTOP_CMND_SYSDEPS: + response.u.sysdeps.features = GLIBTOP_SYSDEPS_ALL; + do_output (s, &response, 0, NULL); + break; + case GLIBTOP_CMND_CPU: + glibtop_get_cpu_l (&server, &response.u.data.cpu); + do_output (s, &response, 0, NULL); + break; + case GLIBTOP_CMND_MEM: + glibtop_get_mem_l (&server, &response.u.data.mem); + do_output (s, &response, 0, NULL); + break; + case GLIBTOP_CMND_SWAP: + glibtop_get_swap_l (&server, &response.u.data.swap); + do_output (s, &response, 0, NULL); + break; + case GLIBTOP_CMND_UPTIME: + glibtop_get_uptime_l (&server, &response.u.data.uptime); + do_output (s, &response, 0, NULL); + break; + case GLIBTOP_CMND_LOADAVG: + glibtop_get_loadavg_l (&server, &response.u.data.loadavg); + do_output (s, &response, 0, NULL); + break; + case GLIBTOP_CMND_SHM_LIMITS: + glibtop_get_shm_limits_l (&server, &response.u.data.shm_limits); + do_output (s, &response, 0, NULL); + break; + case GLIBTOP_CMND_MSG_LIMITS: + glibtop_get_msg_limits_l (&server, &response.u.data.msg_limits); + do_output (s, &response, 0, NULL); + break; + case GLIBTOP_CMND_SEM_LIMITS: + glibtop_get_sem_limits_l (&server, &response.u.data.sem_limits); + do_output (s, &response, 0, NULL); + break; + case GLIBTOP_CMND_PROCLIST: + ptr = glibtop_get_proclist_l (&server, &response.u.data.proclist); + do_output (s, &response, response.u.data.proclist.total, ptr); + glibtop_free_r (&server, ptr); + break; + case GLIBTOP_CMND_PROC_STATE: + memcpy (&pid, parameter, sizeof (pid_t)); + glibtop_get_proc_state_l + (&server, &response.u.data.proc_state, pid); + do_output (s, &response, 0, NULL); + break; + case GLIBTOP_CMND_PROC_UID: + memcpy (&pid, parameter, sizeof (pid_t)); + glibtop_get_proc_uid_l + (&server, &response.u.data.proc_uid, pid); + do_output (s, &response, 0, NULL); + break; + case GLIBTOP_CMND_PROC_MEM: + memcpy (&pid, parameter, sizeof (pid_t)); + glibtop_get_proc_mem_l + (&server, &response.u.data.proc_mem, pid); + do_output (s, &response, 0, NULL); + break; + case GLIBTOP_CMND_PROC_TIME: + memcpy (&pid, parameter, sizeof (pid_t)); + glibtop_get_proc_time_l + (&server, &response.u.data.proc_time, pid); + do_output (s, &response, 0, NULL); + break; + case GLIBTOP_CMND_PROC_SIGNAL: + memcpy (&pid, parameter, sizeof (pid_t)); + glibtop_get_proc_signal_l + (&server, &response.u.data.proc_signal, pid); + do_output (s, &response, 0, NULL); + break; + case GLIBTOP_CMND_PROC_KERNEL: + memcpy (&pid, parameter, sizeof (pid_t)); + glibtop_get_proc_kernel_l + (&server, &response.u.data.proc_kernel, pid); + do_output (s, &response, 0, NULL); + break; + case GLIBTOP_CMND_PROC_SEGMENT: + memcpy (&pid, parameter, sizeof (pid_t)); + glibtop_get_proc_segment_l + (&server, &response.u.data.proc_segment, pid); + do_output (s, &response, 0, NULL); + break; + } + } }