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