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