modprobe: rework/speedup by Timo Teras (timo.teras AT iki.fi)
[oweals/busybox.git] / modutils / modprobe.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Modprobe written from scratch for BusyBox
4  *
5  * Copyright (c) 2008 Timo Teras <timo.teras@iki.fi>
6  * Copyright (c) 2008 Vladimir Dronnikov
7  *
8  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
9  */
10
11 #include "libbb.h"
12 #include "modutils.h"
13 #include <sys/utsname.h>
14 #include <fnmatch.h>
15
16 #define MODULE_FLAG_LOADED              0x0001
17 #define MODULE_FLAG_NEED_DEPS           0x0002
18 //Misnomer? Seems to mean "was seen in modules.dep":
19 #define MODULE_FLAG_EXISTS              0x0004
20 #define MODULE_FLAG_BLACKLISTED         0x0008
21
22 struct module_entry { /* I'll call it ME. */
23         unsigned flags;
24         char *modname; /* stripped of /path/, .ext and s/-/_/g */
25         const char *probed_name; /* verbatim as seen on cmdline */
26         llist_t *aliases; /* strings. aliases from config files */
27         llist_t *options; /* strings. options from config files */
28         llist_t *deps; /* strings. modules we depend on */
29 };
30
31 #define MODPROBE_OPTS  "acdlnrt:VC:" USE_FEATURE_MODPROBE_BLACKLIST("b")
32 enum {
33         MODPROBE_OPT_INSERT_ALL = (INSMOD_OPT_UNUSED << 0), /* a */
34         MODPROBE_OPT_DUMP_ONLY  = (INSMOD_OPT_UNUSED << 1), /* c */
35         MODPROBE_OPT_D          = (INSMOD_OPT_UNUSED << 2), /* d */
36         MODPROBE_OPT_LIST_ONLY  = (INSMOD_OPT_UNUSED << 3), /* l */
37         MODPROBE_OPT_SHOW_ONLY  = (INSMOD_OPT_UNUSED << 4), /* n */
38         MODPROBE_OPT_REMOVE     = (INSMOD_OPT_UNUSED << 5), /* r */
39         MODPROBE_OPT_RESTRICT   = (INSMOD_OPT_UNUSED << 6), /* t */
40         MODPROBE_OPT_VERONLY    = (INSMOD_OPT_UNUSED << 7), /* V */
41         MODPROBE_OPT_CONFIGFILE = (INSMOD_OPT_UNUSED << 8), /* C */
42         MODPROBE_OPT_BLACKLIST  = (INSMOD_OPT_UNUSED << 9) * ENABLE_FEATURE_MODPROBE_BLACKLIST,
43 };
44
45 struct globals {
46         llist_t *db; /* MEs of all modules ever seen (caching for speed) */
47         llist_t *probes; /* MEs of module(s) requested on cmdline */
48         llist_t *cmdline_mopts; /* strings. module options (from cmdline) */
49         int num_deps; /* what is this? */
50         /* bool. "Did we have 'symbol:FOO' requested on cmdline?" */
51         smallint need_symbols;
52 };
53 #define G (*(struct globals*)&bb_common_bufsiz1)
54 #define INIT_G() do { } while (0)
55
56
57 static int read_config(const char *path);
58
59 static struct module_entry *helper_get_module(const char *module, int create)
60 {
61         char modname[MODULE_NAME_LEN];
62         struct module_entry *e;
63         llist_t *l;
64
65         filename2modname(module, modname);
66         for (l = G.db; l != NULL; l = l->link) {
67                 e = (struct module_entry *) l->data;
68                 if (strcmp(e->modname, modname) == 0)
69                         return e;
70         }
71         if (!create)
72                 return NULL;
73
74         e = xzalloc(sizeof(*e));
75         e->modname = xstrdup(modname);
76         llist_add_to(&G.db, e);
77
78         return e;
79 }
80 static struct module_entry *get_or_add_modentry(const char *module)
81 {
82         return helper_get_module(module, 1);
83 }
84 static struct module_entry *get_modentry(const char *module)
85 {
86         return helper_get_module(module, 0);
87 }
88
89 static void add_probe(const char *name)
90 {
91         struct module_entry *m;
92
93         m = get_or_add_modentry(name);
94         m->probed_name = name;
95         m->flags |= MODULE_FLAG_NEED_DEPS;
96         llist_add_to(&G.probes, m);
97
98         G.num_deps++;
99         if (ENABLE_FEATURE_MODUTILS_SYMBOLS
100          && strncmp(m->modname, "symbol:", 7) == 0)
101                 G.need_symbols = 1;
102 }
103
104 static int FAST_FUNC config_file_action(const char *filename,
105                                         struct stat *statbuf UNUSED_PARAM,
106                                         void *userdata UNUSED_PARAM,
107                                         int depth UNUSED_PARAM)
108 {
109         RESERVE_CONFIG_BUFFER(modname, MODULE_NAME_LEN);
110         char *tokens[3], *rmod;
111         parser_t *p;
112         llist_t *l;
113         struct module_entry *m;
114         int rc = TRUE;
115
116         if (bb_basename(filename)[0] == '.')
117                 goto error;
118
119         p = config_open2(filename, fopen_for_read);
120         if (p == NULL) {
121                 rc = FALSE;
122                 goto error;
123         }
124
125         while (config_read(p, tokens, 3, 2, "# \t", PARSE_NORMAL)) {
126 //Use index_in_strings?
127                 if (strcmp(tokens[0], "alias") == 0) {
128                         filename2modname(tokens[1], modname);
129                         if (tokens[2] == NULL)
130                                 continue;
131
132                         for (l = G.probes; l != NULL; l = l->link) {
133                                 m = (struct module_entry *) l->data;
134                                 if (fnmatch(modname, m->modname, 0) != 0)
135                                         continue;
136                                 rmod = filename2modname(tokens[2], NULL);
137                                 llist_add_to(&m->aliases, rmod);
138
139                                 if (m->flags & MODULE_FLAG_NEED_DEPS) {
140                                         m->flags &= ~MODULE_FLAG_NEED_DEPS;
141                                         G.num_deps--;
142                                 }
143
144                                 m = get_or_add_modentry(rmod);
145                                 m->flags |= MODULE_FLAG_NEED_DEPS;
146                                 G.num_deps++;
147                         }
148                 } else if (strcmp(tokens[0], "options") == 0) {
149                         if (tokens[2] == NULL)
150                                 continue;
151                         m = get_or_add_modentry(tokens[1]);
152                         llist_add_to(&m->options, xstrdup(tokens[2]));
153                 } else if (strcmp(tokens[0], "include") == 0) {
154                         read_config(tokens[1]);
155                 } else if (ENABLE_FEATURE_MODPROBE_BLACKLIST
156                  && strcmp(tokens[0], "blacklist") == 0
157                 ) {
158                         get_or_add_modentry(tokens[1])->flags |= MODULE_FLAG_BLACKLISTED;
159                 }
160         }
161         config_close(p);
162 error:
163         RELEASE_CONFIG_BUFFER(modname);
164         return rc;
165 }
166
167 static int read_config(const char *path)
168 {
169         return recursive_action(path, ACTION_RECURSE | ACTION_QUIET,
170                                 config_file_action, NULL, NULL, 1);
171 }
172
173 static char *gather_options(char *opts, llist_t *o)
174 {
175         int optlen;
176
177         if (opts == NULL)
178                 opts = xstrdup("");
179         optlen = strlen(opts);
180
181         for (; o != NULL; o = o->link) {
182                 opts = xrealloc(opts, optlen + strlen(o->data) + 2);
183                 optlen += sprintf(opts + optlen, "%s ", o->data);
184         }
185         return opts;
186 }
187
188 static int do_modprobe(struct module_entry *m)
189 {
190         struct module_entry *m2;
191         char *fn, *options;
192         int rc = -1;
193
194         if (!(m->flags & MODULE_FLAG_EXISTS))
195                 return -ENOENT;
196
197         if (!(option_mask32 & MODPROBE_OPT_REMOVE))
198                 m->deps = llist_rev(m->deps);
199
200         rc = 0;
201         while (m->deps && rc == 0) {
202                 fn = llist_pop(&m->deps);
203                 m2 = get_or_add_modentry(fn);
204                 if (option_mask32 & MODPROBE_OPT_REMOVE) {
205                         if (bb_delete_module(m->modname, O_EXCL) != 0)
206                                 rc = errno;
207                 } else if (!(m2->flags & MODULE_FLAG_LOADED)) {
208                         options = gather_options(NULL, m2->options);
209                         if (m == m2)
210                                 options = gather_options(options, G.cmdline_mopts);
211 //TODO: looks like G.cmdline_mopts can contain either NULL or just one string, not more?
212                         rc = bb_init_module(fn, options);
213                         if (rc == 0)
214                                 m2->flags |= MODULE_FLAG_LOADED;
215                         free(options);
216                 }
217
218                 free(fn);
219         }
220
221         if (rc > 0 && !(option_mask32 & INSMOD_OPT_SILENT)) {
222                 bb_error_msg("failed to %sload module %s: %s",
223                         (option_mask32 & MODPROBE_OPT_REMOVE) ? "un" : "",
224                         m->probed_name ? m->probed_name : m->modname,
225                         moderror(rc)
226                 );
227         }
228
229         return rc;
230 }
231
232 static void load_modules_dep(void)
233 {
234         struct module_entry *m;
235         char *colon, *tokens[2];
236         parser_t *p;
237
238         p = config_open2(CONFIG_DEFAULT_DEPMOD_FILE, xfopen_for_read);
239 //Still true?
240         /* Modprobe does not work at all without modprobe.dep,
241          * even if the full module name is given. Returning error here
242          * was making us later confuse user with this message:
243          * "module /full/path/to/existing/file/module.ko not found".
244          * It's better to die immediately, with good message: */
245 //      if (p == NULL)
246 //              bb_perror_msg_and_die("can't open '%s'", CONFIG_DEFAULT_DEPMOD_FILE);
247
248         while (G.num_deps
249          && config_read(p, tokens, 2, 1, "# \t", PARSE_NORMAL)
250         ) {
251                 colon = last_char_is(tokens[0], ':');
252                 if (colon == NULL)
253                         continue;
254                 *colon = 0;
255
256                 m = get_modentry(tokens[0]);
257                 if (m == NULL)
258                         continue;
259 //Can we skip it too if it is MODULE_FLAG_LOADED?
260
261                 m->flags |= MODULE_FLAG_EXISTS;
262                 if ((m->flags & MODULE_FLAG_NEED_DEPS) && (m->deps == NULL)) {
263                         G.num_deps--;
264                         llist_add_to(&m->deps, xstrdup(tokens[0]));
265                         if (tokens[1])
266                                 string_to_llist(tokens[1], &m->deps, " ");
267                 }
268         }
269         config_close(p);
270 }
271
272 int modprobe_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
273 int modprobe_main(int argc UNUSED_PARAM, char **argv)
274 {
275         struct utsname uts;
276         int rc;
277         unsigned opt;
278         struct module_entry *me;
279
280         opt_complementary = "q-v:v-q";
281         opt = getopt32(argv, INSMOD_OPTS MODPROBE_OPTS INSMOD_ARGS, NULL, NULL);
282         argv += optind;
283
284         if (opt & (MODPROBE_OPT_DUMP_ONLY | MODPROBE_OPT_LIST_ONLY |
285                                 MODPROBE_OPT_SHOW_ONLY))
286                 bb_error_msg_and_die("not supported");
287
288         /* Goto modules location */
289         xchdir(CONFIG_DEFAULT_MODULES_DIR);
290         uname(&uts);
291         xchdir(uts.release);
292
293         if (!argv[0]) {
294                 if (opt & MODPROBE_OPT_REMOVE) {
295                         /* TODO: comment here */
296                         if (bb_delete_module(NULL, O_NONBLOCK|O_EXCL) != 0)
297                                 bb_perror_msg_and_die("rmmod");
298                 }
299                 return EXIT_SUCCESS;
300         }
301
302         if (opt & MODPROBE_OPT_INSERT_ALL) {
303                 /* Each argument is a module name */
304                 do {
305                         add_probe(*argv++);
306                 } while (*argv);
307                 G.probes = llist_rev(G.probes);
308         } else {
309                 /* First argument is module name, rest are parameters */
310                 add_probe(argv[0]);
311 //TODO: looks like G.cmdline_mopts can contain either NULL or just one string, not more?
312 //optimize it them
313                 llist_add_to(&G.cmdline_mopts, parse_cmdline_module_options(argv));
314         }
315
316         /* Retrieve module names of already loaded modules */
317         {
318                 char *s;
319                 parser_t *parser = config_open2("/proc/modules", fopen_for_read);
320                 while (config_read(parser, &s, 1, 1, "# \t", PARSE_NORMAL & ~PARSE_GREEDY))
321                         get_or_add_modentry(s)->flags |= MODULE_FLAG_LOADED;
322                 config_close(parser);
323         }
324
325         read_config("/etc/modprobe.conf");
326         read_config("/etc/modprobe.d");
327         if (ENABLE_FEATURE_MODUTILS_SYMBOLS && G.need_symbols)
328                 read_config("modules.symbols");
329         load_modules_dep();
330         if (ENABLE_FEATURE_MODUTILS_ALIAS && G.num_deps) {
331                 read_config("modules.alias");
332                 load_modules_dep();
333         }
334
335         while ((me = llist_pop(&G.probes)) != NULL) {
336                 if (me->aliases == NULL) {
337                         /* Try if module by literal name is found; literal
338                          * names are blacklisted only if '-b' is given. */
339                         if (!(opt & MODPROBE_OPT_BLACKLIST)
340                          || !(me->flags & MODULE_FLAG_BLACKLISTED)
341                         ) {
342                                 rc = do_modprobe(me);
343                                 if (rc < 0 && !(opt & INSMOD_OPT_SILENT))
344                                         bb_error_msg("module %s not found",
345                                                      me->probed_name);
346                         }
347                 } else {
348                         /* Probe all aliases */
349                         do {
350                                 char *realname = llist_pop(&me->aliases);
351                                 struct module_entry *m2;
352
353                                 m2 = get_or_add_modentry(realname);
354                                 if (!(m2->flags & MODULE_FLAG_BLACKLISTED))
355                                         do_modprobe(m2);
356                                 free(realname);
357                         } while (me->aliases != NULL);
358                 }
359         }
360
361         return EXIT_SUCCESS;
362 }