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