Blame sysdeps/darwin/proclist.c

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
#include <unistd.h>
Packit d37888
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
#include <sys/param.h>
Packit d37888
#include <sys/sysctl.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
pid_t *
Packit d37888
glibtop_get_proclist_p (glibtop *server, glibtop_proclist *buf,
Packit d37888
			gint64 which, gint64 arg)
Packit d37888
{
Packit d37888
	unsigned count, total, i;
Packit d37888
	pid_t *pids_chain;
Packit d37888
	int mib[4];
Packit d37888
	struct kinfo_proc *kp;
Packit d37888
	size_t length;
Packit d37888
Packit d37888
	glibtop_init_p (server, (1 << GLIBTOP_SYSDEPS_PROCLIST), 0);
Packit d37888
Packit d37888
	memset (buf, 0, sizeof (glibtop_proclist));
Packit d37888
Packit d37888
	mib [0] = CTL_KERN;
Packit d37888
	mib [1] = KERN_PROC;
Packit d37888
	mib [2] = (int)(which & GLIBTOP_KERN_PROC_MASK);
Packit d37888
	mib [3] = (int)arg;
Packit d37888
Packit d37888
	if (sysctl (mib, 4, NULL, &length, NULL, 0) < 0) {
Packit d37888
		glibtop_warn_io_r (server, "sysctl (proclist)");
Packit d37888
		return NULL;
Packit d37888
	}
Packit d37888
	if ((kp = (struct kinfo_proc *) g_malloc(length)) == NULL) {
Packit d37888
		glibtop_warn_io_r (server, "malloc (proclist)");
Packit d37888
		return NULL;
Packit d37888
	}
Packit d37888
	if (sysctl (mib, 4, kp, &length, NULL, 0) < 0) {
Packit d37888
		glibtop_warn_io_r (server, "sysctl (proclist)");
Packit d37888
		g_free (kp);
Packit d37888
		return NULL;
Packit d37888
	}
Packit d37888
Packit d37888
	count = length / sizeof (struct kinfo_proc);
Packit d37888
	pids_chain = g_malloc(count * sizeof (unsigned));
Packit d37888
	if (pids_chain ==  NULL) {
Packit d37888
		glibtop_warn_io_r (server, "g_realloc (proclist)");
Packit d37888
		g_free (kp);
Packit d37888
		return NULL;
Packit d37888
	}
Packit d37888
Packit d37888
	for (total = 0, i = 0; i < count; i++) {
Packit d37888
		if ((which & GLIBTOP_EXCLUDE_IDLE) &&
Packit d37888
		    (kp [i].kp_proc.p_stat != SRUN))
Packit d37888
			continue;
Packit d37888
		if ((which & GLIBTOP_EXCLUDE_SYSTEM) &&
Packit d37888
			 (kp [i].kp_eproc.e_pcred.p_ruid == 0))
Packit d37888
			continue;
Packit d37888
		pids_chain [total++] = (unsigned) kp [i].kp_proc.p_pid;
Packit d37888
	}
Packit d37888
Packit d37888
	g_free (kp);
Packit d37888
Packit d37888
	buf->number = total;
Packit d37888
	buf->size = sizeof (unsigned);
Packit d37888
	buf->total = total * sizeof (unsigned);
Packit d37888
	buf->flags = _glibtop_sysdeps_proclist;
Packit d37888
Packit d37888
	return pids_chain;
Packit d37888
}
Packit d37888