From: Eric Andersen Date: Mon, 21 Aug 2000 19:25:16 +0000 (-0000) Subject: lsmod now uses the query_module syscall, rather then /proc X-Git-Tag: 0_47~160 X-Git-Url: https://git.librecmc.org/?a=commitdiff_plain;h=be0dc0d06a730bd3a9e7567af6918a673d5135ee;p=oweals%2Fbusybox.git lsmod now uses the query_module syscall, rather then /proc -Erik --- diff --git a/Changelog b/Changelog index 62db63bd2..d7adeba42 100644 --- a/Changelog +++ b/Changelog @@ -11,6 +11,7 @@ cleaner too, thanks to Allen Soard * Add optional ls file sorting, thanks to a patch from Sterling Huxley + * lsmod now uses the query_module syscall, rather then /proc -Erik Andersen diff --git a/lsmod.c b/lsmod.c index 6eb9c56b8..2a238cd23 100644 --- a/lsmod.c +++ b/lsmod.c @@ -22,19 +22,81 @@ */ #include "internal.h" +#include #include +#include +#include +#include +#include +#include +#include +#include +#include + + + +struct module_info +{ + unsigned long addr; + unsigned long size; + unsigned long flags; + long usecount; +}; + + +int query_module(const char *name, int which, void *buf, size_t bufsize, + size_t *ret); + +/* Values for query_module's which. */ +#define QM_MODULES 1 +#define QM_DEPS 2 +#define QM_REFS 3 +#define QM_SYMBOLS 4 +#define QM_INFO 5 + extern int lsmod_main(int argc, char **argv) { -#if defined BB_FEATURE_USE_DEVPS_PATCH - char *filename = "/dev/modules"; -#else -#if ! defined BB_FEATURE_USE_PROCFS -#error Sorry, I depend on the /proc filesystem right now. -#endif - char *filename = "/proc/modules"; -#endif - - return(print_file_by_name(filename)); + struct module_info info; + char *module_names, *mn, *deps, *dn; + size_t bufsize, nmod, count, i, j; + + module_names = xmalloc(bufsize = 256); + deps = xmalloc(bufsize); + if (query_module(NULL, QM_MODULES, module_names, bufsize, &nmod)) { + fatalError("QM_MODULES: %s\n", strerror(errno)); + } + + printf("Module Size Used by\n"); + for (i = 0, mn = module_names; i < nmod; mn += strlen(mn) + 1, i++) { + if (query_module(mn, QM_INFO, &info, sizeof(info), &count)) { + if (errno == ENOENT) { + /* The module was removed out from underneath us. */ + continue; + } + /* else choke */ + fatalError("module %s: QM_INFO: %s\n", mn, strerror(errno)); + } + while (query_module(mn, QM_REFS, deps, bufsize, &count)) { + if (errno == ENOENT) { + /* The module was removed out from underneath us. */ + continue; + } + if (errno != ENOSPC) { + fatalError("module %s: QM_REFS: %s", mn, strerror(errno)); + } + deps = xrealloc(deps, bufsize = count); + } + printf("%-20s%8lu%4ld ", mn, info.size, info.usecount); + if (count) printf("["); + for (j = 0, dn = deps; j < count; dn += strlen(dn) + 1, j++) { + printf("%s%s", dn, (j==count-1)? "":" "); + } + if (count) printf("]"); + printf("\n"); + } + + + return( 0); } diff --git a/modutils/lsmod.c b/modutils/lsmod.c index 6eb9c56b8..2a238cd23 100644 --- a/modutils/lsmod.c +++ b/modutils/lsmod.c @@ -22,19 +22,81 @@ */ #include "internal.h" +#include #include +#include +#include +#include +#include +#include +#include +#include +#include + + + +struct module_info +{ + unsigned long addr; + unsigned long size; + unsigned long flags; + long usecount; +}; + + +int query_module(const char *name, int which, void *buf, size_t bufsize, + size_t *ret); + +/* Values for query_module's which. */ +#define QM_MODULES 1 +#define QM_DEPS 2 +#define QM_REFS 3 +#define QM_SYMBOLS 4 +#define QM_INFO 5 + extern int lsmod_main(int argc, char **argv) { -#if defined BB_FEATURE_USE_DEVPS_PATCH - char *filename = "/dev/modules"; -#else -#if ! defined BB_FEATURE_USE_PROCFS -#error Sorry, I depend on the /proc filesystem right now. -#endif - char *filename = "/proc/modules"; -#endif - - return(print_file_by_name(filename)); + struct module_info info; + char *module_names, *mn, *deps, *dn; + size_t bufsize, nmod, count, i, j; + + module_names = xmalloc(bufsize = 256); + deps = xmalloc(bufsize); + if (query_module(NULL, QM_MODULES, module_names, bufsize, &nmod)) { + fatalError("QM_MODULES: %s\n", strerror(errno)); + } + + printf("Module Size Used by\n"); + for (i = 0, mn = module_names; i < nmod; mn += strlen(mn) + 1, i++) { + if (query_module(mn, QM_INFO, &info, sizeof(info), &count)) { + if (errno == ENOENT) { + /* The module was removed out from underneath us. */ + continue; + } + /* else choke */ + fatalError("module %s: QM_INFO: %s\n", mn, strerror(errno)); + } + while (query_module(mn, QM_REFS, deps, bufsize, &count)) { + if (errno == ENOENT) { + /* The module was removed out from underneath us. */ + continue; + } + if (errno != ENOSPC) { + fatalError("module %s: QM_REFS: %s", mn, strerror(errno)); + } + deps = xrealloc(deps, bufsize = count); + } + printf("%-20s%8lu%4ld ", mn, info.size, info.usecount); + if (count) printf("["); + for (j = 0, dn = deps; j < count; dn += strlen(dn) + 1, j++) { + printf("%s%s", dn, (j==count-1)? "":" "); + } + if (count) printf("]"); + printf("\n"); + } + + + return( 0); }