Blame sysdeps/bsd/proclist.c

Packit d37888
/* Copyright (C) 1998 Joshua Sled
Packit d37888
   This file is part of LibGTop 1.0.
Packit d37888
Packit d37888
   Contributed by Joshua Sled <jsled@xcf.berkeley.edu>, July 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
/* Fetch list of currently running processes.
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
 * 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
 * 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
/* 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
unsigned *
Packit d37888
glibtop_get_proclist_p (glibtop *server, glibtop_proclist *buf,
Packit d37888
			gint64 real_which, gint64 arg)
Packit d37888
{
Packit d37888
	struct kinfo_proc *pinfo;
Packit d37888
	unsigned *pids = NULL;
Packit d37888
	int which, count;
Packit d37888
	int i,j;
Packit d37888
Packit d37888
	glibtop_init_p (server, (1L << GLIBTOP_SYSDEPS_PROCLIST), 0);
Packit d37888
Packit d37888
	memset (buf, 0, sizeof (glibtop_proclist));
Packit d37888
Packit d37888
	which = (int)(real_which & GLIBTOP_KERN_PROC_MASK);
Packit d37888
Packit d37888
	/* Get the process data */
Packit d37888
	pinfo = kvm_getprocs (server->machine->kd, which, arg, &count);
Packit d37888
	if ((pinfo == NULL) || (count < 1)) {
Packit d37888
		glibtop_warn_io_r (server, "kvm_getprocs (proclist)");
Packit d37888
		return NULL;
Packit d37888
	}
Packit d37888
	count--;
Packit d37888
Packit d37888
	/* Allocate count objects in the pids_chain array
Packit d37888
	 * Same as malloc is pids is NULL, which it is. */
Packit d37888
	pids = g_realloc (pids, count * sizeof (unsigned));
Packit d37888
	/* Copy the pids over to this chain */
Packit d37888
	for (i=j=0; i < count; i++) {
Packit d37888
#if (defined(__FreeBSD__) && (__FreeBSD_version >= 500013)) || defined(__FreeBSD_kernel__)
Packit d37888
#define PROC_STAT	ki_stat
Packit d37888
#define PROC_RUID	ki_ruid
Packit d37888
#define PROC_PID	ki_pid
Packit d37888
Packit d37888
#else
Packit d37888
#define PROC_STAT	kp_proc.p_stat
Packit d37888
#define PROC_RUID	kp_eproc.e_pcred.p_ruid
Packit d37888
#define PROC_PID	kp_proc.p_pid
Packit d37888
Packit d37888
#endif
Packit d37888
Packit d37888
		if ((real_which & GLIBTOP_EXCLUDE_IDLE) &&
Packit d37888
		    (pinfo[i].PROC_STAT != SRUN))
Packit d37888
			continue;
Packit d37888
		else if ((real_which & GLIBTOP_EXCLUDE_SYSTEM) &&
Packit d37888
			 (pinfo[i].PROC_RUID == 0))
Packit d37888
			continue;
Packit d37888
		pids [j++] = (unsigned) pinfo[i].PROC_PID;
Packit d37888
	} /* end for */
Packit d37888
	/* Set the fields in buf */
Packit d37888
	buf->number = j;
Packit d37888
	buf->size = sizeof (unsigned);
Packit d37888
	buf->total = j * sizeof (unsigned);
Packit d37888
	buf->flags = _glibtop_sysdeps_proclist;
Packit d37888
	return pids;
Packit d37888
}