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