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