Blame sysdeps/cygwin/proclist.c

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