1020158ed07809b439275877cc68be7169e8792e
[oweals/busybox.git] / modutils / lsmod.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini lsmod implementation for busybox
4  *
5  * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6  *
7  * Modified by Alcove, Julien Gaulmin <julien.gaulmin@alcove.fr> and
8  * Nicolas Ferre <nicolas.ferre@alcove.fr> to support pre 2.1 kernels
9  * (which lack the query_module() interface).
10  *
11  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
12  */
13
14 #include "busybox.h"
15 #include <stdlib.h>
16 #include <stdio.h>
17 #include <string.h>
18 #include <stddef.h>
19 #include <errno.h>
20 #include <unistd.h>
21 #include <dirent.h>
22 #include <ctype.h>
23 #include <assert.h>
24 #include <getopt.h>
25 #include <sys/utsname.h>
26 #include <sys/file.h>
27
28
29 #ifndef CONFIG_FEATURE_CHECK_TAINTED_MODULE
30 static inline void check_tainted(void) { printf("\n"); }
31 #else
32 #define TAINT_FILENAME                  "/proc/sys/kernel/tainted"
33 #define TAINT_PROPRIETORY_MODULE        (1<<0)
34 #define TAINT_FORCED_MODULE             (1<<1)
35 #define TAINT_UNSAFE_SMP                (1<<2)
36
37 static void check_tainted(void)
38 {
39         int tainted;
40         FILE *f;
41
42         tainted = 0;
43         if ((f = fopen(TAINT_FILENAME, "r"))) {
44                 fscanf(f, "%d", &tainted);
45                 fclose(f);
46         }
47         if (f && tainted) {
48                 printf("    Tainted: %c%c%c\n",
49                                 tainted & TAINT_PROPRIETORY_MODULE      ? 'P' : 'G',
50                                 tainted & TAINT_FORCED_MODULE           ? 'F' : ' ',
51                                 tainted & TAINT_UNSAFE_SMP              ? 'S' : ' ');
52         }
53         else {
54                 printf("    Not tainted\n");
55         }
56 }
57 #endif
58
59 #ifdef CONFIG_FEATURE_QUERY_MODULE_INTERFACE
60
61 struct module_info
62 {
63         unsigned long addr;
64         unsigned long size;
65         unsigned long flags;
66         long usecount;
67 };
68
69
70 int query_module(const char *name, int which, void *buf, size_t bufsize, size_t *ret);
71
72 enum {
73 /* Values for query_module's which.  */
74         QM_MODULES = 1,
75         QM_DEPS = 2,
76         QM_REFS = 3,
77         QM_SYMBOLS = 4,
78         QM_INFO = 5,
79
80 /* Bits of module.flags.  */
81         NEW_MOD_RUNNING = 1,
82         NEW_MOD_DELETED = 2,
83         NEW_MOD_AUTOCLEAN = 4,
84         NEW_MOD_VISITED = 8,
85         NEW_MOD_USED_ONCE = 16,
86         NEW_MOD_INITIALIZING = 64
87 };
88
89 int lsmod_main(int argc, char **argv)
90 {
91         struct module_info info;
92         char *module_names, *mn, *deps, *dn;
93         size_t bufsize, depsize, nmod, count, i, j;
94
95         module_names = xmalloc(bufsize = 256);
96         if (my_query_module(NULL, QM_MODULES, (void **)&module_names, &bufsize,
97                                 &nmod)) {
98                 bb_perror_msg_and_die("QM_MODULES");
99         }
100
101         deps = xmalloc(depsize = 256);
102         printf("Module                  Size  Used by");
103         check_tainted();
104
105         for (i = 0, mn = module_names; i < nmod; mn += strlen(mn) + 1, i++) {
106                 if (query_module(mn, QM_INFO, &info, sizeof(info), &count)) {
107                         if (errno == ENOENT) {
108                                 /* The module was removed out from underneath us. */
109                                 continue;
110                         }
111                         /* else choke */
112                         bb_perror_msg_and_die("module %s: QM_INFO", mn);
113                 }
114                 if (my_query_module(mn, QM_REFS, (void **)&deps, &depsize, &count)) {
115                         if (errno == ENOENT) {
116                                 /* The module was removed out from underneath us. */
117                                 continue;
118                         }
119                         bb_perror_msg_and_die("module %s: QM_REFS", mn);
120                 }
121                 printf("%-20s%8lu%4ld", mn, info.size, info.usecount);
122                 if (info.flags & NEW_MOD_DELETED)
123                         printf(" (deleted)");
124                 else if (info.flags & NEW_MOD_INITIALIZING)
125                         printf(" (initializing)");
126                 else if (!(info.flags & NEW_MOD_RUNNING))
127                         printf(" (uninitialized)");
128                 else {
129                         if (info.flags & NEW_MOD_AUTOCLEAN)
130                                 printf(" (autoclean) ");
131                         if (!(info.flags & NEW_MOD_USED_ONCE))
132                                 printf(" (unused)");
133                 }
134                 if (count) printf(" [");
135                 for (j = 0, dn = deps; j < count; dn += strlen(dn) + 1, j++) {
136                         printf("%s%s", dn, (j==count-1)? "":" ");
137                 }
138                 if (count) printf("]");
139
140                 printf("\n");
141         }
142
143 #ifdef CONFIG_FEATURE_CLEAN_UP
144         free(module_names);
145 #endif
146
147         return( 0);
148 }
149
150 #else /* CONFIG_FEATURE_QUERY_MODULE_INTERFACE */
151
152 int lsmod_main(int argc, char **argv)
153 {
154         printf("Module                  Size  Used by");
155         check_tainted();
156 #if defined(CONFIG_FEATURE_LSMOD_PRETTY_2_6_OUTPUT)
157         {
158           FILE *file;
159           char line[4096];
160
161           file = bb_xfopen("/proc/modules", "r");
162
163           while (fgets(line, sizeof(line), file)) {
164             char *tok;
165
166             tok = strtok(line, " \t");
167             printf("%-19s", tok);
168             tok = strtok(NULL, " \t\n");
169             printf(" %8s", tok);
170             tok = strtok(NULL, " \t\n");
171             /* Null if no module unloading support. */
172             if (tok) {
173               printf("  %s", tok);
174               tok = strtok(NULL, "\n");
175               if (!tok)
176                 tok = "";
177               /* New-style has commas, or -.  If so,
178                  truncate (other fields might follow). */
179               else if (strchr(tok, ',')) {
180                 tok = strtok(tok, "\t ");
181                 /* Strip trailing comma. */
182                 if (tok[strlen(tok)-1] == ',')
183                   tok[strlen(tok)-1] = '\0';
184               } else if (tok[0] == '-'
185                          && (tok[1] == '\0' || isspace(tok[1])))
186                 tok = "";
187               printf(" %s", tok);
188             }
189             printf("\n");
190           }
191           fclose(file);
192         }
193         return EXIT_SUCCESS;
194 #else
195         if (bb_xprint_file_by_name("/proc/modules") == 0)
196                 return EXIT_SUCCESS;
197 #endif  /*  CONFIG_FEATURE_2_6_MODULES  */
198
199         return EXIT_FAILURE;
200 }
201
202 #endif /* CONFIG_FEATURE_QUERY_MODULE_INTERFACE */