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 source tree.
10 //config:config MODPROBE
11 //config: bool "modprobe (27 kb)"
14 //config: Handle the loading of modules, and their dependencies on a high
17 //config:config FEATURE_MODPROBE_BLACKLIST
18 //config: bool "Blacklist support"
20 //config: depends on MODPROBE && !MODPROBE_SMALL
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
36 #include <sys/utsname.h>
40 #define DBG(...) ((void)0)
42 #define DBG(fmt, ...) bb_error_msg("%s: " fmt, __func__, ## __VA_ARGS__)
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"
57 //usage: "The configuration file is searched (in this order):\n"
59 //usage: " /etc/modprobe.conf (2.6 only)\n"
60 //usage: " /etc/modules.conf\n"
61 //usage: " /etc/conf.modules (deprecated)\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"
67 //usage: "/etc/modules.conf entry format:\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"
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"
81 //usage: "Sample /etc/modules.conf file:\n"
83 //usage: " options tulip irq=3\n"
84 //usage: " alias tulip tulip2\n"
85 //usage: " options tulip2 irq=4 io=0x308\n"
87 //usage: "Other functionality offered by 'classic' modprobe is not available in\n"
88 //usage: "this implementation.\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"
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"
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,
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"
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
,
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"
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
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 */
180 int num_unresolved_deps
;
181 /* bool. "Did we have 'symbol:FOO' requested on cmdline?" */
182 smallint need_symbols
;
186 #define G (*ptr_to_globals)
187 #define INIT_G() do { \
188 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
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. */
199 opts
= xstrdup(append
);
201 int optlen
= strlen(opts
);
202 opts
= xrealloc(opts
, optlen
+ strlen(append
) + 2);
203 sprintf(opts
+ optlen
, " %s", append
);
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
);
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:")
238 static int FAST_FUNC
config_file_action(struct recursive_state
*state
,
239 const char *filename
,
240 struct stat
*statbuf UNUSED_PARAM
)
244 struct module_entry
*m
;
248 /* Skip files that begin with a "." */
249 base
= bb_basename(filename
);
253 /* "man modprobe.d" from kmod version 22 suggests
254 * that we shouldn't recurse into /etc/modprobe.d/dir/
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"))
271 p
= config_open2(filename
, fopen_for_read
);
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> */
282 char wildcard
[MODULE_NAME_LEN
];
285 if (tokens
[2] == NULL
)
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)
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
)
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
;
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
;
350 for (s
= start
; ; s
++) {
353 s
= strchrnul(s
+ 1, '"'); /* find trailing quote */
355 s
++; /* skip trailing quote */
372 static char *parse_and_add_kcmdline_module_options(char *options
, const char *modulename
)
378 kcmdline_buf
= xmalloc_open_read_close("/proc/cmdline", NULL
);
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
!= '.')
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
);
399 /* Return: similar to bb_init_module:
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:
408 * FATAL: Module bogus not found. [exitcode 1]
409 * # modprobe -q bogus [silent, exitcode still 1]
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
416 static int do_modprobe(struct module_entry
*m
)
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
));
428 DBG("do_modprob'ing %s", m
->modname
);
430 if (!(option_mask32
& OPT_REMOVE
))
431 m
->deps
= llist_rev(m
->deps
);
435 for (l
= m
->deps
; l
; l
= l
->link
)
436 DBG("dep: %s", l
->data
);
442 struct module_entry
*m2
;
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
) {
451 if (m2
->flags
& MODULE_FLAG_LOADED
) {
452 rc
= bb_delete_module(m2
->modname
, O_EXCL
);
455 bb_perror_msg("can't unload module '%s'",
456 humanly_readable_name(m2
));
460 m2
->flags
&= ~MODULE_FLAG_LOADED
;
463 /* do not error out if *deps* fail to unload */
468 options
= m2
->options
;
470 options
= parse_and_add_kcmdline_module_options(options
, m2
->modname
);
471 #if ENABLE_FEATURE_CMDLINE_MODULE_OPTIONS
473 options
= gather_options_str(options
, G
.cmdline_mopts
);
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
,
485 if (m2
->flags
& MODULE_FLAG_LOADED
) {
486 DBG("%s is already loaded, skipping", fn
);
491 rc
= bb_init_module(fn
, options
);
492 DBG("loaded %s '%s', rc:%d", fn
, options
, rc
);
497 bb_error_msg("can't load module %s (%s): %s",
498 humanly_readable_name(m2
),
504 m2
->flags
|= MODULE_FLAG_LOADED
;
510 static void load_modules_dep(void)
512 struct module_entry
*m
;
513 char *colon
, *tokens
[2];
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], ':');
532 m
= moddb_get(&G
.db
, bb_get_last_path_component_nostrip(tokens
[0]));
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]);
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]));
549 string_to_llist(tokens
[1], &m
->deps
, " \t");
551 DBG("skipping dep line");
556 int modprobe_main(int argc
, char **argv
) MAIN_EXTERNALLY_VISIBLE
;
557 int modprobe_main(int argc UNUSED_PARAM
, char **argv
)
561 struct module_entry
*me
;
565 opt
= getopt32long(argv
, "^" INSMOD_OPTS MODPROBE_OPTS
"\0" MODPROBE_COMPLEMENTARY
,
571 /* Goto modules location */
572 xchdir(CONFIG_DEFAULT_MODULES_DIR
);
574 xchdir(G
.uts
.release
);
576 if (opt
& OPT_LIST_ONLY
) {
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], ':');
592 char name
[MODULE_NAME_LEN
];
594 bb_get_last_path_component_nostrip(tokens
[0]),
597 for (i
= 0; argv
[i
]; i
++) {
598 if (fnmatch(argv
[i
], name
, 0) == 0) {
607 /* Yes, for some reason -l ignores -s... */
608 if (opt
& INSMOD_OPT_SYSLOG
)
609 logmode
= LOGMODE_SYSLOG
;
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();
623 /* Retrieve module names of already loaded modules */
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 */
641 DBG("adding module %s", *argv
);
645 /* First argument is module name, rest are parameters */
646 DBG("probing just module %s", *argv
);
648 #if ENABLE_FEATURE_CMDLINE_MODULE_OPTIONS
649 G
.cmdline_mopts
= parse_cmdline_module_options(argv
, /*quote_spaces:*/ 1);
653 /* Happens if all requested modules are already loaded */
654 if (G
.probes
== NULL
)
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");
662 if (ENABLE_FEATURE_MODUTILS_ALIAS
&& G
.num_unresolved_deps
) {
663 read_config("modules.alias");
667 /* Handle modprobe.blacklist=module1,module2,... */
668 if (ENABLE_FEATURE_MODPROBE_BLACKLIST
) {
672 options
= parse_and_add_kcmdline_module_options(NULL
, "modprobe");
673 while ((substr
= strsep(&options
, " ")) != NULL
) {
674 char *fn
= is_prefixed_with(substr
, "blacklist=");
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 */
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
);
701 /* Probe all real names for the alias */
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
);
718 } while (me
->realnames
!= NULL
);
721 if (ENABLE_FEATURE_CLEAN_UP
)