blob: b4de65b1f5d668d600bdb165d75f5def819db789 [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 *
Rob Landley79e1cab2005-11-15 00:08:29 +00008 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Denis Vlasenkoba1315d2008-09-13 14:59:38 +00009 */
Eric Andersen0139ca92001-07-22 23:01:03 +000010
Mike Frysinger1e12ae92009-05-06 05:24:41 -040011/* Note that unlike older versions of modules.dep/depmod (busybox and m-i-t),
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +020012 * we expect the full dependency list to be specified in modules.dep.
13 * Older versions would only export the direct dependency list.
Mike Frysinger1e12ae92009-05-06 05:24:41 -040014 */
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000015#include "libbb.h"
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000016#include "modutils.h"
Robert Griebl52e8d062002-05-14 23:42:08 +000017#include <sys/utsname.h>
Bernhard Reutner-Fischer2c351a82006-06-03 19:08:49 +000018#include <fnmatch.h>
Eric Andersen0139ca92001-07-22 23:01:03 +000019
Mike Frysinger00ffaea2009-05-05 20:13:45 -040020//#define DBG(fmt, ...) bb_error_msg("%s: " fmt, __func__, ## __VA_ARGS__)
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +000021#define DBG(...) ((void)0)
22
Pascal Bellardb82b34e2010-06-07 01:16:45 +020023//usage:#if !ENABLE_MODPROBE_SMALL
24//usage:#define modprobe_notes_usage
25//usage: "modprobe can (un)load a stack of modules, passing each module options (when\n"
26//usage: "loading). modprobe uses a configuration file to determine what option(s) to\n"
27//usage: "pass each module it loads.\n"
28//usage: "\n"
29//usage: "The configuration file is searched (in this order):\n"
30//usage: "\n"
31//usage: " /etc/modprobe.conf (2.6 only)\n"
32//usage: " /etc/modules.conf\n"
33//usage: " /etc/conf.modules (deprecated)\n"
34//usage: "\n"
35//usage: "They all have the same syntax (see below). If none is present, it is\n"
36//usage: "_not_ an error; each loaded module is then expected to load without\n"
37//usage: "options. Once a file is found, the others are tested for.\n"
38//usage: "\n"
39//usage: "/etc/modules.conf entry format:\n"
40//usage: "\n"
41//usage: " alias <alias_name> <mod_name>\n"
42//usage: " Makes it possible to modprobe alias_name, when there is no such module.\n"
43//usage: " It makes sense if your mod_name is long, or you want a more representative\n"
44//usage: " name for that module (eg. 'scsi' in place of 'aha7xxx').\n"
45//usage: " This makes it also possible to use a different set of options (below) for\n"
46//usage: " the module and the alias.\n"
47//usage: " A module can be aliased more than once.\n"
48//usage: "\n"
49//usage: " options <mod_name|alias_name> <symbol=value...>\n"
50//usage: " When loading module mod_name (or the module aliased by alias_name), pass\n"
51//usage: " the \"symbol=value\" pairs as option to that module.\n"
52//usage: "\n"
53//usage: "Sample /etc/modules.conf file:\n"
54//usage: "\n"
55//usage: " options tulip irq=3\n"
56//usage: " alias tulip tulip2\n"
57//usage: " options tulip2 irq=4 io=0x308\n"
58//usage: "\n"
59//usage: "Other functionality offered by 'classic' modprobe is not available in\n"
60//usage: "this implementation.\n"
61//usage: "\n"
62//usage: "If module options are present both in the config file, and on the command line,\n"
63//usage: "then the options from the command line will be passed to the module _after_\n"
64//usage: "the options from the config file. That way, you can have defaults in the config\n"
65//usage: "file, and override them for a specific usage from the command line.\n"
66//usage:#define modprobe_example_usage
67//usage: "(with the above /etc/modules.conf):\n\n"
68//usage: "$ modprobe tulip\n"
69//usage: " will load the module 'tulip' with default option 'irq=3'\n\n"
70//usage: "$ modprobe tulip irq=5\n"
71//usage: " will load the module 'tulip' with option 'irq=5', thus overriding the default\n\n"
72//usage: "$ modprobe tulip2\n"
73//usage: " will load the module 'tulip' with default options 'irq=4 io=0x308',\n"
74//usage: " which are the default for alias 'tulip2'\n\n"
75//usage: "$ modprobe tulip2 irq=8\n"
76//usage: " will load the module 'tulip' with default options 'irq=4 io=0x308 irq=8',\n"
77//usage: " which are the default for alias 'tulip2' overridden by the option 'irq=8'\n\n"
78//usage: " from the command line\n\n"
79//usage: "$ modprobe tulip2 irq=2 io=0x210\n"
80//usage: " will load the module 'tulip' with default options 'irq=4 io=0x308 irq=4 io=0x210',\n"
81//usage: " which are the default for alias 'tulip2' overridden by the options 'irq=2 io=0x210'\n\n"
82//usage: " from the command line\n"
83//usage:
84//usage:#define modprobe_trivial_usage
85//usage: "[-alrqvs"
86//usage: IF_FEATURE_MODPROBE_BLACKLIST("b")
87//usage: "] MODULE [symbol=value]..."
88//usage:#define modprobe_full_usage "\n\n"
89//usage: "Options:"
90//usage: "\n -a Load multiple MODULEs"
91//usage: "\n -l List (MODULE is a pattern)"
92//usage: "\n -r Remove MODULE (stacks) or do autoclean"
93//usage: "\n -q Quiet"
94//usage: "\n -v Verbose"
95//usage: "\n -s Log to syslog"
96//usage: IF_FEATURE_MODPROBE_BLACKLIST(
97//usage: "\n -b Apply blacklist to module names too"
98//usage: )
99//usage:#endif /* !ENABLE_MODPROBE_SMALL */
100
101/* Note that usage text doesn't document various 2.4 options
102 * we pull in through INSMOD_OPTS define */
103
104#define MODPROBE_COMPLEMENTARY "q-v:v-q:l--ar:a--lr:r--al"
105#define MODPROBE_OPTS "alr" IF_FEATURE_MODPROBE_BLACKLIST("b")
106//#define MODPROBE_COMPLEMENTARY "q-v:v-q:l--acr:a--lr:r--al"
107//#define MODPROBE_OPTS "acd:lnrt:C:" IF_FEATURE_MODPROBE_BLACKLIST("b")
108enum {
109 MODPROBE_OPT_INSERT_ALL = (INSMOD_OPT_UNUSED << 0), /* a */
110 //MODPROBE_OPT_DUMP_ONLY= (INSMOD_OPT_UNUSED << x), /* c */
111 //MODPROBE_OPT_DIRNAME = (INSMOD_OPT_UNUSED << x), /* d */
112 MODPROBE_OPT_LIST_ONLY = (INSMOD_OPT_UNUSED << 1), /* l */
113 //MODPROBE_OPT_SHOW_ONLY= (INSMOD_OPT_UNUSED << x), /* n */
114 MODPROBE_OPT_REMOVE = (INSMOD_OPT_UNUSED << 2), /* r */
115 //MODPROBE_OPT_RESTRICT = (INSMOD_OPT_UNUSED << x), /* t */
116 //MODPROBE_OPT_VERONLY = (INSMOD_OPT_UNUSED << x), /* V */
117 //MODPROBE_OPT_CONFIGFILE=(INSMOD_OPT_UNUSED << x), /* C */
118 MODPROBE_OPT_BLACKLIST = (INSMOD_OPT_UNUSED << 3) * ENABLE_FEATURE_MODPROBE_BLACKLIST,
119};
120
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200121#define MODULE_FLAG_LOADED 0x0001
122#define MODULE_FLAG_NEED_DEPS 0x0002
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000123/* "was seen in modules.dep": */
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200124#define MODULE_FLAG_FOUND_IN_MODDEP 0x0004
125#define MODULE_FLAG_BLACKLISTED 0x0008
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000126
127struct module_entry { /* I'll call it ME. */
128 unsigned flags;
129 char *modname; /* stripped of /path/, .ext and s/-/_/g */
130 const char *probed_name; /* verbatim as seen on cmdline */
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000131 char *options; /* options from config files */
132 llist_t *realnames; /* strings. if this module is an alias, */
133 /* real module name is one of these. */
134//Can there really be more than one? Example from real kernel?
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000135 llist_t *deps; /* strings. modules we depend on */
Eric Andersen60e56f52002-04-26 06:04:01 +0000136};
137
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000138struct globals {
139 llist_t *db; /* MEs of all modules ever seen (caching for speed) */
140 llist_t *probes; /* MEs of module(s) requested on cmdline */
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000141 char *cmdline_mopts; /* module options from cmdline */
142 int num_unresolved_deps;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000143 /* bool. "Did we have 'symbol:FOO' requested on cmdline?" */
144 smallint need_symbols;
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100145} FIX_ALIASING;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000146#define G (*(struct globals*)&bb_common_bufsiz1)
147#define INIT_G() do { } while (0)
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000148
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000149
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000150static int read_config(const char *path);
151
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000152static char *gather_options_str(char *opts, const char *append)
153{
154 /* Speed-optimized. We call gather_options_str many times. */
Denys Vlasenkoda879ec2010-01-26 08:17:45 +0100155 if (append) {
156 if (opts == NULL) {
157 opts = xstrdup(append);
158 } else {
159 int optlen = strlen(opts);
160 opts = xrealloc(opts, optlen + strlen(append) + 2);
161 sprintf(opts + optlen, " %s", append);
162 }
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000163 }
164 return opts;
165}
166
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000167static struct module_entry *helper_get_module(const char *module, int create)
Denis Vlasenkof45c4f42008-06-16 04:09:25 +0000168{
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000169 char modname[MODULE_NAME_LEN];
170 struct module_entry *e;
171 llist_t *l;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000172
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000173 filename2modname(module, modname);
174 for (l = G.db; l != NULL; l = l->link) {
175 e = (struct module_entry *) l->data;
176 if (strcmp(e->modname, modname) == 0)
177 return e;
178 }
179 if (!create)
180 return NULL;
181
182 e = xzalloc(sizeof(*e));
183 e->modname = xstrdup(modname);
184 llist_add_to(&G.db, e);
185
186 return e;
187}
188static struct module_entry *get_or_add_modentry(const char *module)
189{
190 return helper_get_module(module, 1);
191}
192static struct module_entry *get_modentry(const char *module)
193{
194 return helper_get_module(module, 0);
195}
196
197static void add_probe(const char *name)
198{
199 struct module_entry *m;
200
201 m = get_or_add_modentry(name);
Denys Vlasenko140def82009-05-26 12:48:34 +0200202 if (!(option_mask32 & MODPROBE_OPT_REMOVE)
203 && (m->flags & MODULE_FLAG_LOADED)
204 ) {
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000205 DBG("skipping %s, it is already loaded", name);
206 return;
207 }
208
Mike Frysinger00ffaea2009-05-05 20:13:45 -0400209 DBG("queuing %s", name);
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000210 m->probed_name = name;
211 m->flags |= MODULE_FLAG_NEED_DEPS;
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000212 llist_add_to_end(&G.probes, m);
213 G.num_unresolved_deps++;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000214 if (ENABLE_FEATURE_MODUTILS_SYMBOLS
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000215 && strncmp(m->modname, "symbol:", 7) == 0
216 ) {
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000217 G.need_symbols = 1;
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000218 }
Denis Vlasenko9ddc8d52008-05-18 14:39:43 +0000219}
220
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000221static int FAST_FUNC config_file_action(const char *filename,
222 struct stat *statbuf UNUSED_PARAM,
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000223 void *userdata UNUSED_PARAM,
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000224 int depth UNUSED_PARAM)
Rob Landley72615752006-04-10 16:09:52 +0000225{
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000226 char *tokens[3];
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000227 parser_t *p;
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200228 struct module_entry *m;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000229 int rc = TRUE;
Denis Vlasenkoe1ee48e2008-07-29 00:19:44 +0000230
231 if (bb_basename(filename)[0] == '.')
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000232 goto error;
Denis Vlasenkoe1ee48e2008-07-29 00:19:44 +0000233
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000234 p = config_open2(filename, fopen_for_read);
235 if (p == NULL) {
236 rc = FALSE;
237 goto error;
238 }
Rob Landley72615752006-04-10 16:09:52 +0000239
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000240 while (config_read(p, tokens, 3, 2, "# \t", PARSE_NORMAL)) {
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000241//Use index_in_strings?
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000242 if (strcmp(tokens[0], "alias") == 0) {
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000243 /* alias <wildcard> <modulename> */
244 llist_t *l;
245 char wildcard[MODULE_NAME_LEN];
246 char *rmod;
247
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000248 if (tokens[2] == NULL)
249 continue;
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000250 filename2modname(tokens[1], wildcard);
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000251
252 for (l = G.probes; l != NULL; l = l->link) {
253 m = (struct module_entry *) l->data;
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000254 if (fnmatch(wildcard, m->modname, 0) != 0)
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000255 continue;
256 rmod = filename2modname(tokens[2], NULL);
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000257 llist_add_to(&m->realnames, rmod);
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000258
259 if (m->flags & MODULE_FLAG_NEED_DEPS) {
260 m->flags &= ~MODULE_FLAG_NEED_DEPS;
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000261 G.num_unresolved_deps--;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000262 }
263
264 m = get_or_add_modentry(rmod);
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000265 if (!(m->flags & MODULE_FLAG_NEED_DEPS)) {
266 m->flags |= MODULE_FLAG_NEED_DEPS;
267 G.num_unresolved_deps++;
268 }
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000269 }
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000270 } else if (strcmp(tokens[0], "options") == 0) {
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000271 /* options <modulename> <option...> */
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000272 if (tokens[2] == NULL)
273 continue;
274 m = get_or_add_modentry(tokens[1]);
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000275 m->options = gather_options_str(m->options, tokens[2]);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000276 } else if (strcmp(tokens[0], "include") == 0) {
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000277 /* include <filename> */
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000278 read_config(tokens[1]);
279 } else if (ENABLE_FEATURE_MODPROBE_BLACKLIST
280 && strcmp(tokens[0], "blacklist") == 0
281 ) {
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000282 /* blacklist <modulename> */
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000283 get_or_add_modentry(tokens[1])->flags |= MODULE_FLAG_BLACKLISTED;
Rob Landley72615752006-04-10 16:09:52 +0000284 }
Rob Landleye9190962005-12-12 19:38:44 +0000285 }
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000286 config_close(p);
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200287 error:
Robert Griebl52e8d062002-05-14 23:42:08 +0000288 return rc;
289}
290
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000291static int read_config(const char *path)
Robert Griebl52e8d062002-05-14 23:42:08 +0000292{
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000293 return recursive_action(path, ACTION_RECURSE | ACTION_QUIET,
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000294 config_file_action, NULL, NULL, 1);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000295}
Eric Andersen3b1a7442003-12-24 20:30:45 +0000296
Denys Vlasenko7eaf58d2009-06-17 20:33:50 +0200297static const char *humanly_readable_name(struct module_entry *m)
298{
299 /* probed_name may be NULL. modname always exists. */
300 return m->probed_name ? m->probed_name : m->modname;
301}
302
Denys Vlasenko3e26d4f2010-02-27 23:15:22 +0100303static char *parse_and_add_kcmdline_module_options(char *options, const char *modulename)
304{
Denys Vlasenko197202d2010-03-03 04:20:33 +0100305 char *kcmdline_buf;
Denys Vlasenko3e26d4f2010-02-27 23:15:22 +0100306 char *kcmdline;
307 char *kptr;
308 int len;
309
Denys Vlasenko197202d2010-03-03 04:20:33 +0100310 kcmdline_buf = xmalloc_open_read_close("/proc/cmdline", NULL);
311 if (!kcmdline_buf)
Denys Vlasenko3e26d4f2010-02-27 23:15:22 +0100312 return options;
Denys Vlasenko3e26d4f2010-02-27 23:15:22 +0100313
Denys Vlasenko3e26d4f2010-02-27 23:15:22 +0100314 kcmdline = kcmdline_buf;
Denys Vlasenko197202d2010-03-03 04:20:33 +0100315 len = strlen(modulename);
Denys Vlasenko3e26d4f2010-02-27 23:15:22 +0100316 while ((kptr = strsep(&kcmdline, "\n\t ")) != NULL) {
317 if (strncmp(modulename, kptr, len) != 0)
318 continue;
319 kptr += len;
320 if (*kptr != '.')
321 continue;
322 /* It is "modulename.xxxx" */
323 kptr++;
324 if (strchr(kptr, '=') != NULL) {
325 /* It is "modulename.opt=[val]" */
326 options = gather_options_str(options, kptr);
327 }
328 }
Denys Vlasenko197202d2010-03-03 04:20:33 +0100329 free(kcmdline_buf);
Denys Vlasenko3e26d4f2010-02-27 23:15:22 +0100330
331 return options;
332}
333
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200334/* Return: similar to bb_init_module:
335 * 0 on success,
336 * -errno on open/read error,
337 * errno on init_module() error
338 */
Pascal Bellardb82b34e2010-06-07 01:16:45 +0200339/* NB: INSMOD_OPT_SILENT bit suppresses ONLY non-existent modules,
340 * not deleted ones (those are still listed in modules.dep).
341 * module-init-tools version 3.4:
342 * # modprobe bogus
343 * FATAL: Module bogus not found. [exitcode 1]
344 * # modprobe -q bogus [silent, exitcode still 1]
345 * but:
346 * # rm kernel/drivers/net/dummy.ko
347 * # modprobe -q dummy
348 * FATAL: Could not open '/lib/modules/xxx/kernel/drivers/net/dummy.ko': No such file or directory
349 * [exitcode 1]
350 */
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000351static int do_modprobe(struct module_entry *m)
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000352{
Denys Vlasenkof3cbfc02009-05-28 03:54:29 +0200353 struct module_entry *m2 = m2; /* for compiler */
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000354 char *fn, *options;
Denys Vlasenkof3cbfc02009-05-28 03:54:29 +0200355 int rc, first;
Mike Frysinger00ffaea2009-05-05 20:13:45 -0400356 llist_t *l;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000357
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000358 if (!(m->flags & MODULE_FLAG_FOUND_IN_MODDEP)) {
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200359 if (!(option_mask32 & INSMOD_OPT_SILENT))
Denys Vlasenko7eaf58d2009-06-17 20:33:50 +0200360 bb_error_msg("module %s not found in modules.dep",
361 humanly_readable_name(m));
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000362 return -ENOENT;
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000363 }
364 DBG("do_modprob'ing %s", m->modname);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000365
366 if (!(option_mask32 & MODPROBE_OPT_REMOVE))
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000367 m->deps = llist_rev(m->deps);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000368
Mike Frysinger00ffaea2009-05-05 20:13:45 -0400369 for (l = m->deps; l != NULL; l = l->link)
370 DBG("dep: %s", l->data);
371
Denys Vlasenkof3cbfc02009-05-28 03:54:29 +0200372 first = 1;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000373 rc = 0;
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200374 while (m->deps) {
375 rc = 0;
376 fn = llist_pop(&m->deps); /* we leak it */
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000377 m2 = get_or_add_modentry(fn);
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200378
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000379 if (option_mask32 & MODPROBE_OPT_REMOVE) {
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200380 /* modprobe -r */
Denys Vlasenkof3cbfc02009-05-28 03:54:29 +0200381 if (m2->flags & MODULE_FLAG_LOADED) {
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200382 rc = bb_delete_module(m2->modname, O_EXCL);
383 if (rc) {
384 if (first) {
Denys Vlasenko651a2692010-03-23 16:25:17 +0100385 bb_error_msg("can't unload module %s: %s",
Denys Vlasenko7eaf58d2009-06-17 20:33:50 +0200386 humanly_readable_name(m2),
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200387 moderror(rc));
388 break;
389 }
Denys Vlasenkof3cbfc02009-05-28 03:54:29 +0200390 } else {
391 m2->flags &= ~MODULE_FLAG_LOADED;
392 }
393 }
394 /* do not error out if *deps* fail to unload */
395 first = 0;
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200396 continue;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000397 }
398
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200399 if (m2->flags & MODULE_FLAG_LOADED) {
400 DBG("%s is already loaded, skipping", fn);
401 continue;
402 }
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000403
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200404 options = m2->options;
405 m2->options = NULL;
Denys Vlasenko3e26d4f2010-02-27 23:15:22 +0100406 options = parse_and_add_kcmdline_module_options(options, m2->modname);
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200407 if (m == m2)
408 options = gather_options_str(options, G.cmdline_mopts);
409 rc = bb_init_module(fn, options);
410 DBG("loaded %s '%s', rc:%d", fn, options, rc);
Denys Vlasenko725b5a32010-01-10 04:52:45 +0100411 if (rc == EEXIST)
412 rc = 0;
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200413 free(options);
414 if (rc) {
Denys Vlasenko651a2692010-03-23 16:25:17 +0100415 bb_error_msg("can't load module %s (%s): %s",
Denys Vlasenko7eaf58d2009-06-17 20:33:50 +0200416 humanly_readable_name(m2),
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200417 fn,
418 moderror(rc)
419 );
420 break;
421 }
422 m2->flags |= MODULE_FLAG_LOADED;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000423 }
Denis Vlasenko9604e1b2009-03-03 18:47:56 +0000424
Eric Andersen908e3622003-06-20 09:56:37 +0000425 return rc;
Robert Griebl52e8d062002-05-14 23:42:08 +0000426}
427
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000428static void load_modules_dep(void)
429{
430 struct module_entry *m;
431 char *colon, *tokens[2];
432 parser_t *p;
433
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200434 /* Modprobe does not work at all without modules.dep,
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000435 * even if the full module name is given. Returning error here
436 * was making us later confuse user with this message:
437 * "module /full/path/to/existing/file/module.ko not found".
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000438 * It's better to die immediately, with good message.
439 * xfopen_for_read provides that. */
440 p = config_open2(CONFIG_DEFAULT_DEPMOD_FILE, xfopen_for_read);
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000441
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000442 while (G.num_unresolved_deps
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000443 && config_read(p, tokens, 2, 1, "# \t", PARSE_NORMAL)
444 ) {
445 colon = last_char_is(tokens[0], ':');
446 if (colon == NULL)
447 continue;
448 *colon = 0;
449
450 m = get_modentry(tokens[0]);
451 if (m == NULL)
452 continue;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000453
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000454 /* Optimization... */
455 if ((m->flags & MODULE_FLAG_LOADED)
456 && !(option_mask32 & MODPROBE_OPT_REMOVE)
457 ) {
458 DBG("skip deps of %s, it's already loaded", tokens[0]);
459 continue;
460 }
461
462 m->flags |= MODULE_FLAG_FOUND_IN_MODDEP;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000463 if ((m->flags & MODULE_FLAG_NEED_DEPS) && (m->deps == NULL)) {
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000464 G.num_unresolved_deps--;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000465 llist_add_to(&m->deps, xstrdup(tokens[0]));
466 if (tokens[1])
Denys Vlasenko5da42fc2009-08-14 02:10:54 +0200467 string_to_llist(tokens[1], &m->deps, " \t");
Mike Frysinger00ffaea2009-05-05 20:13:45 -0400468 } else
469 DBG("skipping dep line");
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000470 }
471 config_close(p);
472}
473
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000474int modprobe_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko9ddc0042008-08-06 00:51:43 +0000475int modprobe_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen0139ca92001-07-22 23:01:03 +0000476{
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000477 struct utsname uts;
Denis Vlasenkobb26db42008-10-31 02:04:28 +0000478 int rc;
479 unsigned opt;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000480 struct module_entry *me;
Eric Andersen0139ca92001-07-22 23:01:03 +0000481
Pascal Bellardb82b34e2010-06-07 01:16:45 +0200482 opt_complementary = MODPROBE_COMPLEMENTARY;
483 opt = getopt32(argv, INSMOD_OPTS MODPROBE_OPTS INSMOD_ARGS);
Denis Vlasenko9ddc0042008-08-06 00:51:43 +0000484 argv += optind;
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000485
Pascal Bellardb82b34e2010-06-07 01:16:45 +0200486 if (opt & MODPROBE_OPT_LIST_ONLY) {
487 char name[MODULE_NAME_LEN];
488 char *colon, *tokens[2];
489 parser_t *p = config_open2(CONFIG_DEFAULT_DEPMOD_FILE, xfopen_for_read);
490
491 while (config_read(p, tokens, 2, 1, "# \t", PARSE_NORMAL)) {
492 colon = last_char_is(tokens[0], ':');
493 if (!colon)
494 continue;
495 *colon = '\0';
496 filename2modname(tokens[0], name);
497 if (!argv[0])
498 puts(tokens[0]);
499 else {
500 int i;
501 for (i = 0; argv[i]; i++) {
502 if (fnmatch(argv[i], name, 0) == 0) {
503 puts(tokens[0]);
504 }
505 }
506 }
507 }
508 return EXIT_SUCCESS;
509 }
510
511 /* Yes, for some reason -l ignores -s... */
512 if (opt & INSMOD_OPT_SYSLOG)
513 logmode = LOGMODE_SYSLOG;
Robert Griebl236abbf2002-05-22 23:34:35 +0000514
Denis Vlasenkobb26db42008-10-31 02:04:28 +0000515 if (!argv[0]) {
516 if (opt & MODPROBE_OPT_REMOVE) {
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000517 /* "modprobe -r" (w/o params).
518 * "If name is NULL, all unused modules marked
519 * autoclean will be removed".
520 */
Denys Vlasenkoab19ede2009-11-11 21:05:42 +0100521 if (bb_delete_module(NULL, O_NONBLOCK | O_EXCL) != 0)
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000522 bb_perror_msg_and_die("rmmod");
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000523 }
Denis Vlasenkobb26db42008-10-31 02:04:28 +0000524 return EXIT_SUCCESS;
525 }
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000526
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000527 /* Goto modules location */
528 xchdir(CONFIG_DEFAULT_MODULES_DIR);
529 uname(&uts);
530 xchdir(uts.release);
Eric Andersen0139ca92001-07-22 23:01:03 +0000531
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000532 /* Retrieve module names of already loaded modules */
Denis Vlasenkobb26db42008-10-31 02:04:28 +0000533 {
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000534 char *s;
Denis Vlasenkobb26db42008-10-31 02:04:28 +0000535 parser_t *parser = config_open2("/proc/modules", fopen_for_read);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000536 while (config_read(parser, &s, 1, 1, "# \t", PARSE_NORMAL & ~PARSE_GREEDY))
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000537 get_or_add_modentry(s)->flags |= MODULE_FLAG_LOADED;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000538 config_close(parser);
539 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000540
Denys Vlasenko140def82009-05-26 12:48:34 +0200541 if (opt & (MODPROBE_OPT_INSERT_ALL | MODPROBE_OPT_REMOVE)) {
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000542 /* Each argument is a module name */
543 do {
Mike Frysinger00ffaea2009-05-05 20:13:45 -0400544 DBG("adding module %s", *argv);
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000545 add_probe(*argv++);
546 } while (*argv);
547 } else {
548 /* First argument is module name, rest are parameters */
Mike Frysinger00ffaea2009-05-05 20:13:45 -0400549 DBG("probing just module %s", *argv);
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000550 add_probe(argv[0]);
551 G.cmdline_mopts = parse_cmdline_module_options(argv);
552 }
553
554 /* Happens if all requested modules are already loaded */
555 if (G.probes == NULL)
556 return EXIT_SUCCESS;
557
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000558 read_config("/etc/modprobe.conf");
559 read_config("/etc/modprobe.d");
560 if (ENABLE_FEATURE_MODUTILS_SYMBOLS && G.need_symbols)
561 read_config("modules.symbols");
562 load_modules_dep();
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000563 if (ENABLE_FEATURE_MODUTILS_ALIAS && G.num_unresolved_deps) {
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000564 read_config("modules.alias");
565 load_modules_dep();
566 }
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000567
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200568 rc = 0;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000569 while ((me = llist_pop(&G.probes)) != NULL) {
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000570 if (me->realnames == NULL) {
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200571 DBG("probing by module name");
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000572 /* This is not an alias. Literal names are blacklisted
573 * only if '-b' is given.
574 */
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000575 if (!(opt & MODPROBE_OPT_BLACKLIST)
576 || !(me->flags & MODULE_FLAG_BLACKLISTED)
577 ) {
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200578 rc |= do_modprobe(me);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000579 }
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200580 continue;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000581 }
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200582
583 /* Probe all real names for the alias */
584 do {
585 char *realname = llist_pop(&me->realnames);
586 struct module_entry *m2;
587
588 DBG("probing alias %s by realname %s", me->modname, realname);
589 m2 = get_or_add_modentry(realname);
590 if (!(m2->flags & MODULE_FLAG_BLACKLISTED)
591 && (!(m2->flags & MODULE_FLAG_LOADED)
592 || (opt & MODPROBE_OPT_REMOVE))
593 ) {
594//TODO: we can pass "me" as 2nd param to do_modprobe,
595//and make do_modprobe emit more meaningful error messages
596//with alias name included, not just module name alias resolves to.
597 rc |= do_modprobe(m2);
598 }
599 free(realname);
600 } while (me->realnames != NULL);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000601 }
602
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200603 return (rc != 0);
Eric Andersen0139ca92001-07-22 23:01:03 +0000604}