blob: d57dbdf75a688e8f48e171e34463f7b131e6d8f1 [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),
12 * we expect the full dependency list to be specified in modules.dep. Older
13 * versions would only export the direct dependency list.
14 */
15
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000016#include "libbb.h"
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000017#include "modutils.h"
Robert Griebl52e8d062002-05-14 23:42:08 +000018#include <sys/utsname.h>
Bernhard Reutner-Fischer2c351a82006-06-03 19:08:49 +000019#include <fnmatch.h>
Eric Andersen0139ca92001-07-22 23:01:03 +000020
Mike Frysinger00ffaea2009-05-05 20:13:45 -040021//#define DBG(fmt, ...) bb_error_msg("%s: " fmt, __func__, ## __VA_ARGS__)
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +000022#define DBG(...) ((void)0)
23
Denis Vlasenko0e2f3622009-03-05 16:32:27 +000024#define MODULE_FLAG_LOADED 0x0001
25#define MODULE_FLAG_NEED_DEPS 0x0002
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +000026/* "was seen in modules.dep": */
27#define MODULE_FLAG_FOUND_IN_MODDEP 0x0004
Denis Vlasenko0e2f3622009-03-05 16:32:27 +000028#define MODULE_FLAG_BLACKLISTED 0x0008
29
30struct module_entry { /* I'll call it ME. */
31 unsigned flags;
32 char *modname; /* stripped of /path/, .ext and s/-/_/g */
33 const char *probed_name; /* verbatim as seen on cmdline */
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +000034 char *options; /* options from config files */
35 llist_t *realnames; /* strings. if this module is an alias, */
36 /* real module name is one of these. */
37//Can there really be more than one? Example from real kernel?
Denis Vlasenko0e2f3622009-03-05 16:32:27 +000038 llist_t *deps; /* strings. modules we depend on */
Eric Andersen60e56f52002-04-26 06:04:01 +000039};
40
Denis Vlasenko5e34ff22009-04-21 11:09:40 +000041#define MODPROBE_OPTS "acdlnrt:VC:" IF_FEATURE_MODPROBE_BLACKLIST("b")
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000042enum {
43 MODPROBE_OPT_INSERT_ALL = (INSMOD_OPT_UNUSED << 0), /* a */
44 MODPROBE_OPT_DUMP_ONLY = (INSMOD_OPT_UNUSED << 1), /* c */
45 MODPROBE_OPT_D = (INSMOD_OPT_UNUSED << 2), /* d */
46 MODPROBE_OPT_LIST_ONLY = (INSMOD_OPT_UNUSED << 3), /* l */
47 MODPROBE_OPT_SHOW_ONLY = (INSMOD_OPT_UNUSED << 4), /* n */
48 MODPROBE_OPT_REMOVE = (INSMOD_OPT_UNUSED << 5), /* r */
49 MODPROBE_OPT_RESTRICT = (INSMOD_OPT_UNUSED << 6), /* t */
50 MODPROBE_OPT_VERONLY = (INSMOD_OPT_UNUSED << 7), /* V */
51 MODPROBE_OPT_CONFIGFILE = (INSMOD_OPT_UNUSED << 8), /* C */
52 MODPROBE_OPT_BLACKLIST = (INSMOD_OPT_UNUSED << 9) * ENABLE_FEATURE_MODPROBE_BLACKLIST,
53};
54
Denis Vlasenko0e2f3622009-03-05 16:32:27 +000055struct globals {
56 llist_t *db; /* MEs of all modules ever seen (caching for speed) */
57 llist_t *probes; /* MEs of module(s) requested on cmdline */
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +000058 char *cmdline_mopts; /* module options from cmdline */
59 int num_unresolved_deps;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +000060 /* bool. "Did we have 'symbol:FOO' requested on cmdline?" */
61 smallint need_symbols;
62};
63#define G (*(struct globals*)&bb_common_bufsiz1)
64#define INIT_G() do { } while (0)
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000065
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000066
Denis Vlasenko0e2f3622009-03-05 16:32:27 +000067static int read_config(const char *path);
68
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +000069static char *gather_options_str(char *opts, const char *append)
70{
71 /* Speed-optimized. We call gather_options_str many times. */
72 if (opts == NULL) {
73 opts = xstrdup(append);
74 } else {
75 int optlen = strlen(opts);
76 opts = xrealloc(opts, optlen + strlen(append) + 2);
77 sprintf(opts + optlen, " %s", append);
78 }
79 return opts;
80}
81
Denis Vlasenko0e2f3622009-03-05 16:32:27 +000082static struct module_entry *helper_get_module(const char *module, int create)
Denis Vlasenkof45c4f42008-06-16 04:09:25 +000083{
Denis Vlasenko0e2f3622009-03-05 16:32:27 +000084 char modname[MODULE_NAME_LEN];
85 struct module_entry *e;
86 llist_t *l;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000087
Denis Vlasenko0e2f3622009-03-05 16:32:27 +000088 filename2modname(module, modname);
89 for (l = G.db; l != NULL; l = l->link) {
90 e = (struct module_entry *) l->data;
91 if (strcmp(e->modname, modname) == 0)
92 return e;
93 }
94 if (!create)
95 return NULL;
96
97 e = xzalloc(sizeof(*e));
98 e->modname = xstrdup(modname);
99 llist_add_to(&G.db, e);
100
101 return e;
102}
103static struct module_entry *get_or_add_modentry(const char *module)
104{
105 return helper_get_module(module, 1);
106}
107static struct module_entry *get_modentry(const char *module)
108{
109 return helper_get_module(module, 0);
110}
111
112static void add_probe(const char *name)
113{
114 struct module_entry *m;
115
116 m = get_or_add_modentry(name);
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000117 if (m->flags & MODULE_FLAG_LOADED) {
118 DBG("skipping %s, it is already loaded", name);
119 return;
120 }
121
Mike Frysinger00ffaea2009-05-05 20:13:45 -0400122 DBG("queuing %s", name);
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000123 m->probed_name = name;
124 m->flags |= MODULE_FLAG_NEED_DEPS;
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000125 llist_add_to_end(&G.probes, m);
126 G.num_unresolved_deps++;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000127 if (ENABLE_FEATURE_MODUTILS_SYMBOLS
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000128 && strncmp(m->modname, "symbol:", 7) == 0
129 ) {
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000130 G.need_symbols = 1;
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000131 }
Denis Vlasenko9ddc8d52008-05-18 14:39:43 +0000132}
133
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000134static int FAST_FUNC config_file_action(const char *filename,
135 struct stat *statbuf UNUSED_PARAM,
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000136 void *userdata UNUSED_PARAM,
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000137 int depth UNUSED_PARAM)
Rob Landley72615752006-04-10 16:09:52 +0000138{
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000139 char *tokens[3];
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000140 parser_t *p;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000141 struct module_entry *m;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000142 int rc = TRUE;
Denis Vlasenkoe1ee48e2008-07-29 00:19:44 +0000143
144 if (bb_basename(filename)[0] == '.')
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000145 goto error;
Denis Vlasenkoe1ee48e2008-07-29 00:19:44 +0000146
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000147 p = config_open2(filename, fopen_for_read);
148 if (p == NULL) {
149 rc = FALSE;
150 goto error;
151 }
Rob Landley72615752006-04-10 16:09:52 +0000152
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000153 while (config_read(p, tokens, 3, 2, "# \t", PARSE_NORMAL)) {
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000154//Use index_in_strings?
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000155 if (strcmp(tokens[0], "alias") == 0) {
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000156 /* alias <wildcard> <modulename> */
157 llist_t *l;
158 char wildcard[MODULE_NAME_LEN];
159 char *rmod;
160
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000161 if (tokens[2] == NULL)
162 continue;
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000163 filename2modname(tokens[1], wildcard);
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000164
165 for (l = G.probes; l != NULL; l = l->link) {
166 m = (struct module_entry *) l->data;
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000167 if (fnmatch(wildcard, m->modname, 0) != 0)
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000168 continue;
169 rmod = filename2modname(tokens[2], NULL);
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000170 llist_add_to(&m->realnames, rmod);
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000171
172 if (m->flags & MODULE_FLAG_NEED_DEPS) {
173 m->flags &= ~MODULE_FLAG_NEED_DEPS;
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000174 G.num_unresolved_deps--;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000175 }
176
177 m = get_or_add_modentry(rmod);
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000178 if (!(m->flags & MODULE_FLAG_NEED_DEPS)) {
179 m->flags |= MODULE_FLAG_NEED_DEPS;
180 G.num_unresolved_deps++;
181 }
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000182 }
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000183 } else if (strcmp(tokens[0], "options") == 0) {
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000184 /* options <modulename> <option...> */
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000185 if (tokens[2] == NULL)
186 continue;
187 m = get_or_add_modentry(tokens[1]);
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000188 m->options = gather_options_str(m->options, tokens[2]);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000189 } else if (strcmp(tokens[0], "include") == 0) {
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000190 /* include <filename> */
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000191 read_config(tokens[1]);
192 } else if (ENABLE_FEATURE_MODPROBE_BLACKLIST
193 && strcmp(tokens[0], "blacklist") == 0
194 ) {
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000195 /* blacklist <modulename> */
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000196 get_or_add_modentry(tokens[1])->flags |= MODULE_FLAG_BLACKLISTED;
Rob Landley72615752006-04-10 16:09:52 +0000197 }
Rob Landleye9190962005-12-12 19:38:44 +0000198 }
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000199 config_close(p);
200error:
Robert Griebl52e8d062002-05-14 23:42:08 +0000201 return rc;
202}
203
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000204static int read_config(const char *path)
Robert Griebl52e8d062002-05-14 23:42:08 +0000205{
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000206 return recursive_action(path, ACTION_RECURSE | ACTION_QUIET,
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000207 config_file_action, NULL, NULL, 1);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000208}
Eric Andersen3b1a7442003-12-24 20:30:45 +0000209
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000210static int do_modprobe(struct module_entry *m)
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000211{
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000212 struct module_entry *m2;
213 char *fn, *options;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000214 int rc = -1;
Mike Frysinger00ffaea2009-05-05 20:13:45 -0400215 llist_t *l;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000216
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000217 if (!(m->flags & MODULE_FLAG_FOUND_IN_MODDEP)) {
218 DBG("skipping %s, not found in modules.dep", m->modname);
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000219 return -ENOENT;
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000220 }
221 DBG("do_modprob'ing %s", m->modname);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000222
223 if (!(option_mask32 & MODPROBE_OPT_REMOVE))
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000224 m->deps = llist_rev(m->deps);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000225
Mike Frysinger00ffaea2009-05-05 20:13:45 -0400226 for (l = m->deps; l != NULL; l = l->link)
227 DBG("dep: %s", l->data);
228
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000229 rc = 0;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000230 while (m->deps && rc == 0) {
231 fn = llist_pop(&m->deps);
232 m2 = get_or_add_modentry(fn);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000233 if (option_mask32 & MODPROBE_OPT_REMOVE) {
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000234 if (bb_delete_module(m->modname, O_EXCL) != 0)
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000235 rc = errno;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000236 } else if (!(m2->flags & MODULE_FLAG_LOADED)) {
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000237 options = m2->options;
238 m2->options = NULL;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000239 if (m == m2)
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000240 options = gather_options_str(options, G.cmdline_mopts);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000241 rc = bb_init_module(fn, options);
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000242 DBG("loaded %s '%s', rc:%d", fn, options, rc);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000243 if (rc == 0)
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000244 m2->flags |= MODULE_FLAG_LOADED;
Denis Vlasenko9604e1b2009-03-03 18:47:56 +0000245 free(options);
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000246 } else {
247 DBG("%s is already loaded, skipping", fn);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000248 }
249
Denis Vlasenko9604e1b2009-03-03 18:47:56 +0000250 free(fn);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000251 }
252
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000253//FIXME: what if rc < 0?
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000254 if (rc > 0 && !(option_mask32 & INSMOD_OPT_SILENT)) {
Denis Vlasenko54d10052008-12-24 03:11:43 +0000255 bb_error_msg("failed to %sload module %s: %s",
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000256 (option_mask32 & MODPROBE_OPT_REMOVE) ? "un" : "",
257 m->probed_name ? m->probed_name : m->modname,
258 moderror(rc)
259 );
260 }
Denis Vlasenko9604e1b2009-03-03 18:47:56 +0000261
Eric Andersen908e3622003-06-20 09:56:37 +0000262 return rc;
Robert Griebl52e8d062002-05-14 23:42:08 +0000263}
264
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000265static void load_modules_dep(void)
266{
267 struct module_entry *m;
268 char *colon, *tokens[2];
269 parser_t *p;
270
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000271 /* Modprobe does not work at all without modprobe.dep,
272 * even if the full module name is given. Returning error here
273 * was making us later confuse user with this message:
274 * "module /full/path/to/existing/file/module.ko not found".
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000275 * It's better to die immediately, with good message.
276 * xfopen_for_read provides that. */
277 p = config_open2(CONFIG_DEFAULT_DEPMOD_FILE, xfopen_for_read);
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000278
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000279 while (G.num_unresolved_deps
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000280 && config_read(p, tokens, 2, 1, "# \t", PARSE_NORMAL)
281 ) {
282 colon = last_char_is(tokens[0], ':');
283 if (colon == NULL)
284 continue;
285 *colon = 0;
286
287 m = get_modentry(tokens[0]);
288 if (m == NULL)
289 continue;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000290
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000291 /* Optimization... */
292 if ((m->flags & MODULE_FLAG_LOADED)
293 && !(option_mask32 & MODPROBE_OPT_REMOVE)
294 ) {
295 DBG("skip deps of %s, it's already loaded", tokens[0]);
296 continue;
297 }
298
299 m->flags |= MODULE_FLAG_FOUND_IN_MODDEP;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000300 if ((m->flags & MODULE_FLAG_NEED_DEPS) && (m->deps == NULL)) {
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000301 G.num_unresolved_deps--;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000302 llist_add_to(&m->deps, xstrdup(tokens[0]));
303 if (tokens[1])
304 string_to_llist(tokens[1], &m->deps, " ");
Mike Frysinger00ffaea2009-05-05 20:13:45 -0400305 } else
306 DBG("skipping dep line");
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000307 }
308 config_close(p);
309}
310
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000311int modprobe_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko9ddc0042008-08-06 00:51:43 +0000312int modprobe_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen0139ca92001-07-22 23:01:03 +0000313{
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000314 struct utsname uts;
Denis Vlasenkobb26db42008-10-31 02:04:28 +0000315 int rc;
316 unsigned opt;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000317 struct module_entry *me;
Eric Andersen0139ca92001-07-22 23:01:03 +0000318
Denis Vlasenkoe7fca512007-11-10 01:32:18 +0000319 opt_complementary = "q-v:v-q";
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000320 opt = getopt32(argv, INSMOD_OPTS MODPROBE_OPTS INSMOD_ARGS, NULL, NULL);
Denis Vlasenko9ddc0042008-08-06 00:51:43 +0000321 argv += optind;
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000322
Denis Vlasenkobb26db42008-10-31 02:04:28 +0000323 if (opt & (MODPROBE_OPT_DUMP_ONLY | MODPROBE_OPT_LIST_ONLY |
324 MODPROBE_OPT_SHOW_ONLY))
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000325 bb_error_msg_and_die("not supported");
Robert Griebl236abbf2002-05-22 23:34:35 +0000326
Denis Vlasenkobb26db42008-10-31 02:04:28 +0000327 if (!argv[0]) {
328 if (opt & MODPROBE_OPT_REMOVE) {
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000329 /* "modprobe -r" (w/o params).
330 * "If name is NULL, all unused modules marked
331 * autoclean will be removed".
332 */
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000333 if (bb_delete_module(NULL, O_NONBLOCK|O_EXCL) != 0)
334 bb_perror_msg_and_die("rmmod");
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000335 }
Denis Vlasenkobb26db42008-10-31 02:04:28 +0000336 return EXIT_SUCCESS;
337 }
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000338
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000339 /* Goto modules location */
340 xchdir(CONFIG_DEFAULT_MODULES_DIR);
341 uname(&uts);
342 xchdir(uts.release);
Eric Andersen0139ca92001-07-22 23:01:03 +0000343
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000344 /* Retrieve module names of already loaded modules */
Denis Vlasenkobb26db42008-10-31 02:04:28 +0000345 {
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000346 char *s;
Denis Vlasenkobb26db42008-10-31 02:04:28 +0000347 parser_t *parser = config_open2("/proc/modules", fopen_for_read);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000348 while (config_read(parser, &s, 1, 1, "# \t", PARSE_NORMAL & ~PARSE_GREEDY))
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000349 get_or_add_modentry(s)->flags |= MODULE_FLAG_LOADED;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000350 config_close(parser);
351 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000352
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000353 if (opt & MODPROBE_OPT_INSERT_ALL) {
354 /* Each argument is a module name */
355 do {
Mike Frysinger00ffaea2009-05-05 20:13:45 -0400356 DBG("adding module %s", *argv);
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000357 add_probe(*argv++);
358 } while (*argv);
359 } else {
360 /* First argument is module name, rest are parameters */
Mike Frysinger00ffaea2009-05-05 20:13:45 -0400361 DBG("probing just module %s", *argv);
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000362 add_probe(argv[0]);
363 G.cmdline_mopts = parse_cmdline_module_options(argv);
364 }
365
366 /* Happens if all requested modules are already loaded */
367 if (G.probes == NULL)
368 return EXIT_SUCCESS;
369
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000370 read_config("/etc/modprobe.conf");
371 read_config("/etc/modprobe.d");
372 if (ENABLE_FEATURE_MODUTILS_SYMBOLS && G.need_symbols)
373 read_config("modules.symbols");
374 load_modules_dep();
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000375 if (ENABLE_FEATURE_MODUTILS_ALIAS && G.num_unresolved_deps) {
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000376 read_config("modules.alias");
377 load_modules_dep();
378 }
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000379
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000380 while ((me = llist_pop(&G.probes)) != NULL) {
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000381 if (me->realnames == NULL) {
382 /* This is not an alias. Literal names are blacklisted
383 * only if '-b' is given.
384 */
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000385 if (!(opt & MODPROBE_OPT_BLACKLIST)
386 || !(me->flags & MODULE_FLAG_BLACKLISTED)
387 ) {
388 rc = do_modprobe(me);
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000389//FIXME: what if rc > 0?
Denis Vlasenkobb26db42008-10-31 02:04:28 +0000390 if (rc < 0 && !(opt & INSMOD_OPT_SILENT))
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000391 bb_error_msg("module %s not found",
392 me->probed_name);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000393 }
394 } else {
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000395 /* Probe all realnames */
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000396 do {
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000397 char *realname = llist_pop(&me->realnames);
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000398 struct module_entry *m2;
399
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000400 DBG("probing %s by realname %s", me->modname, realname);
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000401 m2 = get_or_add_modentry(realname);
402 if (!(m2->flags & MODULE_FLAG_BLACKLISTED))
403 do_modprobe(m2);
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000404//FIXME: error check?
Denis Vlasenko9604e1b2009-03-03 18:47:56 +0000405 free(realname);
Denis Vlasenkoa6b6f052009-03-06 22:48:11 +0000406 } while (me->realnames != NULL);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000407 }
408 }
409
410 return EXIT_SUCCESS;
Eric Andersen0139ca92001-07-22 23:01:03 +0000411}