typo fix
[busybox-git.git] / modutils / modprobe.c
blob543f53e99674b52122f2cde1e98fdf3bae900171
1 /* vi: set sw=4 ts=4: */
2 /*
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 source tree.
9 */
10 //config:config MODPROBE
11 //config: bool "modprobe (27 kb)"
12 //config: default y
13 //config: help
14 //config: Handle the loading of modules, and their dependencies on a high
15 //config: level.
16 //config:
17 //config:config FEATURE_MODPROBE_BLACKLIST
18 //config: bool "Blacklist support"
19 //config: default y
20 //config: depends on MODPROBE && !MODPROBE_SMALL
21 //config: help
22 //config: Say 'y' here to enable support for the 'blacklist' command in
23 //config: modprobe.conf. This prevents the alias resolver to resolve
24 //config: blacklisted modules. This is useful if you want to prevent your
25 //config: hardware autodetection scripts to load modules like evdev, frame
26 //config: buffer drivers etc.
28 //applet:IF_MODPROBE(IF_NOT_MODPROBE_SMALL(APPLET_NOEXEC(modprobe, modprobe, BB_DIR_SBIN, BB_SUID_DROP, modprobe)))
30 //kbuild:ifneq ($(CONFIG_MODPROBE_SMALL),y)
31 //kbuild:lib-$(CONFIG_MODPROBE) += modprobe.o modutils.o
32 //kbuild:endif
34 #include "libbb.h"
35 #include "modutils.h"
36 #include <sys/utsname.h>
37 #include <fnmatch.h>
39 #if 1
40 #define DBG(...) ((void)0)
41 #else
42 #define DBG(fmt, ...) bb_error_msg("%s: " fmt, __func__, ## __VA_ARGS__)
43 #endif
45 /* Note that unlike older versions of modules.dep/depmod (busybox and m-i-t),
46 * we expect the full dependency list to be specified in modules.dep.
47 * Older versions would only export the direct dependency list.
51 //usage:#if !ENABLE_MODPROBE_SMALL
52 //usage:#define modprobe_notes_usage
53 //usage: "modprobe can (un)load a stack of modules, passing each module options (when\n"
54 //usage: "loading). modprobe uses a configuration file to determine what option(s) to\n"
55 //usage: "pass each module it loads.\n"
56 //usage: "\n"
57 //usage: "The configuration file is searched (in this order):\n"
58 //usage: "\n"
59 //usage: " /etc/modprobe.conf (2.6 only)\n"
60 //usage: " /etc/modules.conf\n"
61 //usage: " /etc/conf.modules (deprecated)\n"
62 //usage: "\n"
63 //usage: "They all have the same syntax (see below). If none is present, it is\n"
64 //usage: "_not_ an error; each loaded module is then expected to load without\n"
65 //usage: "options. Once a file is found, the others are tested for.\n"
66 //usage: "\n"
67 //usage: "/etc/modules.conf entry format:\n"
68 //usage: "\n"
69 //usage: " alias <alias_name> <mod_name>\n"
70 //usage: " Makes it possible to modprobe alias_name, when there is no such module.\n"
71 //usage: " It makes sense if your mod_name is long, or you want a more representative\n"
72 //usage: " name for that module (eg. 'scsi' in place of 'aha7xxx').\n"
73 //usage: " This makes it also possible to use a different set of options (below) for\n"
74 //usage: " the module and the alias.\n"
75 //usage: " A module can be aliased more than once.\n"
76 //usage: "\n"
77 //usage: " options <mod_name|alias_name> <symbol=value...>\n"
78 //usage: " When loading module mod_name (or the module aliased by alias_name), pass\n"
79 //usage: " the \"symbol=value\" pairs as option to that module.\n"
80 //usage: "\n"
81 //usage: "Sample /etc/modules.conf file:\n"
82 //usage: "\n"
83 //usage: " options tulip irq=3\n"
84 //usage: " alias tulip tulip2\n"
85 //usage: " options tulip2 irq=4 io=0x308\n"
86 //usage: "\n"
87 //usage: "Other functionality offered by 'classic' modprobe is not available in\n"
88 //usage: "this implementation.\n"
89 //usage: "\n"
90 //usage: "If module options are present both in the config file, and on the command line,\n"
91 //usage: "then the options from the command line will be passed to the module _after_\n"
92 //usage: "the options from the config file. That way, you can have defaults in the config\n"
93 //usage: "file, and override them for a specific usage from the command line.\n"
94 //usage:#define modprobe_example_usage
95 //usage: "(with the above /etc/modules.conf):\n\n"
96 //usage: "$ modprobe tulip\n"
97 //usage: " will load the module 'tulip' with default option 'irq=3'\n\n"
98 //usage: "$ modprobe tulip irq=5\n"
99 //usage: " will load the module 'tulip' with option 'irq=5', thus overriding the default\n\n"
100 //usage: "$ modprobe tulip2\n"
101 //usage: " will load the module 'tulip' with default options 'irq=4 io=0x308',\n"
102 //usage: " which are the default for alias 'tulip2'\n\n"
103 //usage: "$ modprobe tulip2 irq=8\n"
104 //usage: " will load the module 'tulip' with default options 'irq=4 io=0x308 irq=8',\n"
105 //usage: " which are the default for alias 'tulip2' overridden by the option 'irq=8'\n\n"
106 //usage: " from the command line\n\n"
107 //usage: "$ modprobe tulip2 irq=2 io=0x210\n"
108 //usage: " will load the module 'tulip' with default options 'irq=4 io=0x308 irq=4 io=0x210',\n"
109 //usage: " which are the default for alias 'tulip2' overridden by the options 'irq=2 io=0x210'\n\n"
110 //usage: " from the command line\n"
111 //usage:
112 //usage:#define modprobe_trivial_usage
113 //usage: "[-alrqvsD" IF_FEATURE_MODPROBE_BLACKLIST("b") "]"
114 //usage: " MODULE" IF_FEATURE_CMDLINE_MODULE_OPTIONS(" [SYMBOL=VALUE]...")
115 //usage:#define modprobe_full_usage "\n\n"
116 //usage: " -a Load multiple MODULEs"
117 //usage: "\n -l List (MODULE is a pattern)"
118 //usage: "\n -r Remove MODULE (stacks) or do autoclean"
119 //usage: "\n -q Quiet"
120 //usage: "\n -v Verbose"
121 //usage: "\n -s Log to syslog"
122 //usage: "\n -D Show dependencies"
123 //usage: IF_FEATURE_MODPROBE_BLACKLIST(
124 //usage: "\n -b Apply blacklist to module names too"
125 //usage: )
126 //usage:#endif /* !ENABLE_MODPROBE_SMALL */
128 /* Note: usage text doesn't document various 2.4 options
129 * we pull in through INSMOD_OPTS define
130 * Note2: -b is always accepted, but if !FEATURE_MODPROBE_BLACKLIST,
131 * it is a no-op.
133 #define MODPROBE_OPTS "alrDb"
134 /* -a and -D _are_ in fact compatible */
135 #define MODPROBE_COMPLEMENTARY "q-v:v-q:l--arD:r--alD:a--lr:D--rl"
136 //#define MODPROBE_OPTS "acd:lnrt:C:b"
137 //#define MODPROBE_COMPLEMENTARY "q-v:v-q:l--acr:a--lr:r--al"
138 enum {
139 OPT_INSERT_ALL = (INSMOD_OPT_UNUSED << 0), /* a */
140 //OPT_DUMP_ONLY = (INSMOD_OPT_UNUSED << x), /* c */
141 //OPT_DIRNAME = (INSMOD_OPT_UNUSED << x), /* d */
142 OPT_LIST_ONLY = (INSMOD_OPT_UNUSED << 1), /* l */
143 //OPT_SHOW_ONLY = (INSMOD_OPT_UNUSED << x), /* n */
144 OPT_REMOVE = (INSMOD_OPT_UNUSED << 2), /* r */
145 //OPT_RESTRICT = (INSMOD_OPT_UNUSED << x), /* t */
146 //OPT_VERONLY = (INSMOD_OPT_UNUSED << x), /* V */
147 //OPT_CONFIGFILE = (INSMOD_OPT_UNUSED << x), /* C */
148 OPT_SHOW_DEPS = (INSMOD_OPT_UNUSED << 3), /* D */
149 OPT_BLACKLIST = (INSMOD_OPT_UNUSED << 4) * ENABLE_FEATURE_MODPROBE_BLACKLIST,
151 #if ENABLE_LONG_OPTS
152 static const char modprobe_longopts[] ALIGN1 =
153 /* nobody asked for long opts (yet) */
154 // "all\0" No_argument "a"
155 // "list\0" No_argument "l"
156 // "remove\0" No_argument "r"
157 // "quiet\0" No_argument "q"
158 // "verbose\0" No_argument "v"
159 // "syslog\0" No_argument "s"
160 /* module-init-tools 3.11.1 has only long opt --show-depends
161 * but no short -D, we provide long opt for scripts which
162 * were written for 3.11.1: */
163 "show-depends\0" No_argument "D"
164 // "use-blacklist\0" No_argument "b"
166 #endif
168 #define MODULE_FLAG_LOADED 0x0001
169 #define MODULE_FLAG_NEED_DEPS 0x0002
170 /* "was seen in modules.dep": */
171 #define MODULE_FLAG_FOUND_IN_MODDEP 0x0004
172 #define MODULE_FLAG_BLACKLISTED 0x0008
173 #define MODULE_FLAG_BUILTIN 0x0010
175 struct globals {
176 llist_t *probes; /* MEs of module(s) requested on cmdline */
177 #if ENABLE_FEATURE_CMDLINE_MODULE_OPTIONS
178 char *cmdline_mopts; /* module options from cmdline */
179 #endif
180 int num_unresolved_deps;
181 /* bool. "Did we have 'symbol:FOO' requested on cmdline?" */
182 smallint need_symbols;
183 struct utsname uts;
184 module_db db;
185 } FIX_ALIASING;
186 #define G (*ptr_to_globals)
187 #define INIT_G() do { \
188 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
189 } while (0)
192 static int read_config(const char *path);
194 static char *gather_options_str(char *opts, const char *append)
196 /* Speed-optimized. We call gather_options_str many times. */
197 if (append) {
198 if (opts == NULL) {
199 opts = xstrdup(append);
200 } else {
201 int optlen = strlen(opts);
202 opts = xrealloc(opts, optlen + strlen(append) + 2);
203 sprintf(opts + optlen, " %s", append);
206 return opts;
209 static struct module_entry *get_or_add_modentry(const char *module)
211 return moddb_get_or_create(&G.db, module);
214 static void add_probe(const char *name)
216 struct module_entry *m;
218 m = get_or_add_modentry(name);
219 if (!(option_mask32 & (OPT_REMOVE | OPT_SHOW_DEPS))
220 && (m->flags & (MODULE_FLAG_LOADED | MODULE_FLAG_BUILTIN))
222 DBG("skipping %s, it is already loaded", name);
223 return;
226 DBG("queuing %s", name);
227 m->probed_name = name;
228 m->flags |= MODULE_FLAG_NEED_DEPS;
229 llist_add_to_end(&G.probes, m);
230 G.num_unresolved_deps++;
231 if (ENABLE_FEATURE_MODUTILS_SYMBOLS
232 && is_prefixed_with(m->modname, "symbol:")
234 G.need_symbols = 1;
238 static int FAST_FUNC config_file_action(struct recursive_state *state,
239 const char *filename,
240 struct stat *statbuf UNUSED_PARAM)
242 char *tokens[3];
243 parser_t *p;
244 struct module_entry *m;
245 int rc = TRUE;
246 const char *base;
248 /* Skip files that begin with a "." */
249 base = bb_basename(filename);
250 if (base[0] == '.')
251 goto error;
253 /* "man modprobe.d" from kmod version 22 suggests
254 * that we shouldn't recurse into /etc/modprobe.d/dir/
255 * _subdirectories_:
257 if (state->depth > 1)
258 return SKIP; /* stop recursing */
259 //TODO: instead, can use dirAction in recursive_action() to SKIP dirs
260 //on depth == 1 level. But that's more code...
262 /* In dir recursion, skip files that do not end with a ".conf"
263 * depth==0: read_config("modules.{symbols,alias}") must work,
264 * "include FILE_NOT_ENDING_IN_CONF" must work too.
266 if (state->depth != 0) {
267 if (!is_suffixed_with(base, ".conf"))
268 goto error;
271 p = config_open2(filename, fopen_for_read);
272 if (p == NULL) {
273 rc = FALSE;
274 goto error;
277 while (config_read(p, tokens, 3, 2, "# \t", PARSE_NORMAL)) {
278 //Use index_in_strings?
279 if (strcmp(tokens[0], "alias") == 0) {
280 /* alias <wildcard> <modulename> */
281 llist_t *l;
282 char wildcard[MODULE_NAME_LEN];
283 char *rmod;
285 if (tokens[2] == NULL)
286 continue;
287 filename2modname(tokens[1], wildcard);
289 for (l = G.probes; l; l = l->link) {
290 m = (struct module_entry *) l->data;
291 if (fnmatch(wildcard, m->modname, 0) != 0)
292 continue;
293 rmod = filename2modname(tokens[2], NULL);
294 llist_add_to(&m->realnames, rmod);
296 if (m->flags & MODULE_FLAG_NEED_DEPS) {
297 m->flags &= ~MODULE_FLAG_NEED_DEPS;
298 G.num_unresolved_deps--;
301 m = get_or_add_modentry(rmod);
302 if (!(m->flags & MODULE_FLAG_NEED_DEPS)) {
303 m->flags |= MODULE_FLAG_NEED_DEPS;
304 G.num_unresolved_deps++;
307 } else if (strcmp(tokens[0], "options") == 0) {
308 /* options <modulename> <option...> */
309 if (tokens[2] == NULL)
310 continue;
311 m = get_or_add_modentry(tokens[1]);
312 m->options = gather_options_str(m->options, tokens[2]);
313 } else if (strcmp(tokens[0], "include") == 0) {
314 /* include <filename>/<dirname> (yes, directories also must work) */
315 read_config(tokens[1]);
316 } else if (ENABLE_FEATURE_MODPROBE_BLACKLIST
317 && strcmp(tokens[0], "blacklist") == 0
319 /* blacklist <modulename> */
320 get_or_add_modentry(tokens[1])->flags |= MODULE_FLAG_BLACKLISTED;
323 config_close(p);
324 error:
325 return rc;
328 static int read_config(const char *path)
330 return recursive_action(path, ACTION_RECURSE | ACTION_QUIET,
331 config_file_action, NULL, NULL);
334 static const char *humanly_readable_name(struct module_entry *m)
336 /* probed_name may be NULL. modname always exists. */
337 return m->probed_name ? m->probed_name : m->modname;
340 /* Like strsep(&stringp, "\n\t ") but quoted text goes to single token
341 * even if it contains whitespace.
343 static char *strsep_quotes(char **stringp)
345 char *s, *start = *stringp;
347 if (!start)
348 return NULL;
350 for (s = start; ; s++) {
351 switch (*s) {
352 case '"':
353 s = strchrnul(s + 1, '"'); /* find trailing quote */
354 if (*s != '\0')
355 s++; /* skip trailing quote */
356 /* fall through */
357 case '\0':
358 case '\n':
359 case '\t':
360 case ' ':
361 if (*s != '\0') {
362 *s = '\0';
363 *stringp = s + 1;
364 } else {
365 *stringp = NULL;
367 return start;
372 static char *parse_and_add_kcmdline_module_options(char *options, const char *modulename)
374 char *kcmdline_buf;
375 char *kcmdline;
376 char *kptr;
378 kcmdline_buf = xmalloc_open_read_close("/proc/cmdline", NULL);
379 if (!kcmdline_buf)
380 return options;
382 kcmdline = kcmdline_buf;
383 while ((kptr = strsep_quotes(&kcmdline)) != NULL) {
384 char *after_modulename = is_prefixed_with(kptr, modulename);
385 if (!after_modulename || *after_modulename != '.')
386 continue;
387 /* It is "modulename.xxxx" */
388 kptr = after_modulename + 1;
389 if (strchr(kptr, '=') != NULL) {
390 /* It is "modulename.opt=[val]" */
391 options = gather_options_str(options, kptr);
394 free(kcmdline_buf);
396 return options;
399 /* Return: similar to bb_init_module:
400 * 0 on success,
401 * -errno on open/read error,
402 * errno on init_module() error
404 /* NB: INSMOD_OPT_SILENT bit suppresses ONLY non-existent modules,
405 * not deleted ones (those are still listed in modules.dep).
406 * module-init-tools version 3.4:
407 * # modprobe bogus
408 * FATAL: Module bogus not found. [exitcode 1]
409 * # modprobe -q bogus [silent, exitcode still 1]
410 * but:
411 * # rm kernel/drivers/net/dummy.ko
412 * # modprobe -q dummy
413 * FATAL: Could not open '/lib/modules/xxx/kernel/drivers/net/dummy.ko': No such file or directory
414 * [exitcode 1]
416 static int do_modprobe(struct module_entry *m)
418 int rc, first;
420 if (!(m->flags & MODULE_FLAG_FOUND_IN_MODDEP)) {
421 if (!(option_mask32 & INSMOD_OPT_SILENT))
422 bb_error_msg((m->flags & MODULE_FLAG_BUILTIN) ?
423 "module %s is builtin" :
424 "module %s not found in modules.dep",
425 humanly_readable_name(m));
426 return -ENOENT;
428 DBG("do_modprob'ing %s", m->modname);
430 if (!(option_mask32 & OPT_REMOVE))
431 m->deps = llist_rev(m->deps);
433 if (0) {
434 llist_t *l;
435 for (l = m->deps; l; l = l->link)
436 DBG("dep: %s", l->data);
439 first = 1;
440 rc = 0;
441 while (m->deps) {
442 struct module_entry *m2;
443 char *fn, *options;
445 rc = 0;
446 fn = llist_pop(&m->deps); /* we leak it */
447 m2 = get_or_add_modentry(bb_get_last_path_component_nostrip(fn));
449 if (option_mask32 & OPT_REMOVE) {
450 /* modprobe -r */
451 if (m2->flags & MODULE_FLAG_LOADED) {
452 rc = bb_delete_module(m2->modname, O_EXCL);
453 if (rc) {
454 if (first) {
455 bb_perror_msg("can't unload module '%s'",
456 humanly_readable_name(m2));
457 break;
459 } else {
460 m2->flags &= ~MODULE_FLAG_LOADED;
463 /* do not error out if *deps* fail to unload */
464 first = 0;
465 continue;
468 options = m2->options;
469 m2->options = NULL;
470 options = parse_and_add_kcmdline_module_options(options, m2->modname);
471 #if ENABLE_FEATURE_CMDLINE_MODULE_OPTIONS
472 if (m == m2)
473 options = gather_options_str(options, G.cmdline_mopts);
474 #endif
476 if (option_mask32 & OPT_SHOW_DEPS) {
477 printf(options ? "insmod %s/%s/%s %s\n"
478 : "insmod %s/%s/%s\n",
479 CONFIG_DEFAULT_MODULES_DIR, G.uts.release, fn,
480 options);
481 free(options);
482 continue;
485 if (m2->flags & MODULE_FLAG_LOADED) {
486 DBG("%s is already loaded, skipping", fn);
487 free(options);
488 continue;
491 rc = bb_init_module(fn, options);
492 DBG("loaded %s '%s', rc:%d", fn, options, rc);
493 if (rc == EEXIST)
494 rc = 0;
495 free(options);
496 if (rc) {
497 bb_error_msg("can't load module %s (%s): %s",
498 humanly_readable_name(m2),
500 moderror(rc)
502 break;
504 m2->flags |= MODULE_FLAG_LOADED;
507 return rc;
510 static void load_modules_dep(void)
512 struct module_entry *m;
513 char *colon, *tokens[2];
514 parser_t *p;
516 /* Modprobe does not work at all without modules.dep,
517 * even if the full module name is given. Returning error here
518 * was making us later confuse user with this message:
519 * "module /full/path/to/existing/file/module.ko not found".
520 * It's better to die immediately, with good message.
521 * xfopen_for_read provides that. */
522 p = config_open2(CONFIG_DEFAULT_DEPMOD_FILE, xfopen_for_read);
524 while (G.num_unresolved_deps
525 && config_read(p, tokens, 2, 1, "# \t", PARSE_NORMAL)
527 colon = last_char_is(tokens[0], ':');
528 if (colon == NULL)
529 continue;
530 *colon = '\0';
532 m = moddb_get(&G.db, bb_get_last_path_component_nostrip(tokens[0]));
533 if (m == NULL)
534 continue;
536 /* Optimization... */
537 if ((m->flags & MODULE_FLAG_LOADED)
538 && !(option_mask32 & (OPT_REMOVE | OPT_SHOW_DEPS))
540 DBG("skip deps of %s, it's already loaded", tokens[0]);
541 continue;
544 m->flags |= MODULE_FLAG_FOUND_IN_MODDEP;
545 if ((m->flags & MODULE_FLAG_NEED_DEPS) && (m->deps == NULL)) {
546 G.num_unresolved_deps--;
547 llist_add_to(&m->deps, xstrdup(tokens[0]));
548 if (tokens[1])
549 string_to_llist(tokens[1], &m->deps, " \t");
550 } else
551 DBG("skipping dep line");
553 config_close(p);
556 int modprobe_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
557 int modprobe_main(int argc UNUSED_PARAM, char **argv)
559 int rc;
560 unsigned opt;
561 struct module_entry *me;
563 INIT_G();
565 opt = getopt32long(argv, "^" INSMOD_OPTS MODPROBE_OPTS "\0" MODPROBE_COMPLEMENTARY,
566 modprobe_longopts
567 INSMOD_ARGS
569 argv += optind;
571 /* Goto modules location */
572 xchdir(CONFIG_DEFAULT_MODULES_DIR);
573 uname(&G.uts);
574 xchdir(G.uts.release);
576 if (opt & OPT_LIST_ONLY) {
577 int i;
578 char *colon, *tokens[2];
579 parser_t *p = config_open2(CONFIG_DEFAULT_DEPMOD_FILE, xfopen_for_read);
581 for (i = 0; argv[i]; i++)
582 replace(argv[i], '-', '_');
584 while (config_read(p, tokens, 2, 1, "# \t", PARSE_NORMAL)) {
585 colon = last_char_is(tokens[0], ':');
586 if (!colon)
587 continue;
588 *colon = '\0';
589 if (!argv[0])
590 puts(tokens[0]);
591 else {
592 char name[MODULE_NAME_LEN];
593 filename2modname(
594 bb_get_last_path_component_nostrip(tokens[0]),
595 name
597 for (i = 0; argv[i]; i++) {
598 if (fnmatch(argv[i], name, 0) == 0) {
599 puts(tokens[0]);
604 return EXIT_SUCCESS;
607 /* Yes, for some reason -l ignores -s... */
608 if (opt & INSMOD_OPT_SYSLOG)
609 logmode = LOGMODE_SYSLOG;
611 if (!argv[0]) {
612 if (opt & OPT_REMOVE) {
613 /* "modprobe -r" (w/o params).
614 * "If name is NULL, all unused modules marked
615 * autoclean will be removed".
617 if (bb_delete_module(NULL, O_NONBLOCK | O_EXCL) != 0)
618 bb_perror_nomsg_and_die();
620 return EXIT_SUCCESS;
623 /* Retrieve module names of already loaded modules */
625 char *s;
626 parser_t *parser = config_open2("/proc/modules", fopen_for_read);
627 while (config_read(parser, &s, 1, 1, "# \t", PARSE_NORMAL & ~PARSE_GREEDY))
628 get_or_add_modentry(s)->flags |= MODULE_FLAG_LOADED;
629 config_close(parser);
631 parser = config_open2("modules.builtin", fopen_for_read);
632 /* this file contains lines like "kernel/fs/binfmt_script.ko" */
633 while (config_read(parser, &s, 1, 1, "# \t", PARSE_NORMAL))
634 get_or_add_modentry(bb_basename(s))->flags |= MODULE_FLAG_BUILTIN;
635 config_close(parser);
638 if (opt & (OPT_INSERT_ALL | OPT_REMOVE)) {
639 /* Each argument is a module name */
640 do {
641 DBG("adding module %s", *argv);
642 add_probe(*argv++);
643 } while (*argv);
644 } else {
645 /* First argument is module name, rest are parameters */
646 DBG("probing just module %s", *argv);
647 add_probe(argv[0]);
648 #if ENABLE_FEATURE_CMDLINE_MODULE_OPTIONS
649 G.cmdline_mopts = parse_cmdline_module_options(argv, /*quote_spaces:*/ 1);
650 #endif
653 /* Happens if all requested modules are already loaded */
654 if (G.probes == NULL)
655 return EXIT_SUCCESS;
657 read_config("/etc/modprobe.conf");
658 read_config("/etc/modprobe.d");
659 if (ENABLE_FEATURE_MODUTILS_SYMBOLS && G.need_symbols)
660 read_config("modules.symbols");
661 load_modules_dep();
662 if (ENABLE_FEATURE_MODUTILS_ALIAS && G.num_unresolved_deps) {
663 read_config("modules.alias");
664 load_modules_dep();
667 /* Handle modprobe.blacklist=module1,module2,... */
668 if (ENABLE_FEATURE_MODPROBE_BLACKLIST) {
669 char *options;
670 char *substr;
672 options = parse_and_add_kcmdline_module_options(NULL, "modprobe");
673 while ((substr = strsep(&options, " ")) != NULL) {
674 char *fn = is_prefixed_with(substr, "blacklist=");
675 if (!fn)
676 continue;
677 while ((substr = strsep(&fn, ",")) != NULL) {
678 /* blacklist <modulename> */
679 get_or_add_modentry(substr)->flags |= MODULE_FLAG_BLACKLISTED;
680 DBG("blacklist: %s", substr);
683 /*free(options); - WRONG, strsep may have advanced it */
686 rc = 0;
687 while ((me = llist_pop(&G.probes)) != NULL) {
688 if (me->realnames == NULL) {
689 DBG("probing by module name");
690 /* This is not an alias. Literal names are blacklisted
691 * only if '-b' is given.
693 if (!(opt & OPT_BLACKLIST)
694 || !(me->flags & MODULE_FLAG_BLACKLISTED)
696 rc |= do_modprobe(me);
698 continue;
701 /* Probe all real names for the alias */
702 do {
703 char *realname = llist_pop(&me->realnames);
704 struct module_entry *m2;
706 DBG("probing alias %s by realname %s", me->modname, realname);
707 m2 = get_or_add_modentry(realname);
708 if (!(m2->flags & MODULE_FLAG_BLACKLISTED)
709 && (!(m2->flags & MODULE_FLAG_LOADED)
710 || (opt & (OPT_REMOVE | OPT_SHOW_DEPS)))
712 //TODO: we can pass "me" as 2nd param to do_modprobe,
713 //and make do_modprobe emit more meaningful error messages
714 //with alias name included, not just module name alias resolves to.
715 rc |= do_modprobe(m2);
717 free(realname);
718 } while (me->realnames != NULL);
721 if (ENABLE_FEATURE_CLEAN_UP)
722 moddb_free(&G.db);
724 return (rc != 0);