|
Packit |
d37888 |
/*
|
|
Packit |
d37888 |
This file is part of LibGTop 2.0.
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
LibGTop is free software; you can redistribute it and/or modify it
|
|
Packit |
d37888 |
under the terms of the GNU General Public License as published by
|
|
Packit |
d37888 |
the Free Software Foundation; either version 2 of the License,
|
|
Packit |
d37888 |
or (at your option) any later version.
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
LibGTop is distributed in the hope that it will be useful, but WITHOUT
|
|
Packit |
d37888 |
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
Packit |
d37888 |
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
Packit |
d37888 |
for more details.
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
You should have received a copy of the GNU General Public License
|
|
Packit |
d37888 |
along with LibGTop; see the file COPYING. If not, write to the
|
|
Packit |
d37888 |
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
Packit |
d37888 |
Boston, MA 02110-1301, USA.
|
|
Packit |
d37888 |
*/
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
#include <config.h>
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
#include <glibtop.h>
|
|
Packit |
d37888 |
#include <glibtop/cpu.h>
|
|
Packit |
d37888 |
#include <glibtop/open.h>
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
#include <glibtop_machine.h>
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
#include <mach/mach_host.h>
|
|
Packit |
d37888 |
#include <mach/mach_init.h>
|
|
Packit |
d37888 |
#include <mach/mach_interface.h>
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
#include <glibtop/init_hooks.h>
|
|
Packit |
d37888 |
#include <glibtop/error.h>
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
#include <unistd.h>
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
/* !!! THIS FUNCTION RUNS SUID ROOT - CHANGE WITH CAUTION !!! */
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
void
|
|
Packit |
d37888 |
glibtop_init_p (glibtop *server, const unsigned long features,
|
|
Packit |
d37888 |
const unsigned flags)
|
|
Packit |
d37888 |
{
|
|
Packit |
d37888 |
const _glibtop_init_func_t *init_fkt;
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
if (server == NULL)
|
|
Packit |
d37888 |
glibtop_error_r (NULL, "glibtop_init_p (server == NULL)");
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
/* Do the initialization, but only if not already initialized. */
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
if ((server->flags & _GLIBTOP_INIT_STATE_INIT) == 0) {
|
|
Packit |
d37888 |
glibtop_open_p (server, "glibtop", features, flags);
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
for (init_fkt = _glibtop_init_hook_p; *init_fkt; init_fkt++)
|
|
Packit |
d37888 |
(*init_fkt) (server);
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
server->flags |= _GLIBTOP_INIT_STATE_INIT;
|
|
Packit |
d37888 |
}
|
|
Packit |
d37888 |
}
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
void
|
|
Packit |
d37888 |
glibtop_open_p (glibtop *server, const char *program_name,
|
|
Packit |
d37888 |
const unsigned long features, const unsigned flags)
|
|
Packit |
d37888 |
{
|
|
Packit |
d37888 |
processor_cpu_load_info_data_t *pinfo;
|
|
Packit |
d37888 |
mach_msg_type_number_t info_count;
|
|
Packit |
d37888 |
natural_t processor_count;
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
/* !!! WE ARE ROOT HERE - CHANGE WITH CAUTION !!! */
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
server->name = program_name;
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
server->machine->uid = getuid ();
|
|
Packit |
d37888 |
server->machine->euid = geteuid ();
|
|
Packit |
d37888 |
server->machine->gid = getgid ();
|
|
Packit |
d37888 |
server->machine->egid = getegid ();
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
/* Drop priviledges. */
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
if (setreuid (server->machine->euid, server->machine->uid))
|
|
Packit |
d37888 |
_exit (1);
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
if (setregid (server->machine->egid, server->machine->gid))
|
|
Packit |
d37888 |
_exit (1);
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
/* !!! END OF SUID ROOT PART !!! */
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
/* Our effective uid is now those of the user invoking the server,
|
|
Packit |
d37888 |
* so we do no longer have any priviledges. */
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
if (host_processor_info (mach_host_self (),
|
|
Packit |
d37888 |
PROCESSOR_CPU_LOAD_INFO,
|
|
Packit |
d37888 |
&processor_count,
|
|
Packit |
d37888 |
(processor_info_array_t*)&pinfo,
|
|
Packit |
d37888 |
&info_count)) {
|
|
Packit |
d37888 |
glibtop_error_io_r (server, "host_processor_info");
|
|
Packit |
d37888 |
}
|
|
Packit |
d37888 |
server->ncpu = (processor_count <= GLIBTOP_NCPU) ?
|
|
Packit |
d37888 |
processor_count : GLIBTOP_NCPU;
|
|
Packit |
d37888 |
vm_deallocate (mach_task_self (), (vm_address_t) pinfo, info_count);
|
|
Packit |
d37888 |
}
|