Blame sysdeps/bsd/proclist.c

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/proclist.h>
Packit Service 407539
Packit Service 407539
#include <glibtop_suid.h>
Packit Service 407539
Packit Service 407539
static const unsigned long _glibtop_sysdeps_proclist =
Packit Service 407539
(1L << GLIBTOP_PROCLIST_TOTAL) + (1L << GLIBTOP_PROCLIST_NUMBER) +
Packit Service 407539
(1L << GLIBTOP_PROCLIST_SIZE);
Packit Service 407539
Packit Service 407539
/* Fetch list of currently running processes.
Packit Service 407539
 * The interface of this function is a little bit different from the others:
Packit Service 407539
 * buf->flags is only set if the call succeeded, in this case pids_chain,
Packit Service 407539
 * a list of the pids of all currently running processes is returned,
Packit Service 407539
 * buf->number is the number of elements of this list and buf->size is
Packit Service 407539
 * the size of one single element (sizeof (unsigned)). The total size is
Packit Service 407539
 * stored in buf->total.
Packit Service 407539
 *
Packit Service 407539
 * The calling function has to free the memory to which a pointer is returned.
Packit Service 407539
 *
Packit Service 407539
 * IMPORTANT NOTE:
Packit Service 407539
 *   On error, this function MUST return NULL and set buf->flags to zero !
Packit Service 407539
 *   On success, it returnes a pointer to a list of buf->number elements
Packit Service 407539
 *   each buf->size big. The total size is stored in buf->total.
Packit Service 407539
 * The calling function has to free the memory to which a pointer is returned.
Packit Service 407539
 *
Packit Service 407539
 * On error, NULL is returned and buf->flags is zero. */
Packit Service 407539
Packit Service 407539
/* Init function. */
Packit Service 407539
Packit Service 407539
void
Packit Service 407539
_glibtop_init_proclist_p (glibtop *server)
Packit Service 407539
{
Packit Service 407539
	server->sysdeps.proclist = _glibtop_sysdeps_proclist;
Packit Service 407539
}
Packit Service 407539
Packit Service 407539
unsigned *
Packit Service 407539
glibtop_get_proclist_p (glibtop *server, glibtop_proclist *buf,
Packit Service 407539
			gint64 real_which, gint64 arg)
Packit Service 407539
{
Packit Service 407539
	struct kinfo_proc *pinfo;
Packit Service 407539
	unsigned *pids = NULL;
Packit Service 407539
	int which, count;
Packit Service 407539
	int i,j;
Packit Service 407539
Packit Service 407539
	glibtop_init_p (server, (1L << GLIBTOP_SYSDEPS_PROCLIST), 0);
Packit Service 407539
Packit Service 407539
	memset (buf, 0, sizeof (glibtop_proclist));
Packit Service 407539
Packit Service 407539
	which = (int)(real_which & GLIBTOP_KERN_PROC_MASK);
Packit Service 407539
Packit Service 407539
	/* Get the process data */
Packit Service 407539
	pinfo = kvm_getprocs (server->machine->kd, which, arg, &count);
Packit Service 407539
	if ((pinfo == NULL) || (count < 1)) {
Packit Service 407539
		glibtop_warn_io_r (server, "kvm_getprocs (proclist)");
Packit Service 407539
		return NULL;
Packit Service 407539
	}
Packit Service 407539
	count--;
Packit Service 407539
Packit Service 407539
	/* Allocate count objects in the pids_chain array
Packit Service 407539
	 * Same as malloc is pids is NULL, which it is. */
Packit Service 407539
	pids = g_realloc (pids, count * sizeof (unsigned));
Packit Service 407539
	/* Copy the pids over to this chain */
Packit Service 407539
	for (i=j=0; i < count; i++) {
Packit Service 407539
#if (defined(__FreeBSD__) && (__FreeBSD_version >= 500013)) || defined(__FreeBSD_kernel__)
Packit Service 407539
#define PROC_STAT	ki_stat
Packit Service 407539
#define PROC_RUID	ki_ruid
Packit Service 407539
#define PROC_PID	ki_pid
Packit Service 407539
Packit Service 407539
#else
Packit Service 407539
#define PROC_STAT	kp_proc.p_stat
Packit Service 407539
#define PROC_RUID	kp_eproc.e_pcred.p_ruid
Packit Service 407539
#define PROC_PID	kp_proc.p_pid
Packit Service 407539
Packit Service 407539
#endif
Packit Service 407539
Packit Service 407539
		if ((real_which & GLIBTOP_EXCLUDE_IDLE) &&
Packit Service 407539
		    (pinfo[i].PROC_STAT != SRUN))
Packit Service 407539
			continue;
Packit Service 407539
		else if ((real_which & GLIBTOP_EXCLUDE_SYSTEM) &&
Packit Service 407539
			 (pinfo[i].PROC_RUID == 0))
Packit Service 407539
			continue;
Packit Service 407539
		pids [j++] = (unsigned) pinfo[i].PROC_PID;
Packit Service 407539
	} /* end for */
Packit Service 407539
	/* Set the fields in buf */
Packit Service 407539
	buf->number = j;
Packit Service 407539
	buf->size = sizeof (unsigned);
Packit Service 407539
	buf->total = j * sizeof (unsigned);
Packit Service 407539
	buf->flags = _glibtop_sysdeps_proclist;
Packit Service 407539
	return pids;
Packit Service 407539
}