|
Packit Service |
407539 |
/* Copyright (C) 1998 Joshua Sled
|
|
Packit Service |
407539 |
This file is part of LibGTop 1.0.
|
|
Packit Service |
407539 |
|
|
Packit Service |
407539 |
Contributed by Joshua Sled <jsled@xcf.berkeley.edu>, July 1998.
|
|
Packit Service |
407539 |
|
|
Packit Service |
407539 |
LibGTop is free software; you can redistribute it and/or modify it
|
|
Packit Service |
407539 |
under the terms of the GNU General Public License as published by
|
|
Packit Service |
407539 |
the Free Software Foundation; either version 2 of the License,
|
|
Packit Service |
407539 |
or (at your option) any later version.
|
|
Packit Service |
407539 |
|
|
Packit Service |
407539 |
LibGTop is distributed in the hope that it will be useful, but WITHOUT
|
|
Packit Service |
407539 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
Packit Service |
407539 |
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
Packit Service |
407539 |
for more details.
|
|
Packit Service |
407539 |
|
|
Packit Service |
407539 |
You should have received a copy of the GNU General Public License
|
|
Packit Service |
407539 |
along with LibGTop; see the file COPYING. If not, write to the
|
|
Packit Service |
407539 |
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
Packit Service |
407539 |
Boston, MA 02110-1301, USA.
|
|
Packit Service |
407539 |
*/
|
|
Packit Service |
407539 |
|
|
Packit Service |
407539 |
#include <config.h>
|
|
Packit Service |
407539 |
#include <glibtop.h>
|
|
Packit Service |
407539 |
#include <glibtop/error.h>
|
|
Packit Service |
407539 |
#include <glibtop/cpu.h>
|
|
Packit Service |
407539 |
#include <glibtop/open.h>
|
|
Packit Service |
407539 |
#include <glibtop/init_hooks.h>
|
|
Packit Service |
407539 |
#include <glibtop/machine.h>
|
|
Packit Service |
407539 |
|
|
Packit Service |
407539 |
|
|
Packit Service |
407539 |
/* !!! THIS FUNCTION RUNS SUID ROOT - CHANGE WITH CAUTION !!! */
|
|
Packit Service |
407539 |
|
|
Packit Service |
407539 |
void
|
|
Packit Service |
407539 |
glibtop_init_p (glibtop *server, const unsigned long features,
|
|
Packit Service |
407539 |
const unsigned flags)
|
|
Packit Service |
407539 |
{
|
|
Packit Service |
407539 |
const _glibtop_init_func_t *init_fkt;
|
|
Packit Service |
407539 |
|
|
Packit Service |
407539 |
if (server == NULL)
|
|
Packit Service |
407539 |
glibtop_error_r (NULL, "glibtop_init_p (server == NULL)");
|
|
Packit Service |
407539 |
|
|
Packit Service |
407539 |
/* Do the initialization, but only if not already initialized. */
|
|
Packit Service |
407539 |
|
|
Packit Service |
407539 |
if ((server->flags & _GLIBTOP_INIT_STATE_SYSDEPS) == 0) {
|
|
Packit Service |
407539 |
glibtop_open_p (server, "glibtop", features, flags);
|
|
Packit Service |
407539 |
|
|
Packit Service |
407539 |
for (init_fkt = _glibtop_init_hook_p; *init_fkt; init_fkt++)
|
|
Packit Service |
407539 |
(*init_fkt) (server);
|
|
Packit Service |
407539 |
|
|
Packit Service |
407539 |
server->flags |= _GLIBTOP_INIT_STATE_SYSDEPS;
|
|
Packit Service |
407539 |
}
|
|
Packit Service |
407539 |
}
|
|
Packit Service |
407539 |
|
|
Packit Service |
407539 |
void
|
|
Packit Service |
407539 |
glibtop_open_p (glibtop *server, const char *program_name,
|
|
Packit Service |
407539 |
const unsigned long features,
|
|
Packit Service |
407539 |
const unsigned flags)
|
|
Packit Service |
407539 |
{
|
|
Packit Service |
407539 |
char errbuf[_POSIX2_LINE_MAX];
|
|
Packit Service |
407539 |
glibtop_debug ("glibtop_open_p ()");
|
|
Packit Service |
407539 |
|
|
Packit Service |
407539 |
/* !!! WE ARE ROOT HERE - CHANGE WITH CAUTION !!! */
|
|
Packit Service |
407539 |
server->machine->uid = getuid ();
|
|
Packit Service |
407539 |
server->machine->euid = geteuid ();
|
|
Packit Service |
407539 |
server->machine->gid = getgid ();
|
|
Packit Service |
407539 |
server->machine->egid = getegid ();
|
|
Packit Service |
407539 |
/* Setup machine-specific data */
|
|
Packit Service |
407539 |
server->machine->kd = kvm_openfiles (NULL, NULL, NULL, O_RDONLY, errbuf);
|
|
Packit Service |
407539 |
|
|
Packit Service |
407539 |
if (server->machine->kd == NULL)
|
|
Packit Service |
407539 |
glibtop_error_io_r (server, "kvm_open");
|
|
Packit Service |
407539 |
|
|
Packit Service |
407539 |
/* Drop priviledges. */
|
|
Packit Service |
407539 |
|
|
Packit Service |
407539 |
glibtop_debug ("uid=%d euid=%d gid=%d egid=%d", getuid(), geteuid(), getgid(), getegid());
|
|
Packit Service |
407539 |
|
|
Packit Service |
407539 |
if (setreuid (server->machine->euid, server->machine->uid))
|
|
Packit Service |
407539 |
_exit (1);
|
|
Packit Service |
407539 |
|
|
Packit Service |
407539 |
if (setregid (server->machine->egid, server->machine->gid))
|
|
Packit Service |
407539 |
_exit (1);
|
|
Packit Service |
407539 |
|
|
Packit Service |
407539 |
glibtop_debug ("uid=%d euid=%d gid=%d egid=%d", getuid(), geteuid(), getgid(), getegid());
|
|
Packit Service |
407539 |
|
|
Packit Service |
407539 |
/* !!! END OF SUID ROOT PART !!! */
|
|
Packit Service |
407539 |
|
|
Packit Service |
407539 |
/* Our effective uid is now those of the user invoking the server,
|
|
Packit Service |
407539 |
* so we do no longer have any priviledges. */
|
|
Packit Service |
407539 |
|
|
Packit Service |
407539 |
/* NOTE: On FreeBSD, we do not need to be suid root, we just need to
|
|
Packit Service |
407539 |
* be sgid kmem.
|
|
Packit Service |
407539 |
*
|
|
Packit Service |
407539 |
* The server will only use setegid() to get back it's priviledges,
|
|
Packit Service |
407539 |
* so it will fail if it is suid root and not sgid kmem. */
|
|
Packit Service |
407539 |
}
|