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