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