blob: 996de4074cd0c885f058111d41281e170892052a [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
Denys Vlasenkoe998b082015-01-15 02:48:36 +010018#if 1
Serj Kalichev85781962010-12-28 04:18:18 +010019#define DBG(...) ((void)0)
Denys Vlasenkoe998b082015-01-15 02:48:36 +010020#else
21#define DBG(fmt, ...) bb_error_msg("%s: " fmt, __func__, ## __VA_ARGS__)
22#endif
Serj Kalichev85781962010-12-28 04:18:18 +010023
24/* Note that unlike older versions of modules.dep/depmod (busybox and m-i-t),
25 * we expect the full dependency list to be specified in modules.dep.
26 * Older versions would only export the direct dependency list.
27 */
28
29
Pascal Bellardb82b34e2010-06-07 01:16:45 +020030//usage:#if !ENABLE_MODPROBE_SMALL
31//usage:#define modprobe_notes_usage
32//usage: "modprobe can (un)load a stack of modules, passing each module options (when\n"
33//usage: "loading). modprobe uses a configuration file to determine what option(s) to\n"
34//usage: "pass each module it loads.\n"
35//usage: "\n"
36//usage: "The configuration file is searched (in this order):\n"
37//usage: "\n"
38//usage: " /etc/modprobe.conf (2.6 only)\n"
39//usage: " /etc/modules.conf\n"
40//usage: " /etc/conf.modules (deprecated)\n"
41//usage: "\n"
42//usage: "They all have the same syntax (see below). If none is present, it is\n"
43//usage: "_not_ an error; each loaded module is then expected to load without\n"
44//usage: "options. Once a file is found, the others are tested for.\n"
45//usage: "\n"
46//usage: "/etc/modules.conf entry format:\n"
47//usage: "\n"
48//usage: " alias <alias_name> <mod_name>\n"
49//usage: " Makes it possible to modprobe alias_name, when there is no such module.\n"
50//usage: " It makes sense if your mod_name is long, or you want a more representative\n"
51//usage: " name for that module (eg. 'scsi' in place of 'aha7xxx').\n"
52//usage: " This makes it also possible to use a different set of options (below) for\n"
53//usage: " the module and the alias.\n"
54//usage: " A module can be aliased more than once.\n"
55//usage: "\n"
56//usage: " options <mod_name|alias_name> <symbol=value...>\n"
57//usage: " When loading module mod_name (or the module aliased by alias_name), pass\n"
58//usage: " the \"symbol=value\" pairs as option to that module.\n"
59//usage: "\n"
60//usage: "Sample /etc/modules.conf file:\n"
61//usage: "\n"
62//usage: " options tulip irq=3\n"
63//usage: " alias tulip tulip2\n"
64//usage: " options tulip2 irq=4 io=0x308\n"
65//usage: "\n"
66//usage: "Other functionality offered by 'classic' modprobe is not available in\n"
67//usage: "this implementation.\n"
68//usage: "\n"
69//usage: "If module options are present both in the config file, and on the command line,\n"
70//usage: "then the options from the command line will be passed to the module _after_\n"
71//usage: "the options from the config file. That way, you can have defaults in the config\n"
72//usage: "file, and override them for a specific usage from the command line.\n"
73//usage:#define modprobe_example_usage
74//usage: "(with the above /etc/modules.conf):\n\n"
75//usage: "$ modprobe tulip\n"
76//usage: " will load the module 'tulip' with default option 'irq=3'\n\n"
77//usage: "$ modprobe tulip irq=5\n"
78//usage: " will load the module 'tulip' with option 'irq=5', thus overriding the default\n\n"
79//usage: "$ modprobe tulip2\n"
80//usage: " will load the module 'tulip' with default options 'irq=4 io=0x308',\n"
81//usage: " which are the default for alias 'tulip2'\n\n"
82//usage: "$ modprobe tulip2 irq=8\n"
83//usage: " will load the module 'tulip' with default options 'irq=4 io=0x308 irq=8',\n"
84//usage: " which are the default for alias 'tulip2' overridden by the option 'irq=8'\n\n"
85//usage: " from the command line\n\n"
86//usage: "$ modprobe tulip2 irq=2 io=0x210\n"
87//usage: " will load the module 'tulip' with default options 'irq=4 io=0x308 irq=4 io=0x210',\n"
88//usage: " which are the default for alias 'tulip2' overridden by the options 'irq=2 io=0x210'\n\n"
89//usage: " from the command line\n"
90//usage:
91//usage:#define modprobe_trivial_usage
Serj Kalichev85781962010-12-28 04:18:18 +010092//usage: "[-alrqvsD" IF_FEATURE_MODPROBE_BLACKLIST("b") "]"
Denys Vlasenko5fd3ddf2014-04-19 15:04:39 +020093//usage: " MODULE [SYMBOL=VALUE]..."
Pascal Bellardb82b34e2010-06-07 01:16:45 +020094//usage:#define modprobe_full_usage "\n\n"
Denys Vlasenko66426762011-06-05 03:58:28 +020095//usage: " -a Load multiple MODULEs"
Serj Kalichev85781962010-12-28 04:18:18 +010096//usage: "\n -l List (MODULE is a pattern)"
97//usage: "\n -r Remove MODULE (stacks) or do autoclean"
98//usage: "\n -q Quiet"
99//usage: "\n -v Verbose"
100//usage: "\n -s Log to syslog"
101//usage: "\n -D Show dependencies"
Pascal Bellardb82b34e2010-06-07 01:16:45 +0200102//usage: IF_FEATURE_MODPROBE_BLACKLIST(
Serj Kalichev85781962010-12-28 04:18:18 +0100103//usage: "\n -b Apply blacklist to module names too"
Pascal Bellardb82b34e2010-06-07 01:16:45 +0200104//usage: )
105//usage:#endif /* !ENABLE_MODPROBE_SMALL */
106
Felipe Contreras428bd2d2012-01-31 14:55:15 +0100107/* Note: usage text doesn't document various 2.4 options
108 * we pull in through INSMOD_OPTS define
109 * Note2: -b is always accepted, but if !FEATURE_MODPROBE_BLACKLIST,
110 * it is a no-op.
111 */
112#define MODPROBE_OPTS "alrDb"
Serj Kalichev85781962010-12-28 04:18:18 +0100113/* -a and -D _are_ in fact compatible */
114#define MODPROBE_COMPLEMENTARY ("q-v:v-q:l--arD:r--alD:a--lr:D--rl")
Felipe Contreras428bd2d2012-01-31 14:55:15 +0100115//#define MODPROBE_OPTS "acd:lnrt:C:b"
Serj Kalicheve4e911e2010-12-26 01:56:19 +0100116//#define MODPROBE_COMPLEMENTARY "q-v:v-q:l--acr:a--lr:r--al"
Pascal Bellardb82b34e2010-06-07 01:16:45 +0200117enum {
Serj Kalicheve4e911e2010-12-26 01:56:19 +0100118 OPT_INSERT_ALL = (INSMOD_OPT_UNUSED << 0), /* a */
119 //OPT_DUMP_ONLY = (INSMOD_OPT_UNUSED << x), /* c */
120 //OPT_DIRNAME = (INSMOD_OPT_UNUSED << x), /* d */
121 OPT_LIST_ONLY = (INSMOD_OPT_UNUSED << 1), /* l */
122 //OPT_SHOW_ONLY = (INSMOD_OPT_UNUSED << x), /* n */
123 OPT_REMOVE = (INSMOD_OPT_UNUSED << 2), /* r */
124 //OPT_RESTRICT = (INSMOD_OPT_UNUSED << x), /* t */
125 //OPT_VERONLY = (INSMOD_OPT_UNUSED << x), /* V */
Serj Kalichev85781962010-12-28 04:18:18 +0100126 //OPT_CONFIGFILE = (INSMOD_OPT_UNUSED << x), /* C */
127 OPT_SHOW_DEPS = (INSMOD_OPT_UNUSED << 3), /* D */
128 OPT_BLACKLIST = (INSMOD_OPT_UNUSED << 4) * ENABLE_FEATURE_MODPROBE_BLACKLIST,
Pascal Bellardb82b34e2010-06-07 01:16:45 +0200129};
Serj Kalichev85781962010-12-28 04:18:18 +0100130#if ENABLE_LONG_OPTS
131static const char modprobe_longopts[] ALIGN1 =
132 /* nobody asked for long opts (yet) */
133 // "all\0" No_argument "a"
134 // "list\0" No_argument "l"
135 // "remove\0" No_argument "r"
136 // "quiet\0" No_argument "q"
137 // "verbose\0" No_argument "v"
138 // "syslog\0" No_argument "s"
139 /* module-init-tools 3.11.1 has only long opt --show-depends
140 * but no short -D, we provide long opt for scripts which
141 * were written for 3.11.1: */
Felipe Contreras428bd2d2012-01-31 14:55:15 +0100142 "show-depends\0" No_argument "D"
Serj Kalichev85781962010-12-28 04:18:18 +0100143 // "use-blacklist\0" No_argument "b"
Serj Kalichev85781962010-12-28 04:18:18 +0100144 ;
145#endif
Pascal Bellardb82b34e2010-06-07 01:16:45 +0200146
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200147#define MODULE_FLAG_LOADED 0x0001
148#define MODULE_FLAG_NEED_DEPS 0x0002
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000149/* "was seen in modules.dep": */
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200150#define MODULE_FLAG_FOUND_IN_MODDEP 0x0004
151#define MODULE_FLAG_BLACKLISTED 0x0008
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000152
153struct module_entry { /* I'll call it ME. */
154 unsigned flags;
155 char *modname; /* stripped of /path/, .ext and s/-/_/g */
156 const char *probed_name; /* verbatim as seen on cmdline */
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000157 char *options; /* options from config files */
158 llist_t *realnames; /* strings. if this module is an alias, */
159 /* real module name is one of these. */
160//Can there really be more than one? Example from real kernel?
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000161 llist_t *deps; /* strings. modules we depend on */
Eric Andersen60e56f52002-04-26 06:04:01 +0000162};
163
Timo Terase12e0ac2011-06-20 09:38:13 +0200164#define DB_HASH_SIZE 256
165
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000166struct globals {
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000167 llist_t *probes; /* MEs of module(s) requested on cmdline */
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000168 char *cmdline_mopts; /* module options from cmdline */
169 int num_unresolved_deps;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000170 /* bool. "Did we have 'symbol:FOO' requested on cmdline?" */
171 smallint need_symbols;
Serj Kalicheve4e911e2010-12-26 01:56:19 +0100172 struct utsname uts;
Timo Terase12e0ac2011-06-20 09:38:13 +0200173 llist_t *db[DB_HASH_SIZE]; /* MEs of all modules ever seen (caching for speed) */
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100174} FIX_ALIASING;
Timo Terase12e0ac2011-06-20 09:38:13 +0200175#define G (*ptr_to_globals)
176#define INIT_G() do { \
Denys Vlasenko982e87f2013-07-30 11:52:58 +0200177 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
Timo Terase12e0ac2011-06-20 09:38:13 +0200178} while (0)
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000179
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000180
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000181static int read_config(const char *path);
182
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000183static char *gather_options_str(char *opts, const char *append)
184{
185 /* Speed-optimized. We call gather_options_str many times. */
Denys Vlasenkoda879ec2010-01-26 08:17:45 +0100186 if (append) {
187 if (opts == NULL) {
188 opts = xstrdup(append);
189 } else {
190 int optlen = strlen(opts);
191 opts = xrealloc(opts, optlen + strlen(append) + 2);
192 sprintf(opts + optlen, " %s", append);
193 }
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000194 }
195 return opts;
196}
197
Timo Terase12e0ac2011-06-20 09:38:13 +0200198/* These three functions called many times, optimizing for speed.
199 * Users reported minute-long delays when they runn iptables repeatedly
200 * (iptables use modprobe to install needed kernel modules).
201 */
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000202static struct module_entry *helper_get_module(const char *module, int create)
Denis Vlasenkof45c4f42008-06-16 04:09:25 +0000203{
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000204 char modname[MODULE_NAME_LEN];
205 struct module_entry *e;
206 llist_t *l;
Timo Terase12e0ac2011-06-20 09:38:13 +0200207 unsigned i;
208 unsigned hash;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000209
Denys Vlasenko76b22622015-01-15 03:04:23 +0100210 filename2modname(module, modname);
Timo Terase12e0ac2011-06-20 09:38:13 +0200211
212 hash = 0;
213 for (i = 0; modname[i]; i++)
214 hash = ((hash << 5) + hash) + modname[i];
215 hash %= DB_HASH_SIZE;
216
217 for (l = G.db[hash]; l; l = l->link) {
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000218 e = (struct module_entry *) l->data;
219 if (strcmp(e->modname, modname) == 0)
220 return e;
221 }
222 if (!create)
223 return NULL;
224
225 e = xzalloc(sizeof(*e));
226 e->modname = xstrdup(modname);
Timo Terase12e0ac2011-06-20 09:38:13 +0200227 llist_add_to(&G.db[hash], e);
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000228
229 return e;
230}
Timo Terase12e0ac2011-06-20 09:38:13 +0200231static ALWAYS_INLINE struct module_entry *get_or_add_modentry(const char *module)
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000232{
233 return helper_get_module(module, 1);
234}
Denys Vlasenko76b22622015-01-15 03:04:23 +0100235/* So far this function always gets a module pathname, never an alias name.
236 * The crucial difference is that pathname needs dirname stripping,
237 * while alias name must NOT do it!
238 * Testcase where dirname stripping is likely to go wrong: "modprobe devname:snd/timer"
239 */
240static ALWAYS_INLINE struct module_entry *get_modentry(const char *pathname)
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000241{
Denys Vlasenko76b22622015-01-15 03:04:23 +0100242 return helper_get_module(bb_get_last_path_component_nostrip(pathname), 0);
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000243}
244
245static void add_probe(const char *name)
246{
247 struct module_entry *m;
248
249 m = get_or_add_modentry(name);
Serj Kalicheve4e911e2010-12-26 01:56:19 +0100250 if (!(option_mask32 & (OPT_REMOVE | OPT_SHOW_DEPS))
Denys Vlasenko140def82009-05-26 12:48:34 +0200251 && (m->flags & MODULE_FLAG_LOADED)
252 ) {
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000253 DBG("skipping %s, it is already loaded", name);
254 return;
255 }
256
Mike Frysinger00ffaea2009-05-05 20:13:45 -0400257 DBG("queuing %s", name);
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000258 m->probed_name = name;
259 m->flags |= MODULE_FLAG_NEED_DEPS;
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000260 llist_add_to_end(&G.probes, m);
261 G.num_unresolved_deps++;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000262 if (ENABLE_FEATURE_MODUTILS_SYMBOLS
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100263 && is_prefixed_with(m->modname, "symbol:")
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000264 ) {
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000265 G.need_symbols = 1;
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000266 }
Denis Vlasenko9ddc8d52008-05-18 14:39:43 +0000267}
268
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000269static int FAST_FUNC config_file_action(const char *filename,
270 struct stat *statbuf UNUSED_PARAM,
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000271 void *userdata UNUSED_PARAM,
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000272 int depth UNUSED_PARAM)
Rob Landley72615752006-04-10 16:09:52 +0000273{
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000274 char *tokens[3];
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000275 parser_t *p;
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200276 struct module_entry *m;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000277 int rc = TRUE;
Denis Vlasenkoe1ee48e2008-07-29 00:19:44 +0000278
279 if (bb_basename(filename)[0] == '.')
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000280 goto error;
Denis Vlasenkoe1ee48e2008-07-29 00:19:44 +0000281
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000282 p = config_open2(filename, fopen_for_read);
283 if (p == NULL) {
284 rc = FALSE;
285 goto error;
286 }
Rob Landley72615752006-04-10 16:09:52 +0000287
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000288 while (config_read(p, tokens, 3, 2, "# \t", PARSE_NORMAL)) {
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000289//Use index_in_strings?
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000290 if (strcmp(tokens[0], "alias") == 0) {
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000291 /* alias <wildcard> <modulename> */
292 llist_t *l;
293 char wildcard[MODULE_NAME_LEN];
294 char *rmod;
295
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000296 if (tokens[2] == NULL)
297 continue;
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000298 filename2modname(tokens[1], wildcard);
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000299
Timo Terase12e0ac2011-06-20 09:38:13 +0200300 for (l = G.probes; l; l = l->link) {
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000301 m = (struct module_entry *) l->data;
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000302 if (fnmatch(wildcard, m->modname, 0) != 0)
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000303 continue;
304 rmod = filename2modname(tokens[2], NULL);
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000305 llist_add_to(&m->realnames, rmod);
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000306
307 if (m->flags & MODULE_FLAG_NEED_DEPS) {
308 m->flags &= ~MODULE_FLAG_NEED_DEPS;
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000309 G.num_unresolved_deps--;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000310 }
311
312 m = get_or_add_modentry(rmod);
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000313 if (!(m->flags & MODULE_FLAG_NEED_DEPS)) {
314 m->flags |= MODULE_FLAG_NEED_DEPS;
315 G.num_unresolved_deps++;
316 }
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000317 }
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000318 } else if (strcmp(tokens[0], "options") == 0) {
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000319 /* options <modulename> <option...> */
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000320 if (tokens[2] == NULL)
321 continue;
322 m = get_or_add_modentry(tokens[1]);
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000323 m->options = gather_options_str(m->options, tokens[2]);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000324 } else if (strcmp(tokens[0], "include") == 0) {
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000325 /* include <filename> */
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000326 read_config(tokens[1]);
327 } else if (ENABLE_FEATURE_MODPROBE_BLACKLIST
328 && strcmp(tokens[0], "blacklist") == 0
329 ) {
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000330 /* blacklist <modulename> */
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000331 get_or_add_modentry(tokens[1])->flags |= MODULE_FLAG_BLACKLISTED;
Rob Landley72615752006-04-10 16:09:52 +0000332 }
Rob Landleye9190962005-12-12 19:38:44 +0000333 }
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000334 config_close(p);
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200335 error:
Robert Griebl52e8d062002-05-14 23:42:08 +0000336 return rc;
337}
338
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000339static int read_config(const char *path)
Robert Griebl52e8d062002-05-14 23:42:08 +0000340{
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000341 return recursive_action(path, ACTION_RECURSE | ACTION_QUIET,
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000342 config_file_action, NULL, NULL, 1);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000343}
Eric Andersen3b1a7442003-12-24 20:30:45 +0000344
Denys Vlasenko7eaf58d2009-06-17 20:33:50 +0200345static const char *humanly_readable_name(struct module_entry *m)
346{
347 /* probed_name may be NULL. modname always exists. */
348 return m->probed_name ? m->probed_name : m->modname;
349}
350
Denys Vlasenko3e26d4f2010-02-27 23:15:22 +0100351static char *parse_and_add_kcmdline_module_options(char *options, const char *modulename)
352{
Denys Vlasenko197202d2010-03-03 04:20:33 +0100353 char *kcmdline_buf;
Denys Vlasenko3e26d4f2010-02-27 23:15:22 +0100354 char *kcmdline;
355 char *kptr;
Denys Vlasenko3e26d4f2010-02-27 23:15:22 +0100356
Denys Vlasenko197202d2010-03-03 04:20:33 +0100357 kcmdline_buf = xmalloc_open_read_close("/proc/cmdline", NULL);
358 if (!kcmdline_buf)
Denys Vlasenko3e26d4f2010-02-27 23:15:22 +0100359 return options;
Denys Vlasenko3e26d4f2010-02-27 23:15:22 +0100360
Denys Vlasenko3e26d4f2010-02-27 23:15:22 +0100361 kcmdline = kcmdline_buf;
362 while ((kptr = strsep(&kcmdline, "\n\t ")) != NULL) {
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100363 char *after_modulename = is_prefixed_with(kptr, modulename);
364 if (!after_modulename || *after_modulename != '.')
Denys Vlasenko3e26d4f2010-02-27 23:15:22 +0100365 continue;
366 /* It is "modulename.xxxx" */
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100367 kptr = after_modulename + 1;
Denys Vlasenko3e26d4f2010-02-27 23:15:22 +0100368 if (strchr(kptr, '=') != NULL) {
369 /* It is "modulename.opt=[val]" */
370 options = gather_options_str(options, kptr);
371 }
372 }
Denys Vlasenko197202d2010-03-03 04:20:33 +0100373 free(kcmdline_buf);
Denys Vlasenko3e26d4f2010-02-27 23:15:22 +0100374
375 return options;
376}
377
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200378/* Return: similar to bb_init_module:
379 * 0 on success,
380 * -errno on open/read error,
381 * errno on init_module() error
382 */
Pascal Bellardb82b34e2010-06-07 01:16:45 +0200383/* NB: INSMOD_OPT_SILENT bit suppresses ONLY non-existent modules,
384 * not deleted ones (those are still listed in modules.dep).
385 * module-init-tools version 3.4:
386 * # modprobe bogus
387 * FATAL: Module bogus not found. [exitcode 1]
388 * # modprobe -q bogus [silent, exitcode still 1]
389 * but:
390 * # rm kernel/drivers/net/dummy.ko
391 * # modprobe -q dummy
392 * FATAL: Could not open '/lib/modules/xxx/kernel/drivers/net/dummy.ko': No such file or directory
393 * [exitcode 1]
394 */
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000395static int do_modprobe(struct module_entry *m)
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000396{
Denys Vlasenkof3cbfc02009-05-28 03:54:29 +0200397 int rc, first;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000398
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000399 if (!(m->flags & MODULE_FLAG_FOUND_IN_MODDEP)) {
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200400 if (!(option_mask32 & INSMOD_OPT_SILENT))
Denys Vlasenko7eaf58d2009-06-17 20:33:50 +0200401 bb_error_msg("module %s not found in modules.dep",
402 humanly_readable_name(m));
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000403 return -ENOENT;
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000404 }
405 DBG("do_modprob'ing %s", m->modname);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000406
Serj Kalicheve4e911e2010-12-26 01:56:19 +0100407 if (!(option_mask32 & OPT_REMOVE))
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000408 m->deps = llist_rev(m->deps);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000409
Timo Terase12e0ac2011-06-20 09:38:13 +0200410 if (0) {
411 llist_t *l;
412 for (l = m->deps; l; l = l->link)
413 DBG("dep: %s", l->data);
414 }
Mike Frysinger00ffaea2009-05-05 20:13:45 -0400415
Denys Vlasenkof3cbfc02009-05-28 03:54:29 +0200416 first = 1;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000417 rc = 0;
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200418 while (m->deps) {
Serj Kalichev85781962010-12-28 04:18:18 +0100419 struct module_entry *m2;
420 char *fn, *options;
421
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200422 rc = 0;
423 fn = llist_pop(&m->deps); /* we leak it */
Natanael Copa9de69c02015-01-16 13:53:05 +0100424 m2 = get_or_add_modentry(bb_get_last_path_component_nostrip(fn));
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200425
Serj Kalicheve4e911e2010-12-26 01:56:19 +0100426 if (option_mask32 & OPT_REMOVE) {
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200427 /* modprobe -r */
Denys Vlasenkof3cbfc02009-05-28 03:54:29 +0200428 if (m2->flags & MODULE_FLAG_LOADED) {
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200429 rc = bb_delete_module(m2->modname, O_EXCL);
430 if (rc) {
431 if (first) {
Denys Vlasenko651a2692010-03-23 16:25:17 +0100432 bb_error_msg("can't unload module %s: %s",
Denys Vlasenko7eaf58d2009-06-17 20:33:50 +0200433 humanly_readable_name(m2),
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200434 moderror(rc));
435 break;
436 }
Denys Vlasenkof3cbfc02009-05-28 03:54:29 +0200437 } else {
438 m2->flags &= ~MODULE_FLAG_LOADED;
439 }
440 }
441 /* do not error out if *deps* fail to unload */
442 first = 0;
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200443 continue;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000444 }
445
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200446 options = m2->options;
447 m2->options = NULL;
Denys Vlasenko3e26d4f2010-02-27 23:15:22 +0100448 options = parse_and_add_kcmdline_module_options(options, m2->modname);
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200449 if (m == m2)
450 options = gather_options_str(options, G.cmdline_mopts);
Serj Kalichev85781962010-12-28 04:18:18 +0100451
452 if (option_mask32 & OPT_SHOW_DEPS) {
453 printf(options ? "insmod %s/%s/%s %s\n"
454 : "insmod %s/%s/%s\n",
455 CONFIG_DEFAULT_MODULES_DIR, G.uts.release, fn,
456 options);
457 free(options);
458 continue;
459 }
460
461 if (m2->flags & MODULE_FLAG_LOADED) {
462 DBG("%s is already loaded, skipping", fn);
463 free(options);
464 continue;
465 }
466
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200467 rc = bb_init_module(fn, options);
468 DBG("loaded %s '%s', rc:%d", fn, options, rc);
Denys Vlasenko725b5a32010-01-10 04:52:45 +0100469 if (rc == EEXIST)
470 rc = 0;
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200471 free(options);
472 if (rc) {
Denys Vlasenko651a2692010-03-23 16:25:17 +0100473 bb_error_msg("can't load module %s (%s): %s",
Denys Vlasenko7eaf58d2009-06-17 20:33:50 +0200474 humanly_readable_name(m2),
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200475 fn,
476 moderror(rc)
477 );
478 break;
479 }
480 m2->flags |= MODULE_FLAG_LOADED;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000481 }
Denis Vlasenko9604e1b2009-03-03 18:47:56 +0000482
Eric Andersen908e3622003-06-20 09:56:37 +0000483 return rc;
Robert Griebl52e8d062002-05-14 23:42:08 +0000484}
485
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000486static void load_modules_dep(void)
487{
488 struct module_entry *m;
489 char *colon, *tokens[2];
490 parser_t *p;
491
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200492 /* Modprobe does not work at all without modules.dep,
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000493 * even if the full module name is given. Returning error here
494 * was making us later confuse user with this message:
495 * "module /full/path/to/existing/file/module.ko not found".
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000496 * It's better to die immediately, with good message.
497 * xfopen_for_read provides that. */
498 p = config_open2(CONFIG_DEFAULT_DEPMOD_FILE, xfopen_for_read);
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000499
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000500 while (G.num_unresolved_deps
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000501 && config_read(p, tokens, 2, 1, "# \t", PARSE_NORMAL)
502 ) {
503 colon = last_char_is(tokens[0], ':');
504 if (colon == NULL)
505 continue;
Denys Vlasenko76b22622015-01-15 03:04:23 +0100506 *colon = '\0';
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000507
508 m = get_modentry(tokens[0]);
509 if (m == NULL)
510 continue;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000511
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000512 /* Optimization... */
513 if ((m->flags & MODULE_FLAG_LOADED)
Serj Kalicheve4e911e2010-12-26 01:56:19 +0100514 && !(option_mask32 & (OPT_REMOVE | OPT_SHOW_DEPS))
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000515 ) {
516 DBG("skip deps of %s, it's already loaded", tokens[0]);
517 continue;
518 }
519
520 m->flags |= MODULE_FLAG_FOUND_IN_MODDEP;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000521 if ((m->flags & MODULE_FLAG_NEED_DEPS) && (m->deps == NULL)) {
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000522 G.num_unresolved_deps--;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000523 llist_add_to(&m->deps, xstrdup(tokens[0]));
524 if (tokens[1])
Denys Vlasenko5da42fc2009-08-14 02:10:54 +0200525 string_to_llist(tokens[1], &m->deps, " \t");
Mike Frysinger00ffaea2009-05-05 20:13:45 -0400526 } else
527 DBG("skipping dep line");
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000528 }
529 config_close(p);
530}
531
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000532int modprobe_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko9ddc0042008-08-06 00:51:43 +0000533int modprobe_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen0139ca92001-07-22 23:01:03 +0000534{
Denis Vlasenkobb26db42008-10-31 02:04:28 +0000535 int rc;
536 unsigned opt;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000537 struct module_entry *me;
Eric Andersen0139ca92001-07-22 23:01:03 +0000538
Serj Kalicheve4e911e2010-12-26 01:56:19 +0100539 INIT_G();
540
541 IF_LONG_OPTS(applet_long_options = modprobe_longopts;)
Pascal Bellardb82b34e2010-06-07 01:16:45 +0200542 opt_complementary = MODPROBE_COMPLEMENTARY;
543 opt = getopt32(argv, INSMOD_OPTS MODPROBE_OPTS INSMOD_ARGS);
Denis Vlasenko9ddc0042008-08-06 00:51:43 +0000544 argv += optind;
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000545
Lauri Kasanena48a29f2010-07-08 11:22:30 +0300546 /* Goto modules location */
547 xchdir(CONFIG_DEFAULT_MODULES_DIR);
Serj Kalicheve4e911e2010-12-26 01:56:19 +0100548 uname(&G.uts);
549 xchdir(G.uts.release);
Lauri Kasanena48a29f2010-07-08 11:22:30 +0300550
Serj Kalicheve4e911e2010-12-26 01:56:19 +0100551 if (opt & OPT_LIST_ONLY) {
Denys Vlasenko5dad4ae2011-02-15 02:17:31 +0100552 int i;
Pascal Bellardb82b34e2010-06-07 01:16:45 +0200553 char *colon, *tokens[2];
554 parser_t *p = config_open2(CONFIG_DEFAULT_DEPMOD_FILE, xfopen_for_read);
555
Denys Vlasenko5dad4ae2011-02-15 02:17:31 +0100556 for (i = 0; argv[i]; i++)
557 replace(argv[i], '-', '_');
558
Pascal Bellardb82b34e2010-06-07 01:16:45 +0200559 while (config_read(p, tokens, 2, 1, "# \t", PARSE_NORMAL)) {
560 colon = last_char_is(tokens[0], ':');
561 if (!colon)
562 continue;
563 *colon = '\0';
Pascal Bellardb82b34e2010-06-07 01:16:45 +0200564 if (!argv[0])
565 puts(tokens[0]);
566 else {
Denys Vlasenkoe998b082015-01-15 02:48:36 +0100567 char name[MODULE_NAME_LEN];
568 filename2modname(
569 bb_get_last_path_component_nostrip(tokens[0]),
570 name
571 );
Pascal Bellardb82b34e2010-06-07 01:16:45 +0200572 for (i = 0; argv[i]; i++) {
573 if (fnmatch(argv[i], name, 0) == 0) {
574 puts(tokens[0]);
575 }
576 }
577 }
578 }
579 return EXIT_SUCCESS;
580 }
581
582 /* Yes, for some reason -l ignores -s... */
583 if (opt & INSMOD_OPT_SYSLOG)
584 logmode = LOGMODE_SYSLOG;
Robert Griebl236abbf2002-05-22 23:34:35 +0000585
Denis Vlasenkobb26db42008-10-31 02:04:28 +0000586 if (!argv[0]) {
Serj Kalicheve4e911e2010-12-26 01:56:19 +0100587 if (opt & OPT_REMOVE) {
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000588 /* "modprobe -r" (w/o params).
589 * "If name is NULL, all unused modules marked
590 * autoclean will be removed".
591 */
Denys Vlasenkoab19ede2009-11-11 21:05:42 +0100592 if (bb_delete_module(NULL, O_NONBLOCK | O_EXCL) != 0)
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000593 bb_perror_msg_and_die("rmmod");
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000594 }
Denis Vlasenkobb26db42008-10-31 02:04:28 +0000595 return EXIT_SUCCESS;
596 }
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000597
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000598 /* Retrieve module names of already loaded modules */
Denis Vlasenkobb26db42008-10-31 02:04:28 +0000599 {
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000600 char *s;
Denis Vlasenkobb26db42008-10-31 02:04:28 +0000601 parser_t *parser = config_open2("/proc/modules", fopen_for_read);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000602 while (config_read(parser, &s, 1, 1, "# \t", PARSE_NORMAL & ~PARSE_GREEDY))
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000603 get_or_add_modentry(s)->flags |= MODULE_FLAG_LOADED;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000604 config_close(parser);
605 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000606
Serj Kalicheve4e911e2010-12-26 01:56:19 +0100607 if (opt & (OPT_INSERT_ALL | OPT_REMOVE)) {
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000608 /* Each argument is a module name */
609 do {
Mike Frysinger00ffaea2009-05-05 20:13:45 -0400610 DBG("adding module %s", *argv);
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000611 add_probe(*argv++);
612 } while (*argv);
613 } else {
614 /* First argument is module name, rest are parameters */
Mike Frysinger00ffaea2009-05-05 20:13:45 -0400615 DBG("probing just module %s", *argv);
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000616 add_probe(argv[0]);
Denys Vlasenkoc5830bd2011-02-02 00:00:36 +0100617 G.cmdline_mopts = parse_cmdline_module_options(argv, /*quote_spaces:*/ 1);
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000618 }
619
620 /* Happens if all requested modules are already loaded */
621 if (G.probes == NULL)
622 return EXIT_SUCCESS;
623
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000624 read_config("/etc/modprobe.conf");
625 read_config("/etc/modprobe.d");
626 if (ENABLE_FEATURE_MODUTILS_SYMBOLS && G.need_symbols)
627 read_config("modules.symbols");
628 load_modules_dep();
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000629 if (ENABLE_FEATURE_MODUTILS_ALIAS && G.num_unresolved_deps) {
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000630 read_config("modules.alias");
631 load_modules_dep();
632 }
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000633
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200634 rc = 0;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000635 while ((me = llist_pop(&G.probes)) != NULL) {
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000636 if (me->realnames == NULL) {
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200637 DBG("probing by module name");
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000638 /* This is not an alias. Literal names are blacklisted
639 * only if '-b' is given.
640 */
Serj Kalicheve4e911e2010-12-26 01:56:19 +0100641 if (!(opt & OPT_BLACKLIST)
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000642 || !(me->flags & MODULE_FLAG_BLACKLISTED)
643 ) {
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200644 rc |= do_modprobe(me);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000645 }
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200646 continue;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000647 }
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200648
649 /* Probe all real names for the alias */
650 do {
651 char *realname = llist_pop(&me->realnames);
652 struct module_entry *m2;
653
654 DBG("probing alias %s by realname %s", me->modname, realname);
655 m2 = get_or_add_modentry(realname);
656 if (!(m2->flags & MODULE_FLAG_BLACKLISTED)
657 && (!(m2->flags & MODULE_FLAG_LOADED)
Serj Kalicheve4e911e2010-12-26 01:56:19 +0100658 || (opt & (OPT_REMOVE | OPT_SHOW_DEPS)))
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200659 ) {
660//TODO: we can pass "me" as 2nd param to do_modprobe,
661//and make do_modprobe emit more meaningful error messages
662//with alias name included, not just module name alias resolves to.
663 rc |= do_modprobe(m2);
664 }
665 free(realname);
666 } while (me->realnames != NULL);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000667 }
668
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200669 return (rc != 0);
Eric Andersen0139ca92001-07-22 23:01:03 +0000670}