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