Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 1 | /* |
| 2 | * kmod-lsmod - list modules from linux kernel using libkmod. |
| 3 | * |
Lucas De Marchi | e6b0e49 | 2013-01-16 11:27:21 -0200 | [diff] [blame] | 4 | * Copyright (C) 2011-2013 ProFUSION embedded systems |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 5 | * |
Lucas De Marchi | cb451f3 | 2011-12-12 18:24:35 -0200 | [diff] [blame] | 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. |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 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 |
Lucas De Marchi | cb451f3 | 2011-12-12 18:24:35 -0200 | [diff] [blame] | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 15 | * |
Lucas De Marchi | cb451f3 | 2011-12-12 18:24:35 -0200 | [diff] [blame] | 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/>. |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 18 | */ |
Lucas De Marchi | cb451f3 | 2011-12-12 18:24:35 -0200 | [diff] [blame] | 19 | |
Lucas De Marchi | c2e4286 | 2014-10-03 01:41:42 -0300 | [diff] [blame] | 20 | #include <errno.h> |
| 21 | #include <stddef.h> |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 24 | #include <string.h> |
Lucas De Marchi | c2e4286 | 2014-10-03 01:41:42 -0300 | [diff] [blame] | 25 | #include <unistd.h> |
| 26 | |
Lucas De Marchi | f357866 | 2015-01-02 12:38:27 -0200 | [diff] [blame] | 27 | #include <libkmod/libkmod.h> |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 28 | |
Lucas De Marchi | 4a2e20d | 2012-11-06 16:54:17 -0200 | [diff] [blame] | 29 | #include "kmod.h" |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 30 | |
Lucas De Marchi | 6fcf69e | 2011-12-22 03:01:45 -0200 | [diff] [blame] | 31 | static int do_lsmod(int argc, char *argv[]) |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 32 | { |
| 33 | struct kmod_ctx *ctx; |
Lucas De Marchi | 2411c07 | 2011-12-12 15:41:02 -0200 | [diff] [blame] | 34 | const char *null_config = NULL; |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 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 | |
Lucas De Marchi | 2411c07 | 2011-12-12 15:41:02 -0200 | [diff] [blame] | 43 | ctx = kmod_new(NULL, &null_config); |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 44 | if (ctx == NULL) { |
| 45 | fputs("Error: kmod_new() failed!\n", stderr); |
| 46 | return EXIT_FAILURE; |
| 47 | } |
| 48 | |
Lucas De Marchi | a102e26 | 2011-12-12 13:49:27 -0200 | [diff] [blame] | 49 | err = kmod_module_new_from_loaded(ctx, &list); |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 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 | |
Santiago Vila | 7266ec4 | 2015-04-16 08:44:22 -0300 | [diff] [blame] | 67 | printf("%-19s %8ld %d", name, size, use_count); |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 68 | holders = kmod_module_get_holders(mod); |
| 69 | kmod_list_foreach(hitr, holders) { |
| 70 | struct kmod_module *hm = kmod_module_get_module(hitr); |
| 71 | |
Santiago Vila | 7266ec4 | 2015-04-16 08:44:22 -0300 | [diff] [blame] | 72 | if (!first) { |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 73 | putchar(','); |
Santiago Vila | 7266ec4 | 2015-04-16 08:44:22 -0300 | [diff] [blame] | 74 | } else { |
| 75 | putchar(' '); |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 76 | first = 0; |
Santiago Vila | 7266ec4 | 2015-04-16 08:44:22 -0300 | [diff] [blame] | 77 | } |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 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 | } |
Lucas De Marchi | 6fcf69e | 2011-12-22 03:01:45 -0200 | [diff] [blame] | 91 | |
Lucas De Marchi | 6fcf69e | 2011-12-22 03:01:45 -0200 | [diff] [blame] | 92 | const struct kmod_cmd kmod_cmd_compat_lsmod = { |
| 93 | .name = "lsmod", |
| 94 | .cmd = do_lsmod, |
| 95 | .help = "compat lsmod command", |
| 96 | }; |
| 97 | |
Lucas De Marchi | 252c51a | 2011-12-23 11:33:02 -0200 | [diff] [blame] | 98 | const struct kmod_cmd kmod_cmd_list = { |
| 99 | .name = "list", |
| 100 | .cmd = do_lsmod, |
| 101 | .help = "list currently loaded modules", |
| 102 | }; |