1 /* vi: set sw=4 ts=4: */
3 * Modprobe written from scratch for BusyBox
5 * Copyright (c) 2008 Timo Teras <timo.teras@iki.fi>
6 * Copyright (c) 2008 Vladimir Dronnikov
8 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
11 /* Note that unlike older versions of modules.dep/depmod (busybox and m-i-t),
12 * we expect the full dependency list to be specified in modules.dep.
13 * Older versions would only export the direct dependency list.
17 #include <sys/utsname.h>
20 //#define DBG(fmt, ...) bb_error_msg("%s: " fmt, __func__, ## __VA_ARGS__)
21 #define DBG(...) ((void)0)
23 //usage:#if !ENABLE_MODPROBE_SMALL
24 //usage:#define modprobe_notes_usage
25 //usage: "modprobe can (un)load a stack of modules, passing each module options (when\n"
26 //usage: "loading). modprobe uses a configuration file to determine what option(s) to\n"
27 //usage: "pass each module it loads.\n"
29 //usage: "The configuration file is searched (in this order):\n"
31 //usage: " /etc/modprobe.conf (2.6 only)\n"
32 //usage: " /etc/modules.conf\n"
33 //usage: " /etc/conf.modules (deprecated)\n"
35 //usage: "They all have the same syntax (see below). If none is present, it is\n"
36 //usage: "_not_ an error; each loaded module is then expected to load without\n"
37 //usage: "options. Once a file is found, the others are tested for.\n"
39 //usage: "/etc/modules.conf entry format:\n"
41 //usage: " alias <alias_name> <mod_name>\n"
42 //usage: " Makes it possible to modprobe alias_name, when there is no such module.\n"
43 //usage: " It makes sense if your mod_name is long, or you want a more representative\n"
44 //usage: " name for that module (eg. 'scsi' in place of 'aha7xxx').\n"
45 //usage: " This makes it also possible to use a different set of options (below) for\n"
46 //usage: " the module and the alias.\n"
47 //usage: " A module can be aliased more than once.\n"
49 //usage: " options <mod_name|alias_name> <symbol=value...>\n"
50 //usage: " When loading module mod_name (or the module aliased by alias_name), pass\n"
51 //usage: " the \"symbol=value\" pairs as option to that module.\n"
53 //usage: "Sample /etc/modules.conf file:\n"
55 //usage: " options tulip irq=3\n"
56 //usage: " alias tulip tulip2\n"
57 //usage: " options tulip2 irq=4 io=0x308\n"
59 //usage: "Other functionality offered by 'classic' modprobe is not available in\n"
60 //usage: "this implementation.\n"
62 //usage: "If module options are present both in the config file, and on the command line,\n"
63 //usage: "then the options from the command line will be passed to the module _after_\n"
64 //usage: "the options from the config file. That way, you can have defaults in the config\n"
65 //usage: "file, and override them for a specific usage from the command line.\n"
66 //usage:#define modprobe_example_usage
67 //usage: "(with the above /etc/modules.conf):\n\n"
68 //usage: "$ modprobe tulip\n"
69 //usage: " will load the module 'tulip' with default option 'irq=3'\n\n"
70 //usage: "$ modprobe tulip irq=5\n"
71 //usage: " will load the module 'tulip' with option 'irq=5', thus overriding the default\n\n"
72 //usage: "$ modprobe tulip2\n"
73 //usage: " will load the module 'tulip' with default options 'irq=4 io=0x308',\n"
74 //usage: " which are the default for alias 'tulip2'\n\n"
75 //usage: "$ modprobe tulip2 irq=8\n"
76 //usage: " will load the module 'tulip' with default options 'irq=4 io=0x308 irq=8',\n"
77 //usage: " which are the default for alias 'tulip2' overridden by the option 'irq=8'\n\n"
78 //usage: " from the command line\n\n"
79 //usage: "$ modprobe tulip2 irq=2 io=0x210\n"
80 //usage: " will load the module 'tulip' with default options 'irq=4 io=0x308 irq=4 io=0x210',\n"
81 //usage: " which are the default for alias 'tulip2' overridden by the options 'irq=2 io=0x210'\n\n"
82 //usage: " from the command line\n"
84 //usage:#define modprobe_trivial_usage
86 //usage: IF_FEATURE_MODPROBE_BLACKLIST("b")
87 //usage: "] MODULE [symbol=value]..."
88 //usage:#define modprobe_full_usage "\n\n"
90 //usage: "\n -a Load multiple MODULEs"
91 //usage: "\n -l List (MODULE is a pattern)"
92 //usage: "\n -r Remove MODULE (stacks) or do autoclean"
93 //usage: "\n -q Quiet"
94 //usage: "\n -v Verbose"
95 //usage: "\n -s Log to syslog"
96 //usage: IF_FEATURE_MODPROBE_BLACKLIST(
97 //usage: "\n -b Apply blacklist to module names too"
99 //usage:#endif /* !ENABLE_MODPROBE_SMALL */
101 /* Note that usage text doesn't document various 2.4 options
102 * we pull in through INSMOD_OPTS define */
104 #define MODPROBE_COMPLEMENTARY "q-v:v-q:l--ar:a--lr:r--al"
105 #define MODPROBE_OPTS "alr" IF_FEATURE_MODPROBE_BLACKLIST("b")
106 //#define MODPROBE_COMPLEMENTARY "q-v:v-q:l--acr:a--lr:r--al"
107 //#define MODPROBE_OPTS "acd:lnrt:C:" IF_FEATURE_MODPROBE_BLACKLIST("b")
109 MODPROBE_OPT_INSERT_ALL = (INSMOD_OPT_UNUSED << 0), /* a */
110 //MODPROBE_OPT_DUMP_ONLY= (INSMOD_OPT_UNUSED << x), /* c */
111 //MODPROBE_OPT_DIRNAME = (INSMOD_OPT_UNUSED << x), /* d */
112 MODPROBE_OPT_LIST_ONLY = (INSMOD_OPT_UNUSED << 1), /* l */
113 //MODPROBE_OPT_SHOW_ONLY= (INSMOD_OPT_UNUSED << x), /* n */
114 MODPROBE_OPT_REMOVE = (INSMOD_OPT_UNUSED << 2), /* r */
115 //MODPROBE_OPT_RESTRICT = (INSMOD_OPT_UNUSED << x), /* t */
116 //MODPROBE_OPT_VERONLY = (INSMOD_OPT_UNUSED << x), /* V */
117 //MODPROBE_OPT_CONFIGFILE=(INSMOD_OPT_UNUSED << x), /* C */
118 MODPROBE_OPT_BLACKLIST = (INSMOD_OPT_UNUSED << 3) * ENABLE_FEATURE_MODPROBE_BLACKLIST,
121 #define MODULE_FLAG_LOADED 0x0001
122 #define MODULE_FLAG_NEED_DEPS 0x0002
123 /* "was seen in modules.dep": */
124 #define MODULE_FLAG_FOUND_IN_MODDEP 0x0004
125 #define MODULE_FLAG_BLACKLISTED 0x0008
127 struct module_entry { /* I'll call it ME. */
129 char *modname; /* stripped of /path/, .ext and s/-/_/g */
130 const char *probed_name; /* verbatim as seen on cmdline */
131 char *options; /* options from config files */
132 llist_t *realnames; /* strings. if this module is an alias, */
133 /* real module name is one of these. */
134 //Can there really be more than one? Example from real kernel?
135 llist_t *deps; /* strings. modules we depend on */
139 llist_t *db; /* MEs of all modules ever seen (caching for speed) */
140 llist_t *probes; /* MEs of module(s) requested on cmdline */
141 char *cmdline_mopts; /* module options from cmdline */
142 int num_unresolved_deps;
143 /* bool. "Did we have 'symbol:FOO' requested on cmdline?" */
144 smallint need_symbols;
146 #define G (*(struct globals*)&bb_common_bufsiz1)
147 #define INIT_G() do { } while (0)
150 static int read_config(const char *path);
152 static char *gather_options_str(char *opts, const char *append)
154 /* Speed-optimized. We call gather_options_str many times. */
157 opts = xstrdup(append);
159 int optlen = strlen(opts);
160 opts = xrealloc(opts, optlen + strlen(append) + 2);
161 sprintf(opts + optlen, " %s", append);
167 static struct module_entry *helper_get_module(const char *module, int create)
169 char modname[MODULE_NAME_LEN];
170 struct module_entry *e;
173 filename2modname(module, modname);
174 for (l = G.db; l != NULL; l = l->link) {
175 e = (struct module_entry *) l->data;
176 if (strcmp(e->modname, modname) == 0)
182 e = xzalloc(sizeof(*e));
183 e->modname = xstrdup(modname);
184 llist_add_to(&G.db, e);
188 static struct module_entry *get_or_add_modentry(const char *module)
190 return helper_get_module(module, 1);
192 static struct module_entry *get_modentry(const char *module)
194 return helper_get_module(module, 0);
197 static void add_probe(const char *name)
199 struct module_entry *m;
201 m = get_or_add_modentry(name);
202 if (!(option_mask32 & MODPROBE_OPT_REMOVE)
203 && (m->flags & MODULE_FLAG_LOADED)
205 DBG("skipping %s, it is already loaded", name);
209 DBG("queuing %s", name);
210 m->probed_name = name;
211 m->flags |= MODULE_FLAG_NEED_DEPS;
212 llist_add_to_end(&G.probes, m);
213 G.num_unresolved_deps++;
214 if (ENABLE_FEATURE_MODUTILS_SYMBOLS
215 && strncmp(m->modname, "symbol:", 7) == 0
221 static int FAST_FUNC config_file_action(const char *filename,
222 struct stat *statbuf UNUSED_PARAM,
223 void *userdata UNUSED_PARAM,
224 int depth UNUSED_PARAM)
228 struct module_entry *m;
231 if (bb_basename(filename)[0] == '.')
234 p = config_open2(filename, fopen_for_read);
240 while (config_read(p, tokens, 3, 2, "# \t", PARSE_NORMAL)) {
241 //Use index_in_strings?
242 if (strcmp(tokens[0], "alias") == 0) {
243 /* alias <wildcard> <modulename> */
245 char wildcard[MODULE_NAME_LEN];
248 if (tokens[2] == NULL)
250 filename2modname(tokens[1], wildcard);
252 for (l = G.probes; l != NULL; l = l->link) {
253 m = (struct module_entry *) l->data;
254 if (fnmatch(wildcard, m->modname, 0) != 0)
256 rmod = filename2modname(tokens[2], NULL);
257 llist_add_to(&m->realnames, rmod);
259 if (m->flags & MODULE_FLAG_NEED_DEPS) {
260 m->flags &= ~MODULE_FLAG_NEED_DEPS;
261 G.num_unresolved_deps--;
264 m = get_or_add_modentry(rmod);
265 if (!(m->flags & MODULE_FLAG_NEED_DEPS)) {
266 m->flags |= MODULE_FLAG_NEED_DEPS;
267 G.num_unresolved_deps++;
270 } else if (strcmp(tokens[0], "options") == 0) {
271 /* options <modulename> <option...> */
272 if (tokens[2] == NULL)
274 m = get_or_add_modentry(tokens[1]);
275 m->options = gather_options_str(m->options, tokens[2]);
276 } else if (strcmp(tokens[0], "include") == 0) {
277 /* include <filename> */
278 read_config(tokens[1]);
279 } else if (ENABLE_FEATURE_MODPROBE_BLACKLIST
280 && strcmp(tokens[0], "blacklist") == 0
282 /* blacklist <modulename> */
283 get_or_add_modentry(tokens[1])->flags |= MODULE_FLAG_BLACKLISTED;
291 static int read_config(const char *path)
293 return recursive_action(path, ACTION_RECURSE | ACTION_QUIET,
294 config_file_action, NULL, NULL, 1);
297 static const char *humanly_readable_name(struct module_entry *m)
299 /* probed_name may be NULL. modname always exists. */
300 return m->probed_name ? m->probed_name : m->modname;
303 static char *parse_and_add_kcmdline_module_options(char *options, const char *modulename)
310 kcmdline_buf = xmalloc_open_read_close("/proc/cmdline", NULL);
314 kcmdline = kcmdline_buf;
315 len = strlen(modulename);
316 while ((kptr = strsep(&kcmdline, "\n\t ")) != NULL) {
317 if (strncmp(modulename, kptr, len) != 0)
322 /* It is "modulename.xxxx" */
324 if (strchr(kptr, '=') != NULL) {
325 /* It is "modulename.opt=[val]" */
326 options = gather_options_str(options, kptr);
334 /* Return: similar to bb_init_module:
336 * -errno on open/read error,
337 * errno on init_module() error
339 /* NB: INSMOD_OPT_SILENT bit suppresses ONLY non-existent modules,
340 * not deleted ones (those are still listed in modules.dep).
341 * module-init-tools version 3.4:
343 * FATAL: Module bogus not found. [exitcode 1]
344 * # modprobe -q bogus [silent, exitcode still 1]
346 * # rm kernel/drivers/net/dummy.ko
347 * # modprobe -q dummy
348 * FATAL: Could not open '/lib/modules/xxx/kernel/drivers/net/dummy.ko': No such file or directory
351 static int do_modprobe(struct module_entry *m)
353 struct module_entry *m2 = m2; /* for compiler */
358 if (!(m->flags & MODULE_FLAG_FOUND_IN_MODDEP)) {
359 if (!(option_mask32 & INSMOD_OPT_SILENT))
360 bb_error_msg("module %s not found in modules.dep",
361 humanly_readable_name(m));
364 DBG("do_modprob'ing %s", m->modname);
366 if (!(option_mask32 & MODPROBE_OPT_REMOVE))
367 m->deps = llist_rev(m->deps);
369 for (l = m->deps; l != NULL; l = l->link)
370 DBG("dep: %s", l->data);
376 fn = llist_pop(&m->deps); /* we leak it */
377 m2 = get_or_add_modentry(fn);
379 if (option_mask32 & MODPROBE_OPT_REMOVE) {
381 if (m2->flags & MODULE_FLAG_LOADED) {
382 rc = bb_delete_module(m2->modname, O_EXCL);
385 bb_error_msg("can't unload module %s: %s",
386 humanly_readable_name(m2),
391 m2->flags &= ~MODULE_FLAG_LOADED;
394 /* do not error out if *deps* fail to unload */
399 if (m2->flags & MODULE_FLAG_LOADED) {
400 DBG("%s is already loaded, skipping", fn);
404 options = m2->options;
406 options = parse_and_add_kcmdline_module_options(options, m2->modname);
408 options = gather_options_str(options, G.cmdline_mopts);
409 rc = bb_init_module(fn, options);
410 DBG("loaded %s '%s', rc:%d", fn, options, rc);
415 bb_error_msg("can't load module %s (%s): %s",
416 humanly_readable_name(m2),
422 m2->flags |= MODULE_FLAG_LOADED;
428 static void load_modules_dep(void)
430 struct module_entry *m;
431 char *colon, *tokens[2];
434 /* Modprobe does not work at all without modules.dep,
435 * even if the full module name is given. Returning error here
436 * was making us later confuse user with this message:
437 * "module /full/path/to/existing/file/module.ko not found".
438 * It's better to die immediately, with good message.
439 * xfopen_for_read provides that. */
440 p = config_open2(CONFIG_DEFAULT_DEPMOD_FILE, xfopen_for_read);
442 while (G.num_unresolved_deps
443 && config_read(p, tokens, 2, 1, "# \t", PARSE_NORMAL)
445 colon = last_char_is(tokens[0], ':');
450 m = get_modentry(tokens[0]);
454 /* Optimization... */
455 if ((m->flags & MODULE_FLAG_LOADED)
456 && !(option_mask32 & MODPROBE_OPT_REMOVE)
458 DBG("skip deps of %s, it's already loaded", tokens[0]);
462 m->flags |= MODULE_FLAG_FOUND_IN_MODDEP;
463 if ((m->flags & MODULE_FLAG_NEED_DEPS) && (m->deps == NULL)) {
464 G.num_unresolved_deps--;
465 llist_add_to(&m->deps, xstrdup(tokens[0]));
467 string_to_llist(tokens[1], &m->deps, " \t");
469 DBG("skipping dep line");
474 int modprobe_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
475 int modprobe_main(int argc UNUSED_PARAM, char **argv)
480 struct module_entry *me;
482 opt_complementary = MODPROBE_COMPLEMENTARY;
483 opt = getopt32(argv, INSMOD_OPTS MODPROBE_OPTS INSMOD_ARGS);
486 if (opt & MODPROBE_OPT_LIST_ONLY) {
487 char name[MODULE_NAME_LEN];
488 char *colon, *tokens[2];
489 parser_t *p = config_open2(CONFIG_DEFAULT_DEPMOD_FILE, xfopen_for_read);
491 while (config_read(p, tokens, 2, 1, "# \t", PARSE_NORMAL)) {
492 colon = last_char_is(tokens[0], ':');
496 filename2modname(tokens[0], name);
501 for (i = 0; argv[i]; i++) {
502 if (fnmatch(argv[i], name, 0) == 0) {
511 /* Yes, for some reason -l ignores -s... */
512 if (opt & INSMOD_OPT_SYSLOG)
513 logmode = LOGMODE_SYSLOG;
516 if (opt & MODPROBE_OPT_REMOVE) {
517 /* "modprobe -r" (w/o params).
518 * "If name is NULL, all unused modules marked
519 * autoclean will be removed".
521 if (bb_delete_module(NULL, O_NONBLOCK | O_EXCL) != 0)
522 bb_perror_msg_and_die("rmmod");
527 /* Goto modules location */
528 xchdir(CONFIG_DEFAULT_MODULES_DIR);
532 /* Retrieve module names of already loaded modules */
535 parser_t *parser = config_open2("/proc/modules", fopen_for_read);
536 while (config_read(parser, &s, 1, 1, "# \t", PARSE_NORMAL & ~PARSE_GREEDY))
537 get_or_add_modentry(s)->flags |= MODULE_FLAG_LOADED;
538 config_close(parser);
541 if (opt & (MODPROBE_OPT_INSERT_ALL | MODPROBE_OPT_REMOVE)) {
542 /* Each argument is a module name */
544 DBG("adding module %s", *argv);
548 /* First argument is module name, rest are parameters */
549 DBG("probing just module %s", *argv);
551 G.cmdline_mopts = parse_cmdline_module_options(argv);
554 /* Happens if all requested modules are already loaded */
555 if (G.probes == NULL)
558 read_config("/etc/modprobe.conf");
559 read_config("/etc/modprobe.d");
560 if (ENABLE_FEATURE_MODUTILS_SYMBOLS && G.need_symbols)
561 read_config("modules.symbols");
563 if (ENABLE_FEATURE_MODUTILS_ALIAS && G.num_unresolved_deps) {
564 read_config("modules.alias");
569 while ((me = llist_pop(&G.probes)) != NULL) {
570 if (me->realnames == NULL) {
571 DBG("probing by module name");
572 /* This is not an alias. Literal names are blacklisted
573 * only if '-b' is given.
575 if (!(opt & MODPROBE_OPT_BLACKLIST)
576 || !(me->flags & MODULE_FLAG_BLACKLISTED)
578 rc |= do_modprobe(me);
583 /* Probe all real names for the alias */
585 char *realname = llist_pop(&me->realnames);
586 struct module_entry *m2;
588 DBG("probing alias %s by realname %s", me->modname, realname);
589 m2 = get_or_add_modentry(realname);
590 if (!(m2->flags & MODULE_FLAG_BLACKLISTED)
591 && (!(m2->flags & MODULE_FLAG_LOADED)
592 || (opt & MODPROBE_OPT_REMOVE))
594 //TODO: we can pass "me" as 2nd param to do_modprobe,
595 //and make do_modprobe emit more meaningful error messages
596 //with alias name included, not just module name alias resolves to.
597 rc |= do_modprobe(m2);
600 } while (me->realnames != NULL);