2011-12-11 07:19:41 +07:00
|
|
|
/*
|
|
|
|
* kmod-lsmod - list modules from linux kernel using libkmod.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2011 ProFUSION embedded systems
|
|
|
|
*
|
2011-12-13 03:24:35 +07:00
|
|
|
* 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.
|
2011-12-11 07:19:41 +07:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2011-12-13 03:24:35 +07:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2011-12-11 07:19:41 +07:00
|
|
|
*
|
2011-12-13 03:24:35 +07:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2011-12-11 07:19:41 +07:00
|
|
|
*/
|
2011-12-13 03:24:35 +07:00
|
|
|
|
2011-12-11 07:19:41 +07:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "libkmod.h"
|
|
|
|
|
|
|
|
|
2011-12-22 12:01:45 +07:00
|
|
|
static int do_lsmod(int argc, char *argv[])
|
2011-12-11 07:19:41 +07:00
|
|
|
{
|
|
|
|
struct kmod_ctx *ctx;
|
2011-12-13 00:41:02 +07:00
|
|
|
const char *null_config = NULL;
|
2011-12-11 07:19:41 +07:00
|
|
|
struct kmod_list *list, *itr;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
if (argc != 1) {
|
|
|
|
fprintf(stderr, "Usage: %s\n", argv[0]);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2011-12-13 00:41:02 +07:00
|
|
|
ctx = kmod_new(NULL, &null_config);
|
2011-12-11 07:19:41 +07:00
|
|
|
if (ctx == NULL) {
|
|
|
|
fputs("Error: kmod_new() failed!\n", stderr);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2011-12-12 22:49:27 +07:00
|
|
|
err = kmod_module_new_from_loaded(ctx, &list);
|
2011-12-11 07:19:41 +07:00
|
|
|
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
|
|
|
|
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;
|
|
|
|
}
|
2011-12-22 12:01:45 +07:00
|
|
|
|
|
|
|
#ifndef KMOD_BUNDLE_TOOL
|
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
return do_lsmod(argc, argv);
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
#include "kmod.h"
|
|
|
|
|
|
|
|
const struct kmod_cmd kmod_cmd_compat_lsmod = {
|
|
|
|
.name = "lsmod",
|
|
|
|
.cmd = do_lsmod,
|
|
|
|
.help = "compat lsmod command",
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|