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