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