]>
Commit | Line | Data |
---|---|---|
8f788d58 LDM |
1 | /* |
2 | * libkmod - interface to kernel module operations | |
3 | * | |
e6b0e49b | 4 | * Copyright (C) 2011-2013 ProFUSION embedded systems |
8f788d58 LDM |
5 | * |
6 | * This library is free software; you can redistribute it and/or | |
7 | * modify it under the terms of the GNU Lesser General Public | |
cb451f35 LDM |
8 | * License as published by the Free Software Foundation; either |
9 | * version 2.1 of the License, or (at your option) any later version. | |
8f788d58 LDM |
10 | * |
11 | * This library is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | * Lesser General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU Lesser General Public | |
17 | * License along with this library; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | */ | |
20 | ||
7636e72b | 21 | #include <assert.h> |
8f788d58 LDM |
22 | #include <stdio.h> |
23 | #include <stdlib.h> | |
24 | #include <stddef.h> | |
25 | #include <stdarg.h> | |
26 | #include <unistd.h> | |
27 | #include <errno.h> | |
28 | #include <string.h> | |
29 | #include <ctype.h> | |
30 | #include <inttypes.h> | |
f12ae3c4 GSB |
31 | #include <limits.h> |
32 | #include <dirent.h> | |
8f788d58 LDM |
33 | #include <sys/stat.h> |
34 | #include <sys/types.h> | |
35 | #include <sys/mman.h> | |
144d1826 | 36 | #include <sys/syscall.h> |
eff917c0 | 37 | #include <sys/wait.h> |
8f788d58 | 38 | #include <string.h> |
1c522600 | 39 | #include <fnmatch.h> |
8f788d58 | 40 | |
144d1826 KC |
41 | #ifdef HAVE_LINUX_MODULE_H |
42 | #include <linux/module.h> | |
43 | #endif | |
44 | ||
8f788d58 LDM |
45 | #include "libkmod.h" |
46 | #include "libkmod-private.h" | |
47 | ||
6681951b LDM |
48 | /** |
49 | * SECTION:libkmod-module | |
50 | * @short_description: operate on kernel modules | |
51 | */ | |
52 | ||
8f788d58 LDM |
53 | /** |
54 | * kmod_module: | |
55 | * | |
56 | * Opaque object representing a module. | |
57 | */ | |
58 | struct kmod_module { | |
59 | struct kmod_ctx *ctx; | |
8bdeca11 | 60 | char *hashkey; |
219f9c38 | 61 | char *name; |
f1fb6f85 | 62 | char *path; |
7636e72b | 63 | struct kmod_list *dep; |
bd3f5535 | 64 | char *options; |
60f6760e LDM |
65 | const char *install_commands; /* owned by kmod_config */ |
66 | const char *remove_commands; /* owned by kmod_config */ | |
6ad5f263 | 67 | char *alias; /* only set if this module was created from an alias */ |
1eff942e | 68 | struct kmod_file *file; |
b6a534f7 | 69 | int n_dep; |
bd3f5535 | 70 | int refcount; |
7636e72b LDM |
71 | struct { |
72 | bool dep : 1; | |
bd3f5535 GSB |
73 | bool options : 1; |
74 | bool install_commands : 1; | |
75 | bool remove_commands : 1; | |
7636e72b | 76 | } init; |
b1a51256 LDM |
77 | |
78 | /* | |
79 | * private field used by kmod_module_get_probe_list() to detect | |
80 | * dependency loops | |
81 | */ | |
ece09aac | 82 | bool visited : 1; |
89e92487 LDM |
83 | |
84 | /* | |
85 | * set by kmod_module_get_probe_list: indicates for probe_insert() | |
86 | * whether the module's command and softdep should be ignored | |
87 | */ | |
88 | bool ignorecmd : 1; | |
3805274b LDM |
89 | |
90 | /* | |
91 | * if module was created by searching the modules.builtin file, this | |
92 | * is set. There's nothing much useful one can do with such a | |
93 | * "module", except knowing it's builtin. | |
94 | */ | |
95 | bool builtin : 1; | |
8f788d58 LDM |
96 | }; |
97 | ||
c35347f1 LDM |
98 | static inline const char *path_join(const char *path, size_t prefixlen, |
99 | char buf[PATH_MAX]) | |
e18ad35c GSB |
100 | { |
101 | size_t pathlen; | |
102 | ||
103 | if (path[0] == '/') | |
104 | return path; | |
105 | ||
106 | pathlen = strlen(path); | |
107 | if (prefixlen + pathlen + 1 >= PATH_MAX) | |
108 | return NULL; | |
109 | ||
110 | memcpy(buf + prefixlen, path, pathlen + 1); | |
111 | return buf; | |
112 | } | |
113 | ||
af9572c6 DR |
114 | static inline bool module_is_inkernel(struct kmod_module *mod) |
115 | { | |
116 | int state = kmod_module_get_initstate(mod); | |
3805274b | 117 | |
af9572c6 | 118 | if (state == KMOD_MODULE_LIVE || |
af9572c6 DR |
119 | state == KMOD_MODULE_BUILTIN) |
120 | return true; | |
3805274b LDM |
121 | |
122 | return false; | |
af9572c6 DR |
123 | } |
124 | ||
671d4894 | 125 | int kmod_module_parse_depline(struct kmod_module *mod, char *line) |
7636e72b LDM |
126 | { |
127 | struct kmod_ctx *ctx = mod->ctx; | |
128 | struct kmod_list *list = NULL; | |
e18ad35c GSB |
129 | const char *dirname; |
130 | char buf[PATH_MAX]; | |
7636e72b | 131 | char *p, *saveptr; |
45f27781 | 132 | int err = 0, n = 0; |
e18ad35c | 133 | size_t dirnamelen; |
7636e72b | 134 | |
b6a534f7 GSB |
135 | if (mod->init.dep) |
136 | return mod->n_dep; | |
137 | assert(mod->dep == NULL); | |
7636e72b LDM |
138 | mod->init.dep = true; |
139 | ||
140 | p = strchr(line, ':'); | |
141 | if (p == NULL) | |
142 | return 0; | |
143 | ||
671d4894 | 144 | *p = '\0'; |
e18ad35c GSB |
145 | dirname = kmod_get_dirname(mod->ctx); |
146 | dirnamelen = strlen(dirname); | |
147 | if (dirnamelen + 2 >= PATH_MAX) | |
148 | return 0; | |
28c175ed | 149 | |
e18ad35c GSB |
150 | memcpy(buf, dirname, dirnamelen); |
151 | buf[dirnamelen] = '/'; | |
152 | dirnamelen++; | |
153 | buf[dirnamelen] = '\0'; | |
154 | ||
155 | if (mod->path == NULL) { | |
156 | const char *str = path_join(line, dirnamelen, buf); | |
157 | if (str == NULL) | |
158 | return 0; | |
159 | mod->path = strdup(str); | |
160 | if (mod->path == NULL) | |
161 | return 0; | |
162 | } | |
671d4894 | 163 | |
7636e72b | 164 | p++; |
7636e72b LDM |
165 | for (p = strtok_r(p, " \t", &saveptr); p != NULL; |
166 | p = strtok_r(NULL, " \t", &saveptr)) { | |
1fc1c9a0 | 167 | struct kmod_module *depmod; |
e18ad35c GSB |
168 | const char *path; |
169 | ||
170 | path = path_join(p, dirnamelen, buf); | |
171 | if (path == NULL) { | |
172 | ERR(ctx, "could not join path '%s' and '%s'.\n", | |
173 | dirname, p); | |
174 | goto fail; | |
175 | } | |
7636e72b | 176 | |
e18ad35c | 177 | err = kmod_module_new_from_path(ctx, path, &depmod); |
7636e72b | 178 | if (err < 0) { |
e18ad35c GSB |
179 | ERR(ctx, "ctx=%p path=%s error=%s\n", |
180 | ctx, path, strerror(-err)); | |
7636e72b LDM |
181 | goto fail; |
182 | } | |
183 | ||
e18ad35c | 184 | DBG(ctx, "add dep: %s\n", path); |
7636e72b | 185 | |
b94a7379 | 186 | list = kmod_list_prepend(list, depmod); |
7636e72b LDM |
187 | n++; |
188 | } | |
189 | ||
190 | DBG(ctx, "%d dependencies for %s\n", n, mod->name); | |
191 | ||
192 | mod->dep = list; | |
b6a534f7 | 193 | mod->n_dep = n; |
7636e72b LDM |
194 | return n; |
195 | ||
196 | fail: | |
197 | kmod_module_unref_list(list); | |
198 | mod->init.dep = false; | |
199 | return err; | |
200 | } | |
201 | ||
ece09aac LDM |
202 | void kmod_module_set_visited(struct kmod_module *mod, bool visited) |
203 | { | |
204 | mod->visited = visited; | |
205 | } | |
206 | ||
3805274b LDM |
207 | void kmod_module_set_builtin(struct kmod_module *mod, bool builtin) |
208 | { | |
209 | mod->builtin = builtin; | |
210 | } | |
211 | ||
9c7f3ad0 LDM |
212 | /* |
213 | * Memory layout with alias: | |
214 | * | |
215 | * struct kmod_module { | |
216 | * hashkey -----. | |
217 | * alias -----. | | |
218 | * name ----. | | | |
219 | * } | | | | |
220 | * name <----------' | | | |
221 | * alias <-----------' | | |
222 | * name\alias <--------' | |
223 | * | |
224 | * Memory layout without alias: | |
225 | * | |
226 | * struct kmod_module { | |
227 | * hashkey ---. | |
228 | * alias -----|----> NULL | |
229 | * name ----. | | |
230 | * } | | | |
231 | * name <----------'-' | |
232 | * | |
233 | * @key is "name\alias" or "name" (in which case alias == NULL) | |
234 | */ | |
235 | static int kmod_module_new(struct kmod_ctx *ctx, const char *key, | |
236 | const char *name, size_t namelen, | |
237 | const char *alias, size_t aliaslen, | |
238 | struct kmod_module **mod) | |
239 | { | |
240 | struct kmod_module *m; | |
241 | size_t keylen; | |
242 | ||
243 | m = kmod_pool_get_module(ctx, key); | |
244 | if (m != NULL) { | |
245 | *mod = kmod_module_ref(m); | |
246 | return 0; | |
247 | } | |
248 | ||
249 | if (alias == NULL) | |
250 | keylen = namelen; | |
251 | else | |
252 | keylen = namelen + aliaslen + 1; | |
253 | ||
254 | m = malloc(sizeof(*m) + (alias == NULL ? 1 : 2) * (keylen + 1)); | |
255 | if (m == NULL) { | |
256 | free(m); | |
257 | return -ENOMEM; | |
258 | } | |
259 | ||
260 | memset(m, 0, sizeof(*m)); | |
261 | ||
262 | m->ctx = kmod_ref(ctx); | |
263 | m->name = (char *)m + sizeof(*m); | |
264 | memcpy(m->name, key, keylen + 1); | |
265 | if (alias == NULL) { | |
266 | m->hashkey = m->name; | |
267 | m->alias = NULL; | |
268 | } else { | |
269 | m->name[namelen] = '\0'; | |
270 | m->alias = m->name + namelen + 1; | |
271 | m->hashkey = m->name + keylen + 1; | |
272 | memcpy(m->hashkey, key, keylen + 1); | |
273 | } | |
274 | ||
275 | m->refcount = 1; | |
276 | kmod_pool_add_module(ctx, m, m->hashkey); | |
277 | *mod = m; | |
278 | ||
279 | return 0; | |
280 | } | |
281 | ||
7afc98a1 LDM |
282 | /** |
283 | * kmod_module_new_from_name: | |
284 | * @ctx: kmod library context | |
285 | * @name: name of the module | |
286 | * @mod: where to save the created struct kmod_module | |
287 | * | |
288 | * Create a new struct kmod_module using the module name. @name can not be an | |
289 | * alias, file name or anything else; it must be a module name. There's no | |
9a252c21 | 290 | * check if the module exists in the system. |
7afc98a1 LDM |
291 | * |
292 | * This function is also used internally by many others that return a new | |
293 | * struct kmod_module or a new list of modules. | |
294 | * | |
295 | * The initial refcount is 1, and needs to be decremented to release the | |
296 | * resources of the kmod_module. Since libkmod keeps track of all | |
297 | * kmod_modules created, they are all released upon @ctx destruction too. Do | |
298 | * not unref @ctx before all the desired operations with the returned | |
299 | * kmod_module are done. | |
300 | * | |
301 | * Returns: 0 on success or < 0 otherwise. It fails if name is not a valid | |
302 | * module name or if memory allocation failed. | |
303 | */ | |
8f788d58 LDM |
304 | KMOD_EXPORT int kmod_module_new_from_name(struct kmod_ctx *ctx, |
305 | const char *name, | |
306 | struct kmod_module **mod) | |
307 | { | |
d753b8ca | 308 | size_t namelen; |
6daceb2f | 309 | char name_norm[PATH_MAX]; |
8f788d58 | 310 | |
818f8e8a | 311 | if (ctx == NULL || name == NULL || mod == NULL) |
8f788d58 LDM |
312 | return -ENOENT; |
313 | ||
9c7f3ad0 | 314 | modname_normalize(name, name_norm, &namelen); |
113c66a5 | 315 | |
9c7f3ad0 | 316 | return kmod_module_new(ctx, name_norm, name_norm, namelen, NULL, 0, mod); |
8f788d58 LDM |
317 | } |
318 | ||
6ad5f263 LDM |
319 | int kmod_module_new_from_alias(struct kmod_ctx *ctx, const char *alias, |
320 | const char *name, struct kmod_module **mod) | |
321 | { | |
322 | int err; | |
6daceb2f | 323 | char key[PATH_MAX]; |
113c66a5 LDM |
324 | size_t namelen = strlen(name); |
325 | size_t aliaslen = strlen(alias); | |
6ad5f263 | 326 | |
6daceb2f | 327 | if (namelen + aliaslen + 2 > PATH_MAX) |
113c66a5 | 328 | return -ENAMETOOLONG; |
6ad5f263 | 329 | |
113c66a5 LDM |
330 | memcpy(key, name, namelen); |
331 | memcpy(key + namelen + 1, alias, aliaslen + 1); | |
9c7f3ad0 | 332 | key[namelen] = '\\'; |
6ad5f263 | 333 | |
9c7f3ad0 | 334 | err = kmod_module_new(ctx, key, name, namelen, alias, aliaslen, mod); |
113c66a5 LDM |
335 | if (err < 0) |
336 | return err; | |
6ad5f263 LDM |
337 | |
338 | return 0; | |
6ad5f263 LDM |
339 | } |
340 | ||
7afc98a1 LDM |
341 | /** |
342 | * kmod_module_new_from_path: | |
343 | * @ctx: kmod library context | |
344 | * @path: path where to find the given module | |
345 | * @mod: where to save the created struct kmod_module | |
346 | * | |
347 | * Create a new struct kmod_module using the module path. @path must be an | |
348 | * existent file with in the filesystem and must be accessible to libkmod. | |
349 | * | |
350 | * The initial refcount is 1, and needs to be decremented to release the | |
351 | * resources of the kmod_module. Since libkmod keeps track of all | |
352 | * kmod_modules created, they are all released upon @ctx destruction too. Do | |
353 | * not unref @ctx before all the desired operations with the returned | |
354 | * kmod_module are done. | |
355 | * | |
356 | * If @path is relative, it's treated as relative to the current working | |
357 | * directory. Otherwise, give an absolute path. | |
358 | * | |
359 | * Returns: 0 on success or < 0 otherwise. It fails if file does not exist, if | |
360 | * it's not a valid file for a kmod_module or if memory allocation failed. | |
361 | */ | |
8f788d58 LDM |
362 | KMOD_EXPORT int kmod_module_new_from_path(struct kmod_ctx *ctx, |
363 | const char *path, | |
364 | struct kmod_module **mod) | |
365 | { | |
366 | struct kmod_module *m; | |
367 | int err; | |
368 | struct stat st; | |
6daceb2f | 369 | char name[PATH_MAX]; |
71e975cd | 370 | char *abspath; |
d753b8ca | 371 | size_t namelen; |
8f788d58 | 372 | |
818f8e8a | 373 | if (ctx == NULL || path == NULL || mod == NULL) |
8f788d58 LDM |
374 | return -ENOENT; |
375 | ||
71e975cd | 376 | abspath = path_make_absolute_cwd(path); |
b55df2ee GSB |
377 | if (abspath == NULL) { |
378 | DBG(ctx, "no absolute path for %s\n", path); | |
71e975cd | 379 | return -ENOMEM; |
b55df2ee | 380 | } |
71e975cd LDM |
381 | |
382 | err = stat(abspath, &st); | |
383 | if (err < 0) { | |
b55df2ee GSB |
384 | err = -errno; |
385 | DBG(ctx, "stat %s: %s\n", path, strerror(errno)); | |
71e975cd | 386 | free(abspath); |
b55df2ee | 387 | return err; |
71e975cd | 388 | } |
8f788d58 | 389 | |
973c80ba | 390 | if (path_to_modname(path, name, &namelen) == NULL) { |
b55df2ee | 391 | DBG(ctx, "could not get modname from path %s\n", path); |
973c80ba GSB |
392 | free(abspath); |
393 | return -ENOENT; | |
394 | } | |
d753b8ca | 395 | |
fd186ae9 LDM |
396 | m = kmod_pool_get_module(ctx, name); |
397 | if (m != NULL) { | |
6bd0b8d0 LDM |
398 | if (m->path == NULL) |
399 | m->path = abspath; | |
400 | else if (streq(m->path, abspath)) | |
401 | free(abspath); | |
402 | else { | |
ebaa7beb | 403 | ERR(ctx, "kmod_module '%s' already exists with different path: new-path='%s' old-path='%s'\n", |
9c7f3ad0 | 404 | name, abspath, m->path); |
6bd0b8d0 LDM |
405 | free(abspath); |
406 | return -EEXIST; | |
407 | } | |
408 | ||
fd186ae9 LDM |
409 | *mod = kmod_module_ref(m); |
410 | return 0; | |
411 | } | |
412 | ||
9c7f3ad0 LDM |
413 | err = kmod_module_new(ctx, name, name, namelen, NULL, 0, &m); |
414 | if (err < 0) | |
415 | return err; | |
788ef0f7 | 416 | |
71e975cd | 417 | m->path = abspath; |
8f788d58 LDM |
418 | *mod = m; |
419 | ||
420 | return 0; | |
421 | } | |
422 | ||
7afc98a1 LDM |
423 | /** |
424 | * kmod_module_unref: | |
425 | * @mod: kmod module | |
426 | * | |
427 | * Drop a reference of the kmod module. If the refcount reaches zero, its | |
428 | * resources are released. | |
429 | * | |
430 | * Returns: NULL if @mod is NULL or if the module was released. Otherwise it | |
431 | * returns the passed @mod with its refcount decremented. | |
432 | */ | |
8f788d58 LDM |
433 | KMOD_EXPORT struct kmod_module *kmod_module_unref(struct kmod_module *mod) |
434 | { | |
435 | if (mod == NULL) | |
436 | return NULL; | |
437 | ||
438 | if (--mod->refcount > 0) | |
439 | return mod; | |
440 | ||
441 | DBG(mod->ctx, "kmod_module %p released\n", mod); | |
442 | ||
4084c176 | 443 | kmod_pool_del_module(mod->ctx, mod, mod->hashkey); |
7636e72b | 444 | kmod_module_unref_list(mod->dep); |
1eff942e LDM |
445 | |
446 | if (mod->file) | |
447 | kmod_file_unref(mod->file); | |
448 | ||
8f788d58 | 449 | kmod_unref(mod->ctx); |
bd3f5535 | 450 | free(mod->options); |
f1fb6f85 | 451 | free(mod->path); |
8f788d58 LDM |
452 | free(mod); |
453 | return NULL; | |
454 | } | |
455 | ||
7afc98a1 LDM |
456 | /** |
457 | * kmod_module_ref: | |
458 | * @mod: kmod module | |
459 | * | |
460 | * Take a reference of the kmod module, incrementing its refcount. | |
461 | * | |
462 | * Returns: the passed @module with its refcount incremented. | |
463 | */ | |
8f788d58 LDM |
464 | KMOD_EXPORT struct kmod_module *kmod_module_ref(struct kmod_module *mod) |
465 | { | |
466 | if (mod == NULL) | |
467 | return NULL; | |
468 | ||
469 | mod->refcount++; | |
470 | ||
471 | return mod; | |
472 | } | |
473 | ||
b14dcfda LDM |
474 | #define CHECK_ERR_AND_FINISH(_err, _label_err, _list, label_finish) \ |
475 | do { \ | |
476 | if ((_err) < 0) \ | |
477 | goto _label_err; \ | |
478 | if (*(_list) != NULL) \ | |
479 | goto finish; \ | |
480 | } while (0) | |
481 | ||
7afc98a1 LDM |
482 | /** |
483 | * kmod_module_new_from_lookup: | |
484 | * @ctx: kmod library context | |
485 | * @given_alias: alias to look for | |
486 | * @list: an empty list where to save the list of modules matching | |
487 | * @given_alias | |
488 | * | |
489 | * Create a new list of kmod modules using an alias or module name and lookup | |
490 | * libkmod's configuration files and indexes in order to find the module. | |
491 | * Once it's found in one of the places, it stops searching and create the | |
492 | * list of modules that is saved in @list. | |
493 | * | |
494 | * The search order is: 1. aliases in configuration file; 2. module names in | |
495 | * modules.dep index; 3. symbol aliases in modules.symbols index; 4. aliases | |
496 | * in modules.alias index. | |
497 | * | |
498 | * The initial refcount is 1, and needs to be decremented to release the | |
499 | * resources of the kmod_module. The returned @list must be released by | |
500 | * calling kmod_module_unref_list(). Since libkmod keeps track of all | |
501 | * kmod_modules created, they are all released upon @ctx destruction too. Do | |
502 | * not unref @ctx before all the desired operations with the returned list are | |
503 | * completed. | |
504 | * | |
505 | * Returns: 0 on success or < 0 otherwise. It fails if any of the lookup | |
506 | * methods failed, which is basically due to memory allocation fail. If module | |
507 | * is not found, it still returns 0, but @list is an empty list. | |
508 | */ | |
7f3eb0cc | 509 | KMOD_EXPORT int kmod_module_new_from_lookup(struct kmod_ctx *ctx, |
d917f274 | 510 | const char *given_alias, |
7f3eb0cc LDM |
511 | struct kmod_list **list) |
512 | { | |
7f3eb0cc | 513 | int err; |
6daceb2f | 514 | char alias[PATH_MAX]; |
7f3eb0cc | 515 | |
4308b176 | 516 | if (ctx == NULL || given_alias == NULL) |
7f3eb0cc LDM |
517 | return -ENOENT; |
518 | ||
7f3eb0cc LDM |
519 | if (list == NULL || *list != NULL) { |
520 | ERR(ctx, "An empty list is needed to create lookup\n"); | |
521 | return -ENOSYS; | |
522 | } | |
523 | ||
b55df2ee GSB |
524 | if (alias_normalize(given_alias, alias, NULL) < 0) { |
525 | DBG(ctx, "invalid alias: %s\n", given_alias); | |
d470db10 | 526 | return -EINVAL; |
b55df2ee GSB |
527 | } |
528 | ||
529 | DBG(ctx, "input alias=%s, normalized=%s\n", given_alias, alias); | |
d917f274 | 530 | |
b14dcfda | 531 | /* Aliases from config file override all the others */ |
7f3eb0cc | 532 | err = kmod_lookup_alias_from_config(ctx, alias, list); |
b14dcfda | 533 | CHECK_ERR_AND_FINISH(err, fail, list, finish); |
7f3eb0cc | 534 | |
b55df2ee | 535 | DBG(ctx, "lookup modules.dep %s\n", alias); |
64700e47 LDM |
536 | err = kmod_lookup_alias_from_moddep_file(ctx, alias, list); |
537 | CHECK_ERR_AND_FINISH(err, fail, list, finish); | |
538 | ||
b55df2ee | 539 | DBG(ctx, "lookup modules.symbols %s\n", alias); |
9ba6f57b LDM |
540 | err = kmod_lookup_alias_from_symbols_file(ctx, alias, list); |
541 | CHECK_ERR_AND_FINISH(err, fail, list, finish); | |
b14dcfda | 542 | |
b55df2ee | 543 | DBG(ctx, "lookup install and remove commands %s\n", alias); |
f4fc5523 LDM |
544 | err = kmod_lookup_alias_from_commands(ctx, alias, list); |
545 | CHECK_ERR_AND_FINISH(err, fail, list, finish); | |
7afc98a1 | 546 | |
b55df2ee | 547 | DBG(ctx, "lookup modules.aliases %s\n", alias); |
49e61ca3 LDM |
548 | err = kmod_lookup_alias_from_aliases_file(ctx, alias, list); |
549 | CHECK_ERR_AND_FINISH(err, fail, list, finish); | |
550 | ||
3805274b LDM |
551 | DBG(ctx, "lookup modules.builtin %s\n", alias); |
552 | err = kmod_lookup_alias_from_builtin_file(ctx, alias, list); | |
553 | CHECK_ERR_AND_FINISH(err, fail, list, finish); | |
554 | ||
b14dcfda | 555 | finish: |
b55df2ee | 556 | DBG(ctx, "lookup %s=%d, list=%p\n", alias, err, *list); |
7f3eb0cc | 557 | return err; |
b14dcfda | 558 | fail: |
b55df2ee | 559 | DBG(ctx, "Failed to lookup %s\n", alias); |
b14dcfda LDM |
560 | kmod_module_unref_list(*list); |
561 | *list = NULL; | |
84f42204 | 562 | return err; |
7f3eb0cc | 563 | } |
b14dcfda LDM |
564 | #undef CHECK_ERR_AND_FINISH |
565 | ||
7afc98a1 | 566 | /** |
91428ae1 | 567 | * kmod_module_unref_list: |
7afc98a1 LDM |
568 | * @list: list of kmod modules |
569 | * | |
570 | * Drop a reference of each kmod module in @list and releases the resources | |
571 | * taken by the list itself. | |
572 | * | |
573 | * Returns: NULL if @mod is NULL or if the module was released. Otherwise it | |
574 | * returns the passed @mod with its refcount decremented. | |
575 | */ | |
7f3eb0cc LDM |
576 | KMOD_EXPORT int kmod_module_unref_list(struct kmod_list *list) |
577 | { | |
578 | for (; list != NULL; list = kmod_list_remove(list)) | |
579 | kmod_module_unref(list->data); | |
580 | ||
581 | return 0; | |
582 | } | |
583 | ||
0d46743c LDM |
584 | /** |
585 | * kmod_module_get_filtered_blacklist: | |
586 | * @ctx: kmod library context | |
587 | * @input: list of kmod_module to be filtered with blacklist | |
588 | * @output: where to save the new list | |
589 | * | |
471a7d00 | 590 | * This function should not be used. Use kmod_module_apply_filter instead. |
d80b103c | 591 | * |
0d46743c | 592 | * Given a list @input, this function filter it out with config's blacklist |
d80b103c | 593 | * and save it in @output. |
0d46743c LDM |
594 | * |
595 | * Returns: 0 on success or < 0 otherwise. @output is saved with the updated | |
596 | * list. | |
597 | */ | |
598 | KMOD_EXPORT int kmod_module_get_filtered_blacklist(const struct kmod_ctx *ctx, | |
599 | const struct kmod_list *input, | |
600 | struct kmod_list **output) | |
601 | { | |
d80b103c | 602 | return kmod_module_apply_filter(ctx, KMOD_FILTER_BLACKLIST, input, output); |
0d46743c LDM |
603 | } |
604 | ||
b72f74b5 LDM |
605 | static const struct kmod_list *module_get_dependencies_noref(const struct kmod_module *mod) |
606 | { | |
607 | if (!mod->init.dep) { | |
608 | /* lazy init */ | |
609 | char *line = kmod_search_moddep(mod->ctx, mod->name); | |
610 | ||
611 | if (line == NULL) | |
612 | return NULL; | |
613 | ||
614 | kmod_module_parse_depline((struct kmod_module *)mod, line); | |
615 | free(line); | |
616 | ||
617 | if (!mod->init.dep) | |
618 | return NULL; | |
619 | } | |
620 | ||
621 | return mod->dep; | |
622 | } | |
623 | ||
7afc98a1 | 624 | /** |
91428ae1 | 625 | * kmod_module_get_dependencies: |
7afc98a1 LDM |
626 | * @mod: kmod module |
627 | * | |
628 | * Search the modules.dep index to find the dependencies of the given @mod. | |
629 | * The result is cached in @mod, so subsequent calls to this function will | |
630 | * return the already searched list of modules. | |
631 | * | |
632 | * Returns: NULL on failure or if there are any dependencies. Otherwise it | |
633 | * returns a list of kmod modules that can be released by calling | |
634 | * kmod_module_unref_list(). | |
635 | */ | |
f1cd799f | 636 | KMOD_EXPORT struct kmod_list *kmod_module_get_dependencies(const struct kmod_module *mod) |
0835fc3b | 637 | { |
f1cd799f LDM |
638 | struct kmod_list *l, *l_new, *list_new = NULL; |
639 | ||
640 | if (mod == NULL) | |
641 | return NULL; | |
642 | ||
b72f74b5 | 643 | module_get_dependencies_noref(mod); |
f1cd799f LDM |
644 | |
645 | kmod_list_foreach(l, mod->dep) { | |
646 | l_new = kmod_list_append(list_new, kmod_module_ref(l->data)); | |
647 | if (l_new == NULL) { | |
648 | kmod_module_unref(l->data); | |
649 | goto fail; | |
650 | } | |
651 | ||
652 | list_new = l_new; | |
653 | } | |
654 | ||
655 | return list_new; | |
656 | ||
657 | fail: | |
658 | ERR(mod->ctx, "out of memory\n"); | |
659 | kmod_module_unref_list(list_new); | |
660 | return NULL; | |
0835fc3b LDM |
661 | } |
662 | ||
7afc98a1 LDM |
663 | /** |
664 | * kmod_module_get_module: | |
665 | * @entry: an entry in a list of kmod modules. | |
666 | * | |
667 | * Get the kmod module of this @entry in the list, increasing its refcount. | |
668 | * After it's used, unref it. Since the refcount is incremented upon return, | |
669 | * you still have to call kmod_module_unref_list() to release the list of kmod | |
670 | * modules. | |
671 | * | |
672 | * Returns: NULL on failure or the kmod_module contained in this list entry | |
673 | * with its refcount incremented. | |
674 | */ | |
ad4d1ae5 | 675 | KMOD_EXPORT struct kmod_module *kmod_module_get_module(const struct kmod_list *entry) |
6e869df7 | 676 | { |
ad4d1ae5 GSB |
677 | if (entry == NULL) |
678 | return NULL; | |
28c175ed | 679 | |
ad4d1ae5 | 680 | return kmod_module_ref(entry->data); |
6e869df7 LDM |
681 | } |
682 | ||
7afc98a1 LDM |
683 | /** |
684 | * kmod_module_get_name: | |
685 | * @mod: kmod module | |
686 | * | |
687 | * Get the name of this kmod module. Name is always available, independently | |
688 | * if it was created by kmod_module_new_from_name() or another function and | |
689 | * it's always normalized (dashes are replaced with underscores). | |
690 | * | |
691 | * Returns: the name of this kmod module. | |
692 | */ | |
1ce08a56 | 693 | KMOD_EXPORT const char *kmod_module_get_name(const struct kmod_module *mod) |
6e869df7 | 694 | { |
818f8e8a LDM |
695 | if (mod == NULL) |
696 | return NULL; | |
697 | ||
6e869df7 LDM |
698 | return mod->name; |
699 | } | |
700 | ||
7afc98a1 LDM |
701 | /** |
702 | * kmod_module_get_path: | |
703 | * @mod: kmod module | |
704 | * | |
705 | * Get the path of this kmod module. If this kmod module was not created by | |
706 | * path, it can search the modules.dep index in order to find out the module | |
db74ceec | 707 | * under context's dirname. |
7afc98a1 LDM |
708 | * |
709 | * Returns: the path of this kmod module or NULL if such information is not | |
710 | * available. | |
711 | */ | |
1ce08a56 | 712 | KMOD_EXPORT const char *kmod_module_get_path(const struct kmod_module *mod) |
6e869df7 | 713 | { |
e005facd | 714 | char *line; |
c5e7b1f7 | 715 | |
818f8e8a LDM |
716 | if (mod == NULL) |
717 | return NULL; | |
718 | ||
d01c67e3 | 719 | DBG(mod->ctx, "name='%s' path='%s'\n", mod->name, mod->path); |
c5e7b1f7 | 720 | |
e005facd LDM |
721 | if (mod->path != NULL) |
722 | return mod->path; | |
723 | if (mod->init.dep) | |
724 | return NULL; | |
c5e7b1f7 | 725 | |
e005facd LDM |
726 | /* lazy init */ |
727 | line = kmod_search_moddep(mod->ctx, mod->name); | |
728 | if (line == NULL) | |
729 | return NULL; | |
730 | ||
731 | kmod_module_parse_depline((struct kmod_module *) mod, line); | |
732 | free(line); | |
c5e7b1f7 | 733 | |
6e869df7 LDM |
734 | return mod->path; |
735 | } | |
736 | ||
737 | ||
8f788d58 LDM |
738 | extern long delete_module(const char *name, unsigned int flags); |
739 | ||
7afc98a1 LDM |
740 | /** |
741 | * kmod_module_remove_module: | |
742 | * @mod: kmod module | |
743 | * @flags: flags to pass to Linux kernel when removing the module | |
744 | * | |
745 | * Remove a module from Linux kernel. | |
746 | * | |
747 | * Returns: 0 on success or < 0 on failure. | |
748 | */ | |
8f788d58 LDM |
749 | KMOD_EXPORT int kmod_module_remove_module(struct kmod_module *mod, |
750 | unsigned int flags) | |
751 | { | |
752 | int err; | |
8f788d58 LDM |
753 | |
754 | if (mod == NULL) | |
755 | return -ENOENT; | |
756 | ||
757 | /* Filter out other flags */ | |
758 | flags &= (KMOD_REMOVE_FORCE | KMOD_REMOVE_NOWAIT); | |
759 | ||
d753b8ca | 760 | err = delete_module(mod->name, flags); |
8f788d58 | 761 | if (err != 0) { |
ba998b9c LDM |
762 | err = -errno; |
763 | ERR(mod->ctx, "could not remove '%s': %m\n", mod->name); | |
8f788d58 LDM |
764 | } |
765 | ||
ba998b9c | 766 | return err; |
8f788d58 LDM |
767 | } |
768 | ||
708624a4 | 769 | extern long init_module(const void *mem, unsigned long len, const char *args); |
8f788d58 | 770 | |
144d1826 KC |
771 | #ifndef __NR_finit_module |
772 | # define __NR_finit_module -1 | |
773 | #endif | |
774 | static inline int finit_module(int fd, const char *uargs, int flags) | |
775 | { | |
776 | return syscall(__NR_finit_module, fd, uargs, flags); | |
777 | } | |
778 | ||
7afc98a1 LDM |
779 | /** |
780 | * kmod_module_insert_module: | |
781 | * @mod: kmod module | |
142db570 LDM |
782 | * @flags: flags are not passed to Linux Kernel, but instead they dictate the |
783 | * behavior of this function. | |
7afc98a1 LDM |
784 | * @options: module's options to pass to Linux Kernel. |
785 | * | |
786 | * Insert a module in Linux kernel. It opens the file pointed by @mod, | |
787 | * mmap'ing it and passing to kernel. | |
788 | * | |
bbf59327 LDM |
789 | * Returns: 0 on success or < 0 on failure. If module is already loaded it |
790 | * returns -EEXIST. | |
7afc98a1 | 791 | */ |
8f788d58 | 792 | KMOD_EXPORT int kmod_module_insert_module(struct kmod_module *mod, |
3a721bbc GSB |
793 | unsigned int flags, |
794 | const char *options) | |
8f788d58 LDM |
795 | { |
796 | int err; | |
708624a4 | 797 | const void *mem; |
3d8226ed GSB |
798 | off_t size; |
799 | struct kmod_file *file; | |
708624a4 GSB |
800 | struct kmod_elf *elf = NULL; |
801 | const char *path; | |
3a721bbc | 802 | const char *args = options ? options : ""; |
8f788d58 LDM |
803 | |
804 | if (mod == NULL) | |
805 | return -ENOENT; | |
806 | ||
708624a4 GSB |
807 | path = kmod_module_get_path(mod); |
808 | if (path == NULL) { | |
b787b569 | 809 | ERR(mod->ctx, "could not find module by name='%s'\n", mod->name); |
8f788d58 LDM |
810 | return -ENOSYS; |
811 | } | |
812 | ||
c68e92f7 | 813 | file = kmod_file_open(mod->ctx, path); |
3d8226ed | 814 | if (file == NULL) { |
8f788d58 LDM |
815 | err = -errno; |
816 | return err; | |
817 | } | |
818 | ||
144d1826 KC |
819 | if (kmod_file_get_direct(file)) { |
820 | unsigned int kernel_flags = 0; | |
821 | ||
822 | #ifdef HAVE_LINUX_MODULE_H | |
823 | if (flags & KMOD_INSERT_FORCE_VERMAGIC) | |
824 | kernel_flags |= MODULE_INIT_IGNORE_VERMAGIC; | |
825 | if (flags & KMOD_INSERT_FORCE_MODVERSION) | |
826 | kernel_flags |= MODULE_INIT_IGNORE_MODVERSIONS; | |
827 | #endif | |
828 | ||
829 | err = finit_module(kmod_file_get_fd(file), args, kernel_flags); | |
830 | if (err == 0 || errno != ENOSYS) | |
831 | goto init_finished; | |
832 | } | |
833 | ||
3d8226ed GSB |
834 | size = kmod_file_get_size(file); |
835 | mem = kmod_file_get_contents(file); | |
8f788d58 | 836 | |
708624a4 GSB |
837 | if (flags & (KMOD_INSERT_FORCE_VERMAGIC | KMOD_INSERT_FORCE_MODVERSION)) { |
838 | elf = kmod_elf_new(mem, size); | |
839 | if (elf == NULL) { | |
840 | err = -errno; | |
841 | goto elf_failed; | |
842 | } | |
843 | ||
844 | if (flags & KMOD_INSERT_FORCE_MODVERSION) { | |
845 | err = kmod_elf_strip_section(elf, "__versions"); | |
846 | if (err < 0) | |
847 | INFO(mod->ctx, "Failed to strip modversion: %s\n", strerror(-err)); | |
848 | } | |
849 | ||
850 | if (flags & KMOD_INSERT_FORCE_VERMAGIC) { | |
851 | err = kmod_elf_strip_vermagic(elf); | |
852 | if (err < 0) | |
853 | INFO(mod->ctx, "Failed to strip vermagic: %s\n", strerror(-err)); | |
854 | } | |
855 | ||
856 | mem = kmod_elf_get_memory(elf); | |
857 | } | |
858 | ||
3d8226ed | 859 | err = init_module(mem, size, args); |
144d1826 | 860 | init_finished: |
bbf59327 LDM |
861 | if (err < 0) { |
862 | err = -errno; | |
863 | INFO(mod->ctx, "Failed to insert module '%s': %m\n", path); | |
864 | } | |
8f788d58 | 865 | |
708624a4 GSB |
866 | if (elf != NULL) |
867 | kmod_elf_unref(elf); | |
868 | elf_failed: | |
3d8226ed | 869 | kmod_file_unref(file); |
8f788d58 LDM |
870 | |
871 | return err; | |
872 | } | |
f12ae3c4 | 873 | |
ddbda022 LDM |
874 | static bool module_is_blacklisted(struct kmod_module *mod) |
875 | { | |
876 | struct kmod_ctx *ctx = mod->ctx; | |
e7fc2c86 LDM |
877 | const struct kmod_config *config = kmod_get_config(ctx); |
878 | const struct kmod_list *bl = config->blacklists; | |
ddbda022 LDM |
879 | const struct kmod_list *l; |
880 | ||
881 | kmod_list_foreach(l, bl) { | |
882 | const char *modname = kmod_blacklist_get_modname(l); | |
883 | ||
884 | if (streq(modname, mod->name)) | |
885 | return true; | |
886 | } | |
887 | ||
888 | return false; | |
889 | } | |
890 | ||
d80b103c DR |
891 | /** |
892 | * kmod_module_apply_filter | |
893 | * @ctx: kmod library context | |
894 | * @filter_type: bitmask to filter modules on | |
895 | * @input: list of kmod_module to be filtered | |
896 | * @output: where to save the new list | |
897 | * | |
898 | * Given a list @input, this function filter it out by the filter mask | |
899 | * and save it in @output. | |
900 | * | |
901 | * Returns: 0 on success or < 0 otherwise. @output is saved with the updated | |
902 | * list. | |
903 | */ | |
904 | KMOD_EXPORT int kmod_module_apply_filter(const struct kmod_ctx *ctx, | |
905 | enum kmod_filter filter_type, | |
906 | const struct kmod_list *input, | |
907 | struct kmod_list **output) | |
908 | { | |
909 | const struct kmod_list *li; | |
910 | ||
911 | if (ctx == NULL || output == NULL) | |
912 | return -ENOENT; | |
913 | ||
914 | *output = NULL; | |
915 | if (input == NULL) | |
916 | return 0; | |
917 | ||
918 | kmod_list_foreach(li, input) { | |
919 | struct kmod_module *mod = li->data; | |
920 | struct kmod_list *node; | |
921 | ||
922 | if ((filter_type & KMOD_FILTER_BLACKLIST) && | |
923 | module_is_blacklisted(mod)) | |
924 | continue; | |
925 | ||
bdda7e14 | 926 | if ((filter_type & KMOD_FILTER_BUILTIN) && mod->builtin) |
d80b103c DR |
927 | continue; |
928 | ||
929 | node = kmod_list_append(*output, mod); | |
930 | if (node == NULL) | |
931 | goto fail; | |
932 | ||
933 | *output = node; | |
934 | kmod_module_ref(mod); | |
935 | } | |
936 | ||
937 | return 0; | |
938 | ||
939 | fail: | |
940 | kmod_module_unref_list(*output); | |
941 | *output = NULL; | |
942 | return -ENOMEM; | |
943 | } | |
944 | ||
ddbda022 LDM |
945 | static int command_do(struct kmod_module *mod, const char *type, |
946 | const char *cmd) | |
947 | { | |
948 | const char *modname = kmod_module_get_name(mod); | |
949 | int err; | |
950 | ||
951 | DBG(mod->ctx, "%s %s\n", type, cmd); | |
952 | ||
953 | setenv("MODPROBE_MODULE", modname, 1); | |
954 | err = system(cmd); | |
955 | unsetenv("MODPROBE_MODULE"); | |
956 | ||
957 | if (err == -1 || WEXITSTATUS(err)) { | |
958 | ERR(mod->ctx, "Error running %s command for %s\n", | |
959 | type, modname); | |
960 | if (err != -1) | |
961 | err = -WEXITSTATUS(err); | |
962 | } | |
963 | ||
964 | return err; | |
965 | } | |
966 | ||
b1a51256 LDM |
967 | struct probe_insert_cb { |
968 | int (*run_install)(struct kmod_module *m, const char *cmd, void *data); | |
969 | void *data; | |
970 | }; | |
971 | ||
ddbda022 LDM |
972 | static int module_do_install_commands(struct kmod_module *mod, |
973 | const char *options, | |
974 | struct probe_insert_cb *cb) | |
975 | { | |
976 | const char *command = kmod_module_get_install_commands(mod); | |
977 | char *p, *cmd; | |
978 | int err; | |
979 | size_t cmdlen, options_len, varlen; | |
980 | ||
981 | assert(command); | |
982 | ||
983 | if (options == NULL) | |
984 | options = ""; | |
985 | ||
986 | options_len = strlen(options); | |
987 | cmdlen = strlen(command); | |
988 | varlen = sizeof("$CMDLINE_OPTS") - 1; | |
989 | ||
990 | cmd = memdup(command, cmdlen + 1); | |
991 | if (cmd == NULL) | |
992 | return -ENOMEM; | |
993 | ||
994 | while ((p = strstr(cmd, "$CMDLINE_OPTS")) != NULL) { | |
995 | size_t prefixlen = p - cmd; | |
996 | size_t suffixlen = cmdlen - prefixlen - varlen; | |
997 | size_t slen = cmdlen - varlen + options_len; | |
998 | char *suffix = p + varlen; | |
999 | char *s = malloc(slen + 1); | |
1000 | if (s == NULL) { | |
1001 | free(cmd); | |
1002 | return -ENOMEM; | |
1003 | } | |
1004 | memcpy(s, cmd, p - cmd); | |
1005 | memcpy(s + prefixlen, options, options_len); | |
1006 | memcpy(s + prefixlen + options_len, suffix, suffixlen); | |
1007 | s[slen] = '\0'; | |
1008 | ||
1009 | free(cmd); | |
1010 | cmd = s; | |
1011 | cmdlen = slen; | |
1012 | } | |
1013 | ||
1014 | if (cb->run_install != NULL) | |
1015 | err = cb->run_install(mod, cmd, cb->data); | |
1016 | else | |
1017 | err = command_do(mod, "install", cmd); | |
1018 | ||
1019 | free(cmd); | |
1020 | ||
1021 | return err; | |
1022 | } | |
1023 | ||
ddbda022 LDM |
1024 | static char *module_options_concat(const char *opt, const char *xopt) |
1025 | { | |
1026 | // TODO: we might need to check if xopt overrides options on opt | |
1027 | size_t optlen = opt == NULL ? 0 : strlen(opt); | |
1028 | size_t xoptlen = xopt == NULL ? 0 : strlen(xopt); | |
1029 | char *r; | |
1030 | ||
1031 | if (optlen == 0 && xoptlen == 0) | |
1032 | return NULL; | |
1033 | ||
1034 | r = malloc(optlen + xoptlen + 2); | |
1035 | ||
1036 | if (opt != NULL) { | |
1037 | memcpy(r, opt, optlen); | |
1038 | r[optlen] = ' '; | |
1039 | optlen++; | |
1040 | } | |
1041 | ||
1042 | if (xopt != NULL) | |
1043 | memcpy(r + optlen, xopt, xoptlen); | |
1044 | ||
1045 | r[optlen + xoptlen] = '\0'; | |
1046 | ||
1047 | return r; | |
1048 | } | |
1049 | ||
b1a51256 | 1050 | static int __kmod_module_get_probe_list(struct kmod_module *mod, |
89e92487 | 1051 | bool ignorecmd, |
b1a51256 LDM |
1052 | struct kmod_list **list); |
1053 | ||
1054 | /* re-entrant */ | |
1055 | static int __kmod_module_fill_softdep(struct kmod_module *mod, | |
1056 | struct kmod_list **list) | |
ddbda022 | 1057 | { |
b1a51256 | 1058 | struct kmod_list *pre = NULL, *post = NULL, *l; |
ddbda022 | 1059 | int err; |
ddbda022 | 1060 | |
b1a51256 LDM |
1061 | err = kmod_module_get_softdeps(mod, &pre, &post); |
1062 | if (err < 0) { | |
050db08c LDM |
1063 | ERR(mod->ctx, "could not get softdep: %s\n", |
1064 | strerror(-err)); | |
b1a51256 | 1065 | goto fail; |
ddbda022 LDM |
1066 | } |
1067 | ||
b1a51256 LDM |
1068 | kmod_list_foreach(l, pre) { |
1069 | struct kmod_module *m = l->data; | |
89e92487 | 1070 | err = __kmod_module_get_probe_list(m, false, list); |
b1a51256 LDM |
1071 | if (err < 0) |
1072 | goto fail; | |
1073 | } | |
ddbda022 | 1074 | |
b1a51256 LDM |
1075 | l = kmod_list_append(*list, kmod_module_ref(mod)); |
1076 | if (l == NULL) { | |
1077 | kmod_module_unref(mod); | |
1078 | err = -ENOMEM; | |
1079 | goto fail; | |
ddbda022 | 1080 | } |
b1a51256 | 1081 | *list = l; |
89e92487 | 1082 | mod->ignorecmd = (pre != NULL || post != NULL); |
ddbda022 | 1083 | |
b1a51256 LDM |
1084 | kmod_list_foreach(l, post) { |
1085 | struct kmod_module *m = l->data; | |
89e92487 | 1086 | err = __kmod_module_get_probe_list(m, false, list); |
b1a51256 LDM |
1087 | if (err < 0) |
1088 | goto fail; | |
1089 | } | |
ddbda022 | 1090 | |
b1a51256 LDM |
1091 | fail: |
1092 | kmod_module_unref_list(pre); | |
1093 | kmod_module_unref_list(post); | |
ddbda022 | 1094 | |
b1a51256 LDM |
1095 | return err; |
1096 | } | |
ddbda022 | 1097 | |
b1a51256 LDM |
1098 | /* re-entrant */ |
1099 | static int __kmod_module_get_probe_list(struct kmod_module *mod, | |
89e92487 | 1100 | bool ignorecmd, |
b1a51256 LDM |
1101 | struct kmod_list **list) |
1102 | { | |
1103 | struct kmod_list *dep, *l; | |
1104 | int err = 0; | |
ddbda022 | 1105 | |
b1a51256 LDM |
1106 | if (mod->visited) { |
1107 | DBG(mod->ctx, "Ignore module '%s': already visited\n", | |
1108 | mod->name); | |
1109 | return 0; | |
1110 | } | |
8cd0f9e4 | 1111 | mod->visited = true; |
ddbda022 | 1112 | |
b1a51256 | 1113 | dep = kmod_module_get_dependencies(mod); |
b1a51256 LDM |
1114 | kmod_list_foreach(l, dep) { |
1115 | struct kmod_module *m = l->data; | |
1116 | err = __kmod_module_fill_softdep(m, list); | |
1117 | if (err < 0) | |
89e92487 | 1118 | goto finish; |
b1a51256 | 1119 | } |
ddbda022 | 1120 | |
89e92487 LDM |
1121 | if (ignorecmd) { |
1122 | l = kmod_list_append(*list, kmod_module_ref(mod)); | |
1123 | if (l == NULL) { | |
1124 | kmod_module_unref(mod); | |
1125 | err = -ENOMEM; | |
1126 | goto finish; | |
1127 | } | |
1128 | *list = l; | |
1129 | mod->ignorecmd = true; | |
1130 | } else | |
1131 | err = __kmod_module_fill_softdep(mod, list); | |
1132 | ||
ddbda022 | 1133 | finish: |
b1a51256 LDM |
1134 | kmod_module_unref_list(dep); |
1135 | return err; | |
1136 | } | |
1137 | ||
1138 | static int kmod_module_get_probe_list(struct kmod_module *mod, | |
89e92487 | 1139 | bool ignorecmd, |
b1a51256 LDM |
1140 | struct kmod_list **list) |
1141 | { | |
1142 | int err; | |
1143 | ||
1144 | assert(mod != NULL); | |
1145 | assert(list != NULL && *list == NULL); | |
1146 | ||
1147 | /* | |
1148 | * Make sure we don't get screwed by previous calls to this function | |
1149 | */ | |
1150 | kmod_set_modules_visited(mod->ctx, false); | |
1151 | ||
89e92487 | 1152 | err = __kmod_module_get_probe_list(mod, ignorecmd, list); |
b1a51256 LDM |
1153 | if (err < 0) { |
1154 | kmod_module_unref_list(*list); | |
1155 | *list = NULL; | |
1156 | } | |
ddbda022 LDM |
1157 | |
1158 | return err; | |
1159 | } | |
1160 | ||
1161 | /** | |
1162 | * kmod_module_probe_insert_module: | |
1163 | * @mod: kmod module | |
1164 | * @flags: flags are not passed to Linux Kernel, but instead they dictate the | |
1165 | * behavior of this function. | |
b1a51256 LDM |
1166 | * @extra_options: module's options to pass to Linux Kernel. It applies only |
1167 | * to @mod, not to its dependencies. | |
1168 | * @run_install: function to run when @mod is backed by an install command. | |
ddbda022 | 1169 | * @data: data to give back to @run_install callback |
6bd0713d LDM |
1170 | * @print_action: function to call with the action being taken (install or |
1171 | * insmod). It's useful for tools like modprobe when running with verbose | |
1172 | * output or in dry-run mode. | |
ddbda022 | 1173 | * |
b1a51256 | 1174 | * Insert a module in Linux kernel resolving dependencies, soft dependencies, |
ddbda022 LDM |
1175 | * install commands and applying blacklist. |
1176 | * | |
7aed4608 LDM |
1177 | * If @run_install is NULL, this function will fork and exec by calling |
1178 | * system(3). Don't pass a NULL argument in @run_install if your binary is | |
1179 | * setuid/setgid (see warning in system(3)). If you need control over the | |
1180 | * execution of an install command, give a callback function instead. | |
ddbda022 | 1181 | * |
b1a51256 LDM |
1182 | * Returns: 0 on success, > 0 if stopped by a reason given in @flags or < 0 on |
1183 | * failure. | |
ddbda022 LDM |
1184 | */ |
1185 | KMOD_EXPORT int kmod_module_probe_insert_module(struct kmod_module *mod, | |
1186 | unsigned int flags, const char *extra_options, | |
1187 | int (*run_install)(struct kmod_module *m, | |
1188 | const char *cmd, void *data), | |
6bd0713d LDM |
1189 | const void *data, |
1190 | void (*print_action)(struct kmod_module *m, | |
1191 | bool install, | |
1192 | const char *options)) | |
ddbda022 | 1193 | { |
b1a51256 | 1194 | struct kmod_list *list = NULL, *l; |
ddbda022 | 1195 | struct probe_insert_cb cb; |
b1a51256 LDM |
1196 | int err; |
1197 | ||
1198 | if (mod == NULL) | |
1199 | return -ENOENT; | |
1200 | ||
269de2e0 LDM |
1201 | if (!(flags & KMOD_PROBE_IGNORE_LOADED) |
1202 | && module_is_inkernel(mod)) { | |
814a57ba | 1203 | if (flags & KMOD_PROBE_FAIL_ON_LOADED) |
af9572c6 DR |
1204 | return -EEXIST; |
1205 | else | |
1206 | return 0; | |
1207 | } | |
1208 | ||
6882017f LDM |
1209 | /* |
1210 | * Ugly assignement + check. We need to check if we were told to check | |
1211 | * blacklist and also return the reason why we failed. | |
1212 | * KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY will take effect only if the | |
1213 | * module is an alias, so we also need to check it | |
1214 | */ | |
1215 | if ((mod->alias != NULL && ((err = flags & KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY))) | |
1216 | || (err = flags & KMOD_PROBE_APPLY_BLACKLIST_ALL) | |
1217 | || (err = flags & KMOD_PROBE_APPLY_BLACKLIST)) { | |
b1a51256 LDM |
1218 | if (module_is_blacklisted(mod)) |
1219 | return err; | |
1220 | } | |
1221 | ||
89e92487 LDM |
1222 | err = kmod_module_get_probe_list(mod, |
1223 | !!(flags & KMOD_PROBE_IGNORE_COMMAND), &list); | |
b1a51256 LDM |
1224 | if (err < 0) |
1225 | return err; | |
1226 | ||
1227 | if (flags & KMOD_PROBE_APPLY_BLACKLIST_ALL) { | |
1228 | struct kmod_list *filtered = NULL; | |
1229 | ||
d80b103c DR |
1230 | err = kmod_module_apply_filter(mod->ctx, |
1231 | KMOD_FILTER_BLACKLIST, list, &filtered); | |
b1a51256 LDM |
1232 | if (err < 0) |
1233 | return err; | |
1234 | ||
1235 | kmod_module_unref_list(list); | |
1236 | if (filtered == NULL) | |
1237 | return KMOD_PROBE_APPLY_BLACKLIST_ALL; | |
1238 | ||
1239 | list = filtered; | |
1240 | } | |
ddbda022 LDM |
1241 | |
1242 | cb.run_install = run_install; | |
1243 | cb.data = (void *) data; | |
1244 | ||
b1a51256 LDM |
1245 | kmod_list_foreach(l, list) { |
1246 | struct kmod_module *m = l->data; | |
1247 | const char *moptions = kmod_module_get_options(m); | |
1248 | const char *cmd = kmod_module_get_install_commands(m); | |
abd5557b LDM |
1249 | char *options; |
1250 | ||
1251 | if (!(flags & KMOD_PROBE_IGNORE_LOADED) | |
1252 | && module_is_inkernel(m)) { | |
1253 | DBG(mod->ctx, "Ignoring module '%s': already loaded\n", | |
1254 | m->name); | |
1255 | err = -EEXIST; | |
1256 | goto finish_module; | |
1257 | } | |
1258 | ||
1259 | options = module_options_concat(moptions, | |
b1a51256 LDM |
1260 | m == mod ? extra_options : NULL); |
1261 | ||
89e92487 | 1262 | if (cmd != NULL && !m->ignorecmd) { |
6bd0713d LDM |
1263 | if (print_action != NULL) |
1264 | print_action(m, true, options ?: ""); | |
1265 | ||
4c1ffb75 LDM |
1266 | if (!(flags & KMOD_PROBE_DRY_RUN)) |
1267 | err = module_do_install_commands(m, options, | |
1268 | &cb); | |
b1a51256 | 1269 | } else { |
6bd0713d LDM |
1270 | if (print_action != NULL) |
1271 | print_action(m, false, options ?: ""); | |
1272 | ||
4c1ffb75 LDM |
1273 | if (!(flags & KMOD_PROBE_DRY_RUN)) |
1274 | err = kmod_module_insert_module(m, flags, | |
1275 | options); | |
b1a51256 LDM |
1276 | } |
1277 | ||
1278 | free(options); | |
1279 | ||
abd5557b | 1280 | finish_module: |
b1a51256 | 1281 | /* |
5f351473 | 1282 | * Treat "already loaded" error. If we were told to stop on |
3bc92e89 LDM |
1283 | * already loaded and the module being loaded is not a softdep |
1284 | * or dep, bail out. Otherwise, just ignore and continue. | |
5f351473 LDM |
1285 | * |
1286 | * We need to check here because of race conditions. We | |
1287 | * checked first if module was already loaded but it may have | |
1288 | * been loaded between the check and the moment we try to | |
1289 | * insert it. | |
b1a51256 | 1290 | */ |
5f351473 | 1291 | if (err == -EEXIST && m == mod && |
814a57ba | 1292 | (flags & KMOD_PROBE_FAIL_ON_LOADED)) |
5f351473 | 1293 | break; |
5f351473 | 1294 | |
3bc92e89 LDM |
1295 | if (err == -EEXIST) |
1296 | err = 0; | |
1297 | else if (err < 0) | |
b1a51256 LDM |
1298 | break; |
1299 | } | |
ddbda022 | 1300 | |
b1a51256 LDM |
1301 | kmod_module_unref_list(list); |
1302 | return err; | |
1303 | } | |
ddbda022 | 1304 | |
7afc98a1 LDM |
1305 | /** |
1306 | * kmod_module_get_options: | |
1307 | * @mod: kmod module | |
1308 | * | |
1309 | * Get options of this kmod module. Options come from the configuration file | |
1310 | * and are cached in @mod. The first call to this function will search for | |
1311 | * this module in configuration and subsequent calls return the cached string. | |
1312 | * | |
1313 | * Returns: a string with all the options separated by spaces. This string is | |
1314 | * owned by @mod, do not free it. | |
1315 | */ | |
49ce6d07 LDM |
1316 | KMOD_EXPORT const char *kmod_module_get_options(const struct kmod_module *mod) |
1317 | { | |
1318 | if (mod == NULL) | |
1319 | return NULL; | |
1320 | ||
1321 | if (!mod->init.options) { | |
1322 | /* lazy init */ | |
1323 | struct kmod_module *m = (struct kmod_module *)mod; | |
e7fc2c86 LDM |
1324 | const struct kmod_list *l; |
1325 | const struct kmod_config *config; | |
49ce6d07 LDM |
1326 | char *opts = NULL; |
1327 | size_t optslen = 0; | |
1328 | ||
e7fc2c86 | 1329 | config = kmod_get_config(mod->ctx); |
49ce6d07 | 1330 | |
e7fc2c86 | 1331 | kmod_list_foreach(l, config->options) { |
49ce6d07 LDM |
1332 | const char *modname = kmod_option_get_modname(l); |
1333 | const char *str; | |
1334 | size_t len; | |
1335 | void *tmp; | |
1336 | ||
07b8c823 LDM |
1337 | DBG(mod->ctx, "modname=%s mod->name=%s mod->alias=%s\n", modname, mod->name, mod->alias); |
1338 | if (!(streq(modname, mod->name) || (mod->alias != NULL && | |
1339 | streq(modname, mod->alias)))) | |
49ce6d07 LDM |
1340 | continue; |
1341 | ||
07b8c823 | 1342 | DBG(mod->ctx, "passed = modname=%s mod->name=%s mod->alias=%s\n", modname, mod->name, mod->alias); |
49ce6d07 LDM |
1343 | str = kmod_option_get_options(l); |
1344 | len = strlen(str); | |
1345 | if (len < 1) | |
1346 | continue; | |
1347 | ||
1348 | tmp = realloc(opts, optslen + len + 2); | |
1349 | if (tmp == NULL) { | |
1350 | free(opts); | |
1351 | goto failed; | |
1352 | } | |
1353 | ||
1354 | opts = tmp; | |
1355 | ||
1356 | if (optslen > 0) { | |
1357 | opts[optslen] = ' '; | |
1358 | optslen++; | |
1359 | } | |
1360 | ||
1361 | memcpy(opts + optslen, str, len); | |
1362 | optslen += len; | |
1363 | opts[optslen] = '\0'; | |
1364 | } | |
1365 | ||
1366 | m->init.options = true; | |
1367 | m->options = opts; | |
1368 | } | |
1369 | ||
1370 | return mod->options; | |
1371 | ||
1372 | failed: | |
1373 | ERR(mod->ctx, "out of memory\n"); | |
1374 | return NULL; | |
1375 | } | |
1376 | ||
7afc98a1 LDM |
1377 | /** |
1378 | * kmod_module_get_install_commands: | |
1379 | * @mod: kmod module | |
1380 | * | |
1381 | * Get install commands for this kmod module. Install commands come from the | |
1382 | * configuration file and are cached in @mod. The first call to this function | |
1383 | * will search for this module in configuration and subsequent calls return | |
1384 | * the cached string. The install commands are returned as they were in the | |
1385 | * configuration, concatenated by ';'. No other processing is made in this | |
1386 | * string. | |
1387 | * | |
1388 | * Returns: a string with all install commands separated by semicolons. This | |
1389 | * string is owned by @mod, do not free it. | |
1390 | */ | |
49ce6d07 LDM |
1391 | KMOD_EXPORT const char *kmod_module_get_install_commands(const struct kmod_module *mod) |
1392 | { | |
1393 | if (mod == NULL) | |
1394 | return NULL; | |
1395 | ||
1396 | if (!mod->init.install_commands) { | |
1397 | /* lazy init */ | |
1398 | struct kmod_module *m = (struct kmod_module *)mod; | |
e7fc2c86 LDM |
1399 | const struct kmod_list *l; |
1400 | const struct kmod_config *config; | |
49ce6d07 | 1401 | |
e7fc2c86 | 1402 | config = kmod_get_config(mod->ctx); |
49ce6d07 | 1403 | |
e7fc2c86 | 1404 | kmod_list_foreach(l, config->install_commands) { |
49ce6d07 | 1405 | const char *modname = kmod_command_get_modname(l); |
49ce6d07 | 1406 | |
a6bf2495 | 1407 | if (fnmatch(modname, mod->name, 0) != 0) |
49ce6d07 LDM |
1408 | continue; |
1409 | ||
60f6760e | 1410 | m->install_commands = kmod_command_get_command(l); |
49ce6d07 | 1411 | |
60f6760e LDM |
1412 | /* |
1413 | * find only the first command, as modprobe from | |
1414 | * module-init-tools does | |
1415 | */ | |
1416 | break; | |
49ce6d07 LDM |
1417 | } |
1418 | ||
1419 | m->init.install_commands = true; | |
49ce6d07 LDM |
1420 | } |
1421 | ||
1422 | return mod->install_commands; | |
49ce6d07 LDM |
1423 | } |
1424 | ||
f4fc5523 LDM |
1425 | void kmod_module_set_install_commands(struct kmod_module *mod, const char *cmd) |
1426 | { | |
1427 | mod->init.install_commands = true; | |
1428 | mod->install_commands = cmd; | |
1429 | } | |
1430 | ||
1c522600 GSB |
1431 | static struct kmod_list *lookup_softdep(struct kmod_ctx *ctx, const char * const * array, unsigned int count) |
1432 | { | |
1433 | struct kmod_list *ret = NULL; | |
1434 | unsigned i; | |
1435 | ||
1436 | for (i = 0; i < count; i++) { | |
1437 | const char *depname = array[i]; | |
1438 | struct kmod_list *lst = NULL; | |
1439 | int err; | |
1440 | ||
1441 | err = kmod_module_new_from_lookup(ctx, depname, &lst); | |
1442 | if (err < 0) { | |
1443 | ERR(ctx, "failed to lookup soft dependency '%s', continuing anyway.\n", depname); | |
1444 | continue; | |
1445 | } else if (lst != NULL) | |
1446 | ret = kmod_list_append_list(ret, lst); | |
1447 | } | |
1448 | return ret; | |
1449 | } | |
1450 | ||
1451 | /** | |
1452 | * kmod_module_get_softdeps: | |
1453 | * @mod: kmod module | |
1454 | * @pre: where to save the list of preceding soft dependencies. | |
1455 | * @post: where to save the list of post soft dependencies. | |
1456 | * | |
1457 | * Get soft dependencies for this kmod module. Soft dependencies come | |
2bd7cbf6 LDM |
1458 | * from configuration file and are not cached in @mod because it may include |
1459 | * dependency cycles that would make we leak kmod_module. Any call | |
1460 | * to this function will search for this module in configuration, allocate a | |
1461 | * list and return the result. | |
1c522600 GSB |
1462 | * |
1463 | * Both @pre and @post are newly created list of kmod_module and | |
1464 | * should be unreferenced with kmod_module_unref_list(). | |
1465 | * | |
1466 | * Returns: 0 on success or < 0 otherwise. | |
1467 | */ | |
2bd7cbf6 LDM |
1468 | KMOD_EXPORT int kmod_module_get_softdeps(const struct kmod_module *mod, |
1469 | struct kmod_list **pre, | |
1470 | struct kmod_list **post) | |
1c522600 | 1471 | { |
e7fc2c86 LDM |
1472 | const struct kmod_list *l; |
1473 | const struct kmod_config *config; | |
1c522600 GSB |
1474 | |
1475 | if (mod == NULL || pre == NULL || post == NULL) | |
1476 | return -ENOENT; | |
1477 | ||
1478 | assert(*pre == NULL); | |
1479 | assert(*post == NULL); | |
1480 | ||
e7fc2c86 | 1481 | config = kmod_get_config(mod->ctx); |
1c522600 | 1482 | |
e7fc2c86 | 1483 | kmod_list_foreach(l, config->softdeps) { |
2bd7cbf6 LDM |
1484 | const char *modname = kmod_softdep_get_name(l); |
1485 | const char * const *array; | |
1486 | unsigned count; | |
1c522600 | 1487 | |
2bd7cbf6 LDM |
1488 | if (fnmatch(modname, mod->name, 0) != 0) |
1489 | continue; | |
1c522600 | 1490 | |
2bd7cbf6 LDM |
1491 | array = kmod_softdep_get_pre(l, &count); |
1492 | *pre = lookup_softdep(mod->ctx, array, count); | |
1493 | array = kmod_softdep_get_post(l, &count); | |
1494 | *post = lookup_softdep(mod->ctx, array, count); | |
1c522600 | 1495 | |
2bd7cbf6 LDM |
1496 | /* |
1497 | * find only the first command, as modprobe from | |
1498 | * module-init-tools does | |
1499 | */ | |
1500 | break; | |
1c522600 GSB |
1501 | } |
1502 | ||
1503 | return 0; | |
1c522600 GSB |
1504 | } |
1505 | ||
7afc98a1 LDM |
1506 | /** |
1507 | * kmod_module_get_remove_commands: | |
1508 | * @mod: kmod module | |
1509 | * | |
1510 | * Get remove commands for this kmod module. Remove commands come from the | |
1511 | * configuration file and are cached in @mod. The first call to this function | |
1512 | * will search for this module in configuration and subsequent calls return | |
1513 | * the cached string. The remove commands are returned as they were in the | |
1514 | * configuration, concatenated by ';'. No other processing is made in this | |
1515 | * string. | |
1516 | * | |
1517 | * Returns: a string with all remove commands separated by semicolons. This | |
1518 | * string is owned by @mod, do not free it. | |
1519 | */ | |
49ce6d07 LDM |
1520 | KMOD_EXPORT const char *kmod_module_get_remove_commands(const struct kmod_module *mod) |
1521 | { | |
1522 | if (mod == NULL) | |
1523 | return NULL; | |
1524 | ||
1525 | if (!mod->init.remove_commands) { | |
1526 | /* lazy init */ | |
1527 | struct kmod_module *m = (struct kmod_module *)mod; | |
e7fc2c86 LDM |
1528 | const struct kmod_list *l; |
1529 | const struct kmod_config *config; | |
49ce6d07 | 1530 | |
e7fc2c86 | 1531 | config = kmod_get_config(mod->ctx); |
49ce6d07 | 1532 | |
e7fc2c86 | 1533 | kmod_list_foreach(l, config->remove_commands) { |
49ce6d07 | 1534 | const char *modname = kmod_command_get_modname(l); |
49ce6d07 | 1535 | |
a6bf2495 | 1536 | if (fnmatch(modname, mod->name, 0) != 0) |
49ce6d07 LDM |
1537 | continue; |
1538 | ||
60f6760e | 1539 | m->remove_commands = kmod_command_get_command(l); |
49ce6d07 | 1540 | |
60f6760e LDM |
1541 | /* |
1542 | * find only the first command, as modprobe from | |
1543 | * module-init-tools does | |
1544 | */ | |
1545 | break; | |
49ce6d07 LDM |
1546 | } |
1547 | ||
1548 | m->init.remove_commands = true; | |
49ce6d07 LDM |
1549 | } |
1550 | ||
1551 | return mod->remove_commands; | |
49ce6d07 LDM |
1552 | } |
1553 | ||
f4fc5523 LDM |
1554 | void kmod_module_set_remove_commands(struct kmod_module *mod, const char *cmd) |
1555 | { | |
1556 | mod->init.remove_commands = true; | |
1557 | mod->remove_commands = cmd; | |
1558 | } | |
1559 | ||
49ce6d07 LDM |
1560 | /** |
1561 | * SECTION:libkmod-loaded | |
1562 | * @short_description: currently loaded modules | |
1563 | * | |
1564 | * Information about currently loaded modules, as reported by Linux kernel. | |
1565 | * These information are not cached by libkmod and are always read from /sys | |
1566 | * and /proc/modules. | |
1567 | */ | |
1568 | ||
1569 | /** | |
1570 | * kmod_module_new_from_loaded: | |
1571 | * @ctx: kmod library context | |
1572 | * @list: where to save the list of loaded modules | |
1573 | * | |
7afc98a1 LDM |
1574 | * Create a new list of kmod modules with all modules currently loaded in |
1575 | * kernel. It uses /proc/modules to get the names of loaded modules and to | |
1576 | * create kmod modules by calling kmod_module_new_from_name() in each of them. | |
1577 | * They are put are put in @list in no particular order. | |
49ce6d07 | 1578 | * |
7afc98a1 LDM |
1579 | * The initial refcount is 1, and needs to be decremented to release the |
1580 | * resources of the kmod_module. The returned @list must be released by | |
1581 | * calling kmod_module_unref_list(). Since libkmod keeps track of all | |
1582 | * kmod_modules created, they are all released upon @ctx destruction too. Do | |
1583 | * not unref @ctx before all the desired operations with the returned list are | |
1584 | * completed. | |
49ce6d07 LDM |
1585 | * |
1586 | * Returns: 0 on success or < 0 on error. | |
1587 | */ | |
1588 | KMOD_EXPORT int kmod_module_new_from_loaded(struct kmod_ctx *ctx, | |
1589 | struct kmod_list **list) | |
1590 | { | |
1591 | struct kmod_list *l = NULL; | |
1592 | FILE *fp; | |
1593 | char line[4096]; | |
1594 | ||
1595 | if (ctx == NULL || list == NULL) | |
1596 | return -ENOENT; | |
1597 | ||
79e5ea91 | 1598 | fp = fopen("/proc/modules", "re"); |
49ce6d07 LDM |
1599 | if (fp == NULL) { |
1600 | int err = -errno; | |
1601 | ERR(ctx, "could not open /proc/modules: %s\n", strerror(errno)); | |
1602 | return err; | |
1603 | } | |
1604 | ||
1605 | while (fgets(line, sizeof(line), fp)) { | |
1606 | struct kmod_module *m; | |
1607 | struct kmod_list *node; | |
1608 | int err; | |
1609 | char *saveptr, *name = strtok_r(line, " \t", &saveptr); | |
1610 | ||
1611 | err = kmod_module_new_from_name(ctx, name, &m); | |
1612 | if (err < 0) { | |
1613 | ERR(ctx, "could not get module from name '%s': %s\n", | |
1614 | name, strerror(-err)); | |
1615 | continue; | |
1616 | } | |
1617 | ||
1618 | node = kmod_list_append(l, m); | |
1619 | if (node) | |
1620 | l = node; | |
1621 | else { | |
1622 | ERR(ctx, "out of memory\n"); | |
1623 | kmod_module_unref(m); | |
1624 | } | |
1625 | } | |
1626 | ||
1627 | fclose(fp); | |
1628 | *list = l; | |
1629 | ||
1630 | return 0; | |
1631 | } | |
1632 | ||
7afc98a1 LDM |
1633 | /** |
1634 | * kmod_module_initstate_str: | |
1635 | * @state: the state as returned by kmod_module_get_initstate() | |
1636 | * | |
1637 | * Translate a initstate to a string. | |
1638 | * | |
1639 | * Returns: the string associated to the @state. This string is statically | |
1640 | * allocated, do not free it. | |
1641 | */ | |
f12ae3c4 GSB |
1642 | KMOD_EXPORT const char *kmod_module_initstate_str(enum kmod_module_initstate state) |
1643 | { | |
7bede7b6 DR |
1644 | switch (state) { |
1645 | case KMOD_MODULE_BUILTIN: | |
1646 | return "builtin"; | |
1647 | case KMOD_MODULE_LIVE: | |
1648 | return "live"; | |
1649 | case KMOD_MODULE_COMING: | |
1650 | return "coming"; | |
1651 | case KMOD_MODULE_GOING: | |
1652 | return "going"; | |
1653 | default: | |
1654 | return NULL; | |
1655 | } | |
f12ae3c4 GSB |
1656 | } |
1657 | ||
7afc98a1 LDM |
1658 | /** |
1659 | * kmod_module_get_initstate: | |
1660 | * @mod: kmod module | |
1661 | * | |
1662 | * Get the initstate of this @mod, as returned by Linux Kernel, by reading | |
1663 | * /sys filesystem. | |
1664 | * | |
1665 | * Returns: < 0 on error or enum kmod_initstate if module is found in kernel. | |
1666 | */ | |
f12ae3c4 GSB |
1667 | KMOD_EXPORT int kmod_module_get_initstate(const struct kmod_module *mod) |
1668 | { | |
1669 | char path[PATH_MAX], buf[32]; | |
1670 | int fd, err, pathlen; | |
1671 | ||
818f8e8a LDM |
1672 | if (mod == NULL) |
1673 | return -ENOENT; | |
1674 | ||
3805274b LDM |
1675 | if (mod->builtin) |
1676 | return KMOD_MODULE_BUILTIN; | |
1677 | ||
f12ae3c4 GSB |
1678 | pathlen = snprintf(path, sizeof(path), |
1679 | "/sys/module/%s/initstate", mod->name); | |
79e5ea91 | 1680 | fd = open(path, O_RDONLY|O_CLOEXEC); |
f12ae3c4 GSB |
1681 | if (fd < 0) { |
1682 | err = -errno; | |
1683 | ||
ddbda022 LDM |
1684 | DBG(mod->ctx, "could not open '%s': %s\n", |
1685 | path, strerror(-err)); | |
1686 | ||
f12ae3c4 GSB |
1687 | if (pathlen > (int)sizeof("/initstate") - 1) { |
1688 | struct stat st; | |
1689 | path[pathlen - (sizeof("/initstate") - 1)] = '\0'; | |
1690 | if (stat(path, &st) == 0 && S_ISDIR(st.st_mode)) | |
1691 | return KMOD_MODULE_BUILTIN; | |
1692 | } | |
1693 | ||
926f67a6 | 1694 | DBG(mod->ctx, "could not open '%s': %s\n", |
f12ae3c4 GSB |
1695 | path, strerror(-err)); |
1696 | return err; | |
1697 | } | |
1698 | ||
1699 | err = read_str_safe(fd, buf, sizeof(buf)); | |
1700 | close(fd); | |
1701 | if (err < 0) { | |
1702 | ERR(mod->ctx, "could not read from '%s': %s\n", | |
1703 | path, strerror(-err)); | |
1704 | return err; | |
1705 | } | |
1706 | ||
877e80cd | 1707 | if (streq(buf, "live\n")) |
f12ae3c4 | 1708 | return KMOD_MODULE_LIVE; |
877e80cd | 1709 | else if (streq(buf, "coming\n")) |
f12ae3c4 | 1710 | return KMOD_MODULE_COMING; |
877e80cd | 1711 | else if (streq(buf, "going\n")) |
f12ae3c4 GSB |
1712 | return KMOD_MODULE_GOING; |
1713 | ||
1714 | ERR(mod->ctx, "unknown %s: '%s'\n", path, buf); | |
1715 | return -EINVAL; | |
1716 | } | |
1717 | ||
2d7bab5c LDM |
1718 | /** |
1719 | * kmod_module_get_size: | |
1720 | * @mod: kmod module | |
1721 | * | |
486f9013 DR |
1722 | * Get the size of this kmod module as returned by Linux kernel. If supported, |
1723 | * the size is read from the coresize attribute in /sys/module. For older | |
1724 | * kernels, this falls back on /proc/modules and searches for the specified | |
1725 | * module to get its size. | |
2d7bab5c LDM |
1726 | * |
1727 | * Returns: the size of this kmod module. | |
1728 | */ | |
1729 | KMOD_EXPORT long kmod_module_get_size(const struct kmod_module *mod) | |
1730 | { | |
2d7bab5c LDM |
1731 | FILE *fp; |
1732 | char line[4096]; | |
1733 | int lineno = 0; | |
1734 | long size = -ENOENT; | |
486f9013 | 1735 | int dfd, cfd; |
2d7bab5c LDM |
1736 | |
1737 | if (mod == NULL) | |
1738 | return -ENOENT; | |
1739 | ||
486f9013 DR |
1740 | /* try to open the module dir in /sys. If this fails, don't |
1741 | * bother trying to find the size as we know the module isn't | |
1742 | * loaded. | |
1743 | */ | |
1744 | snprintf(line, sizeof(line), "/sys/module/%s", mod->name); | |
1745 | dfd = open(line, O_RDONLY); | |
1746 | if (dfd < 0) | |
1747 | return -errno; | |
1748 | ||
1749 | /* available as of linux 3.3.x */ | |
1750 | cfd = openat(dfd, "coresize", O_RDONLY|O_CLOEXEC); | |
1751 | if (cfd >= 0) { | |
1752 | if (read_str_long(cfd, &size, 10) < 0) | |
1753 | ERR(mod->ctx, "failed to read coresize from %s\n", line); | |
1754 | close(cfd); | |
1755 | goto done; | |
1756 | } | |
1757 | ||
1758 | /* fall back on parsing /proc/modules */ | |
79e5ea91 | 1759 | fp = fopen("/proc/modules", "re"); |
2d7bab5c LDM |
1760 | if (fp == NULL) { |
1761 | int err = -errno; | |
1762 | ERR(mod->ctx, | |
1763 | "could not open /proc/modules: %s\n", strerror(errno)); | |
1764 | return err; | |
1765 | } | |
1766 | ||
1767 | while (fgets(line, sizeof(line), fp)) { | |
1768 | char *saveptr, *endptr, *tok = strtok_r(line, " \t", &saveptr); | |
1769 | long value; | |
1770 | ||
1771 | lineno++; | |
1772 | if (tok == NULL || !streq(tok, mod->name)) | |
1773 | continue; | |
1774 | ||
1775 | tok = strtok_r(NULL, " \t", &saveptr); | |
1776 | if (tok == NULL) { | |
1777 | ERR(mod->ctx, | |
1778 | "invalid line format at /proc/modules:%d\n", lineno); | |
1779 | break; | |
1780 | } | |
1781 | ||
1782 | value = strtol(tok, &endptr, 10); | |
1783 | if (endptr == tok || *endptr != '\0') { | |
1784 | ERR(mod->ctx, | |
1785 | "invalid line format at /proc/modules:%d\n", lineno); | |
1786 | break; | |
1787 | } | |
1788 | ||
1789 | size = value; | |
1790 | break; | |
1791 | } | |
1792 | fclose(fp); | |
486f9013 DR |
1793 | |
1794 | done: | |
1795 | close(dfd); | |
2d7bab5c LDM |
1796 | return size; |
1797 | } | |
1798 | ||
7afc98a1 LDM |
1799 | /** |
1800 | * kmod_module_get_refcnt: | |
1801 | * @mod: kmod module | |
1802 | * | |
1803 | * Get the ref count of this @mod, as returned by Linux Kernel, by reading | |
1804 | * /sys filesystem. | |
1805 | * | |
1806 | * Returns: 0 on success or < 0 on failure. | |
1807 | */ | |
f12ae3c4 GSB |
1808 | KMOD_EXPORT int kmod_module_get_refcnt(const struct kmod_module *mod) |
1809 | { | |
1810 | char path[PATH_MAX]; | |
1811 | long refcnt; | |
1812 | int fd, err; | |
1813 | ||
818f8e8a LDM |
1814 | if (mod == NULL) |
1815 | return -ENOENT; | |
1816 | ||
f12ae3c4 | 1817 | snprintf(path, sizeof(path), "/sys/module/%s/refcnt", mod->name); |
79e5ea91 | 1818 | fd = open(path, O_RDONLY|O_CLOEXEC); |
f12ae3c4 GSB |
1819 | if (fd < 0) { |
1820 | err = -errno; | |
9c5f057c | 1821 | DBG(mod->ctx, "could not open '%s': %s\n", |
f12ae3c4 GSB |
1822 | path, strerror(errno)); |
1823 | return err; | |
1824 | } | |
1825 | ||
1826 | err = read_str_long(fd, &refcnt, 10); | |
1827 | close(fd); | |
1828 | if (err < 0) { | |
1829 | ERR(mod->ctx, "could not read integer from '%s': '%s'\n", | |
1830 | path, strerror(-err)); | |
1831 | return err; | |
1832 | } | |
1833 | ||
1834 | return (int)refcnt; | |
1835 | } | |
1836 | ||
7afc98a1 LDM |
1837 | /** |
1838 | * kmod_module_get_holders: | |
1839 | * @mod: kmod module | |
1840 | * | |
1841 | * Get a list of kmod modules that are holding this @mod, as returned by Linux | |
1842 | * Kernel. After use, free the @list by calling kmod_module_unref_list(). | |
1843 | * | |
1844 | * Returns: a new list of kmod modules on success or NULL on failure. | |
1845 | */ | |
f12ae3c4 GSB |
1846 | KMOD_EXPORT struct kmod_list *kmod_module_get_holders(const struct kmod_module *mod) |
1847 | { | |
1848 | char dname[PATH_MAX]; | |
1849 | struct kmod_list *list = NULL; | |
1850 | DIR *d; | |
f12ae3c4 GSB |
1851 | |
1852 | if (mod == NULL) | |
1853 | return NULL; | |
818f8e8a | 1854 | |
f12ae3c4 GSB |
1855 | snprintf(dname, sizeof(dname), "/sys/module/%s/holders", mod->name); |
1856 | ||
1857 | d = opendir(dname); | |
1858 | if (d == NULL) { | |
1859 | ERR(mod->ctx, "could not open '%s': %s\n", | |
53886ddd | 1860 | dname, strerror(errno)); |
f12ae3c4 GSB |
1861 | return NULL; |
1862 | } | |
1863 | ||
53886ddd LDM |
1864 | for (;;) { |
1865 | struct dirent de, *entp; | |
f12ae3c4 | 1866 | struct kmod_module *holder; |
53886ddd | 1867 | struct kmod_list *l; |
f12ae3c4 GSB |
1868 | int err; |
1869 | ||
53886ddd LDM |
1870 | err = readdir_r(d, &de, &entp); |
1871 | if (err != 0) { | |
1872 | ERR(mod->ctx, "could not iterate for module '%s': %s\n", | |
1873 | mod->name, strerror(-err)); | |
1874 | goto fail; | |
1875 | } | |
1876 | ||
1877 | if (entp == NULL) | |
1878 | break; | |
1879 | ||
1880 | if (de.d_name[0] == '.') { | |
1881 | if (de.d_name[1] == '\0' || | |
1882 | (de.d_name[1] == '.' && de.d_name[2] == '\0')) | |
f12ae3c4 GSB |
1883 | continue; |
1884 | } | |
1885 | ||
53886ddd | 1886 | err = kmod_module_new_from_name(mod->ctx, de.d_name, &holder); |
f12ae3c4 GSB |
1887 | if (err < 0) { |
1888 | ERR(mod->ctx, "could not create module for '%s': %s\n", | |
53886ddd LDM |
1889 | de.d_name, strerror(-err)); |
1890 | goto fail; | |
f12ae3c4 GSB |
1891 | } |
1892 | ||
53886ddd LDM |
1893 | l = kmod_list_append(list, holder); |
1894 | if (l != NULL) { | |
1895 | list = l; | |
1896 | } else { | |
f12ae3c4 GSB |
1897 | ERR(mod->ctx, "out of memory\n"); |
1898 | kmod_module_unref(holder); | |
53886ddd | 1899 | goto fail; |
f12ae3c4 GSB |
1900 | } |
1901 | } | |
1902 | ||
1903 | closedir(d); | |
1904 | return list; | |
53886ddd LDM |
1905 | |
1906 | fail: | |
1907 | closedir(d); | |
1908 | kmod_module_unref_list(list); | |
1909 | return NULL; | |
f12ae3c4 GSB |
1910 | } |
1911 | ||
1912 | struct kmod_module_section { | |
1913 | unsigned long address; | |
1914 | char name[]; | |
1915 | }; | |
1916 | ||
1917 | static void kmod_module_section_free(struct kmod_module_section *section) | |
1918 | { | |
1919 | free(section); | |
1920 | } | |
1921 | ||
7afc98a1 LDM |
1922 | /** |
1923 | * kmod_module_get_sections: | |
1924 | * @mod: kmod module | |
1925 | * | |
1926 | * Get a list of kmod sections of this @mod, as returned by Linux Kernel. The | |
1927 | * structure contained in this list is internal to libkmod and their fields | |
1928 | * can be obtained by calling kmod_module_section_get_name() and | |
1929 | * kmod_module_section_get_address(). | |
1930 | * | |
1931 | * After use, free the @list by calling kmod_module_section_free_list(). | |
1932 | * | |
1933 | * Returns: a new list of kmod module sections on success or NULL on failure. | |
1934 | */ | |
f12ae3c4 GSB |
1935 | KMOD_EXPORT struct kmod_list *kmod_module_get_sections(const struct kmod_module *mod) |
1936 | { | |
1937 | char dname[PATH_MAX]; | |
1938 | struct kmod_list *list = NULL; | |
1939 | DIR *d; | |
1940 | int dfd; | |
f12ae3c4 GSB |
1941 | |
1942 | if (mod == NULL) | |
1943 | return NULL; | |
28c175ed | 1944 | |
f12ae3c4 GSB |
1945 | snprintf(dname, sizeof(dname), "/sys/module/%s/sections", mod->name); |
1946 | ||
1947 | d = opendir(dname); | |
1948 | if (d == NULL) { | |
1949 | ERR(mod->ctx, "could not open '%s': %s\n", | |
1950 | dname, strerror(errno)); | |
1951 | return NULL; | |
1952 | } | |
1953 | ||
1954 | dfd = dirfd(d); | |
40923bdb LDM |
1955 | |
1956 | for (;;) { | |
1957 | struct dirent de, *entp; | |
f12ae3c4 | 1958 | struct kmod_module_section *section; |
40923bdb | 1959 | struct kmod_list *l; |
f12ae3c4 GSB |
1960 | unsigned long address; |
1961 | size_t namesz; | |
1962 | int fd, err; | |
1963 | ||
40923bdb LDM |
1964 | err = readdir_r(d, &de, &entp); |
1965 | if (err != 0) { | |
1966 | ERR(mod->ctx, "could not iterate for module '%s': %s\n", | |
1967 | mod->name, strerror(-err)); | |
1968 | goto fail; | |
1969 | } | |
1970 | ||
1971 | if (de.d_name[0] == '.') { | |
1972 | if (de.d_name[1] == '\0' || | |
1973 | (de.d_name[1] == '.' && de.d_name[2] == '\0')) | |
f12ae3c4 GSB |
1974 | continue; |
1975 | } | |
1976 | ||
8e3e5839 | 1977 | fd = openat(dfd, de.d_name, O_RDONLY|O_CLOEXEC); |
f12ae3c4 | 1978 | if (fd < 0) { |
40923bdb LDM |
1979 | ERR(mod->ctx, "could not open '%s/%s': %m\n", |
1980 | dname, de.d_name); | |
1981 | goto fail; | |
f12ae3c4 GSB |
1982 | } |
1983 | ||
1984 | err = read_str_ulong(fd, &address, 16); | |
40923bdb | 1985 | close(fd); |
f12ae3c4 | 1986 | if (err < 0) { |
40923bdb LDM |
1987 | ERR(mod->ctx, "could not read long from '%s/%s': %m\n", |
1988 | dname, de.d_name); | |
1989 | goto fail; | |
1990 | } | |
1991 | ||
1992 | namesz = strlen(de.d_name) + 1; | |
1993 | section = malloc(sizeof(*section) + namesz); | |
1994 | ||
1995 | if (section == NULL) { | |
1996 | ERR(mod->ctx, "out of memory\n"); | |
1997 | goto fail; | |
f12ae3c4 GSB |
1998 | } |
1999 | ||
f12ae3c4 | 2000 | section->address = address; |
40923bdb | 2001 | memcpy(section->name, de.d_name, namesz); |
f12ae3c4 | 2002 | |
40923bdb LDM |
2003 | l = kmod_list_append(list, section); |
2004 | if (l != NULL) { | |
2005 | list = l; | |
2006 | } else { | |
f12ae3c4 GSB |
2007 | ERR(mod->ctx, "out of memory\n"); |
2008 | free(section); | |
40923bdb | 2009 | goto fail; |
f12ae3c4 | 2010 | } |
f12ae3c4 GSB |
2011 | } |
2012 | ||
2013 | closedir(d); | |
2014 | return list; | |
40923bdb LDM |
2015 | |
2016 | fail: | |
2017 | closedir(d); | |
2018 | kmod_module_unref_list(list); | |
2019 | return NULL; | |
f12ae3c4 GSB |
2020 | } |
2021 | ||
7afc98a1 LDM |
2022 | /** |
2023 | * kmod_module_section_get_module_name: | |
2024 | * @entry: a list entry representing a kmod module section | |
2025 | * | |
2026 | * Get the name of a kmod module section. | |
2027 | * | |
2028 | * After use, free the @list by calling kmod_module_section_free_list(). | |
2029 | * | |
2030 | * Returns: the name of this kmod module section on success or NULL on | |
2031 | * failure. The string is owned by the section, do not free it. | |
2032 | */ | |
f12ae3c4 GSB |
2033 | KMOD_EXPORT const char *kmod_module_section_get_name(const struct kmod_list *entry) |
2034 | { | |
2035 | struct kmod_module_section *section; | |
28c175ed | 2036 | |
f12ae3c4 GSB |
2037 | if (entry == NULL) |
2038 | return NULL; | |
28c175ed | 2039 | |
f12ae3c4 GSB |
2040 | section = entry->data; |
2041 | return section->name; | |
2042 | } | |
2043 | ||
7afc98a1 LDM |
2044 | /** |
2045 | * kmod_module_section_get_address: | |
2046 | * @entry: a list entry representing a kmod module section | |
2047 | * | |
2048 | * Get the address of a kmod module section. | |
2049 | * | |
2050 | * After use, free the @list by calling kmod_module_section_free_list(). | |
2051 | * | |
2052 | * Returns: the address of this kmod module section on success or ULONG_MAX | |
2053 | * on failure. | |
2054 | */ | |
f12ae3c4 GSB |
2055 | KMOD_EXPORT unsigned long kmod_module_section_get_address(const struct kmod_list *entry) |
2056 | { | |
2057 | struct kmod_module_section *section; | |
28c175ed | 2058 | |
f12ae3c4 GSB |
2059 | if (entry == NULL) |
2060 | return (unsigned long)-1; | |
28c175ed | 2061 | |
f12ae3c4 GSB |
2062 | section = entry->data; |
2063 | return section->address; | |
2064 | } | |
2065 | ||
7afc98a1 LDM |
2066 | /** |
2067 | * kmod_module_section_free_list: | |
2068 | * @list: kmod module section list | |
2069 | * | |
2070 | * Release the resources taken by @list | |
2071 | */ | |
f12ae3c4 GSB |
2072 | KMOD_EXPORT void kmod_module_section_free_list(struct kmod_list *list) |
2073 | { | |
2074 | while (list) { | |
2075 | kmod_module_section_free(list->data); | |
2076 | list = kmod_list_remove(list); | |
2077 | } | |
2078 | } | |
708624a4 | 2079 | |
1eff942e LDM |
2080 | static struct kmod_elf *kmod_module_get_elf(const struct kmod_module *mod) |
2081 | { | |
2082 | if (mod->file == NULL) { | |
2083 | const char *path = kmod_module_get_path(mod); | |
2084 | ||
2085 | if (path == NULL) { | |
2086 | errno = ENOENT; | |
2087 | return NULL; | |
2088 | } | |
2089 | ||
2090 | ((struct kmod_module *)mod)->file = kmod_file_open(mod->ctx, | |
2091 | path); | |
2092 | if (mod->file == NULL) | |
2093 | return NULL; | |
2094 | } | |
2095 | ||
2096 | return kmod_file_get_elf(mod->file); | |
2097 | } | |
2098 | ||
708624a4 GSB |
2099 | struct kmod_module_info { |
2100 | char *key; | |
2101 | char value[]; | |
2102 | }; | |
2103 | ||
2104 | static struct kmod_module_info *kmod_module_info_new(const char *key, size_t keylen, const char *value, size_t valuelen) | |
2105 | { | |
2106 | struct kmod_module_info *info; | |
2107 | ||
2108 | info = malloc(sizeof(struct kmod_module_info) + keylen + valuelen + 2); | |
2109 | if (info == NULL) | |
2110 | return NULL; | |
2111 | ||
2112 | info->key = (char *)info + sizeof(struct kmod_module_info) | |
2113 | + valuelen + 1; | |
2114 | memcpy(info->key, key, keylen); | |
2115 | info->key[keylen] = '\0'; | |
2116 | memcpy(info->value, value, valuelen); | |
2117 | info->value[valuelen] = '\0'; | |
2118 | return info; | |
2119 | } | |
2120 | ||
2121 | static void kmod_module_info_free(struct kmod_module_info *info) | |
2122 | { | |
2123 | free(info); | |
2124 | } | |
2125 | ||
f64458ca MM |
2126 | static struct kmod_list *kmod_module_info_append(struct kmod_list **list, const char *key, size_t keylen, const char *value, size_t valuelen) |
2127 | { | |
2128 | struct kmod_module_info *info; | |
2129 | struct kmod_list *n; | |
2130 | ||
2131 | info = kmod_module_info_new(key, keylen, value, valuelen); | |
6333934e | 2132 | if (info == NULL) |
f64458ca | 2133 | return NULL; |
f64458ca | 2134 | n = kmod_list_append(*list, info); |
6333934e MM |
2135 | if (n != NULL) |
2136 | *list = n; | |
2137 | else | |
f64458ca | 2138 | kmod_module_info_free(info); |
f64458ca MM |
2139 | return n; |
2140 | } | |
2141 | ||
708624a4 GSB |
2142 | /** |
2143 | * kmod_module_get_info: | |
2144 | * @mod: kmod module | |
2145 | * @list: where to return list of module information. Use | |
2146 | * kmod_module_info_get_key() and | |
2147 | * kmod_module_info_get_value(). Release this list with | |
db74ceec | 2148 | * kmod_module_info_free_list() |
708624a4 GSB |
2149 | * |
2150 | * Get a list of entries in ELF section ".modinfo", these contain | |
2151 | * alias, license, depends, vermagic and other keys with respective | |
8fe1681c MM |
2152 | * values. If the module is signed (CONFIG_MODULE_SIG), information |
2153 | * about the module signature is included as well: signer, | |
2154 | * sig_key and sig_hashalgo. | |
708624a4 GSB |
2155 | * |
2156 | * After use, free the @list by calling kmod_module_info_free_list(). | |
2157 | * | |
2158 | * Returns: 0 on success or < 0 otherwise. | |
2159 | */ | |
2160 | KMOD_EXPORT int kmod_module_get_info(const struct kmod_module *mod, struct kmod_list **list) | |
2161 | { | |
708624a4 | 2162 | struct kmod_elf *elf; |
708624a4 | 2163 | char **strings; |
f64458ca | 2164 | int i, count, ret = -ENOMEM; |
8fe1681c | 2165 | struct kmod_signature_info sig_info; |
708624a4 GSB |
2166 | |
2167 | if (mod == NULL || list == NULL) | |
2168 | return -ENOENT; | |
2169 | ||
2170 | assert(*list == NULL); | |
2171 | ||
1eff942e LDM |
2172 | elf = kmod_module_get_elf(mod); |
2173 | if (elf == NULL) | |
708624a4 GSB |
2174 | return -errno; |
2175 | ||
708624a4 | 2176 | count = kmod_elf_get_strings(elf, ".modinfo", &strings); |
1eff942e LDM |
2177 | if (count < 0) |
2178 | return count; | |
708624a4 GSB |
2179 | |
2180 | for (i = 0; i < count; i++) { | |
708624a4 GSB |
2181 | struct kmod_list *n; |
2182 | const char *key, *value; | |
2183 | size_t keylen, valuelen; | |
2184 | ||
2185 | key = strings[i]; | |
2186 | value = strchr(key, '='); | |
2187 | if (value == NULL) { | |
2188 | keylen = strlen(key); | |
2189 | valuelen = 0; | |
2190 | } else { | |
2191 | keylen = value - key; | |
2192 | value++; | |
2193 | valuelen = strlen(value); | |
2194 | } | |
2195 | ||
f64458ca MM |
2196 | n = kmod_module_info_append(list, key, keylen, value, valuelen); |
2197 | if (n == NULL) | |
708624a4 | 2198 | goto list_error; |
708624a4 | 2199 | } |
8fe1681c MM |
2200 | |
2201 | if (kmod_module_signature_info(mod->file, &sig_info)) { | |
2202 | struct kmod_list *n; | |
2203 | char *key_hex; | |
2204 | ||
2205 | n = kmod_module_info_append(list, "signer", strlen("signer"), | |
2206 | sig_info.signer, sig_info.signer_len); | |
2207 | if (n == NULL) | |
2208 | goto list_error; | |
2209 | count++; | |
2210 | ||
2211 | /* Display the key id as 01:12:DE:AD:BE:EF:... */ | |
2212 | key_hex = malloc(sig_info.key_id_len * 3); | |
2213 | if (key_hex == NULL) | |
2214 | goto list_error; | |
2215 | for (i = 0; i < (int)sig_info.key_id_len; i++) { | |
2216 | sprintf(key_hex + i * 3, "%02X", | |
2217 | (unsigned char)sig_info.key_id[i]); | |
2218 | if (i < (int)sig_info.key_id_len - 1) | |
2219 | key_hex[i * 3 + 2] = ':'; | |
2220 | } | |
2221 | n = kmod_module_info_append(list, "sig_key", strlen("sig_key"), | |
2222 | key_hex, sig_info.key_id_len * 3 - 1); | |
2223 | free(key_hex); | |
2224 | if (n == NULL) | |
2225 | goto list_error; | |
2226 | count++; | |
2227 | ||
2228 | n = kmod_module_info_append(list, | |
2229 | "sig_hashalgo", strlen("sig_hashalgo"), | |
2230 | sig_info.hash_algo, strlen(sig_info.hash_algo)); | |
2231 | if (n == NULL) | |
2232 | goto list_error; | |
2233 | count++; | |
2234 | ||
2235 | /* | |
2236 | * Omit sig_info.id_type and sig_info.algo for now, as these | |
2237 | * are currently constant. | |
2238 | */ | |
2239 | } | |
708624a4 GSB |
2240 | ret = count; |
2241 | ||
2242 | list_error: | |
6333934e MM |
2243 | if (ret < 0) { |
2244 | kmod_module_info_free_list(*list); | |
2245 | *list = NULL; | |
2246 | } | |
708624a4 | 2247 | free(strings); |
708624a4 GSB |
2248 | return ret; |
2249 | } | |
2250 | ||
2251 | /** | |
2252 | * kmod_module_info_get_key: | |
2253 | * @entry: a list entry representing a kmod module info | |
2254 | * | |
2255 | * Get the key of a kmod module info. | |
2256 | * | |
2257 | * Returns: the key of this kmod module info on success or NULL on | |
2258 | * failure. The string is owned by the info, do not free it. | |
2259 | */ | |
2260 | KMOD_EXPORT const char *kmod_module_info_get_key(const struct kmod_list *entry) | |
2261 | { | |
2262 | struct kmod_module_info *info; | |
2263 | ||
2264 | if (entry == NULL) | |
2265 | return NULL; | |
2266 | ||
2267 | info = entry->data; | |
2268 | return info->key; | |
2269 | } | |
2270 | ||
2271 | /** | |
2272 | * kmod_module_info_get_value: | |
2273 | * @entry: a list entry representing a kmod module info | |
2274 | * | |
2275 | * Get the value of a kmod module info. | |
2276 | * | |
2277 | * Returns: the value of this kmod module info on success or NULL on | |
2278 | * failure. The string is owned by the info, do not free it. | |
2279 | */ | |
2280 | KMOD_EXPORT const char *kmod_module_info_get_value(const struct kmod_list *entry) | |
2281 | { | |
2282 | struct kmod_module_info *info; | |
2283 | ||
2284 | if (entry == NULL) | |
2285 | return NULL; | |
2286 | ||
2287 | info = entry->data; | |
2288 | return info->value; | |
2289 | } | |
2290 | ||
2291 | /** | |
2292 | * kmod_module_info_free_list: | |
2293 | * @list: kmod module info list | |
2294 | * | |
2295 | * Release the resources taken by @list | |
2296 | */ | |
2297 | KMOD_EXPORT void kmod_module_info_free_list(struct kmod_list *list) | |
2298 | { | |
2299 | while (list) { | |
2300 | kmod_module_info_free(list->data); | |
2301 | list = kmod_list_remove(list); | |
2302 | } | |
2303 | } | |
2304 | ||
2305 | struct kmod_module_version { | |
2306 | uint64_t crc; | |
2307 | char symbol[]; | |
2308 | }; | |
2309 | ||
2310 | static struct kmod_module_version *kmod_module_versions_new(uint64_t crc, const char *symbol) | |
2311 | { | |
2312 | struct kmod_module_version *mv; | |
2313 | size_t symbollen = strlen(symbol) + 1; | |
2314 | ||
2315 | mv = malloc(sizeof(struct kmod_module_version) + symbollen); | |
2316 | if (mv == NULL) | |
2317 | return NULL; | |
2318 | ||
2319 | mv->crc = crc; | |
2320 | memcpy(mv->symbol, symbol, symbollen); | |
2321 | return mv; | |
2322 | } | |
2323 | ||
2324 | static void kmod_module_version_free(struct kmod_module_version *version) | |
2325 | { | |
2326 | free(version); | |
2327 | } | |
2328 | ||
2329 | /** | |
2330 | * kmod_module_get_versions: | |
2331 | * @mod: kmod module | |
2332 | * @list: where to return list of module versions. Use | |
db74ceec LDM |
2333 | * kmod_module_version_get_symbol() and |
2334 | * kmod_module_version_get_crc(). Release this list with | |
2335 | * kmod_module_versions_free_list() | |
708624a4 GSB |
2336 | * |
2337 | * Get a list of entries in ELF section "__versions". | |
2338 | * | |
2339 | * After use, free the @list by calling kmod_module_versions_free_list(). | |
2340 | * | |
2341 | * Returns: 0 on success or < 0 otherwise. | |
2342 | */ | |
2343 | KMOD_EXPORT int kmod_module_get_versions(const struct kmod_module *mod, struct kmod_list **list) | |
2344 | { | |
708624a4 | 2345 | struct kmod_elf *elf; |
708624a4 | 2346 | struct kmod_modversion *versions; |
708624a4 GSB |
2347 | int i, count, ret = 0; |
2348 | ||
2349 | if (mod == NULL || list == NULL) | |
2350 | return -ENOENT; | |
2351 | ||
2352 | assert(*list == NULL); | |
2353 | ||
1eff942e LDM |
2354 | elf = kmod_module_get_elf(mod); |
2355 | if (elf == NULL) | |
708624a4 GSB |
2356 | return -errno; |
2357 | ||
708624a4 | 2358 | count = kmod_elf_get_modversions(elf, &versions); |
1eff942e LDM |
2359 | if (count < 0) |
2360 | return count; | |
708624a4 GSB |
2361 | |
2362 | for (i = 0; i < count; i++) { | |
2363 | struct kmod_module_version *mv; | |
2364 | struct kmod_list *n; | |
2365 | ||
2366 | mv = kmod_module_versions_new(versions[i].crc, versions[i].symbol); | |
2367 | if (mv == NULL) { | |
2368 | ret = -errno; | |
2369 | kmod_module_versions_free_list(*list); | |
2370 | *list = NULL; | |
2371 | goto list_error; | |
2372 | } | |
2373 | ||
2374 | n = kmod_list_append(*list, mv); | |
2375 | if (n != NULL) | |
2376 | *list = n; | |
2377 | else { | |
2378 | kmod_module_version_free(mv); | |
2379 | kmod_module_versions_free_list(*list); | |
2380 | *list = NULL; | |
2381 | ret = -ENOMEM; | |
2382 | goto list_error; | |
2383 | } | |
2384 | } | |
2385 | ret = count; | |
2386 | ||
2387 | list_error: | |
2388 | free(versions); | |
708624a4 GSB |
2389 | return ret; |
2390 | } | |
2391 | ||
2392 | /** | |
2393 | * kmod_module_versions_get_symbol: | |
2394 | * @entry: a list entry representing a kmod module versions | |
2395 | * | |
2396 | * Get the symbol of a kmod module versions. | |
2397 | * | |
2398 | * Returns: the symbol of this kmod module versions on success or NULL | |
2399 | * on failure. The string is owned by the versions, do not free it. | |
2400 | */ | |
2401 | KMOD_EXPORT const char *kmod_module_version_get_symbol(const struct kmod_list *entry) | |
2402 | { | |
2403 | struct kmod_module_version *version; | |
2404 | ||
2405 | if (entry == NULL) | |
2406 | return NULL; | |
2407 | ||
2408 | version = entry->data; | |
2409 | return version->symbol; | |
2410 | } | |
2411 | ||
2412 | /** | |
2413 | * kmod_module_version_get_crc: | |
2414 | * @entry: a list entry representing a kmod module version | |
2415 | * | |
2416 | * Get the crc of a kmod module version. | |
2417 | * | |
2418 | * Returns: the crc of this kmod module version on success or NULL on | |
2419 | * failure. The string is owned by the version, do not free it. | |
2420 | */ | |
2421 | KMOD_EXPORT uint64_t kmod_module_version_get_crc(const struct kmod_list *entry) | |
2422 | { | |
2423 | struct kmod_module_version *version; | |
2424 | ||
2425 | if (entry == NULL) | |
2426 | return 0; | |
2427 | ||
2428 | version = entry->data; | |
2429 | return version->crc; | |
2430 | } | |
2431 | ||
2432 | /** | |
2433 | * kmod_module_versions_free_list: | |
2434 | * @list: kmod module versions list | |
2435 | * | |
2436 | * Release the resources taken by @list | |
2437 | */ | |
2438 | KMOD_EXPORT void kmod_module_versions_free_list(struct kmod_list *list) | |
2439 | { | |
2440 | while (list) { | |
2441 | kmod_module_version_free(list->data); | |
2442 | list = kmod_list_remove(list); | |
2443 | } | |
2444 | } | |
45e6db9c GSB |
2445 | |
2446 | struct kmod_module_symbol { | |
2447 | uint64_t crc; | |
2448 | char symbol[]; | |
2449 | }; | |
2450 | ||
2451 | static struct kmod_module_symbol *kmod_module_symbols_new(uint64_t crc, const char *symbol) | |
2452 | { | |
2453 | struct kmod_module_symbol *mv; | |
2454 | size_t symbollen = strlen(symbol) + 1; | |
2455 | ||
2456 | mv = malloc(sizeof(struct kmod_module_symbol) + symbollen); | |
2457 | if (mv == NULL) | |
2458 | return NULL; | |
2459 | ||
2460 | mv->crc = crc; | |
2461 | memcpy(mv->symbol, symbol, symbollen); | |
2462 | return mv; | |
2463 | } | |
2464 | ||
2465 | static void kmod_module_symbol_free(struct kmod_module_symbol *symbol) | |
2466 | { | |
2467 | free(symbol); | |
2468 | } | |
2469 | ||
2470 | /** | |
2471 | * kmod_module_get_symbols: | |
2472 | * @mod: kmod module | |
2473 | * @list: where to return list of module symbols. Use | |
db74ceec LDM |
2474 | * kmod_module_symbol_get_symbol() and |
2475 | * kmod_module_symbol_get_crc(). Release this list with | |
2476 | * kmod_module_symbols_free_list() | |
45e6db9c GSB |
2477 | * |
2478 | * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings". | |
2479 | * | |
2480 | * After use, free the @list by calling kmod_module_symbols_free_list(). | |
2481 | * | |
2482 | * Returns: 0 on success or < 0 otherwise. | |
2483 | */ | |
2484 | KMOD_EXPORT int kmod_module_get_symbols(const struct kmod_module *mod, struct kmod_list **list) | |
2485 | { | |
45e6db9c | 2486 | struct kmod_elf *elf; |
45e6db9c | 2487 | struct kmod_modversion *symbols; |
45e6db9c GSB |
2488 | int i, count, ret = 0; |
2489 | ||
2490 | if (mod == NULL || list == NULL) | |
2491 | return -ENOENT; | |
2492 | ||
2493 | assert(*list == NULL); | |
2494 | ||
1eff942e LDM |
2495 | elf = kmod_module_get_elf(mod); |
2496 | if (elf == NULL) | |
45e6db9c GSB |
2497 | return -errno; |
2498 | ||
45e6db9c | 2499 | count = kmod_elf_get_symbols(elf, &symbols); |
1eff942e LDM |
2500 | if (count < 0) |
2501 | return count; | |
45e6db9c GSB |
2502 | |
2503 | for (i = 0; i < count; i++) { | |
2504 | struct kmod_module_symbol *mv; | |
2505 | struct kmod_list *n; | |
2506 | ||
2507 | mv = kmod_module_symbols_new(symbols[i].crc, symbols[i].symbol); | |
2508 | if (mv == NULL) { | |
2509 | ret = -errno; | |
2510 | kmod_module_symbols_free_list(*list); | |
2511 | *list = NULL; | |
2512 | goto list_error; | |
2513 | } | |
2514 | ||
2515 | n = kmod_list_append(*list, mv); | |
2516 | if (n != NULL) | |
2517 | *list = n; | |
2518 | else { | |
2519 | kmod_module_symbol_free(mv); | |
2520 | kmod_module_symbols_free_list(*list); | |
2521 | *list = NULL; | |
2522 | ret = -ENOMEM; | |
2523 | goto list_error; | |
2524 | } | |
2525 | } | |
2526 | ret = count; | |
2527 | ||
2528 | list_error: | |
2529 | free(symbols); | |
45e6db9c GSB |
2530 | return ret; |
2531 | } | |
2532 | ||
2533 | /** | |
db74ceec | 2534 | * kmod_module_symbol_get_symbol: |
45e6db9c GSB |
2535 | * @entry: a list entry representing a kmod module symbols |
2536 | * | |
2537 | * Get the symbol of a kmod module symbols. | |
2538 | * | |
2539 | * Returns: the symbol of this kmod module symbols on success or NULL | |
2540 | * on failure. The string is owned by the symbols, do not free it. | |
2541 | */ | |
2542 | KMOD_EXPORT const char *kmod_module_symbol_get_symbol(const struct kmod_list *entry) | |
2543 | { | |
2544 | struct kmod_module_symbol *symbol; | |
2545 | ||
2546 | if (entry == NULL) | |
2547 | return NULL; | |
2548 | ||
2549 | symbol = entry->data; | |
2550 | return symbol->symbol; | |
2551 | } | |
2552 | ||
2553 | /** | |
2554 | * kmod_module_symbol_get_crc: | |
2555 | * @entry: a list entry representing a kmod module symbol | |
2556 | * | |
2557 | * Get the crc of a kmod module symbol. | |
2558 | * | |
2559 | * Returns: the crc of this kmod module symbol on success or NULL on | |
2560 | * failure. The string is owned by the symbol, do not free it. | |
2561 | */ | |
2562 | KMOD_EXPORT uint64_t kmod_module_symbol_get_crc(const struct kmod_list *entry) | |
2563 | { | |
2564 | struct kmod_module_symbol *symbol; | |
2565 | ||
2566 | if (entry == NULL) | |
2567 | return 0; | |
2568 | ||
2569 | symbol = entry->data; | |
2570 | return symbol->crc; | |
2571 | } | |
2572 | ||
2573 | /** | |
2574 | * kmod_module_symbols_free_list: | |
2575 | * @list: kmod module symbols list | |
2576 | * | |
2577 | * Release the resources taken by @list | |
2578 | */ | |
2579 | KMOD_EXPORT void kmod_module_symbols_free_list(struct kmod_list *list) | |
2580 | { | |
2581 | while (list) { | |
2582 | kmod_module_symbol_free(list->data); | |
2583 | list = kmod_list_remove(list); | |
2584 | } | |
2585 | } | |
674f8590 GSB |
2586 | |
2587 | struct kmod_module_dependency_symbol { | |
2588 | uint64_t crc; | |
2589 | uint8_t bind; | |
2590 | char symbol[]; | |
2591 | }; | |
2592 | ||
2593 | static struct kmod_module_dependency_symbol *kmod_module_dependency_symbols_new(uint64_t crc, uint8_t bind, const char *symbol) | |
2594 | { | |
2595 | struct kmod_module_dependency_symbol *mv; | |
2596 | size_t symbollen = strlen(symbol) + 1; | |
2597 | ||
2598 | mv = malloc(sizeof(struct kmod_module_dependency_symbol) + symbollen); | |
2599 | if (mv == NULL) | |
2600 | return NULL; | |
2601 | ||
2602 | mv->crc = crc; | |
2603 | mv->bind = bind; | |
2604 | memcpy(mv->symbol, symbol, symbollen); | |
2605 | return mv; | |
2606 | } | |
2607 | ||
2608 | static void kmod_module_dependency_symbol_free(struct kmod_module_dependency_symbol *dependency_symbol) | |
2609 | { | |
2610 | free(dependency_symbol); | |
2611 | } | |
2612 | ||
2613 | /** | |
2614 | * kmod_module_get_dependency_symbols: | |
2615 | * @mod: kmod module | |
2616 | * @list: where to return list of module dependency_symbols. Use | |
2617 | * kmod_module_dependency_symbol_get_symbol() and | |
2618 | * kmod_module_dependency_symbol_get_crc(). Release this list with | |
2619 | * kmod_module_dependency_symbols_free_list() | |
2620 | * | |
2621 | * Get a list of entries in ELF section ".symtab" or "__ksymtab_strings". | |
2622 | * | |
2623 | * After use, free the @list by calling | |
2624 | * kmod_module_dependency_symbols_free_list(). | |
2625 | * | |
2626 | * Returns: 0 on success or < 0 otherwise. | |
2627 | */ | |
2628 | KMOD_EXPORT int kmod_module_get_dependency_symbols(const struct kmod_module *mod, struct kmod_list **list) | |
2629 | { | |
674f8590 | 2630 | struct kmod_elf *elf; |
674f8590 | 2631 | struct kmod_modversion *symbols; |
674f8590 GSB |
2632 | int i, count, ret = 0; |
2633 | ||
2634 | if (mod == NULL || list == NULL) | |
2635 | return -ENOENT; | |
2636 | ||
2637 | assert(*list == NULL); | |
2638 | ||
1eff942e LDM |
2639 | elf = kmod_module_get_elf(mod); |
2640 | if (elf == NULL) | |
674f8590 GSB |
2641 | return -errno; |
2642 | ||
674f8590 | 2643 | count = kmod_elf_get_dependency_symbols(elf, &symbols); |
1eff942e LDM |
2644 | if (count < 0) |
2645 | return count; | |
674f8590 GSB |
2646 | |
2647 | for (i = 0; i < count; i++) { | |
2648 | struct kmod_module_dependency_symbol *mv; | |
2649 | struct kmod_list *n; | |
2650 | ||
2651 | mv = kmod_module_dependency_symbols_new(symbols[i].crc, | |
2652 | symbols[i].bind, | |
2653 | symbols[i].symbol); | |
2654 | if (mv == NULL) { | |
2655 | ret = -errno; | |
2656 | kmod_module_dependency_symbols_free_list(*list); | |
2657 | *list = NULL; | |
2658 | goto list_error; | |
2659 | } | |
2660 | ||
2661 | n = kmod_list_append(*list, mv); | |
2662 | if (n != NULL) | |
2663 | *list = n; | |
2664 | else { | |
2665 | kmod_module_dependency_symbol_free(mv); | |
2666 | kmod_module_dependency_symbols_free_list(*list); | |
2667 | *list = NULL; | |
2668 | ret = -ENOMEM; | |
2669 | goto list_error; | |
2670 | } | |
2671 | } | |
2672 | ret = count; | |
2673 | ||
2674 | list_error: | |
2675 | free(symbols); | |
674f8590 GSB |
2676 | return ret; |
2677 | } | |
2678 | ||
2679 | /** | |
2680 | * kmod_module_dependency_symbol_get_symbol: | |
2681 | * @entry: a list entry representing a kmod module dependency_symbols | |
2682 | * | |
2683 | * Get the dependency symbol of a kmod module | |
2684 | * | |
2685 | * Returns: the symbol of this kmod module dependency_symbols on success or NULL | |
2686 | * on failure. The string is owned by the dependency_symbols, do not free it. | |
2687 | */ | |
2688 | KMOD_EXPORT const char *kmod_module_dependency_symbol_get_symbol(const struct kmod_list *entry) | |
2689 | { | |
2690 | struct kmod_module_dependency_symbol *dependency_symbol; | |
2691 | ||
2692 | if (entry == NULL) | |
2693 | return NULL; | |
2694 | ||
2695 | dependency_symbol = entry->data; | |
2696 | return dependency_symbol->symbol; | |
2697 | } | |
2698 | ||
2699 | /** | |
2700 | * kmod_module_dependency_symbol_get_crc: | |
2701 | * @entry: a list entry representing a kmod module dependency_symbol | |
2702 | * | |
2703 | * Get the crc of a kmod module dependency_symbol. | |
2704 | * | |
2705 | * Returns: the crc of this kmod module dependency_symbol on success or NULL on | |
2706 | * failure. The string is owned by the dependency_symbol, do not free it. | |
2707 | */ | |
2708 | KMOD_EXPORT uint64_t kmod_module_dependency_symbol_get_crc(const struct kmod_list *entry) | |
2709 | { | |
2710 | struct kmod_module_dependency_symbol *dependency_symbol; | |
2711 | ||
2712 | if (entry == NULL) | |
2713 | return 0; | |
2714 | ||
2715 | dependency_symbol = entry->data; | |
2716 | return dependency_symbol->crc; | |
2717 | } | |
2718 | ||
2719 | /** | |
2720 | * kmod_module_dependency_symbol_get_bind: | |
2721 | * @entry: a list entry representing a kmod module dependency_symbol | |
2722 | * | |
2723 | * Get the bind type of a kmod module dependency_symbol. | |
2724 | * | |
2725 | * Returns: the bind of this kmod module dependency_symbol on success | |
2726 | * or < 0 on failure. | |
2727 | */ | |
2728 | KMOD_EXPORT int kmod_module_dependency_symbol_get_bind(const struct kmod_list *entry) | |
2729 | { | |
2730 | struct kmod_module_dependency_symbol *dependency_symbol; | |
2731 | ||
2732 | if (entry == NULL) | |
2733 | return 0; | |
2734 | ||
2735 | dependency_symbol = entry->data; | |
2736 | return dependency_symbol->bind; | |
2737 | } | |
2738 | ||
2739 | /** | |
2740 | * kmod_module_dependency_symbols_free_list: | |
2741 | * @list: kmod module dependency_symbols list | |
2742 | * | |
2743 | * Release the resources taken by @list | |
2744 | */ | |
2745 | KMOD_EXPORT void kmod_module_dependency_symbols_free_list(struct kmod_list *list) | |
2746 | { | |
2747 | while (list) { | |
2748 | kmod_module_dependency_symbol_free(list->data); | |
2749 | list = kmod_list_remove(list); | |
2750 | } | |
2751 | } |