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