|
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 <procinfo.h>
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
#include <glibtop.h>
|
|
Packit |
d37888 |
#include <glibtop/proclist.h>
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
static const unsigned long _glibtop_sysdeps_proclist =
|
|
Packit |
d37888 |
(1 << GLIBTOP_PROCLIST_NUMBER) + (1 << GLIBTOP_PROCLIST_TOTAL) +
|
|
Packit |
d37888 |
(1 << GLIBTOP_PROCLIST_SIZE);
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
#define BLOCK_COUNT 256
|
|
Packit |
d37888 |
#define BLOCK_SIZE (BLOCK_COUNT * sizeof (unsigned int))
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
/* Init function. */
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
void
|
|
Packit |
d37888 |
_glibtop_init_proclist_s (glibtop *server)
|
|
Packit |
d37888 |
{
|
|
Packit |
d37888 |
server->sysdeps.proclist = _glibtop_sysdeps_proclist;
|
|
Packit |
d37888 |
}
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
/* Fetch list of currently running processes.
|
|
Packit |
d37888 |
*
|
|
Packit |
d37888 |
* IMPORTANT NOTE:
|
|
Packit |
d37888 |
* On error, this function MUST return NULL and set buf->flags to zero !
|
|
Packit |
d37888 |
* On success, it returnes a pointer to a list of buf->number elements
|
|
Packit |
d37888 |
* each buf->size big. The total size is stored in buf->total. */
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
unsigned *
|
|
Packit |
d37888 |
glibtop_get_proclist_s (glibtop *server, glibtop_proclist *buf,
|
|
Packit |
d37888 |
gint64 which, gint64 arg)
|
|
Packit |
d37888 |
{
|
|
Packit |
d37888 |
struct procsinfo pinfo;
|
|
Packit |
d37888 |
int count, total;
|
|
Packit |
d37888 |
unsigned pids [BLOCK_COUNT], *pids_chain = NULL;
|
|
Packit |
d37888 |
int pids_size = 0, pids_offset = 0, new_size;
|
|
Packit |
d37888 |
pid_t current;
|
|
Packit |
d37888 |
int result;
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
glibtop_init_s (&server, (1L << GLIBTOP_SYSDEPS_PROCLIST), 0);
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
memset (buf, 0, sizeof (glibtop_proclist));
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
for( count = total = 0, current = 0
|
|
Packit |
d37888 |
, result = getprocs(&pinfo, sizeof(pinfo), NULL, 0, ¤t, 1);
|
|
Packit |
d37888 |
result == 1;
|
|
Packit |
d37888 |
result = getprocs(&pinfo, sizeof(pinfo), NULL, 0, ¤t, 1))
|
|
Packit |
d37888 |
{
|
|
Packit |
d37888 |
if (which & GLIBTOP_EXCLUDE_IDLE)
|
|
Packit |
d37888 |
{
|
|
Packit |
d37888 |
if (pinfo.pi_state & SIDL)
|
|
Packit |
d37888 |
{
|
|
Packit |
d37888 |
/* exclude idle processes */
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
continue;
|
|
Packit |
d37888 |
}
|
|
Packit |
d37888 |
}
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
if (which & GLIBTOP_EXCLUDE_SYSTEM)
|
|
Packit |
d37888 |
{
|
|
Packit |
d37888 |
if (pinfo.pi_flags & SKPROC)
|
|
Packit |
d37888 |
{
|
|
Packit |
d37888 |
/* exclude Kernel processes */
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
continue;
|
|
Packit |
d37888 |
}
|
|
Packit |
d37888 |
}
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
if (which & GLIBTOP_EXCLUDE_NOTTY)
|
|
Packit |
d37888 |
{
|
|
Packit |
d37888 |
if (!pinfo.pi_ttyp)
|
|
Packit |
d37888 |
{
|
|
Packit |
d37888 |
/* exclude processes without tty */
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
continue;
|
|
Packit |
d37888 |
}
|
|
Packit |
d37888 |
}
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
switch(which & GLIBTOP_KERN_PROC_MASK)
|
|
Packit |
d37888 |
{
|
|
Packit |
d37888 |
case GLIBTOP_KERN_PROC_ALL:
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
/* return information about all processes
|
|
Packit |
d37888 |
* so, let's go ahead
|
|
Packit |
d37888 |
*/
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
break;
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
case GLIBTOP_KERN_PROC_PID:
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
/* return information about all processes with
|
|
Packit |
d37888 |
* pid passed in arg
|
|
Packit |
d37888 |
*/
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
if (pinfo.pi_pid != arg)
|
|
Packit |
d37888 |
{
|
|
Packit |
d37888 |
continue;
|
|
Packit |
d37888 |
}
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
break;
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
case GLIBTOP_KERN_PROC_PGRP:
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
/* return information about all processes in
|
|
Packit |
d37888 |
* process group passed in arg
|
|
Packit |
d37888 |
*/
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
if (pinfo.pi_pgrp != arg)
|
|
Packit |
d37888 |
{
|
|
Packit |
d37888 |
continue;
|
|
Packit |
d37888 |
}
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
break;
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
case GLIBTOP_KERN_PROC_SESSION:
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
/* return information about all processes in
|
|
Packit |
d37888 |
* session passed in arg
|
|
Packit |
d37888 |
*/
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
if (pinfo.pi_sid != arg)
|
|
Packit |
d37888 |
{
|
|
Packit |
d37888 |
continue;
|
|
Packit |
d37888 |
}
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
break;
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
case GLIBTOP_KERN_PROC_TTY:
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
/* return information about all processes with
|
|
Packit |
d37888 |
* tty device number passed in arg
|
|
Packit |
d37888 |
*/
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
if (pinfo.pi_ttyd != arg)
|
|
Packit |
d37888 |
{
|
|
Packit |
d37888 |
continue;
|
|
Packit |
d37888 |
}
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
break;
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
case GLIBTOP_KERN_PROC_UID:
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
/* return information about all processes with
|
|
Packit |
d37888 |
* effective uid passed in arg
|
|
Packit |
d37888 |
*/
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
if (pinfo.pi_cred.cr_uid != arg)
|
|
Packit |
d37888 |
{
|
|
Packit |
d37888 |
continue;
|
|
Packit |
d37888 |
}
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
break;
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
case GLIBTOP_KERN_PROC_RUID:
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
/* return information about all processes with
|
|
Packit |
d37888 |
* real uid passed in arg
|
|
Packit |
d37888 |
*/
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
if (pinfo.pi_cred.cr_ruid != arg)
|
|
Packit |
d37888 |
{
|
|
Packit |
d37888 |
continue;
|
|
Packit |
d37888 |
}
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
break;
|
|
Packit |
d37888 |
}
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
if (count >= BLOCK_COUNT)
|
|
Packit |
d37888 |
{
|
|
Packit |
d37888 |
/* The following call to g_realloc will be
|
|
Packit |
d37888 |
* equivalent to g_malloc () if `pids_chain' is
|
|
Packit |
d37888 |
* NULL. We just calculate the new size and copy `pids'
|
|
Packit |
d37888 |
* to the 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 |
(pids_chain, new_size);
|
|
Packit |
d37888 |
memcpy (pids_chain + pids_offset, pids, BLOCK_SIZE);
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
pids_size = new_size;
|
|
Packit |
d37888 |
pids_offset += BLOCK_COUNT;
|
|
Packit |
d37888 |
count = 0;
|
|
Packit |
d37888 |
}
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
/* pids is now big enough to hold at least one single pid. */
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
pids[count++] = pinfo.pi_pid;
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
total++;
|
|
Packit |
d37888 |
}
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
if (result == -1)
|
|
Packit |
d37888 |
{
|
|
Packit |
d37888 |
glibtop_error_io_r(server, "Cannot read procsinfo");
|
|
Packit |
d37888 |
}
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
/* count is only zero if an error occured (one a running Linux system,
|
|
Packit |
d37888 |
* we have at least one single process). */
|
|
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 (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 |
/* Since everything is ok now, we can set buf->flags, fill in the
|
|
Packit |
d37888 |
* remaining fields and return the `pids_chain'. */
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
buf->size = sizeof(unsigned int);
|
|
Packit |
d37888 |
buf->number = total;
|
|
Packit |
d37888 |
buf->total = total * buf->size;
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
buf->flags = _glibtop_sysdeps_proclist;
|
|
Packit |
d37888 |
|
|
Packit |
d37888 |
return pids_chain;
|
|
Packit |
d37888 |
}
|