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