diff options
author | Martin Baulig <martin@home-of-linux.org> | 1998-07-13 23:27:10 +0000 |
---|---|---|
committer | Martin Baulig <martin@src.gnome.org> | 1998-07-13 23:27:10 +0000 |
commit | 345a657d2c5bbfae11b848016f012c199a6b328d (patch) | |
tree | 09de1593d93eb206bc2afbfc757aae705343cc77 | |
parent | da7d7ff1163371de85e4735b94d1749fe12e0860 (diff) | |
download | libgtop-345a657d2c5bbfae11b848016f012c199a6b328d.tar.gz |
New file.
1998-07-14 Martin Baulig <martin@home-of-linux.org>
* src/daemon/slave.c: New file.
* src/daemon/*.c: Done some more work on the daemon.
* sysdeps/common/gnuslib.c: Removed IPC stuff.
* include/glibtop/gnuserv.h: Removed IPC stuff.
* include/glibtop/command.h (glibtop_response_unit): Added
typedef for `struct _glibtop_response_unit'.
* lib/Makefile.am: Using `$(top_srcdir)/features.def'
instead of `$(top_builddir)/features.def'.
* sysdeps/guile/Makefile.am: Using `$(top_srcdir)/features.def'
instead of `$(top_builddir)/features.def'.
* sysdeps/guile/names/Makefile.am: Dito.
* sysdeps/stub/*.c: changed suffix of all functions
from '_s' to '_r'; see also ChangeLog entry from Jun 6.
-rw-r--r-- | .cvsignore | 1 | ||||
-rw-r--r-- | ChangeLog | 24 | ||||
-rw-r--r-- | include/glibtop/command.h | 4 | ||||
-rw-r--r-- | include/glibtop/gnuserv.h | 44 | ||||
-rw-r--r-- | lib/Makefile.am | 4 | ||||
-rw-r--r-- | src/daemon/Makefile.am | 2 | ||||
-rw-r--r-- | src/daemon/gnuserv.c | 283 | ||||
-rw-r--r-- | src/daemon/main.c | 259 | ||||
-rw-r--r-- | src/daemon/slave.c | 155 | ||||
-rw-r--r-- | sysdeps/common/gnuslib.c | 87 |
10 files changed, 329 insertions, 534 deletions
@@ -24,3 +24,4 @@ stamp-h sun4 sun4sol2 support +i386-freebsd @@ -1,3 +1,27 @@ +1998-07-14 Martin Baulig <martin@home-of-linux.org> + + * src/daemon/slave.c: New file. + + * src/daemon/*.c: Done some more work on the daemon. + + * sysdeps/common/gnuslib.c: Removed IPC stuff. + + * include/glibtop/gnuserv.h: Removed IPC stuff. + + * include/glibtop/command.h (glibtop_response_unit): Added + typedef for `struct _glibtop_response_unit'. + + * lib/Makefile.am: Using `$(top_srcdir)/features.def' + instead of `$(top_builddir)/features.def'. + + * sysdeps/guile/Makefile.am: Using `$(top_srcdir)/features.def' + instead of `$(top_builddir)/features.def'. + + * sysdeps/guile/names/Makefile.am: Dito. + + * sysdeps/stub/*.c: changed suffix of all functions + from '_s' to '_r'; see also ChangeLog entry from Jun 6. + 1998-07-13 Martin Baulig <martin@home-of-linux.org> * glibtop.h: Fixed invocation of `glibtop_close_r'. diff --git a/include/glibtop/command.h b/include/glibtop/command.h index 1987e5d1..c76ed0ed 100644 --- a/include/glibtop/command.h +++ b/include/glibtop/command.h @@ -53,7 +53,9 @@ __BEGIN_DECLS #define _GLIBTOP_PARAM_SIZE 16 typedef struct _glibtop_command glibtop_command; + typedef struct _glibtop_response glibtop_response; +typedef union _glibtop_response_union glibtop_response_union; struct _glibtop_command { @@ -73,7 +75,7 @@ struct _glibtop_response { off_t offset; size_t size, data_size; - union _glibtop_response_union u; + glibtop_response_union u; }; #define glibtop_call(p1, p2, p3, p4) glibtop_call_r(glibtop_global_server, p1, p2, p3, p4) diff --git a/include/glibtop/gnuserv.h b/include/glibtop/gnuserv.h index 6d1df518..96099259 100644 --- a/include/glibtop/gnuserv.h +++ b/include/glibtop/gnuserv.h @@ -55,8 +55,7 @@ static char header_rcsid [] = "!Header: gnuserv.h,v 2.4 95/02/16 11:58:11 arup a */ #define INTERNET_DOMAIN_SOCKETS -#define UNIX_DOMAIN_SOCKETS -// #define SYSV_IPC +#define UNIX_DOMAIN_SOCKETS /* * Define additional authentication protocols to be used. These methods will @@ -90,15 +89,13 @@ static char header_rcsid [] = "!Header: gnuserv.h,v 2.4 95/02/16 11:58:11 arup a * Pick a default communication scheme, if none was specified. */ -#if !defined(SYSV_IPC) && !defined(UNIX_DOMAIN_SOCKETS) && !defined(INTERNET_DOMAIN_SOCKETS) +#if !defined(UNIX_DOMAIN_SOCKETS) && !defined(INTERNET_DOMAIN_SOCKETS) -#ifdef HAVE_SYSVIPC -#define SYSV_IPC /* SYSV systems use SYSV IPC by default */ -#endif /* HAVE_SYSVIPC */ +/* BSD systems use Unix Domain sockets by default */ #ifdef BSD -#define UNIX_DOMAIN_SOCKETS /* BSD systems use Unix Domain sockets by default */ -#endif /* BSD */ +#define UNIX_DOMAIN_SOCKETS +#endif #endif /* No communication method pre-defined */ @@ -110,24 +107,6 @@ static char header_rcsid [] = "!Header: gnuserv.h,v 2.4 95/02/16 11:58:11 arup a * what you want. */ -# define GSERV_BUFSZ BUFSIZ - - -#ifdef SYSV_IPC -#include <sys/ipc.h> -#include <sys/msg.h> - -#define send_string(s,str) \ - if (strlen(msgp->mtext) + strlen(str) < GSERV_BUFSZ) \ - strcat(msgp->mtext,str); \ - else \ - { \ - fprintf(stderr,"%s: not enough message buffer space\n",progname); \ - exit(1); \ - } \ - -#endif /* SYSV_IPC */ - #if defined(INTERNET_DOMAIN_SOCKETS) || defined(UNIX_DOMAIN_SOCKETS) #include <sys/socket.h> #endif /* INTERNET_DOMAIN_SOCKETS || UNIX_DOMAIN_SOCKETS */ @@ -154,9 +133,6 @@ static char header_rcsid [] = "!Header: gnuserv.h,v 2.4 95/02/16 11:58:11 arup a #undef TRUE #define TRUE 1 -// extern char *optarg; -// extern int optind; - /* The casts shut Sun's compiler up and are safe in the context these are actually used. */ #define max2(x,y) (((int) (x) > (int) (y)) ? (x) : (y)) @@ -177,16 +153,6 @@ static char header_rcsid [] = "!Header: gnuserv.h,v 2.4 95/02/16 11:58:11 arup a /* function declarations */ extern int glibtop_make_connection __P((const char *, int, int *)); -#ifdef SYSV_IPC -void disconnect_from_ipc_server(); -#endif - -#if defined(INTERNET_DOMAIN_SOCKETS) || defined(UNIX_DOMAIN_SOCKETS) -// void send_string (int s, const char *msg); -// void disconnect_from_server (int s, int echo); -// int read_line (int s, char *dest); -#endif - #ifdef INTERNET_DOMAIN_SOCKETS extern long glibtop_internet_addr __P((const char *)); #endif diff --git a/lib/Makefile.am b/lib/Makefile.am index 32a555a9..23e211e1 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -13,8 +13,8 @@ libgtop_la_SOURCES = init.c open.c close.c command.c read.c read_data.c \ BUILT_SOURCES = lib.c -lib.c: lib.awk $(top_builddir)/config.h $(top_builddir)/features.def - $(AWK) -f $(srcdir)/lib.awk < $(top_builddir)/features.def > lib-t +lib.c: lib.awk $(top_builddir)/config.h $(top_srcdir)/features.def + $(AWK) -f $(srcdir)/lib.awk < $(top_srcdir)/features.def > lib-t mv lib-t lib.c EXTRA_DIST = lib.awk diff --git a/src/daemon/Makefile.am b/src/daemon/Makefile.am index fd221379..ab15886e 100644 --- a/src/daemon/Makefile.am +++ b/src/daemon/Makefile.am @@ -10,7 +10,7 @@ bin_PROGRAMS = gnuserv EXTRA_PROGRAMS = gtop_daemon -gnuserv_SOURCES = gnuserv.c main.c +gnuserv_SOURCES = gnuserv.c main.c slave.c gnuserv_LDADD = $(top_builddir)/lib/libgtop.la \ $(top_builddir)/sysdeps/common/libgtop_common.la \ $(top_builddir)/sysdeps/@sysdeps_dir@/libgtop_sysdeps.la \ diff --git a/src/daemon/gnuserv.c b/src/daemon/gnuserv.c index 4e40e4d2..18bff88c 100644 --- a/src/daemon/gnuserv.c +++ b/src/daemon/gnuserv.c @@ -29,11 +29,11 @@ * ../etc/gnuserv.README relative to the directory containing this file) */ -static char rcsid[] = "!Header: gnuserv.c,v 2.1 95/02/16 11:58:27 arup alpha !"; - #include <glibtop.h> #include <glibtop/open.h> #include <glibtop/close.h> +#include <glibtop/command.h> +#include <glibtop/xmalloc.h> #include "server_config.h" @@ -46,195 +46,13 @@ static char rcsid[] = "!Header: gnuserv.c,v 2.1 95/02/16 11:58:27 arup alpha !"; #include <sys/select.h> #endif -extern void handle_parent_connection __P ((glibtop *, int)); -extern void handle_child_connection __P ((glibtop *, int)); - -#if !defined(SYSV_IPC) && !defined(UNIX_DOMAIN_SOCKETS) && !defined(INTERNET_DOMAIN_SOCKETS) -main () -{ - fprintf (stderr, "Sorry, the Emacs server is only supported on systems that have\n"); - fprintf (stderr, "Unix Domain sockets, Internet Domain sockets or System V IPC\n"); - exit (1); -} /* main */ - -#else /* SYSV_IPC || UNIX_DOMAIN_SOCKETS || INTERNET_DOMAIN_SOCKETS */ - -#ifdef SYSV_IPC - -int ipc_qid = 0; /* ipc message queue id */ -int ipc_wpid = 0; /* watchdog task pid */ - - -/* - * ipc_exit -- clean up the queue id and queue, then kill the watchdog task - * if it exists. exit with the given status. - */ -void -ipc_exit (int stat) -{ - msgctl (ipc_qid, IPC_RMID, 0); - - if (ipc_wpid != 0) - kill (ipc_wpid, SIGKILL); - - exit (stat); -} /* ipc_exit */ - - -/* - * ipc_handle_signal -- catch the signal given and clean up. - */ -void -ipc_handle_signal (int sig) -{ - ipc_exit (0); -} /* ipc_handle_signal */ - - -/* - * ipc_spawn_watchdog -- spawn a watchdog task to clean up the message queue should the - * server process die. - */ -void -ipc_spawn_watchdog (void) -{ - if ((ipc_wpid = fork ()) == 0) { /* child process */ - int ppid = getppid (); /* parent's process id */ - - setpgrp (); /* gnu kills process group on exit */ - - while (1) { - if (kill (ppid, 0) < 0) { /* ppid is no longer - * valid, parent may - * have died */ - ipc_exit (0); - } /* if */ - sleep (10); /* have another go later */ - } /* while */ - } /* if */ -} /* ipc_spawn_watchdog */ - - -/* - * ipc_init -- initialize server, setting the global msqid that can be listened on. - */ -void -ipc_init (struct msgbuf **msgpp) -{ - key_t key; /* messge key */ - char buf[GSERV_BUFSZ]; /* pathname for key */ - - sprintf (buf, "/tmp/lgtd%d", (int) geteuid ()); - creat (buf, 0600); - key = ftok (buf, 1); - - if ((ipc_qid = msgget (key, 0600 | IPC_CREAT)) == -1) - glibtop_error_io ("unable to create msg queue"); - - ipc_spawn_watchdog (); - - signal (SIGTERM, ipc_handle_signal); - signal (SIGINT, ipc_handle_signal); - - if ((*msgpp = (struct msgbuf *) - malloc (sizeof **msgpp + GSERV_BUFSZ)) == NULL) { - glibtop_warn_io ("unable to allocate space for message buffer"); - ipc_exit (1); - } /* if */ -} /* ipc_init */ - - -/* - * handle_ipc_request -- accept a request from a client, pass the request on - * to the GNU Emacs process, then wait for its reply and - * pass that on to the client. - */ -void -handle_ipc_request (struct msgbuf *msgp) -{ -#if 0 - struct msqid_ds msg_st; /* message status */ - char buf[GSERV_BUFSZ]; - int len; /* length of message / read */ - int s, result_len; /* tag fields on the response from emacs */ - int offset = 0; - int total = 1; /* # bytes that will actually be sent off */ - - if ((len = msgrcv (ipc_qid, msgp, GSERV_BUFSZ - 1, 1, 0)) < 0) { - glibtop_warn_io ("msgrcv"); - ipc_exit (1); - } /* if */ - msgctl (ipc_qid, IPC_STAT, &msg_st); - strncpy (buf, msgp->mtext, len); - buf[len] = '\0'; /* terminate */ - - printf ("%d %s", ipc_qid, buf); - fflush (stdout); - - /* now for the response from gnu */ - msgp->mtext[0] = '\0'; - -#if 0 - if ((len = read (0, buf, GSERV_BUFSZ - 1)) < 0) { - glibtop_warn_io ("read"); - ipc_exit (1); - } /* if */ - sscanf (buf, "%d:%[^\n]\n", &junk, msgp->mtext); -#else - - /* read in "n/m:" (n=client fd, m=message length) */ - - while (offset < (GSERV_BUFSZ - 1) && - ((len = read (0, buf + offset, 1)) > 0) && - buf[offset] != ':') { - offset += len; - } +extern void handle_parent_connection __P ((int)); +extern void handle_child_connection __P ((int)); +extern void handle_ipc_connection __P ((int)); - if (len < 0) - glibtop_error_io ("read"); - - /* parse the response from emacs, getting client fd & result length */ - buf[offset] = '\0'; - sscanf (buf, "%d/%d", &s, &result_len); - - while (result_len > 0) { - if ((len = read (0, buf, min2 (result_len, GSERV_BUFSZ - 1))) < 0) - glibtop_error_io ("read"); - - /* Send this string off, but only if we have enough space */ - - if (GSERV_BUFSZ > total) { - if (total + len <= GSERV_BUFSZ) - buf[len] = 0; - else - buf[GSERV_BUFSZ - total] = 0; - - send_string (s, buf); - total += strlen (buf); - } - result_len -= len; - } - - /* eat the newline */ - while ((len = read (0, buf, 1)) == 0); - if (len < 0) - glibtop_error_io ("read"); - - if (buf[0] != '\n') - glibtop_error ("garbage after result [%c]", buf[0]); -#endif - - /* Send a response back to the client. */ - - msgp->mtype = msg_st.msg_lspid; - if (msgsnd (ipc_qid, msgp, strlen (msgp->mtext) + 1, 0) < 0) - glibtop_warn_io ("msgsend(gnuserv)"); -#else - glibtop_error ("handle_ipc_request (): Function not implemented"); +#if !defined(UNIX_DOMAIN_SOCKETS) && !defined(INTERNET_DOMAIN_SOCKETS) +#error "Unix Domain sockets or Internet Domain sockets are required" #endif -} /* handle_ipc_request */ -#endif /* SYSV_IPC */ - #ifdef INTERNET_DOMAIN_SOCKETS @@ -244,7 +62,11 @@ handle_ipc_request (struct msgbuf *msgp) static Xauth *server_xauth = NULL; -#endif +#endif /* INTERNET_DOMAIN_SOCKETS */ + +/* + * timed_read - Read with timeout. + */ static int timed_read (int fd, char *buf, int max, int timeout, int one_line) @@ -289,10 +111,10 @@ timed_read (int fd, char *buf, int max, int timeout, int one_line) } - /* * permitted -- return whether a given host is allowed to connect to the server. */ + static int permitted (u_long host_addr, int fd) { @@ -311,21 +133,22 @@ permitted (u_long host_addr, int fd) return FALSE; #ifdef DEBUG - fprintf (stderr, "Client sent authenticatin protocol '%s'\n", auth_protocol); + fprintf (stderr, "Client sent authenticatin protocol '%s'\n", + auth_protocol); #endif if (strcmp (auth_protocol, DEFAUTH_NAME) && strcmp (auth_protocol, MCOOKIE_NAME)) { - glibtop_warn ("Authentication protocol from client is invalid", auth_protocol); - + glibtop_warn ("Invalid authentication protocol " + "'%s' from client", auth_protocol); return FALSE; } - if (!strcmp (auth_protocol, MCOOKIE_NAME)) { + if (!strcmp (auth_protocol, MCOOKIE_NAME)) { /* * doing magic cookie auth */ - + if (timed_read (fd, buf, 10, AUTH_TIMEOUT, 1) <= 0) return FALSE; @@ -336,27 +159,30 @@ permitted (u_long host_addr, int fd) #ifdef AUTH_MAGIC_COOKIE if (server_xauth && server_xauth->data && - !memcmp (buf, server_xauth->data, auth_data_len)) { + !memcmp (buf, server_xauth->data, auth_data_len)) { return TRUE; } #else - glibtop_warn ("client tried Xauth, but server is not compiled with Xauth"); + glibtop_warn ("Client tried Xauth, but server is " + "not compiled with Xauth"); #endif /* - * auth failed, but allow this to fall through to the GNU_SECURE - * protocol.... + * auth failed, but allow this to fall through to the + * GNU_SECURE protocol.... */ - glibtop_warn ("Xauth authentication failed, trying GNU_SECURE auth..."); - + glibtop_warn ("Xauth authentication failed, " + "trying GNU_SECURE auth..."); + } + /* Other auth protocols go here, and should execute only if * the * auth_protocol name matches. */ - } + /* Now, try the old GNU_SECURE stuff... */ - + #ifdef DEBUG fprintf (stderr, "Doing GNU_SECURE auth ...\n"); #endif @@ -370,9 +196,9 @@ permitted (u_long host_addr, int fd) if (host_addr == permitted_hosts [i]) return (TRUE); } - + return (FALSE); -} /* permitted */ +} /* @@ -383,6 +209,7 @@ permitted (u_long host_addr, int fd) * add each host that is named in the file. * Return the number of hosts added. */ + static int setup_table (void) { @@ -436,11 +263,11 @@ setup_table (void) return hosts; } /* setup_table */ - /* * internet_init -- initialize server, returning an internet socket that can * be listened on. */ + static int internet_init (void) { @@ -479,9 +306,10 @@ internet_init (void) /* - * handle_internet_request -- accept a request from a client and send the information - * to stdout (the gnu process). + * handle_internet_request -- accept a request from a client and send the + * information to stdout (the gnu process). */ + static void handle_internet_request (int ls) { @@ -519,7 +347,7 @@ handle_internet_request (int ls) if (pid) return; - handle_parent_connection (glibtop_global_server, s); + handle_parent_connection (s); close (s); @@ -631,7 +459,7 @@ handle_unix_request (int ls) if (pid) return; - handle_child_connection (glibtop_global_server, s); + handle_child_connection (s); close (s); @@ -659,18 +487,13 @@ main (int argc, char *argv []) int uls = -1; /* unix domain listen socket */ pid_t pid; -#ifdef SYSV_IPC - struct msgbuf *msgp; /* message buffer */ - -#endif /* SYSV_IPC */ - glibtop_init_r (&glibtop_global_server, 0, GLIBTOP_OPEN_NO_OVERRIDE); /* Fork a child. * * The parent will listen for incoming internet connections * and the child will listen for connections from the local - * host using unix domain name sockets or SysV IPC. + * host using unix domain name sockets. */ signal (SIGCHLD, handle_signal); @@ -696,15 +519,10 @@ main (int argc, char *argv []) fprintf (stderr, "Child ID: (%d, %d) - (%d, %d)\n", getuid (), geteuid (), getgid (), getegid ()); -#ifdef SYSV_IPC - /* get a msqid to listen on, and a message buffer. */ - ipc_init (&msgp); -#endif /* SYSV_IPC */ - #ifdef UNIX_DOMAIN_SOCKETS /* get a unix domain socket to listen on. */ uls = unix_init (); -#endif /* UNIX_DOMAIN_SOCKETS */ +#endif } else { /* We are the parent. */ @@ -756,13 +574,10 @@ main (int argc, char *argv []) #ifdef INTERNET_DOMAIN_SOCKETS /* get a internet domain socket to listen on. */ ils = internet_init (); -#endif /* INTERNET_DOMAIN_SOCKETS */ +#endif } while (1) { -#ifdef SYSV_IPC - handle_ipc_request (msgp); -#else /* NOT SYSV_IPC */ fd_set rmask; int ret; @@ -770,8 +585,7 @@ main (int argc, char *argv []) if ((ret == -1) && (errno == ECHILD)) break; - if ((ret == -1) && ((errno == EAGAIN) || - (errno == ERESTART))) + if ((ret == -1) && ((errno == EAGAIN))) continue; if (ret > 0) fprintf (stderr, "Child %d exited.\n", ret); @@ -812,14 +626,11 @@ main (int argc, char *argv []) #ifdef INTERNET_DOMAIN_SOCKETS if (ils > 0 && FD_ISSET (ils, &rmask)) handle_internet_request (ils); -#endif /* INTERNET_DOMAIN_SOCKETS */ +#endif if (FD_ISSET (fileno (stdin), &rmask)) - handle_child_connection (glibtop_global_server, fileno (stdin)); -#endif /* NOT SYSV_IPC */ - } /* while */ + handle_child_connection (fileno (stdin)); + } return 0; -} /* main */ - -#endif /* SYSV_IPC || UNIX_DOMAIN_SOCKETS || INTERNET_DOMAIN_SOCKETS */ +} diff --git a/src/daemon/main.c b/src/daemon/main.c index 3537ee92..6351e33e 100644 --- a/src/daemon/main.c +++ b/src/daemon/main.c @@ -34,6 +34,9 @@ #undef REAL_DEBUG #define PARENT_DEBUG +#define MSG_BUFSZ sizeof (struct _glibtop_ipc_message) +#define MSG_MSGSZ (MSG_BUFSZ - sizeof (long)) + #if defined(HAVE_GETDTABLESIZE) #define GET_MAX_FDS() getdtablesize() #else @@ -42,11 +45,13 @@ #define GET_MAX_FDS() 256 #endif -#define _offset_union(p) ((char *) &response.u.p - (char *) &response) +extern void handle_slave_command __P((glibtop_command *, glibtop_response *, const void *)); + +#define _offset_union(p) ((char *) &resp->u.p - (char *) resp) #define _offset_data(p) _offset_union (data.p) static void -do_output (int s, glibtop_response *response, off_t offset, +do_output (int s, glibtop_response *resp, off_t offset, size_t data_size, const void *data) { #ifdef REAL_DEBUG @@ -54,18 +59,18 @@ do_output (int s, glibtop_response *response, off_t offset, sizeof (glibtop_response), offset); #endif - response->offset = offset; - response->data_size = data_size; + resp->offset = offset; + resp->data_size = data_size; - if (send (s, response, sizeof (glibtop_response), 0) < 0) + if (send (s, resp, sizeof (glibtop_response), 0) < 0) glibtop_warn_io ("send"); - if (response->data_size) { + if (resp->data_size) { #ifdef REAL_DEBUG - fprintf (stderr, "Writing %d bytes of data.\n", response->data_size); + fprintf (stderr, "Writing %d bytes of data.\n", resp->data_size); #endif - if (send (s, data, response->data_size, 0) , 0) + if (send (s, data, resp->data_size, 0) , 0) glibtop_warn_io ("send"); } } @@ -106,20 +111,16 @@ do_read (int s, void *ptr, size_t total_size) } void -handle_parent_connection (glibtop *server, int s) +handle_parent_connection (int s) { - pid_t pid; + glibtop *server = glibtop_global_server; + glibtop_response _resp, *resp = &_resp; + glibtop_command _cmnd, *cmnd = &_cmnd; char parameter [BUFSIZ]; - struct timeval tv; - glibtop_response response; - glibtop_command cmnd; unsigned method; - int null = 0; + pid_t pid; void *ptr; - tv.tv_sec = 5; - tv.tv_usec = 0; - method = GLIBTOP_METHOD_UNIX; glibtop_set_parameter_l (server, GLIBTOP_PARAM_METHOD, @@ -134,30 +135,30 @@ handle_parent_connection (glibtop *server, int s) while (do_read (s, &cmnd, sizeof (glibtop_command))) { #ifdef PARENT_DEBUG fprintf (stderr, "Parent (%d) received command %d from client.\n", - getpid (), cmnd.command); + getpid (), cmnd->command); #endif - if (cmnd.data_size >= BUFSIZ) { - glibtop_warn ("Client sent %d bytes, but buffer is %d", cmnd.size, BUFSIZ); + if (cmnd->data_size >= BUFSIZ) { + glibtop_warn ("Client sent %d bytes, but buffer is %d", cmnd->size, BUFSIZ); return; } memset (parameter, 0, sizeof (parameter)); - if (cmnd.data_size) { + if (cmnd->data_size) { #ifdef PARENT_DEBUG - fprintf (stderr, "Client has %d bytes of data.\n", cmnd.data_size); + fprintf (stderr, "Client has %d bytes of data.\n", cmnd->data_size); #endif - do_read (s, parameter, cmnd.data_size); + do_read (s, parameter, cmnd->data_size); - } else if (cmnd.size) { - memcpy (parameter, cmnd.parameter, cmnd.size); + } else if (cmnd->size) { + memcpy (parameter, cmnd->parameter, cmnd->size); } - switch (cmnd.command) { + switch (cmnd->command) { case GLIBTOP_CMND_QUIT: - do_output (s, &response, 0, 0, NULL); + do_output (s, resp, 0, 0, NULL); fprintf (stderr, "Sending QUIT command (%d).\n", server->socket); @@ -171,233 +172,147 @@ handle_parent_connection (glibtop *server, int s) close (server->socket); return; case GLIBTOP_CMND_SYSDEPS: - response.u.sysdeps.features = GLIBTOP_SYSDEPS_ALL; - do_output (s, &response, _offset_union (sysdeps), 0, NULL); + resp->u.sysdeps.features = GLIBTOP_SYSDEPS_ALL; + do_output (s, resp, _offset_union (sysdeps), 0, NULL); break; case GLIBTOP_CMND_CPU: - glibtop_get_cpu_l (server, &response.u.data.cpu); - do_output (s, &response, _offset_data (cpu), 0, NULL); + glibtop_get_cpu_l (server, &resp->u.data.cpu); + do_output (s, resp, _offset_data (cpu), 0, NULL); break; case GLIBTOP_CMND_MEM: - glibtop_get_mem_l (server, &response.u.data.mem); - do_output (s, &response, _offset_data (mem), 0, NULL); + glibtop_get_mem_l (server, &resp->u.data.mem); + do_output (s, resp, _offset_data (mem), 0, NULL); break; case GLIBTOP_CMND_SWAP: - glibtop_get_swap_l (server, &response.u.data.swap); - do_output (s, &response, _offset_data (swap), 0, NULL); + glibtop_get_swap_l (server, &resp->u.data.swap); + do_output (s, resp, _offset_data (swap), 0, NULL); break; case GLIBTOP_CMND_UPTIME: - glibtop_get_uptime_l (server, &response.u.data.uptime); - do_output (s, &response, _offset_data (uptime), 0, NULL); + glibtop_get_uptime_l (server, &resp->u.data.uptime); + do_output (s, resp, _offset_data (uptime), 0, NULL); break; case GLIBTOP_CMND_LOADAVG: - glibtop_get_loadavg_l (server, &response.u.data.loadavg); - do_output (s, &response, _offset_data (loadavg), 0, NULL); + glibtop_get_loadavg_l (server, &resp->u.data.loadavg); + do_output (s, resp, _offset_data (loadavg), 0, NULL); break; case GLIBTOP_CMND_SHM_LIMITS: - glibtop_get_shm_limits_l (server, &response.u.data.shm_limits); - do_output (s, &response, _offset_data (shm_limits), 0, NULL); + glibtop_get_shm_limits_l (server, &resp->u.data.shm_limits); + do_output (s, resp, _offset_data (shm_limits), 0, NULL); break; case GLIBTOP_CMND_MSG_LIMITS: - glibtop_get_msg_limits_l (server, &response.u.data.msg_limits); - do_output (s, &response, _offset_data (msg_limits), 0, NULL); + glibtop_get_msg_limits_l (server, &resp->u.data.msg_limits); + do_output (s, resp, _offset_data (msg_limits), 0, NULL); break; case GLIBTOP_CMND_SEM_LIMITS: - glibtop_get_sem_limits_l (server, &response.u.data.sem_limits); - do_output (s, &response, _offset_data (sem_limits), 0, NULL); + glibtop_get_sem_limits_l (server, &resp->u.data.sem_limits); + do_output (s, resp, _offset_data (sem_limits), 0, NULL); break; case GLIBTOP_CMND_PROCLIST: - ptr = glibtop_get_proclist_l (server, &response.u.data.proclist); - do_output (s, &response, _offset_data (proclist), - response.u.data.proclist.total, ptr); + ptr = glibtop_get_proclist_l (server, &resp->u.data.proclist); + do_output (s, resp, _offset_data (proclist), + resp->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, _offset_data (proc_state), 0, NULL); + (server, &resp->u.data.proc_state, pid); + do_output (s, resp, _offset_data (proc_state), 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, _offset_data (proc_uid), 0, NULL); + (server, &resp->u.data.proc_uid, pid); + do_output (s, resp, _offset_data (proc_uid), 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, _offset_data (proc_mem), 0, NULL); + (server, &resp->u.data.proc_mem, pid); + do_output (s, resp, _offset_data (proc_mem), 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, _offset_data (proc_time), 0, NULL); + (server, &resp->u.data.proc_time, pid); + do_output (s, resp, _offset_data (proc_time), 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, _offset_data (proc_signal), 0, NULL); + (server, &resp->u.data.proc_signal, pid); + do_output (s, resp, _offset_data (proc_signal), 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, _offset_data (proc_kernel), 0, NULL); + (server, &resp->u.data.proc_kernel, pid); + do_output (s, resp, _offset_data (proc_kernel), 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, _offset_data (proc_segment), 0, NULL); + (server, &resp->u.data.proc_segment, pid); + do_output (s, resp, _offset_data (proc_segment), 0, NULL); break; default: - glibtop_warn_r (server, - "Parent received unknown command %u", - cmnd.command); + glibtop_warn ("Parent received unknown command %u", + cmnd->command); break; } } } void -handle_child_connection (glibtop *server, int s) +handle_child_connection (int s) { - pid_t pid; + glibtop *server = glibtop_global_server; + glibtop_response _resp, *resp = &_resp; + glibtop_command _cmnd, *cmnd = &_cmnd; char parameter [BUFSIZ]; - struct timeval tv; - glibtop_response response; - glibtop_command cmnd; void *ptr; - tv.tv_sec = 5; - tv.tv_usec = 0; - while (do_read (s, &cmnd, sizeof (glibtop_command))) { #ifdef CHILD_DEBUG fprintf (stderr, "Child (%d - %d) received command " - "%d from client.\n", getpid (), s, cmnd.command); + "%d from client.\n", getpid (), s, cmnd->command); #endif - if (cmnd.data_size >= BUFSIZ) { - glibtop_warn ("Client sent %d bytes, but buffer is %d", cmnd.size, BUFSIZ); + if (cmnd->data_size >= BUFSIZ) { + glibtop_warn ("Client sent %d bytes, but buffer is %d", cmnd->size, BUFSIZ); return; } memset (parameter, 0, sizeof (parameter)); - if (cmnd.data_size) { + if (cmnd->data_size) { #ifdef CHILD_DEBUG - fprintf (stderr, "Client has %d bytes of data.\n", cmnd.data_size); + fprintf (stderr, "Client has %d bytes of data.\n", cmnd->data_size); #endif - do_read (s, parameter, cmnd.data_size); + do_read (s, parameter, cmnd->data_size); - } else if (cmnd.size) { - memcpy (parameter, cmnd.parameter, cmnd.size); + } else if (cmnd->size) { + memcpy (parameter, cmnd->parameter, cmnd->size); } - switch (cmnd.command) { + switch (cmnd->command) { case GLIBTOP_CMND_QUIT: - do_output (s, &response, 0, 0, NULL); - - fprintf (stderr, "Child received QUIT command.\n"); - + do_output (s, resp, 0, 0, NULL); return; - case GLIBTOP_CMND_SYSDEPS: - response.u.sysdeps.features = glibtop_server_features; - do_output (s, &response, _offset_union (sysdeps), 0, NULL); - break; - case GLIBTOP_CMND_CPU: - glibtop_get_cpu_l (server, &response.u.data.cpu); - do_output (s, &response, _offset_data (cpu), 0, NULL); - break; - case GLIBTOP_CMND_MEM: - glibtop_get_mem_l (server, &response.u.data.mem); - do_output (s, &response, _offset_data (mem), 0, NULL); - break; - case GLIBTOP_CMND_SWAP: - glibtop_get_swap_l (server, &response.u.data.swap); - do_output (s, &response, _offset_data (swap), 0, NULL); - break; - case GLIBTOP_CMND_UPTIME: - glibtop_get_uptime_l (server, &response.u.data.uptime); - do_output (s, &response, _offset_data (uptime), 0, NULL); - break; - case GLIBTOP_CMND_LOADAVG: - glibtop_get_loadavg_l (server, &response.u.data.loadavg); - do_output (s, &response, _offset_data (loadavg), 0, NULL); - break; - case GLIBTOP_CMND_SHM_LIMITS: - glibtop_get_shm_limits_l (server, &response.u.data.shm_limits); - do_output (s, &response, _offset_data (shm_limits), 0, NULL); - break; - case GLIBTOP_CMND_MSG_LIMITS: - glibtop_get_msg_limits_l (server, &response.u.data.msg_limits); - do_output (s, &response, _offset_data (msg_limits), 0, NULL); - break; - case GLIBTOP_CMND_SEM_LIMITS: - glibtop_get_sem_limits_l (server, &response.u.data.sem_limits); - do_output (s, &response, _offset_data (sem_limits), 0, NULL); - break; +#if GLIBTOP_SUID_PROCLIST case GLIBTOP_CMND_PROCLIST: - ptr = glibtop_get_proclist_l (server, &response.u.data.proclist); - do_output (s, &response, _offset_data (proclist), - response.u.data.proclist.total, ptr); + ptr = glibtop_get_proclist_p + (server, &resp->u.data.proclist); + do_output (s, resp, _offset_data (proclist), + resp->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, _offset_data (proc_state), 0, NULL); - break; - case GLIBTOP_CMND_PROC_UID: - pid = 1; - memcpy (&pid, parameter, sizeof (pid_t)); - glibtop_get_proc_uid_l - (server, &response.u.data.proc_uid, pid); - do_output (s, &response, _offset_data (proc_uid), 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, _offset_data (proc_mem), 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, _offset_data (proc_time), 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, _offset_data (proc_signal), 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, _offset_data (proc_kernel), 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, _offset_data (proc_segment), 0, NULL); - break; +#endif default: - glibtop_warn_r (server, - "Child received unknown command %u", - cmnd.command); + handle_slave_command (cmnd, resp, parameter); + do_output (s, resp, resp->offset, 0, NULL); break; - } } } diff --git a/src/daemon/slave.c b/src/daemon/slave.c new file mode 100644 index 00000000..19c07025 --- /dev/null +++ b/src/daemon/slave.c @@ -0,0 +1,155 @@ +/* $Id$ */ + +/* Copyright (C) 1995, 1996, 1997 Free Software Foundation, Inc. + This file is part of the Gnome Top Library. + Contributed by Martin Baulig <martin@home-of-linux.org>, 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 <glibtop.h> +#include <glibtop/gnuserv.h> + +#include <glibtop/open.h> +#include <glibtop/union.h> + +#include <glibtop/command.h> +#include <glibtop/parameter.h> + +#define _offset_union(p) ((char *) &resp->u.p - (char *) resp) +#define _offset_data(p) _offset_union (data.p) + +void +handle_slave_command (glibtop_command *cmnd, glibtop_response *resp, + const void *parameter) +{ + glibtop *server = glibtop_global_server; + pid_t pid; + + switch (cmnd->command) { + case GLIBTOP_CMND_SYSDEPS: + resp->u.sysdeps.features = glibtop_server_features; + resp->offset = _offset_union (sysdeps); + break; +#if GLIBTOP_SUID_CPU + case GLIBTOP_CMND_CPU: + glibtop_get_cpu_p (server, &resp->u.data.cpu); + resp->offset = _offset_data (cpu); + break; +#endif +#if GLIBTOP_SUID_MEM + case GLIBTOP_CMND_MEM: + glibtop_get_mem_p (server, &resp->u.data.mem); + resp->offset = _offset_data (mem); + break; +#endif +#if GLIBTOP_SUID_SWAP + case GLIBTOP_CMND_SWAP: + glibtop_get_swap_p (server, &resp->u.data.swap); + resp->offset = _offset_data (swap); + break; +#endif +#if GLIBTOP_SUID_UPTIME + case GLIBTOP_CMND_UPTIME: + glibtop_get_uptime_p (server, &resp->u.data.uptime); + resp->offset = _offset_data (uptime); + break; +#endif +#if GLIBTOP_SUID_LOADAVG + case GLIBTOP_CMND_LOADAVG: + glibtop_get_loadavg_p (server, &resp->u.data.loadavg); + resp->offset = _offset_data (loadavg); + break; +#endif +#if GLIBTOP_SUID_SHM_LIMITS + case GLIBTOP_CMND_SHM_LIMITS: + glibtop_get_shm_limits_p (server, &resp->u.data.shm_limits); + resp->offset = _offset_data (shm_limits); + break; +#endif +#if GLIBTOP_SUID_MSG_LIMITS + case GLIBTOP_CMND_MSG_LIMITS: + glibtop_get_msg_limits_p (server, &resp->u.data.msg_limits); + resp->offset = _offset_data (msg_limits); + break; +#endif +#if GLIBTOP_SUID_SEM_LIMITS + case GLIBTOP_CMND_SEM_LIMITS: + glibtop_get_sem_limits_p (server, &resp->u.data.sem_limits); + resp->offset = _offset_data (sem_limits); + break; +#endif +#if GLIBTOP_SUID_PROC_STATE + case GLIBTOP_CMND_PROC_STATE: + memcpy (&pid, parameter, sizeof (pid_t)); + glibtop_get_proc_state_p + (server, &resp->u.data.proc_state, pid); + resp->offset = _offset_data (proc_state); + break; +#endif +#if GLIBTOP_SUID_PROC_UID + case GLIBTOP_CMND_PROC_UID: + memcpy (&pid, parameter, sizeof (pid_t)); + glibtop_get_proc_uid_p + (server, &resp->u.data.proc_uid, pid); + resp->offset = _offset_data (proc_uid); + break; +#endif +#if GLIBTOP_SUID_PROC_MEM + case GLIBTOP_CMND_PROC_MEM: + memcpy (&pid, parameter, sizeof (pid_t)); + glibtop_get_proc_mem_p + (server, &resp->u.data.proc_mem, pid); + resp->offset = _offset_data (proc_mem); + break; +#endif +#if GLIBTOP_SUID_PROC_TIME + case GLIBTOP_CMND_PROC_TIME: + memcpy (&pid, parameter, sizeof (pid_t)); + glibtop_get_proc_time_p + (server, &resp->u.data.proc_time, pid); + resp->offset = _offset_data (proc_time); + break; +#endif +#if GLIBTOP_SUID_PROC_SIGNAL + case GLIBTOP_CMND_PROC_SIGNAL: + memcpy (&pid, parameter, sizeof (pid_t)); + glibtop_get_proc_signal_p + (server, &resp->u.data.proc_signal, pid); + resp->offset = _offset_data (proc_signal); + break; +#endif +#if GLIBTOP_SUID_PROC_KERNEL + case GLIBTOP_CMND_PROC_KERNEL: + memcpy (&pid, parameter, sizeof (pid_t)); + glibtop_get_proc_kernel_p + (server, &resp->u.data.proc_kernel, pid); + resp->offset = _offset_data (proc_kernel); + break; +#endif +#if GLIBTOP_SUID_PROC_SEGMENT + case GLIBTOP_CMND_PROC_SEGMENT: + memcpy (&pid, parameter, sizeof (pid_t)); + glibtop_get_proc_segment_p + (server, &resp->u.data.proc_segment, pid); + resp->offset = _offset_data (proc_segment); + break; +#endif + default: + glibtop_error ("Child received unknown command %u", + cmnd->command); + break; + } +} diff --git a/sysdeps/common/gnuslib.c b/sysdeps/common/gnuslib.c index 99cbf47b..0a6d0e05 100644 --- a/sysdeps/common/gnuslib.c +++ b/sysdeps/common/gnuslib.c @@ -29,26 +29,17 @@ * ../etc/gnuserv.README relative to the directory containing this file) */ -#if 0 -static char rcsid[] = "!Header: gnuslib.c,v 2.4 95/02/16 11:57:37 arup alpha !"; - -#endif - +#include <glibtop.h> #include <glibtop/gnuserv.h> #include <fcntl.h> -#ifdef SYSV_IPC -static int connect_to_ipc_server (void); - -#endif #ifdef UNIX_DOMAIN_SOCKETS -static int connect_to_unix_server (void); - +static int connect_to_unix_server __P((void)); #endif -#ifdef INTERNET_DOMAIN_SOCKETS -static int connect_to_internet_server (const char *serverhost, u_short port); +#ifdef INTERNET_DOMAIN_SOCKETS +static int connect_to_internet_server __P((const char *, u_short)); #endif /* On some systems, e.g. DGUX, inet_addr returns a 'struct in_addr'. */ @@ -94,10 +85,6 @@ glibtop_make_connection (hostarg, portarg, s) *s = connect_to_internet_server (hostarg, portarg); return (int) CONN_INTERNET; #endif -#ifdef SYSV_IPC - return -1; /* hostarg should always be NULL for SYSV_IPC - */ -#endif } else { /* no hostname given. Use unix-domain/sysv-ipc, or * * internet-domain connection to local host if they're not @@ -105,9 +92,6 @@ glibtop_make_connection (hostarg, portarg, s) #if defined(UNIX_DOMAIN_SOCKETS) *s = connect_to_unix_server (); return (int) CONN_UNIX; -#elif defined(SYSV_IPC) - *s = connect_to_ipc_server (); - return (int) CONN_IPC; #elif defined(INTERNET_DOMAIN_SOCKETS) { char localhost[HOSTNAMSZ]; @@ -122,69 +106,6 @@ glibtop_make_connection (hostarg, portarg, s) } } -#ifdef SYSV_IPC -/* - * connect_to_ipc_server -- establish connection with server process via SYSV IPC - * Returns msqid for server if successful. - */ -static int -connect_to_ipc_server (void) -{ - int s; /* connected msqid */ - key_t key; /* message key */ - char buf[GSERV_BUFSZ + 1]; /* buffer for filename */ - - sprintf (buf, "/tmp/lgtd%d", (int) geteuid ()); - creat (buf, 0600); - if ((key = ftok (buf, 1)) == -1) - glibtop_error_io ("unable to get ipc key from %s", buf); - - if ((s = msgget (key, 0600)) == -1) - glibtop_error_io ("unable to access msg queue"); - - return (s); - -} /* connect_to_ipc_server */ - - -/* - * disconnect_from_ipc_server -- inform the server that sending has finished, - * and wait for its reply. - */ -void -disconnect_from_ipc_server (s, msgp, echo) - int s; - struct msgbuf *msgp; - int echo; -{ - int len; /* length of received message */ - - send_string (s, EOT_STR); /* EOT terminates this message */ - msgp->mtype = 1; - - if (msgsnd (s, msgp, strlen (msgp->mtext) + 1, 0) < 0) { - perror (progname); - fprintf (stderr, "%s: unable to send message to server\n", progname); - exit (1); - }; /* if */ - - if ((len = msgrcv (s, msgp, GSERV_BUFSZ, getpid (), 0)) < 0) { - perror (progname); - fprintf (stderr, "%s: unable to receive message from server\n", progname); - exit (1); - }; /* if */ - - if (echo) { - msgp->mtext[len] = '\0'; /* string terminate message */ - fputs (msgp->mtext, stdout); - if (msgp->mtext[len - 1] != '\n') - putchar ('\n'); - }; /* if */ - -} /* disconnect_from_ipc_server */ -#endif /* SYSV_IPC */ - - #if defined(INTERNET_DOMAIN_SOCKETS) || defined(UNIX_DOMAIN_SOCKETS) /* * send_string -- send string to socket. |