Files
libgtop/sysdeps/sun4/open.c
Martin Baulig 5e97af535f 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/<sysdeps_dir>/) 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/<sysdeps_dir>/) 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  <martin@home-of-linux.org>

	* 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-22 09:26:43 +00:00

305 lines
7.9 KiB
C

/* $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/open.h>
#include <glibtop/xmalloc.h>
struct nlist _glibtop_nlist[] = {
#ifdef i386
{ "avenrun" }, /* 0 */
{ "ccpu" }, /* 1 */
{ "mpid" }, /* 2 */
{ "nproc" }, /* 3 */
{ "proc" }, /* 4 */
{ "total" }, /* 5 */
{ "cp_time" }, /* 6 */
{ "pages" }, /* 7 */
{ "epages" }, /* 8 */
{ "shminfo" }, /* 9 */
{ "msginfo" }, /* 10 */
{ "seminfo" }, /* 11 */
#else
{ "_avenrun" }, /* 0 */
{ "_ccpu" }, /* 1 */
{ "_mpid" }, /* 2 */
{ "_nproc" }, /* 3 */
{ "_proc" }, /* 4 */
{ "_total" }, /* 5 */
{ "_cp_time" }, /* 6 */
{ "_pages" }, /* 7 */
{ "_epages" }, /* 8 */
{ "_shminfo" }, /* 9 */
{ "_msginfo" }, /* 10 */
{ "_seminfo" }, /* 11 */
#ifdef MULTIPROCESSOR
{ "_ncpu" },
{ "_mp_time" },
#endif
#endif
{ 0 }
};
/* Opens pipe to gtop server. Returns 0 on success and -1 on error. */
/* !!! THIS FUNCTION RUNS SUID ROOT - CHANGE WITH CAUTION !!! */
void
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;
/* !!! 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 ();
/* initialize the kernel interface */
server->machine.kd = kvm_open (NULL, NULL, NULL, O_RDONLY, "libgtop");
if (server->machine.kd == NULL)
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);
if (server->machine.nlist_count < 0)
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) &&
(_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 */
server->machine.nlist_count -= 2;
server->machine.ncpu = 1;
}
#endif
#ifdef solbourne
{
unsigned int status, type;
/* Get the number of CPUs on this system. */
syscall(SYS_getcpustatus, &status,
&server->machine.ncpu, &type);
}
#endif
/* Make sure all of the symbols were found. */
if ((server->machine.nlist_count > 0) &&
(_glibtop_check_nlist (server, _glibtop_nlist) > 0))
_exit (1);
/* Get process array stuff. */
(void) _glibtop_getkval (server, _glibtop_nlist[X_NPROC].n_value,
(int *)(&server->machine.nproc),
sizeof (server->machine.nproc),
_glibtop_nlist[X_NPROC].n_name);
(void) _glibtop_getkval (server, _glibtop_nlist[X_PROC].n_value,
(int *)(&server->machine.ptable_offset),
sizeof (server->machine.ptable_offset),
_glibtop_nlist[X_PROC].n_name);
server->machine.ptable_size = (unsigned long) server->machine.nproc *
(unsigned long) sizeof (struct proc);
server->machine.proc_table = glibtop_malloc_r
(server, server->machine.ptable_size);
/* This are for the memory statistics. */
(void) _glibtop_getkval (server, _glibtop_nlist[X_PAGES].n_value,
(int *)(&server->machine.pages),
sizeof (server->machine.pages),
_glibtop_nlist[X_PAGES].n_name);
(void) _glibtop_getkval (server, _glibtop_nlist[X_EPAGES].n_value,
(int *)(&server->machine.epages),
sizeof (server->machine.epages),
_glibtop_nlist[X_EPAGES].n_name);
server->machine.bytesize = server->machine.epages -
server->machine.pages;
server->machine.count = server->machine.bytesize / sizeof (struct page);
server->machine.physpage =
(struct page *) glibtop_malloc_r (server, server->machine.bytesize);
/* get the page size with "getpagesize" and calculate pageshift from it */
pagesize = getpagesize();
server->machine.pageshift = 0;
while (pagesize > 1) {
server->machine.pageshift++;
pagesize >>= 1;
}
/* we only need the amount of log(2)1024 for our conversion */
server->machine.pageshift -= LOG1024;
/* Drop priviledges. */
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. */
/* 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. */
int
_glibtop_check_nlist (void *server, register struct nlist *nlst)
{
register int not_found;
/* check to see if we got ALL the symbols we requested */
/* this will write one line to stderr for every symbol not found */
not_found = 0;
while (nlst->n_name != NULL) {
#ifdef i386
if (nlst->n_value == 0) {
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);
not_found++;
}
#endif
nlst++;
}
return not_found;
}
/* Used internally. Fetches value from kernel. */
int
_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));
}
return 1;
}
/* Used internally. Reads process table from kernel. */
void
_glibtop_read_proc_table (void *void_server)
{
glibtop *server = (glibtop *) void_server;
/* !!! THE FOLLOWING CODE RUNS SGID KMEM - CHANGE WITH CAUTION !!! */
setregid (server->machine.gid, server->machine.egid);
/* Read process table from kernel. */
(void) _glibtop_getkval (server, server->machine.ptable_offset,
(int *) server->machine.proc_table,
(size_t) server->machine.ptable_size,
_glibtop_nlist[X_PROC].n_name);
if (setregid (server->machine.egid, server->machine.gid))
_exit (1);
/* !!! END OF SGID KMEM PART !!! */
}
/* Used internally. Finds pid in process table. */
struct proc *
_glibtop_find_pid (void *void_server, pid_t pid)
{
register struct proc *pp;
register int i;
glibtop *server = (glibtop *) void_server;
for (pp = server->machine.proc_table, i = 0;
i < server->machine.nproc; pp++, i++) {
if ((pp->p_stat != 0) && (pp->p_pid == pid))
return pp;
}
return NULL;
}