modprobe,rmmod: reject module names with slashes
[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 //#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
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 struct module_entry { /* I'll call it ME. */
151         unsigned flags;
152         char *modname; /* stripped of /path/, .ext and s/-/_/g */
153         const char *probed_name; /* verbatim as seen on cmdline */
154         char *options; /* options from config files */
155         llist_t *realnames; /* strings. if this module is an alias, */
156         /* real module name is one of these. */
157 //Can there really be more than one? Example from real kernel?
158         llist_t *deps; /* strings. modules we depend on */
159 };
160
161 #define DB_HASH_SIZE 256
162
163 struct globals {
164         llist_t *probes; /* MEs of module(s) requested on cmdline */
165         char *cmdline_mopts; /* module options from cmdline */
166         int num_unresolved_deps;
167         /* bool. "Did we have 'symbol:FOO' requested on cmdline?" */
168         smallint need_symbols;
169         struct utsname uts;
170         llist_t *db[DB_HASH_SIZE]; /* MEs of all modules ever seen (caching for speed) */
171 } FIX_ALIASING;
172 #define G (*ptr_to_globals)
173 #define INIT_G() do { \
174         SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
175 } while (0)
176
177
178 static int read_config(const char *path);
179
180 static char *gather_options_str(char *opts, const char *append)
181 {
182         /* Speed-optimized. We call gather_options_str many times. */
183         if (append) {
184                 if (opts == NULL) {
185                         opts = xstrdup(append);
186                 } else {
187                         int optlen = strlen(opts);
188                         opts = xrealloc(opts, optlen + strlen(append) + 2);
189                         sprintf(opts + optlen, " %s", append);
190                 }
191         }
192         return opts;
193 }
194
195 /* These three functions called many times, optimizing for speed.
196  * Users reported minute-long delays when they runn iptables repeatedly
197  * (iptables use modprobe to install needed kernel modules).
198  */
199 static struct module_entry *helper_get_module(const char *module, int create)
200 {
201         char modname[MODULE_NAME_LEN];
202         struct module_entry *e;
203         llist_t *l;
204         unsigned i;
205         unsigned hash;
206
207         filename2modname(module, modname);
208
209         hash = 0;
210         for (i = 0; modname[i]; i++)
211                 hash = ((hash << 5) + hash) + modname[i];
212         hash %= DB_HASH_SIZE;
213
214         for (l = G.db[hash]; l; l = l->link) {
215                 e = (struct module_entry *) l->data;
216                 if (strcmp(e->modname, modname) == 0)
217                         return e;
218         }
219         if (!create)
220                 return NULL;
221
222         e = xzalloc(sizeof(*e));
223         e->modname = xstrdup(modname);
224         llist_add_to(&G.db[hash], e);
225
226         return e;
227 }
228 static ALWAYS_INLINE struct module_entry *get_or_add_modentry(const char *module)
229 {
230         return helper_get_module(module, 1);
231 }
232 static ALWAYS_INLINE struct module_entry *get_modentry(const char *module)
233 {
234         return helper_get_module(module, 0);
235 }
236
237 static void add_probe(const char *name)
238 {
239         struct module_entry *m;
240
241         /*
242          * get_or_add_modentry() strips path from name and works
243          * on remaining basename.
244          * This would make "rmmod dir/name" and "modprobe dir/name"
245          * to work like "rmmod name" and "modprobe name",
246          * which is wrong, and can be abused via implicit modprobing:
247          * "ifconfig /usbserial up" tries to modprobe netdev-/usbserial.
248          */
249         if (strchr(name, '/'))
250                 bb_error_msg_and_die("malformed module name '%s'", name);
251
252         m = get_or_add_modentry(name);
253         if (!(option_mask32 & (OPT_REMOVE | OPT_SHOW_DEPS))
254          && (m->flags & MODULE_FLAG_LOADED)
255         ) {
256                 DBG("skipping %s, it is already loaded", name);
257                 return;
258         }
259
260         DBG("queuing %s", name);
261         m->probed_name = name;
262         m->flags |= MODULE_FLAG_NEED_DEPS;
263         llist_add_to_end(&G.probes, m);
264         G.num_unresolved_deps++;
265         if (ENABLE_FEATURE_MODUTILS_SYMBOLS
266          && strncmp(m->modname, "symbol:", 7) == 0
267         ) {
268                 G.need_symbols = 1;
269         }
270 }
271
272 static int FAST_FUNC config_file_action(const char *filename,
273                                         struct stat *statbuf UNUSED_PARAM,
274                                         void *userdata UNUSED_PARAM,
275                                         int depth UNUSED_PARAM)
276 {
277         char *tokens[3];
278         parser_t *p;
279         struct module_entry *m;
280         int rc = TRUE;
281
282         if (bb_basename(filename)[0] == '.')
283                 goto error;
284
285         p = config_open2(filename, fopen_for_read);
286         if (p == NULL) {
287                 rc = FALSE;
288                 goto error;
289         }
290
291         while (config_read(p, tokens, 3, 2, "# \t", PARSE_NORMAL)) {
292 //Use index_in_strings?
293                 if (strcmp(tokens[0], "alias") == 0) {
294                         /* alias <wildcard> <modulename> */
295                         llist_t *l;
296                         char wildcard[MODULE_NAME_LEN];
297                         char *rmod;
298
299                         if (tokens[2] == NULL)
300                                 continue;
301                         filename2modname(tokens[1], wildcard);
302
303                         for (l = G.probes; l; l = l->link) {
304                                 m = (struct module_entry *) l->data;
305                                 if (fnmatch(wildcard, m->modname, 0) != 0)
306                                         continue;
307                                 rmod = filename2modname(tokens[2], NULL);
308                                 llist_add_to(&m->realnames, rmod);
309
310                                 if (m->flags & MODULE_FLAG_NEED_DEPS) {
311                                         m->flags &= ~MODULE_FLAG_NEED_DEPS;
312                                         G.num_unresolved_deps--;
313                                 }
314
315                                 m = get_or_add_modentry(rmod);
316                                 if (!(m->flags & MODULE_FLAG_NEED_DEPS)) {
317                                         m->flags |= MODULE_FLAG_NEED_DEPS;
318                                         G.num_unresolved_deps++;
319                                 }
320                         }
321                 } else if (strcmp(tokens[0], "options") == 0) {
322                         /* options <modulename> <option...> */
323                         if (tokens[2] == NULL)
324                                 continue;
325                         m = get_or_add_modentry(tokens[1]);
326                         m->options = gather_options_str(m->options, tokens[2]);
327                 } else if (strcmp(tokens[0], "include") == 0) {
328                         /* include <filename> */
329                         read_config(tokens[1]);
330                 } else if (ENABLE_FEATURE_MODPROBE_BLACKLIST
331                  && strcmp(tokens[0], "blacklist") == 0
332                 ) {
333                         /* blacklist <modulename> */
334                         get_or_add_modentry(tokens[1])->flags |= MODULE_FLAG_BLACKLISTED;
335                 }
336         }
337         config_close(p);
338  error:
339         return rc;
340 }
341
342 static int read_config(const char *path)
343 {
344         return recursive_action(path, ACTION_RECURSE | ACTION_QUIET,
345                                 config_file_action, NULL, NULL, 1);
346 }
347
348 static const char *humanly_readable_name(struct module_entry *m)
349 {
350         /* probed_name may be NULL. modname always exists. */
351         return m->probed_name ? m->probed_name : m->modname;
352 }
353
354 static char *parse_and_add_kcmdline_module_options(char *options, const char *modulename)
355 {
356         char *kcmdline_buf;
357         char *kcmdline;
358         char *kptr;
359         int len;
360
361         kcmdline_buf = xmalloc_open_read_close("/proc/cmdline", NULL);
362         if (!kcmdline_buf)
363                 return options;
364
365         kcmdline = kcmdline_buf;
366         len = strlen(modulename);
367         while ((kptr = strsep(&kcmdline, "\n\t ")) != NULL) {
368                 if (strncmp(modulename, kptr, len) != 0)
369                         continue;
370                 kptr += len;
371                 if (*kptr != '.')
372                         continue;
373                 /* It is "modulename.xxxx" */
374                 kptr++;
375                 if (strchr(kptr, '=') != NULL) {
376                         /* It is "modulename.opt=[val]" */
377                         options = gather_options_str(options, kptr);
378                 }
379         }
380         free(kcmdline_buf);
381
382         return options;
383 }
384
385 /* Return: similar to bb_init_module:
386  * 0 on success,
387  * -errno on open/read error,
388  * errno on init_module() error
389  */
390 /* NB: INSMOD_OPT_SILENT bit suppresses ONLY non-existent modules,
391  * not deleted ones (those are still listed in modules.dep).
392  * module-init-tools version 3.4:
393  * # modprobe bogus
394  * FATAL: Module bogus not found. [exitcode 1]
395  * # modprobe -q bogus            [silent, exitcode still 1]
396  * but:
397  * # rm kernel/drivers/net/dummy.ko
398  * # modprobe -q dummy
399  * FATAL: Could not open '/lib/modules/xxx/kernel/drivers/net/dummy.ko': No such file or directory
400  * [exitcode 1]
401  */
402 static int do_modprobe(struct module_entry *m)
403 {
404         int rc, first;
405
406         if (!(m->flags & MODULE_FLAG_FOUND_IN_MODDEP)) {
407                 if (!(option_mask32 & INSMOD_OPT_SILENT))
408                         bb_error_msg("module %s not found in modules.dep",
409                                 humanly_readable_name(m));
410                 return -ENOENT;
411         }
412         DBG("do_modprob'ing %s", m->modname);
413
414         if (!(option_mask32 & OPT_REMOVE))
415                 m->deps = llist_rev(m->deps);
416
417         if (0) {
418                 llist_t *l;
419                 for (l = m->deps; l; l = l->link)
420                         DBG("dep: %s", l->data);
421         }
422
423         first = 1;
424         rc = 0;
425         while (m->deps) {
426                 struct module_entry *m2;
427                 char *fn, *options;
428
429                 rc = 0;
430                 fn = llist_pop(&m->deps); /* we leak it */
431                 m2 = get_or_add_modentry(fn);
432
433                 if (option_mask32 & OPT_REMOVE) {
434                         /* modprobe -r */
435                         if (m2->flags & MODULE_FLAG_LOADED) {
436                                 rc = bb_delete_module(m2->modname, O_EXCL);
437                                 if (rc) {
438                                         if (first) {
439                                                 bb_error_msg("can't unload module %s: %s",
440                                                         humanly_readable_name(m2),
441                                                         moderror(rc));
442                                                 break;
443                                         }
444                                 } else {
445                                         m2->flags &= ~MODULE_FLAG_LOADED;
446                                 }
447                         }
448                         /* do not error out if *deps* fail to unload */
449                         first = 0;
450                         continue;
451                 }
452
453                 options = m2->options;
454                 m2->options = NULL;
455                 options = parse_and_add_kcmdline_module_options(options, m2->modname);
456                 if (m == m2)
457                         options = gather_options_str(options, G.cmdline_mopts);
458
459                 if (option_mask32 & OPT_SHOW_DEPS) {
460                         printf(options ? "insmod %s/%s/%s %s\n"
461                                         : "insmod %s/%s/%s\n",
462                                 CONFIG_DEFAULT_MODULES_DIR, G.uts.release, fn,
463                                 options);
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
546         INIT_G();
547
548         IF_LONG_OPTS(applet_long_options = modprobe_longopts;)
549         opt_complementary = MODPROBE_COMPLEMENTARY;
550         opt = getopt32(argv, INSMOD_OPTS MODPROBE_OPTS INSMOD_ARGS);
551         argv += optind;
552
553         /* Goto modules location */
554         xchdir(CONFIG_DEFAULT_MODULES_DIR);
555         uname(&G.uts);
556         xchdir(G.uts.release);
557
558         if (opt & OPT_LIST_ONLY) {
559                 int i;
560                 char name[MODULE_NAME_LEN];
561                 char *colon, *tokens[2];
562                 parser_t *p = config_open2(CONFIG_DEFAULT_DEPMOD_FILE, xfopen_for_read);
563
564                 for (i = 0; argv[i]; i++)
565                         replace(argv[i], '-', '_');
566
567                 while (config_read(p, tokens, 2, 1, "# \t", PARSE_NORMAL)) {
568                         colon = last_char_is(tokens[0], ':');
569                         if (!colon)
570                                 continue;
571                         *colon = '\0';
572                         filename2modname(tokens[0], name);
573                         if (!argv[0])
574                                 puts(tokens[0]);
575                         else {
576                                 for (i = 0; argv[i]; i++) {
577                                         if (fnmatch(argv[i], name, 0) == 0) {
578                                                 puts(tokens[0]);
579                                         }
580                                 }
581                         }
582                 }
583                 return EXIT_SUCCESS;
584         }
585
586         /* Yes, for some reason -l ignores -s... */
587         if (opt & INSMOD_OPT_SYSLOG)
588                 logmode = LOGMODE_SYSLOG;
589
590         if (!argv[0]) {
591                 if (opt & OPT_REMOVE) {
592                         /* "modprobe -r" (w/o params).
593                          * "If name is NULL, all unused modules marked
594                          * autoclean will be removed".
595                          */
596                         if (bb_delete_module(NULL, O_NONBLOCK | O_EXCL) != 0)
597                                 bb_perror_msg_and_die("rmmod");
598                 }
599                 return EXIT_SUCCESS;
600         }
601
602         /* Retrieve module names of already loaded modules */
603         {
604                 char *s;
605                 parser_t *parser = config_open2("/proc/modules", fopen_for_read);
606                 while (config_read(parser, &s, 1, 1, "# \t", PARSE_NORMAL & ~PARSE_GREEDY))
607                         get_or_add_modentry(s)->flags |= MODULE_FLAG_LOADED;
608                 config_close(parser);
609         }
610
611         if (opt & (OPT_INSERT_ALL | OPT_REMOVE)) {
612                 /* Each argument is a module name */
613                 do {
614                         DBG("adding module %s", *argv);
615                         add_probe(*argv++);
616                 } while (*argv);
617         } else {
618                 /* First argument is module name, rest are parameters */
619                 DBG("probing just module %s", *argv);
620                 add_probe(argv[0]);
621                 G.cmdline_mopts = parse_cmdline_module_options(argv, /*quote_spaces:*/ 1);
622         }
623
624         /* Happens if all requested modules are already loaded */
625         if (G.probes == NULL)
626                 return EXIT_SUCCESS;
627
628         read_config("/etc/modprobe.conf");
629         read_config("/etc/modprobe.d");
630         if (ENABLE_FEATURE_MODUTILS_SYMBOLS && G.need_symbols)
631                 read_config("modules.symbols");
632         load_modules_dep();
633         if (ENABLE_FEATURE_MODUTILS_ALIAS && G.num_unresolved_deps) {
634                 read_config("modules.alias");
635                 load_modules_dep();
636         }
637
638         rc = 0;
639         while ((me = llist_pop(&G.probes)) != NULL) {
640                 if (me->realnames == NULL) {
641                         DBG("probing by module name");
642                         /* This is not an alias. Literal names are blacklisted
643                          * only if '-b' is given.
644                          */
645                         if (!(opt & OPT_BLACKLIST)
646                          || !(me->flags & MODULE_FLAG_BLACKLISTED)
647                         ) {
648                                 rc |= do_modprobe(me);
649                         }
650                         continue;
651                 }
652
653                 /* Probe all real names for the alias */
654                 do {
655                         char *realname = llist_pop(&me->realnames);
656                         struct module_entry *m2;
657
658                         DBG("probing alias %s by realname %s", me->modname, realname);
659                         m2 = get_or_add_modentry(realname);
660                         if (!(m2->flags & MODULE_FLAG_BLACKLISTED)
661                          && (!(m2->flags & MODULE_FLAG_LOADED)
662                             || (opt & (OPT_REMOVE | OPT_SHOW_DEPS)))
663                         ) {
664 //TODO: we can pass "me" as 2nd param to do_modprobe,
665 //and make do_modprobe emit more meaningful error messages
666 //with alias name included, not just module name alias resolves to.
667                                 rc |= do_modprobe(m2);
668                         }
669                         free(realname);
670                 } while (me->realnames != NULL);
671         }
672
673         return (rc != 0);
674 }