blob: 678f4bea54b44d0abb7167c04802a18f1d0e6a28 [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 Vlasenkob9f2d9f2011-01-18 13:58:01 +010011//applet:IF_MODPROBE(APPLET(modprobe, BB_DIR_SBIN, BB_SUID_DROP))
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +000012
Serj Kalichev85781962010-12-28 04:18:18 +010013#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
Pascal Bellardb82b34e2010-06-07 01:16:45 +020027//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
Serj Kalichev85781962010-12-28 04:18:18 +010089//usage: "[-alrqvsD" IF_FEATURE_MODPROBE_BLACKLIST("b") "]"
90//usage: " MODULE [symbol=value]..."
Pascal Bellardb82b34e2010-06-07 01:16:45 +020091//usage:#define modprobe_full_usage "\n\n"
Denys Vlasenko66426762011-06-05 03:58:28 +020092//usage: " -a Load multiple MODULEs"
Serj Kalichev85781962010-12-28 04:18:18 +010093//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"
Pascal Bellardb82b34e2010-06-07 01:16:45 +020099//usage: IF_FEATURE_MODPROBE_BLACKLIST(
Serj Kalichev85781962010-12-28 04:18:18 +0100100//usage: "\n -b Apply blacklist to module names too"
Pascal Bellardb82b34e2010-06-07 01:16:45 +0200101//usage: )
102//usage:#endif /* !ENABLE_MODPROBE_SMALL */
103
104/* Note that usage text doesn't document various 2.4 options
105 * we pull in through INSMOD_OPTS define */
Serj Kalichev85781962010-12-28 04:18:18 +0100106#define MODPROBE_OPTS "alrD" IF_FEATURE_MODPROBE_BLACKLIST("b")
107/* -a and -D _are_ in fact compatible */
108#define MODPROBE_COMPLEMENTARY ("q-v:v-q:l--arD:r--alD:a--lr:D--rl")
Pascal Bellardb82b34e2010-06-07 01:16:45 +0200109//#define MODPROBE_OPTS "acd:lnrt:C:" IF_FEATURE_MODPROBE_BLACKLIST("b")
Serj Kalicheve4e911e2010-12-26 01:56:19 +0100110//#define MODPROBE_COMPLEMENTARY "q-v:v-q:l--acr:a--lr:r--al"
Pascal Bellardb82b34e2010-06-07 01:16:45 +0200111enum {
Serj Kalicheve4e911e2010-12-26 01:56:19 +0100112 OPT_INSERT_ALL = (INSMOD_OPT_UNUSED << 0), /* a */
113 //OPT_DUMP_ONLY = (INSMOD_OPT_UNUSED << x), /* c */
114 //OPT_DIRNAME = (INSMOD_OPT_UNUSED << x), /* d */
115 OPT_LIST_ONLY = (INSMOD_OPT_UNUSED << 1), /* l */
116 //OPT_SHOW_ONLY = (INSMOD_OPT_UNUSED << x), /* n */
117 OPT_REMOVE = (INSMOD_OPT_UNUSED << 2), /* r */
118 //OPT_RESTRICT = (INSMOD_OPT_UNUSED << x), /* t */
119 //OPT_VERONLY = (INSMOD_OPT_UNUSED << x), /* V */
Serj Kalichev85781962010-12-28 04:18:18 +0100120 //OPT_CONFIGFILE = (INSMOD_OPT_UNUSED << x), /* C */
121 OPT_SHOW_DEPS = (INSMOD_OPT_UNUSED << 3), /* D */
122 OPT_BLACKLIST = (INSMOD_OPT_UNUSED << 4) * ENABLE_FEATURE_MODPROBE_BLACKLIST,
Pascal Bellardb82b34e2010-06-07 01:16:45 +0200123};
Serj Kalichev85781962010-12-28 04:18:18 +0100124#if ENABLE_LONG_OPTS
125static const char modprobe_longopts[] ALIGN1 =
126 /* nobody asked for long opts (yet) */
127 // "all\0" No_argument "a"
128 // "list\0" No_argument "l"
129 // "remove\0" No_argument "r"
130 // "quiet\0" No_argument "q"
131 // "verbose\0" No_argument "v"
132 // "syslog\0" No_argument "s"
133 /* module-init-tools 3.11.1 has only long opt --show-depends
134 * but no short -D, we provide long opt for scripts which
135 * were written for 3.11.1: */
136 "show-depends\0" No_argument "D"
137 // IF_FEATURE_MODPROBE_BLACKLIST(
138 // "use-blacklist\0" No_argument "b"
139 // )
140 ;
141#endif
Pascal Bellardb82b34e2010-06-07 01:16:45 +0200142
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200143#define MODULE_FLAG_LOADED 0x0001
144#define MODULE_FLAG_NEED_DEPS 0x0002
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000145/* "was seen in modules.dep": */
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200146#define MODULE_FLAG_FOUND_IN_MODDEP 0x0004
147#define MODULE_FLAG_BLACKLISTED 0x0008
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000148
149struct module_entry { /* I'll call it ME. */
150 unsigned flags;
151 char *modname; /* stripped of /path/, .ext and s/-/_/g */
152 const char *probed_name; /* verbatim as seen on cmdline */
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000153 char *options; /* options from config files */
154 llist_t *realnames; /* strings. if this module is an alias, */
155 /* real module name is one of these. */
156//Can there really be more than one? Example from real kernel?
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000157 llist_t *deps; /* strings. modules we depend on */
Eric Andersen60e56f52002-04-26 06:04:01 +0000158};
159
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000160struct globals {
161 llist_t *db; /* MEs of all modules ever seen (caching for speed) */
162 llist_t *probes; /* MEs of module(s) requested on cmdline */
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000163 char *cmdline_mopts; /* module options from cmdline */
164 int num_unresolved_deps;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000165 /* bool. "Did we have 'symbol:FOO' requested on cmdline?" */
166 smallint need_symbols;
Serj Kalicheve4e911e2010-12-26 01:56:19 +0100167 struct utsname uts;
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100168} FIX_ALIASING;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000169#define G (*(struct globals*)&bb_common_bufsiz1)
170#define INIT_G() do { } while (0)
Serj Kalicheve4e911e2010-12-26 01:56:19 +0100171struct BUG_G_too_big {
Denys Vlasenkob7c9fb22011-02-03 00:05:48 +0100172 char BUG_G_too_big[sizeof(G) <= COMMON_BUFSIZE ? 1 : -1];
Serj Kalicheve4e911e2010-12-26 01:56:19 +0100173};
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000174
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000175
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000176static int read_config(const char *path);
177
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000178static char *gather_options_str(char *opts, const char *append)
179{
180 /* Speed-optimized. We call gather_options_str many times. */
Denys Vlasenkoda879ec2010-01-26 08:17:45 +0100181 if (append) {
182 if (opts == NULL) {
183 opts = xstrdup(append);
184 } else {
185 int optlen = strlen(opts);
186 opts = xrealloc(opts, optlen + strlen(append) + 2);
187 sprintf(opts + optlen, " %s", append);
188 }
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000189 }
190 return opts;
191}
192
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000193static struct module_entry *helper_get_module(const char *module, int create)
Denis Vlasenkof45c4f42008-06-16 04:09:25 +0000194{
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000195 char modname[MODULE_NAME_LEN];
196 struct module_entry *e;
197 llist_t *l;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000198
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000199 filename2modname(module, modname);
200 for (l = G.db; l != NULL; l = l->link) {
201 e = (struct module_entry *) l->data;
202 if (strcmp(e->modname, modname) == 0)
203 return e;
204 }
205 if (!create)
206 return NULL;
207
208 e = xzalloc(sizeof(*e));
209 e->modname = xstrdup(modname);
210 llist_add_to(&G.db, e);
211
212 return e;
213}
214static struct module_entry *get_or_add_modentry(const char *module)
215{
216 return helper_get_module(module, 1);
217}
218static struct module_entry *get_modentry(const char *module)
219{
220 return helper_get_module(module, 0);
221}
222
223static void add_probe(const char *name)
224{
225 struct module_entry *m;
226
227 m = get_or_add_modentry(name);
Serj Kalicheve4e911e2010-12-26 01:56:19 +0100228 if (!(option_mask32 & (OPT_REMOVE | OPT_SHOW_DEPS))
Denys Vlasenko140def82009-05-26 12:48:34 +0200229 && (m->flags & MODULE_FLAG_LOADED)
230 ) {
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000231 DBG("skipping %s, it is already loaded", name);
232 return;
233 }
234
Mike Frysinger00ffaea2009-05-05 20:13:45 -0400235 DBG("queuing %s", name);
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000236 m->probed_name = name;
237 m->flags |= MODULE_FLAG_NEED_DEPS;
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000238 llist_add_to_end(&G.probes, m);
239 G.num_unresolved_deps++;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000240 if (ENABLE_FEATURE_MODUTILS_SYMBOLS
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000241 && strncmp(m->modname, "symbol:", 7) == 0
242 ) {
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000243 G.need_symbols = 1;
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000244 }
Denis Vlasenko9ddc8d52008-05-18 14:39:43 +0000245}
246
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000247static int FAST_FUNC config_file_action(const char *filename,
248 struct stat *statbuf UNUSED_PARAM,
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000249 void *userdata UNUSED_PARAM,
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000250 int depth UNUSED_PARAM)
Rob Landley72615752006-04-10 16:09:52 +0000251{
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000252 char *tokens[3];
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000253 parser_t *p;
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200254 struct module_entry *m;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000255 int rc = TRUE;
Denis Vlasenkoe1ee48e2008-07-29 00:19:44 +0000256
257 if (bb_basename(filename)[0] == '.')
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000258 goto error;
Denis Vlasenkoe1ee48e2008-07-29 00:19:44 +0000259
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000260 p = config_open2(filename, fopen_for_read);
261 if (p == NULL) {
262 rc = FALSE;
263 goto error;
264 }
Rob Landley72615752006-04-10 16:09:52 +0000265
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000266 while (config_read(p, tokens, 3, 2, "# \t", PARSE_NORMAL)) {
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000267//Use index_in_strings?
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000268 if (strcmp(tokens[0], "alias") == 0) {
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000269 /* alias <wildcard> <modulename> */
270 llist_t *l;
271 char wildcard[MODULE_NAME_LEN];
272 char *rmod;
273
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000274 if (tokens[2] == NULL)
275 continue;
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000276 filename2modname(tokens[1], wildcard);
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000277
278 for (l = G.probes; l != NULL; l = l->link) {
279 m = (struct module_entry *) l->data;
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000280 if (fnmatch(wildcard, m->modname, 0) != 0)
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000281 continue;
282 rmod = filename2modname(tokens[2], NULL);
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000283 llist_add_to(&m->realnames, rmod);
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000284
285 if (m->flags & MODULE_FLAG_NEED_DEPS) {
286 m->flags &= ~MODULE_FLAG_NEED_DEPS;
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000287 G.num_unresolved_deps--;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000288 }
289
290 m = get_or_add_modentry(rmod);
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000291 if (!(m->flags & MODULE_FLAG_NEED_DEPS)) {
292 m->flags |= MODULE_FLAG_NEED_DEPS;
293 G.num_unresolved_deps++;
294 }
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000295 }
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000296 } else if (strcmp(tokens[0], "options") == 0) {
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000297 /* options <modulename> <option...> */
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000298 if (tokens[2] == NULL)
299 continue;
300 m = get_or_add_modentry(tokens[1]);
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000301 m->options = gather_options_str(m->options, tokens[2]);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000302 } else if (strcmp(tokens[0], "include") == 0) {
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000303 /* include <filename> */
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000304 read_config(tokens[1]);
305 } else if (ENABLE_FEATURE_MODPROBE_BLACKLIST
306 && strcmp(tokens[0], "blacklist") == 0
307 ) {
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000308 /* blacklist <modulename> */
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000309 get_or_add_modentry(tokens[1])->flags |= MODULE_FLAG_BLACKLISTED;
Rob Landley72615752006-04-10 16:09:52 +0000310 }
Rob Landleye9190962005-12-12 19:38:44 +0000311 }
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000312 config_close(p);
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200313 error:
Robert Griebl52e8d062002-05-14 23:42:08 +0000314 return rc;
315}
316
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000317static int read_config(const char *path)
Robert Griebl52e8d062002-05-14 23:42:08 +0000318{
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000319 return recursive_action(path, ACTION_RECURSE | ACTION_QUIET,
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000320 config_file_action, NULL, NULL, 1);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000321}
Eric Andersen3b1a7442003-12-24 20:30:45 +0000322
Denys Vlasenko7eaf58d2009-06-17 20:33:50 +0200323static const char *humanly_readable_name(struct module_entry *m)
324{
325 /* probed_name may be NULL. modname always exists. */
326 return m->probed_name ? m->probed_name : m->modname;
327}
328
Denys Vlasenko3e26d4f2010-02-27 23:15:22 +0100329static char *parse_and_add_kcmdline_module_options(char *options, const char *modulename)
330{
Denys Vlasenko197202d2010-03-03 04:20:33 +0100331 char *kcmdline_buf;
Denys Vlasenko3e26d4f2010-02-27 23:15:22 +0100332 char *kcmdline;
333 char *kptr;
334 int len;
335
Denys Vlasenko197202d2010-03-03 04:20:33 +0100336 kcmdline_buf = xmalloc_open_read_close("/proc/cmdline", NULL);
337 if (!kcmdline_buf)
Denys Vlasenko3e26d4f2010-02-27 23:15:22 +0100338 return options;
Denys Vlasenko3e26d4f2010-02-27 23:15:22 +0100339
Denys Vlasenko3e26d4f2010-02-27 23:15:22 +0100340 kcmdline = kcmdline_buf;
Denys Vlasenko197202d2010-03-03 04:20:33 +0100341 len = strlen(modulename);
Denys Vlasenko3e26d4f2010-02-27 23:15:22 +0100342 while ((kptr = strsep(&kcmdline, "\n\t ")) != NULL) {
343 if (strncmp(modulename, kptr, len) != 0)
344 continue;
345 kptr += len;
346 if (*kptr != '.')
347 continue;
348 /* It is "modulename.xxxx" */
349 kptr++;
350 if (strchr(kptr, '=') != NULL) {
351 /* It is "modulename.opt=[val]" */
352 options = gather_options_str(options, kptr);
353 }
354 }
Denys Vlasenko197202d2010-03-03 04:20:33 +0100355 free(kcmdline_buf);
Denys Vlasenko3e26d4f2010-02-27 23:15:22 +0100356
357 return options;
358}
359
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200360/* Return: similar to bb_init_module:
361 * 0 on success,
362 * -errno on open/read error,
363 * errno on init_module() error
364 */
Pascal Bellardb82b34e2010-06-07 01:16:45 +0200365/* NB: INSMOD_OPT_SILENT bit suppresses ONLY non-existent modules,
366 * not deleted ones (those are still listed in modules.dep).
367 * module-init-tools version 3.4:
368 * # modprobe bogus
369 * FATAL: Module bogus not found. [exitcode 1]
370 * # modprobe -q bogus [silent, exitcode still 1]
371 * but:
372 * # rm kernel/drivers/net/dummy.ko
373 * # modprobe -q dummy
374 * FATAL: Could not open '/lib/modules/xxx/kernel/drivers/net/dummy.ko': No such file or directory
375 * [exitcode 1]
376 */
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000377static int do_modprobe(struct module_entry *m)
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000378{
Denys Vlasenkof3cbfc02009-05-28 03:54:29 +0200379 int rc, first;
Mike Frysinger00ffaea2009-05-05 20:13:45 -0400380 llist_t *l;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000381
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000382 if (!(m->flags & MODULE_FLAG_FOUND_IN_MODDEP)) {
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200383 if (!(option_mask32 & INSMOD_OPT_SILENT))
Denys Vlasenko7eaf58d2009-06-17 20:33:50 +0200384 bb_error_msg("module %s not found in modules.dep",
385 humanly_readable_name(m));
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000386 return -ENOENT;
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000387 }
388 DBG("do_modprob'ing %s", m->modname);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000389
Serj Kalicheve4e911e2010-12-26 01:56:19 +0100390 if (!(option_mask32 & OPT_REMOVE))
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000391 m->deps = llist_rev(m->deps);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000392
Mike Frysinger00ffaea2009-05-05 20:13:45 -0400393 for (l = m->deps; l != NULL; l = l->link)
394 DBG("dep: %s", l->data);
395
Denys Vlasenkof3cbfc02009-05-28 03:54:29 +0200396 first = 1;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000397 rc = 0;
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200398 while (m->deps) {
Serj Kalichev85781962010-12-28 04:18:18 +0100399 struct module_entry *m2;
400 char *fn, *options;
401
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200402 rc = 0;
403 fn = llist_pop(&m->deps); /* we leak it */
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000404 m2 = get_or_add_modentry(fn);
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200405
Serj Kalicheve4e911e2010-12-26 01:56:19 +0100406 if (option_mask32 & OPT_REMOVE) {
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200407 /* modprobe -r */
Denys Vlasenkof3cbfc02009-05-28 03:54:29 +0200408 if (m2->flags & MODULE_FLAG_LOADED) {
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200409 rc = bb_delete_module(m2->modname, O_EXCL);
410 if (rc) {
411 if (first) {
Denys Vlasenko651a2692010-03-23 16:25:17 +0100412 bb_error_msg("can't unload module %s: %s",
Denys Vlasenko7eaf58d2009-06-17 20:33:50 +0200413 humanly_readable_name(m2),
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200414 moderror(rc));
415 break;
416 }
Denys Vlasenkof3cbfc02009-05-28 03:54:29 +0200417 } else {
418 m2->flags &= ~MODULE_FLAG_LOADED;
419 }
420 }
421 /* do not error out if *deps* fail to unload */
422 first = 0;
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200423 continue;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000424 }
425
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200426 options = m2->options;
427 m2->options = NULL;
Denys Vlasenko3e26d4f2010-02-27 23:15:22 +0100428 options = parse_and_add_kcmdline_module_options(options, m2->modname);
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200429 if (m == m2)
430 options = gather_options_str(options, G.cmdline_mopts);
Serj Kalichev85781962010-12-28 04:18:18 +0100431
432 if (option_mask32 & OPT_SHOW_DEPS) {
433 printf(options ? "insmod %s/%s/%s %s\n"
434 : "insmod %s/%s/%s\n",
435 CONFIG_DEFAULT_MODULES_DIR, G.uts.release, fn,
436 options);
437 free(options);
438 continue;
439 }
440
441 if (m2->flags & MODULE_FLAG_LOADED) {
442 DBG("%s is already loaded, skipping", fn);
443 free(options);
444 continue;
445 }
446
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200447 rc = bb_init_module(fn, options);
448 DBG("loaded %s '%s', rc:%d", fn, options, rc);
Denys Vlasenko725b5a32010-01-10 04:52:45 +0100449 if (rc == EEXIST)
450 rc = 0;
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200451 free(options);
452 if (rc) {
Denys Vlasenko651a2692010-03-23 16:25:17 +0100453 bb_error_msg("can't load module %s (%s): %s",
Denys Vlasenko7eaf58d2009-06-17 20:33:50 +0200454 humanly_readable_name(m2),
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200455 fn,
456 moderror(rc)
457 );
458 break;
459 }
460 m2->flags |= MODULE_FLAG_LOADED;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000461 }
Denis Vlasenko9604e1b2009-03-03 18:47:56 +0000462
Eric Andersen908e3622003-06-20 09:56:37 +0000463 return rc;
Robert Griebl52e8d062002-05-14 23:42:08 +0000464}
465
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000466static void load_modules_dep(void)
467{
468 struct module_entry *m;
469 char *colon, *tokens[2];
470 parser_t *p;
471
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200472 /* Modprobe does not work at all without modules.dep,
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000473 * even if the full module name is given. Returning error here
474 * was making us later confuse user with this message:
475 * "module /full/path/to/existing/file/module.ko not found".
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000476 * It's better to die immediately, with good message.
477 * xfopen_for_read provides that. */
478 p = config_open2(CONFIG_DEFAULT_DEPMOD_FILE, xfopen_for_read);
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000479
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000480 while (G.num_unresolved_deps
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000481 && config_read(p, tokens, 2, 1, "# \t", PARSE_NORMAL)
482 ) {
483 colon = last_char_is(tokens[0], ':');
484 if (colon == NULL)
485 continue;
486 *colon = 0;
487
488 m = get_modentry(tokens[0]);
489 if (m == NULL)
490 continue;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000491
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000492 /* Optimization... */
493 if ((m->flags & MODULE_FLAG_LOADED)
Serj Kalicheve4e911e2010-12-26 01:56:19 +0100494 && !(option_mask32 & (OPT_REMOVE | OPT_SHOW_DEPS))
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000495 ) {
496 DBG("skip deps of %s, it's already loaded", tokens[0]);
497 continue;
498 }
499
500 m->flags |= MODULE_FLAG_FOUND_IN_MODDEP;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000501 if ((m->flags & MODULE_FLAG_NEED_DEPS) && (m->deps == NULL)) {
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000502 G.num_unresolved_deps--;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000503 llist_add_to(&m->deps, xstrdup(tokens[0]));
504 if (tokens[1])
Denys Vlasenko5da42fc2009-08-14 02:10:54 +0200505 string_to_llist(tokens[1], &m->deps, " \t");
Mike Frysinger00ffaea2009-05-05 20:13:45 -0400506 } else
507 DBG("skipping dep line");
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000508 }
509 config_close(p);
510}
511
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000512int modprobe_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko9ddc0042008-08-06 00:51:43 +0000513int modprobe_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen0139ca92001-07-22 23:01:03 +0000514{
Denis Vlasenkobb26db42008-10-31 02:04:28 +0000515 int rc;
516 unsigned opt;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000517 struct module_entry *me;
Eric Andersen0139ca92001-07-22 23:01:03 +0000518
Serj Kalicheve4e911e2010-12-26 01:56:19 +0100519 INIT_G();
520
521 IF_LONG_OPTS(applet_long_options = modprobe_longopts;)
Pascal Bellardb82b34e2010-06-07 01:16:45 +0200522 opt_complementary = MODPROBE_COMPLEMENTARY;
523 opt = getopt32(argv, INSMOD_OPTS MODPROBE_OPTS INSMOD_ARGS);
Denis Vlasenko9ddc0042008-08-06 00:51:43 +0000524 argv += optind;
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000525
Lauri Kasanena48a29f2010-07-08 11:22:30 +0300526 /* Goto modules location */
527 xchdir(CONFIG_DEFAULT_MODULES_DIR);
Serj Kalicheve4e911e2010-12-26 01:56:19 +0100528 uname(&G.uts);
529 xchdir(G.uts.release);
Lauri Kasanena48a29f2010-07-08 11:22:30 +0300530
Serj Kalicheve4e911e2010-12-26 01:56:19 +0100531 if (opt & OPT_LIST_ONLY) {
Denys Vlasenko5dad4ae2011-02-15 02:17:31 +0100532 int i;
Pascal Bellardb82b34e2010-06-07 01:16:45 +0200533 char name[MODULE_NAME_LEN];
534 char *colon, *tokens[2];
535 parser_t *p = config_open2(CONFIG_DEFAULT_DEPMOD_FILE, xfopen_for_read);
536
Denys Vlasenko5dad4ae2011-02-15 02:17:31 +0100537 for (i = 0; argv[i]; i++)
538 replace(argv[i], '-', '_');
539
Pascal Bellardb82b34e2010-06-07 01:16:45 +0200540 while (config_read(p, tokens, 2, 1, "# \t", PARSE_NORMAL)) {
541 colon = last_char_is(tokens[0], ':');
542 if (!colon)
543 continue;
544 *colon = '\0';
545 filename2modname(tokens[0], name);
546 if (!argv[0])
547 puts(tokens[0]);
548 else {
Pascal Bellardb82b34e2010-06-07 01:16:45 +0200549 for (i = 0; argv[i]; i++) {
550 if (fnmatch(argv[i], name, 0) == 0) {
551 puts(tokens[0]);
552 }
553 }
554 }
555 }
556 return EXIT_SUCCESS;
557 }
558
559 /* Yes, for some reason -l ignores -s... */
560 if (opt & INSMOD_OPT_SYSLOG)
561 logmode = LOGMODE_SYSLOG;
Robert Griebl236abbf2002-05-22 23:34:35 +0000562
Denis Vlasenkobb26db42008-10-31 02:04:28 +0000563 if (!argv[0]) {
Serj Kalicheve4e911e2010-12-26 01:56:19 +0100564 if (opt & OPT_REMOVE) {
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000565 /* "modprobe -r" (w/o params).
566 * "If name is NULL, all unused modules marked
567 * autoclean will be removed".
568 */
Denys Vlasenkoab19ede2009-11-11 21:05:42 +0100569 if (bb_delete_module(NULL, O_NONBLOCK | O_EXCL) != 0)
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000570 bb_perror_msg_and_die("rmmod");
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000571 }
Denis Vlasenkobb26db42008-10-31 02:04:28 +0000572 return EXIT_SUCCESS;
573 }
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000574
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000575 /* Retrieve module names of already loaded modules */
Denis Vlasenkobb26db42008-10-31 02:04:28 +0000576 {
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000577 char *s;
Denis Vlasenkobb26db42008-10-31 02:04:28 +0000578 parser_t *parser = config_open2("/proc/modules", fopen_for_read);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000579 while (config_read(parser, &s, 1, 1, "# \t", PARSE_NORMAL & ~PARSE_GREEDY))
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000580 get_or_add_modentry(s)->flags |= MODULE_FLAG_LOADED;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000581 config_close(parser);
582 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000583
Serj Kalicheve4e911e2010-12-26 01:56:19 +0100584 if (opt & (OPT_INSERT_ALL | OPT_REMOVE)) {
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000585 /* Each argument is a module name */
586 do {
Mike Frysinger00ffaea2009-05-05 20:13:45 -0400587 DBG("adding module %s", *argv);
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000588 add_probe(*argv++);
589 } while (*argv);
590 } else {
591 /* First argument is module name, rest are parameters */
Mike Frysinger00ffaea2009-05-05 20:13:45 -0400592 DBG("probing just module %s", *argv);
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000593 add_probe(argv[0]);
Denys Vlasenkoc5830bd2011-02-02 00:00:36 +0100594 G.cmdline_mopts = parse_cmdline_module_options(argv, /*quote_spaces:*/ 1);
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000595 }
596
597 /* Happens if all requested modules are already loaded */
598 if (G.probes == NULL)
599 return EXIT_SUCCESS;
600
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000601 read_config("/etc/modprobe.conf");
602 read_config("/etc/modprobe.d");
603 if (ENABLE_FEATURE_MODUTILS_SYMBOLS && G.need_symbols)
604 read_config("modules.symbols");
605 load_modules_dep();
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000606 if (ENABLE_FEATURE_MODUTILS_ALIAS && G.num_unresolved_deps) {
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000607 read_config("modules.alias");
608 load_modules_dep();
609 }
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000610
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200611 rc = 0;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000612 while ((me = llist_pop(&G.probes)) != NULL) {
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000613 if (me->realnames == NULL) {
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200614 DBG("probing by module name");
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000615 /* This is not an alias. Literal names are blacklisted
616 * only if '-b' is given.
617 */
Serj Kalicheve4e911e2010-12-26 01:56:19 +0100618 if (!(opt & OPT_BLACKLIST)
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000619 || !(me->flags & MODULE_FLAG_BLACKLISTED)
620 ) {
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200621 rc |= do_modprobe(me);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000622 }
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200623 continue;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000624 }
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200625
626 /* Probe all real names for the alias */
627 do {
628 char *realname = llist_pop(&me->realnames);
629 struct module_entry *m2;
630
631 DBG("probing alias %s by realname %s", me->modname, realname);
632 m2 = get_or_add_modentry(realname);
633 if (!(m2->flags & MODULE_FLAG_BLACKLISTED)
634 && (!(m2->flags & MODULE_FLAG_LOADED)
Serj Kalicheve4e911e2010-12-26 01:56:19 +0100635 || (opt & (OPT_REMOVE | OPT_SHOW_DEPS)))
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200636 ) {
637//TODO: we can pass "me" as 2nd param to do_modprobe,
638//and make do_modprobe emit more meaningful error messages
639//with alias name included, not just module name alias resolves to.
640 rc |= do_modprobe(m2);
641 }
642 free(realname);
643 } while (me->realnames != NULL);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000644 }
645
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200646 return (rc != 0);
Eric Andersen0139ca92001-07-22 23:01:03 +0000647}