]>
Commit | Line | Data |
---|---|---|
72c51a9e GSB |
1 | /* |
2 | * kmod-lsmod - list modules from linux kernel using libkmod. | |
3 | * | |
e6b0e49b | 4 | * Copyright (C) 2011-2013 ProFUSION embedded systems |
72c51a9e | 5 | * |
cb451f35 LDM |
6 | * This program is free software: you can redistribute it and/or modify |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation, either version 2 of the License, or | |
9 | * (at your option) any later version. | |
72c51a9e GSB |
10 | * |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
cb451f35 LDM |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * GNU General Public License for more details. | |
72c51a9e | 15 | * |
cb451f35 LDM |
16 | * You should have received a copy of the GNU General Public License |
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
72c51a9e | 18 | */ |
cb451f35 | 19 | |
c2e4286b LDM |
20 | #include <errno.h> |
21 | #include <stddef.h> | |
72c51a9e GSB |
22 | #include <stdio.h> |
23 | #include <stdlib.h> | |
72c51a9e | 24 | #include <string.h> |
c2e4286b LDM |
25 | #include <unistd.h> |
26 | ||
f357866d | 27 | #include <libkmod/libkmod.h> |
72c51a9e | 28 | |
4a2e20df | 29 | #include "kmod.h" |
72c51a9e | 30 | |
6fcf69e6 | 31 | static int do_lsmod(int argc, char *argv[]) |
72c51a9e GSB |
32 | { |
33 | struct kmod_ctx *ctx; | |
2411c077 | 34 | const char *null_config = NULL; |
72c51a9e GSB |
35 | struct kmod_list *list, *itr; |
36 | int err; | |
37 | ||
38 | if (argc != 1) { | |
39 | fprintf(stderr, "Usage: %s\n", argv[0]); | |
40 | return EXIT_FAILURE; | |
41 | } | |
42 | ||
2411c077 | 43 | ctx = kmod_new(NULL, &null_config); |
72c51a9e GSB |
44 | if (ctx == NULL) { |
45 | fputs("Error: kmod_new() failed!\n", stderr); | |
46 | return EXIT_FAILURE; | |
47 | } | |
48 | ||
a102e262 | 49 | err = kmod_module_new_from_loaded(ctx, &list); |
72c51a9e GSB |
50 | if (err < 0) { |
51 | fprintf(stderr, "Error: could not get list of modules: %s\n", | |
52 | strerror(-err)); | |
53 | kmod_unref(ctx); | |
54 | return EXIT_FAILURE; | |
55 | } | |
56 | ||
57 | puts("Module Size Used by"); | |
58 | ||
59 | kmod_list_foreach(itr, list) { | |
60 | struct kmod_module *mod = kmod_module_get_module(itr); | |
61 | const char *name = kmod_module_get_name(mod); | |
62 | int use_count = kmod_module_get_refcnt(mod); | |
63 | long size = kmod_module_get_size(mod); | |
64 | struct kmod_list *holders, *hitr; | |
65 | int first = 1; | |
66 | ||
7266ec43 | 67 | printf("%-19s %8ld %d", name, size, use_count); |
72c51a9e GSB |
68 | holders = kmod_module_get_holders(mod); |
69 | kmod_list_foreach(hitr, holders) { | |
70 | struct kmod_module *hm = kmod_module_get_module(hitr); | |
71 | ||
7266ec43 | 72 | if (!first) { |
72c51a9e | 73 | putchar(','); |
7266ec43 SV |
74 | } else { |
75 | putchar(' '); | |
72c51a9e | 76 | first = 0; |
7266ec43 | 77 | } |
72c51a9e GSB |
78 | |
79 | fputs(kmod_module_get_name(hm), stdout); | |
80 | kmod_module_unref(hm); | |
81 | } | |
82 | putchar('\n'); | |
83 | kmod_module_unref_list(holders); | |
84 | kmod_module_unref(mod); | |
85 | } | |
86 | kmod_module_unref_list(list); | |
87 | kmod_unref(ctx); | |
88 | ||
89 | return EXIT_SUCCESS; | |
90 | } | |
6fcf69e6 | 91 | |
6fcf69e6 LDM |
92 | const struct kmod_cmd kmod_cmd_compat_lsmod = { |
93 | .name = "lsmod", | |
94 | .cmd = do_lsmod, | |
95 | .help = "compat lsmod command", | |
96 | }; | |
97 | ||
252c51a9 LDM |
98 | const struct kmod_cmd kmod_cmd_list = { |
99 | .name = "list", | |
100 | .cmd = do_lsmod, | |
101 | .help = "list currently loaded modules", | |
102 | }; |