blob: a5ade2add580e7b8180e48fac601ae90e03f24bc [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
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000011#include "libbb.h"
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000012#include "modutils.h"
Robert Griebl52e8d062002-05-14 23:42:08 +000013#include <sys/utsname.h>
Bernhard Reutner-Fischer2c351a82006-06-03 19:08:49 +000014#include <fnmatch.h>
Eric Andersen0139ca92001-07-22 23:01:03 +000015
Denis Vlasenko0e2f3622009-03-05 16:32:27 +000016#define MODULE_FLAG_LOADED 0x0001
17#define MODULE_FLAG_NEED_DEPS 0x0002
18//Misnomer? Seems to mean "was seen in modules.dep":
19#define MODULE_FLAG_EXISTS 0x0004
20#define MODULE_FLAG_BLACKLISTED 0x0008
21
22struct module_entry { /* I'll call it ME. */
23 unsigned flags;
24 char *modname; /* stripped of /path/, .ext and s/-/_/g */
25 const char *probed_name; /* verbatim as seen on cmdline */
26 llist_t *aliases; /* strings. aliases from config files */
27 llist_t *options; /* strings. options from config files */
28 llist_t *deps; /* strings. modules we depend on */
Eric Andersen60e56f52002-04-26 06:04:01 +000029};
30
Denis Vlasenko0e2f3622009-03-05 16:32:27 +000031#define MODPROBE_OPTS "acdlnrt:VC:" USE_FEATURE_MODPROBE_BLACKLIST("b")
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000032enum {
33 MODPROBE_OPT_INSERT_ALL = (INSMOD_OPT_UNUSED << 0), /* a */
34 MODPROBE_OPT_DUMP_ONLY = (INSMOD_OPT_UNUSED << 1), /* c */
35 MODPROBE_OPT_D = (INSMOD_OPT_UNUSED << 2), /* d */
36 MODPROBE_OPT_LIST_ONLY = (INSMOD_OPT_UNUSED << 3), /* l */
37 MODPROBE_OPT_SHOW_ONLY = (INSMOD_OPT_UNUSED << 4), /* n */
38 MODPROBE_OPT_REMOVE = (INSMOD_OPT_UNUSED << 5), /* r */
39 MODPROBE_OPT_RESTRICT = (INSMOD_OPT_UNUSED << 6), /* t */
40 MODPROBE_OPT_VERONLY = (INSMOD_OPT_UNUSED << 7), /* V */
41 MODPROBE_OPT_CONFIGFILE = (INSMOD_OPT_UNUSED << 8), /* C */
42 MODPROBE_OPT_BLACKLIST = (INSMOD_OPT_UNUSED << 9) * ENABLE_FEATURE_MODPROBE_BLACKLIST,
43};
44
Denis Vlasenko0e2f3622009-03-05 16:32:27 +000045struct globals {
46 llist_t *db; /* MEs of all modules ever seen (caching for speed) */
47 llist_t *probes; /* MEs of module(s) requested on cmdline */
48 llist_t *cmdline_mopts; /* strings. module options (from cmdline) */
49 int num_deps; /* what is this? */
50 /* bool. "Did we have 'symbol:FOO' requested on cmdline?" */
51 smallint need_symbols;
52};
53#define G (*(struct globals*)&bb_common_bufsiz1)
54#define INIT_G() do { } while (0)
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000055
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000056
Denis Vlasenko0e2f3622009-03-05 16:32:27 +000057static int read_config(const char *path);
58
59static struct module_entry *helper_get_module(const char *module, int create)
Denis Vlasenkof45c4f42008-06-16 04:09:25 +000060{
Denis Vlasenko0e2f3622009-03-05 16:32:27 +000061 char modname[MODULE_NAME_LEN];
62 struct module_entry *e;
63 llist_t *l;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000064
Denis Vlasenko0e2f3622009-03-05 16:32:27 +000065 filename2modname(module, modname);
66 for (l = G.db; l != NULL; l = l->link) {
67 e = (struct module_entry *) l->data;
68 if (strcmp(e->modname, modname) == 0)
69 return e;
70 }
71 if (!create)
72 return NULL;
73
74 e = xzalloc(sizeof(*e));
75 e->modname = xstrdup(modname);
76 llist_add_to(&G.db, e);
77
78 return e;
79}
80static struct module_entry *get_or_add_modentry(const char *module)
81{
82 return helper_get_module(module, 1);
83}
84static struct module_entry *get_modentry(const char *module)
85{
86 return helper_get_module(module, 0);
87}
88
89static void add_probe(const char *name)
90{
91 struct module_entry *m;
92
93 m = get_or_add_modentry(name);
94 m->probed_name = name;
95 m->flags |= MODULE_FLAG_NEED_DEPS;
96 llist_add_to(&G.probes, m);
97
98 G.num_deps++;
99 if (ENABLE_FEATURE_MODUTILS_SYMBOLS
100 && strncmp(m->modname, "symbol:", 7) == 0)
101 G.need_symbols = 1;
Denis Vlasenko9ddc8d52008-05-18 14:39:43 +0000102}
103
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000104static int FAST_FUNC config_file_action(const char *filename,
105 struct stat *statbuf UNUSED_PARAM,
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000106 void *userdata UNUSED_PARAM,
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000107 int depth UNUSED_PARAM)
Rob Landley72615752006-04-10 16:09:52 +0000108{
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000109 RESERVE_CONFIG_BUFFER(modname, MODULE_NAME_LEN);
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000110 char *tokens[3], *rmod;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000111 parser_t *p;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000112 llist_t *l;
113 struct module_entry *m;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000114 int rc = TRUE;
Denis Vlasenkoe1ee48e2008-07-29 00:19:44 +0000115
116 if (bb_basename(filename)[0] == '.')
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000117 goto error;
Denis Vlasenkoe1ee48e2008-07-29 00:19:44 +0000118
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000119 p = config_open2(filename, fopen_for_read);
120 if (p == NULL) {
121 rc = FALSE;
122 goto error;
123 }
Rob Landley72615752006-04-10 16:09:52 +0000124
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000125 while (config_read(p, tokens, 3, 2, "# \t", PARSE_NORMAL)) {
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000126//Use index_in_strings?
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000127 if (strcmp(tokens[0], "alias") == 0) {
128 filename2modname(tokens[1], modname);
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000129 if (tokens[2] == NULL)
130 continue;
131
132 for (l = G.probes; l != NULL; l = l->link) {
133 m = (struct module_entry *) l->data;
134 if (fnmatch(modname, m->modname, 0) != 0)
135 continue;
136 rmod = filename2modname(tokens[2], NULL);
137 llist_add_to(&m->aliases, rmod);
138
139 if (m->flags & MODULE_FLAG_NEED_DEPS) {
140 m->flags &= ~MODULE_FLAG_NEED_DEPS;
141 G.num_deps--;
142 }
143
144 m = get_or_add_modentry(rmod);
145 m->flags |= MODULE_FLAG_NEED_DEPS;
146 G.num_deps++;
147 }
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000148 } else if (strcmp(tokens[0], "options") == 0) {
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000149 if (tokens[2] == NULL)
150 continue;
151 m = get_or_add_modentry(tokens[1]);
152 llist_add_to(&m->options, xstrdup(tokens[2]));
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000153 } else if (strcmp(tokens[0], "include") == 0) {
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000154 read_config(tokens[1]);
155 } else if (ENABLE_FEATURE_MODPROBE_BLACKLIST
156 && strcmp(tokens[0], "blacklist") == 0
157 ) {
158 get_or_add_modentry(tokens[1])->flags |= MODULE_FLAG_BLACKLISTED;
Rob Landley72615752006-04-10 16:09:52 +0000159 }
Rob Landleye9190962005-12-12 19:38:44 +0000160 }
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000161 config_close(p);
162error:
Denis Vlasenko9604e1b2009-03-03 18:47:56 +0000163 RELEASE_CONFIG_BUFFER(modname);
Robert Griebl52e8d062002-05-14 23:42:08 +0000164 return rc;
165}
166
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000167static int read_config(const char *path)
Robert Griebl52e8d062002-05-14 23:42:08 +0000168{
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000169 return recursive_action(path, ACTION_RECURSE | ACTION_QUIET,
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000170 config_file_action, NULL, NULL, 1);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000171}
Eric Andersen3b1a7442003-12-24 20:30:45 +0000172
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000173static char *gather_options(char *opts, llist_t *o)
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000174{
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000175 int optlen;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000176
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000177 if (opts == NULL)
178 opts = xstrdup("");
179 optlen = strlen(opts);
Eric Andersen3b1a7442003-12-24 20:30:45 +0000180
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000181 for (; o != NULL; o = o->link) {
182 opts = xrealloc(opts, optlen + strlen(o->data) + 2);
183 optlen += sprintf(opts + optlen, "%s ", o->data);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000184 }
185 return opts;
186}
187
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000188static int do_modprobe(struct module_entry *m)
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000189{
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000190 struct module_entry *m2;
191 char *fn, *options;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000192 int rc = -1;
193
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000194 if (!(m->flags & MODULE_FLAG_EXISTS))
195 return -ENOENT;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000196
197 if (!(option_mask32 & MODPROBE_OPT_REMOVE))
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000198 m->deps = llist_rev(m->deps);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000199
200 rc = 0;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000201 while (m->deps && rc == 0) {
202 fn = llist_pop(&m->deps);
203 m2 = get_or_add_modentry(fn);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000204 if (option_mask32 & MODPROBE_OPT_REMOVE) {
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000205 if (bb_delete_module(m->modname, O_EXCL) != 0)
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000206 rc = errno;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000207 } else if (!(m2->flags & MODULE_FLAG_LOADED)) {
208 options = gather_options(NULL, m2->options);
209 if (m == m2)
210 options = gather_options(options, G.cmdline_mopts);
211//TODO: looks like G.cmdline_mopts can contain either NULL or just one string, not more?
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000212 rc = bb_init_module(fn, options);
213 if (rc == 0)
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000214 m2->flags |= MODULE_FLAG_LOADED;
Denis Vlasenko9604e1b2009-03-03 18:47:56 +0000215 free(options);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000216 }
217
Denis Vlasenko9604e1b2009-03-03 18:47:56 +0000218 free(fn);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000219 }
220
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000221 if (rc > 0 && !(option_mask32 & INSMOD_OPT_SILENT)) {
Denis Vlasenko54d10052008-12-24 03:11:43 +0000222 bb_error_msg("failed to %sload module %s: %s",
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000223 (option_mask32 & MODPROBE_OPT_REMOVE) ? "un" : "",
224 m->probed_name ? m->probed_name : m->modname,
225 moderror(rc)
226 );
227 }
Denis Vlasenko9604e1b2009-03-03 18:47:56 +0000228
Eric Andersen908e3622003-06-20 09:56:37 +0000229 return rc;
Robert Griebl52e8d062002-05-14 23:42:08 +0000230}
231
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000232static void load_modules_dep(void)
233{
234 struct module_entry *m;
235 char *colon, *tokens[2];
236 parser_t *p;
237
238 p = config_open2(CONFIG_DEFAULT_DEPMOD_FILE, xfopen_for_read);
239//Still true?
240 /* Modprobe does not work at all without modprobe.dep,
241 * even if the full module name is given. Returning error here
242 * was making us later confuse user with this message:
243 * "module /full/path/to/existing/file/module.ko not found".
244 * It's better to die immediately, with good message: */
245// if (p == NULL)
246// bb_perror_msg_and_die("can't open '%s'", CONFIG_DEFAULT_DEPMOD_FILE);
247
248 while (G.num_deps
249 && config_read(p, tokens, 2, 1, "# \t", PARSE_NORMAL)
250 ) {
251 colon = last_char_is(tokens[0], ':');
252 if (colon == NULL)
253 continue;
254 *colon = 0;
255
256 m = get_modentry(tokens[0]);
257 if (m == NULL)
258 continue;
259//Can we skip it too if it is MODULE_FLAG_LOADED?
260
261 m->flags |= MODULE_FLAG_EXISTS;
262 if ((m->flags & MODULE_FLAG_NEED_DEPS) && (m->deps == NULL)) {
263 G.num_deps--;
264 llist_add_to(&m->deps, xstrdup(tokens[0]));
265 if (tokens[1])
266 string_to_llist(tokens[1], &m->deps, " ");
267 }
268 }
269 config_close(p);
270}
271
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000272int modprobe_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenko9ddc0042008-08-06 00:51:43 +0000273int modprobe_main(int argc UNUSED_PARAM, char **argv)
Eric Andersen0139ca92001-07-22 23:01:03 +0000274{
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000275 struct utsname uts;
Denis Vlasenkobb26db42008-10-31 02:04:28 +0000276 int rc;
277 unsigned opt;
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000278 struct module_entry *me;
Eric Andersen0139ca92001-07-22 23:01:03 +0000279
Denis Vlasenkoe7fca512007-11-10 01:32:18 +0000280 opt_complementary = "q-v:v-q";
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000281 opt = getopt32(argv, INSMOD_OPTS MODPROBE_OPTS INSMOD_ARGS, NULL, NULL);
Denis Vlasenko9ddc0042008-08-06 00:51:43 +0000282 argv += optind;
Denis Vlasenkob68979a2007-11-02 23:31:10 +0000283
Denis Vlasenkobb26db42008-10-31 02:04:28 +0000284 if (opt & (MODPROBE_OPT_DUMP_ONLY | MODPROBE_OPT_LIST_ONLY |
285 MODPROBE_OPT_SHOW_ONLY))
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000286 bb_error_msg_and_die("not supported");
Robert Griebl236abbf2002-05-22 23:34:35 +0000287
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000288 /* Goto modules location */
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000289 xchdir(CONFIG_DEFAULT_MODULES_DIR);
290 uname(&uts);
291 xchdir(uts.release);
Robert Griebl52e8d062002-05-14 23:42:08 +0000292
Denis Vlasenkobb26db42008-10-31 02:04:28 +0000293 if (!argv[0]) {
294 if (opt & MODPROBE_OPT_REMOVE) {
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000295 /* TODO: comment here */
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000296 if (bb_delete_module(NULL, O_NONBLOCK|O_EXCL) != 0)
297 bb_perror_msg_and_die("rmmod");
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000298 }
Denis Vlasenkobb26db42008-10-31 02:04:28 +0000299 return EXIT_SUCCESS;
300 }
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000301
302 if (opt & MODPROBE_OPT_INSERT_ALL) {
303 /* Each argument is a module name */
304 do {
305 add_probe(*argv++);
306 } while (*argv);
307 G.probes = llist_rev(G.probes);
308 } else {
309 /* First argument is module name, rest are parameters */
310 add_probe(argv[0]);
311//TODO: looks like G.cmdline_mopts can contain either NULL or just one string, not more?
312//optimize it them
313 llist_add_to(&G.cmdline_mopts, parse_cmdline_module_options(argv));
Eric Andersen0139ca92001-07-22 23:01:03 +0000314 }
315
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000316 /* Retrieve module names of already loaded modules */
Denis Vlasenkobb26db42008-10-31 02:04:28 +0000317 {
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000318 char *s;
Denis Vlasenkobb26db42008-10-31 02:04:28 +0000319 parser_t *parser = config_open2("/proc/modules", fopen_for_read);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000320 while (config_read(parser, &s, 1, 1, "# \t", PARSE_NORMAL & ~PARSE_GREEDY))
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000321 get_or_add_modentry(s)->flags |= MODULE_FLAG_LOADED;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000322 config_close(parser);
323 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000324
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000325 read_config("/etc/modprobe.conf");
326 read_config("/etc/modprobe.d");
327 if (ENABLE_FEATURE_MODUTILS_SYMBOLS && G.need_symbols)
328 read_config("modules.symbols");
329 load_modules_dep();
330 if (ENABLE_FEATURE_MODUTILS_ALIAS && G.num_deps) {
331 read_config("modules.alias");
332 load_modules_dep();
333 }
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000334
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000335 while ((me = llist_pop(&G.probes)) != NULL) {
336 if (me->aliases == NULL) {
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000337 /* Try if module by literal name is found; literal
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000338 * names are blacklisted only if '-b' is given. */
339 if (!(opt & MODPROBE_OPT_BLACKLIST)
340 || !(me->flags & MODULE_FLAG_BLACKLISTED)
341 ) {
342 rc = do_modprobe(me);
Denis Vlasenkobb26db42008-10-31 02:04:28 +0000343 if (rc < 0 && !(opt & INSMOD_OPT_SILENT))
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000344 bb_error_msg("module %s not found",
345 me->probed_name);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000346 }
347 } else {
348 /* Probe all aliases */
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000349 do {
350 char *realname = llist_pop(&me->aliases);
351 struct module_entry *m2;
352
353 m2 = get_or_add_modentry(realname);
354 if (!(m2->flags & MODULE_FLAG_BLACKLISTED))
355 do_modprobe(m2);
Denis Vlasenko9604e1b2009-03-03 18:47:56 +0000356 free(realname);
Denis Vlasenko0e2f3622009-03-05 16:32:27 +0000357 } while (me->aliases != NULL);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000358 }
359 }
360
361 return EXIT_SUCCESS;
Eric Andersen0139ca92001-07-22 23:01:03 +0000362}