blob: 291e4cb906aa03d66a52b21c65a762ed5d4db2db [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 */
Denys Vlasenkoe32b64c2016-11-23 07:54:52 +010010//config:config MODPROBE
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020011//config: bool "modprobe (29 kb)"
Denys Vlasenkoa1cd0d92016-12-23 15:12:27 +010012//config: default y
Denys Vlasenkoe32b64c2016-11-23 07:54:52 +010013//config: select PLATFORM_LINUX
14//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020015//config: Handle the loading of modules, and their dependencies on a high
16//config: level.
Denys Vlasenkoe32b64c2016-11-23 07:54:52 +010017//config:
18//config:config FEATURE_MODPROBE_BLACKLIST
19//config: bool "Blacklist support"
Denys Vlasenkoa1cd0d92016-12-23 15:12:27 +010020//config: default y
21//config: depends on MODPROBE && !MODPROBE_SMALL
Denys Vlasenkoe32b64c2016-11-23 07:54:52 +010022//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020023//config: Say 'y' here to enable support for the 'blacklist' command in
24//config: modprobe.conf. This prevents the alias resolver to resolve
25//config: blacklisted modules. This is useful if you want to prevent your
26//config: hardware autodetection scripts to load modules like evdev, frame
27//config: buffer drivers etc.
Eric Andersen0139ca92001-07-22 23:01:03 +000028
Denys Vlasenko3346b4a2017-08-04 02:56:39 +020029//applet:IF_MODPROBE(IF_NOT_MODPROBE_SMALL(APPLET_NOEXEC(modprobe, modprobe, BB_DIR_SBIN, BB_SUID_DROP, modprobe)))
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +000030
Denys Vlasenkoa1cd0d92016-12-23 15:12:27 +010031//kbuild:ifneq ($(CONFIG_MODPROBE_SMALL),y)
Denys Vlasenkoe32b64c2016-11-23 07:54:52 +010032//kbuild:lib-$(CONFIG_MODPROBE) += modprobe.o modutils.o
Denys Vlasenkoa1cd0d92016-12-23 15:12:27 +010033//kbuild:endif
Denys Vlasenkoe32b64c2016-11-23 07:54:52 +010034
Serj Kalichev85781962010-12-28 04:18:18 +010035#include "libbb.h"
36#include "modutils.h"
37#include <sys/utsname.h>
38#include <fnmatch.h>
39
Denys Vlasenkoe998b082015-01-15 02:48:36 +010040#if 1
Serj Kalichev85781962010-12-28 04:18:18 +010041#define DBG(...) ((void)0)
Denys Vlasenkoe998b082015-01-15 02:48:36 +010042#else
43#define DBG(fmt, ...) bb_error_msg("%s: " fmt, __func__, ## __VA_ARGS__)
44#endif
Serj Kalichev85781962010-12-28 04:18:18 +010045
46/* Note that unlike older versions of modules.dep/depmod (busybox and m-i-t),
47 * we expect the full dependency list to be specified in modules.dep.
48 * Older versions would only export the direct dependency list.
49 */
50
51
Pascal Bellardb82b34e2010-06-07 01:16:45 +020052//usage:#if !ENABLE_MODPROBE_SMALL
53//usage:#define modprobe_notes_usage
54//usage: "modprobe can (un)load a stack of modules, passing each module options (when\n"
55//usage: "loading). modprobe uses a configuration file to determine what option(s) to\n"
56//usage: "pass each module it loads.\n"
57//usage: "\n"
58//usage: "The configuration file is searched (in this order):\n"
59//usage: "\n"
60//usage: " /etc/modprobe.conf (2.6 only)\n"
61//usage: " /etc/modules.conf\n"
62//usage: " /etc/conf.modules (deprecated)\n"
63//usage: "\n"
64//usage: "They all have the same syntax (see below). If none is present, it is\n"
65//usage: "_not_ an error; each loaded module is then expected to load without\n"
66//usage: "options. Once a file is found, the others are tested for.\n"
67//usage: "\n"
68//usage: "/etc/modules.conf entry format:\n"
69//usage: "\n"
70//usage: " alias <alias_name> <mod_name>\n"
71//usage: " Makes it possible to modprobe alias_name, when there is no such module.\n"
72//usage: " It makes sense if your mod_name is long, or you want a more representative\n"
73//usage: " name for that module (eg. 'scsi' in place of 'aha7xxx').\n"
74//usage: " This makes it also possible to use a different set of options (below) for\n"
75//usage: " the module and the alias.\n"
76//usage: " A module can be aliased more than once.\n"
77//usage: "\n"
78//usage: " options <mod_name|alias_name> <symbol=value...>\n"
79//usage: " When loading module mod_name (or the module aliased by alias_name), pass\n"
80//usage: " the \"symbol=value\" pairs as option to that module.\n"
81//usage: "\n"
82//usage: "Sample /etc/modules.conf file:\n"
83//usage: "\n"
84//usage: " options tulip irq=3\n"
85//usage: " alias tulip tulip2\n"
86//usage: " options tulip2 irq=4 io=0x308\n"
87//usage: "\n"
88//usage: "Other functionality offered by 'classic' modprobe is not available in\n"
89//usage: "this implementation.\n"
90//usage: "\n"
91//usage: "If module options are present both in the config file, and on the command line,\n"
92//usage: "then the options from the command line will be passed to the module _after_\n"
93//usage: "the options from the config file. That way, you can have defaults in the config\n"
94//usage: "file, and override them for a specific usage from the command line.\n"
95//usage:#define modprobe_example_usage
96//usage: "(with the above /etc/modules.conf):\n\n"
97//usage: "$ modprobe tulip\n"
98//usage: " will load the module 'tulip' with default option 'irq=3'\n\n"
99//usage: "$ modprobe tulip irq=5\n"
100//usage: " will load the module 'tulip' with option 'irq=5', thus overriding the default\n\n"
101//usage: "$ modprobe tulip2\n"
102//usage: " will load the module 'tulip' with default options 'irq=4 io=0x308',\n"
103//usage: " which are the default for alias 'tulip2'\n\n"
104//usage: "$ modprobe tulip2 irq=8\n"
105//usage: " will load the module 'tulip' with default options 'irq=4 io=0x308 irq=8',\n"
106//usage: " which are the default for alias 'tulip2' overridden by the option 'irq=8'\n\n"
107//usage: " from the command line\n\n"
108//usage: "$ modprobe tulip2 irq=2 io=0x210\n"
109//usage: " will load the module 'tulip' with default options 'irq=4 io=0x308 irq=4 io=0x210',\n"
110//usage: " which are the default for alias 'tulip2' overridden by the options 'irq=2 io=0x210'\n\n"
111//usage: " from the command line\n"
112//usage:
113//usage:#define modprobe_trivial_usage
Serj Kalichev85781962010-12-28 04:18:18 +0100114//usage: "[-alrqvsD" IF_FEATURE_MODPROBE_BLACKLIST("b") "]"
Kang-Che Sungb1d6a2c2017-01-31 21:09:17 +0800115//usage: " MODULE" IF_FEATURE_CMDLINE_MODULE_OPTIONS(" [SYMBOL=VALUE]...")
Pascal Bellardb82b34e2010-06-07 01:16:45 +0200116//usage:#define modprobe_full_usage "\n\n"
Denys Vlasenko66426762011-06-05 03:58:28 +0200117//usage: " -a Load multiple MODULEs"
Serj Kalichev85781962010-12-28 04:18:18 +0100118//usage: "\n -l List (MODULE is a pattern)"
119//usage: "\n -r Remove MODULE (stacks) or do autoclean"
120//usage: "\n -q Quiet"
121//usage: "\n -v Verbose"
122//usage: "\n -s Log to syslog"
123//usage: "\n -D Show dependencies"
Pascal Bellardb82b34e2010-06-07 01:16:45 +0200124//usage: IF_FEATURE_MODPROBE_BLACKLIST(
Serj Kalichev85781962010-12-28 04:18:18 +0100125//usage: "\n -b Apply blacklist to module names too"
Pascal Bellardb82b34e2010-06-07 01:16:45 +0200126//usage: )
127//usage:#endif /* !ENABLE_MODPROBE_SMALL */
128
Felipe Contreras428bd2d2012-01-31 14:55:15 +0100129/* Note: usage text doesn't document various 2.4 options
130 * we pull in through INSMOD_OPTS define
131 * Note2: -b is always accepted, but if !FEATURE_MODPROBE_BLACKLIST,
132 * it is a no-op.
133 */
134#define MODPROBE_OPTS "alrDb"
Serj Kalichev85781962010-12-28 04:18:18 +0100135/* -a and -D _are_ in fact compatible */
Denys Vlasenko22542ec2017-08-08 21:55:02 +0200136#define MODPROBE_COMPLEMENTARY "q-v:v-q:l--arD:r--alD:a--lr:D--rl"
Felipe Contreras428bd2d2012-01-31 14:55:15 +0100137//#define MODPROBE_OPTS "acd:lnrt:C:b"
Serj Kalicheve4e911e2010-12-26 01:56:19 +0100138//#define MODPROBE_COMPLEMENTARY "q-v:v-q:l--acr:a--lr:r--al"
Pascal Bellardb82b34e2010-06-07 01:16:45 +0200139enum {
Serj Kalicheve4e911e2010-12-26 01:56:19 +0100140 OPT_INSERT_ALL = (INSMOD_OPT_UNUSED << 0), /* a */
141 //OPT_DUMP_ONLY = (INSMOD_OPT_UNUSED << x), /* c */
142 //OPT_DIRNAME = (INSMOD_OPT_UNUSED << x), /* d */
143 OPT_LIST_ONLY = (INSMOD_OPT_UNUSED << 1), /* l */
144 //OPT_SHOW_ONLY = (INSMOD_OPT_UNUSED << x), /* n */
145 OPT_REMOVE = (INSMOD_OPT_UNUSED << 2), /* r */
146 //OPT_RESTRICT = (INSMOD_OPT_UNUSED << x), /* t */
147 //OPT_VERONLY = (INSMOD_OPT_UNUSED << x), /* V */
Serj Kalichev85781962010-12-28 04:18:18 +0100148 //OPT_CONFIGFILE = (INSMOD_OPT_UNUSED << x), /* C */
149 OPT_SHOW_DEPS = (INSMOD_OPT_UNUSED << 3), /* D */
150 OPT_BLACKLIST = (INSMOD_OPT_UNUSED << 4) * ENABLE_FEATURE_MODPROBE_BLACKLIST,
Pascal Bellardb82b34e2010-06-07 01:16:45 +0200151};
Serj Kalichev85781962010-12-28 04:18:18 +0100152#if ENABLE_LONG_OPTS
153static const char modprobe_longopts[] ALIGN1 =
154 /* nobody asked for long opts (yet) */
155 // "all\0" No_argument "a"
156 // "list\0" No_argument "l"
157 // "remove\0" No_argument "r"
158 // "quiet\0" No_argument "q"
159 // "verbose\0" No_argument "v"
160 // "syslog\0" No_argument "s"
161 /* module-init-tools 3.11.1 has only long opt --show-depends
162 * but no short -D, we provide long opt for scripts which
163 * were written for 3.11.1: */
Felipe Contreras428bd2d2012-01-31 14:55:15 +0100164 "show-depends\0" No_argument "D"
Serj Kalichev85781962010-12-28 04:18:18 +0100165 // "use-blacklist\0" No_argument "b"
Serj Kalichev85781962010-12-28 04:18:18 +0100166 ;
167#endif
Pascal Bellardb82b34e2010-06-07 01:16:45 +0200168
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200169#define MODULE_FLAG_LOADED 0x0001
170#define MODULE_FLAG_NEED_DEPS 0x0002
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000171/* "was seen in modules.dep": */
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200172#define MODULE_FLAG_FOUND_IN_MODDEP 0x0004
173#define MODULE_FLAG_BLACKLISTED 0x0008
Ben Hutchings803c85a2017-04-06 11:54:04 +0200174#define MODULE_FLAG_BUILTIN 0x0010
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000175
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000176struct globals {
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000177 llist_t *probes; /* MEs of module(s) requested on cmdline */
Kang-Che Sungb1d6a2c2017-01-31 21:09:17 +0800178#if ENABLE_FEATURE_CMDLINE_MODULE_OPTIONS
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000179 char *cmdline_mopts; /* module options from cmdline */
Kang-Che Sungb1d6a2c2017-01-31 21:09:17 +0800180#endif
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000181 int num_unresolved_deps;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000182 /* bool. "Did we have 'symbol:FOO' requested on cmdline?" */
183 smallint need_symbols;
Serj Kalicheve4e911e2010-12-26 01:56:19 +0100184 struct utsname uts;
Timo Teräs48dc80b2015-11-05 18:54:55 +0100185 module_db db;
Denys Vlasenko98a4c7c2010-02-04 15:00:15 +0100186} FIX_ALIASING;
Timo Terase12e0ac2011-06-20 09:38:13 +0200187#define G (*ptr_to_globals)
188#define INIT_G() do { \
Denys Vlasenko982e87f2013-07-30 11:52:58 +0200189 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
Timo Terase12e0ac2011-06-20 09:38:13 +0200190} while (0)
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000191
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000192
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000193static int read_config(const char *path);
194
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000195static char *gather_options_str(char *opts, const char *append)
196{
197 /* Speed-optimized. We call gather_options_str many times. */
Denys Vlasenkoda879ec2010-01-26 08:17:45 +0100198 if (append) {
199 if (opts == NULL) {
200 opts = xstrdup(append);
201 } else {
202 int optlen = strlen(opts);
203 opts = xrealloc(opts, optlen + strlen(append) + 2);
204 sprintf(opts + optlen, " %s", append);
205 }
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000206 }
207 return opts;
208}
209
Timo Teräs48dc80b2015-11-05 18:54:55 +0100210static struct module_entry *get_or_add_modentry(const char *module)
Denis Vlasenkof45c4f42008-06-16 04:09:25 +0000211{
Timo Teräs48dc80b2015-11-05 18:54:55 +0100212 return moddb_get_or_create(&G.db, module);
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000213}
214
215static void add_probe(const char *name)
216{
217 struct module_entry *m;
218
219 m = get_or_add_modentry(name);
Serj Kalicheve4e911e2010-12-26 01:56:19 +0100220 if (!(option_mask32 & (OPT_REMOVE | OPT_SHOW_DEPS))
Ben Hutchings803c85a2017-04-06 11:54:04 +0200221 && (m->flags & (MODULE_FLAG_LOADED | MODULE_FLAG_BUILTIN))
Denys Vlasenko140def82009-05-26 12:48:34 +0200222 ) {
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000223 DBG("skipping %s, it is already loaded", name);
224 return;
225 }
226
Mike Frysinger00ffaea2009-05-05 20:13:45 -0400227 DBG("queuing %s", name);
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000228 m->probed_name = name;
229 m->flags |= MODULE_FLAG_NEED_DEPS;
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000230 llist_add_to_end(&G.probes, m);
231 G.num_unresolved_deps++;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000232 if (ENABLE_FEATURE_MODUTILS_SYMBOLS
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100233 && is_prefixed_with(m->modname, "symbol:")
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000234 ) {
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000235 G.need_symbols = 1;
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000236 }
Denis Vlasenko9ddc8d52008-05-18 14:39:43 +0000237}
238
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000239static int FAST_FUNC config_file_action(const char *filename,
240 struct stat *statbuf UNUSED_PARAM,
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000241 void *userdata UNUSED_PARAM,
Denys Vlasenko20dd4992016-03-29 19:23:55 +0200242 int depth)
Rob Landley72615752006-04-10 16:09:52 +0000243{
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000244 char *tokens[3];
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000245 parser_t *p;
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200246 struct module_entry *m;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000247 int rc = TRUE;
Denys Vlasenkof4fc3032018-11-27 11:26:48 +0100248 const char *base;
Denis Vlasenkoe1ee48e2008-07-29 00:19:44 +0000249
Denys Vlasenko20dd4992016-03-29 19:23:55 +0200250 /* Skip files that begin with a "." */
Mike Frysinger3a5cc982016-02-12 23:26:51 -0500251 base = bb_basename(filename);
252 if (base[0] == '.')
253 goto error;
254
Denys Vlasenko4f0b5402017-04-06 15:22:24 +0200255 /* "man modprobe.d" from kmod version 22 suggests
256 * that we shouldn't recurse into /etc/modprobe.d/dir/
257 * _subdirectories_:
258 */
259 if (depth > 1)
260 return SKIP; /* stop recursing */
261//TODO: instead, can use dirAction in recursive_action() to SKIP dirs
262//on depth == 1 level. But that's more code...
263
Denys Vlasenko20dd4992016-03-29 19:23:55 +0200264 /* In dir recursion, skip files that do not end with a ".conf"
265 * depth==0: read_config("modules.{symbols,alias}") must work,
266 * "include FILE_NOT_ENDING_IN_CONF" must work too.
267 */
268 if (depth != 0) {
Denys Vlasenkof4fc3032018-11-27 11:26:48 +0100269 if (!is_suffixed_with(base, ".conf"))
Denys Vlasenko20dd4992016-03-29 19:23:55 +0200270 goto error;
271 }
Denis Vlasenkoe1ee48e2008-07-29 00:19:44 +0000272
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000273 p = config_open2(filename, fopen_for_read);
274 if (p == NULL) {
275 rc = FALSE;
276 goto error;
277 }
Rob Landley72615752006-04-10 16:09:52 +0000278
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000279 while (config_read(p, tokens, 3, 2, "# \t", PARSE_NORMAL)) {
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000280//Use index_in_strings?
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000281 if (strcmp(tokens[0], "alias") == 0) {
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000282 /* alias <wildcard> <modulename> */
283 llist_t *l;
284 char wildcard[MODULE_NAME_LEN];
285 char *rmod;
286
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000287 if (tokens[2] == NULL)
288 continue;
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000289 filename2modname(tokens[1], wildcard);
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000290
Timo Terase12e0ac2011-06-20 09:38:13 +0200291 for (l = G.probes; l; l = l->link) {
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000292 m = (struct module_entry *) l->data;
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000293 if (fnmatch(wildcard, m->modname, 0) != 0)
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000294 continue;
295 rmod = filename2modname(tokens[2], NULL);
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000296 llist_add_to(&m->realnames, rmod);
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000297
298 if (m->flags & MODULE_FLAG_NEED_DEPS) {
299 m->flags &= ~MODULE_FLAG_NEED_DEPS;
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000300 G.num_unresolved_deps--;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000301 }
302
303 m = get_or_add_modentry(rmod);
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000304 if (!(m->flags & MODULE_FLAG_NEED_DEPS)) {
305 m->flags |= MODULE_FLAG_NEED_DEPS;
306 G.num_unresolved_deps++;
307 }
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000308 }
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000309 } else if (strcmp(tokens[0], "options") == 0) {
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000310 /* options <modulename> <option...> */
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000311 if (tokens[2] == NULL)
312 continue;
313 m = get_or_add_modentry(tokens[1]);
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000314 m->options = gather_options_str(m->options, tokens[2]);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000315 } else if (strcmp(tokens[0], "include") == 0) {
Denys Vlasenko20dd4992016-03-29 19:23:55 +0200316 /* include <filename>/<dirname> (yes, directories also must work) */
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000317 read_config(tokens[1]);
318 } else if (ENABLE_FEATURE_MODPROBE_BLACKLIST
319 && strcmp(tokens[0], "blacklist") == 0
320 ) {
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000321 /* blacklist <modulename> */
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000322 get_or_add_modentry(tokens[1])->flags |= MODULE_FLAG_BLACKLISTED;
Rob Landley72615752006-04-10 16:09:52 +0000323 }
Rob Landleye9190962005-12-12 19:38:44 +0000324 }
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000325 config_close(p);
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200326 error:
Robert Griebl52e8d062002-05-14 23:42:08 +0000327 return rc;
328}
329
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000330static int read_config(const char *path)
Robert Griebl52e8d062002-05-14 23:42:08 +0000331{
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000332 return recursive_action(path, ACTION_RECURSE | ACTION_QUIET,
Denys Vlasenko20dd4992016-03-29 19:23:55 +0200333 config_file_action, NULL, NULL,
334 /*depth:*/ 0);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000335}
Eric Andersen3b1a7442003-12-24 20:30:45 +0000336
Denys Vlasenko7eaf58d2009-06-17 20:33:50 +0200337static const char *humanly_readable_name(struct module_entry *m)
338{
339 /* probed_name may be NULL. modname always exists. */
340 return m->probed_name ? m->probed_name : m->modname;
341}
342
Peter Korsgaard87529732015-09-09 14:55:07 +0200343/* Like strsep(&stringp, "\n\t ") but quoted text goes to single token
344 * even if it contains whitespace.
345 */
346static char *strsep_quotes(char **stringp)
347{
348 char *s, *start = *stringp;
349
350 if (!start)
351 return NULL;
352
353 for (s = start; ; s++) {
354 switch (*s) {
355 case '"':
356 s = strchrnul(s + 1, '"'); /* find trailing quote */
357 if (*s != '\0')
358 s++; /* skip trailing quote */
359 /* fall through */
360 case '\0':
361 case '\n':
362 case '\t':
363 case ' ':
364 if (*s != '\0') {
365 *s = '\0';
366 *stringp = s + 1;
367 } else {
368 *stringp = NULL;
369 }
370 return start;
371 }
372 }
373}
374
Denys Vlasenko3e26d4f2010-02-27 23:15:22 +0100375static char *parse_and_add_kcmdline_module_options(char *options, const char *modulename)
376{
Denys Vlasenko197202d2010-03-03 04:20:33 +0100377 char *kcmdline_buf;
Denys Vlasenko3e26d4f2010-02-27 23:15:22 +0100378 char *kcmdline;
379 char *kptr;
Denys Vlasenko3e26d4f2010-02-27 23:15:22 +0100380
Denys Vlasenko197202d2010-03-03 04:20:33 +0100381 kcmdline_buf = xmalloc_open_read_close("/proc/cmdline", NULL);
382 if (!kcmdline_buf)
Denys Vlasenko3e26d4f2010-02-27 23:15:22 +0100383 return options;
Denys Vlasenko3e26d4f2010-02-27 23:15:22 +0100384
Denys Vlasenko3e26d4f2010-02-27 23:15:22 +0100385 kcmdline = kcmdline_buf;
Peter Korsgaard87529732015-09-09 14:55:07 +0200386 while ((kptr = strsep_quotes(&kcmdline)) != NULL) {
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100387 char *after_modulename = is_prefixed_with(kptr, modulename);
388 if (!after_modulename || *after_modulename != '.')
Denys Vlasenko3e26d4f2010-02-27 23:15:22 +0100389 continue;
390 /* It is "modulename.xxxx" */
Denys Vlasenko8dff01d2015-03-12 17:48:34 +0100391 kptr = after_modulename + 1;
Denys Vlasenko3e26d4f2010-02-27 23:15:22 +0100392 if (strchr(kptr, '=') != NULL) {
393 /* It is "modulename.opt=[val]" */
394 options = gather_options_str(options, kptr);
395 }
396 }
Denys Vlasenko197202d2010-03-03 04:20:33 +0100397 free(kcmdline_buf);
Denys Vlasenko3e26d4f2010-02-27 23:15:22 +0100398
399 return options;
400}
401
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200402/* Return: similar to bb_init_module:
403 * 0 on success,
404 * -errno on open/read error,
405 * errno on init_module() error
406 */
Pascal Bellardb82b34e2010-06-07 01:16:45 +0200407/* NB: INSMOD_OPT_SILENT bit suppresses ONLY non-existent modules,
408 * not deleted ones (those are still listed in modules.dep).
409 * module-init-tools version 3.4:
410 * # modprobe bogus
411 * FATAL: Module bogus not found. [exitcode 1]
412 * # modprobe -q bogus [silent, exitcode still 1]
413 * but:
414 * # rm kernel/drivers/net/dummy.ko
415 * # modprobe -q dummy
416 * FATAL: Could not open '/lib/modules/xxx/kernel/drivers/net/dummy.ko': No such file or directory
417 * [exitcode 1]
418 */
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000419static int do_modprobe(struct module_entry *m)
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000420{
Denys Vlasenkof3cbfc02009-05-28 03:54:29 +0200421 int rc, first;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000422
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000423 if (!(m->flags & MODULE_FLAG_FOUND_IN_MODDEP)) {
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200424 if (!(option_mask32 & INSMOD_OPT_SILENT))
Ben Hutchings803c85a2017-04-06 11:54:04 +0200425 bb_error_msg((m->flags & MODULE_FLAG_BUILTIN) ?
426 "module %s is builtin" :
427 "module %s not found in modules.dep",
428 humanly_readable_name(m));
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000429 return -ENOENT;
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000430 }
431 DBG("do_modprob'ing %s", m->modname);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000432
Serj Kalicheve4e911e2010-12-26 01:56:19 +0100433 if (!(option_mask32 & OPT_REMOVE))
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000434 m->deps = llist_rev(m->deps);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000435
Timo Terase12e0ac2011-06-20 09:38:13 +0200436 if (0) {
437 llist_t *l;
438 for (l = m->deps; l; l = l->link)
439 DBG("dep: %s", l->data);
440 }
Mike Frysinger00ffaea2009-05-05 20:13:45 -0400441
Denys Vlasenkof3cbfc02009-05-28 03:54:29 +0200442 first = 1;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000443 rc = 0;
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200444 while (m->deps) {
Serj Kalichev85781962010-12-28 04:18:18 +0100445 struct module_entry *m2;
446 char *fn, *options;
447
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200448 rc = 0;
449 fn = llist_pop(&m->deps); /* we leak it */
Natanael Copa9de69c02015-01-16 13:53:05 +0100450 m2 = get_or_add_modentry(bb_get_last_path_component_nostrip(fn));
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200451
Serj Kalicheve4e911e2010-12-26 01:56:19 +0100452 if (option_mask32 & OPT_REMOVE) {
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200453 /* modprobe -r */
Denys Vlasenkof3cbfc02009-05-28 03:54:29 +0200454 if (m2->flags & MODULE_FLAG_LOADED) {
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200455 rc = bb_delete_module(m2->modname, O_EXCL);
456 if (rc) {
457 if (first) {
Denys Vlasenko941e7a42015-10-24 04:19:56 +0200458 bb_perror_msg("can't unload module '%s'",
Denys Vlasenkocd139742015-10-24 04:17:04 +0200459 humanly_readable_name(m2));
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200460 break;
461 }
Denys Vlasenkof3cbfc02009-05-28 03:54:29 +0200462 } else {
463 m2->flags &= ~MODULE_FLAG_LOADED;
464 }
465 }
466 /* do not error out if *deps* fail to unload */
467 first = 0;
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200468 continue;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000469 }
470
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200471 options = m2->options;
472 m2->options = NULL;
Denys Vlasenko3e26d4f2010-02-27 23:15:22 +0100473 options = parse_and_add_kcmdline_module_options(options, m2->modname);
Kang-Che Sungb1d6a2c2017-01-31 21:09:17 +0800474#if ENABLE_FEATURE_CMDLINE_MODULE_OPTIONS
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200475 if (m == m2)
476 options = gather_options_str(options, G.cmdline_mopts);
Kang-Che Sungb1d6a2c2017-01-31 21:09:17 +0800477#endif
Serj Kalichev85781962010-12-28 04:18:18 +0100478
479 if (option_mask32 & OPT_SHOW_DEPS) {
480 printf(options ? "insmod %s/%s/%s %s\n"
481 : "insmod %s/%s/%s\n",
482 CONFIG_DEFAULT_MODULES_DIR, G.uts.release, fn,
483 options);
484 free(options);
485 continue;
486 }
487
488 if (m2->flags & MODULE_FLAG_LOADED) {
489 DBG("%s is already loaded, skipping", fn);
490 free(options);
491 continue;
492 }
493
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200494 rc = bb_init_module(fn, options);
495 DBG("loaded %s '%s', rc:%d", fn, options, rc);
Denys Vlasenko725b5a32010-01-10 04:52:45 +0100496 if (rc == EEXIST)
497 rc = 0;
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200498 free(options);
499 if (rc) {
Denys Vlasenko651a2692010-03-23 16:25:17 +0100500 bb_error_msg("can't load module %s (%s): %s",
Denys Vlasenko7eaf58d2009-06-17 20:33:50 +0200501 humanly_readable_name(m2),
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200502 fn,
503 moderror(rc)
504 );
505 break;
506 }
507 m2->flags |= MODULE_FLAG_LOADED;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000508 }
Denis Vlasenko9604e1b2009-03-03 18:47:56 +0000509
Eric Andersen908e3622003-06-20 09:56:37 +0000510 return rc;
Robert Griebl52e8d062002-05-14 23:42:08 +0000511}
512
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000513static void load_modules_dep(void)
514{
515 struct module_entry *m;
516 char *colon, *tokens[2];
517 parser_t *p;
518
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200519 /* Modprobe does not work at all without modules.dep,
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000520 * even if the full module name is given. Returning error here
521 * was making us later confuse user with this message:
522 * "module /full/path/to/existing/file/module.ko not found".
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000523 * It's better to die immediately, with good message.
524 * xfopen_for_read provides that. */
525 p = config_open2(CONFIG_DEFAULT_DEPMOD_FILE, xfopen_for_read);
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000526
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000527 while (G.num_unresolved_deps
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000528 && config_read(p, tokens, 2, 1, "# \t", PARSE_NORMAL)
529 ) {
530 colon = last_char_is(tokens[0], ':');
531 if (colon == NULL)
532 continue;
Denys Vlasenko76b22622015-01-15 03:04:23 +0100533 *colon = '\0';
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000534
Timo Teräs48dc80b2015-11-05 18:54:55 +0100535 m = moddb_get(&G.db, bb_get_last_path_component_nostrip(tokens[0]));
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000536 if (m == NULL)
537 continue;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000538
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000539 /* Optimization... */
540 if ((m->flags & MODULE_FLAG_LOADED)
Serj Kalicheve4e911e2010-12-26 01:56:19 +0100541 && !(option_mask32 & (OPT_REMOVE | OPT_SHOW_DEPS))
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000542 ) {
543 DBG("skip deps of %s, it's already loaded", tokens[0]);
544 continue;
545 }
546
547 m->flags |= MODULE_FLAG_FOUND_IN_MODDEP;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000548 if ((m->flags & MODULE_FLAG_NEED_DEPS) && (m->deps == NULL)) {
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000549 G.num_unresolved_deps--;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000550 llist_add_to(&m->deps, xstrdup(tokens[0]));
551 if (tokens[1])
Denys Vlasenko5da42fc2009-08-14 02:10:54 +0200552 string_to_llist(tokens[1], &m->deps, " \t");
Mike Frysinger00ffaea2009-05-05 20:13:45 -0400553 } else
554 DBG("skipping dep line");
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000555 }
556 config_close(p);
557}
558
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000559int modprobe_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko9ddc0042008-08-06 00:51:43 +0000560int modprobe_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen0139ca92001-07-22 23:01:03 +0000561{
Denis Vlasenkobb26db42008-10-31 02:04:28 +0000562 int rc;
563 unsigned opt;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000564 struct module_entry *me;
Eric Andersen0139ca92001-07-22 23:01:03 +0000565
Serj Kalicheve4e911e2010-12-26 01:56:19 +0100566 INIT_G();
567
Denys Vlasenko22542ec2017-08-08 21:55:02 +0200568 opt = getopt32long(argv, "^" INSMOD_OPTS MODPROBE_OPTS "\0" MODPROBE_COMPLEMENTARY,
569 modprobe_longopts
570 INSMOD_ARGS
571 );
Denis Vlasenko9ddc0042008-08-06 00:51:43 +0000572 argv += optind;
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000573
Lauri Kasanena48a29f2010-07-08 11:22:30 +0300574 /* Goto modules location */
575 xchdir(CONFIG_DEFAULT_MODULES_DIR);
Serj Kalicheve4e911e2010-12-26 01:56:19 +0100576 uname(&G.uts);
577 xchdir(G.uts.release);
Lauri Kasanena48a29f2010-07-08 11:22:30 +0300578
Serj Kalicheve4e911e2010-12-26 01:56:19 +0100579 if (opt & OPT_LIST_ONLY) {
Denys Vlasenko5dad4ae2011-02-15 02:17:31 +0100580 int i;
Pascal Bellardb82b34e2010-06-07 01:16:45 +0200581 char *colon, *tokens[2];
582 parser_t *p = config_open2(CONFIG_DEFAULT_DEPMOD_FILE, xfopen_for_read);
583
Denys Vlasenko5dad4ae2011-02-15 02:17:31 +0100584 for (i = 0; argv[i]; i++)
585 replace(argv[i], '-', '_');
586
Pascal Bellardb82b34e2010-06-07 01:16:45 +0200587 while (config_read(p, tokens, 2, 1, "# \t", PARSE_NORMAL)) {
588 colon = last_char_is(tokens[0], ':');
589 if (!colon)
590 continue;
591 *colon = '\0';
Pascal Bellardb82b34e2010-06-07 01:16:45 +0200592 if (!argv[0])
593 puts(tokens[0]);
594 else {
Denys Vlasenkoe998b082015-01-15 02:48:36 +0100595 char name[MODULE_NAME_LEN];
596 filename2modname(
597 bb_get_last_path_component_nostrip(tokens[0]),
598 name
599 );
Pascal Bellardb82b34e2010-06-07 01:16:45 +0200600 for (i = 0; argv[i]; i++) {
601 if (fnmatch(argv[i], name, 0) == 0) {
602 puts(tokens[0]);
603 }
604 }
605 }
606 }
607 return EXIT_SUCCESS;
608 }
609
610 /* Yes, for some reason -l ignores -s... */
611 if (opt & INSMOD_OPT_SYSLOG)
612 logmode = LOGMODE_SYSLOG;
Robert Griebl236abbf2002-05-22 23:34:35 +0000613
Denis Vlasenkobb26db42008-10-31 02:04:28 +0000614 if (!argv[0]) {
Serj Kalicheve4e911e2010-12-26 01:56:19 +0100615 if (opt & OPT_REMOVE) {
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000616 /* "modprobe -r" (w/o params).
617 * "If name is NULL, all unused modules marked
618 * autoclean will be removed".
619 */
Denys Vlasenkoab19ede2009-11-11 21:05:42 +0100620 if (bb_delete_module(NULL, O_NONBLOCK | O_EXCL) != 0)
Denys Vlasenkocd139742015-10-24 04:17:04 +0200621 bb_perror_nomsg_and_die();
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000622 }
Denis Vlasenkobb26db42008-10-31 02:04:28 +0000623 return EXIT_SUCCESS;
624 }
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000625
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000626 /* Retrieve module names of already loaded modules */
Denis Vlasenkobb26db42008-10-31 02:04:28 +0000627 {
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000628 char *s;
Denis Vlasenkobb26db42008-10-31 02:04:28 +0000629 parser_t *parser = config_open2("/proc/modules", fopen_for_read);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000630 while (config_read(parser, &s, 1, 1, "# \t", PARSE_NORMAL & ~PARSE_GREEDY))
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000631 get_or_add_modentry(s)->flags |= MODULE_FLAG_LOADED;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000632 config_close(parser);
Ben Hutchings803c85a2017-04-06 11:54:04 +0200633
634 parser = config_open2("modules.builtin", fopen_for_read);
635 while (config_read(parser, &s, 1, 1, "# \t", PARSE_NORMAL))
636 get_or_add_modentry(s)->flags |= MODULE_FLAG_BUILTIN;
637 config_close(parser);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000638 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000639
Serj Kalicheve4e911e2010-12-26 01:56:19 +0100640 if (opt & (OPT_INSERT_ALL | OPT_REMOVE)) {
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000641 /* Each argument is a module name */
642 do {
Mike Frysinger00ffaea2009-05-05 20:13:45 -0400643 DBG("adding module %s", *argv);
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000644 add_probe(*argv++);
645 } while (*argv);
646 } else {
647 /* First argument is module name, rest are parameters */
Mike Frysinger00ffaea2009-05-05 20:13:45 -0400648 DBG("probing just module %s", *argv);
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000649 add_probe(argv[0]);
Kang-Che Sungb1d6a2c2017-01-31 21:09:17 +0800650#if ENABLE_FEATURE_CMDLINE_MODULE_OPTIONS
Denys Vlasenkoc5830bd2011-02-02 00:00:36 +0100651 G.cmdline_mopts = parse_cmdline_module_options(argv, /*quote_spaces:*/ 1);
Kang-Che Sungb1d6a2c2017-01-31 21:09:17 +0800652#endif
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000653 }
654
655 /* Happens if all requested modules are already loaded */
656 if (G.probes == NULL)
657 return EXIT_SUCCESS;
658
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000659 read_config("/etc/modprobe.conf");
660 read_config("/etc/modprobe.d");
661 if (ENABLE_FEATURE_MODUTILS_SYMBOLS && G.need_symbols)
662 read_config("modules.symbols");
663 load_modules_dep();
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000664 if (ENABLE_FEATURE_MODUTILS_ALIAS && G.num_unresolved_deps) {
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000665 read_config("modules.alias");
666 load_modules_dep();
667 }
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000668
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200669 rc = 0;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000670 while ((me = llist_pop(&G.probes)) != NULL) {
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000671 if (me->realnames == NULL) {
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200672 DBG("probing by module name");
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000673 /* This is not an alias. Literal names are blacklisted
674 * only if '-b' is given.
675 */
Serj Kalicheve4e911e2010-12-26 01:56:19 +0100676 if (!(opt & OPT_BLACKLIST)
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000677 || !(me->flags & MODULE_FLAG_BLACKLISTED)
678 ) {
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200679 rc |= do_modprobe(me);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000680 }
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200681 continue;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000682 }
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200683
684 /* Probe all real names for the alias */
685 do {
686 char *realname = llist_pop(&me->realnames);
687 struct module_entry *m2;
688
689 DBG("probing alias %s by realname %s", me->modname, realname);
690 m2 = get_or_add_modentry(realname);
691 if (!(m2->flags & MODULE_FLAG_BLACKLISTED)
692 && (!(m2->flags & MODULE_FLAG_LOADED)
Serj Kalicheve4e911e2010-12-26 01:56:19 +0100693 || (opt & (OPT_REMOVE | OPT_SHOW_DEPS)))
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200694 ) {
695//TODO: we can pass "me" as 2nd param to do_modprobe,
696//and make do_modprobe emit more meaningful error messages
697//with alias name included, not just module name alias resolves to.
698 rc |= do_modprobe(m2);
699 }
700 free(realname);
701 } while (me->realnames != NULL);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000702 }
703
Timo Teräs48dc80b2015-11-05 18:54:55 +0100704 if (ENABLE_FEATURE_CLEAN_UP)
705 moddb_free(&G.db);
706
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200707 return (rc != 0);
Eric Andersen0139ca92001-07-22 23:01:03 +0000708}