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.
13 #include <sys/utsname.h>
16 #define MODULE_FLAG_LOADED 0x0001
17 #define MODULE_FLAG_NEED_DEPS 0x0002
18 //Misnomer? Seems to mean "was seen in modules.dep":
19 #define MODULE_FLAG_EXISTS 0x0004
20 #define MODULE_FLAG_BLACKLISTED 0x0008
22 struct module_entry { /* I'll call it ME. */
24 char *modname; /* stripped of /path/, .ext and s/-/_/g */
25 const char *probed_name; /* verbatim as seen on cmdline */
26 llist_t *aliases; /* strings. aliases from config files */
27 llist_t *options; /* strings. options from config files */
28 llist_t *deps; /* strings. modules we depend on */
31 #define MODPROBE_OPTS "acdlnrt:VC:" USE_FEATURE_MODPROBE_BLACKLIST("b")
33 MODPROBE_OPT_INSERT_ALL = (INSMOD_OPT_UNUSED << 0), /* a */
34 MODPROBE_OPT_DUMP_ONLY = (INSMOD_OPT_UNUSED << 1), /* c */
35 MODPROBE_OPT_D = (INSMOD_OPT_UNUSED << 2), /* d */
36 MODPROBE_OPT_LIST_ONLY = (INSMOD_OPT_UNUSED << 3), /* l */
37 MODPROBE_OPT_SHOW_ONLY = (INSMOD_OPT_UNUSED << 4), /* n */
38 MODPROBE_OPT_REMOVE = (INSMOD_OPT_UNUSED << 5), /* r */
39 MODPROBE_OPT_RESTRICT = (INSMOD_OPT_UNUSED << 6), /* t */
40 MODPROBE_OPT_VERONLY = (INSMOD_OPT_UNUSED << 7), /* V */
41 MODPROBE_OPT_CONFIGFILE = (INSMOD_OPT_UNUSED << 8), /* C */
42 MODPROBE_OPT_BLACKLIST = (INSMOD_OPT_UNUSED << 9) * ENABLE_FEATURE_MODPROBE_BLACKLIST,
46 llist_t *db; /* MEs of all modules ever seen (caching for speed) */
47 llist_t *probes; /* MEs of module(s) requested on cmdline */
48 llist_t *cmdline_mopts; /* strings. module options (from cmdline) */
49 int num_deps; /* what is this? */
50 /* bool. "Did we have 'symbol:FOO' requested on cmdline?" */
51 smallint need_symbols;
53 #define G (*(struct globals*)&bb_common_bufsiz1)
54 #define INIT_G() do { } while (0)
57 static int read_config(const char *path);
59 static struct module_entry *helper_get_module(const char *module, int create)
61 char modname[MODULE_NAME_LEN];
62 struct module_entry *e;
65 filename2modname(module, modname);
66 for (l = G.db; l != NULL; l = l->link) {
67 e = (struct module_entry *) l->data;
68 if (strcmp(e->modname, modname) == 0)
74 e = xzalloc(sizeof(*e));
75 e->modname = xstrdup(modname);
76 llist_add_to(&G.db, e);
80 static struct module_entry *get_or_add_modentry(const char *module)
82 return helper_get_module(module, 1);
84 static struct module_entry *get_modentry(const char *module)
86 return helper_get_module(module, 0);
89 static void add_probe(const char *name)
91 struct module_entry *m;
93 m = get_or_add_modentry(name);
94 m->probed_name = name;
95 m->flags |= MODULE_FLAG_NEED_DEPS;
96 llist_add_to(&G.probes, m);
99 if (ENABLE_FEATURE_MODUTILS_SYMBOLS
100 && strncmp(m->modname, "symbol:", 7) == 0)
104 static int FAST_FUNC config_file_action(const char *filename,
105 struct stat *statbuf UNUSED_PARAM,
106 void *userdata UNUSED_PARAM,
107 int depth UNUSED_PARAM)
109 RESERVE_CONFIG_BUFFER(modname, MODULE_NAME_LEN);
110 char *tokens[3], *rmod;
113 struct module_entry *m;
116 if (bb_basename(filename)[0] == '.')
119 p = config_open2(filename, fopen_for_read);
125 while (config_read(p, tokens, 3, 2, "# \t", PARSE_NORMAL)) {
126 //Use index_in_strings?
127 if (strcmp(tokens[0], "alias") == 0) {
128 filename2modname(tokens[1], modname);
129 if (tokens[2] == NULL)
132 for (l = G.probes; l != NULL; l = l->link) {
133 m = (struct module_entry *) l->data;
134 if (fnmatch(modname, m->modname, 0) != 0)
136 rmod = filename2modname(tokens[2], NULL);
137 llist_add_to(&m->aliases, rmod);
139 if (m->flags & MODULE_FLAG_NEED_DEPS) {
140 m->flags &= ~MODULE_FLAG_NEED_DEPS;
144 m = get_or_add_modentry(rmod);
145 m->flags |= MODULE_FLAG_NEED_DEPS;
148 } else if (strcmp(tokens[0], "options") == 0) {
149 if (tokens[2] == NULL)
151 m = get_or_add_modentry(tokens[1]);
152 llist_add_to(&m->options, xstrdup(tokens[2]));
153 } else if (strcmp(tokens[0], "include") == 0) {
154 read_config(tokens[1]);
155 } else if (ENABLE_FEATURE_MODPROBE_BLACKLIST
156 && strcmp(tokens[0], "blacklist") == 0
158 get_or_add_modentry(tokens[1])->flags |= MODULE_FLAG_BLACKLISTED;
163 RELEASE_CONFIG_BUFFER(modname);
167 static int read_config(const char *path)
169 return recursive_action(path, ACTION_RECURSE | ACTION_QUIET,
170 config_file_action, NULL, NULL, 1);
173 static char *gather_options(char *opts, llist_t *o)
179 optlen = strlen(opts);
181 for (; o != NULL; o = o->link) {
182 opts = xrealloc(opts, optlen + strlen(o->data) + 2);
183 optlen += sprintf(opts + optlen, "%s ", o->data);
188 static int do_modprobe(struct module_entry *m)
190 struct module_entry *m2;
194 if (!(m->flags & MODULE_FLAG_EXISTS))
197 if (!(option_mask32 & MODPROBE_OPT_REMOVE))
198 m->deps = llist_rev(m->deps);
201 while (m->deps && rc == 0) {
202 fn = llist_pop(&m->deps);
203 m2 = get_or_add_modentry(fn);
204 if (option_mask32 & MODPROBE_OPT_REMOVE) {
205 if (bb_delete_module(m->modname, O_EXCL) != 0)
207 } else if (!(m2->flags & MODULE_FLAG_LOADED)) {
208 options = gather_options(NULL, m2->options);
210 options = gather_options(options, G.cmdline_mopts);
211 //TODO: looks like G.cmdline_mopts can contain either NULL or just one string, not more?
212 rc = bb_init_module(fn, options);
214 m2->flags |= MODULE_FLAG_LOADED;
221 if (rc > 0 && !(option_mask32 & INSMOD_OPT_SILENT)) {
222 bb_error_msg("failed to %sload module %s: %s",
223 (option_mask32 & MODPROBE_OPT_REMOVE) ? "un" : "",
224 m->probed_name ? m->probed_name : m->modname,
232 static void load_modules_dep(void)
234 struct module_entry *m;
235 char *colon, *tokens[2];
238 p = config_open2(CONFIG_DEFAULT_DEPMOD_FILE, xfopen_for_read);
240 /* Modprobe does not work at all without modprobe.dep,
241 * even if the full module name is given. Returning error here
242 * was making us later confuse user with this message:
243 * "module /full/path/to/existing/file/module.ko not found".
244 * It's better to die immediately, with good message: */
246 // bb_perror_msg_and_die("can't open '%s'", CONFIG_DEFAULT_DEPMOD_FILE);
249 && config_read(p, tokens, 2, 1, "# \t", PARSE_NORMAL)
251 colon = last_char_is(tokens[0], ':');
256 m = get_modentry(tokens[0]);
259 //Can we skip it too if it is MODULE_FLAG_LOADED?
261 m->flags |= MODULE_FLAG_EXISTS;
262 if ((m->flags & MODULE_FLAG_NEED_DEPS) && (m->deps == NULL)) {
264 llist_add_to(&m->deps, xstrdup(tokens[0]));
266 string_to_llist(tokens[1], &m->deps, " ");
272 int modprobe_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
273 int modprobe_main(int argc UNUSED_PARAM, char **argv)
278 struct module_entry *me;
280 opt_complementary = "q-v:v-q";
281 opt = getopt32(argv, INSMOD_OPTS MODPROBE_OPTS INSMOD_ARGS, NULL, NULL);
284 if (opt & (MODPROBE_OPT_DUMP_ONLY | MODPROBE_OPT_LIST_ONLY |
285 MODPROBE_OPT_SHOW_ONLY))
286 bb_error_msg_and_die("not supported");
288 /* Goto modules location */
289 xchdir(CONFIG_DEFAULT_MODULES_DIR);
294 if (opt & MODPROBE_OPT_REMOVE) {
295 /* TODO: comment here */
296 if (bb_delete_module(NULL, O_NONBLOCK|O_EXCL) != 0)
297 bb_perror_msg_and_die("rmmod");
302 if (opt & MODPROBE_OPT_INSERT_ALL) {
303 /* Each argument is a module name */
307 G.probes = llist_rev(G.probes);
309 /* First argument is module name, rest are parameters */
311 //TODO: looks like G.cmdline_mopts can contain either NULL or just one string, not more?
313 llist_add_to(&G.cmdline_mopts, parse_cmdline_module_options(argv));
316 /* Retrieve module names of already loaded modules */
319 parser_t *parser = config_open2("/proc/modules", fopen_for_read);
320 while (config_read(parser, &s, 1, 1, "# \t", PARSE_NORMAL & ~PARSE_GREEDY))
321 get_or_add_modentry(s)->flags |= MODULE_FLAG_LOADED;
322 config_close(parser);
325 read_config("/etc/modprobe.conf");
326 read_config("/etc/modprobe.d");
327 if (ENABLE_FEATURE_MODUTILS_SYMBOLS && G.need_symbols)
328 read_config("modules.symbols");
330 if (ENABLE_FEATURE_MODUTILS_ALIAS && G.num_deps) {
331 read_config("modules.alias");
335 while ((me = llist_pop(&G.probes)) != NULL) {
336 if (me->aliases == NULL) {
337 /* Try if module by literal name is found; literal
338 * names are blacklisted only if '-b' is given. */
339 if (!(opt & MODPROBE_OPT_BLACKLIST)
340 || !(me->flags & MODULE_FLAG_BLACKLISTED)
342 rc = do_modprobe(me);
343 if (rc < 0 && !(opt & INSMOD_OPT_SILENT))
344 bb_error_msg("module %s not found",
348 /* Probe all aliases */
350 char *realname = llist_pop(&me->aliases);
351 struct module_entry *m2;
353 m2 = get_or_add_modentry(realname);
354 if (!(m2->flags & MODULE_FLAG_BLACKLISTED))
357 } while (me->aliases != NULL);