source: MondoRescue/branches/2.2.9/mindi-busybox/modutils/modprobe.c@ 3320

Last change on this file since 3320 was 3320, checked in by Bruno Cornec, 9 years ago
  • Re-add (thanks git BTW) the 2.2.9 branch which had been destroyed in the move to 3.0
  • Property svn:eol-style set to native
File size: 17.9 KB
Line 
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 source tree.
9 */
10
11//applet:IF_MODPROBE(APPLET(modprobe, _BB_DIR_SBIN, _BB_SUID_DROP))
12
13//usage:#if !ENABLE_MODPROBE_SMALL
14//usage:#define modprobe_notes_usage
15//usage: "modprobe can (un)load a stack of modules, passing each module options (when\n"
16//usage: "loading). modprobe uses a configuration file to determine what option(s) to\n"
17//usage: "pass each module it loads.\n"
18//usage: "\n"
19//usage: "The configuration file is searched (in this order):\n"
20//usage: "\n"
21//usage: " /etc/modprobe.conf (2.6 only)\n"
22//usage: " /etc/modules.conf\n"
23//usage: " /etc/conf.modules (deprecated)\n"
24//usage: "\n"
25//usage: "They all have the same syntax (see below). If none is present, it is\n"
26//usage: "_not_ an error; each loaded module is then expected to load without\n"
27//usage: "options. Once a file is found, the others are tested for.\n"
28//usage: "\n"
29//usage: "/etc/modules.conf entry format:\n"
30//usage: "\n"
31//usage: " alias <alias_name> <mod_name>\n"
32//usage: " Makes it possible to modprobe alias_name, when there is no such module.\n"
33//usage: " It makes sense if your mod_name is long, or you want a more representative\n"
34//usage: " name for that module (eg. 'scsi' in place of 'aha7xxx').\n"
35//usage: " This makes it also possible to use a different set of options (below) for\n"
36//usage: " the module and the alias.\n"
37//usage: " A module can be aliased more than once.\n"
38//usage: "\n"
39//usage: " options <mod_name|alias_name> <symbol=value...>\n"
40//usage: " When loading module mod_name (or the module aliased by alias_name), pass\n"
41//usage: " the \"symbol=value\" pairs as option to that module.\n"
42//usage: "\n"
43//usage: "Sample /etc/modules.conf file:\n"
44//usage: "\n"
45//usage: " options tulip irq=3\n"
46//usage: " alias tulip tulip2\n"
47//usage: " options tulip2 irq=4 io=0x308\n"
48//usage: "\n"
49//usage: "Other functionality offered by 'classic' modprobe is not available in\n"
50//usage: "this implementation.\n"
51//usage: "\n"
52//usage: "If module options are present both in the config file, and on the command line,\n"
53//usage: "then the options from the command line will be passed to the module _after_\n"
54//usage: "the options from the config file. That way, you can have defaults in the config\n"
55//usage: "file, and override them for a specific usage from the command line.\n"
56//usage:#define modprobe_example_usage
57//usage: "(with the above /etc/modules.conf):\n\n"
58//usage: "$ modprobe tulip\n"
59//usage: " will load the module 'tulip' with default option 'irq=3'\n\n"
60//usage: "$ modprobe tulip irq=5\n"
61//usage: " will load the module 'tulip' with option 'irq=5', thus overriding the default\n\n"
62//usage: "$ modprobe tulip2\n"
63//usage: " will load the module 'tulip' with default options 'irq=4 io=0x308',\n"
64//usage: " which are the default for alias 'tulip2'\n\n"
65//usage: "$ modprobe tulip2 irq=8\n"
66//usage: " will load the module 'tulip' with default options 'irq=4 io=0x308 irq=8',\n"
67//usage: " which are the default for alias 'tulip2' overridden by the option 'irq=8'\n\n"
68//usage: " from the command line\n\n"
69//usage: "$ modprobe tulip2 irq=2 io=0x210\n"
70//usage: " will load the module 'tulip' with default options 'irq=4 io=0x308 irq=4 io=0x210',\n"
71//usage: " which are the default for alias 'tulip2' overridden by the options 'irq=2 io=0x210'\n\n"
72//usage: " from the command line\n"
73//usage:
74//usage:#define modprobe_trivial_usage
75//usage: "[-alrqvs"
76//usage: IF_FEATURE_MODPROBE_BLACKLIST("b")
77//usage: "] MODULE [symbol=value]..."
78//usage:#define modprobe_full_usage "\n\n"
79//usage: "Options:"
80//usage: "\n -a Load multiple MODULEs"
81//usage: "\n -l List (MODULE is a pattern)"
82//usage: "\n -r Remove MODULE (stacks) or do autoclean"
83//usage: "\n -q Quiet"
84//usage: "\n -v Verbose"
85//usage: "\n -s Log to syslog"
86//usage: IF_FEATURE_MODPROBE_BLACKLIST(
87//usage: "\n -b Apply blacklist to module names too"
88//usage: )
89//usage:#endif /* !ENABLE_MODPROBE_SMALL */
90
91#include "libbb.h"
92#include "modutils.h"
93#include <sys/utsname.h>
94#include <fnmatch.h>
95
96//#define DBG(fmt, ...) bb_error_msg("%s: " fmt, __func__, ## __VA_ARGS__)
97#define DBG(...) ((void)0)
98
99/* Note that unlike older versions of modules.dep/depmod (busybox and m-i-t),
100 * we expect the full dependency list to be specified in modules.dep.
101 * Older versions would only export the direct dependency list.
102 */
103
104/* Note that usage text doesn't document various 2.4 options
105 * we pull in through INSMOD_OPTS define */
106
107#define MODPROBE_COMPLEMENTARY "q-v:v-q:l--ar:a--lr:r--al"
108#define MODPROBE_OPTS "alr" IF_FEATURE_MODPROBE_BLACKLIST("b")
109//#define MODPROBE_COMPLEMENTARY "q-v:v-q:l--acr:a--lr:r--al"
110//#define MODPROBE_OPTS "acd:lnrt:C:" IF_FEATURE_MODPROBE_BLACKLIST("b")
111enum {
112 MODPROBE_OPT_INSERT_ALL = (INSMOD_OPT_UNUSED << 0), /* a */
113 //MODPROBE_OPT_DUMP_ONLY= (INSMOD_OPT_UNUSED << x), /* c */
114 //MODPROBE_OPT_DIRNAME = (INSMOD_OPT_UNUSED << x), /* d */
115 MODPROBE_OPT_LIST_ONLY = (INSMOD_OPT_UNUSED << 1), /* l */
116 //MODPROBE_OPT_SHOW_ONLY= (INSMOD_OPT_UNUSED << x), /* n */
117 MODPROBE_OPT_REMOVE = (INSMOD_OPT_UNUSED << 2), /* r */
118 //MODPROBE_OPT_RESTRICT = (INSMOD_OPT_UNUSED << x), /* t */
119 //MODPROBE_OPT_VERONLY = (INSMOD_OPT_UNUSED << x), /* V */
120 //MODPROBE_OPT_CONFIGFILE=(INSMOD_OPT_UNUSED << x), /* C */
121 MODPROBE_OPT_BLACKLIST = (INSMOD_OPT_UNUSED << 3) * ENABLE_FEATURE_MODPROBE_BLACKLIST,
122};
123
124#define MODULE_FLAG_LOADED 0x0001
125#define MODULE_FLAG_NEED_DEPS 0x0002
126/* "was seen in modules.dep": */
127#define MODULE_FLAG_FOUND_IN_MODDEP 0x0004
128#define MODULE_FLAG_BLACKLISTED 0x0008
129
130struct module_entry { /* I'll call it ME. */
131 unsigned flags;
132 char *modname; /* stripped of /path/, .ext and s/-/_/g */
133 const char *probed_name; /* verbatim as seen on cmdline */
134 char *options; /* options from config files */
135 llist_t *realnames; /* strings. if this module is an alias, */
136 /* real module name is one of these. */
137//Can there really be more than one? Example from real kernel?
138 llist_t *deps; /* strings. modules we depend on */
139};
140
141struct globals {
142 llist_t *db; /* MEs of all modules ever seen (caching for speed) */
143 llist_t *probes; /* MEs of module(s) requested on cmdline */
144 char *cmdline_mopts; /* module options from cmdline */
145 int num_unresolved_deps;
146 /* bool. "Did we have 'symbol:FOO' requested on cmdline?" */
147 smallint need_symbols;
148} FIX_ALIASING;
149#define G (*(struct globals*)&bb_common_bufsiz1)
150#define INIT_G() do { } while (0)
151
152
153static int read_config(const char *path);
154
155static char *gather_options_str(char *opts, const char *append)
156{
157 /* Speed-optimized. We call gather_options_str many times. */
158 if (append) {
159 if (opts == NULL) {
160 opts = xstrdup(append);
161 } else {
162 int optlen = strlen(opts);
163 opts = xrealloc(opts, optlen + strlen(append) + 2);
164 sprintf(opts + optlen, " %s", append);
165 }
166 }
167 return opts;
168}
169
170static struct module_entry *helper_get_module(const char *module, int create)
171{
172 char modname[MODULE_NAME_LEN];
173 struct module_entry *e;
174 llist_t *l;
175
176 filename2modname(module, modname);
177 for (l = G.db; l != NULL; l = l->link) {
178 e = (struct module_entry *) l->data;
179 if (strcmp(e->modname, modname) == 0)
180 return e;
181 }
182 if (!create)
183 return NULL;
184
185 e = xzalloc(sizeof(*e));
186 e->modname = xstrdup(modname);
187 llist_add_to(&G.db, e);
188
189 return e;
190}
191static struct module_entry *get_or_add_modentry(const char *module)
192{
193 return helper_get_module(module, 1);
194}
195static struct module_entry *get_modentry(const char *module)
196{
197 return helper_get_module(module, 0);
198}
199
200static void add_probe(const char *name)
201{
202 struct module_entry *m;
203
204 m = get_or_add_modentry(name);
205 if (!(option_mask32 & MODPROBE_OPT_REMOVE)
206 && (m->flags & MODULE_FLAG_LOADED)
207 ) {
208 DBG("skipping %s, it is already loaded", name);
209 return;
210 }
211
212 DBG("queuing %s", name);
213 m->probed_name = name;
214 m->flags |= MODULE_FLAG_NEED_DEPS;
215 llist_add_to_end(&G.probes, m);
216 G.num_unresolved_deps++;
217 if (ENABLE_FEATURE_MODUTILS_SYMBOLS
218 && strncmp(m->modname, "symbol:", 7) == 0
219 ) {
220 G.need_symbols = 1;
221 }
222}
223
224static int FAST_FUNC config_file_action(const char *filename,
225 struct stat *statbuf UNUSED_PARAM,
226 void *userdata UNUSED_PARAM,
227 int depth UNUSED_PARAM)
228{
229 char *tokens[3];
230 parser_t *p;
231 struct module_entry *m;
232 int rc = TRUE;
233
234 if (bb_basename(filename)[0] == '.')
235 goto error;
236
237 p = config_open2(filename, fopen_for_read);
238 if (p == NULL) {
239 rc = FALSE;
240 goto error;
241 }
242
243 while (config_read(p, tokens, 3, 2, "# \t", PARSE_NORMAL)) {
244//Use index_in_strings?
245 if (strcmp(tokens[0], "alias") == 0) {
246 /* alias <wildcard> <modulename> */
247 llist_t *l;
248 char wildcard[MODULE_NAME_LEN];
249 char *rmod;
250
251 if (tokens[2] == NULL)
252 continue;
253 filename2modname(tokens[1], wildcard);
254
255 for (l = G.probes; l != NULL; l = l->link) {
256 m = (struct module_entry *) l->data;
257 if (fnmatch(wildcard, m->modname, 0) != 0)
258 continue;
259 rmod = filename2modname(tokens[2], NULL);
260 llist_add_to(&m->realnames, rmod);
261
262 if (m->flags & MODULE_FLAG_NEED_DEPS) {
263 m->flags &= ~MODULE_FLAG_NEED_DEPS;
264 G.num_unresolved_deps--;
265 }
266
267 m = get_or_add_modentry(rmod);
268 if (!(m->flags & MODULE_FLAG_NEED_DEPS)) {
269 m->flags |= MODULE_FLAG_NEED_DEPS;
270 G.num_unresolved_deps++;
271 }
272 }
273 } else if (strcmp(tokens[0], "options") == 0) {
274 /* options <modulename> <option...> */
275 if (tokens[2] == NULL)
276 continue;
277 m = get_or_add_modentry(tokens[1]);
278 m->options = gather_options_str(m->options, tokens[2]);
279 } else if (strcmp(tokens[0], "include") == 0) {
280 /* include <filename> */
281 read_config(tokens[1]);
282 } else if (ENABLE_FEATURE_MODPROBE_BLACKLIST
283 && strcmp(tokens[0], "blacklist") == 0
284 ) {
285 /* blacklist <modulename> */
286 get_or_add_modentry(tokens[1])->flags |= MODULE_FLAG_BLACKLISTED;
287 }
288 }
289 config_close(p);
290 error:
291 return rc;
292}
293
294static int read_config(const char *path)
295{
296 return recursive_action(path, ACTION_RECURSE | ACTION_QUIET,
297 config_file_action, NULL, NULL, 1);
298}
299
300static const char *humanly_readable_name(struct module_entry *m)
301{
302 /* probed_name may be NULL. modname always exists. */
303 return m->probed_name ? m->probed_name : m->modname;
304}
305
306static char *parse_and_add_kcmdline_module_options(char *options, const char *modulename)
307{
308 char *kcmdline_buf;
309 char *kcmdline;
310 char *kptr;
311 int len;
312
313 kcmdline_buf = xmalloc_open_read_close("/proc/cmdline", NULL);
314 if (!kcmdline_buf)
315 return options;
316
317 kcmdline = kcmdline_buf;
318 len = strlen(modulename);
319 while ((kptr = strsep(&kcmdline, "\n\t ")) != NULL) {
320 if (strncmp(modulename, kptr, len) != 0)
321 continue;
322 kptr += len;
323 if (*kptr != '.')
324 continue;
325 /* It is "modulename.xxxx" */
326 kptr++;
327 if (strchr(kptr, '=') != NULL) {
328 /* It is "modulename.opt=[val]" */
329 options = gather_options_str(options, kptr);
330 }
331 }
332 free(kcmdline_buf);
333
334 return options;
335}
336
337/* Return: similar to bb_init_module:
338 * 0 on success,
339 * -errno on open/read error,
340 * errno on init_module() error
341 */
342/* NB: INSMOD_OPT_SILENT bit suppresses ONLY non-existent modules,
343 * not deleted ones (those are still listed in modules.dep).
344 * module-init-tools version 3.4:
345 * # modprobe bogus
346 * FATAL: Module bogus not found. [exitcode 1]
347 * # modprobe -q bogus [silent, exitcode still 1]
348 * but:
349 * # rm kernel/drivers/net/dummy.ko
350 * # modprobe -q dummy
351 * FATAL: Could not open '/lib/modules/xxx/kernel/drivers/net/dummy.ko': No such file or directory
352 * [exitcode 1]
353 */
354static int do_modprobe(struct module_entry *m)
355{
356 struct module_entry *m2 = m2; /* for compiler */
357 char *fn, *options;
358 int rc, first;
359 llist_t *l;
360
361 if (!(m->flags & MODULE_FLAG_FOUND_IN_MODDEP)) {
362 if (!(option_mask32 & INSMOD_OPT_SILENT))
363 bb_error_msg("module %s not found in modules.dep",
364 humanly_readable_name(m));
365 return -ENOENT;
366 }
367 DBG("do_modprob'ing %s", m->modname);
368
369 if (!(option_mask32 & MODPROBE_OPT_REMOVE))
370 m->deps = llist_rev(m->deps);
371
372 for (l = m->deps; l != NULL; l = l->link)
373 DBG("dep: %s", l->data);
374
375 first = 1;
376 rc = 0;
377 while (m->deps) {
378 rc = 0;
379 fn = llist_pop(&m->deps); /* we leak it */
380 m2 = get_or_add_modentry(fn);
381
382 if (option_mask32 & MODPROBE_OPT_REMOVE) {
383 /* modprobe -r */
384 if (m2->flags & MODULE_FLAG_LOADED) {
385 rc = bb_delete_module(m2->modname, O_EXCL);
386 if (rc) {
387 if (first) {
388 bb_error_msg("can't unload module %s: %s",
389 humanly_readable_name(m2),
390 moderror(rc));
391 break;
392 }
393 } else {
394 m2->flags &= ~MODULE_FLAG_LOADED;
395 }
396 }
397 /* do not error out if *deps* fail to unload */
398 first = 0;
399 continue;
400 }
401
402 if (m2->flags & MODULE_FLAG_LOADED) {
403 DBG("%s is already loaded, skipping", fn);
404 continue;
405 }
406
407 options = m2->options;
408 m2->options = NULL;
409 options = parse_and_add_kcmdline_module_options(options, m2->modname);
410 if (m == m2)
411 options = gather_options_str(options, G.cmdline_mopts);
412 rc = bb_init_module(fn, options);
413 DBG("loaded %s '%s', rc:%d", fn, options, rc);
414 if (rc == EEXIST)
415 rc = 0;
416 free(options);
417 if (rc) {
418 bb_error_msg("can't load module %s (%s): %s",
419 humanly_readable_name(m2),
420 fn,
421 moderror(rc)
422 );
423 break;
424 }
425 m2->flags |= MODULE_FLAG_LOADED;
426 }
427
428 return rc;
429}
430
431static void load_modules_dep(void)
432{
433 struct module_entry *m;
434 char *colon, *tokens[2];
435 parser_t *p;
436
437 /* Modprobe does not work at all without modules.dep,
438 * even if the full module name is given. Returning error here
439 * was making us later confuse user with this message:
440 * "module /full/path/to/existing/file/module.ko not found".
441 * It's better to die immediately, with good message.
442 * xfopen_for_read provides that. */
443 p = config_open2(CONFIG_DEFAULT_DEPMOD_FILE, xfopen_for_read);
444
445 while (G.num_unresolved_deps
446 && config_read(p, tokens, 2, 1, "# \t", PARSE_NORMAL)
447 ) {
448 colon = last_char_is(tokens[0], ':');
449 if (colon == NULL)
450 continue;
451 *colon = 0;
452
453 m = get_modentry(tokens[0]);
454 if (m == NULL)
455 continue;
456
457 /* Optimization... */
458 if ((m->flags & MODULE_FLAG_LOADED)
459 && !(option_mask32 & MODPROBE_OPT_REMOVE)
460 ) {
461 DBG("skip deps of %s, it's already loaded", tokens[0]);
462 continue;
463 }
464
465 m->flags |= MODULE_FLAG_FOUND_IN_MODDEP;
466 if ((m->flags & MODULE_FLAG_NEED_DEPS) && (m->deps == NULL)) {
467 G.num_unresolved_deps--;
468 llist_add_to(&m->deps, xstrdup(tokens[0]));
469 if (tokens[1])
470 string_to_llist(tokens[1], &m->deps, " \t");
471 } else
472 DBG("skipping dep line");
473 }
474 config_close(p);
475}
476
477int modprobe_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
478int modprobe_main(int argc UNUSED_PARAM, char **argv)
479{
480 struct utsname uts;
481 int rc;
482 unsigned opt;
483 struct module_entry *me;
484
485 opt_complementary = MODPROBE_COMPLEMENTARY;
486 opt = getopt32(argv, INSMOD_OPTS MODPROBE_OPTS INSMOD_ARGS);
487 argv += optind;
488
489 /* Goto modules location */
490 xchdir(CONFIG_DEFAULT_MODULES_DIR);
491 uname(&uts);
492 xchdir(uts.release);
493
494 if (opt & MODPROBE_OPT_LIST_ONLY) {
495 char name[MODULE_NAME_LEN];
496 char *colon, *tokens[2];
497 parser_t *p = config_open2(CONFIG_DEFAULT_DEPMOD_FILE, xfopen_for_read);
498
499 while (config_read(p, tokens, 2, 1, "# \t", PARSE_NORMAL)) {
500 colon = last_char_is(tokens[0], ':');
501 if (!colon)
502 continue;
503 *colon = '\0';
504 filename2modname(tokens[0], name);
505 if (!argv[0])
506 puts(tokens[0]);
507 else {
508 int i;
509 for (i = 0; argv[i]; i++) {
510 if (fnmatch(argv[i], name, 0) == 0) {
511 puts(tokens[0]);
512 }
513 }
514 }
515 }
516 return EXIT_SUCCESS;
517 }
518
519 /* Yes, for some reason -l ignores -s... */
520 if (opt & INSMOD_OPT_SYSLOG)
521 logmode = LOGMODE_SYSLOG;
522
523 if (!argv[0]) {
524 if (opt & MODPROBE_OPT_REMOVE) {
525 /* "modprobe -r" (w/o params).
526 * "If name is NULL, all unused modules marked
527 * autoclean will be removed".
528 */
529 if (bb_delete_module(NULL, O_NONBLOCK | O_EXCL) != 0)
530 bb_perror_msg_and_die("rmmod");
531 }
532 return EXIT_SUCCESS;
533 }
534
535 /* Retrieve module names of already loaded modules */
536 {
537 char *s;
538 parser_t *parser = config_open2("/proc/modules", fopen_for_read);
539 while (config_read(parser, &s, 1, 1, "# \t", PARSE_NORMAL & ~PARSE_GREEDY))
540 get_or_add_modentry(s)->flags |= MODULE_FLAG_LOADED;
541 config_close(parser);
542 }
543
544 if (opt & (MODPROBE_OPT_INSERT_ALL | MODPROBE_OPT_REMOVE)) {
545 /* Each argument is a module name */
546 do {
547 DBG("adding module %s", *argv);
548 add_probe(*argv++);
549 } while (*argv);
550 } else {
551 /* First argument is module name, rest are parameters */
552 DBG("probing just module %s", *argv);
553 add_probe(argv[0]);
554 G.cmdline_mopts = parse_cmdline_module_options(argv);
555 }
556
557 /* Happens if all requested modules are already loaded */
558 if (G.probes == NULL)
559 return EXIT_SUCCESS;
560
561 read_config("/etc/modprobe.conf");
562 read_config("/etc/modprobe.d");
563 if (ENABLE_FEATURE_MODUTILS_SYMBOLS && G.need_symbols)
564 read_config("modules.symbols");
565 load_modules_dep();
566 if (ENABLE_FEATURE_MODUTILS_ALIAS && G.num_unresolved_deps) {
567 read_config("modules.alias");
568 load_modules_dep();
569 }
570
571 rc = 0;
572 while ((me = llist_pop(&G.probes)) != NULL) {
573 if (me->realnames == NULL) {
574 DBG("probing by module name");
575 /* This is not an alias. Literal names are blacklisted
576 * only if '-b' is given.
577 */
578 if (!(opt & MODPROBE_OPT_BLACKLIST)
579 || !(me->flags & MODULE_FLAG_BLACKLISTED)
580 ) {
581 rc |= do_modprobe(me);
582 }
583 continue;
584 }
585
586 /* Probe all real names for the alias */
587 do {
588 char *realname = llist_pop(&me->realnames);
589 struct module_entry *m2;
590
591 DBG("probing alias %s by realname %s", me->modname, realname);
592 m2 = get_or_add_modentry(realname);
593 if (!(m2->flags & MODULE_FLAG_BLACKLISTED)
594 && (!(m2->flags & MODULE_FLAG_LOADED)
595 || (opt & MODPROBE_OPT_REMOVE))
596 ) {
597//TODO: we can pass "me" as 2nd param to do_modprobe,
598//and make do_modprobe emit more meaningful error messages
599//with alias name included, not just module name alias resolves to.
600 rc |= do_modprobe(m2);
601 }
602 free(realname);
603 } while (me->realnames != NULL);
604 }
605
606 return (rc != 0);
607}
Note: See TracBrowser for help on using the repository browser.