Blame sysdeps/linux/proclist.c

Packit Service 407539
/* Copyright (C) 1998-99 Martin Baulig
Packit Service 407539
   This file is part of LibGTop 1.0.
Packit Service 407539
Packit Service 407539
   Contributed by Martin Baulig <martin@home-of-linux.org>, April 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/proclist.h>
Packit Service 407539
Packit Service 407539
#include <glibtop/procuid.h>
Packit Service 407539
#include <glibtop/procstate.h>
Packit Service 407539
Packit Service 407539
#include <sys/stat.h>
Packit Service 407539
#include <unistd.h>
Packit Service 407539
#include <dirent.h>
Packit Service 407539
#include <ctype.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
/* Init function. */
Packit Service 407539
Packit Service 407539
void
Packit Service 407539
_glibtop_init_proclist_s (glibtop *server)
Packit Service 407539
{
Packit Service 407539
	server->sysdeps.proclist = _glibtop_sysdeps_proclist;
Packit Service 407539
}
Packit Service 407539
Packit Service 407539
/* Fetch list of currently running processes.
Packit Service 407539
 *
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
 * On error, NULL is returned and buf->flags is zero. */
Packit Service 407539
Packit Service 407539
pid_t*
Packit Service 407539
glibtop_get_proclist_s (glibtop *server, glibtop_proclist *buf,
Packit Service 407539
			gint64 which, gint64 arg)
Packit Service 407539
{
Packit Service 407539
	DIR *proc;
Packit Service 407539
	struct dirent *entry;
Packit Service 407539
	GArray *pids;
Packit Service 407539
	glibtop_proc_uid procuid;
Packit Service 407539
	glibtop_proc_state procstate;
Packit Service 407539
	struct stat statb;
Packit Service 407539
Packit Service 407539
	memset (buf, 0, sizeof (glibtop_proclist));
Packit Service 407539
Packit Service 407539
	proc = opendir ("/proc");
Packit Service 407539
	if (!proc) return NULL;
Packit Service 407539
Packit Service 407539
	if(fstat(dirfd(proc), &statb) != 0) return NULL;
Packit Service 407539
Packit Service 407539
	pids = g_array_sized_new(FALSE, FALSE, sizeof(pid_t), statb.st_nlink);
Packit Service 407539
Packit Service 407539
	/* read every every entry in /proc */
Packit Service 407539
Packit Service 407539
	while((entry = readdir (proc))) {
Packit Service 407539
		pid_t pid;
Packit Service 407539
Packit Service 407539
		if (entry->d_type != DT_DIR)
Packit Service 407539
			continue;
Packit Service 407539
Packit Service 407539
		if (!(pid = strtoul(entry->d_name, NULL, 10)))
Packit Service 407539
			continue;
Packit Service 407539
Packit Service 407539
		switch (which & GLIBTOP_KERN_PROC_MASK) {
Packit Service 407539
		case GLIBTOP_KERN_PROC_ALL:
Packit Service 407539
			break;
Packit Service 407539
		case GLIBTOP_KERN_PROC_PID:
Packit Service 407539
			if ((pid_t) arg != pid)
Packit Service 407539
				continue;
Packit Service 407539
			break;
Packit Service 407539
		case GLIBTOP_KERN_PROC_UID:
Packit Service 407539
		{
Packit Service 407539
			char path[32];
Packit Service 407539
			struct stat path_stat;
Packit Service 407539
Packit Service 407539
			snprintf(path, sizeof path, "/proc/%u", (unsigned)pid);
Packit Service 407539
Packit Service 407539
			if (stat(path, &path_stat))
Packit Service 407539
				continue;
Packit Service 407539
Packit Service 407539
			if ((uid_t) arg != path_stat.st_uid)
Packit Service 407539
				continue;
Packit Service 407539
		}
Packit Service 407539
			break;
Packit Service 407539
		case GLIBTOP_KERN_PROC_PGRP:
Packit Service 407539
			/* Do you really, really need this ? */
Packit Service 407539
			glibtop_get_proc_uid_s (server, &procuid, pid);
Packit Service 407539
			if (procuid.flags & (1L << GLIBTOP_PROC_UID_PGRP))
Packit Service 407539
				if ((int) arg != procuid.pgrp)
Packit Service 407539
					continue;
Packit Service 407539
			break;
Packit Service 407539
		case GLIBTOP_KERN_PROC_SESSION:
Packit Service 407539
			/* Do you really, really need this ? */
Packit Service 407539
			glibtop_get_proc_uid_s (server, &procuid, pid);
Packit Service 407539
			if (procuid.flags & (1L << GLIBTOP_PROC_UID_SESSION))
Packit Service 407539
				if ((int) arg != procuid.session)
Packit Service 407539
					continue;
Packit Service 407539
			break;
Packit Service 407539
		case GLIBTOP_KERN_PROC_TTY:
Packit Service 407539
			/* Do you really, really need this ? */
Packit Service 407539
			glibtop_get_proc_uid_s (server, &procuid, pid);
Packit Service 407539
			if (procuid.flags & (1L << GLIBTOP_PROC_UID_TTY))
Packit Service 407539
				if ((int) arg != procuid.tty)
Packit Service 407539
					continue;
Packit Service 407539
			break;
Packit Service 407539
		case GLIBTOP_KERN_PROC_RUID:
Packit Service 407539
			/* Do you really, really need this ? */
Packit Service 407539
			glibtop_get_proc_uid_s (server, &procuid, pid);
Packit Service 407539
			if (procuid.flags & (1L << GLIBTOP_PROC_UID_EUID))
Packit Service 407539
				if ((int) arg != procuid.euid)
Packit Service 407539
					continue;
Packit Service 407539
			break;
Packit Service 407539
		}
Packit Service 407539
Packit Service 407539
		if (which & GLIBTOP_EXCLUDE_NOTTY) {
Packit Service 407539
			glibtop_get_proc_uid_s (server, &procuid, pid);
Packit Service 407539
			if (procuid.flags & (1L << GLIBTOP_PROC_UID_TTY))
Packit Service 407539
				if (procuid.tty == -1) continue;
Packit Service 407539
		}
Packit Service 407539
Packit Service 407539
		if (which & GLIBTOP_EXCLUDE_IDLE) {
Packit Service 407539
			glibtop_get_proc_state_s (server, &procstate, pid);
Packit Service 407539
			if (procstate.flags & (1L << GLIBTOP_PROC_STATE_STATE))
Packit Service 407539
				if (procstate.state != GLIBTOP_PROCESS_RUNNING) continue;
Packit Service 407539
		}
Packit Service 407539
Packit Service 407539
		if (which & GLIBTOP_EXCLUDE_SYSTEM) {
Packit Service 407539
			glibtop_get_proc_uid_s (server, &procuid, pid);
Packit Service 407539
			if (procuid.flags & (1L << GLIBTOP_PROC_UID_UID))
Packit Service 407539
				if (procuid.uid == 0) continue;
Packit Service 407539
		}
Packit Service 407539
Packit Service 407539
		g_array_append_val(pids, pid);
Packit Service 407539
	}
Packit Service 407539
Packit Service 407539
	closedir (proc);
Packit Service 407539
Packit Service 407539
	buf->flags = _glibtop_sysdeps_proclist;
Packit Service 407539
	buf->size = sizeof(pid_t);
Packit Service 407539
	buf->number = pids->len;
Packit Service 407539
	buf->total = buf->number * buf->size;
Packit Service 407539
Packit Service 407539
	return (pid_t*)g_array_free(pids, FALSE);
Packit Service 407539
}