Blame sysdeps/linux/procwd.c

Packit d37888
/* Copyright (C) 2007 BenoƮt Dejean
Packit d37888
   This file is part of LibGTop 2.
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/procwd.h>
Packit d37888
#include <glibtop/error.h>
Packit d37888
Packit d37888
#include <glibtop_private.h>
Packit d37888
Packit d37888
#include <unistd.h>
Packit d37888
#include <dirent.h>
Packit d37888
#include <sys/types.h>
Packit d37888
Packit d37888
Packit d37888
void
Packit d37888
_glibtop_init_proc_wd_s(glibtop *server)
Packit d37888
{
Packit d37888
	server->sysdeps.proc_wd =
Packit d37888
	(1 << GLIBTOP_PROC_WD_EXE) +
Packit d37888
	(1 << GLIBTOP_PROC_WD_ROOT) +
Packit d37888
	(1 << GLIBTOP_PROC_WD_NUMBER);
Packit d37888
Packit d37888
}
Packit d37888
Packit d37888
static gboolean is_in(GPtrArray *array, const char *str)
Packit d37888
{
Packit d37888
	guint i;
Packit d37888
Packit d37888
	for (i = 0; i != array->len; ++i) {
Packit d37888
		if (strcmp(g_ptr_array_index(array, i), str) == 0)
Packit d37888
			return TRUE;
Packit d37888
	}
Packit d37888
Packit d37888
	return FALSE;
Packit d37888
}
Packit d37888
Packit d37888
Packit d37888
char**
Packit d37888
glibtop_get_proc_wd_s(glibtop *server, glibtop_proc_wd *buf, pid_t pid)
Packit d37888
{
Packit d37888
	GPtrArray *dirs;
Packit d37888
	char path[80];
Packit d37888
	char dir[256];
Packit d37888
	DIR *task;
Packit d37888
Packit d37888
	memset(buf, 0, sizeof(glibtop_proc_wd));
Packit d37888
Packit d37888
	g_snprintf(path, sizeof path, "/proc/%u/root", pid);
Packit d37888
	if (safe_readlink(path, buf->root, sizeof buf->root))
Packit d37888
		buf->flags |= (1 << GLIBTOP_PROC_WD_ROOT);
Packit d37888
Packit d37888
	g_snprintf(path, sizeof path, "/proc/%u/exe", pid);
Packit d37888
	if (safe_readlink(path, buf->exe, sizeof buf->exe))
Packit d37888
		buf->flags |= (1 << GLIBTOP_PROC_WD_EXE);
Packit d37888
Packit d37888
	dirs = g_ptr_array_sized_new(2);
Packit d37888
Packit d37888
	g_snprintf(path, sizeof path, "/proc/%u/cwd", pid);
Packit d37888
	if (safe_readlink(path, dir, sizeof dir))
Packit d37888
		g_ptr_array_add(dirs, g_strdup(dir));
Packit d37888
Packit d37888
	g_snprintf(path, sizeof path, "/proc/%u/task", pid);
Packit d37888
	if ((task = opendir(path)) != NULL) {
Packit d37888
		struct dirent *sub;
Packit d37888
		while ((sub = readdir(task)) != NULL) {
Packit d37888
			/* task dirs have numeric name */
Packit d37888
			if (!isdigit(sub->d_name[0]))
Packit d37888
				continue;
Packit d37888
			g_snprintf(path, sizeof path, "/proc/%u/task/%s/cwd", pid, sub->d_name);
Packit d37888
			if (safe_readlink(path, dir, sizeof dir) && !is_in(dirs, dir))
Packit d37888
				g_ptr_array_add(dirs, g_strdup(dir));
Packit d37888
		}
Packit d37888
		closedir(task);
Packit d37888
	}
Packit d37888
Packit d37888
	buf->number = dirs->len;
Packit d37888
	buf->flags |= (1 << GLIBTOP_PROC_WD_NUMBER);
Packit d37888
Packit d37888
	g_ptr_array_add(dirs, NULL);
Packit d37888
Packit d37888
	return (char**) g_ptr_array_free(dirs, FALSE);
Packit d37888
}
Packit d37888