|
Packit |
d37888 |
/* Copyright (C) 1998-99 Martin Baulig
|
|
Packit |
d37888 |
This file is part of LibGTop 1.0.
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
Contributed by Martin Baulig <martin@home-of-linux.org>, April 1998.
|
|
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 |
#include <glibtop.h>
|
|
Packit |
d37888 |
#include <glibtop/error.h>
|
|
Packit |
d37888 |
#include <glibtop/proclist.h>
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
#include <glibtop_suid.h>
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
static const unsigned long _glibtop_sysdeps_proclist =
|
|
Packit |
d37888 |
(1L << GLIBTOP_PROCLIST_TOTAL) + (1L << GLIBTOP_PROCLIST_NUMBER) +
|
|
Packit |
d37888 |
(1L << GLIBTOP_PROCLIST_SIZE);
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
/* Init function. */
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
void
|
|
Packit |
d37888 |
_glibtop_init_proclist_p (glibtop *server)
|
|
Packit |
d37888 |
{
|
|
Packit |
d37888 |
server->sysdeps.proclist = _glibtop_sysdeps_proclist;
|
|
Packit |
d37888 |
}
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
/* How many elements are there per proctable entry? */
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
#define ELEMENTS_PER_ENTRY 8
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
/* We have a buffer for BLOCK_COUNT pids; when it's full, it is copied
|
|
Packit |
d37888 |
* to a newly realloc()ed area. */
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
#define BLOCK_COUNT 256
|
|
Packit |
d37888 |
#define BLOCK_SIZE (BLOCK_COUNT * sizeof (unsigned))
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
/* Fetch list of currently running processes.
|
|
Packit |
d37888 |
*
|
|
Packit |
d37888 |
* The interface of this function is a little bit different from the others:
|
|
Packit |
d37888 |
* buf->flags is only set if the call succeeded, in this case pids_chain,
|
|
Packit |
d37888 |
* a list of the pids of all currently running processes is returned,
|
|
Packit |
d37888 |
* buf->number is the number of elements of this list and buf->size is
|
|
Packit |
d37888 |
* the size of one single element (sizeof (unsigned)). The total size is
|
|
Packit |
d37888 |
* stored in buf->total.
|
|
Packit |
d37888 |
*
|
|
Packit |
d37888 |
* The calling function has to free the memory to which a pointer is returned.
|
|
Packit |
d37888 |
*
|
|
Packit |
d37888 |
* On error, NULL is returned and buf->flags is zero. */
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
/* !!! THIS FUNCTION RUNS SUID ROOT - CHANGE WITH CAUTION !!! */
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
unsigned *
|
|
Packit |
d37888 |
glibtop_get_proclist_p (glibtop *server, glibtop_proclist *buf,
|
|
Packit |
d37888 |
gint64 which, gint64 arg)
|
|
Packit |
d37888 |
{
|
|
Packit |
d37888 |
unsigned count = 0, total = 0;
|
|
Packit |
d37888 |
unsigned pids [BLOCK_COUNT], *pids_chain = NULL;
|
|
Packit |
d37888 |
unsigned pids_size = 0, pids_offset = 0, new_size;
|
|
Packit |
d37888 |
struct tbl_procinfo procinfo [8];
|
|
Packit |
d37888 |
int entry, max_elements, k;
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
glibtop_init_p (server, GLIBTOP_SYSDEPS_PROCLIST, 0);
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
memset (buf, 0, sizeof (glibtop_proclist));
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
for (entry = 0; entry < server->machine->proctable_entries;
|
|
Packit |
d37888 |
entry += ELEMENTS_PER_ENTRY)
|
|
Packit |
d37888 |
{
|
|
Packit |
d37888 |
/* !!! THE FOLLOWING CODE RUNS SUID ROOT -
|
|
Packit |
d37888 |
* CHANGE WITH CAUTION !!! */
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
glibtop_suid_enter (server);
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
max_elements = table
|
|
Packit |
d37888 |
(TBL_PROCINFO, entry, (char *) &procinfo,
|
|
Packit |
d37888 |
ELEMENTS_PER_ENTRY, sizeof (struct tbl_procinfo));
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
glibtop_suid_leave (server);
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
/* !!! END OF SUID ROOT PART !!! */
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
for (k = 0; k < max_elements; k++)
|
|
Packit |
d37888 |
{
|
|
Packit |
d37888 |
/* Does this entry contain a real process? */
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
if (procinfo [k].pi_status == 0)
|
|
Packit |
d37888 |
continue;
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
/* Fine. Now we first try to store it in pids.
|
|
Packit |
d37888 |
* If this buffer is full, we copy it to the
|
|
Packit |
d37888 |
* pids_chain. */
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
if (count >= BLOCK_COUNT) {
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
/* The following call to g_realloc ()
|
|
Packit |
d37888 |
* will be equivalent to g_malloc ()
|
|
Packit |
d37888 |
* if `pids_chain' is NULL. We just calculate
|
|
Packit |
d37888 |
* the new size and copy `pids' to the
|
|
Packit |
d37888 |
* beginning of the newly allocated block. */
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
new_size = pids_size + BLOCK_SIZE;
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
pids_chain = g_realloc
|
|
Packit |
d37888 |
(server, pids_chain, new_size);
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
memcpy (pids_chain + pids_offset,
|
|
Packit |
d37888 |
pids, BLOCK_SIZE);
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
pids_size = new_size;
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
pids_offset += BLOCK_COUNT;
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
count = 0;
|
|
Packit |
d37888 |
}
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
/* pids is now big enough to hold at least
|
|
Packit |
d37888 |
* one single pid. */
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
pids [count++] = procinfo [k].pi_pid;
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
total++;
|
|
Packit |
d37888 |
}
|
|
Packit |
d37888 |
}
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
/* count is only zero if an error occured
|
|
Packit |
d37888 |
* (eg. the server is not suid root). */
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
if (!count) return NULL;
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
/* The following call to g_realloc () will be equivalent to
|
|
Packit |
d37888 |
* g_malloc () if `pids_chain' is NULL. We just calculate the
|
|
Packit |
d37888 |
* new size and copy `pids' to the beginning of the newly allocated
|
|
Packit |
d37888 |
* block. */
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
new_size = pids_size + count * sizeof (unsigned);
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
pids_chain = g_realloc (server, pids_chain, new_size);
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
memcpy (pids_chain + pids_offset, pids, count * sizeof (unsigned));
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
pids_size = new_size;
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
pids_offset += BLOCK_COUNT;
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
/* Since everything is ok now, we can set buf->flags, fill in the
|
|
Packit |
d37888 |
* remaining fields and return `pids_chain'. */
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
buf->flags = _glibtop_sysdeps_proclist;
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
buf->size = sizeof (unsigned);
|
|
Packit |
d37888 |
buf->number = total;
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
buf->total = total * sizeof (unsigned);
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
return pids_chain;
|
|
Packit |
d37888 |
}
|