]> git.ipfire.org Git - thirdparty/kmod.git/blame - tools/lsmod.c
Move hash implementation to shared directory
[thirdparty/kmod.git] / tools / lsmod.c
CommitLineData
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
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
4a2e20df 28#include "kmod.h"
72c51a9e 29
6fcf69e6 30static int do_lsmod(int argc, char *argv[])
72c51a9e
GSB
31{
32 struct kmod_ctx *ctx;
2411c077 33 const char *null_config = NULL;
72c51a9e
GSB
34 struct kmod_list *list, *itr;
35 int err;
36
37 if (argc != 1) {
38 fprintf(stderr, "Usage: %s\n", argv[0]);
39 return EXIT_FAILURE;
40 }
41
2411c077 42 ctx = kmod_new(NULL, &null_config);
72c51a9e
GSB
43 if (ctx == NULL) {
44 fputs("Error: kmod_new() failed!\n", stderr);
45 return EXIT_FAILURE;
46 }
47
a102e262 48 err = kmod_module_new_from_loaded(ctx, &list);
72c51a9e
GSB
49 if (err < 0) {
50 fprintf(stderr, "Error: could not get list of modules: %s\n",
51 strerror(-err));
52 kmod_unref(ctx);
53 return EXIT_FAILURE;
54 }
55
56 puts("Module Size Used by");
57
58 kmod_list_foreach(itr, list) {
59 struct kmod_module *mod = kmod_module_get_module(itr);
60 const char *name = kmod_module_get_name(mod);
61 int use_count = kmod_module_get_refcnt(mod);
62 long size = kmod_module_get_size(mod);
63 struct kmod_list *holders, *hitr;
64 int first = 1;
65
66 printf("%-19s %8ld %d ", name, size, use_count);
67 holders = kmod_module_get_holders(mod);
68 kmod_list_foreach(hitr, holders) {
69 struct kmod_module *hm = kmod_module_get_module(hitr);
70
71 if (!first)
72 putchar(',');
73 else
74 first = 0;
75
76 fputs(kmod_module_get_name(hm), stdout);
77 kmod_module_unref(hm);
78 }
79 putchar('\n');
80 kmod_module_unref_list(holders);
81 kmod_module_unref(mod);
82 }
83 kmod_module_unref_list(list);
84 kmod_unref(ctx);
85
86 return EXIT_SUCCESS;
87}
6fcf69e6 88
6fcf69e6
LDM
89const struct kmod_cmd kmod_cmd_compat_lsmod = {
90 .name = "lsmod",
91 .cmd = do_lsmod,
92 .help = "compat lsmod command",
93};
94
252c51a9
LDM
95const struct kmod_cmd kmod_cmd_list = {
96 .name = "list",
97 .cmd = do_lsmod,
98 .help = "list currently loaded modules",
99};