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