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