File: inode.c

package info (click to toggle)
memprof 0.4.1-3
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,376 kB
  • ctags: 782
  • sloc: sh: 7,570; ansic: 6,401; makefile: 255; sed: 93
file content (123 lines) | stat: -rw-r--r-- 2,782 bytes parent folder | download
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
/* -*- mode: C; c-file-style: "linux" -*- */

/* MemProf -- memory profiler and leak detector
 * Copyright (C) 1999 Red Hat, Inc.
 *
 * This program 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.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */
/*====*/

#include <glib.h>
#include <sys/stat.h>
#include <dirent.h>
#include "memprof.h"

/************************************************************
 * Inode finding code - not needed for kernel 2.2 or greater
 ************************************************************/

GHashTable *inode_table = NULL;

typedef struct {
	dev_t device;
	ino_t inode;
	gchar *name;
} Inode;

static guint
inode_hash (gconstpointer data)
{
	return (((Inode *)data)->device + (((Inode *)data)->inode << 11));
}

static gint
inode_compare (gconstpointer a, gconstpointer b)
{
	return ((((Inode *)a)->device == ((Inode *)b)->device) &&
		(((Inode *)a)->inode == ((Inode *)b)->inode));
}

void
read_inode (const gchar *path)
{
	struct stat stbuf;

	g_return_if_fail (path != NULL);

	if (!inode_table)
	        inode_table = g_hash_table_new (inode_hash, inode_compare);

	if (!stat (path, &stbuf)) {
		Inode *inode = g_new (Inode, 1);
		inode->device = stbuf.st_dev;
		inode->inode = stbuf.st_ino;
		if (!g_hash_table_lookup (inode_table, inode)) {
			inode->name = g_strdup (path);
			g_hash_table_insert (inode_table, inode, inode);
		} else
			g_free (inode);
	}
}

static void
read_inodes ()
{
	static const char *directories[] = {
		"/lib",
		"/usr/lib",
		"/usr/X11R6/lib",
		"/usr/local/lib",
		"/opt/gnome/lib",
		NULL
	};

	const char **dirname;

	for (dirname = directories; *dirname; dirname++)
	{
		DIR *dir = opendir (*dirname);
      
		if (dir) {
			struct dirent *ent;
			while ((ent = readdir (dir))) {
				gchar buf[1024];
				snprintf(buf, 1024-1, "%s/%s", *dirname, ent->d_name);
				read_inode (buf);
			}
	  
			closedir (dir);
		}
	}
}

gchar *
locate_inode (dev_t device, ino_t inode)
{
	Inode lookup;
	Inode *result;

	lookup.device = device;
	lookup.inode = inode;

	if (!inode_table)
		read_inodes ();
	
	result = g_hash_table_lookup (inode_table, &lookup);
	if (result)
		return result->name;
	else
		return NULL;
}