File: procopenfiles.c

package info (click to toggle)
libgtop2 2.40.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid
  • size: 8,072 kB
  • sloc: ansic: 25,393; sh: 7,089; makefile: 418; perl: 268; xml: 179; sed: 16
file content (128 lines) | stat: -rw-r--r-- 3,611 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/* Copyright (C) 2006 Henry Zhang
   This file is part of LibGTop 2.14.

   Contributed by Henry Zhang <hua.zhang@sun.com>, July 2006.

   LibGTop is free software; you can redistribute it and/or modify it
   under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License,
   or (at your option) any later version.

   LibGTop is distributed in the hope that it will be useful, but WITHOUT
   ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
   FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
   for more details.

   You should have received a copy of the GNU General Public License
   along with LibGTop; see the file COPYING. If not, write to the
   Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
*/

#include <config.h>
#include <glibtop.h>
#include <glibtop/error.h>
#include <glibtop/procopenfiles.h>
#include <sys/types.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <dirent.h>
#include <string.h>
#include <stdio.h>

#include "glibtop_private.h"

static const unsigned long _glibtop_sysdeps_proc_open_files =
(1L << GLIBTOP_PROC_OPEN_FILES_NUMBER)|
(1L << GLIBTOP_PROC_OPEN_FILES_TOTAL)|
(1L << GLIBTOP_PROC_OPEN_FILES_SIZE);

/* Init function. */

void
_glibtop_init_proc_open_files_s (glibtop *server)
{
	server->sysdeps.proc_open_files = _glibtop_sysdeps_proc_open_files;
}



/* Provides detailed information about a process' open files */

glibtop_open_files_entry *
glibtop_get_proc_open_files_s (glibtop *server, glibtop_proc_open_files *buf,	pid_t pid)
{
	char filename [BUFSIZ];
	GArray *entries;
	struct dirent *direntry;
	DIR *dir;
	int errno;

	glibtop_init_s (&server, GLIBTOP_SYSDEPS_PROC_OPEN_FILES, 0);

	memset (buf, 0, sizeof (glibtop_proc_open_files));

	sprintf (filename, "/proc/%d/fd", pid);

	dir = opendir (filename);
	if (!dir) return NULL;

	entries = g_array_new(FALSE, FALSE, sizeof(glibtop_open_files_entry));

	while((direntry = readdir(dir))) {
		char tgt [BUFSIZ];
		int rv;
		glibtop_open_files_entry entry = {0};
		struct stat statbuf;

		if(direntry->d_name[0] == '.')
			continue;

		if ((entry.fd = (int) g_ascii_strtoull(direntry->d_name, NULL, 10)) == 0)
			continue;
					
		/* from /path get object name */
		g_snprintf(filename, sizeof filename, "/proc/%d/path/%s",
			   pid, direntry->d_name);

		rv = readlink(filename, tgt, sizeof(tgt) - 1);
		/* read object, if not have, set it as NULL, but this fd still need to insert into the array */
		if(rv < 0) 
			rv = 0;
		tgt[rv] = '\0';
		
		/* from /fd get the stat data */
		g_snprintf(filename, sizeof filename, "/proc/%d/fd/%s",
			   pid, direntry->d_name);

		if(stat (filename, &statbuf))
			statbuf.st_mode = 0;		

		switch (statbuf.st_mode & S_IFMT) {
			case S_IFIFO:    /* pipe */
				entry.type = GLIBTOP_FILE_TYPE_PIPE;
				break;
			case S_IFSOCK:	/* socket */
			/* at solaris, now a little difficult to tell the Socket type, so here I 
			   give the type 0, it will not impact the existed code. Later will provide 
			   a patch to tell the type, and get the object name */
				entry.type = 0;
				break;
			default:
				entry.type = GLIBTOP_FILE_TYPE_FILE;
		}
				
		g_strlcpy(entry.info.file.name, tgt, sizeof entry.info.file.name);

		g_array_append_val(entries, entry);
	}

	closedir (dir);

	buf->flags = _glibtop_sysdeps_proc_open_files;
	buf->number = entries->len;
	buf->size = sizeof(glibtop_open_files_entry);
	buf->total = buf->number * buf->size;

	return (glibtop_open_files_entry*)g_array_free(entries, FALSE);
}