diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 17:03:56 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-19 17:03:56 +0000 |
commit | 18da3ffcd7f3c8a0c5f790c801b5813503c2273d (patch) | |
tree | 84caf98dc5cef3d123c56ba12e35fd67026e0693 /tools/lsmod.c | |
parent | Initial commit. (diff) | |
download | kmod-18da3ffcd7f3c8a0c5f790c801b5813503c2273d.tar.xz kmod-18da3ffcd7f3c8a0c5f790c801b5813503c2273d.zip |
Adding upstream version 31+20240202.upstream/31+20240202
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'tools/lsmod.c')
-rw-r--r-- | tools/lsmod.c | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/tools/lsmod.c b/tools/lsmod.c new file mode 100644 index 0000000..d9a27f2 --- /dev/null +++ b/tools/lsmod.c @@ -0,0 +1,102 @@ +/* + * kmod-lsmod - list modules from linux kernel using libkmod. + * + * Copyright (C) 2011-2013 ProFUSION embedded systems + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <errno.h> +#include <stddef.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#include <libkmod/libkmod.h> + +#include "kmod.h" + +static int do_lsmod(int argc, char *argv[]) +{ + struct kmod_ctx *ctx; + const char *null_config = NULL; + struct kmod_list *list, *itr; + int err; + + if (argc != 1) { + fprintf(stderr, "Usage: %s\n", argv[0]); + return EXIT_FAILURE; + } + + ctx = kmod_new(NULL, &null_config); + if (ctx == NULL) { + fputs("Error: kmod_new() failed!\n", stderr); + return EXIT_FAILURE; + } + + err = kmod_module_new_from_loaded(ctx, &list); + if (err < 0) { + fprintf(stderr, "Error: could not get list of modules: %s\n", + strerror(-err)); + kmod_unref(ctx); + return EXIT_FAILURE; + } + + puts("Module Size Used by"); + + kmod_list_foreach(itr, list) { + struct kmod_module *mod = kmod_module_get_module(itr); + const char *name = kmod_module_get_name(mod); + int use_count = kmod_module_get_refcnt(mod); + long size = kmod_module_get_size(mod); + struct kmod_list *holders, *hitr; + int first = 1; + + printf("%-19s %8ld %d", name, size, use_count); + holders = kmod_module_get_holders(mod); + kmod_list_foreach(hitr, holders) { + struct kmod_module *hm = kmod_module_get_module(hitr); + + if (!first) { + putchar(','); + } else { + putchar(' '); + first = 0; + } + + fputs(kmod_module_get_name(hm), stdout); + kmod_module_unref(hm); + } + putchar('\n'); + kmod_module_unref_list(holders); + kmod_module_unref(mod); + } + kmod_module_unref_list(list); + kmod_unref(ctx); + + return EXIT_SUCCESS; +} + +const struct kmod_cmd kmod_cmd_compat_lsmod = { + .name = "lsmod", + .cmd = do_lsmod, + .help = "compat lsmod command", +}; + +const struct kmod_cmd kmod_cmd_list = { + .name = "list", + .cmd = do_lsmod, + .help = "list currently loaded modules", +}; |