Bernhard Fischer pointed out some leftover debris needing cleanup.
[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
16
17 #ifndef CONFIG_FEATURE_CHECK_TAINTED_MODULE
18 static inline void check_tainted(void) { printf("\n"); }
19 #else
20 #define TAINT_FILENAME                  "/proc/sys/kernel/tainted"
21 #define TAINT_PROPRIETORY_MODULE        (1<<0)
22 #define TAINT_FORCED_MODULE             (1<<1)
23 #define TAINT_UNSAFE_SMP                (1<<2)
24
25 static void check_tainted(void)
26 {
27         int tainted;
28         FILE *f;
29
30         tainted = 0;
31         if ((f = fopen(TAINT_FILENAME, "r"))) {
32                 fscanf(f, "%d", &tainted);
33                 fclose(f);
34         }
35         if (f && tainted) {
36                 printf("    Tainted: %c%c%c\n",
37                                 tainted & TAINT_PROPRIETORY_MODULE      ? 'P' : 'G',
38                                 tainted & TAINT_FORCED_MODULE           ? 'F' : ' ',
39                                 tainted & TAINT_UNSAFE_SMP              ? 'S' : ' ');
40         }
41         else {
42                 printf("    Not tainted\n");
43         }
44 }
45 #endif
46
47 #ifdef CONFIG_FEATURE_QUERY_MODULE_INTERFACE
48
49 struct module_info
50 {
51         unsigned long addr;
52         unsigned long size;
53         unsigned long flags;
54         long usecount;
55 };
56
57
58 int query_module(const char *name, int which, void *buf, size_t bufsize, size_t *ret);
59
60 enum {
61 /* Values for query_module's which.  */
62         QM_MODULES = 1,
63         QM_DEPS = 2,
64         QM_REFS = 3,
65         QM_SYMBOLS = 4,
66         QM_INFO = 5,
67
68 /* Bits of module.flags.  */
69         NEW_MOD_RUNNING = 1,
70         NEW_MOD_DELETED = 2,
71         NEW_MOD_AUTOCLEAN = 4,
72         NEW_MOD_VISITED = 8,
73         NEW_MOD_USED_ONCE = 16,
74         NEW_MOD_INITIALIZING = 64
75 };
76
77 int lsmod_main(int argc, char **argv)
78 {
79         struct module_info info;
80         char *module_names, *mn, *deps, *dn;
81         size_t bufsize, depsize, nmod, count, i, j;
82
83         module_names = xmalloc(bufsize = 256);
84         if (my_query_module(NULL, QM_MODULES, &module_names, &bufsize, &nmod)) {
85                 bb_perror_msg_and_die("QM_MODULES");
86         }
87
88         deps = xmalloc(depsize = 256);
89         printf("Module                  Size  Used by");
90         check_tainted();
91
92         for (i = 0, mn = module_names; i < nmod; mn += strlen(mn) + 1, i++) {
93                 if (query_module(mn, QM_INFO, &info, sizeof(info), &count)) {
94                         if (errno == ENOENT) {
95                                 /* The module was removed out from underneath us. */
96                                 continue;
97                         }
98                         /* else choke */
99                         bb_perror_msg_and_die("module %s: QM_INFO", mn);
100                 }
101                 if (my_query_module(mn, QM_REFS, &deps, &depsize, &count)) {
102                         if (errno == ENOENT) {
103                                 /* The module was removed out from underneath us. */
104                                 continue;
105                         }
106                         bb_perror_msg_and_die("module %s: QM_REFS", mn);
107                 }
108                 printf("%-20s%8lu%4ld", mn, info.size, info.usecount);
109                 if (info.flags & NEW_MOD_DELETED)
110                         printf(" (deleted)");
111                 else if (info.flags & NEW_MOD_INITIALIZING)
112                         printf(" (initializing)");
113                 else if (!(info.flags & NEW_MOD_RUNNING))
114                         printf(" (uninitialized)");
115                 else {
116                         if (info.flags & NEW_MOD_AUTOCLEAN)
117                                 printf(" (autoclean) ");
118                         if (!(info.flags & NEW_MOD_USED_ONCE))
119                                 printf(" (unused)");
120                 }
121                 if (count) printf(" [");
122                 for (j = 0, dn = deps; j < count; dn += strlen(dn) + 1, j++) {
123                         printf("%s%s", dn, (j==count-1)? "":" ");
124                 }
125                 if (count) printf("]");
126
127                 printf("\n");
128         }
129
130 #ifdef CONFIG_FEATURE_CLEAN_UP
131         free(module_names);
132 #endif
133
134         return( 0);
135 }
136
137 #else /* CONFIG_FEATURE_QUERY_MODULE_INTERFACE */
138
139 int lsmod_main(int argc, char **argv)
140 {
141         FILE *file = xfopen("/proc/modules", "r");
142
143         printf("Module                  Size  Used by");
144         check_tainted();
145 #if defined(CONFIG_FEATURE_LSMOD_PRETTY_2_6_OUTPUT)
146         {
147           char line[4096];
148
149           while (fgets(line, sizeof(line), file)) {
150             char *tok;
151
152             tok = strtok(line, " \t");
153             printf("%-19s", tok);
154             tok = strtok(NULL, " \t\n");
155             printf(" %8s", tok);
156             tok = strtok(NULL, " \t\n");
157             /* Null if no module unloading support. */
158             if (tok) {
159               printf("  %s", tok);
160               tok = strtok(NULL, "\n");
161               if (!tok)
162                 tok = "";
163               /* New-style has commas, or -.  If so,
164                  truncate (other fields might follow). */
165               else if (strchr(tok, ',')) {
166                 tok = strtok(tok, "\t ");
167                 /* Strip trailing comma. */
168                 if (tok[strlen(tok)-1] == ',')
169                   tok[strlen(tok)-1] = '\0';
170               } else if (tok[0] == '-'
171                          && (tok[1] == '\0' || isspace(tok[1])))
172                 tok = "";
173               printf(" %s", tok);
174             }
175             printf("\n");
176           }
177           fclose(file);
178         }
179 #else
180         xprint_and_close_file(file);
181 #endif  /*  CONFIG_FEATURE_2_6_MODULES  */
182         return EXIT_SUCCESS;
183 }
184
185 #endif /* CONFIG_FEATURE_QUERY_MODULE_INTERFACE */