blob: 2a238cd23bf6609e4742258b68eb408f289361f2 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Erik Andersen6f23cec1999-12-15 22:14:12 +00002/*
3 * Mini lsmod implementation for busybox
4 *
Erik Andersen61677fe2000-04-13 01:18:56 +00005 * Copyright (C) 1999,2000 by Lineo, inc.
Erik Andersen6f23cec1999-12-15 22:14:12 +00006 * Written by Erik Andersen <andersen@lineo.com>, <andersee@debian.org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23
24#include "internal.h"
Eric Andersenbe0dc0d2000-08-21 19:25:16 +000025#include <stdlib.h>
Erik Andersen6f23cec1999-12-15 22:14:12 +000026#include <stdio.h>
Eric Andersenbe0dc0d2000-08-21 19:25:16 +000027#include <stddef.h>
28#include <errno.h>
29#include <unistd.h>
30#include <dirent.h>
31#include <ctype.h>
32#include <assert.h>
33#include <getopt.h>
34#include <sys/utsname.h>
35
36
37
38struct module_info
39{
40 unsigned long addr;
41 unsigned long size;
42 unsigned long flags;
43 long usecount;
44};
45
46
47int query_module(const char *name, int which, void *buf, size_t bufsize,
48 size_t *ret);
49
50/* Values for query_module's which. */
51#define QM_MODULES 1
52#define QM_DEPS 2
53#define QM_REFS 3
54#define QM_SYMBOLS 4
55#define QM_INFO 5
56
Erik Andersen6f23cec1999-12-15 22:14:12 +000057
58
Erik Andersen6f23cec1999-12-15 22:14:12 +000059extern int lsmod_main(int argc, char **argv)
60{
Eric Andersenbe0dc0d2000-08-21 19:25:16 +000061 struct module_info info;
62 char *module_names, *mn, *deps, *dn;
63 size_t bufsize, nmod, count, i, j;
Erik Andersene49d5ec2000-02-08 19:58:47 +000064
Eric Andersenbe0dc0d2000-08-21 19:25:16 +000065 module_names = xmalloc(bufsize = 256);
66 deps = xmalloc(bufsize);
67 if (query_module(NULL, QM_MODULES, module_names, bufsize, &nmod)) {
68 fatalError("QM_MODULES: %s\n", strerror(errno));
69 }
70
71 printf("Module Size Used by\n");
72 for (i = 0, mn = module_names; i < nmod; mn += strlen(mn) + 1, i++) {
73 if (query_module(mn, QM_INFO, &info, sizeof(info), &count)) {
74 if (errno == ENOENT) {
75 /* The module was removed out from underneath us. */
76 continue;
77 }
78 /* else choke */
79 fatalError("module %s: QM_INFO: %s\n", mn, strerror(errno));
80 }
81 while (query_module(mn, QM_REFS, deps, bufsize, &count)) {
82 if (errno == ENOENT) {
83 /* The module was removed out from underneath us. */
84 continue;
85 }
86 if (errno != ENOSPC) {
87 fatalError("module %s: QM_REFS: %s", mn, strerror(errno));
88 }
89 deps = xrealloc(deps, bufsize = count);
90 }
91 printf("%-20s%8lu%4ld ", mn, info.size, info.usecount);
92 if (count) printf("[");
93 for (j = 0, dn = deps; j < count; dn += strlen(dn) + 1, j++) {
94 printf("%s%s", dn, (j==count-1)? "":" ");
95 }
96 if (count) printf("]");
97 printf("\n");
98 }
99
100
101 return( 0);
Erik Andersen6f23cec1999-12-15 22:14:12 +0000102}