blob: a5cf4babf2aa31204d36099298e1bdf2b1e99f09 [file] [log] [blame]
Eric Andersen0139ca92001-07-22 23:01:03 +00001/* vi: set sw=4 ts=4: */
2/*
Robert Grieblaead70b2002-07-26 15:54:20 +00003 * Modprobe written from scratch for BusyBox
Eric Andersen60e56f52002-04-26 06:04:01 +00004 *
Denis Vlasenkoba1315d2008-09-13 14:59:38 +00005 * Copyright (c) 2008 Timo Teras <timo.teras@iki.fi>
6 * Copyright (c) 2008 Vladimir Dronnikov
Robert Griebl6859d762002-08-05 02:57:12 +00007 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02008 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Denis Vlasenkoba1315d2008-09-13 14:59:38 +00009 */
Eric Andersen0139ca92001-07-22 23:01:03 +000010
Denys Vlasenkoc15613c2010-10-15 11:29:02 +020011//applet:IF_MODPROBE(APPLET(modprobe, _BB_DIR_SBIN, _BB_SUID_DROP))
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +000012
Pascal Bellardb82b34e2010-06-07 01:16:45 +020013//usage:#if !ENABLE_MODPROBE_SMALL
14//usage:#define modprobe_notes_usage
15//usage: "modprobe can (un)load a stack of modules, passing each module options (when\n"
16//usage: "loading). modprobe uses a configuration file to determine what option(s) to\n"
17//usage: "pass each module it loads.\n"
18//usage: "\n"
19//usage: "The configuration file is searched (in this order):\n"
20//usage: "\n"
21//usage: " /etc/modprobe.conf (2.6 only)\n"
22//usage: " /etc/modules.conf\n"
23//usage: " /etc/conf.modules (deprecated)\n"
24//usage: "\n"
25//usage: "They all have the same syntax (see below). If none is present, it is\n"
26//usage: "_not_ an error; each loaded module is then expected to load without\n"
27//usage: "options. Once a file is found, the others are tested for.\n"
28//usage: "\n"
29//usage: "/etc/modules.conf entry format:\n"
30//usage: "\n"
31//usage: " alias <alias_name> <mod_name>\n"
32//usage: " Makes it possible to modprobe alias_name, when there is no such module.\n"
33//usage: " It makes sense if your mod_name is long, or you want a more representative\n"
34//usage: " name for that module (eg. 'scsi' in place of 'aha7xxx').\n"
35//usage: " This makes it also possible to use a different set of options (below) for\n"
36//usage: " the module and the alias.\n"
37//usage: " A module can be aliased more than once.\n"
38//usage: "\n"
39//usage: " options <mod_name|alias_name> <symbol=value...>\n"
40//usage: " When loading module mod_name (or the module aliased by alias_name), pass\n"
41//usage: " the \"symbol=value\" pairs as option to that module.\n"
42//usage: "\n"
43//usage: "Sample /etc/modules.conf file:\n"
44//usage: "\n"
45//usage: " options tulip irq=3\n"
46//usage: " alias tulip tulip2\n"
47//usage: " options tulip2 irq=4 io=0x308\n"
48//usage: "\n"
49//usage: "Other functionality offered by 'classic' modprobe is not available in\n"
50//usage: "this implementation.\n"
51//usage: "\n"
52//usage: "If module options are present both in the config file, and on the command line,\n"
53//usage: "then the options from the command line will be passed to the module _after_\n"
54//usage: "the options from the config file. That way, you can have defaults in the config\n"
55//usage: "file, and override them for a specific usage from the command line.\n"
56//usage:#define modprobe_example_usage
57//usage: "(with the above /etc/modules.conf):\n\n"
58//usage: "$ modprobe tulip\n"
59//usage: " will load the module 'tulip' with default option 'irq=3'\n\n"
60//usage: "$ modprobe tulip irq=5\n"
61//usage: " will load the module 'tulip' with option 'irq=5', thus overriding the default\n\n"
62//usage: "$ modprobe tulip2\n"
63//usage: " will load the module 'tulip' with default options 'irq=4 io=0x308',\n"
64//usage: " which are the default for alias 'tulip2'\n\n"
65//usage: "$ modprobe tulip2 irq=8\n"
66//usage: " will load the module 'tulip' with default options 'irq=4 io=0x308 irq=8',\n"
67//usage: " which are the default for alias 'tulip2' overridden by the option 'irq=8'\n\n"
68//usage: " from the command line\n\n"
69//usage: "$ modprobe tulip2 irq=2 io=0x210\n"
70//usage: " will load the module 'tulip' with default options 'irq=4 io=0x308 irq=4 io=0x210',\n"
71//usage: " which are the default for alias 'tulip2' overridden by the options 'irq=2 io=0x210'\n\n"
72//usage: " from the command line\n"
73//usage:
74//usage:#define modprobe_trivial_usage
75//usage: "[-alrqvs"
76//usage: IF_FEATURE_MODPROBE_BLACKLIST("b")
77//usage: "] MODULE [symbol=value]..."
78//usage:#define modprobe_full_usage "\n\n"
79//usage: "Options:"
80//usage: "\n -a Load multiple MODULEs"
81//usage: "\n -l List (MODULE is a pattern)"
82//usage: "\n -r Remove MODULE (stacks) or do autoclean"
83//usage: "\n -q Quiet"
84//usage: "\n -v Verbose"
85//usage: "\n -s Log to syslog"
86//usage: IF_FEATURE_MODPROBE_BLACKLIST(
87//usage: "\n -b Apply blacklist to module names too"
88//usage: )
89//usage:#endif /* !ENABLE_MODPROBE_SMALL */
90
Denys Vlasenkoc15613c2010-10-15 11:29:02 +020091#include "libbb.h"
92#include "modutils.h"
93#include <sys/utsname.h>
94#include <fnmatch.h>
95
96//#define DBG(fmt, ...) bb_error_msg("%s: " fmt, __func__, ## __VA_ARGS__)
97#define DBG(...) ((void)0)
98
99/* Note that unlike older versions of modules.dep/depmod (busybox and m-i-t),
100 * we expect the full dependency list to be specified in modules.dep.
101 * Older versions would only export the direct dependency list.
102 */
103
Pascal Bellardb82b34e2010-06-07 01:16:45 +0200104/* Note that usage text doesn't document various 2.4 options
105 * we pull in through INSMOD_OPTS define */
106
107#define MODPROBE_COMPLEMENTARY "q-v:v-q:l--ar:a--lr:r--al"
108#define MODPROBE_OPTS "alr" IF_FEATURE_MODPROBE_BLACKLIST("b")
109//#define MODPROBE_COMPLEMENTARY "q-v:v-q:l--acr:a--lr:r--al"
110//#define MODPROBE_OPTS "acd:lnrt:C:" IF_FEATURE_MODPROBE_BLACKLIST("b")
111enum {
112 MODPROBE_OPT_INSERT_ALL = (INSMOD_OPT_UNUSED << 0), /* a */
113 //MODPROBE_OPT_DUMP_ONLY= (INSMOD_OPT_UNUSED << x), /* c */
114 //MODPROBE_OPT_DIRNAME = (INSMOD_OPT_UNUSED << x), /* d */
115 MODPROBE_OPT_LIST_ONLY = (INSMOD_OPT_UNUSED << 1), /* l */
116 //MODPROBE_OPT_SHOW_ONLY= (INSMOD_OPT_UNUSED << x), /* n */
117 MODPROBE_OPT_REMOVE = (INSMOD_OPT_UNUSED << 2), /* r */
118 //MODPROBE_OPT_RESTRICT = (INSMOD_OPT_UNUSED << x), /* t */
119 //MODPROBE_OPT_VERONLY = (INSMOD_OPT_UNUSED << x), /* V */
120 //MODPROBE_OPT_CONFIGFILE=(INSMOD_OPT_UNUSED << x), /* C */
121 MODPROBE_OPT_BLACKLIST = (INSMOD_OPT_UNUSED << 3) * ENABLE_FEATURE_MODPROBE_BLACKLIST,
122};
123
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200124#define MODULE_FLAG_LOADED 0x0001
125#define MODULE_FLAG_NEED_DEPS 0x0002
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000126/* "was seen in modules.dep": */
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200127#define MODULE_FLAG_FOUND_IN_MODDEP 0x0004
128#define MODULE_FLAG_BLACKLISTED 0x0008
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000129
130struct module_entry { /* I'll call it ME. */
131 unsigned flags;
132 char *modname; /* stripped of /path/, .ext and s/-/_/g */
133 const char *probed_name; /* verbatim as seen on cmdline */
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000134 char *options; /* options from config files */
135 llist_t *realnames; /* strings. if this module is an alias, */
136 /* real module name is one of these. */
137//Can there really be more than one? Example from real kernel?
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000138 llist_t *deps; /* strings. modules we depend on */
Eric Andersen60e56f52002-04-26 06:04:01 +0000139};
140
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000141struct globals {
142 llist_t *db; /* MEs of all modules ever seen (caching for speed) */
143 llist_t *probes; /* MEs of module(s) requested on cmdline */
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000144 char *cmdline_mopts; /* module options from cmdline */
145 int num_unresolved_deps;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000146 /* bool. "Did we have 'symbol:FOO' requested on cmdline?" */
147 smallint need_symbols;
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100148} FIX_ALIASING;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000149#define G (*(struct globals*)&bb_common_bufsiz1)
150#define INIT_G() do { } while (0)
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000151
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000152
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000153static int read_config(const char *path);
154
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000155static char *gather_options_str(char *opts, const char *append)
156{
157 /* Speed-optimized. We call gather_options_str many times. */
Denys Vlasenkoda879ec2010-01-26 08:17:45 +0100158 if (append) {
159 if (opts == NULL) {
160 opts = xstrdup(append);
161 } else {
162 int optlen = strlen(opts);
163 opts = xrealloc(opts, optlen + strlen(append) + 2);
164 sprintf(opts + optlen, " %s", append);
165 }
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000166 }
167 return opts;
168}
169
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000170static struct module_entry *helper_get_module(const char *module, int create)
Denis Vlasenkof45c4f42008-06-16 04:09:25 +0000171{
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000172 char modname[MODULE_NAME_LEN];
173 struct module_entry *e;
174 llist_t *l;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000175
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000176 filename2modname(module, modname);
177 for (l = G.db; l != NULL; l = l->link) {
178 e = (struct module_entry *) l->data;
179 if (strcmp(e->modname, modname) == 0)
180 return e;
181 }
182 if (!create)
183 return NULL;
184
185 e = xzalloc(sizeof(*e));
186 e->modname = xstrdup(modname);
187 llist_add_to(&G.db, e);
188
189 return e;
190}
191static struct module_entry *get_or_add_modentry(const char *module)
192{
193 return helper_get_module(module, 1);
194}
195static struct module_entry *get_modentry(const char *module)
196{
197 return helper_get_module(module, 0);
198}
199
200static void add_probe(const char *name)
201{
202 struct module_entry *m;
203
204 m = get_or_add_modentry(name);
Denys Vlasenko140def82009-05-26 12:48:34 +0200205 if (!(option_mask32 & MODPROBE_OPT_REMOVE)
206 && (m->flags & MODULE_FLAG_LOADED)
207 ) {
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000208 DBG("skipping %s, it is already loaded", name);
209 return;
210 }
211
Mike Frysinger00ffaea2009-05-05 20:13:45 -0400212 DBG("queuing %s", name);
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000213 m->probed_name = name;
214 m->flags |= MODULE_FLAG_NEED_DEPS;
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000215 llist_add_to_end(&G.probes, m);
216 G.num_unresolved_deps++;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000217 if (ENABLE_FEATURE_MODUTILS_SYMBOLS
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000218 && strncmp(m->modname, "symbol:", 7) == 0
219 ) {
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000220 G.need_symbols = 1;
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000221 }
Denis Vlasenko9ddc8d52008-05-18 14:39:43 +0000222}
223
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000224static int FAST_FUNC config_file_action(const char *filename,
225 struct stat *statbuf UNUSED_PARAM,
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000226 void *userdata UNUSED_PARAM,
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000227 int depth UNUSED_PARAM)
Rob Landley72615752006-04-10 16:09:52 +0000228{
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000229 char *tokens[3];
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000230 parser_t *p;
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200231 struct module_entry *m;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000232 int rc = TRUE;
Denis Vlasenkoe1ee48e2008-07-29 00:19:44 +0000233
234 if (bb_basename(filename)[0] == '.')
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000235 goto error;
Denis Vlasenkoe1ee48e2008-07-29 00:19:44 +0000236
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000237 p = config_open2(filename, fopen_for_read);
238 if (p == NULL) {
239 rc = FALSE;
240 goto error;
241 }
Rob Landley72615752006-04-10 16:09:52 +0000242
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000243 while (config_read(p, tokens, 3, 2, "# \t", PARSE_NORMAL)) {
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000244//Use index_in_strings?
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000245 if (strcmp(tokens[0], "alias") == 0) {
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000246 /* alias <wildcard> <modulename> */
247 llist_t *l;
248 char wildcard[MODULE_NAME_LEN];
249 char *rmod;
250
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000251 if (tokens[2] == NULL)
252 continue;
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000253 filename2modname(tokens[1], wildcard);
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000254
255 for (l = G.probes; l != NULL; l = l->link) {
256 m = (struct module_entry *) l->data;
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000257 if (fnmatch(wildcard, m->modname, 0) != 0)
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000258 continue;
259 rmod = filename2modname(tokens[2], NULL);
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000260 llist_add_to(&m->realnames, rmod);
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000261
262 if (m->flags & MODULE_FLAG_NEED_DEPS) {
263 m->flags &= ~MODULE_FLAG_NEED_DEPS;
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000264 G.num_unresolved_deps--;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000265 }
266
267 m = get_or_add_modentry(rmod);
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000268 if (!(m->flags & MODULE_FLAG_NEED_DEPS)) {
269 m->flags |= MODULE_FLAG_NEED_DEPS;
270 G.num_unresolved_deps++;
271 }
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000272 }
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000273 } else if (strcmp(tokens[0], "options") == 0) {
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000274 /* options <modulename> <option...> */
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000275 if (tokens[2] == NULL)
276 continue;
277 m = get_or_add_modentry(tokens[1]);
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000278 m->options = gather_options_str(m->options, tokens[2]);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000279 } else if (strcmp(tokens[0], "include") == 0) {
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000280 /* include <filename> */
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000281 read_config(tokens[1]);
282 } else if (ENABLE_FEATURE_MODPROBE_BLACKLIST
283 && strcmp(tokens[0], "blacklist") == 0
284 ) {
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000285 /* blacklist <modulename> */
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000286 get_or_add_modentry(tokens[1])->flags |= MODULE_FLAG_BLACKLISTED;
Rob Landley72615752006-04-10 16:09:52 +0000287 }
Rob Landleye9190962005-12-12 19:38:44 +0000288 }
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000289 config_close(p);
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200290 error:
Robert Griebl52e8d062002-05-14 23:42:08 +0000291 return rc;
292}
293
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000294static int read_config(const char *path)
Robert Griebl52e8d062002-05-14 23:42:08 +0000295{
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000296 return recursive_action(path, ACTION_RECURSE | ACTION_QUIET,
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000297 config_file_action, NULL, NULL, 1);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000298}
Eric Andersen3b1a7442003-12-24 20:30:45 +0000299
Denys Vlasenko7eaf58d2009-06-17 20:33:50 +0200300static const char *humanly_readable_name(struct module_entry *m)
301{
302 /* probed_name may be NULL. modname always exists. */
303 return m->probed_name ? m->probed_name : m->modname;
304}
305
Denys Vlasenko3e26d4f2010-02-27 23:15:22 +0100306static char *parse_and_add_kcmdline_module_options(char *options, const char *modulename)
307{
Denys Vlasenko197202d2010-03-03 04:20:33 +0100308 char *kcmdline_buf;
Denys Vlasenko3e26d4f2010-02-27 23:15:22 +0100309 char *kcmdline;
310 char *kptr;
311 int len;
312
Denys Vlasenko197202d2010-03-03 04:20:33 +0100313 kcmdline_buf = xmalloc_open_read_close("/proc/cmdline", NULL);
314 if (!kcmdline_buf)
Denys Vlasenko3e26d4f2010-02-27 23:15:22 +0100315 return options;
Denys Vlasenko3e26d4f2010-02-27 23:15:22 +0100316
Denys Vlasenko3e26d4f2010-02-27 23:15:22 +0100317 kcmdline = kcmdline_buf;
Denys Vlasenko197202d2010-03-03 04:20:33 +0100318 len = strlen(modulename);
Denys Vlasenko3e26d4f2010-02-27 23:15:22 +0100319 while ((kptr = strsep(&kcmdline, "\n\t ")) != NULL) {
320 if (strncmp(modulename, kptr, len) != 0)
321 continue;
322 kptr += len;
323 if (*kptr != '.')
324 continue;
325 /* It is "modulename.xxxx" */
326 kptr++;
327 if (strchr(kptr, '=') != NULL) {
328 /* It is "modulename.opt=[val]" */
329 options = gather_options_str(options, kptr);
330 }
331 }
Denys Vlasenko197202d2010-03-03 04:20:33 +0100332 free(kcmdline_buf);
Denys Vlasenko3e26d4f2010-02-27 23:15:22 +0100333
334 return options;
335}
336
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200337/* Return: similar to bb_init_module:
338 * 0 on success,
339 * -errno on open/read error,
340 * errno on init_module() error
341 */
Pascal Bellardb82b34e2010-06-07 01:16:45 +0200342/* NB: INSMOD_OPT_SILENT bit suppresses ONLY non-existent modules,
343 * not deleted ones (those are still listed in modules.dep).
344 * module-init-tools version 3.4:
345 * # modprobe bogus
346 * FATAL: Module bogus not found. [exitcode 1]
347 * # modprobe -q bogus [silent, exitcode still 1]
348 * but:
349 * # rm kernel/drivers/net/dummy.ko
350 * # modprobe -q dummy
351 * FATAL: Could not open '/lib/modules/xxx/kernel/drivers/net/dummy.ko': No such file or directory
352 * [exitcode 1]
353 */
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000354static int do_modprobe(struct module_entry *m)
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000355{
Denys Vlasenkof3cbfc02009-05-28 03:54:29 +0200356 struct module_entry *m2 = m2; /* for compiler */
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000357 char *fn, *options;
Denys Vlasenkof3cbfc02009-05-28 03:54:29 +0200358 int rc, first;
Mike Frysinger00ffaea2009-05-05 20:13:45 -0400359 llist_t *l;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000360
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000361 if (!(m->flags & MODULE_FLAG_FOUND_IN_MODDEP)) {
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200362 if (!(option_mask32 & INSMOD_OPT_SILENT))
Denys Vlasenko7eaf58d2009-06-17 20:33:50 +0200363 bb_error_msg("module %s not found in modules.dep",
364 humanly_readable_name(m));
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000365 return -ENOENT;
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000366 }
367 DBG("do_modprob'ing %s", m->modname);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000368
369 if (!(option_mask32 & MODPROBE_OPT_REMOVE))
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000370 m->deps = llist_rev(m->deps);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000371
Mike Frysinger00ffaea2009-05-05 20:13:45 -0400372 for (l = m->deps; l != NULL; l = l->link)
373 DBG("dep: %s", l->data);
374
Denys Vlasenkof3cbfc02009-05-28 03:54:29 +0200375 first = 1;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000376 rc = 0;
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200377 while (m->deps) {
378 rc = 0;
379 fn = llist_pop(&m->deps); /* we leak it */
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000380 m2 = get_or_add_modentry(fn);
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200381
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000382 if (option_mask32 & MODPROBE_OPT_REMOVE) {
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200383 /* modprobe -r */
Denys Vlasenkof3cbfc02009-05-28 03:54:29 +0200384 if (m2->flags & MODULE_FLAG_LOADED) {
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200385 rc = bb_delete_module(m2->modname, O_EXCL);
386 if (rc) {
387 if (first) {
Denys Vlasenko651a2692010-03-23 16:25:17 +0100388 bb_error_msg("can't unload module %s: %s",
Denys Vlasenko7eaf58d2009-06-17 20:33:50 +0200389 humanly_readable_name(m2),
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200390 moderror(rc));
391 break;
392 }
Denys Vlasenkof3cbfc02009-05-28 03:54:29 +0200393 } else {
394 m2->flags &= ~MODULE_FLAG_LOADED;
395 }
396 }
397 /* do not error out if *deps* fail to unload */
398 first = 0;
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200399 continue;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000400 }
401
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200402 if (m2->flags & MODULE_FLAG_LOADED) {
403 DBG("%s is already loaded, skipping", fn);
404 continue;
405 }
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000406
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200407 options = m2->options;
408 m2->options = NULL;
Denys Vlasenko3e26d4f2010-02-27 23:15:22 +0100409 options = parse_and_add_kcmdline_module_options(options, m2->modname);
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200410 if (m == m2)
411 options = gather_options_str(options, G.cmdline_mopts);
412 rc = bb_init_module(fn, options);
413 DBG("loaded %s '%s', rc:%d", fn, options, rc);
Denys Vlasenko725b5a32010-01-10 04:52:45 +0100414 if (rc == EEXIST)
415 rc = 0;
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200416 free(options);
417 if (rc) {
Denys Vlasenko651a2692010-03-23 16:25:17 +0100418 bb_error_msg("can't load module %s (%s): %s",
Denys Vlasenko7eaf58d2009-06-17 20:33:50 +0200419 humanly_readable_name(m2),
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200420 fn,
421 moderror(rc)
422 );
423 break;
424 }
425 m2->flags |= MODULE_FLAG_LOADED;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000426 }
Denis Vlasenko9604e1b2009-03-03 18:47:56 +0000427
Eric Andersen908e3622003-06-20 09:56:37 +0000428 return rc;
Robert Griebl52e8d062002-05-14 23:42:08 +0000429}
430
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000431static void load_modules_dep(void)
432{
433 struct module_entry *m;
434 char *colon, *tokens[2];
435 parser_t *p;
436
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200437 /* Modprobe does not work at all without modules.dep,
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000438 * even if the full module name is given. Returning error here
439 * was making us later confuse user with this message:
440 * "module /full/path/to/existing/file/module.ko not found".
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000441 * It's better to die immediately, with good message.
442 * xfopen_for_read provides that. */
443 p = config_open2(CONFIG_DEFAULT_DEPMOD_FILE, xfopen_for_read);
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000444
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000445 while (G.num_unresolved_deps
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000446 && config_read(p, tokens, 2, 1, "# \t", PARSE_NORMAL)
447 ) {
448 colon = last_char_is(tokens[0], ':');
449 if (colon == NULL)
450 continue;
451 *colon = 0;
452
453 m = get_modentry(tokens[0]);
454 if (m == NULL)
455 continue;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000456
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000457 /* Optimization... */
458 if ((m->flags & MODULE_FLAG_LOADED)
459 && !(option_mask32 & MODPROBE_OPT_REMOVE)
460 ) {
461 DBG("skip deps of %s, it's already loaded", tokens[0]);
462 continue;
463 }
464
465 m->flags |= MODULE_FLAG_FOUND_IN_MODDEP;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000466 if ((m->flags & MODULE_FLAG_NEED_DEPS) && (m->deps == NULL)) {
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000467 G.num_unresolved_deps--;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000468 llist_add_to(&m->deps, xstrdup(tokens[0]));
469 if (tokens[1])
Denys Vlasenko5da42fc2009-08-14 02:10:54 +0200470 string_to_llist(tokens[1], &m->deps, " \t");
Mike Frysinger00ffaea2009-05-05 20:13:45 -0400471 } else
472 DBG("skipping dep line");
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000473 }
474 config_close(p);
475}
476
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000477int modprobe_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko9ddc0042008-08-06 00:51:43 +0000478int modprobe_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen0139ca92001-07-22 23:01:03 +0000479{
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000480 struct utsname uts;
Denis Vlasenkobb26db42008-10-31 02:04:28 +0000481 int rc;
482 unsigned opt;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000483 struct module_entry *me;
Eric Andersen0139ca92001-07-22 23:01:03 +0000484
Pascal Bellardb82b34e2010-06-07 01:16:45 +0200485 opt_complementary = MODPROBE_COMPLEMENTARY;
486 opt = getopt32(argv, INSMOD_OPTS MODPROBE_OPTS INSMOD_ARGS);
Denis Vlasenko9ddc0042008-08-06 00:51:43 +0000487 argv += optind;
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000488
Lauri Kasanena48a29f2010-07-08 11:22:30 +0300489 /* Goto modules location */
490 xchdir(CONFIG_DEFAULT_MODULES_DIR);
491 uname(&uts);
492 xchdir(uts.release);
493
Pascal Bellardb82b34e2010-06-07 01:16:45 +0200494 if (opt & MODPROBE_OPT_LIST_ONLY) {
495 char name[MODULE_NAME_LEN];
496 char *colon, *tokens[2];
497 parser_t *p = config_open2(CONFIG_DEFAULT_DEPMOD_FILE, xfopen_for_read);
498
499 while (config_read(p, tokens, 2, 1, "# \t", PARSE_NORMAL)) {
500 colon = last_char_is(tokens[0], ':');
501 if (!colon)
502 continue;
503 *colon = '\0';
504 filename2modname(tokens[0], name);
505 if (!argv[0])
506 puts(tokens[0]);
507 else {
508 int i;
509 for (i = 0; argv[i]; i++) {
510 if (fnmatch(argv[i], name, 0) == 0) {
511 puts(tokens[0]);
512 }
513 }
514 }
515 }
516 return EXIT_SUCCESS;
517 }
518
519 /* Yes, for some reason -l ignores -s... */
520 if (opt & INSMOD_OPT_SYSLOG)
521 logmode = LOGMODE_SYSLOG;
Robert Griebl236abbf2002-05-22 23:34:35 +0000522
Denis Vlasenkobb26db42008-10-31 02:04:28 +0000523 if (!argv[0]) {
524 if (opt & MODPROBE_OPT_REMOVE) {
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000525 /* "modprobe -r" (w/o params).
526 * "If name is NULL, all unused modules marked
527 * autoclean will be removed".
528 */
Denys Vlasenkoab19ede2009-11-11 21:05:42 +0100529 if (bb_delete_module(NULL, O_NONBLOCK | O_EXCL) != 0)
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000530 bb_perror_msg_and_die("rmmod");
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000531 }
Denis Vlasenkobb26db42008-10-31 02:04:28 +0000532 return EXIT_SUCCESS;
533 }
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000534
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000535 /* Retrieve module names of already loaded modules */
Denis Vlasenkobb26db42008-10-31 02:04:28 +0000536 {
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000537 char *s;
Denis Vlasenkobb26db42008-10-31 02:04:28 +0000538 parser_t *parser = config_open2("/proc/modules", fopen_for_read);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000539 while (config_read(parser, &s, 1, 1, "# \t", PARSE_NORMAL & ~PARSE_GREEDY))
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000540 get_or_add_modentry(s)->flags |= MODULE_FLAG_LOADED;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000541 config_close(parser);
542 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000543
Denys Vlasenko140def82009-05-26 12:48:34 +0200544 if (opt & (MODPROBE_OPT_INSERT_ALL | MODPROBE_OPT_REMOVE)) {
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000545 /* Each argument is a module name */
546 do {
Mike Frysinger00ffaea2009-05-05 20:13:45 -0400547 DBG("adding module %s", *argv);
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000548 add_probe(*argv++);
549 } while (*argv);
550 } else {
551 /* First argument is module name, rest are parameters */
Mike Frysinger00ffaea2009-05-05 20:13:45 -0400552 DBG("probing just module %s", *argv);
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000553 add_probe(argv[0]);
554 G.cmdline_mopts = parse_cmdline_module_options(argv);
555 }
556
557 /* Happens if all requested modules are already loaded */
558 if (G.probes == NULL)
559 return EXIT_SUCCESS;
560
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000561 read_config("/etc/modprobe.conf");
562 read_config("/etc/modprobe.d");
563 if (ENABLE_FEATURE_MODUTILS_SYMBOLS && G.need_symbols)
564 read_config("modules.symbols");
565 load_modules_dep();
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000566 if (ENABLE_FEATURE_MODUTILS_ALIAS && G.num_unresolved_deps) {
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000567 read_config("modules.alias");
568 load_modules_dep();
569 }
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000570
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200571 rc = 0;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000572 while ((me = llist_pop(&G.probes)) != NULL) {
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000573 if (me->realnames == NULL) {
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200574 DBG("probing by module name");
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000575 /* This is not an alias. Literal names are blacklisted
576 * only if '-b' is given.
577 */
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000578 if (!(opt & MODPROBE_OPT_BLACKLIST)
579 || !(me->flags & MODULE_FLAG_BLACKLISTED)
580 ) {
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200581 rc |= do_modprobe(me);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000582 }
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200583 continue;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000584 }
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200585
586 /* Probe all real names for the alias */
587 do {
588 char *realname = llist_pop(&me->realnames);
589 struct module_entry *m2;
590
591 DBG("probing alias %s by realname %s", me->modname, realname);
592 m2 = get_or_add_modentry(realname);
593 if (!(m2->flags & MODULE_FLAG_BLACKLISTED)
594 && (!(m2->flags & MODULE_FLAG_LOADED)
595 || (opt & MODPROBE_OPT_REMOVE))
596 ) {
597//TODO: we can pass "me" as 2nd param to do_modprobe,
598//and make do_modprobe emit more meaningful error messages
599//with alias name included, not just module name alias resolves to.
600 rc |= do_modprobe(m2);
601 }
602 free(realname);
603 } while (me->realnames != NULL);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000604 }
605
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200606 return (rc != 0);
Eric Andersen0139ca92001-07-22 23:01:03 +0000607}