modprobe: read modules.builtin
[oweals/busybox.git] / modutils / modprobe.c
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 y
13 //config:       select PLATFORM_LINUX
14 //config:       help
15 //config:         Handle the loading of modules, and their dependencies on a high
16 //config:         level.
17 //config:
18 //config:config FEATURE_MODPROBE_BLACKLIST
19 //config:       bool "Blacklist support"
20 //config:       default y
21 //config:       depends on MODPROBE && !MODPROBE_SMALL
22 //config:       help
23 //config:         Say 'y' here to enable support for the 'blacklist' command in
24 //config:         modprobe.conf. This prevents the alias resolver to resolve
25 //config:         blacklisted modules. This is useful if you want to prevent your
26 //config:         hardware autodetection scripts to load modules like evdev, frame
27 //config:         buffer drivers etc.
28
29 //applet:IF_MODPROBE(IF_NOT_MODPROBE_SMALL(APPLET(modprobe, BB_DIR_SBIN, BB_SUID_DROP)))
30
31 //kbuild:ifneq ($(CONFIG_MODPROBE_SMALL),y)
32 //kbuild:lib-$(CONFIG_MODPROBE) += modprobe.o modutils.o
33 //kbuild:endif
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" IF_FEATURE_CMDLINE_MODULE_OPTIONS(" [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 #define MODULE_FLAG_BUILTIN             0x0010
175
176 struct globals {
177         llist_t *probes; /* MEs of module(s) requested on cmdline */
178 #if ENABLE_FEATURE_CMDLINE_MODULE_OPTIONS
179         char *cmdline_mopts; /* module options from cmdline */
180 #endif
181         int num_unresolved_deps;
182         /* bool. "Did we have 'symbol:FOO' requested on cmdline?" */
183         smallint need_symbols;
184         struct utsname uts;
185         module_db db;
186 } FIX_ALIASING;
187 #define G (*ptr_to_globals)
188 #define INIT_G() do { \
189         SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
190 } while (0)
191
192
193 static int read_config(const char *path);
194
195 static char *gather_options_str(char *opts, const char *append)
196 {
197         /* Speed-optimized. We call gather_options_str many times. */
198         if (append) {
199                 if (opts == NULL) {
200                         opts = xstrdup(append);
201                 } else {
202                         int optlen = strlen(opts);
203                         opts = xrealloc(opts, optlen + strlen(append) + 2);
204                         sprintf(opts + optlen, " %s", append);
205                 }
206         }
207         return opts;
208 }
209
210 static struct module_entry *get_or_add_modentry(const char *module)
211 {
212         return moddb_get_or_create(&G.db, module);
213 }
214
215 static void add_probe(const char *name)
216 {
217         struct module_entry *m;
218
219         m = get_or_add_modentry(name);
220         if (!(option_mask32 & (OPT_REMOVE | OPT_SHOW_DEPS))
221          && (m->flags & (MODULE_FLAG_LOADED | MODULE_FLAG_BUILTIN))
222         ) {
223                 DBG("skipping %s, it is already loaded", name);
224                 return;
225         }
226
227         DBG("queuing %s", name);
228         m->probed_name = name;
229         m->flags |= MODULE_FLAG_NEED_DEPS;
230         llist_add_to_end(&G.probes, m);
231         G.num_unresolved_deps++;
232         if (ENABLE_FEATURE_MODUTILS_SYMBOLS
233          && is_prefixed_with(m->modname, "symbol:")
234         ) {
235                 G.need_symbols = 1;
236         }
237 }
238
239 static int FAST_FUNC config_file_action(const char *filename,
240                                         struct stat *statbuf UNUSED_PARAM,
241                                         void *userdata UNUSED_PARAM,
242                                         int depth)
243 {
244         char *tokens[3];
245         parser_t *p;
246         struct module_entry *m;
247         int rc = TRUE;
248         const char *base, *ext;
249
250         /* Skip files that begin with a "." */
251         base = bb_basename(filename);
252         if (base[0] == '.')
253                 goto error;
254
255         /* In dir recursion, skip files that do not end with a ".conf"
256          * depth==0: read_config("modules.{symbols,alias}") must work,
257          * "include FILE_NOT_ENDING_IN_CONF" must work too.
258          */
259         if (depth != 0) {
260                 ext = strrchr(base, '.');
261                 if (ext == NULL || strcmp(ext + 1, "conf"))
262                         goto error;
263         }
264
265         p = config_open2(filename, fopen_for_read);
266         if (p == NULL) {
267                 rc = FALSE;
268                 goto error;
269         }
270
271         while (config_read(p, tokens, 3, 2, "# \t", PARSE_NORMAL)) {
272 //Use index_in_strings?
273                 if (strcmp(tokens[0], "alias") == 0) {
274                         /* alias <wildcard> <modulename> */
275                         llist_t *l;
276                         char wildcard[MODULE_NAME_LEN];
277                         char *rmod;
278
279                         if (tokens[2] == NULL)
280                                 continue;
281                         filename2modname(tokens[1], wildcard);
282
283                         for (l = G.probes; l; l = l->link) {
284                                 m = (struct module_entry *) l->data;
285                                 if (fnmatch(wildcard, m->modname, 0) != 0)
286                                         continue;
287                                 rmod = filename2modname(tokens[2], NULL);
288                                 llist_add_to(&m->realnames, rmod);
289
290                                 if (m->flags & MODULE_FLAG_NEED_DEPS) {
291                                         m->flags &= ~MODULE_FLAG_NEED_DEPS;
292                                         G.num_unresolved_deps--;
293                                 }
294
295                                 m = get_or_add_modentry(rmod);
296                                 if (!(m->flags & MODULE_FLAG_NEED_DEPS)) {
297                                         m->flags |= MODULE_FLAG_NEED_DEPS;
298                                         G.num_unresolved_deps++;
299                                 }
300                         }
301                 } else if (strcmp(tokens[0], "options") == 0) {
302                         /* options <modulename> <option...> */
303                         if (tokens[2] == NULL)
304                                 continue;
305                         m = get_or_add_modentry(tokens[1]);
306                         m->options = gather_options_str(m->options, tokens[2]);
307                 } else if (strcmp(tokens[0], "include") == 0) {
308                         /* include <filename>/<dirname> (yes, directories also must work) */
309                         read_config(tokens[1]);
310                 } else if (ENABLE_FEATURE_MODPROBE_BLACKLIST
311                  && strcmp(tokens[0], "blacklist") == 0
312                 ) {
313                         /* blacklist <modulename> */
314                         get_or_add_modentry(tokens[1])->flags |= MODULE_FLAG_BLACKLISTED;
315                 }
316         }
317         config_close(p);
318  error:
319         return rc;
320 }
321
322 static int read_config(const char *path)
323 {
324         return recursive_action(path, ACTION_RECURSE | ACTION_QUIET,
325                                 config_file_action, NULL, NULL,
326                                 /*depth:*/ 0);
327 }
328
329 static const char *humanly_readable_name(struct module_entry *m)
330 {
331         /* probed_name may be NULL. modname always exists. */
332         return m->probed_name ? m->probed_name : m->modname;
333 }
334
335 /* Like strsep(&stringp, "\n\t ") but quoted text goes to single token
336  * even if it contains whitespace.
337  */
338 static char *strsep_quotes(char **stringp)
339 {
340         char *s, *start = *stringp;
341
342         if (!start)
343                 return NULL;
344
345         for (s = start; ; s++) {
346                 switch (*s) {
347                 case '"':
348                         s = strchrnul(s + 1, '"'); /* find trailing quote */
349                         if (*s != '\0')
350                                 s++; /* skip trailing quote */
351                         /* fall through */
352                 case '\0':
353                 case '\n':
354                 case '\t':
355                 case ' ':
356                         if (*s != '\0') {
357                                 *s = '\0';
358                                 *stringp = s + 1;
359                         } else {
360                                 *stringp = NULL;
361                         }
362                         return start;
363                 }
364         }
365 }
366
367 static char *parse_and_add_kcmdline_module_options(char *options, const char *modulename)
368 {
369         char *kcmdline_buf;
370         char *kcmdline;
371         char *kptr;
372
373         kcmdline_buf = xmalloc_open_read_close("/proc/cmdline", NULL);
374         if (!kcmdline_buf)
375                 return options;
376
377         kcmdline = kcmdline_buf;
378         while ((kptr = strsep_quotes(&kcmdline)) != NULL) {
379                 char *after_modulename = is_prefixed_with(kptr, modulename);
380                 if (!after_modulename || *after_modulename != '.')
381                         continue;
382                 /* It is "modulename.xxxx" */
383                 kptr = after_modulename + 1;
384                 if (strchr(kptr, '=') != NULL) {
385                         /* It is "modulename.opt=[val]" */
386                         options = gather_options_str(options, kptr);
387                 }
388         }
389         free(kcmdline_buf);
390
391         return options;
392 }
393
394 /* Return: similar to bb_init_module:
395  * 0 on success,
396  * -errno on open/read error,
397  * errno on init_module() error
398  */
399 /* NB: INSMOD_OPT_SILENT bit suppresses ONLY non-existent modules,
400  * not deleted ones (those are still listed in modules.dep).
401  * module-init-tools version 3.4:
402  * # modprobe bogus
403  * FATAL: Module bogus not found. [exitcode 1]
404  * # modprobe -q bogus            [silent, exitcode still 1]
405  * but:
406  * # rm kernel/drivers/net/dummy.ko
407  * # modprobe -q dummy
408  * FATAL: Could not open '/lib/modules/xxx/kernel/drivers/net/dummy.ko': No such file or directory
409  * [exitcode 1]
410  */
411 static int do_modprobe(struct module_entry *m)
412 {
413         int rc, first;
414
415         if (!(m->flags & MODULE_FLAG_FOUND_IN_MODDEP)) {
416                 if (!(option_mask32 & INSMOD_OPT_SILENT))
417                         bb_error_msg((m->flags & MODULE_FLAG_BUILTIN) ?
418                                      "module %s is builtin" :
419                                      "module %s not found in modules.dep",
420                                      humanly_readable_name(m));
421                 return -ENOENT;
422         }
423         DBG("do_modprob'ing %s", m->modname);
424
425         if (!(option_mask32 & OPT_REMOVE))
426                 m->deps = llist_rev(m->deps);
427
428         if (0) {
429                 llist_t *l;
430                 for (l = m->deps; l; l = l->link)
431                         DBG("dep: %s", l->data);
432         }
433
434         first = 1;
435         rc = 0;
436         while (m->deps) {
437                 struct module_entry *m2;
438                 char *fn, *options;
439
440                 rc = 0;
441                 fn = llist_pop(&m->deps); /* we leak it */
442                 m2 = get_or_add_modentry(bb_get_last_path_component_nostrip(fn));
443
444                 if (option_mask32 & OPT_REMOVE) {
445                         /* modprobe -r */
446                         if (m2->flags & MODULE_FLAG_LOADED) {
447                                 rc = bb_delete_module(m2->modname, O_EXCL);
448                                 if (rc) {
449                                         if (first) {
450                                                 bb_perror_msg("can't unload module '%s'",
451                                                         humanly_readable_name(m2));
452                                                 break;
453                                         }
454                                 } else {
455                                         m2->flags &= ~MODULE_FLAG_LOADED;
456                                 }
457                         }
458                         /* do not error out if *deps* fail to unload */
459                         first = 0;
460                         continue;
461                 }
462
463                 options = m2->options;
464                 m2->options = NULL;
465                 options = parse_and_add_kcmdline_module_options(options, m2->modname);
466 #if ENABLE_FEATURE_CMDLINE_MODULE_OPTIONS
467                 if (m == m2)
468                         options = gather_options_str(options, G.cmdline_mopts);
469 #endif
470
471                 if (option_mask32 & OPT_SHOW_DEPS) {
472                         printf(options ? "insmod %s/%s/%s %s\n"
473                                         : "insmod %s/%s/%s\n",
474                                 CONFIG_DEFAULT_MODULES_DIR, G.uts.release, fn,
475                                 options);
476                         free(options);
477                         continue;
478                 }
479
480                 if (m2->flags & MODULE_FLAG_LOADED) {
481                         DBG("%s is already loaded, skipping", fn);
482                         free(options);
483                         continue;
484                 }
485
486                 rc = bb_init_module(fn, options);
487                 DBG("loaded %s '%s', rc:%d", fn, options, rc);
488                 if (rc == EEXIST)
489                         rc = 0;
490                 free(options);
491                 if (rc) {
492                         bb_error_msg("can't load module %s (%s): %s",
493                                 humanly_readable_name(m2),
494                                 fn,
495                                 moderror(rc)
496                         );
497                         break;
498                 }
499                 m2->flags |= MODULE_FLAG_LOADED;
500         }
501
502         return rc;
503 }
504
505 static void load_modules_dep(void)
506 {
507         struct module_entry *m;
508         char *colon, *tokens[2];
509         parser_t *p;
510
511         /* Modprobe does not work at all without modules.dep,
512          * even if the full module name is given. Returning error here
513          * was making us later confuse user with this message:
514          * "module /full/path/to/existing/file/module.ko not found".
515          * It's better to die immediately, with good message.
516          * xfopen_for_read provides that. */
517         p = config_open2(CONFIG_DEFAULT_DEPMOD_FILE, xfopen_for_read);
518
519         while (G.num_unresolved_deps
520          && config_read(p, tokens, 2, 1, "# \t", PARSE_NORMAL)
521         ) {
522                 colon = last_char_is(tokens[0], ':');
523                 if (colon == NULL)
524                         continue;
525                 *colon = '\0';
526
527                 m = moddb_get(&G.db, bb_get_last_path_component_nostrip(tokens[0]));
528                 if (m == NULL)
529                         continue;
530
531                 /* Optimization... */
532                 if ((m->flags & MODULE_FLAG_LOADED)
533                  && !(option_mask32 & (OPT_REMOVE | OPT_SHOW_DEPS))
534                 ) {
535                         DBG("skip deps of %s, it's already loaded", tokens[0]);
536                         continue;
537                 }
538
539                 m->flags |= MODULE_FLAG_FOUND_IN_MODDEP;
540                 if ((m->flags & MODULE_FLAG_NEED_DEPS) && (m->deps == NULL)) {
541                         G.num_unresolved_deps--;
542                         llist_add_to(&m->deps, xstrdup(tokens[0]));
543                         if (tokens[1])
544                                 string_to_llist(tokens[1], &m->deps, " \t");
545                 } else
546                         DBG("skipping dep line");
547         }
548         config_close(p);
549 }
550
551 int modprobe_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
552 int modprobe_main(int argc UNUSED_PARAM, char **argv)
553 {
554         int rc;
555         unsigned opt;
556         struct module_entry *me;
557
558         INIT_G();
559
560         IF_LONG_OPTS(applet_long_options = modprobe_longopts;)
561         opt_complementary = MODPROBE_COMPLEMENTARY;
562         opt = getopt32(argv, INSMOD_OPTS MODPROBE_OPTS INSMOD_ARGS);
563         argv += optind;
564
565         /* Goto modules location */
566         xchdir(CONFIG_DEFAULT_MODULES_DIR);
567         uname(&G.uts);
568         xchdir(G.uts.release);
569
570         if (opt & OPT_LIST_ONLY) {
571                 int i;
572                 char *colon, *tokens[2];
573                 parser_t *p = config_open2(CONFIG_DEFAULT_DEPMOD_FILE, xfopen_for_read);
574
575                 for (i = 0; argv[i]; i++)
576                         replace(argv[i], '-', '_');
577
578                 while (config_read(p, tokens, 2, 1, "# \t", PARSE_NORMAL)) {
579                         colon = last_char_is(tokens[0], ':');
580                         if (!colon)
581                                 continue;
582                         *colon = '\0';
583                         if (!argv[0])
584                                 puts(tokens[0]);
585                         else {
586                                 char name[MODULE_NAME_LEN];
587                                 filename2modname(
588                                         bb_get_last_path_component_nostrip(tokens[0]),
589                                         name
590                                 );
591                                 for (i = 0; argv[i]; i++) {
592                                         if (fnmatch(argv[i], name, 0) == 0) {
593                                                 puts(tokens[0]);
594                                         }
595                                 }
596                         }
597                 }
598                 return EXIT_SUCCESS;
599         }
600
601         /* Yes, for some reason -l ignores -s... */
602         if (opt & INSMOD_OPT_SYSLOG)
603                 logmode = LOGMODE_SYSLOG;
604
605         if (!argv[0]) {
606                 if (opt & OPT_REMOVE) {
607                         /* "modprobe -r" (w/o params).
608                          * "If name is NULL, all unused modules marked
609                          * autoclean will be removed".
610                          */
611                         if (bb_delete_module(NULL, O_NONBLOCK | O_EXCL) != 0)
612                                 bb_perror_nomsg_and_die();
613                 }
614                 return EXIT_SUCCESS;
615         }
616
617         /* Retrieve module names of already loaded modules */
618         {
619                 char *s;
620                 parser_t *parser = config_open2("/proc/modules", fopen_for_read);
621                 while (config_read(parser, &s, 1, 1, "# \t", PARSE_NORMAL & ~PARSE_GREEDY))
622                         get_or_add_modentry(s)->flags |= MODULE_FLAG_LOADED;
623                 config_close(parser);
624
625                 parser = config_open2("modules.builtin", fopen_for_read);
626                 while (config_read(parser, &s, 1, 1, "# \t", PARSE_NORMAL))
627                         get_or_add_modentry(s)->flags |= MODULE_FLAG_BUILTIN;
628                 config_close(parser);
629         }
630
631         if (opt & (OPT_INSERT_ALL | OPT_REMOVE)) {
632                 /* Each argument is a module name */
633                 do {
634                         DBG("adding module %s", *argv);
635                         add_probe(*argv++);
636                 } while (*argv);
637         } else {
638                 /* First argument is module name, rest are parameters */
639                 DBG("probing just module %s", *argv);
640                 add_probe(argv[0]);
641 #if ENABLE_FEATURE_CMDLINE_MODULE_OPTIONS
642                 G.cmdline_mopts = parse_cmdline_module_options(argv, /*quote_spaces:*/ 1);
643 #endif
644         }
645
646         /* Happens if all requested modules are already loaded */
647         if (G.probes == NULL)
648                 return EXIT_SUCCESS;
649
650         read_config("/etc/modprobe.conf");
651         read_config("/etc/modprobe.d");
652         if (ENABLE_FEATURE_MODUTILS_SYMBOLS && G.need_symbols)
653                 read_config("modules.symbols");
654         load_modules_dep();
655         if (ENABLE_FEATURE_MODUTILS_ALIAS && G.num_unresolved_deps) {
656                 read_config("modules.alias");
657                 load_modules_dep();
658         }
659
660         rc = 0;
661         while ((me = llist_pop(&G.probes)) != NULL) {
662                 if (me->realnames == NULL) {
663                         DBG("probing by module name");
664                         /* This is not an alias. Literal names are blacklisted
665                          * only if '-b' is given.
666                          */
667                         if (!(opt & OPT_BLACKLIST)
668                          || !(me->flags & MODULE_FLAG_BLACKLISTED)
669                         ) {
670                                 rc |= do_modprobe(me);
671                         }
672                         continue;
673                 }
674
675                 /* Probe all real names for the alias */
676                 do {
677                         char *realname = llist_pop(&me->realnames);
678                         struct module_entry *m2;
679
680                         DBG("probing alias %s by realname %s", me->modname, realname);
681                         m2 = get_or_add_modentry(realname);
682                         if (!(m2->flags & MODULE_FLAG_BLACKLISTED)
683                          && (!(m2->flags & MODULE_FLAG_LOADED)
684                             || (opt & (OPT_REMOVE | OPT_SHOW_DEPS)))
685                         ) {
686 //TODO: we can pass "me" as 2nd param to do_modprobe,
687 //and make do_modprobe emit more meaningful error messages
688 //with alias name included, not just module name alias resolves to.
689                                 rc |= do_modprobe(m2);
690                         }
691                         free(realname);
692                 } while (me->realnames != NULL);
693         }
694
695         if (ENABLE_FEATURE_CLEAN_UP)
696                 moddb_free(&G.db);
697
698         return (rc != 0);
699 }