summaryrefslogtreecommitdiff
path: root/sysdeps/linux/procopenfiles.c
blob: 54f74254557e8868b99d656329c5f1536619cf34 (plain)
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/* Copyright (C) 1998-99 Martin Baulig
   Copyright (C) 2004 Nicolás Lichtmaier
   This file is part of LibGTop 1.0.

   Modified by Nicolás Lichtmaier to give a process open files.

   Contributed by Martin Baulig <martin@home-of-linux.org>, April 1998.

   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., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.
*/

#include <config.h>
#include <glibtop.h>
#include <glibtop/error.h>
#include <glibtop/procopenfiles.h>
#include <sys/types.h>
#include <dirent.h>
#include <string.h>
#include <stdio.h>
#include <arpa/inet.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;
}



typedef void (*LineParser)(GHashTable *dict, const char *line);


static void
parse_file(const char *filename, LineParser parser, GHashTable *dict)
{
	FILE *f;
	char *line = NULL;
	size_t size = 0;

	f = fopen(filename, "r");

	if(!f) {
		g_warning("Cannot open '%s'", filename);
		return;
	}


	/* skip the first line */
	if (getline(&line, &size, f) == -1)
		goto eof;

	while (getline(&line, &size, f) != -1)
		parser(dict, line);

 eof:
	free(line);
	fclose(f);
}


static GHashTable*
get_all(const char *filename, LineParser parser)
{
	GHashTable *dict;

	dict = g_hash_table_new_full(g_direct_hash, g_direct_equal,
				     NULL, g_free);

	parse_file(filename, parser, dict);

	return dict;
}





struct InetSocketEntry
{
	char host[GLIBTOP_OPEN_DEST_HOST_LEN + 1];
	int port;
};


static void
inet_socket_parser(GHashTable *dict, const char* line)
{
	struct InetSocketEntry *se;
	int sock;
	unsigned addr;

	se = g_malloc0(sizeof *se);

	if(sscanf(line, "%*d: %*x:%*x %8x:%4x %*x %*x:%*x %*x:%*x %*d %*d %*d %d",
		  &addr, &se->port, &sock) != 3)
		goto error;

	if(!inet_ntop(AF_INET, &addr, se->host, sizeof se->host))
		goto error;

	g_hash_table_insert(dict, GINT_TO_POINTER(sock), se);
	return;

 error:
	g_free(se);
}


static inline GHashTable *
get_all_inet_sockets()
{
	return get_all("/proc/net/tcp", inet_socket_parser);
}





struct LocalSocketEntry
{
	char name[GLIBTOP_OPEN_DEST_HOST_LEN + 1];
};


static void
local_socket_parser(GHashTable *dict, const char *line)
{
	int sock;
	struct LocalSocketEntry *use;
	char *p;

	use = g_malloc0(sizeof *use);

	/* dfaf1640: 00000003 00000000 00000000 0001 03  6457 /dev/log */
	p = skip_multiple_token(line, 6);

	sock = strtoul(p, &p, 10);
	g_strlcpy(use->name, p, sizeof use->name);
	g_strstrip(use->name);
	g_hash_table_insert(dict, GINT_TO_POINTER(sock), use);
}


static inline GHashTable *
get_all_local_sockets()
{
	return get_all("/proc/net/unix", local_socket_parser);
}



/* 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 fn [BUFSIZ];
	GArray *entries;
	GHashTable *inet_sockets = NULL, *local_sockets = NULL;
	struct dirent *direntry;
	DIR *dir;

	glibtop_init_s (&server, GLIBTOP_SYSDEPS_PROC_OPEN_FILES, 0);

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

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

	dir = opendir (fn);
	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};

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

		g_snprintf(fn, sizeof fn, "/proc/%d/fd/%s",
			   pid, direntry->d_name);

		rv = readlink(fn, tgt, sizeof(tgt) - 1);
		if(rv < 0) continue;
		tgt[rv] = '\0';

		entry.fd = atoi(direntry->d_name);

		if(g_str_has_prefix(tgt, "socket:["))
		{
			int sockfd;
			struct InetSocketEntry *ise;
			struct LocalSocketEntry *lse;

			if(!inet_sockets) inet_sockets = get_all_inet_sockets();
			if(!local_sockets) local_sockets = get_all_local_sockets();

			sockfd = atoi(tgt + 8);

			ise = g_hash_table_lookup(inet_sockets,
						 GINT_TO_POINTER(sockfd));

			if(ise) {
				entry.type = GLIBTOP_FILE_TYPE_INETSOCKET;
				entry.info.sock.dest_port = ise->port;
				g_strlcpy(entry.info.sock.dest_host, ise->host,
					  sizeof entry.info.sock.dest_host);
				goto found;
			}

			lse = g_hash_table_lookup(local_sockets,
						  GINT_TO_POINTER(sockfd));

			if(lse) {
				entry.type = GLIBTOP_FILE_TYPE_LOCALSOCKET;
				g_strlcpy(entry.info.localsock.name, lse->name,
					  sizeof entry.info.localsock.name);
				goto found;
			}

		found:
			(void)0; /* kills warning */
		}
		else if(g_str_has_prefix(tgt, "pipe:["))
		{
			entry.type = GLIBTOP_FILE_TYPE_PIPE;
		}
		else
		{
			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);

	if(inet_sockets) g_hash_table_destroy(inet_sockets);
	if(local_sockets) g_hash_table_destroy(local_sockets);

	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);
}