From 5e97af535f32c0d4bdc3c1188e962b07a1e8e50d Mon Sep 17 00:00:00 2001 From: Martin Baulig Date: Wed, 22 Jul 1998 09:26:43 +0000 Subject: [PATCH] Summary of this long ChangeLog: * Splitted `libgtop_sysdeps.la' into `libgtop_sysdeps.la' and `libgtop_sysdeps_suid.la'. Everything that needs to be suid/sgid is in `libgtop_sysdeps_suid.la' and the rest in `libgtop_sysdeps.la'. The functions from `libgtop_sysdeps_suid.la' have the `_p' prefix and the ones from `libgtop_sysdeps.la' the `_s' prefix. The suid library uses `glibtop_open_p' and `glibtop_close_p', the normal one `glibtop_init_s' (found in lib/init.c), `glibtop_open_s' and `glibtop_close_s'. * Added `libgtop_suid_common.la' containing stuff from sysdeps/common that is required from the suid server (currently everything from error.c and xmalloc.c). This means, we can add some more stuff to `libgtop_common.la' without the risk that it may be dangerous in the suid server. * The ``library order'' is much clearer now: -> `libgtop.la' (from lib/) contains user-level stuff; all functions except `glibtop_init_r' have the `_l' suffix. -> `libgtop_common.la' (from sysdeps/common/) contains stuff that is common among the sysdeps directories. use anything from `libgtop_sysdeps.la' and `libgtop_sysdeps_suid.la'. -> `libgtop_sysdeps.la' (from sysdeps//) contains everything from the sysdeps directory that doesn't need to be suid/sgid. -> `libgtop_suid_common.la' (from sysdeps/common/) is used from the suid server instead of `libgtop_common.la'. -> `libgtop_sysdeps_suid.la' (from sysdeps//) contains everything from the sysdeps directory that needs to be in the suid server. * To summarize: the server is linked only with functions that need to be suid and everything else is linked only with functions that do not need to be suid. Martin 1998-07-22 Martin Baulig * lib/init.c (glibtop_init_s): Added this init function of the sysdeps directory `libgtop_sysdeps.la'. * lib/open.c (glibtop_open_l): Unconditionally calling `glibtop_init_s' after server initialization. * lib/lib.awk: Removed references to functions from `libgtop_sysdeps_suid.la' to avoid undefined symbols. * sysdeps/stub/open.c (glibtop_open_s): Renamed this function from `glibtop_open_r'. * sysdeps/stub/close.c (glibtop_close_s): Renamed this function from `glibtop_close_l'. * sysdeps/kernel/open.c (glibtop_open_s): Renamed this function from `glibtop_open_r'. * sysdeps/kernel/close.c (glibtop_close_s): Renamed this function from `glibtop_close_l'. * sysdeps/linux/open.c (glibtop_open_s): Renamed this function from `glibtop_open_r'. * sysdeps/linux/close.c (glibtop_close_s): Renamed this function from `glibtop_close_l'. * sysdeps/osf1/Makefile.am (lib_LTLIBRARIES): Added `libgtop_sysdeps_suid.la' for the suid server. * sysdeps/osf1/open_suid.c (glibtop_open_p): New file. Contains all stuff that was formerly in `open.c'. * sysdeps/osf1/open.c: Moved everything from here into the new file `open_suid.c'. * sysdeps/osf1/open.c (glibtop_open_s): New function. * sysdeps/osf1/close_suid.c (glibtop_close_p): New file. * sysdeps/osf1/close.c (glibtop_close_s): New function. * sysdeps/osf1/*.c: Using the new init, open and close functions. * sysdeps/sun4/Makefile.am (lib_LTLIBRARIES): Added `libgtop_sysdeps_suid.la' for the suid server. * sysdeps/sun4/nosuid.c (glibtop_open_s, glibtop_close_s): New file * sysdeps/sun4/*.c: All functions now have the `_p' suffix. * sysdeps/common/Makefile.am (lib_LTLIBRARIES): Added `libgtop_suid_common.la' which only contains stuff that is needed in the suid parts. * sysdeps/common/xmalloc.c: Using `glibtop_error_io_r' instead of `glibtop_error_r'. * sysdeps/{kernel, linux, osf1, sun4, stub}/init.c: Removed. `glibtop_init_s' has been moved into `lib/init.c' since it's the same in all the sysdeps directories. * src/server/main.c: It is now an error to request a feature that does not need the suid server. * src/proxy: Removed. --- .cvsignore | 3 +- ChangeLog | 70 +++++++++ configure.in | 7 + examples/first.c | 6 +- examples/second.c | 2 +- features.def | 2 +- glibtop.h | 1 + include/glibtop/close.h | 3 + include/glibtop/open.h | 3 +- include/glibtop/procsignal.h | 2 +- lib/init.c | 18 +++ lib/lib.awk | 35 +++-- lib/open.c | 40 +++-- src/Makefile.am | 11 +- src/daemon/Makefile.am | 27 ++-- sysdeps/kernel/init.c => src/daemon/daemon.h | 51 ++++--- src/daemon/gnuserv.c | 31 +++- src/daemon/io.c | 82 ++++++++++ src/daemon/main.c | 152 +------------------ src/daemon/slave.c | 61 +++++++- src/server/Makefile.am | 6 +- src/server/main.c | 89 ++++++----- sysdeps/common/Makefile.am | 4 +- sysdeps/common/xmalloc.c | 10 +- sysdeps/kernel/Makefile.am | 2 +- sysdeps/kernel/close.c | 2 +- sysdeps/kernel/open.c | 6 +- sysdeps/linux/close.c | 2 +- sysdeps/linux/init.c | 44 ------ sysdeps/linux/open.c | 2 +- sysdeps/osf1/Makefile.am | 13 +- sysdeps/osf1/close.c | 2 +- sysdeps/{sun4/init.c => osf1/close_suid.c} | 24 +-- sysdeps/osf1/cpu.c | 2 +- sysdeps/osf1/loadavg.c | 2 +- sysdeps/osf1/mem.c | 4 +- sysdeps/osf1/msg_limits.c | 2 +- sysdeps/osf1/open.c | 39 +---- sysdeps/osf1/open_suid.c | 66 ++++++++ sysdeps/osf1/prockernel.c | 4 +- sysdeps/osf1/proclist.c | 4 +- sysdeps/osf1/procmem.c | 4 +- sysdeps/osf1/procsegment.c | 4 +- sysdeps/osf1/procsignal.c | 2 +- sysdeps/osf1/procstate.c | 4 +- sysdeps/osf1/proctime.c | 4 +- sysdeps/osf1/procuid.c | 6 +- sysdeps/osf1/sem_limits.c | 2 +- sysdeps/osf1/shm_limits.c | 2 +- sysdeps/osf1/swap.c | 2 +- sysdeps/osf1/uptime.c | 2 +- sysdeps/stub/Makefile.am | 2 +- sysdeps/stub/close.c | 2 +- sysdeps/stub/init.c | 42 ----- sysdeps/stub/open.c | 2 +- sysdeps/sun4/Makefile.am | 9 +- sysdeps/sun4/close.c | 2 +- sysdeps/sun4/cpu.c | 2 +- sysdeps/sun4/loadavg.c | 2 +- sysdeps/sun4/mem.c | 2 +- sysdeps/sun4/msg_limits.c | 2 +- sysdeps/{osf1/init.c => sun4/nosuid.c} | 22 +-- sysdeps/sun4/open.c | 64 +++++--- sysdeps/sun4/prockernel.c | 2 +- sysdeps/sun4/proclist.c | 2 +- sysdeps/sun4/procmem.c | 2 +- sysdeps/sun4/procsegment.c | 2 +- sysdeps/sun4/procsignal.c | 2 +- sysdeps/sun4/procstate.c | 2 +- sysdeps/sun4/proctime.c | 2 +- sysdeps/sun4/procuid.c | 2 +- sysdeps/sun4/sem_limits.c | 2 +- sysdeps/sun4/shm_limits.c | 2 +- sysdeps/sun4/swap.c | 2 +- sysdeps/sun4/uptime.c | 2 +- 75 files changed, 622 insertions(+), 523 deletions(-) rename sysdeps/kernel/init.c => src/daemon/daemon.h (51%) create mode 100644 src/daemon/io.c delete mode 100644 sysdeps/linux/init.c rename sysdeps/{sun4/init.c => osf1/close_suid.c} (67%) create mode 100644 sysdeps/osf1/open_suid.c delete mode 100644 sysdeps/stub/init.c rename sysdeps/{osf1/init.c => sun4/nosuid.c} (74%) diff --git a/.cvsignore b/.cvsignore index 6b0b0b60..02d78200 100644 --- a/.cvsignore +++ b/.cvsignore @@ -1,8 +1,6 @@ Makefile Makefile.in aclocal.m4 -alpha-dec-osf1 -autoh31167 config.cache config.h config.h.in @@ -25,3 +23,4 @@ sun4 sun4sol2 support i386-freebsd +alpha-dec-osf3.0 diff --git a/ChangeLog b/ChangeLog index 7c0b6310..884d17f8 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,73 @@ +1998-07-22 Martin Baulig + + * lib/init.c (glibtop_init_s): Added this init function of + the sysdeps directory `libgtop_sysdeps.la'. + + * lib/open.c (glibtop_open_l): Unconditionally calling + `glibtop_init_s' after server initialization. + + * lib/lib.awk: Removed references to functions from + `libgtop_sysdeps_suid.la' to avoid undefined symbols. + + * sysdeps/stub/open.c (glibtop_open_s): Renamed this + function from `glibtop_open_r'. + + * sysdeps/stub/close.c (glibtop_close_s): Renamed this + function from `glibtop_close_l'. + + * sysdeps/kernel/open.c (glibtop_open_s): Renamed this + function from `glibtop_open_r'. + + * sysdeps/kernel/close.c (glibtop_close_s): Renamed this + function from `glibtop_close_l'. + + * sysdeps/linux/open.c (glibtop_open_s): Renamed this + function from `glibtop_open_r'. + + * sysdeps/linux/close.c (glibtop_close_s): Renamed this + function from `glibtop_close_l'. + + * sysdeps/osf1/Makefile.am (lib_LTLIBRARIES): Added + `libgtop_sysdeps_suid.la' for the suid server. + + * sysdeps/osf1/open_suid.c (glibtop_open_p): New file. + Contains all stuff that was formerly in `open.c'. + + * sysdeps/osf1/open.c: Moved everything from here into + the new file `open_suid.c'. + + * sysdeps/osf1/open.c (glibtop_open_s): New function. + + * sysdeps/osf1/close_suid.c (glibtop_close_p): New file. + + * sysdeps/osf1/close.c (glibtop_close_s): New function. + + * sysdeps/osf1/*.c: Using the new init, open and close + functions. + + * sysdeps/sun4/Makefile.am (lib_LTLIBRARIES): Added + `libgtop_sysdeps_suid.la' for the suid server. + + * sysdeps/sun4/nosuid.c (glibtop_open_s, glibtop_close_s): New file + + * sysdeps/sun4/*.c: All functions now have the `_p' suffix. + + * sysdeps/common/Makefile.am (lib_LTLIBRARIES): Added + `libgtop_suid_common.la' which only contains stuff that is + needed in the suid parts. + + * sysdeps/common/xmalloc.c: Using `glibtop_error_io_r' instead + of `glibtop_error_r'. + + * sysdeps/{kernel, linux, osf1, sun4, stub}/init.c: Removed. + `glibtop_init_s' has been moved into `lib/init.c' since it's the + same in all the sysdeps directories. + + * src/server/main.c: It is now an error to request a feature that + does not need the suid server. + + * src/proxy: Removed. + 1998-07-21 Martin Baulig * doc/ChangeLog: New file. diff --git a/configure.in b/configure.in index 1e426ed1..c46d5a5b 100644 --- a/configure.in +++ b/configure.in @@ -231,6 +231,13 @@ if test "$need_gnome_support" = yes; then fi fi +if test x$libgtop_need_server = xyes ; then + sysdeps_suid_lib="\$(top_builddir)/sysdeps/\$(sysdeps_dir)/libgtop_sysdeps_suid.la" +else + sysdeps_suid_lib= +fi +AC_SUBST(sysdeps_suid_lib) + AC_OUTPUT([ Makefile po/Makefile.in diff --git a/examples/first.c b/examples/first.c index bd7a35b7..33ac2f94 100644 --- a/examples/first.c +++ b/examples/first.c @@ -211,7 +211,7 @@ main (int argc, char *argv []) glibtop_get_proc_signal (&data.proc_signal, pid); printf ("Proc_Signal PID %5u (0x%08lx): " - "%d %d %d %d\n", pid, data.proc_signal.flags, + "%lu %lu %lu %lu\n", pid, data.proc_signal.flags, data.proc_signal.signal, data.proc_signal.blocked, data.proc_signal.sigignore, data.proc_signal.sigcatch); @@ -282,7 +282,7 @@ main (int argc, char *argv []) glibtop_get_proc_signal (&data.proc_signal, ppid); printf ("Proc_Signal PPID %5u (0x%08lx): " - "%d %d %d %d\n", ppid, data.proc_signal.flags, + "%lu %lu %lu %u\n", ppid, data.proc_signal.flags, data.proc_signal.signal, data.proc_signal.blocked, data.proc_signal.sigignore, data.proc_signal.sigcatch); @@ -353,7 +353,7 @@ main (int argc, char *argv []) glibtop_get_proc_signal (&data.proc_signal, 1); printf ("Proc_Signal INIT %5u (0x%08lx): " - "%d %d %d %d\n", 1, data.proc_signal.flags, + "%lu %lu %lu %lu\n", 1, data.proc_signal.flags, data.proc_signal.signal, data.proc_signal.blocked, data.proc_signal.sigignore, data.proc_signal.sigcatch); diff --git a/examples/second.c b/examples/second.c index 423111ab..f36c7ce4 100644 --- a/examples/second.c +++ b/examples/second.c @@ -110,7 +110,7 @@ main (int argc, char *argv []) glibtop_get_proc_signal (&data.proc_signal, pid); fprintf (stderr, "Proc_Signal PID %5u (0x%08lx): " - "%d %d %d %d\n", pid, data.proc_signal.flags, + "%lu %lu %lu %lu\n", pid, data.proc_signal.flags, data.proc_signal.signal, data.proc_signal.blocked, data.proc_signal.sigignore, data.proc_signal.sigcatch); diff --git a/features.def b/features.def index d4de80a1..ea83e9b3 100644 --- a/features.def +++ b/features.def @@ -11,6 +11,6 @@ proc_state str(cmd):char(state):ulong(uid,gid) proc_uid long(uid,euid,gid,egid,pid,ppid,pgrp,session,tty,tpgid,priority,nice) proc_mem long(size,vsize,resident,share,rss,rss_rlim) proc_time long(start_time,utime,stime,cutime,cstime,timeout,it_real_value) -proc_signal long(signal,blocked,sigignore,sigcatch) +proc_signal ulong(signal,blocked,sigignore,sigcatch) proc_kernel ulong(k_flags,min_flt,maj_flt,cmin_flt,cmaj_flt,kstk_esp,kstk_eip,wchan) proc_segment long(trs,lrs,drs,dt):ulong(start_code,end_code,start_stack) diff --git a/glibtop.h b/glibtop.h index aa8e99b5..d0855e73 100644 --- a/glibtop.h +++ b/glibtop.h @@ -63,6 +63,7 @@ extern const unsigned long glibtop_server_features; #define glibtop_close() glibtop_close_r(glibtop_global_server); extern glibtop *glibtop_init_r __P((glibtop **, const unsigned long, const unsigned)); +extern glibtop *glibtop_init_s __P((glibtop **, const unsigned long, const unsigned)); #ifdef HAVE_GUILE diff --git a/include/glibtop/close.h b/include/glibtop/close.h index 6241737a..f31826ce 100644 --- a/include/glibtop/close.h +++ b/include/glibtop/close.h @@ -28,6 +28,9 @@ __BEGIN_DECLS extern void glibtop_close_r __P((glibtop *)); +extern void glibtop_close_s __P((glibtop *)); +extern void glibtop_close_p __P((glibtop *)); + __END_DECLS #endif diff --git a/include/glibtop/open.h b/include/glibtop/open.h index 0da35985..e4b3b4f5 100644 --- a/include/glibtop/open.h +++ b/include/glibtop/open.h @@ -42,7 +42,8 @@ __BEGIN_DECLS 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)); +extern void glibtop_open_p __P((glibtop *, const char *, const unsigned long, const unsigned)); +extern void glibtop_open_s __P((glibtop *, const char *, const unsigned long, const unsigned)); __END_DECLS diff --git a/include/glibtop/procsignal.h b/include/glibtop/procsignal.h index e3690ea0..0c760ce3 100644 --- a/include/glibtop/procsignal.h +++ b/include/glibtop/procsignal.h @@ -41,7 +41,7 @@ typedef struct _glibtop_proc_signal glibtop_proc_signal; struct _glibtop_proc_signal { unsigned long flags; - int signal, /* mask of pending signals */ + unsigned long signal, /* mask of pending signals */ blocked, /* mask of blocked signals */ sigignore, /* mask of ignored signals */ sigcatch; /* mask of caught signals */ diff --git a/lib/init.c b/lib/init.c index 71b6b71a..38af9b06 100644 --- a/lib/init.c +++ b/lib/init.c @@ -191,3 +191,21 @@ glibtop_init_r (glibtop **server_ptr, const unsigned long features, return server; } + +glibtop * +glibtop_init_s (glibtop **server, const unsigned long features, + const unsigned flags) +{ + if (*server != NULL) + return *server; + + fprintf (stderr, "DEBUG: %s (%d)\n", __FILE__, __LINE__); + + if (glibtop_global_server == NULL) { + glibtop_global_server = &_glibtop_global_server; + glibtop_open_s (glibtop_global_server, "glibtop", + features, flags); + } + + return *server = glibtop_global_server; +} diff --git a/lib/lib.awk b/lib/lib.awk index 3ea6569e..3dc1bbba 100644 --- a/lib/lib.awk +++ b/lib/lib.awk @@ -2,14 +2,13 @@ BEGIN { print "/* lib.c */"; print "/* This is a generated file. Please modify `lib.awk' */"; print ""; - + print "#include "; print "#include "; print "#include "; print "#include "; - + print ""; - } function output(feature) { @@ -25,7 +24,7 @@ function output(feature) { } else { param = ""; } - + print "glibtop_get_"feature"_l (glibtop *server, glibtop_"feature" *buf"param")"; print "{"; print "\tglibtop_init_r (&server, GLIBTOP_SYSDEPS_"toupper(feature)", 0);"; @@ -33,23 +32,37 @@ function output(feature) { print "\tif ((server->flags & _GLIBTOP_INIT_STATE_SERVER) &&"; print "\t (server->features & (1 << GLIBTOP_SYSDEPS_"toupper(feature)")))"; print "\t{"; - + if (feature ~ /^proc_/) { - print "\t\t"prefix"glibtop_call_l (server, GLIBTOP_CMND_"toupper(feature)", sizeof (pid_t),"; - print "\t\t\t\t&pid, sizeof (glibtop_"feature"), buf);"; + print "\t\tglibtop_call_l (server, GLIBTOP_CMND_"toupper(feature)","; + print "\t\t\t\tsizeof (pid_t), &pid,"; + print "\t\t\t\tsizeof (glibtop_"feature"),"; + print "\t\t\t\tbuf);"; print "\t} else {"; - print "\t\t"prefix"glibtop_get_"feature"_r (server, buf, pid);"; + print "#if (!GLIBTOP_SUID_"toupper(feature)")"; + print "\t\tglibtop_get_"feature"_r (server, buf, pid);"; } else { - print "\t\t"prefix"glibtop_call_l (server, GLIBTOP_CMND_"toupper(feature)", 0, NULL,"; - print "\t\t\t sizeof (glibtop_"feature"), buf);"; + if (feature ~ /^proclist$/) { + print "\t\treturn glibtop_call_l (server, GLIBTOP_CMND_PROCLIST,"; + print "\t\t\t\t 0, NULL, sizeof (glibtop_proclist),"; + print "\t\t\t\t buf);"; + } else { + print "\t\tglibtop_call_l (server, GLIBTOP_CMND_"toupper(feature)", 0, NULL,"; + print "\t\t\t\tsizeof (glibtop_"feature"), buf);"; + } print "\t} else {"; + print "#if (!GLIBTOP_SUID_"toupper(feature)")"; print "\t\t"prefix"glibtop_get_"feature"_r (server, buf);"; } + print "#else"; + print "\t\terrno = ENOSYS;"; + print "\t\tglibtop_error_io_r (server, \"glibtop_get_"feature"\");"; + print "#endif"; print "\t}"; print "}"; print ""; } - + /^(\w+)/ { output($1) } diff --git a/lib/open.c b/lib/open.c index fe90bfff..30477691 100644 --- a/lib/open.c +++ b/lib/open.c @@ -44,12 +44,23 @@ glibtop_open_l (glibtop *server, const char *program_name, server->flags |= _GLIBTOP_INIT_STATE_OPEN; + server->features = features; + + switch (server->method) { + case GLIBTOP_METHOD_PIPE: + case GLIBTOP_METHOD_UNIX: + if (glibtop_server_features & features) + break; + + fprintf (stderr, "Using the server is not required.\n"); + + server->method = GLIBTOP_METHOD_DIRECT; + break; + } + switch (server->method) { case GLIBTOP_METHOD_DIRECT: - fprintf (stderr, "Calling sysdeps open function.\n"); - - glibtop_open_r (server, program_name, features, flags); - + server->features = 0; break; case GLIBTOP_METHOD_INET: fprintf (stderr, "Connecting to '%s' port %ld.\n", @@ -58,9 +69,9 @@ glibtop_open_l (glibtop *server, const char *program_name, connect_type = glibtop_make_connection (server->server_host, server->server_port, &server->socket); - + fprintf (stderr, "Connect Type is %d.\n", connect_type); - + server->flags |= _GLIBTOP_INIT_STATE_SERVER; server->features = -1; @@ -81,14 +92,6 @@ glibtop_open_l (glibtop *server, const char *program_name, fprintf (stderr, "Opening pipe to server (%s).\n", GTOP_SERVER); -#if 0 - if (socketpair (AF_UNIX, SOCK_STREAM, 0, server->input)) - glibtop_error_io_r (server, "socketpair"); - - if (socketpair (AF_UNIX, SOCK_STREAM, 0, server->output)) - glibtop_error_io_r (server, "socketpair"); -#endif - if (pipe (server->input) || pipe (server->output)) glibtop_error_io_r (server, "cannot make a pipe"); @@ -102,7 +105,7 @@ glibtop_open_l (glibtop *server, const char *program_name, dup2 (server->input [1], 1); dup2 (server->output [0], 0); execl (GTOP_SERVER, NULL); - glibtop_error_io_r (server, "execl %s", GTOP_SERVER); + glibtop_error_io_r (server, "execl (%s)", GTOP_SERVER); _exit (2); } @@ -138,4 +141,11 @@ glibtop_open_l (glibtop *server, const char *program_name, fprintf (stderr, "Server features are %lu.\n", server->features); } + + /* In any case, we call the open functions of our own sysdeps + * directory. */ + + fprintf (stderr, "Calling sysdeps open function.\n"); + + glibtop_open_s (server, program_name, features, flags); } diff --git a/src/Makefile.am b/src/Makefile.am index 1f27265f..a3e337cb 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1 +1,10 @@ -SUBDIRS = server daemon +if NEED_LIBGTOP +server_SUBDIRS = server +else +server_SUBDIRS = +endif + +SUBDIRS = $(server_SUBDIRS) daemon + +DIST_SUBDIRS = server daemon + diff --git a/src/daemon/Makefile.am b/src/daemon/Makefile.am index ab15886e..1a4d63d2 100644 --- a/src/daemon/Makefile.am +++ b/src/daemon/Makefile.am @@ -4,22 +4,23 @@ 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@"\" -DDEBUG -DREAL_DEBUG +CFLAGS = -Wall -W @CFLAGS@ \ + -DGTOP_SERVER=\""@LIBGTOP_SERVER@"\" \ + -DDEBUG -DREAL_DEBUG -D_BSD \ + -DLIBGTOP_DAEMON_SLAVE -bin_PROGRAMS = gnuserv +bin_PROGRAMS = libgtop_daemon -EXTRA_PROGRAMS = gtop_daemon +if NEED_LIBGTOP +suid_sysdeps = $(top_builddir)/sysdeps/@sysdeps_dir@/libgtop_sysdeps_suid.la +else +suid_sysdeps = +endif -gnuserv_SOURCES = gnuserv.c main.c slave.c -gnuserv_LDADD = $(top_builddir)/lib/libgtop.la \ +libgtop_daemon_SOURCES = gnuserv.c main.c io.c +libgtop_daemon_LDADD = $(top_builddir)/lib/libgtop.la \ $(top_builddir)/sysdeps/common/libgtop_common.la \ $(top_builddir)/sysdeps/@sysdeps_dir@/libgtop_sysdeps.la \ + @sysdeps_suid_lib@ \ @INTLLIBS@ @LIBSUPPORT@ @X_LIBS@ -lXau -gnuserv_LDFLAGS = -static - -gtop_daemon_SOURCES = main.c inet.c loop.c output.c version.c -gtop_daemon_LDADD = $(top_builddir)/lib/libgtop.la \ - $(top_builddir)/sysdeps/common/libgtop_common.la \ - $(top_builddir)/sysdeps/@sysdeps_dir@/libgtop_sysdeps.la \ - @INTLLIBS@ @LIBSUPPORT@ -gtop_daemon_LDFLAGS = -static +libgtop_daemon_LDFLAGS = -static diff --git a/sysdeps/kernel/init.c b/src/daemon/daemon.h similarity index 51% rename from sysdeps/kernel/init.c rename to src/daemon/daemon.h index f491070f..9a807833 100644 --- a/sysdeps/kernel/init.c +++ b/src/daemon/daemon.h @@ -19,26 +19,43 @@ write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +#ifndef __GLIBTOP_DAEMON_H__ +#define __GLIBTOP_DAEMON_H__ + #include +#include + #include +#include +#include +#include +#include +#include -static glibtop _glibtop_global_server; -glibtop *glibtop_global_server = NULL; +#include -glibtop * -glibtop_init_r (glibtop **server, const unsigned long features, - const unsigned flags) -{ - if (*server != NULL) - return *server; +__BEGIN_DECLS - fprintf (stderr, "DEBUG: %s (%d)\n", __FILE__, __LINE__); +#if defined(HAVE_GETDTABLESIZE) +#define GET_MAX_FDS() getdtablesize() +#else +/* Fallthrough case - please add other #elif cases above + for different OS's as necessary */ +#define GET_MAX_FDS() 256 +#endif - if (glibtop_global_server == NULL) { - glibtop_global_server = &_glibtop_global_server; - glibtop_open_r (glibtop_global_server, "glibtop", - features, flags); - } - - return *server = glibtop_global_server; -} +#define _offset_union(p) ((char *) &resp->u.p - (char *) resp) +#define _offset_data(p) _offset_union (data.p) + +#define MSG_BUFSZ sizeof (struct _glibtop_ipc_message) +#define MSG_MSGSZ (MSG_BUFSZ - sizeof (long)) + +extern void handle_slave_connection __P((int, int)); +extern void handle_slave_command __P((glibtop_command *, glibtop_response *, const void *)); + +extern void do_output __P((int, glibtop_response *, off_t, size_t, const void *)); +extern int do_read __P((int, void *, size_t)); + +__END_DECLS + +#endif diff --git a/src/daemon/gnuserv.c b/src/daemon/gnuserv.c index cebe4431..e210184c 100644 --- a/src/daemon/gnuserv.c +++ b/src/daemon/gnuserv.c @@ -49,7 +49,7 @@ #endif extern void handle_parent_connection __P ((int)); -extern void handle_child_connection __P ((int)); +extern void handle_slave_connection __P ((int, int)); extern void handle_ipc_connection __P ((int)); #if !defined(UNIX_DOMAIN_SOCKETS) && !defined(INTERNET_DOMAIN_SOCKETS) @@ -216,7 +216,7 @@ static int setup_table (void) { char hostname [HOSTNAMSZ]; - u_int host_addr; + long host_addr; int i, hosts = 0; /* Make sure every entry is null */ @@ -225,7 +225,7 @@ setup_table (void) gethostname (hostname, HOSTNAMSZ); - if (((long) host_addr = glibtop_internet_addr (hostname)) == -1) + if ((host_addr = glibtop_internet_addr (hostname)) == -1) glibtop_error ("Can't resolve '%s'", hostname); #ifdef AUTH_MAGIC_COOKIE @@ -326,13 +326,15 @@ handle_internet_request (int ls) glibtop_error_io ("accept"); #ifdef DEBUG - fprintf (stderr, "Connection was made from %s.\n", inet_ntoa (peer.sin_addr)); + fprintf (stderr, "Connection was made from %s.\n", + inet_ntoa (peer.sin_addr)); #endif /* Check that access is allowed - if not return crud to the client */ if (!permitted (peer.sin_addr.s_addr, s)) { close (s); - glibtop_warn ("Refused connection from %s.", inet_ntoa (peer.sin_addr)); + glibtop_warn ("Refused connection from %s.", + inet_ntoa (peer.sin_addr)); return; } /* if */ @@ -453,6 +455,7 @@ handle_unix_request (int ls) fprintf (stderr, "Accepted connection on socket %d.\n", s); #endif +#ifdef GLIBTOP_DAEMON_SLAVE pid = fork (); if (pid == -1) @@ -461,7 +464,8 @@ handle_unix_request (int ls) if (pid) return; - handle_child_connection (s); + handle_slave_connection (s, s); +#endif close (s); @@ -502,7 +506,11 @@ main (int argc, char *argv []) signal (SIGCHLD, handle_signal); +#ifdef GLIBTOP_DAEMON_SLAVE pid = fork (); +#else + pid = getpid (); +#endif if (pid == -1) glibtop_error_io ("fork failed"); @@ -542,7 +550,11 @@ main (int argc, char *argv []) } else { /* We are the parent. */ +#ifdef GLIBTOP_DAEMON_SLAVE const unsigned method = GLIBTOP_METHOD_UNIX; +#else + const unsigned method = GLIBTOP_METHOD_PIPE; +#endif const unsigned long features = GLIBTOP_SYSDEPS_ALL; @@ -657,8 +669,11 @@ main (int argc, char *argv []) handle_internet_request (ils); #endif - if (FD_ISSET (fileno (stdin), &rmask)) - handle_child_connection (fileno (stdin)); +#ifdef GLIBTOP_DAEMON_SLAVE + if ((pid == 0) && FD_ISSET (fileno (stdin), &rmask)) + handle_slave_connection (fileno (stdin), + fileno (stdout)); +#endif } return 0; diff --git a/src/daemon/io.c b/src/daemon/io.c new file mode 100644 index 00000000..f3c6a410 --- /dev/null +++ b/src/daemon/io.c @@ -0,0 +1,82 @@ +/* $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 "daemon.h" + +void +do_output (int s, glibtop_response *resp, off_t offset, + size_t data_size, const void *data) +{ +#ifdef REAL_DEBUG + fprintf (stderr, "Really writing %d bytes at offset %lu.\n", + sizeof (glibtop_response), offset); +#endif + + resp->offset = offset; + resp->data_size = data_size; + + if (send (s, resp, sizeof (glibtop_response), 0) < 0) + glibtop_warn_io ("send"); + + if (resp->data_size) { +#ifdef REAL_DEBUG + fprintf (stderr, "Writing %d bytes of data.\n", resp->data_size); +#endif + + if (send (s, data, resp->data_size, 0) , 0) + glibtop_warn_io ("send"); + } +} + +int +do_read (int s, void *ptr, size_t total_size) +{ + int nread; + size_t already_read = 0, remaining = total_size; + + while (already_read < total_size) { + if (s) + nread = recv (s, ptr, remaining, 0); + else + nread = read (0, ptr, remaining); + + if ((already_read == 0) && (nread == 0)) { + glibtop_warn ("pid %d received eof.", getpid ()); + return 0; + } + + if (nread <= 0) { + glibtop_warn_io ("recv"); + return 0; + } + + already_read += nread; + remaining -= nread; + (char *) ptr += nread; + +#ifdef REAL_DEBUG + fprintf (stderr, "READ (%d): %d - %d - %d\n", + nread, already_read, remaining, total_size); +#endif + } + + return already_read; +} diff --git a/src/daemon/main.c b/src/daemon/main.c index 9cef3083..ff58c80f 100644 --- a/src/daemon/main.c +++ b/src/daemon/main.c @@ -19,99 +19,7 @@ write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -#include - -#include -#include -#include -#include -#include -#include - -#include -#include - -#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 -/* Fallthrough case - please add other #elif cases above - for different OS's as necessary */ -#define GET_MAX_FDS() 256 -#endif - -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 __P((int, glibtop_response *, off_t, size_t, const void *)); -static int do_read __P((int, void *, size_t)); - -static void -do_output (int s, glibtop_response *resp, off_t offset, - size_t data_size, const void *data) -{ -#ifdef REAL_DEBUG - fprintf (stderr, "Really writing %d bytes at offset %lu.\n", - sizeof (glibtop_response), offset); -#endif - - resp->offset = offset; - resp->data_size = data_size; - - if (send (s, resp, sizeof (glibtop_response), 0) < 0) - glibtop_warn_io ("send"); - - if (resp->data_size) { -#ifdef REAL_DEBUG - fprintf (stderr, "Writing %d bytes of data.\n", resp->data_size); -#endif - - if (send (s, data, resp->data_size, 0) , 0) - glibtop_warn_io ("send"); - } -} - -static int -do_read (int s, void *ptr, size_t total_size) -{ - int nread; - size_t already_read = 0, remaining = total_size; - - while (already_read < total_size) { - if (s) - nread = recv (s, ptr, remaining, 0); - else - nread = read (0, ptr, remaining); - - if ((already_read == 0) && (nread == 0)) { - glibtop_warn ("pid %d received eof.", getpid ()); - return 0; - } - - if (nread <= 0) { - glibtop_warn_io ("recv"); - return 0; - } - - already_read += nread; - remaining -= nread; - (char *) ptr += nread; - -#ifdef REAL_DEBUG - fprintf (stderr, "READ (%d): %d - %d - %d\n", - nread, already_read, remaining, total_size); -#endif - } - - return already_read; -} +#include "daemon.h" void handle_parent_connection (int s) @@ -125,7 +33,7 @@ handle_parent_connection (int s) fprintf (stderr, "Parent features = %lu\n", glibtop_server_features); - while (do_read (s, &cmnd, sizeof (glibtop_command))) { + while (do_read (s, cmnd, sizeof (glibtop_command))) { #ifdef PARENT_DEBUG fprintf (stderr, "Parent (%d) received command %d from client.\n", getpid (), cmnd->command); @@ -153,6 +61,7 @@ handle_parent_connection (int s) case GLIBTOP_CMND_QUIT: do_output (s, resp, 0, 0, NULL); +#ifdef GLIBTOP_DAEMON_SLAVE fprintf (stderr, "Sending QUIT command (%d).\n", server->socket); @@ -163,6 +72,7 @@ handle_parent_connection (int s) server->socket); close (server->socket); +#endif return; case GLIBTOP_CMND_SYSDEPS: resp->u.sysdeps.features = GLIBTOP_SYSDEPS_ALL; @@ -255,57 +165,3 @@ handle_parent_connection (int s) } } } - -void -handle_child_connection (int s) -{ - glibtop *server = glibtop_global_server; - glibtop_response _resp, *resp = &_resp; - glibtop_command _cmnd, *cmnd = &_cmnd; - char parameter [BUFSIZ]; - void *ptr; - - 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); -#endif - - 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) { -#ifdef CHILD_DEBUG - fprintf (stderr, "Client has %d bytes of data.\n", cmnd->data_size); -#endif - - do_read (s, parameter, cmnd->data_size); - - } else if (cmnd->size) { - memcpy (parameter, cmnd->parameter, cmnd->size); - } - - switch (cmnd->command) { - case GLIBTOP_CMND_QUIT: - do_output (s, resp, 0, 0, NULL); - return; -#if GLIBTOP_SUID_PROCLIST - case GLIBTOP_CMND_PROCLIST: - 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; -#endif - default: - 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 index 19c07025..cefeda2d 100644 --- a/src/daemon/slave.c +++ b/src/daemon/slave.c @@ -19,17 +19,62 @@ write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -#include -#include +#include "daemon.h" -#include -#include +void +handle_slave_connection (int input, int output) +{ + glibtop *server = glibtop_global_server; + glibtop_response _resp, *resp = &_resp; + glibtop_command _cmnd, *cmnd = &_cmnd; + char parameter [BUFSIZ]; + void *ptr; -#include -#include + while (do_read (input, cmnd, sizeof (glibtop_command))) { +#ifdef SLAVE_DEBUG + fprintf (stderr, "Slave %d received command " + "%d from client.\n", getpid (), cmnd->command); +#endif + + if (cmnd->data_size >= BUFSIZ) + glibtop_error ("Client sent %d bytes, " + "but buffer is %d", + cmnd->size, BUFSIZ); -#define _offset_union(p) ((char *) &resp->u.p - (char *) resp) -#define _offset_data(p) _offset_union (data.p) + memset (parameter, 0, sizeof (parameter)); + + if (cmnd->data_size) { +#ifdef SLAVE_DEBUG + fprintf (stderr, "Client has %d bytes of data.\n", + cmnd->data_size); +#endif + + do_read (input, parameter, cmnd->data_size); + + } else if (cmnd->size) { + memcpy (parameter, cmnd->parameter, cmnd->size); + } + + switch (cmnd->command) { + case GLIBTOP_CMND_QUIT: + do_output (output, resp, 0, 0, NULL); + return; +#if GLIBTOP_SUID_PROCLIST + case GLIBTOP_CMND_PROCLIST: + ptr = glibtop_get_proclist_p + (server, &resp->u.data.proclist); + do_output (output, resp, _offset_data (proclist), + resp->u.data.proclist.total, ptr); + glibtop_free_r (server, ptr); + break; +#endif + default: + handle_slave_command (cmnd, resp, parameter); + do_output (output, resp, resp->offset, 0, NULL); + break; + } + } +} void handle_slave_command (glibtop_command *cmnd, glibtop_response *resp, diff --git a/src/server/Makefile.am b/src/server/Makefile.am index 5bab7942..324c6ae4 100644 --- a/src/server/Makefile.am +++ b/src/server/Makefile.am @@ -9,8 +9,8 @@ CFLAGS = -Wall -W @CFLAGS@ bin_PROGRAMS = gtop_server gtop_server_SOURCES = main.c output.c version.c -gtop_server_LDADD = $(top_builddir)/sysdeps/common/libgtop_common.la \ - $(top_builddir)/sysdeps/@sysdeps_dir@/libgtop_sysdeps.la \ - @INTLLIBS@ @LIBSUPPORT@ +gtop_server_LDADD = $(top_builddir)/sysdeps/@sysdeps_dir@/libgtop_sysdeps_suid.la \ + $(top_builddir)/sysdeps/common/libgtop_suid_common.la \ + @LIBSUPPORT@ gtop_server_LDFLAGS = -static diff --git a/src/server/main.c b/src/server/main.c index 3aa7ca70..161e49ca 100644 --- a/src/server/main.c +++ b/src/server/main.c @@ -57,7 +57,7 @@ int main(int argc, char *argv[]) glibtop_version (); - glibtop_open_r (&server, argv [0], 0, 0); + glibtop_open_p (&server, argv [0], 0, 0); if (setreuid (euid, uid)) _exit (1); @@ -128,147 +128,146 @@ int main(int argc, char *argv[]) glibtop_output (sizeof (glibtop_sysdeps), &sysdeps); glibtop_output (0, NULL); break; - case GLIBTOP_CMND_CPU: #if GLIBTOP_SUID_CPU + case GLIBTOP_CMND_CPU: glibtop_get_cpu_p (&server, &data.cpu); -#endif glibtop_output (sizeof (glibtop_cpu), &data.cpu); glibtop_output (0, NULL); break; - case GLIBTOP_CMND_MEM: -#if GLIBTOP_SUID_MEM - glibtop_get_mem_p (&server, &data.mem); #endif +#if GLIBTOP_SUID_MEM + case GLIBTOP_CMND_MEM: + glibtop_get_mem_p (&server, &data.mem); glibtop_output (sizeof (glibtop_mem), &data.mem); glibtop_output (0, NULL); break; - case GLIBTOP_CMND_SWAP: -#if GLIBTOP_SUID_SWAP - glibtop_get_swap_p (&server, &data.swap); #endif +#if GLIBTOP_SUID_SWAP + case GLIBTOP_CMND_SWAP: + glibtop_get_swap_p (&server, &data.swap); glibtop_output (sizeof (glibtop_swap), &data.swap); glibtop_output (0, NULL); break; - case GLIBTOP_CMND_UPTIME: -#if GLIBTOP_SUID_UPTIME - glibtop_get_uptime_p (&server, &data.uptime); #endif +#if GLIBTOP_SUID_UPTIME + case GLIBTOP_CMND_UPTIME: + glibtop_get_uptime_p (&server, &data.uptime); glibtop_output (sizeof (glibtop_uptime), &data.uptime); glibtop_output (0, NULL); break; - case GLIBTOP_CMND_LOADAVG: -#if GLIBTOP_SUID_LOADAVG - glibtop_get_loadavg_p (&server, &data.loadavg); #endif +#if GLIBTOP_SUID_LOADAVG + case GLIBTOP_CMND_LOADAVG: + glibtop_get_loadavg_p (&server, &data.loadavg); glibtop_output (sizeof (glibtop_loadavg), &data.loadavg); glibtop_output (0, NULL); break; - case GLIBTOP_CMND_SHM_LIMITS: -#if GLIBTOP_SUID_SHM_LIMITS - glibtop_get_shm_limits_p (&server, &data.shm_limits); #endif +#if GLIBTOP_SUID_SHM_LIMITS + case GLIBTOP_CMND_SHM_LIMITS: + glibtop_get_shm_limits_p (&server, &data.shm_limits); glibtop_output (sizeof (glibtop_shm_limits), &data.shm_limits); glibtop_output (0, NULL); break; - case GLIBTOP_CMND_MSG_LIMITS: -#if GLIBTOP_SUID_MSG_LIMITS - glibtop_get_msg_limits_p (&server, &data.msg_limits); #endif +#if GLIBTOP_SUID_MSG_LIMITS + case GLIBTOP_CMND_MSG_LIMITS: + glibtop_get_msg_limits_p (&server, &data.msg_limits); glibtop_output (sizeof (glibtop_msg_limits), &data.msg_limits); glibtop_output (0, NULL); break; - case GLIBTOP_CMND_SEM_LIMITS: -#if GLIBTOP_SUID_SEM_LIMITS - glibtop_get_sem_limits_p (&server, &data.sem_limits); #endif +#if GLIBTOP_SUID_SEM_LIMITS + case GLIBTOP_CMND_SEM_LIMITS: + glibtop_get_sem_limits_p (&server, &data.sem_limits); glibtop_output (sizeof (glibtop_sem_limits), &data.sem_limits); glibtop_output (0, NULL); break; - case GLIBTOP_CMND_PROCLIST: -#if GLIBTOP_SUID_PROCLIST - ptr = glibtop_get_proclist_p (&server, &data.proclist); -#else - ptr = NULL; #endif +#if GLIBTOP_SUID_PROCLIST + case GLIBTOP_CMND_PROCLIST: + ptr = glibtop_get_proclist_p (&server, &data.proclist); glibtop_output (sizeof (glibtop_proclist), &data.proclist); glibtop_output (data.proclist.total, ptr); - glibtop_free_r (&server, ptr); + if (ptr) free (ptr); break; +#endif +#if GLIBTOP_SUID_PROC_STATE case GLIBTOP_CMND_PROC_STATE: memcpy (&pid, parameter, sizeof (pid_t)); -#if GLIBTOP_SUID_PROC_STATE glibtop_get_proc_state_p (&server, &data.proc_state, pid); -#endif glibtop_output (sizeof (glibtop_proc_state), &data.proc_state); glibtop_output (0, NULL); break; +#endif +#if GLIBTOP_SUID_PROC_UID case GLIBTOP_CMND_PROC_UID: memcpy (&pid, parameter, sizeof (pid_t)); -#if GLIBTOP_SUID_PROC_UID glibtop_get_proc_uid_p (&server, &data.proc_uid, pid); -#endif glibtop_output (sizeof (glibtop_proc_uid), &data.proc_uid); glibtop_output (0, NULL); break; +#endif +#if GLIBTOP_SUID_PROC_MEM case GLIBTOP_CMND_PROC_MEM: memcpy (&pid, parameter, sizeof (pid_t)); -#if GLIBTOP_SUID_PROC_MEM glibtop_get_proc_mem_p (&server, &data.proc_mem, pid); -#endif glibtop_output (sizeof (glibtop_proc_mem), &data.proc_mem); glibtop_output (0, NULL); break; +#endif +#if GLIBTOP_SUID_PROC_TIME case GLIBTOP_CMND_PROC_TIME: memcpy (&pid, parameter, sizeof (pid_t)); -#if GLIBTOP_SUID_PROC_TIME glibtop_get_proc_time_p (&server, &data.proc_time, pid); -#endif glibtop_output (sizeof (glibtop_proc_time), &data.proc_time); glibtop_output (0, NULL); break; +#endif +#if GLIBTOP_SUID_PROC_SIGNAL case GLIBTOP_CMND_PROC_SIGNAL: memcpy (&pid, parameter, sizeof (pid_t)); -#if GLIBTOP_SUID_PROC_SIGNAL glibtop_get_proc_signal_p (&server, &data.proc_signal, pid); -#endif glibtop_output (sizeof (glibtop_proc_signal), &data.proc_signal); glibtop_output (0, NULL); break; +#endif +#if GLIBTOP_SUID_PROC_KERNEL case GLIBTOP_CMND_PROC_KERNEL: memcpy (&pid, parameter, sizeof (pid_t)); -#if GLIBTOP_SUID_PROC_KERNEL glibtop_get_proc_kernel_p (&server, &data.proc_kernel, pid); -#endif glibtop_output (sizeof (glibtop_proc_kernel), &data.proc_kernel); glibtop_output (0, NULL); break; +#endif +#if GLIBTOP_SUID_PROC_SEGMENT case GLIBTOP_CMND_PROC_SEGMENT: memcpy (&pid, parameter, sizeof (pid_t)); -#if GLIBTOP_SUID_PROC_SEGMENT glibtop_get_proc_segment_p (&server, &data.proc_segment, pid); -#endif glibtop_output (sizeof (glibtop_proc_segment), &data.proc_segment); glibtop_output (0, NULL); break; - +#endif + default: + _exit(1); } } _exit(0); diff --git a/sysdeps/common/Makefile.am b/sysdeps/common/Makefile.am index 763a1316..74f9ee5e 100644 --- a/sysdeps/common/Makefile.am +++ b/sysdeps/common/Makefile.am @@ -6,6 +6,8 @@ INCLUDES = -I$(top_builddir) -I$(top_srcdir) @machine_incs@ \ CFLAGS = -Wall -W @CFLAGS@ -lib_LTLIBRARIES = libgtop_common.la +lib_LTLIBRARIES = libgtop_common.la libgtop_suid_common.la libgtop_common_la_SOURCES = sysdeps.c xmalloc.c error.c gnuslib.c + +libgtop_suid_common_la_SOURCES = xmalloc.c error.c diff --git a/sysdeps/common/xmalloc.c b/sysdeps/common/xmalloc.c index 54b48eaa..9963ae7f 100644 --- a/sysdeps/common/xmalloc.c +++ b/sysdeps/common/xmalloc.c @@ -29,8 +29,7 @@ glibtop_malloc_r (glibtop *server, size_t size) void *buf = malloc (size); if (!buf) - glibtop_error_r (server, _("malloc %d bytes: %s"), - size, strerror (errno)); + glibtop_error_io_r (server, "malloc %d bytes", size); return buf; } @@ -41,8 +40,8 @@ glibtop_calloc_r (glibtop *server, size_t nmemb, size_t size) void *buf = calloc (nmemb, size); if (!buf) - glibtop_error_r (server, _("calloc %d block (%d bytes each): %s"), - nmemb, size, strerror (errno)); + glibtop_error_io_r (server, "calloc %d blocks (%d bytes each)", + nmemb, size); return buf; } @@ -53,8 +52,7 @@ glibtop_realloc_r (glibtop *server, void *ptr, size_t size) void *buf = realloc (ptr, size); if (!buf) - glibtop_error_r (server, _("realloc %d bytes: %s"), - size, strerror (errno)); + glibtop_error_io_r (server, "realloc %d bytes", size); return buf; } diff --git a/sysdeps/kernel/Makefile.am b/sysdeps/kernel/Makefile.am index f1530505..cad0a81e 100644 --- a/sysdeps/kernel/Makefile.am +++ b/sysdeps/kernel/Makefile.am @@ -8,7 +8,7 @@ CFLAGS = -Wall -W @CFLAGS@ lib_LTLIBRARIES = libgtop_sysdeps.la -libgtop_sysdeps_la_SOURCES = init.c open.c close.c kernel.s cpu.c mem.c swap.c \ +libgtop_sysdeps_la_SOURCES = open.c close.c kernel.s cpu.c mem.c swap.c \ uptime.c loadavg.c shm_limits.c msg_limits.c \ sem_limits.c proclist.c procstate.c procuid.c \ proctime.c procmem.c procsignal.c prockernel.c \ diff --git a/sysdeps/kernel/close.c b/sysdeps/kernel/close.c index 02f2e5b3..9506f275 100644 --- a/sysdeps/kernel/close.c +++ b/sysdeps/kernel/close.c @@ -24,5 +24,5 @@ /* Closes pipe to gtop server. */ void -glibtop_close_l (glibtop *server) +glibtop_close_s (glibtop *server) { } diff --git a/sysdeps/kernel/open.c b/sysdeps/kernel/open.c index 9a9b0a7a..cc8c9b2f 100644 --- a/sysdeps/kernel/open.c +++ b/sysdeps/kernel/open.c @@ -51,11 +51,11 @@ static void set_linux_version(void) { /* Opens pipe to gtop server. Returns 0 on success and -1 on error. */ void -glibtop_open_r (glibtop *server, const char *program_name, - const unsigned long features, const unsigned flags) +glibtop_open_s (glibtop *server, const char *program_name, + const unsigned long features, const unsigned flags) { server->name = program_name; - + set_linux_version (); server->os_version_code = (unsigned long) linux_version_code; } diff --git a/sysdeps/linux/close.c b/sysdeps/linux/close.c index 02f2e5b3..9506f275 100644 --- a/sysdeps/linux/close.c +++ b/sysdeps/linux/close.c @@ -24,5 +24,5 @@ /* Closes pipe to gtop server. */ void -glibtop_close_l (glibtop *server) +glibtop_close_s (glibtop *server) { } diff --git a/sysdeps/linux/init.c b/sysdeps/linux/init.c deleted file mode 100644 index 4ccea04f..00000000 --- a/sysdeps/linux/init.c +++ /dev/null @@ -1,44 +0,0 @@ -/* $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 - -static glibtop _glibtop_global_server; -glibtop *glibtop_global_server = NULL; - -glibtop * -glibtop_init_r (glibtop **server, const unsigned long features, - const unsigned flags) -{ - if (*server != NULL) - return *server; - - fprintf (stderr, "DEBUG: %s (%d)\n", __FILE__, __LINE__); - - if (glibtop_global_server == NULL) { - glibtop_global_server = &_glibtop_global_server; - glibtop_open_r (glibtop_global_server, "glibtop", - features, flags); - } - - return *server = glibtop_global_server; -} diff --git a/sysdeps/linux/open.c b/sysdeps/linux/open.c index cf44cc3d..8f59b3fe 100644 --- a/sysdeps/linux/open.c +++ b/sysdeps/linux/open.c @@ -51,7 +51,7 @@ static void set_linux_version(void) { /* Opens pipe to gtop server. Returns 0 on success and -1 on error. */ void -glibtop_open_r (glibtop *server, const char *program_name, +glibtop_open_s (glibtop *server, const char *program_name, const unsigned long features, const unsigned flags) { server->name = program_name; diff --git a/sysdeps/osf1/Makefile.am b/sysdeps/osf1/Makefile.am index 35a64d0a..71623907 100644 --- a/sysdeps/osf1/Makefile.am +++ b/sysdeps/osf1/Makefile.am @@ -6,11 +6,14 @@ INCLUDES = -I$(top_builddir) -I$(top_srcdir) @machine_incs@ \ CFLAGS = -Wall -W @CFLAGS@ -lib_LTLIBRARIES = libgtop_sysdeps.la +lib_LTLIBRARIES = libgtop_sysdeps.la libgtop_sysdeps_suid.la -libgtop_sysdeps_la_SOURCES = init.c open.c close.c cpu.c mem.c swap.c \ +libgtop_sysdeps_la_SOURCES = open.c close.c cpu.c mem.c swap.c \ uptime.c loadavg.c shm_limits.c msg_limits.c \ - sem_limits.c proclist.c procstate.c procuid.c \ - proctime.c procmem.c procsignal.c prockernel.c \ - procsegment.c + sem_limits.c libgtop_sysdeps_la_LIBADD = -lmach + +libgtop_sysdeps_suid_la_SOURCES = open_suid.c close_suid.c proclist.c \ + procstate.c procuid.c proctime.c procmem.c \ + procsignal.c prockernel.c procsegment.c +libgtop_sysdeps_suid_la_LIBADD = -lmach diff --git a/sysdeps/osf1/close.c b/sysdeps/osf1/close.c index ff7ebea4..9506f275 100644 --- a/sysdeps/osf1/close.c +++ b/sysdeps/osf1/close.c @@ -24,5 +24,5 @@ /* Closes pipe to gtop server. */ void -glibtop_close (glibtop *server) +glibtop_close_s (glibtop *server) { } diff --git a/sysdeps/sun4/init.c b/sysdeps/osf1/close_suid.c similarity index 67% rename from sysdeps/sun4/init.c rename to sysdeps/osf1/close_suid.c index 9960bb17..bcee30cf 100644 --- a/sysdeps/sun4/init.c +++ b/sysdeps/osf1/close_suid.c @@ -19,24 +19,10 @@ write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -#include -#include +#include -static glibtop _glibtop_global_server; -glibtop *glibtop_global_server = NULL; +/* Closes pipe to gtop server. */ -glibtop * -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_r (glibtop_global_server, "glibtop", - features, flags); - } - - return *server = glibtop_global_server; -} +void +glibtop_close_p (glibtop *server) +{ } diff --git a/sysdeps/osf1/cpu.c b/sysdeps/osf1/cpu.c index 9cf71117..1258ac17 100644 --- a/sysdeps/osf1/cpu.c +++ b/sysdeps/osf1/cpu.c @@ -36,7 +36,7 @@ glibtop_get_cpu_s (glibtop *server, glibtop_cpu *buf) struct tbl_sysinfo sysinfo; int ret; - glibtop_init (); + glibtop_init_s (&server, 0, 0); memset (buf, 0, sizeof (glibtop_cpu)); diff --git a/sysdeps/osf1/loadavg.c b/sysdeps/osf1/loadavg.c index c7d4dab0..4bbf583f 100644 --- a/sysdeps/osf1/loadavg.c +++ b/sysdeps/osf1/loadavg.c @@ -34,7 +34,7 @@ glibtop_get_loadavg_s (glibtop *server, glibtop_loadavg *buf) struct tbl_loadavg loadavg; int ret; - glibtop_init (); + glibtop_init_s (&server, 0, 0); memset (buf, 0, sizeof (glibtop_loadavg)); diff --git a/sysdeps/osf1/mem.c b/sysdeps/osf1/mem.c index 83045519..edb24ca7 100644 --- a/sysdeps/osf1/mem.c +++ b/sysdeps/osf1/mem.c @@ -40,8 +40,8 @@ glibtop_get_mem_s (glibtop *server, glibtop_mem *buf) { vm_statistics_data_t vmstats; - glibtop_init (); - + glibtop_init_s (&server, 0, 0); + memset (buf, 0, sizeof (glibtop_mem)); buf->flags = _glibtop_sysdeps_mem; diff --git a/sysdeps/osf1/msg_limits.c b/sysdeps/osf1/msg_limits.c index becadb49..e0d33e13 100644 --- a/sysdeps/osf1/msg_limits.c +++ b/sysdeps/osf1/msg_limits.c @@ -35,7 +35,7 @@ glibtop_get_msg_limits_s (glibtop *server, glibtop_msg_limits *buf) { int ret, value; - glibtop_init (); + glibtop_init_s (&server, 0, 0); memset (buf, 0, sizeof (glibtop_msg_limits)); diff --git a/sysdeps/osf1/open.c b/sysdeps/osf1/open.c index 540e499b..0cb4a91b 100644 --- a/sysdeps/osf1/open.c +++ b/sysdeps/osf1/open.c @@ -19,6 +19,7 @@ write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ +#include #include #include @@ -26,39 +27,7 @@ /* Opens pipe to gtop server. Returns 0 on success and -1 on error. */ -/* !!! THIS FUNCTION RUNS SUID ROOT - CHANGE WITH CAUTION !!! */ - void -glibtop_open (glibtop *server, const char *program_name) -{ - /* !!! WE ARE ROOT HERE - CHANGE WITH CAUTION !!! */ - - memset (server, 0, sizeof (glibtop)); - server->name = program_name; - - server->machine.uid = getuid (); - server->machine.euid = geteuid (); - server->machine.gid = getgid (); - server->machine.egid = getegid (); - - server->machine.proctable_entries = table (TBL_PROCINFO, 0, NULL, INT_MAX, 0); - - /* Drop priviledges; we only become root when necessary. - - setreuid (ruid, euid) - set real and effective user id; - setregid (rgid, egid) - set real and effective group id; - - */ - - if (setreuid (server->machine.euid, server->machine.uid)) - _exit (1); - - if (setregid (server->machine.egid, server->machine.gid)) - _exit (1); - - /* !!! END OF SUID ROOT PART !!! */ - - /* Our effective uid is now those of the user invoking the server, - so we do no longer have any priviledges. - */ -} +glibtop_open_s (glibtop *server, const char *program_name, + const unsigned long features, const unsigned flags) +{ } diff --git a/sysdeps/osf1/open_suid.c b/sysdeps/osf1/open_suid.c new file mode 100644 index 00000000..891ab4a2 --- /dev/null +++ b/sysdeps/osf1/open_suid.c @@ -0,0 +1,66 @@ +/* $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 + +#include +#include + +/* Opens pipe to gtop server. Returns 0 on success and -1 on error. */ + +/* !!! THIS FUNCTION RUNS SUID ROOT - CHANGE WITH CAUTION !!! */ + +void +glibtop_open_p (glibtop *server, const char *program_name, + const unsigned long features, const unsigned flags) +{ + /* !!! WE ARE ROOT HERE - CHANGE WITH CAUTION !!! */ + + server->name = program_name; + + server->machine.uid = getuid (); + server->machine.euid = geteuid (); + server->machine.gid = getgid (); + server->machine.egid = getegid (); + + server->machine.proctable_entries = table + (TBL_PROCINFO, 0, NULL, INT_MAX, 0); + + /* Drop priviledges; we only become root when necessary. + + setreuid (ruid, euid) - set real and effective user id; + setregid (rgid, egid) - set real and effective group id; + + */ + + if (setreuid (server->machine.euid, server->machine.uid)) + _exit (1); + + if (setregid (server->machine.egid, server->machine.gid)) + _exit (1); + + /* !!! END OF SUID ROOT PART !!! */ + + /* Our effective uid is now those of the user invoking the server, + so we do no longer have any priviledges. + */ +} diff --git a/sysdeps/osf1/prockernel.c b/sysdeps/osf1/prockernel.c index bd04ca29..9312f924 100644 --- a/sysdeps/osf1/prockernel.c +++ b/sysdeps/osf1/prockernel.c @@ -25,8 +25,8 @@ /* Provides detailed information about a process. */ void -glibtop_get_proc_kernel_r (glibtop *server, glibtop_proc_kernel *buf, - pid_t pid) +glibtop_get_proc_kernel_p (glibtop *server, glibtop_proc_kernel *buf, + pid_t pid) { memset (buf, 0, sizeof (glibtop_proc_kernel)); } diff --git a/sysdeps/osf1/proclist.c b/sysdeps/osf1/proclist.c index d8b0e48d..8dcb8af7 100644 --- a/sysdeps/osf1/proclist.c +++ b/sysdeps/osf1/proclist.c @@ -54,7 +54,7 @@ static const unsigned long _glibtop_sysdeps_proclist = /* !!! THIS FUNCTION RUNS SUID ROOT - CHANGE WITH CAUTION !!! */ unsigned * -glibtop_get_proclist_r (glibtop *server, glibtop_proclist *buf) +glibtop_get_proclist_p (glibtop *server, glibtop_proclist *buf) { unsigned count = 0, total = 0; unsigned pids [BLOCK_COUNT], *pids_chain = NULL; @@ -62,7 +62,7 @@ glibtop_get_proclist_r (glibtop *server, glibtop_proclist *buf) struct tbl_procinfo procinfo [8]; int entry, max_elements, k; - glibtop_init (); + glibtop_open_p (server, 0, 0); memset (buf, 0, sizeof (glibtop_proclist)); diff --git a/sysdeps/osf1/procmem.c b/sysdeps/osf1/procmem.c index bb1e4795..70eadd88 100644 --- a/sysdeps/osf1/procmem.c +++ b/sysdeps/osf1/procmem.c @@ -25,8 +25,8 @@ /* Provides detailed information about a process. */ void -glibtop_get_proc_mem_r (glibtop *server, glibtop_proc_mem *buf, - pid_t pid) +glibtop_get_proc_mem_p (glibtop *server, glibtop_proc_mem *buf, + pid_t pid) { memset (buf, 0, sizeof (glibtop_proc_mem)); } diff --git a/sysdeps/osf1/procsegment.c b/sysdeps/osf1/procsegment.c index 21cd4814..6bc504d1 100644 --- a/sysdeps/osf1/procsegment.c +++ b/sysdeps/osf1/procsegment.c @@ -25,8 +25,8 @@ /* Provides detailed information about a process. */ void -glibtop_get_proc_segment_r (glibtop *server, glibtop_proc_segment *buf, - pid_t pid) +glibtop_get_proc_segment_p (glibtop *server, glibtop_proc_segment *buf, + pid_t pid) { memset (buf, 0, sizeof (glibtop_proc_segment)); } diff --git a/sysdeps/osf1/procsignal.c b/sysdeps/osf1/procsignal.c index d4674f8f..09f4c7d0 100644 --- a/sysdeps/osf1/procsignal.c +++ b/sysdeps/osf1/procsignal.c @@ -25,7 +25,7 @@ /* Provides detailed information about a process. */ void -glibtop_get_proc_signal_r (glibtop *server, glibtop_proc_signal *buf, +glibtop_get_proc_signal_p (glibtop *server, glibtop_proc_signal *buf, pid_t pid) { memset (buf, 0, sizeof (glibtop_proc_signal)); diff --git a/sysdeps/osf1/procstate.c b/sysdeps/osf1/procstate.c index acf32c2e..4a3bd963 100644 --- a/sysdeps/osf1/procstate.c +++ b/sysdeps/osf1/procstate.c @@ -25,8 +25,8 @@ /* Provides detailed information about a process. */ void -glibtop_get_proc_state_r (glibtop *server, glibtop_proc_state *buf, - pid_t pid) +glibtop_get_proc_state_p (glibtop *server, glibtop_proc_state *buf, + pid_t pid) { memset (buf, 0, sizeof (glibtop_proc_state)); } diff --git a/sysdeps/osf1/proctime.c b/sysdeps/osf1/proctime.c index d2417173..c0e6fddc 100644 --- a/sysdeps/osf1/proctime.c +++ b/sysdeps/osf1/proctime.c @@ -25,8 +25,8 @@ /* Provides detailed information about a process. */ void -glibtop_get_proc_time_r (glibtop *server, glibtop_proc_time *buf, - pid_t pid) +glibtop_get_proc_time_p (glibtop *server, glibtop_proc_time *buf, + pid_t pid) { memset (buf, 0, sizeof (glibtop_proc_time)); } diff --git a/sysdeps/osf1/procuid.c b/sysdeps/osf1/procuid.c index 57b49d5f..c289ce54 100644 --- a/sysdeps/osf1/procuid.c +++ b/sysdeps/osf1/procuid.c @@ -39,15 +39,15 @@ static const unsigned long _glibtop_sysdeps_proc_uid = (1 << GLIBTOP_PROC_UID_TTY) + (1 << GLIBTOP_PROC_UID_TPGID); void -glibtop_get_proc_uid_r (glibtop *server, glibtop_proc_uid *buf, - pid_t pid) +glibtop_get_proc_uid_p (glibtop *server, glibtop_proc_uid *buf, + pid_t pid) { struct tbl_procinfo procinfo; task_basic_info_data_t taskinfo; int ret, info_count; task_t thistask; - glibtop_init (); + glibtop_open_p (server, 0, 0); memset (buf, 0, sizeof (glibtop_proc_uid)); diff --git a/sysdeps/osf1/sem_limits.c b/sysdeps/osf1/sem_limits.c index 820bb457..45c6e663 100644 --- a/sysdeps/osf1/sem_limits.c +++ b/sysdeps/osf1/sem_limits.c @@ -35,7 +35,7 @@ glibtop_get_sem_limits_s (glibtop *server, glibtop_sem_limits *buf) { int ret, value; - glibtop_init (); + glibtop_init_s (&server, 0, 0); memset (buf, 0, sizeof (glibtop_sem_limits)); diff --git a/sysdeps/osf1/shm_limits.c b/sysdeps/osf1/shm_limits.c index 990d2cb4..5cb55f23 100644 --- a/sysdeps/osf1/shm_limits.c +++ b/sysdeps/osf1/shm_limits.c @@ -34,7 +34,7 @@ glibtop_get_shm_limits_s (glibtop *server, glibtop_shm_limits *buf) { int ret, value; - glibtop_init (); + glibtop_init_s (&server, 0, 0); memset (buf, 0, sizeof (glibtop_shm_limits)); diff --git a/sysdeps/osf1/swap.c b/sysdeps/osf1/swap.c index 22143db1..5f1b8c6a 100644 --- a/sysdeps/osf1/swap.c +++ b/sysdeps/osf1/swap.c @@ -35,7 +35,7 @@ glibtop_get_swap_s (glibtop *server, glibtop_swap *buf) struct tbl_swapinfo swapinfo; int i; - glibtop_init (); + glibtop_init_s (&server, 0, 0); memset (buf, 0, sizeof (glibtop_swap)); diff --git a/sysdeps/osf1/uptime.c b/sysdeps/osf1/uptime.c index ea64f8c4..d7f9e0af 100644 --- a/sysdeps/osf1/uptime.c +++ b/sysdeps/osf1/uptime.c @@ -36,7 +36,7 @@ glibtop_get_uptime_s (glibtop *server, glibtop_uptime *buf) struct tbl_sysinfo sysinfo; int ret; - glibtop_init (); + glibtop_init_s (&server, 0, 0); memset (buf, 0, sizeof (glibtop_uptime)); diff --git a/sysdeps/stub/Makefile.am b/sysdeps/stub/Makefile.am index 2545ce47..cb8a7e0b 100644 --- a/sysdeps/stub/Makefile.am +++ b/sysdeps/stub/Makefile.am @@ -8,7 +8,7 @@ CFLAGS = -Wall -W @CFLAGS@ lib_LTLIBRARIES = libgtop_sysdeps.la -libgtop_sysdeps_la_SOURCES = init.c open.c close.c cpu.c mem.c swap.c \ +libgtop_sysdeps_la_SOURCES = open.c close.c cpu.c mem.c swap.c \ uptime.c loadavg.c shm_limits.c msg_limits.c \ sem_limits.c proclist.c procstate.c procuid.c \ proctime.c procmem.c procsignal.c prockernel.c \ diff --git a/sysdeps/stub/close.c b/sysdeps/stub/close.c index 02f2e5b3..9506f275 100644 --- a/sysdeps/stub/close.c +++ b/sysdeps/stub/close.c @@ -24,5 +24,5 @@ /* Closes pipe to gtop server. */ void -glibtop_close_l (glibtop *server) +glibtop_close_s (glibtop *server) { } diff --git a/sysdeps/stub/init.c b/sysdeps/stub/init.c deleted file mode 100644 index 9960bb17..00000000 --- a/sysdeps/stub/init.c +++ /dev/null @@ -1,42 +0,0 @@ -/* $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 - -static glibtop _glibtop_global_server; -glibtop *glibtop_global_server = NULL; - -glibtop * -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_r (glibtop_global_server, "glibtop", - features, flags); - } - - return *server = glibtop_global_server; -} diff --git a/sysdeps/stub/open.c b/sysdeps/stub/open.c index a45a6a60..a1ae1c5d 100644 --- a/sysdeps/stub/open.c +++ b/sysdeps/stub/open.c @@ -24,7 +24,7 @@ /* Opens pipe to gtop server. Returns 0 on success and -1 on error. */ void -glibtop_open_r (glibtop *server, const char *program_name, +glibtop_open_s (glibtop *server, const char *program_name, const unsigned long features, const unsigned flags) { server->name = program_name; diff --git a/sysdeps/sun4/Makefile.am b/sysdeps/sun4/Makefile.am index cc5126d7..09e3e6f6 100644 --- a/sysdeps/sun4/Makefile.am +++ b/sysdeps/sun4/Makefile.am @@ -6,11 +6,14 @@ INCLUDES = -I$(top_builddir) -I$(top_srcdir) @machine_incs@ \ CFLAGS = -Wall -W @CFLAGS@ -lib_LTLIBRARIES = libgtop_sysdeps.la +lib_LTLIBRARIES = libgtop_sysdeps.la libgtop_sysdeps_suid.la -libgtop_sysdeps_la_SOURCES = init.c open.c close.c cpu.c mem.c swap.c \ +libgtop_sysdeps_la_SOURCES = nosuid.c +libgtop_sysdeps_la_LIBADD = -lkvm + +libgtop_sysdeps_suid_la_SOURCES = open.c close.c cpu.c mem.c swap.c \ uptime.c loadavg.c shm_limits.c msg_limits.c \ sem_limits.c proclist.c procstate.c procuid.c \ proctime.c procmem.c procsignal.c prockernel.c \ procsegment.c -libgtop_sysdeps_la_LIBADD = -lkvm +libgtop_sysdeps_suid_la_LIBADD = -lkvm diff --git a/sysdeps/sun4/close.c b/sysdeps/sun4/close.c index 02f2e5b3..bcee30cf 100644 --- a/sysdeps/sun4/close.c +++ b/sysdeps/sun4/close.c @@ -24,5 +24,5 @@ /* Closes pipe to gtop server. */ void -glibtop_close_l (glibtop *server) +glibtop_close_p (glibtop *server) { } diff --git a/sysdeps/sun4/cpu.c b/sysdeps/sun4/cpu.c index 7fd278c5..a51bedfd 100644 --- a/sysdeps/sun4/cpu.c +++ b/sysdeps/sun4/cpu.c @@ -35,7 +35,7 @@ glibtop_get_cpu_p (glibtop *server, glibtop_cpu *buf) long cp_time [CPUSTATES], mp_time [NCPU][CPUSTATES]; int i; - glibtop_init_r (&server, 0, 0); + glibtop_init_p (&server, 0, 0); memset (buf, 0, sizeof (glibtop_cpu)); diff --git a/sysdeps/sun4/loadavg.c b/sysdeps/sun4/loadavg.c index df1a0dc6..61bb17a7 100644 --- a/sysdeps/sun4/loadavg.c +++ b/sysdeps/sun4/loadavg.c @@ -33,7 +33,7 @@ glibtop_get_loadavg_p (glibtop *server, glibtop_loadavg *buf) load_avg avenrun [3]; int i; - glibtop_init_r (&server, 0, 0); + glibtop_init_p (&server, 0, 0); memset (buf, 0, sizeof (glibtop_loadavg)); diff --git a/sysdeps/sun4/mem.c b/sysdeps/sun4/mem.c index 0eb195b5..1d5c3738 100644 --- a/sysdeps/sun4/mem.c +++ b/sysdeps/sun4/mem.c @@ -35,7 +35,7 @@ static const unsigned long _glibtop_sysdeps_mem = void glibtop_get_mem_p (glibtop *server, glibtop_mem *buf) { - glibtop_init_r (&server, 0, 0); + glibtop_init_p (&server, 0, 0); memset (buf, 0, sizeof (glibtop_mem)); diff --git a/sysdeps/sun4/msg_limits.c b/sysdeps/sun4/msg_limits.c index 504cc389..11474a7a 100644 --- a/sysdeps/sun4/msg_limits.c +++ b/sysdeps/sun4/msg_limits.c @@ -41,7 +41,7 @@ glibtop_get_msg_limits_p (glibtop *server, glibtop_msg_limits *buf) { struct msginfo msginfo; - glibtop_init_r (&server, 0, 0); + glibtop_init_p (&server, 0, 0); memset (buf, 0, sizeof (glibtop_msg_limits)); diff --git a/sysdeps/osf1/init.c b/sysdeps/sun4/nosuid.c similarity index 74% rename from sysdeps/osf1/init.c rename to sysdeps/sun4/nosuid.c index 43f7469c..e43e5c9f 100644 --- a/sysdeps/osf1/init.c +++ b/sysdeps/sun4/nosuid.c @@ -22,19 +22,11 @@ #include #include -static glibtop _glibtop_global_server; -glibtop *glibtop_global_server = NULL; +void +glibtop_open_s (glibtop *server, const char *program_name, + const unsigned long features, const unsigned flags) +{ } -glibtop * -glibtop_init_r (glibtop **server) -{ - if (*server != NULL) - return *server; - - if (glibtop_global_server == NULL) { - glibtop_global_server = &_glibtop_global_server; - glibtop_open (glibtop_global_server, "glibtop"); - } - - return *server = glibtop_global_server; -} +void +glibtop_close_s (glibtop *server) +{ } diff --git a/sysdeps/sun4/open.c b/sysdeps/sun4/open.c index f97fec38..507461b7 100644 --- a/sysdeps/sun4/open.c +++ b/sysdeps/sun4/open.c @@ -63,8 +63,18 @@ struct nlist _glibtop_nlist[] = { /* !!! THIS FUNCTION RUNS SUID ROOT - CHANGE WITH CAUTION !!! */ void -glibtop_open_r (glibtop *server, const char *program_name, - const unsigned long features, const unsigned flags) +glibtop_init_p (glibtop *server, const char *program_name, + const unsigned long features, const unsigned flags) +{ + if (server == NULL) + glibtop_error_r (NULL, "glibtop_init_p (server == NULL)"); + + glibtop_open_p (server, program_name, features, flags); +} + +void +glibtop_open_p (glibtop *server, const char *program_name, + const unsigned long features, const unsigned flags) { register int pagesize; @@ -82,22 +92,25 @@ glibtop_open_r (glibtop *server, const char *program_name, server->machine.kd = kvm_open (NULL, NULL, NULL, O_RDONLY, "libgtop"); if (server->machine.kd == NULL) - glibtop_error_r (server, "kvm_open: %s", strerror (errno)); + glibtop_error_io_r (server, "kvm_open"); /* get the list of symbols we want to access in the kernel */ - server->machine.nlist_count = kvm_nlist (server->machine.kd, _glibtop_nlist); + server->machine.nlist_count = kvm_nlist + (server->machine.kd, _glibtop_nlist); if (server->machine.nlist_count < 0) - glibtop_error_r (server, "nlist: %s", strerror (errno)); + glibtop_error_io_r (server, "nlist"); #ifdef MULTIPROCESSOR /* were ncpu and xp_time not found in the nlist? */ - if ((server->machine.nlist_count > 0) && (_glibtop_nlist[X_NCPU].n_type == 0) && + if ((server->machine.nlist_count > 0) && + (_glibtop_nlist[X_NCPU].n_type == 0) && (_glibtop_nlist[X_MP_TIME].n_type == 0)) { - /* we were compiled on an MP system but we are not running on one */ - /* so we will pretend this didn't happen and set ncpu = 1 */ + /* we were compiled on an MP system but we are not running + * on one, so we will pretend this didn't happen and set + * ncpu = 1 */ server->machine.nlist_count -= 2; server->machine.ncpu = 1; } @@ -108,7 +121,8 @@ glibtop_open_r (glibtop *server, const char *program_name, unsigned int status, type; /* Get the number of CPUs on this system. */ - syscall(SYS_getcpustatus, &status, &server->machine.ncpu, &type); + syscall(SYS_getcpustatus, &status, + &server->machine.ncpu, &type); } #endif @@ -181,16 +195,17 @@ glibtop_open_r (glibtop *server, const char *program_name, /* !!! END OF SUID ROOT PART !!! */ /* Our effective uid is now those of the user invoking the server, - so we do no longer have any priviledges. - */ + * so we do no longer have any priviledges. */ - /* NOTE: On SunOS, we do not need to be suid root, we just need to be sgid kmem. - * The server will only use setegid() to get back it's priviledges, so it - * will fail if it is suid root and not sgid kmem. - */ + /* NOTE: On SunOS, we do not need to be suid root, we just need to + * be sgid kmem. + * + * The server will only use setegid() to get back it's priviledges, + * so it will fail if it is suid root and not sgid kmem. */ } -/* Used internally. Returns number of symbols that cannot be found in the nlist. */ +/* Used internally. Returns number of symbols that cannot be found in + * the nlist. */ int _glibtop_check_nlist (void *server, register struct nlist *nlst) @@ -206,12 +221,16 @@ _glibtop_check_nlist (void *server, register struct nlist *nlst) #ifdef i386 if (nlst->n_value == 0) { - glibtop_error_r (server, "kernel: no symbol named `%s'", nlst->n_name); + glibtop_error_r (server, + "kernel: no symbol named `%s'", + nlst->n_name); not_found++; } #else if (nlst->n_type == 0) { - glibtop_error_r (server, "kernel: no symbol named `%s'", nlst->n_name); + glibtop_error_r (server, + "kernel: no symbol named `%s'", + nlst->n_name); not_found++; } #endif @@ -225,18 +244,19 @@ _glibtop_check_nlist (void *server, register struct nlist *nlst) /* Used internally. Fetches value from kernel. */ int -_glibtop_getkval (void *void_server, unsigned long offset, int *ptr, int size, char *refstr) +_glibtop_getkval (void *void_server, unsigned long offset, int *ptr, + int size, char *refstr) { glibtop *server = (glibtop *) void_server; if (kvm_read (server->machine.kd, offset, ptr, size) != size) { if (*refstr == '!') return 0; - + glibtop_error_r (server, "kvm_read(%s): %s", - refstr, strerror (errno)); + refstr, strerror (errno)); } - + return 1; } diff --git a/sysdeps/sun4/prockernel.c b/sysdeps/sun4/prockernel.c index 5f13b69f..97ba82a0 100644 --- a/sysdeps/sun4/prockernel.c +++ b/sysdeps/sun4/prockernel.c @@ -33,7 +33,7 @@ glibtop_get_proc_kernel_p (glibtop *server, glibtop_proc_kernel *buf, { struct proc *pp; - glibtop_init_r (&server, 0, 0); + glibtop_init_p (&server, 0, 0); memset (buf, 0, sizeof (glibtop_proc_kernel)); diff --git a/sysdeps/sun4/proclist.c b/sysdeps/sun4/proclist.c index bc8dfebc..4241d94c 100644 --- a/sysdeps/sun4/proclist.c +++ b/sysdeps/sun4/proclist.c @@ -42,7 +42,7 @@ glibtop_get_proclist_p (glibtop *server, glibtop_proclist *buf) unsigned *proc_list = NULL; size_t proc_size; - glibtop_init_r (&server, 0, 0); + glibtop_init_p (&server, 0, 0); memset (buf, 0, sizeof (glibtop_proclist)); diff --git a/sysdeps/sun4/procmem.c b/sysdeps/sun4/procmem.c index 256fa98c..edb4378e 100644 --- a/sysdeps/sun4/procmem.c +++ b/sysdeps/sun4/procmem.c @@ -34,7 +34,7 @@ glibtop_get_proc_mem_p (glibtop *server, glibtop_proc_mem *buf, { struct proc *pp; - glibtop_init_r (&server, 0, 0); + glibtop_init_p (&server, 0, 0); memset (buf, 0, sizeof (glibtop_proc_mem)); diff --git a/sysdeps/sun4/procsegment.c b/sysdeps/sun4/procsegment.c index 8df0f083..de6ba44c 100644 --- a/sysdeps/sun4/procsegment.c +++ b/sysdeps/sun4/procsegment.c @@ -33,7 +33,7 @@ glibtop_get_proc_segment_p (glibtop *server, glibtop_proc_segment *buf, { struct proc *pp; - glibtop_init_r (&server, 0, 0); + glibtop_init_p (&server, 0, 0); memset (buf, 0, sizeof (glibtop_proc_segment)); diff --git a/sysdeps/sun4/procsignal.c b/sysdeps/sun4/procsignal.c index ff256200..7726427f 100644 --- a/sysdeps/sun4/procsignal.c +++ b/sysdeps/sun4/procsignal.c @@ -34,7 +34,7 @@ glibtop_get_proc_signal_p (glibtop *server, glibtop_proc_signal *buf, { struct proc *pp; - glibtop_init_r (&server, 0, 0); + glibtop_init_p (&server, 0, 0); memset (buf, 0, sizeof (glibtop_proc_signal)); diff --git a/sysdeps/sun4/procstate.c b/sysdeps/sun4/procstate.c index d3066a9c..ee99b5be 100644 --- a/sysdeps/sun4/procstate.c +++ b/sysdeps/sun4/procstate.c @@ -33,7 +33,7 @@ glibtop_get_proc_state_p (glibtop *server, glibtop_proc_state *buf, { struct proc *pp; - glibtop_init_r (&server, 0, 0); + glibtop_init_p (&server, 0, 0); memset (buf, 0, sizeof (glibtop_proc_state)); diff --git a/sysdeps/sun4/proctime.c b/sysdeps/sun4/proctime.c index 89673e11..7bc4eb4c 100644 --- a/sysdeps/sun4/proctime.c +++ b/sysdeps/sun4/proctime.c @@ -33,7 +33,7 @@ glibtop_get_proc_time_p (glibtop *server, glibtop_proc_time *buf, { struct proc *pp; - glibtop_init_r (&server, 0, 0); + glibtop_init_p (&server, 0, 0); memset (buf, 0, sizeof (glibtop_proc_time)); diff --git a/sysdeps/sun4/procuid.c b/sysdeps/sun4/procuid.c index f30142b1..cd3525dc 100644 --- a/sysdeps/sun4/procuid.c +++ b/sysdeps/sun4/procuid.c @@ -37,7 +37,7 @@ glibtop_get_proc_uid_p (glibtop *server, glibtop_proc_uid *buf, { struct proc *pp; - glibtop_init_r (&server, 0, 0); + glibtop_init_p (&server, 0, 0); memset (buf, 0, sizeof (glibtop_proc_uid)); diff --git a/sysdeps/sun4/sem_limits.c b/sysdeps/sun4/sem_limits.c index ef8aa1e8..768d001e 100644 --- a/sysdeps/sun4/sem_limits.c +++ b/sysdeps/sun4/sem_limits.c @@ -43,7 +43,7 @@ glibtop_get_sem_limits_p (glibtop *server, glibtop_sem_limits *buf) { struct seminfo seminfo; - glibtop_init_r (&server, 0, 0); + glibtop_init_p (&server, 0, 0); memset (buf, 0, sizeof (glibtop_sem_limits)); diff --git a/sysdeps/sun4/shm_limits.c b/sysdeps/sun4/shm_limits.c index f43cef8b..c6ff9a7b 100644 --- a/sysdeps/sun4/shm_limits.c +++ b/sysdeps/sun4/shm_limits.c @@ -41,7 +41,7 @@ glibtop_get_shm_limits_p (glibtop *server, glibtop_shm_limits *buf) { struct shminfo shminfo; - glibtop_init_r (&server, 0, 0); + glibtop_init_p (&server, 0, 0); memset (buf, 0, sizeof (glibtop_shm_limits)); diff --git a/sysdeps/sun4/swap.c b/sysdeps/sun4/swap.c index 853e9477..3b43245b 100644 --- a/sysdeps/sun4/swap.c +++ b/sysdeps/sun4/swap.c @@ -27,7 +27,7 @@ void glibtop_get_swap_p (glibtop *server, glibtop_swap *buf) { - glibtop_init_r (&server, 0, 0); + glibtop_init_p (&server, 0, 0); memset (buf, 0, sizeof (glibtop_swap)); } diff --git a/sysdeps/sun4/uptime.c b/sysdeps/sun4/uptime.c index 0a3827fb..104d54ad 100644 --- a/sysdeps/sun4/uptime.c +++ b/sysdeps/sun4/uptime.c @@ -33,7 +33,7 @@ glibtop_get_uptime_p (glibtop *server, glibtop_uptime *buf) { glibtop_cpu cpu; - glibtop_init_r (&server, 0, 0); + glibtop_init_p (&server, 0, 0); /* Get currect cpu usage. */