Magellan Linux

Contents of /tags/mkinitrd-6_3_1/busybox/modutils/modprobe.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1143 - (show annotations) (download)
Thu Aug 19 12:44:27 2010 UTC (13 years, 9 months ago) by niro
File MIME type: text/plain
File size: 18234 byte(s)
tagged 'mkinitrd-6_3_1'
1 /* vi: set sw=4 ts=4: */
2 /*
3 * Modprobe written from scratch for BusyBox
4 *
5 * Copyright (c) 2008 Timo Teras <timo.teras@iki.fi>
6 * Copyright (c) 2008 Vladimir Dronnikov
7 *
8 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
9 */
10
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.
14 */
15 #include "libbb.h"
16 #include "modutils.h"
17 #include <sys/utsname.h>
18 #include <fnmatch.h>
19
20 //#define DBG(fmt, ...) bb_error_msg("%s: " fmt, __func__, ## __VA_ARGS__)
21 #define DBG(...) ((void)0)
22
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"
28 //usage: "\n"
29 //usage: "The configuration file is searched (in this order):\n"
30 //usage: "\n"
31 //usage: " /etc/modprobe.conf (2.6 only)\n"
32 //usage: " /etc/modules.conf\n"
33 //usage: " /etc/conf.modules (deprecated)\n"
34 //usage: "\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"
38 //usage: "\n"
39 //usage: "/etc/modules.conf entry format:\n"
40 //usage: "\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"
48 //usage: "\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"
52 //usage: "\n"
53 //usage: "Sample /etc/modules.conf file:\n"
54 //usage: "\n"
55 //usage: " options tulip irq=3\n"
56 //usage: " alias tulip tulip2\n"
57 //usage: " options tulip2 irq=4 io=0x308\n"
58 //usage: "\n"
59 //usage: "Other functionality offered by 'classic' modprobe is not available in\n"
60 //usage: "this implementation.\n"
61 //usage: "\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"
83 //usage:
84 //usage:#define modprobe_trivial_usage
85 //usage: "[-alrqvs"
86 //usage: IF_FEATURE_MODPROBE_BLACKLIST("b")
87 //usage: "] MODULE [symbol=value]..."
88 //usage:#define modprobe_full_usage "\n\n"
89 //usage: "Options:"
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"
98 //usage: )
99 //usage:#endif /* !ENABLE_MODPROBE_SMALL */
100
101 /* Note that usage text doesn't document various 2.4 options
102 * we pull in through INSMOD_OPTS define */
103
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")
108 enum {
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,
119 };
120
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
126
127 struct module_entry { /* I'll call it ME. */
128 unsigned flags;
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 */
136 };
137
138 struct globals {
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;
145 } FIX_ALIASING;
146 #define G (*(struct globals*)&bb_common_bufsiz1)
147 #define INIT_G() do { } while (0)
148
149
150 static int read_config(const char *path);
151
152 static char *gather_options_str(char *opts, const char *append)
153 {
154 /* Speed-optimized. We call gather_options_str many times. */
155 if (append) {
156 if (opts == NULL) {
157 opts = xstrdup(append);
158 } else {
159 int optlen = strlen(opts);
160 opts = xrealloc(opts, optlen + strlen(append) + 2);
161 sprintf(opts + optlen, " %s", append);
162 }
163 }
164 return opts;
165 }
166
167 static struct module_entry *helper_get_module(const char *module, int create)
168 {
169 char modname[MODULE_NAME_LEN];
170 struct module_entry *e;
171 llist_t *l;
172
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)
177 return e;
178 }
179 if (!create)
180 return NULL;
181
182 e = xzalloc(sizeof(*e));
183 e->modname = xstrdup(modname);
184 llist_add_to(&G.db, e);
185
186 return e;
187 }
188 static struct module_entry *get_or_add_modentry(const char *module)
189 {
190 return helper_get_module(module, 1);
191 }
192 static struct module_entry *get_modentry(const char *module)
193 {
194 return helper_get_module(module, 0);
195 }
196
197 static void add_probe(const char *name)
198 {
199 struct module_entry *m;
200
201 m = get_or_add_modentry(name);
202 if (!(option_mask32 & MODPROBE_OPT_REMOVE)
203 && (m->flags & MODULE_FLAG_LOADED)
204 ) {
205 DBG("skipping %s, it is already loaded", name);
206 return;
207 }
208
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
216 ) {
217 G.need_symbols = 1;
218 }
219 }
220
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)
225 {
226 char *tokens[3];
227 parser_t *p;
228 struct module_entry *m;
229 int rc = TRUE;
230
231 if (bb_basename(filename)[0] == '.')
232 goto error;
233
234 p = config_open2(filename, fopen_for_read);
235 if (p == NULL) {
236 rc = FALSE;
237 goto error;
238 }
239
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> */
244 llist_t *l;
245 char wildcard[MODULE_NAME_LEN];
246 char *rmod;
247
248 if (tokens[2] == NULL)
249 continue;
250 filename2modname(tokens[1], wildcard);
251
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)
255 continue;
256 rmod = filename2modname(tokens[2], NULL);
257 llist_add_to(&m->realnames, rmod);
258
259 if (m->flags & MODULE_FLAG_NEED_DEPS) {
260 m->flags &= ~MODULE_FLAG_NEED_DEPS;
261 G.num_unresolved_deps--;
262 }
263
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++;
268 }
269 }
270 } else if (strcmp(tokens[0], "options") == 0) {
271 /* options <modulename> <option...> */
272 if (tokens[2] == NULL)
273 continue;
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
281 ) {
282 /* blacklist <modulename> */
283 get_or_add_modentry(tokens[1])->flags |= MODULE_FLAG_BLACKLISTED;
284 }
285 }
286 config_close(p);
287 error:
288 return rc;
289 }
290
291 static int read_config(const char *path)
292 {
293 return recursive_action(path, ACTION_RECURSE | ACTION_QUIET,
294 config_file_action, NULL, NULL, 1);
295 }
296
297 static const char *humanly_readable_name(struct module_entry *m)
298 {
299 /* probed_name may be NULL. modname always exists. */
300 return m->probed_name ? m->probed_name : m->modname;
301 }
302
303 static char *parse_and_add_kcmdline_module_options(char *options, const char *modulename)
304 {
305 char *kcmdline_buf;
306 char *kcmdline;
307 char *kptr;
308 int len;
309
310 kcmdline_buf = xmalloc_open_read_close("/proc/cmdline", NULL);
311 if (!kcmdline_buf)
312 return options;
313
314 kcmdline = kcmdline_buf;
315 len = strlen(modulename);
316 while ((kptr = strsep(&kcmdline, "\n\t ")) != NULL) {
317 if (strncmp(modulename, kptr, len) != 0)
318 continue;
319 kptr += len;
320 if (*kptr != '.')
321 continue;
322 /* It is "modulename.xxxx" */
323 kptr++;
324 if (strchr(kptr, '=') != NULL) {
325 /* It is "modulename.opt=[val]" */
326 options = gather_options_str(options, kptr);
327 }
328 }
329 free(kcmdline_buf);
330
331 return options;
332 }
333
334 /* Return: similar to bb_init_module:
335 * 0 on success,
336 * -errno on open/read error,
337 * errno on init_module() error
338 */
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:
342 * # modprobe bogus
343 * FATAL: Module bogus not found. [exitcode 1]
344 * # modprobe -q bogus [silent, exitcode still 1]
345 * but:
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
349 * [exitcode 1]
350 */
351 static int do_modprobe(struct module_entry *m)
352 {
353 struct module_entry *m2 = m2; /* for compiler */
354 char *fn, *options;
355 int rc, first;
356 llist_t *l;
357
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));
362 return -ENOENT;
363 }
364 DBG("do_modprob'ing %s", m->modname);
365
366 if (!(option_mask32 & MODPROBE_OPT_REMOVE))
367 m->deps = llist_rev(m->deps);
368
369 for (l = m->deps; l != NULL; l = l->link)
370 DBG("dep: %s", l->data);
371
372 first = 1;
373 rc = 0;
374 while (m->deps) {
375 rc = 0;
376 fn = llist_pop(&m->deps); /* we leak it */
377 m2 = get_or_add_modentry(fn);
378
379 if (option_mask32 & MODPROBE_OPT_REMOVE) {
380 /* modprobe -r */
381 if (m2->flags & MODULE_FLAG_LOADED) {
382 rc = bb_delete_module(m2->modname, O_EXCL);
383 if (rc) {
384 if (first) {
385 bb_error_msg("can't unload module %s: %s",
386 humanly_readable_name(m2),
387 moderror(rc));
388 break;
389 }
390 } else {
391 m2->flags &= ~MODULE_FLAG_LOADED;
392 }
393 }
394 /* do not error out if *deps* fail to unload */
395 first = 0;
396 continue;
397 }
398
399 if (m2->flags & MODULE_FLAG_LOADED) {
400 DBG("%s is already loaded, skipping", fn);
401 continue;
402 }
403
404 options = m2->options;
405 m2->options = NULL;
406 options = parse_and_add_kcmdline_module_options(options, m2->modname);
407 if (m == m2)
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);
411 if (rc == EEXIST)
412 rc = 0;
413 free(options);
414 if (rc) {
415 bb_error_msg("can't load module %s (%s): %s",
416 humanly_readable_name(m2),
417 fn,
418 moderror(rc)
419 );
420 break;
421 }
422 m2->flags |= MODULE_FLAG_LOADED;
423 }
424
425 return rc;
426 }
427
428 static void load_modules_dep(void)
429 {
430 struct module_entry *m;
431 char *colon, *tokens[2];
432 parser_t *p;
433
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);
441
442 while (G.num_unresolved_deps
443 && config_read(p, tokens, 2, 1, "# \t", PARSE_NORMAL)
444 ) {
445 colon = last_char_is(tokens[0], ':');
446 if (colon == NULL)
447 continue;
448 *colon = 0;
449
450 m = get_modentry(tokens[0]);
451 if (m == NULL)
452 continue;
453
454 /* Optimization... */
455 if ((m->flags & MODULE_FLAG_LOADED)
456 && !(option_mask32 & MODPROBE_OPT_REMOVE)
457 ) {
458 DBG("skip deps of %s, it's already loaded", tokens[0]);
459 continue;
460 }
461
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]));
466 if (tokens[1])
467 string_to_llist(tokens[1], &m->deps, " \t");
468 } else
469 DBG("skipping dep line");
470 }
471 config_close(p);
472 }
473
474 int modprobe_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
475 int modprobe_main(int argc UNUSED_PARAM, char **argv)
476 {
477 struct utsname uts;
478 int rc;
479 unsigned opt;
480 struct module_entry *me;
481
482 opt_complementary = MODPROBE_COMPLEMENTARY;
483 opt = getopt32(argv, INSMOD_OPTS MODPROBE_OPTS INSMOD_ARGS);
484 argv += optind;
485
486 /* Goto modules location */
487 xchdir(CONFIG_DEFAULT_MODULES_DIR);
488 uname(&uts);
489 xchdir(uts.release);
490
491 if (opt & MODPROBE_OPT_LIST_ONLY) {
492 char name[MODULE_NAME_LEN];
493 char *colon, *tokens[2];
494 parser_t *p = config_open2(CONFIG_DEFAULT_DEPMOD_FILE, xfopen_for_read);
495
496 while (config_read(p, tokens, 2, 1, "# \t", PARSE_NORMAL)) {
497 colon = last_char_is(tokens[0], ':');
498 if (!colon)
499 continue;
500 *colon = '\0';
501 filename2modname(tokens[0], name);
502 if (!argv[0])
503 puts(tokens[0]);
504 else {
505 int i;
506 for (i = 0; argv[i]; i++) {
507 if (fnmatch(argv[i], name, 0) == 0) {
508 puts(tokens[0]);
509 }
510 }
511 }
512 }
513 return EXIT_SUCCESS;
514 }
515
516 /* Yes, for some reason -l ignores -s... */
517 if (opt & INSMOD_OPT_SYSLOG)
518 logmode = LOGMODE_SYSLOG;
519
520 if (!argv[0]) {
521 if (opt & MODPROBE_OPT_REMOVE) {
522 /* "modprobe -r" (w/o params).
523 * "If name is NULL, all unused modules marked
524 * autoclean will be removed".
525 */
526 if (bb_delete_module(NULL, O_NONBLOCK | O_EXCL) != 0)
527 bb_perror_msg_and_die("rmmod");
528 }
529 return EXIT_SUCCESS;
530 }
531
532 /* Retrieve module names of already loaded modules */
533 {
534 char *s;
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);
539 }
540
541 if (opt & (MODPROBE_OPT_INSERT_ALL | MODPROBE_OPT_REMOVE)) {
542 /* Each argument is a module name */
543 do {
544 DBG("adding module %s", *argv);
545 add_probe(*argv++);
546 } while (*argv);
547 } else {
548 /* First argument is module name, rest are parameters */
549 DBG("probing just module %s", *argv);
550 add_probe(argv[0]);
551 G.cmdline_mopts = parse_cmdline_module_options(argv);
552 }
553
554 /* Happens if all requested modules are already loaded */
555 if (G.probes == NULL)
556 return EXIT_SUCCESS;
557
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");
562 load_modules_dep();
563 if (ENABLE_FEATURE_MODUTILS_ALIAS && G.num_unresolved_deps) {
564 read_config("modules.alias");
565 load_modules_dep();
566 }
567
568 rc = 0;
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.
574 */
575 if (!(opt & MODPROBE_OPT_BLACKLIST)
576 || !(me->flags & MODULE_FLAG_BLACKLISTED)
577 ) {
578 rc |= do_modprobe(me);
579 }
580 continue;
581 }
582
583 /* Probe all real names for the alias */
584 do {
585 char *realname = llist_pop(&me->realnames);
586 struct module_entry *m2;
587
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))
593 ) {
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);
598 }
599 free(realname);
600 } while (me->realnames != NULL);
601 }
602
603 return (rc != 0);
604 }