Blame sysdeps/solaris/procopenfiles.c

Packit Service 407539
/* Copyright (C) 2006 Henry Zhang
Packit Service 407539
   This file is part of LibGTop 2.14.
Packit Service 407539
Packit Service 407539
   Contributed by Henry Zhang <hua.zhang@sun.com>, July 2006.
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.h>
Packit Service 407539
#include <glibtop/error.h>
Packit Service 407539
#include <glibtop/procopenfiles.h>
Packit Service 407539
#include <sys/types.h>
Packit Service 407539
#include <fcntl.h>
Packit Service 407539
#include <sys/stat.h>
Packit Service 407539
#include <dirent.h>
Packit Service 407539
#include <string.h>
Packit Service 407539
#include <stdio.h>
Packit Service 407539
Packit Service 407539
#include "glibtop_private.h"
Packit Service 407539
Packit Service 407539
static const unsigned long _glibtop_sysdeps_proc_open_files =
Packit Service 407539
(1L << GLIBTOP_PROC_OPEN_FILES_NUMBER)|
Packit Service 407539
(1L << GLIBTOP_PROC_OPEN_FILES_TOTAL)|
Packit Service 407539
(1L << GLIBTOP_PROC_OPEN_FILES_SIZE);
Packit Service 407539
Packit Service 407539
/* Init function. */
Packit Service 407539
Packit Service 407539
void
Packit Service 407539
_glibtop_init_proc_open_files_s (glibtop *server)
Packit Service 407539
{
Packit Service 407539
	server->sysdeps.proc_open_files = _glibtop_sysdeps_proc_open_files;
Packit Service 407539
}
Packit Service 407539
Packit Service 407539
Packit Service 407539
Packit Service 407539
/* Provides detailed information about a process' open files */
Packit Service 407539
Packit Service 407539
glibtop_open_files_entry *
Packit Service 407539
glibtop_get_proc_open_files_s (glibtop *server, glibtop_proc_open_files *buf,	pid_t pid)
Packit Service 407539
{
Packit Service 407539
	char filename [BUFSIZ];
Packit Service 407539
	GArray *entries;
Packit Service 407539
	struct dirent *direntry;
Packit Service 407539
	DIR *dir;
Packit Service 407539
	int errno;
Packit Service 407539
Packit Service 407539
	glibtop_init_s (&server, GLIBTOP_SYSDEPS_PROC_OPEN_FILES, 0);
Packit Service 407539
Packit Service 407539
	memset (buf, 0, sizeof (glibtop_proc_open_files));
Packit Service 407539
Packit Service 407539
	sprintf (filename, "/proc/%d/fd", pid);
Packit Service 407539
Packit Service 407539
	dir = opendir (filename);
Packit Service 407539
	if (!dir) return NULL;
Packit Service 407539
Packit Service 407539
	entries = g_array_new(FALSE, FALSE, sizeof(glibtop_open_files_entry));
Packit Service 407539
Packit Service 407539
	while((direntry = readdir(dir))) {
Packit Service 407539
		char tgt [BUFSIZ];
Packit Service 407539
		int rv;
Packit Service 407539
		glibtop_open_files_entry entry = {0};
Packit Service 407539
		struct stat statbuf;
Packit Service 407539
Packit Service 407539
		if(direntry->d_name[0] == '.')
Packit Service 407539
			continue;
Packit Service 407539
Packit Service 407539
		if ((entry.fd = (int) g_ascii_strtoull(direntry->d_name, NULL, 10)) == 0)
Packit Service 407539
			continue;
Packit Service 407539
					
Packit Service 407539
		/* from /path get object name */
Packit Service 407539
		g_snprintf(filename, sizeof filename, "/proc/%d/path/%s",
Packit Service 407539
			   pid, direntry->d_name);
Packit Service 407539
Packit Service 407539
		rv = readlink(filename, tgt, sizeof(tgt) - 1);
Packit Service 407539
		/* read object, if not have, set it as NULL, but this fd still need to insert into the array */
Packit Service 407539
		if(rv < 0) 
Packit Service 407539
			rv = 0;
Packit Service 407539
		tgt[rv] = '\0';
Packit Service 407539
		
Packit Service 407539
		/* from /fd get the stat data */
Packit Service 407539
		g_snprintf(filename, sizeof filename, "/proc/%d/fd/%s",
Packit Service 407539
			   pid, direntry->d_name);
Packit Service 407539
Packit Service 407539
		if(stat (filename, &statbuf))
Packit Service 407539
			statbuf.st_mode = 0;		
Packit Service 407539
Packit Service 407539
		switch (statbuf.st_mode & S_IFMT) {
Packit Service 407539
			case S_IFIFO:    /* pipe */
Packit Service 407539
				entry.type = GLIBTOP_FILE_TYPE_PIPE;
Packit Service 407539
				break;
Packit Service 407539
			case S_IFSOCK:	/* socket */
Packit Service 407539
			/* at solaris, now a little difficult to tell the Socket type, so here I 
Packit Service 407539
			   give the type 0, it will not impact the existed code. Later will provide 
Packit Service 407539
			   a patch to tell the type, and get the object name */
Packit Service 407539
				entry.type = 0;
Packit Service 407539
				break;
Packit Service 407539
			default:
Packit Service 407539
				entry.type = GLIBTOP_FILE_TYPE_FILE;
Packit Service 407539
		}
Packit Service 407539
				
Packit Service 407539
		g_strlcpy(entry.info.file.name, tgt, sizeof entry.info.file.name);
Packit Service 407539
Packit Service 407539
		g_array_append_val(entries, entry);
Packit Service 407539
	}
Packit Service 407539
Packit Service 407539
	closedir (dir);
Packit Service 407539
Packit Service 407539
	buf->flags = _glibtop_sysdeps_proc_open_files;
Packit Service 407539
	buf->number = entries->len;
Packit Service 407539
	buf->size = sizeof(glibtop_open_files_entry);
Packit Service 407539
	buf->total = buf->number * buf->size;
Packit Service 407539
Packit Service 407539
	return (glibtop_open_files_entry*)g_array_free(entries, FALSE);
Packit Service 407539
}