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. Older
13 * versions would only export the direct dependency list.
18 #include <sys/utsname.h>
21 //#define DBG(fmt, ...) bb_error_msg("%s: " fmt, __func__, ## __VA_ARGS__)
22 #define DBG(...) ((void)0)
24 #define MODULE_FLAG_LOADED 0x0001
25 #define MODULE_FLAG_NEED_DEPS 0x0002
26 /* "was seen in modules.dep": */
27 #define MODULE_FLAG_FOUND_IN_MODDEP 0x0004
28 #define MODULE_FLAG_BLACKLISTED 0x0008
30 struct module_entry { /* I'll call it ME. */
32 char *modname; /* stripped of /path/, .ext and s/-/_/g */
33 const char *probed_name; /* verbatim as seen on cmdline */
34 char *options; /* options from config files */
35 llist_t *realnames; /* strings. if this module is an alias, */
36 /* real module name is one of these. */
37 //Can there really be more than one? Example from real kernel?
38 llist_t *deps; /* strings. modules we depend on */
41 #define MODPROBE_OPTS "acdlnrt:VC:" IF_FEATURE_MODPROBE_BLACKLIST("b")
43 MODPROBE_OPT_INSERT_ALL = (INSMOD_OPT_UNUSED << 0), /* a */
44 MODPROBE_OPT_DUMP_ONLY = (INSMOD_OPT_UNUSED << 1), /* c */
45 MODPROBE_OPT_D = (INSMOD_OPT_UNUSED << 2), /* d */
46 MODPROBE_OPT_LIST_ONLY = (INSMOD_OPT_UNUSED << 3), /* l */
47 MODPROBE_OPT_SHOW_ONLY = (INSMOD_OPT_UNUSED << 4), /* n */
48 MODPROBE_OPT_REMOVE = (INSMOD_OPT_UNUSED << 5), /* r */
49 MODPROBE_OPT_RESTRICT = (INSMOD_OPT_UNUSED << 6), /* t */
50 MODPROBE_OPT_VERONLY = (INSMOD_OPT_UNUSED << 7), /* V */
51 MODPROBE_OPT_CONFIGFILE = (INSMOD_OPT_UNUSED << 8), /* C */
52 MODPROBE_OPT_BLACKLIST = (INSMOD_OPT_UNUSED << 9) * ENABLE_FEATURE_MODPROBE_BLACKLIST,
56 llist_t *db; /* MEs of all modules ever seen (caching for speed) */
57 llist_t *probes; /* MEs of module(s) requested on cmdline */
58 char *cmdline_mopts; /* module options from cmdline */
59 int num_unresolved_deps;
60 /* bool. "Did we have 'symbol:FOO' requested on cmdline?" */
61 smallint need_symbols;
63 #define G (*(struct globals*)&bb_common_bufsiz1)
64 #define INIT_G() do { } while (0)
67 static int read_config(const char *path);
69 static char *gather_options_str(char *opts, const char *append)
71 /* Speed-optimized. We call gather_options_str many times. */
73 opts = xstrdup(append);
75 int optlen = strlen(opts);
76 opts = xrealloc(opts, optlen + strlen(append) + 2);
77 sprintf(opts + optlen, " %s", append);
82 static struct module_entry *helper_get_module(const char *module, int create)
84 char modname[MODULE_NAME_LEN];
85 struct module_entry *e;
88 filename2modname(module, modname);
89 for (l = G.db; l != NULL; l = l->link) {
90 e = (struct module_entry *) l->data;
91 if (strcmp(e->modname, modname) == 0)
97 e = xzalloc(sizeof(*e));
98 e->modname = xstrdup(modname);
99 llist_add_to(&G.db, e);
103 static struct module_entry *get_or_add_modentry(const char *module)
105 return helper_get_module(module, 1);
107 static struct module_entry *get_modentry(const char *module)
109 return helper_get_module(module, 0);
112 static void add_probe(const char *name)
114 struct module_entry *m;
116 m = get_or_add_modentry(name);
117 if (m->flags & MODULE_FLAG_LOADED) {
118 DBG("skipping %s, it is already loaded", name);
122 DBG("queuing %s", name);
123 m->probed_name = name;
124 m->flags |= MODULE_FLAG_NEED_DEPS;
125 llist_add_to_end(&G.probes, m);
126 G.num_unresolved_deps++;
127 if (ENABLE_FEATURE_MODUTILS_SYMBOLS
128 && strncmp(m->modname, "symbol:", 7) == 0
134 static int FAST_FUNC config_file_action(const char *filename,
135 struct stat *statbuf UNUSED_PARAM,
136 void *userdata UNUSED_PARAM,
137 int depth UNUSED_PARAM)
141 struct module_entry *m;
144 if (bb_basename(filename)[0] == '.')
147 p = config_open2(filename, fopen_for_read);
153 while (config_read(p, tokens, 3, 2, "# \t", PARSE_NORMAL)) {
154 //Use index_in_strings?
155 if (strcmp(tokens[0], "alias") == 0) {
156 /* alias <wildcard> <modulename> */
158 char wildcard[MODULE_NAME_LEN];
161 if (tokens[2] == NULL)
163 filename2modname(tokens[1], wildcard);
165 for (l = G.probes; l != NULL; l = l->link) {
166 m = (struct module_entry *) l->data;
167 if (fnmatch(wildcard, m->modname, 0) != 0)
169 rmod = filename2modname(tokens[2], NULL);
170 llist_add_to(&m->realnames, rmod);
172 if (m->flags & MODULE_FLAG_NEED_DEPS) {
173 m->flags &= ~MODULE_FLAG_NEED_DEPS;
174 G.num_unresolved_deps--;
177 m = get_or_add_modentry(rmod);
178 if (!(m->flags & MODULE_FLAG_NEED_DEPS)) {
179 m->flags |= MODULE_FLAG_NEED_DEPS;
180 G.num_unresolved_deps++;
183 } else if (strcmp(tokens[0], "options") == 0) {
184 /* options <modulename> <option...> */
185 if (tokens[2] == NULL)
187 m = get_or_add_modentry(tokens[1]);
188 m->options = gather_options_str(m->options, tokens[2]);
189 } else if (strcmp(tokens[0], "include") == 0) {
190 /* include <filename> */
191 read_config(tokens[1]);
192 } else if (ENABLE_FEATURE_MODPROBE_BLACKLIST
193 && strcmp(tokens[0], "blacklist") == 0
195 /* blacklist <modulename> */
196 get_or_add_modentry(tokens[1])->flags |= MODULE_FLAG_BLACKLISTED;
204 static int read_config(const char *path)
206 return recursive_action(path, ACTION_RECURSE | ACTION_QUIET,
207 config_file_action, NULL, NULL, 1);
210 static int do_modprobe(struct module_entry *m)
212 struct module_entry *m2;
217 if (!(m->flags & MODULE_FLAG_FOUND_IN_MODDEP)) {
218 DBG("skipping %s, not found in modules.dep", m->modname);
221 DBG("do_modprob'ing %s", m->modname);
223 if (!(option_mask32 & MODPROBE_OPT_REMOVE))
224 m->deps = llist_rev(m->deps);
226 for (l = m->deps; l != NULL; l = l->link)
227 DBG("dep: %s", l->data);
230 while (m->deps && rc == 0) {
231 fn = llist_pop(&m->deps);
232 m2 = get_or_add_modentry(fn);
233 if (option_mask32 & MODPROBE_OPT_REMOVE) {
234 if (bb_delete_module(m->modname, O_EXCL) != 0)
236 } else if (!(m2->flags & MODULE_FLAG_LOADED)) {
237 options = m2->options;
240 options = gather_options_str(options, G.cmdline_mopts);
241 rc = bb_init_module(fn, options);
242 DBG("loaded %s '%s', rc:%d", fn, options, rc);
244 m2->flags |= MODULE_FLAG_LOADED;
247 DBG("%s is already loaded, skipping", fn);
253 //FIXME: what if rc < 0?
254 if (rc > 0 && !(option_mask32 & INSMOD_OPT_SILENT)) {
255 bb_error_msg("failed to %sload module %s: %s",
256 (option_mask32 & MODPROBE_OPT_REMOVE) ? "un" : "",
257 m->probed_name ? m->probed_name : m->modname,
265 static void load_modules_dep(void)
267 struct module_entry *m;
268 char *colon, *tokens[2];
271 /* Modprobe does not work at all without modprobe.dep,
272 * even if the full module name is given. Returning error here
273 * was making us later confuse user with this message:
274 * "module /full/path/to/existing/file/module.ko not found".
275 * It's better to die immediately, with good message.
276 * xfopen_for_read provides that. */
277 p = config_open2(CONFIG_DEFAULT_DEPMOD_FILE, xfopen_for_read);
279 while (G.num_unresolved_deps
280 && config_read(p, tokens, 2, 1, "# \t", PARSE_NORMAL)
282 colon = last_char_is(tokens[0], ':');
287 m = get_modentry(tokens[0]);
291 /* Optimization... */
292 if ((m->flags & MODULE_FLAG_LOADED)
293 && !(option_mask32 & MODPROBE_OPT_REMOVE)
295 DBG("skip deps of %s, it's already loaded", tokens[0]);
299 m->flags |= MODULE_FLAG_FOUND_IN_MODDEP;
300 if ((m->flags & MODULE_FLAG_NEED_DEPS) && (m->deps == NULL)) {
301 G.num_unresolved_deps--;
302 llist_add_to(&m->deps, xstrdup(tokens[0]));
304 string_to_llist(tokens[1], &m->deps, " ");
306 DBG("skipping dep line");
311 int modprobe_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
312 int modprobe_main(int argc UNUSED_PARAM, char **argv)
317 struct module_entry *me;
319 opt_complementary = "q-v:v-q";
320 opt = getopt32(argv, INSMOD_OPTS MODPROBE_OPTS INSMOD_ARGS, NULL, NULL);
323 if (opt & (MODPROBE_OPT_DUMP_ONLY | MODPROBE_OPT_LIST_ONLY |
324 MODPROBE_OPT_SHOW_ONLY))
325 bb_error_msg_and_die("not supported");
328 if (opt & MODPROBE_OPT_REMOVE) {
329 /* "modprobe -r" (w/o params).
330 * "If name is NULL, all unused modules marked
331 * autoclean will be removed".
333 if (bb_delete_module(NULL, O_NONBLOCK|O_EXCL) != 0)
334 bb_perror_msg_and_die("rmmod");
339 /* Goto modules location */
340 xchdir(CONFIG_DEFAULT_MODULES_DIR);
344 /* Retrieve module names of already loaded modules */
347 parser_t *parser = config_open2("/proc/modules", fopen_for_read);
348 while (config_read(parser, &s, 1, 1, "# \t", PARSE_NORMAL & ~PARSE_GREEDY))
349 get_or_add_modentry(s)->flags |= MODULE_FLAG_LOADED;
350 config_close(parser);
353 if (opt & MODPROBE_OPT_INSERT_ALL) {
354 /* Each argument is a module name */
356 DBG("adding module %s", *argv);
360 /* First argument is module name, rest are parameters */
361 DBG("probing just module %s", *argv);
363 G.cmdline_mopts = parse_cmdline_module_options(argv);
366 /* Happens if all requested modules are already loaded */
367 if (G.probes == NULL)
370 read_config("/etc/modprobe.conf");
371 read_config("/etc/modprobe.d");
372 if (ENABLE_FEATURE_MODUTILS_SYMBOLS && G.need_symbols)
373 read_config("modules.symbols");
375 if (ENABLE_FEATURE_MODUTILS_ALIAS && G.num_unresolved_deps) {
376 read_config("modules.alias");
380 while ((me = llist_pop(&G.probes)) != NULL) {
381 if (me->realnames == NULL) {
382 /* This is not an alias. Literal names are blacklisted
383 * only if '-b' is given.
385 if (!(opt & MODPROBE_OPT_BLACKLIST)
386 || !(me->flags & MODULE_FLAG_BLACKLISTED)
388 rc = do_modprobe(me);
389 //FIXME: what if rc > 0?
390 if (rc < 0 && !(opt & INSMOD_OPT_SILENT))
391 bb_error_msg("module %s not found",
395 /* Probe all realnames */
397 char *realname = llist_pop(&me->realnames);
398 struct module_entry *m2;
400 DBG("probing %s by realname %s", me->modname, realname);
401 m2 = get_or_add_modentry(realname);
402 if (!(m2->flags & MODULE_FLAG_BLACKLISTED))
404 //FIXME: error check?
406 } while (me->realnames != NULL);