File: dt_kernel_module.c

package info (click to toggle)
dtrace 2.0.5-1
  • links: PTS
  • area: main
  • in suites: sid
  • size: 24,408 kB
  • sloc: ansic: 61,247; sh: 17,997; asm: 1,717; lex: 947; awk: 754; yacc: 695; perl: 37; sed: 17; makefile: 15
file content (222 lines) | stat: -rw-r--r-- 4,932 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
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
/*
 * Oracle Linux DTrace.
 * Copyright (c) 2009, 2025, Oracle and/or its affiliates. All rights reserved.
 * Licensed under the Universal Permissive License v 1.0 as shown at
 * http://oss.oracle.com/licenses/upl.
 */

/*
 * Kernel module list management.  We must maintain bindings from
 * name->filesystem path for all the current kernel's modules, since the system
 * maintains no such list and all mechanisms other than find(1)-analogues have
 * been deprecated or removed in kmod.  However, we can rely on modules.dep
 * to contain paths to all modules, in- or out-of-tree.
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <assert.h>
#include <dirent.h>
#include <errno.h>
#include <pthread.h>
#include <string.h>
#include <alloca.h>
#include <unistd.h>

#include <dt_kernel_module.h>
#include <dt_string.h>
#include <port.h>

static pthread_mutex_t kern_path_update_lock = PTHREAD_MUTEX_INITIALIZER;

static uint32_t
kernpath_hval(const dt_kern_path_t *dkp)
{
	return str2hval(dkp->dkp_name, 0);
}

static int
kernpath_cmp(const dt_kern_path_t *p,
	     const dt_kern_path_t *q)
{
	return strcmp(p->dkp_name, q->dkp_name);
}

DEFINE_HE_STD_LINK_FUNCS(kernpath, dt_kern_path_t, dkp_he)

static void *
kernpath_del_path(dt_kern_path_t *head, dt_kern_path_t *dkpp)
{
	head = kernpath_del(head, dkpp);
	free(dkpp->dkp_name);
	free(dkpp->dkp_path);
	free(dkpp);
	return head;
}

static dt_htab_ops_t kernpath_htab_ops = {
	.hval = (htab_hval_fn)kernpath_hval,
	.cmp = (htab_cmp_fn)kernpath_cmp,
	.add = (htab_add_fn)kernpath_add,
	.del = (htab_del_fn)kernpath_del_path,
	.next = (htab_next_fn)kernpath_next
};

/*
 * On successful return, name and path become owned by dt_kern_path_create()
 * (and may be freed immediately).  The returned entry is owned by the
 * dt_kernpaths hash into which it is interned.
 */

dt_kern_path_t *
dt_kern_path_create(dtrace_hdl_t *dtp, char *name, char *path)
{
	dt_kern_path_t *dkpp;
	dt_kern_path_t tmpl;

	if (!dtp->dt_kernpaths) {
		dtp->dt_kernpaths = dt_htab_create(&kernpath_htab_ops);

		if (!dtp->dt_kernpaths)
			return NULL; /* caller must handle allocation failure */
	}

	tmpl.dkp_name = name;
	if ((dkpp = dt_htab_lookup(dtp->dt_kernpaths, &tmpl)) != NULL) {
		free(name);
		free(path);
		return dkpp;
	}

	if ((dkpp = malloc(sizeof(dt_kern_path_t))) == NULL)
		return NULL;

	dt_dprintf("Adding %s -> %s\n", name, path);

	memset(dkpp, 0, sizeof(dt_kern_path_t));
	dkpp->dkp_name = name;
	dkpp->dkp_path = path;			/* strdup()ped by our caller */
	if (dt_htab_insert(dtp->dt_kernpaths, dkpp) < 0) {
		free(dkpp);
		return NULL;
	}

	return dkpp;
}

/*
 * Construct the mapping of kernel module name -> path for all modules,
 * by reading modules.dep.
 *
 * Must be called under the kern_path_update_lock.
 */
int
dt_kern_path_update(dtrace_hdl_t *dtp)
{
	FILE *dep;
	char *buf;
	char *depname;
	char *line = NULL;
	size_t n;
	size_t mplen = strlen(dtp->dt_module_path);
	int err;
	struct stat s;

	assert(MUTEX_HELD(&kern_path_update_lock));

	depname = alloca(strlen(dtp->dt_module_path) + strlen("/modules.dep") + 1);
	strcpy(depname, dtp->dt_module_path);
	strcat(depname, "/modules.dep");

	if (stat(depname, &s) < 0)
		return EDT_OBJIO;

	if ((buf = malloc(s.st_size + 1)) == NULL)
		return EDT_NOMEM;

	if ((dep = fopen(depname, "r")) == NULL) {
		free(buf);
		return EDT_OBJIO;
	}

	setvbuf(dep, buf, _IOFBF, s.st_size + 1);

	errno = 0;
	while (getline(&line, &n, dep) >= 0) {
		char *suffix;
		char *modname;
		char *modpath;
		char *p;

		suffix = strstr(line, ".ko:");
		if (suffix == NULL)
			suffix = strstr(line, ".ko.gz:");

		if (suffix == NULL)
			suffix = strstr(line, ".ko.xz:");

		if (suffix == NULL)
			continue;

		suffix[3] = '\0';		/* chop the dep components */

		modname = strrchr(line, '/');
		if (!modname)
			modname = line;
		else
			modname++;

		modname = strndup(modname, strlen(modname) - 3);
		if (errno == ENOMEM)
			break;

		modpath = malloc(mplen + 1 + strlen(line) + 1);
		if (!modpath) {
			free(modname);
			break; /* OOM */
		}

		p = stpcpy(modpath, dtp->dt_module_path);
		p = stpcpy(p, "/");
		strcpy(p, line);

		if (dt_kern_path_create(dtp, modname, modpath) == NULL)
			break; /* OOM */
	}
	err = errno;

	free(line);
	fclose(dep);
	free(buf);

	if (err == ENOMEM)
		return EDT_NOMEM;
	else
		return err;
}

dt_kern_path_t *
dt_kern_path_lookup_by_name(dtrace_hdl_t *dtp, const char *name)
{
	dt_kern_path_t tmpl;

	if (!dtp->dt_kernpaths) {
		int dterrno;

		pthread_mutex_lock(&kern_path_update_lock);
		dterrno = dt_kern_path_update(dtp);
		pthread_mutex_unlock(&kern_path_update_lock);

		if (dterrno != 0) {
			dt_dprintf("Error initializing kernel module paths: "
			    "%s\n", dtrace_errmsg(dtp, dterrno));
			return NULL;
		}

		dt_dprintf("Initialized %zi kernel module paths\n",
		    dt_htab_entries(dtp->dt_kernpaths));
	}

	tmpl.dkp_name = (char *) name;
	return dt_htab_lookup(dtp->dt_kernpaths, &tmpl);
}