]> git.ipfire.org Git - thirdparty/kmod.git/blame - tools/kmod-lsmod.c
tools: kmod: Add handling of compat modprobe
[thirdparty/kmod.git] / tools / kmod-lsmod.c
CommitLineData
72c51a9e
GSB
1/*
2 * kmod-lsmod - list modules from linux kernel using libkmod.
3 *
4 * Copyright (C) 2011 ProFUSION embedded systems
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
72c51a9e
GSB
20#include <stdio.h>
21#include <stdlib.h>
22#include <stddef.h>
23#include <errno.h>
24#include <unistd.h>
25#include <string.h>
26#include "libkmod.h"
27
28
6fcf69e6 29static int do_lsmod(int argc, char *argv[])
72c51a9e
GSB
30{
31 struct kmod_ctx *ctx;
2411c077 32 const char *null_config = NULL;
72c51a9e
GSB
33 struct kmod_list *list, *itr;
34 int err;
35
36 if (argc != 1) {
37 fprintf(stderr, "Usage: %s\n", argv[0]);
38 return EXIT_FAILURE;
39 }
40
2411c077 41 ctx = kmod_new(NULL, &null_config);
72c51a9e
GSB
42 if (ctx == NULL) {
43 fputs("Error: kmod_new() failed!\n", stderr);
44 return EXIT_FAILURE;
45 }
46
a102e262 47 err = kmod_module_new_from_loaded(ctx, &list);
72c51a9e
GSB
48 if (err < 0) {
49 fprintf(stderr, "Error: could not get list of modules: %s\n",
50 strerror(-err));
51 kmod_unref(ctx);
52 return EXIT_FAILURE;
53 }
54
55 puts("Module Size Used by");
56
57 kmod_list_foreach(itr, list) {
58 struct kmod_module *mod = kmod_module_get_module(itr);
59 const char *name = kmod_module_get_name(mod);
60 int use_count = kmod_module_get_refcnt(mod);
61 long size = kmod_module_get_size(mod);
62 struct kmod_list *holders, *hitr;
63 int first = 1;
64
65 printf("%-19s %8ld %d ", name, size, use_count);
66 holders = kmod_module_get_holders(mod);
67 kmod_list_foreach(hitr, holders) {
68 struct kmod_module *hm = kmod_module_get_module(hitr);
69
70 if (!first)
71 putchar(',');
72 else
73 first = 0;
74
75 fputs(kmod_module_get_name(hm), stdout);
76 kmod_module_unref(hm);
77 }
78 putchar('\n');
79 kmod_module_unref_list(holders);
80 kmod_module_unref(mod);
81 }
82 kmod_module_unref_list(list);
83 kmod_unref(ctx);
84
85 return EXIT_SUCCESS;
86}
6fcf69e6
LDM
87
88#ifndef KMOD_BUNDLE_TOOL
89int main(int argc, char *argv[])
90{
91 return do_lsmod(argc, argv);
92}
93
94#else
95#include "kmod.h"
96
97const struct kmod_cmd kmod_cmd_compat_lsmod = {
98 .name = "lsmod",
99 .cmd = do_lsmod,
100 .help = "compat lsmod command",
101};
102
103#endif