blob: d9a27f2d797def9b8af694cb6352e44e851cce58 [file] [log] [blame]
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -02001/*
2 * kmod-lsmod - list modules from linux kernel using libkmod.
3 *
Lucas De Marchie6b0e492013-01-16 11:27:21 -02004 * Copyright (C) 2011-2013 ProFUSION embedded systems
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -02005 *
Lucas De Marchicb451f32011-12-12 18:24:35 -02006 * 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.
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -020010 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Lucas De Marchicb451f32011-12-12 18:24:35 -020013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -020015 *
Lucas De Marchicb451f32011-12-12 18:24:35 -020016 * 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/>.
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -020018 */
Lucas De Marchicb451f32011-12-12 18:24:35 -020019
Lucas De Marchic2e42862014-10-03 01:41:42 -030020#include <errno.h>
21#include <stddef.h>
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -020022#include <stdio.h>
23#include <stdlib.h>
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -020024#include <string.h>
Lucas De Marchic2e42862014-10-03 01:41:42 -030025#include <unistd.h>
26
Lucas De Marchif3578662015-01-02 12:38:27 -020027#include <libkmod/libkmod.h>
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -020028
Lucas De Marchi4a2e20d2012-11-06 16:54:17 -020029#include "kmod.h"
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -020030
Lucas De Marchi6fcf69e2011-12-22 03:01:45 -020031static int do_lsmod(int argc, char *argv[])
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -020032{
33 struct kmod_ctx *ctx;
Lucas De Marchi2411c072011-12-12 15:41:02 -020034 const char *null_config = NULL;
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -020035 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
Lucas De Marchi2411c072011-12-12 15:41:02 -020043 ctx = kmod_new(NULL, &null_config);
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -020044 if (ctx == NULL) {
45 fputs("Error: kmod_new() failed!\n", stderr);
46 return EXIT_FAILURE;
47 }
48
Lucas De Marchia102e262011-12-12 13:49:27 -020049 err = kmod_module_new_from_loaded(ctx, &list);
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -020050 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
Santiago Vila7266ec42015-04-16 08:44:22 -030067 printf("%-19s %8ld %d", name, size, use_count);
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -020068 holders = kmod_module_get_holders(mod);
69 kmod_list_foreach(hitr, holders) {
70 struct kmod_module *hm = kmod_module_get_module(hitr);
71
Santiago Vila7266ec42015-04-16 08:44:22 -030072 if (!first) {
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -020073 putchar(',');
Santiago Vila7266ec42015-04-16 08:44:22 -030074 } else {
75 putchar(' ');
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -020076 first = 0;
Santiago Vila7266ec42015-04-16 08:44:22 -030077 }
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -020078
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}
Lucas De Marchi6fcf69e2011-12-22 03:01:45 -020091
Lucas De Marchi6fcf69e2011-12-22 03:01:45 -020092const struct kmod_cmd kmod_cmd_compat_lsmod = {
93 .name = "lsmod",
94 .cmd = do_lsmod,
95 .help = "compat lsmod command",
96};
97
Lucas De Marchi252c51a2011-12-23 11:33:02 -020098const struct kmod_cmd kmod_cmd_list = {
99 .name = "list",
100 .cmd = do_lsmod,
101 .help = "list currently loaded modules",
102};