blob: bef657929a15a4aa4ff0eda6cc24a02cf10846fe [file] [log] [blame]
Eric Andersen0139ca92001-07-22 23:01:03 +00001/* vi: set sw=4 ts=4: */
2/*
Robert Grieblaead70b2002-07-26 15:54:20 +00003 * Modprobe written from scratch for BusyBox
Eric Andersen60e56f52002-04-26 06:04:01 +00004 *
Denis Vlasenkoba1315d2008-09-13 14:59:38 +00005 * Copyright (c) 2008 Timo Teras <timo.teras@iki.fi>
6 * Copyright (c) 2008 Vladimir Dronnikov
Robert Griebl6859d762002-08-05 02:57:12 +00007 *
Rob Landley79e1cab2005-11-15 00:08:29 +00008 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Denis Vlasenkoba1315d2008-09-13 14:59:38 +00009 */
Eric Andersen0139ca92001-07-22 23:01:03 +000010
Mike Frysinger1e12ae92009-05-06 05:24:41 -040011/* Note that unlike older versions of modules.dep/depmod (busybox and m-i-t),
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +020012 * we expect the full dependency list to be specified in modules.dep.
13 * Older versions would only export the direct dependency list.
Mike Frysinger1e12ae92009-05-06 05:24:41 -040014 */
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000015#include "libbb.h"
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000016#include "modutils.h"
Robert Griebl52e8d062002-05-14 23:42:08 +000017#include <sys/utsname.h>
Bernhard Reutner-Fischer2c351a82006-06-03 19:08:49 +000018#include <fnmatch.h>
Eric Andersen0139ca92001-07-22 23:01:03 +000019
Mike Frysinger00ffaea2009-05-05 20:13:45 -040020//#define DBG(fmt, ...) bb_error_msg("%s: " fmt, __func__, ## __VA_ARGS__)
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +000021#define DBG(...) ((void)0)
22
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +020023#define MODULE_FLAG_LOADED 0x0001
24#define MODULE_FLAG_NEED_DEPS 0x0002
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +000025/* "was seen in modules.dep": */
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +020026#define MODULE_FLAG_FOUND_IN_MODDEP 0x0004
27#define MODULE_FLAG_BLACKLISTED 0x0008
Denis Vlasenko0e2f3622009-03-05 16:32:27 +000028
29struct module_entry { /* I'll call it ME. */
30 unsigned flags;
31 char *modname; /* stripped of /path/, .ext and s/-/_/g */
32 const char *probed_name; /* verbatim as seen on cmdline */
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +000033 char *options; /* options from config files */
34 llist_t *realnames; /* strings. if this module is an alias, */
35 /* real module name is one of these. */
36//Can there really be more than one? Example from real kernel?
Denis Vlasenko0e2f3622009-03-05 16:32:27 +000037 llist_t *deps; /* strings. modules we depend on */
Eric Andersen60e56f52002-04-26 06:04:01 +000038};
39
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +020040/* NB: INSMOD_OPT_SILENT bit suppresses ONLY non-existent modules,
41 * not deleted ones (those are still listed in modules.dep).
42 * module-init-tools version 3.4:
43 * # modprobe bogus
44 * FATAL: Module bogus not found. [exitcode 1]
45 * # modprobe -q bogus [silent, exitcode still 1]
46 * but:
47 * # rm kernel/drivers/net/dummy.ko
48 * # modprobe -q dummy
49 * FATAL: Could not open '/lib/modules/xxx/kernel/drivers/net/dummy.ko': No such file or directory
50 * [exitcode 1]
51 */
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000052#define MODPROBE_OPTS "acdlnrt:VC:" IF_FEATURE_MODPROBE_BLACKLIST("b")
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000053enum {
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +020054 MODPROBE_OPT_INSERT_ALL = (INSMOD_OPT_UNUSED << 0), /* a */
55 MODPROBE_OPT_DUMP_ONLY = (INSMOD_OPT_UNUSED << 1), /* c */
56 MODPROBE_OPT_D = (INSMOD_OPT_UNUSED << 2), /* d */
57 MODPROBE_OPT_LIST_ONLY = (INSMOD_OPT_UNUSED << 3), /* l */
58 MODPROBE_OPT_SHOW_ONLY = (INSMOD_OPT_UNUSED << 4), /* n */
59 MODPROBE_OPT_REMOVE = (INSMOD_OPT_UNUSED << 5), /* r */
60 MODPROBE_OPT_RESTRICT = (INSMOD_OPT_UNUSED << 6), /* t */
61 MODPROBE_OPT_VERONLY = (INSMOD_OPT_UNUSED << 7), /* V */
62 MODPROBE_OPT_CONFIGFILE = (INSMOD_OPT_UNUSED << 8), /* C */
63 MODPROBE_OPT_BLACKLIST = (INSMOD_OPT_UNUSED << 9) * ENABLE_FEATURE_MODPROBE_BLACKLIST,
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000064};
65
Denis Vlasenko0e2f3622009-03-05 16:32:27 +000066struct globals {
67 llist_t *db; /* MEs of all modules ever seen (caching for speed) */
68 llist_t *probes; /* MEs of module(s) requested on cmdline */
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +000069 char *cmdline_mopts; /* module options from cmdline */
70 int num_unresolved_deps;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +000071 /* bool. "Did we have 'symbol:FOO' requested on cmdline?" */
72 smallint need_symbols;
73};
74#define G (*(struct globals*)&bb_common_bufsiz1)
75#define INIT_G() do { } while (0)
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000076
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000077
Denis Vlasenko0e2f3622009-03-05 16:32:27 +000078static int read_config(const char *path);
79
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +000080static char *gather_options_str(char *opts, const char *append)
81{
82 /* Speed-optimized. We call gather_options_str many times. */
83 if (opts == NULL) {
84 opts = xstrdup(append);
85 } else {
86 int optlen = strlen(opts);
87 opts = xrealloc(opts, optlen + strlen(append) + 2);
88 sprintf(opts + optlen, " %s", append);
89 }
90 return opts;
91}
92
Denis Vlasenko0e2f3622009-03-05 16:32:27 +000093static struct module_entry *helper_get_module(const char *module, int create)
Denis Vlasenkof45c4f42008-06-16 04:09:25 +000094{
Denis Vlasenko0e2f3622009-03-05 16:32:27 +000095 char modname[MODULE_NAME_LEN];
96 struct module_entry *e;
97 llist_t *l;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000098
Denis Vlasenko0e2f3622009-03-05 16:32:27 +000099 filename2modname(module, modname);
100 for (l = G.db; l != NULL; l = l->link) {
101 e = (struct module_entry *) l->data;
102 if (strcmp(e->modname, modname) == 0)
103 return e;
104 }
105 if (!create)
106 return NULL;
107
108 e = xzalloc(sizeof(*e));
109 e->modname = xstrdup(modname);
110 llist_add_to(&G.db, e);
111
112 return e;
113}
114static struct module_entry *get_or_add_modentry(const char *module)
115{
116 return helper_get_module(module, 1);
117}
118static struct module_entry *get_modentry(const char *module)
119{
120 return helper_get_module(module, 0);
121}
122
123static void add_probe(const char *name)
124{
125 struct module_entry *m;
126
127 m = get_or_add_modentry(name);
Denys Vlasenko140def82009-05-26 12:48:34 +0200128 if (!(option_mask32 & MODPROBE_OPT_REMOVE)
129 && (m->flags & MODULE_FLAG_LOADED)
130 ) {
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000131 DBG("skipping %s, it is already loaded", name);
132 return;
133 }
134
Mike Frysinger00ffaea2009-05-05 20:13:45 -0400135 DBG("queuing %s", name);
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000136 m->probed_name = name;
137 m->flags |= MODULE_FLAG_NEED_DEPS;
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000138 llist_add_to_end(&G.probes, m);
139 G.num_unresolved_deps++;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000140 if (ENABLE_FEATURE_MODUTILS_SYMBOLS
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000141 && strncmp(m->modname, "symbol:", 7) == 0
142 ) {
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000143 G.need_symbols = 1;
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000144 }
Denis Vlasenko9ddc8d52008-05-18 14:39:43 +0000145}
146
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000147static int FAST_FUNC config_file_action(const char *filename,
148 struct stat *statbuf UNUSED_PARAM,
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000149 void *userdata UNUSED_PARAM,
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000150 int depth UNUSED_PARAM)
Rob Landley72615752006-04-10 16:09:52 +0000151{
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000152 char *tokens[3];
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000153 parser_t *p;
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200154 struct module_entry *m;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000155 int rc = TRUE;
Denis Vlasenkoe1ee48e2008-07-29 00:19:44 +0000156
157 if (bb_basename(filename)[0] == '.')
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000158 goto error;
Denis Vlasenkoe1ee48e2008-07-29 00:19:44 +0000159
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000160 p = config_open2(filename, fopen_for_read);
161 if (p == NULL) {
162 rc = FALSE;
163 goto error;
164 }
Rob Landley72615752006-04-10 16:09:52 +0000165
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000166 while (config_read(p, tokens, 3, 2, "# \t", PARSE_NORMAL)) {
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000167//Use index_in_strings?
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000168 if (strcmp(tokens[0], "alias") == 0) {
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000169 /* alias <wildcard> <modulename> */
170 llist_t *l;
171 char wildcard[MODULE_NAME_LEN];
172 char *rmod;
173
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000174 if (tokens[2] == NULL)
175 continue;
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000176 filename2modname(tokens[1], wildcard);
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000177
178 for (l = G.probes; l != NULL; l = l->link) {
179 m = (struct module_entry *) l->data;
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000180 if (fnmatch(wildcard, m->modname, 0) != 0)
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000181 continue;
182 rmod = filename2modname(tokens[2], NULL);
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000183 llist_add_to(&m->realnames, rmod);
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000184
185 if (m->flags & MODULE_FLAG_NEED_DEPS) {
186 m->flags &= ~MODULE_FLAG_NEED_DEPS;
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000187 G.num_unresolved_deps--;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000188 }
189
190 m = get_or_add_modentry(rmod);
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000191 if (!(m->flags & MODULE_FLAG_NEED_DEPS)) {
192 m->flags |= MODULE_FLAG_NEED_DEPS;
193 G.num_unresolved_deps++;
194 }
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000195 }
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000196 } else if (strcmp(tokens[0], "options") == 0) {
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000197 /* options <modulename> <option...> */
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000198 if (tokens[2] == NULL)
199 continue;
200 m = get_or_add_modentry(tokens[1]);
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000201 m->options = gather_options_str(m->options, tokens[2]);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000202 } else if (strcmp(tokens[0], "include") == 0) {
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000203 /* include <filename> */
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000204 read_config(tokens[1]);
205 } else if (ENABLE_FEATURE_MODPROBE_BLACKLIST
206 && strcmp(tokens[0], "blacklist") == 0
207 ) {
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000208 /* blacklist <modulename> */
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000209 get_or_add_modentry(tokens[1])->flags |= MODULE_FLAG_BLACKLISTED;
Rob Landley72615752006-04-10 16:09:52 +0000210 }
Rob Landleye9190962005-12-12 19:38:44 +0000211 }
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000212 config_close(p);
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200213 error:
Robert Griebl52e8d062002-05-14 23:42:08 +0000214 return rc;
215}
216
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000217static int read_config(const char *path)
Robert Griebl52e8d062002-05-14 23:42:08 +0000218{
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000219 return recursive_action(path, ACTION_RECURSE | ACTION_QUIET,
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000220 config_file_action, NULL, NULL, 1);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000221}
Eric Andersen3b1a7442003-12-24 20:30:45 +0000222
Denys Vlasenko7eaf58d2009-06-17 20:33:50 +0200223static const char *humanly_readable_name(struct module_entry *m)
224{
225 /* probed_name may be NULL. modname always exists. */
226 return m->probed_name ? m->probed_name : m->modname;
227}
228
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200229/* Return: similar to bb_init_module:
230 * 0 on success,
231 * -errno on open/read error,
232 * errno on init_module() error
233 */
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000234static int do_modprobe(struct module_entry *m)
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000235{
Denys Vlasenkof3cbfc02009-05-28 03:54:29 +0200236 struct module_entry *m2 = m2; /* for compiler */
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000237 char *fn, *options;
Denys Vlasenkof3cbfc02009-05-28 03:54:29 +0200238 int rc, first;
Mike Frysinger00ffaea2009-05-05 20:13:45 -0400239 llist_t *l;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000240
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000241 if (!(m->flags & MODULE_FLAG_FOUND_IN_MODDEP)) {
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200242 if (!(option_mask32 & INSMOD_OPT_SILENT))
Denys Vlasenko7eaf58d2009-06-17 20:33:50 +0200243 bb_error_msg("module %s not found in modules.dep",
244 humanly_readable_name(m));
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000245 return -ENOENT;
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000246 }
247 DBG("do_modprob'ing %s", m->modname);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000248
249 if (!(option_mask32 & MODPROBE_OPT_REMOVE))
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000250 m->deps = llist_rev(m->deps);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000251
Mike Frysinger00ffaea2009-05-05 20:13:45 -0400252 for (l = m->deps; l != NULL; l = l->link)
253 DBG("dep: %s", l->data);
254
Denys Vlasenkof3cbfc02009-05-28 03:54:29 +0200255 first = 1;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000256 rc = 0;
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200257 while (m->deps) {
258 rc = 0;
259 fn = llist_pop(&m->deps); /* we leak it */
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000260 m2 = get_or_add_modentry(fn);
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200261
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000262 if (option_mask32 & MODPROBE_OPT_REMOVE) {
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200263 /* modprobe -r */
Denys Vlasenkof3cbfc02009-05-28 03:54:29 +0200264 if (m2->flags & MODULE_FLAG_LOADED) {
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200265 rc = bb_delete_module(m2->modname, O_EXCL);
266 if (rc) {
267 if (first) {
268 bb_error_msg("failed to unload module %s: %s",
Denys Vlasenko7eaf58d2009-06-17 20:33:50 +0200269 humanly_readable_name(m2),
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200270 moderror(rc));
271 break;
272 }
Denys Vlasenkof3cbfc02009-05-28 03:54:29 +0200273 } else {
274 m2->flags &= ~MODULE_FLAG_LOADED;
275 }
276 }
277 /* do not error out if *deps* fail to unload */
278 first = 0;
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200279 continue;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000280 }
281
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200282 if (m2->flags & MODULE_FLAG_LOADED) {
283 DBG("%s is already loaded, skipping", fn);
284 continue;
285 }
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000286
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200287 options = m2->options;
288 m2->options = NULL;
289 if (m == m2)
290 options = gather_options_str(options, G.cmdline_mopts);
291 rc = bb_init_module(fn, options);
292 DBG("loaded %s '%s', rc:%d", fn, options, rc);
293 free(options);
294 if (rc) {
295 bb_error_msg("failed to load module %s (%s): %s",
Denys Vlasenko7eaf58d2009-06-17 20:33:50 +0200296 humanly_readable_name(m2),
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200297 fn,
298 moderror(rc)
299 );
300 break;
301 }
302 m2->flags |= MODULE_FLAG_LOADED;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000303 }
Denis Vlasenko9604e1b2009-03-03 18:47:56 +0000304
Eric Andersen908e3622003-06-20 09:56:37 +0000305 return rc;
Robert Griebl52e8d062002-05-14 23:42:08 +0000306}
307
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000308static void load_modules_dep(void)
309{
310 struct module_entry *m;
311 char *colon, *tokens[2];
312 parser_t *p;
313
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200314 /* Modprobe does not work at all without modules.dep,
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000315 * even if the full module name is given. Returning error here
316 * was making us later confuse user with this message:
317 * "module /full/path/to/existing/file/module.ko not found".
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000318 * It's better to die immediately, with good message.
319 * xfopen_for_read provides that. */
320 p = config_open2(CONFIG_DEFAULT_DEPMOD_FILE, xfopen_for_read);
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000321
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000322 while (G.num_unresolved_deps
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000323 && config_read(p, tokens, 2, 1, "# \t", PARSE_NORMAL)
324 ) {
325 colon = last_char_is(tokens[0], ':');
326 if (colon == NULL)
327 continue;
328 *colon = 0;
329
330 m = get_modentry(tokens[0]);
331 if (m == NULL)
332 continue;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000333
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000334 /* Optimization... */
335 if ((m->flags & MODULE_FLAG_LOADED)
336 && !(option_mask32 & MODPROBE_OPT_REMOVE)
337 ) {
338 DBG("skip deps of %s, it's already loaded", tokens[0]);
339 continue;
340 }
341
342 m->flags |= MODULE_FLAG_FOUND_IN_MODDEP;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000343 if ((m->flags & MODULE_FLAG_NEED_DEPS) && (m->deps == NULL)) {
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000344 G.num_unresolved_deps--;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000345 llist_add_to(&m->deps, xstrdup(tokens[0]));
346 if (tokens[1])
347 string_to_llist(tokens[1], &m->deps, " ");
Mike Frysinger00ffaea2009-05-05 20:13:45 -0400348 } else
349 DBG("skipping dep line");
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000350 }
351 config_close(p);
352}
353
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000354int modprobe_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko9ddc0042008-08-06 00:51:43 +0000355int modprobe_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen0139ca92001-07-22 23:01:03 +0000356{
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000357 struct utsname uts;
Denis Vlasenkobb26db42008-10-31 02:04:28 +0000358 int rc;
359 unsigned opt;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000360 struct module_entry *me;
Eric Andersen0139ca92001-07-22 23:01:03 +0000361
Denis Vlasenkoe7fca512007-11-10 01:32:18 +0000362 opt_complementary = "q-v:v-q";
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000363 opt = getopt32(argv, INSMOD_OPTS MODPROBE_OPTS INSMOD_ARGS, NULL, NULL);
Denis Vlasenko9ddc0042008-08-06 00:51:43 +0000364 argv += optind;
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000365
Denis Vlasenkobb26db42008-10-31 02:04:28 +0000366 if (opt & (MODPROBE_OPT_DUMP_ONLY | MODPROBE_OPT_LIST_ONLY |
367 MODPROBE_OPT_SHOW_ONLY))
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000368 bb_error_msg_and_die("not supported");
Robert Griebl236abbf2002-05-22 23:34:35 +0000369
Denis Vlasenkobb26db42008-10-31 02:04:28 +0000370 if (!argv[0]) {
371 if (opt & MODPROBE_OPT_REMOVE) {
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000372 /* "modprobe -r" (w/o params).
373 * "If name is NULL, all unused modules marked
374 * autoclean will be removed".
375 */
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000376 if (bb_delete_module(NULL, O_NONBLOCK|O_EXCL) != 0)
377 bb_perror_msg_and_die("rmmod");
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000378 }
Denis Vlasenkobb26db42008-10-31 02:04:28 +0000379 return EXIT_SUCCESS;
380 }
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000381
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000382 /* Goto modules location */
383 xchdir(CONFIG_DEFAULT_MODULES_DIR);
384 uname(&uts);
385 xchdir(uts.release);
Eric Andersen0139ca92001-07-22 23:01:03 +0000386
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000387 /* Retrieve module names of already loaded modules */
Denis Vlasenkobb26db42008-10-31 02:04:28 +0000388 {
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000389 char *s;
Denis Vlasenkobb26db42008-10-31 02:04:28 +0000390 parser_t *parser = config_open2("/proc/modules", fopen_for_read);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000391 while (config_read(parser, &s, 1, 1, "# \t", PARSE_NORMAL & ~PARSE_GREEDY))
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000392 get_or_add_modentry(s)->flags |= MODULE_FLAG_LOADED;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000393 config_close(parser);
394 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000395
Denys Vlasenko140def82009-05-26 12:48:34 +0200396 if (opt & (MODPROBE_OPT_INSERT_ALL | MODPROBE_OPT_REMOVE)) {
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000397 /* Each argument is a module name */
398 do {
Mike Frysinger00ffaea2009-05-05 20:13:45 -0400399 DBG("adding module %s", *argv);
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000400 add_probe(*argv++);
401 } while (*argv);
402 } else {
403 /* First argument is module name, rest are parameters */
Mike Frysinger00ffaea2009-05-05 20:13:45 -0400404 DBG("probing just module %s", *argv);
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000405 add_probe(argv[0]);
406 G.cmdline_mopts = parse_cmdline_module_options(argv);
407 }
408
409 /* Happens if all requested modules are already loaded */
410 if (G.probes == NULL)
411 return EXIT_SUCCESS;
412
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000413 read_config("/etc/modprobe.conf");
414 read_config("/etc/modprobe.d");
415 if (ENABLE_FEATURE_MODUTILS_SYMBOLS && G.need_symbols)
416 read_config("modules.symbols");
417 load_modules_dep();
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000418 if (ENABLE_FEATURE_MODUTILS_ALIAS && G.num_unresolved_deps) {
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000419 read_config("modules.alias");
420 load_modules_dep();
421 }
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000422
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200423 rc = 0;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000424 while ((me = llist_pop(&G.probes)) != NULL) {
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000425 if (me->realnames == NULL) {
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200426 DBG("probing by module name");
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000427 /* This is not an alias. Literal names are blacklisted
428 * only if '-b' is given.
429 */
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000430 if (!(opt & MODPROBE_OPT_BLACKLIST)
431 || !(me->flags & MODULE_FLAG_BLACKLISTED)
432 ) {
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200433 rc |= do_modprobe(me);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000434 }
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200435 continue;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000436 }
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200437
438 /* Probe all real names for the alias */
439 do {
440 char *realname = llist_pop(&me->realnames);
441 struct module_entry *m2;
442
443 DBG("probing alias %s by realname %s", me->modname, realname);
444 m2 = get_or_add_modentry(realname);
445 if (!(m2->flags & MODULE_FLAG_BLACKLISTED)
446 && (!(m2->flags & MODULE_FLAG_LOADED)
447 || (opt & MODPROBE_OPT_REMOVE))
448 ) {
449//TODO: we can pass "me" as 2nd param to do_modprobe,
450//and make do_modprobe emit more meaningful error messages
451//with alias name included, not just module name alias resolves to.
452 rc |= do_modprobe(m2);
453 }
454 free(realname);
455 } while (me->realnames != NULL);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000456 }
457
Denys Vlasenkoee47f6e2009-06-17 18:46:06 +0200458 return (rc != 0);
Eric Andersen0139ca92001-07-22 23:01:03 +0000459}