modprobe: add more useful debug points
[oweals/busybox.git] / modutils / modprobe.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Modprobe written from scratch for BusyBox
4  *
5  * Copyright (c) 2008 Timo Teras <timo.teras@iki.fi>
6  * Copyright (c) 2008 Vladimir Dronnikov
7  *
8  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
9  */
10
11 #include "libbb.h"
12 #include "modutils.h"
13 #include <sys/utsname.h>
14 #include <fnmatch.h>
15
16 //#define DBG(fmt, ...) bb_error_msg("%s: " fmt, __func__, ## __VA_ARGS__)
17 #define DBG(...) ((void)0)
18
19 #define MODULE_FLAG_LOADED              0x0001
20 #define MODULE_FLAG_NEED_DEPS           0x0002
21 /* "was seen in modules.dep": */
22 #define MODULE_FLAG_FOUND_IN_MODDEP     0x0004
23 #define MODULE_FLAG_BLACKLISTED         0x0008
24
25 struct module_entry { /* I'll call it ME. */
26         unsigned flags;
27         char *modname; /* stripped of /path/, .ext and s/-/_/g */
28         const char *probed_name; /* verbatim as seen on cmdline */
29         char *options; /* options from config files */
30         llist_t *realnames; /* strings. if this module is an alias, */
31         /* real module name is one of these. */
32 //Can there really be more than one? Example from real kernel?
33         llist_t *deps; /* strings. modules we depend on */
34 };
35
36 #define MODPROBE_OPTS  "acdlnrt:VC:" IF_FEATURE_MODPROBE_BLACKLIST("b")
37 enum {
38         MODPROBE_OPT_INSERT_ALL = (INSMOD_OPT_UNUSED << 0), /* a */
39         MODPROBE_OPT_DUMP_ONLY  = (INSMOD_OPT_UNUSED << 1), /* c */
40         MODPROBE_OPT_D          = (INSMOD_OPT_UNUSED << 2), /* d */
41         MODPROBE_OPT_LIST_ONLY  = (INSMOD_OPT_UNUSED << 3), /* l */
42         MODPROBE_OPT_SHOW_ONLY  = (INSMOD_OPT_UNUSED << 4), /* n */
43         MODPROBE_OPT_REMOVE     = (INSMOD_OPT_UNUSED << 5), /* r */
44         MODPROBE_OPT_RESTRICT   = (INSMOD_OPT_UNUSED << 6), /* t */
45         MODPROBE_OPT_VERONLY    = (INSMOD_OPT_UNUSED << 7), /* V */
46         MODPROBE_OPT_CONFIGFILE = (INSMOD_OPT_UNUSED << 8), /* C */
47         MODPROBE_OPT_BLACKLIST  = (INSMOD_OPT_UNUSED << 9) * ENABLE_FEATURE_MODPROBE_BLACKLIST,
48 };
49
50 struct globals {
51         llist_t *db; /* MEs of all modules ever seen (caching for speed) */
52         llist_t *probes; /* MEs of module(s) requested on cmdline */
53         char *cmdline_mopts; /* module options from cmdline */
54         int num_unresolved_deps;
55         /* bool. "Did we have 'symbol:FOO' requested on cmdline?" */
56         smallint need_symbols;
57 };
58 #define G (*(struct globals*)&bb_common_bufsiz1)
59 #define INIT_G() do { } while (0)
60
61
62 static int read_config(const char *path);
63
64 static char *gather_options_str(char *opts, const char *append)
65 {
66         /* Speed-optimized. We call gather_options_str many times. */
67         if (opts == NULL) {
68                 opts = xstrdup(append);
69         } else {
70                 int optlen = strlen(opts);
71                 opts = xrealloc(opts, optlen + strlen(append) + 2);
72                 sprintf(opts + optlen, " %s", append);
73         }
74         return opts;
75 }
76
77 static struct module_entry *helper_get_module(const char *module, int create)
78 {
79         char modname[MODULE_NAME_LEN];
80         struct module_entry *e;
81         llist_t *l;
82
83         filename2modname(module, modname);
84         for (l = G.db; l != NULL; l = l->link) {
85                 e = (struct module_entry *) l->data;
86                 if (strcmp(e->modname, modname) == 0)
87                         return e;
88         }
89         if (!create)
90                 return NULL;
91
92         e = xzalloc(sizeof(*e));
93         e->modname = xstrdup(modname);
94         llist_add_to(&G.db, e);
95
96         return e;
97 }
98 static struct module_entry *get_or_add_modentry(const char *module)
99 {
100         return helper_get_module(module, 1);
101 }
102 static struct module_entry *get_modentry(const char *module)
103 {
104         return helper_get_module(module, 0);
105 }
106
107 static void add_probe(const char *name)
108 {
109         struct module_entry *m;
110
111         m = get_or_add_modentry(name);
112         if (m->flags & MODULE_FLAG_LOADED) {
113                 DBG("skipping %s, it is already loaded", name);
114                 return;
115         }
116
117         DBG("queuing %s", name);
118         m->probed_name = name;
119         m->flags |= MODULE_FLAG_NEED_DEPS;
120         llist_add_to_end(&G.probes, m);
121         G.num_unresolved_deps++;
122         if (ENABLE_FEATURE_MODUTILS_SYMBOLS
123          && strncmp(m->modname, "symbol:", 7) == 0
124         ) {
125                 G.need_symbols = 1;
126         }
127 }
128
129 static int FAST_FUNC config_file_action(const char *filename,
130                                         struct stat *statbuf UNUSED_PARAM,
131                                         void *userdata UNUSED_PARAM,
132                                         int depth UNUSED_PARAM)
133 {
134         char *tokens[3];
135         parser_t *p;
136         struct module_entry *m;
137         int rc = TRUE;
138
139         if (bb_basename(filename)[0] == '.')
140                 goto error;
141
142         p = config_open2(filename, fopen_for_read);
143         if (p == NULL) {
144                 rc = FALSE;
145                 goto error;
146         }
147
148         while (config_read(p, tokens, 3, 2, "# \t", PARSE_NORMAL)) {
149 //Use index_in_strings?
150                 if (strcmp(tokens[0], "alias") == 0) {
151                         /* alias <wildcard> <modulename> */
152                         llist_t *l;
153                         char wildcard[MODULE_NAME_LEN];
154                         char *rmod;
155
156                         if (tokens[2] == NULL)
157                                 continue;
158                         filename2modname(tokens[1], wildcard);
159
160                         for (l = G.probes; l != NULL; l = l->link) {
161                                 m = (struct module_entry *) l->data;
162                                 if (fnmatch(wildcard, m->modname, 0) != 0)
163                                         continue;
164                                 rmod = filename2modname(tokens[2], NULL);
165                                 llist_add_to(&m->realnames, rmod);
166
167                                 if (m->flags & MODULE_FLAG_NEED_DEPS) {
168                                         m->flags &= ~MODULE_FLAG_NEED_DEPS;
169                                         G.num_unresolved_deps--;
170                                 }
171
172                                 m = get_or_add_modentry(rmod);
173                                 if (!(m->flags & MODULE_FLAG_NEED_DEPS)) {
174                                         m->flags |= MODULE_FLAG_NEED_DEPS;
175                                         G.num_unresolved_deps++;
176                                 }
177                         }
178                 } else if (strcmp(tokens[0], "options") == 0) {
179                         /* options <modulename> <option...> */
180                         if (tokens[2] == NULL)
181                                 continue;
182                         m = get_or_add_modentry(tokens[1]);
183                         m->options = gather_options_str(m->options, tokens[2]);
184                 } else if (strcmp(tokens[0], "include") == 0) {
185                         /* include <filename> */
186                         read_config(tokens[1]);
187                 } else if (ENABLE_FEATURE_MODPROBE_BLACKLIST
188                  && strcmp(tokens[0], "blacklist") == 0
189                 ) {
190                         /* blacklist <modulename> */
191                         get_or_add_modentry(tokens[1])->flags |= MODULE_FLAG_BLACKLISTED;
192                 }
193         }
194         config_close(p);
195 error:
196         return rc;
197 }
198
199 static int read_config(const char *path)
200 {
201         return recursive_action(path, ACTION_RECURSE | ACTION_QUIET,
202                                 config_file_action, NULL, NULL, 1);
203 }
204
205 static int do_modprobe(struct module_entry *m)
206 {
207         struct module_entry *m2;
208         char *fn, *options;
209         int rc = -1;
210         llist_t *l;
211
212         if (!(m->flags & MODULE_FLAG_FOUND_IN_MODDEP)) {
213                 DBG("skipping %s, not found in modules.dep", m->modname);
214                 return -ENOENT;
215         }
216         DBG("do_modprob'ing %s", m->modname);
217
218         if (!(option_mask32 & MODPROBE_OPT_REMOVE))
219                 m->deps = llist_rev(m->deps);
220
221         for (l = m->deps; l != NULL; l = l->link)
222                 DBG("dep: %s", l->data);
223
224         rc = 0;
225         while (m->deps && rc == 0) {
226                 fn = llist_pop(&m->deps);
227                 m2 = get_or_add_modentry(fn);
228                 if (option_mask32 & MODPROBE_OPT_REMOVE) {
229                         if (bb_delete_module(m->modname, O_EXCL) != 0)
230                                 rc = errno;
231                 } else if (!(m2->flags & MODULE_FLAG_LOADED)) {
232                         options = m2->options;
233                         m2->options = NULL;
234                         if (m == m2)
235                                 options = gather_options_str(options, G.cmdline_mopts);
236                         rc = bb_init_module(fn, options);
237                         DBG("loaded %s '%s', rc:%d", fn, options, rc);
238                         if (rc == 0)
239                                 m2->flags |= MODULE_FLAG_LOADED;
240                         free(options);
241                 } else {
242                         DBG("%s is already loaded, skipping", fn);
243                 }
244
245                 free(fn);
246         }
247
248 //FIXME: what if rc < 0?
249         if (rc > 0 && !(option_mask32 & INSMOD_OPT_SILENT)) {
250                 bb_error_msg("failed to %sload module %s: %s",
251                         (option_mask32 & MODPROBE_OPT_REMOVE) ? "un" : "",
252                         m->probed_name ? m->probed_name : m->modname,
253                         moderror(rc)
254                 );
255         }
256
257         return rc;
258 }
259
260 static void load_modules_dep(void)
261 {
262         struct module_entry *m;
263         char *colon, *tokens[2];
264         parser_t *p;
265
266         /* Modprobe does not work at all without modprobe.dep,
267          * even if the full module name is given. Returning error here
268          * was making us later confuse user with this message:
269          * "module /full/path/to/existing/file/module.ko not found".
270          * It's better to die immediately, with good message.
271          * xfopen_for_read provides that. */
272         p = config_open2(CONFIG_DEFAULT_DEPMOD_FILE, xfopen_for_read);
273
274         while (G.num_unresolved_deps
275          && config_read(p, tokens, 2, 1, "# \t", PARSE_NORMAL)
276         ) {
277                 colon = last_char_is(tokens[0], ':');
278                 if (colon == NULL)
279                         continue;
280                 *colon = 0;
281
282                 m = get_modentry(tokens[0]);
283                 if (m == NULL)
284                         continue;
285
286                 /* Optimization... */
287                 if ((m->flags & MODULE_FLAG_LOADED)
288                  && !(option_mask32 & MODPROBE_OPT_REMOVE)
289                 ) {
290                         DBG("skip deps of %s, it's already loaded", tokens[0]);
291                         continue;
292                 }
293
294                 m->flags |= MODULE_FLAG_FOUND_IN_MODDEP;
295                 if ((m->flags & MODULE_FLAG_NEED_DEPS) && (m->deps == NULL)) {
296                         G.num_unresolved_deps--;
297                         llist_add_to(&m->deps, xstrdup(tokens[0]));
298                         if (tokens[1])
299                                 string_to_llist(tokens[1], &m->deps, " ");
300                 } else
301                         DBG("skipping dep line");
302         }
303         config_close(p);
304 }
305
306 int modprobe_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
307 int modprobe_main(int argc UNUSED_PARAM, char **argv)
308 {
309         struct utsname uts;
310         int rc;
311         unsigned opt;
312         struct module_entry *me;
313
314         opt_complementary = "q-v:v-q";
315         opt = getopt32(argv, INSMOD_OPTS MODPROBE_OPTS INSMOD_ARGS, NULL, NULL);
316         argv += optind;
317
318         if (opt & (MODPROBE_OPT_DUMP_ONLY | MODPROBE_OPT_LIST_ONLY |
319                                 MODPROBE_OPT_SHOW_ONLY))
320                 bb_error_msg_and_die("not supported");
321
322         if (!argv[0]) {
323                 if (opt & MODPROBE_OPT_REMOVE) {
324                         /* "modprobe -r" (w/o params).
325                          * "If name is NULL, all unused modules marked
326                          * autoclean will be removed".
327                          */
328                         if (bb_delete_module(NULL, O_NONBLOCK|O_EXCL) != 0)
329                                 bb_perror_msg_and_die("rmmod");
330                 }
331                 return EXIT_SUCCESS;
332         }
333
334         /* Goto modules location */
335         xchdir(CONFIG_DEFAULT_MODULES_DIR);
336         uname(&uts);
337         xchdir(uts.release);
338
339         /* Retrieve module names of already loaded modules */
340         {
341                 char *s;
342                 parser_t *parser = config_open2("/proc/modules", fopen_for_read);
343                 while (config_read(parser, &s, 1, 1, "# \t", PARSE_NORMAL & ~PARSE_GREEDY))
344                         get_or_add_modentry(s)->flags |= MODULE_FLAG_LOADED;
345                 config_close(parser);
346         }
347
348         if (opt & MODPROBE_OPT_INSERT_ALL) {
349                 /* Each argument is a module name */
350                 do {
351                         DBG("adding module %s", *argv);
352                         add_probe(*argv++);
353                 } while (*argv);
354         } else {
355                 /* First argument is module name, rest are parameters */
356                 DBG("probing just module %s", *argv);
357                 add_probe(argv[0]);
358                 G.cmdline_mopts = parse_cmdline_module_options(argv);
359         }
360
361         /* Happens if all requested modules are already loaded */
362         if (G.probes == NULL)
363                 return EXIT_SUCCESS;
364
365         read_config("/etc/modprobe.conf");
366         read_config("/etc/modprobe.d");
367         if (ENABLE_FEATURE_MODUTILS_SYMBOLS && G.need_symbols)
368                 read_config("modules.symbols");
369         load_modules_dep();
370         if (ENABLE_FEATURE_MODUTILS_ALIAS && G.num_unresolved_deps) {
371                 read_config("modules.alias");
372                 load_modules_dep();
373         }
374
375         while ((me = llist_pop(&G.probes)) != NULL) {
376                 if (me->realnames == NULL) {
377                         /* This is not an alias. Literal names are blacklisted
378                          * only if '-b' is given.
379                          */
380                         if (!(opt & MODPROBE_OPT_BLACKLIST)
381                          || !(me->flags & MODULE_FLAG_BLACKLISTED)
382                         ) {
383                                 rc = do_modprobe(me);
384 //FIXME: what if rc > 0?
385                                 if (rc < 0 && !(opt & INSMOD_OPT_SILENT))
386                                         bb_error_msg("module %s not found",
387                                                      me->probed_name);
388                         }
389                 } else {
390                         /* Probe all realnames */
391                         do {
392                                 char *realname = llist_pop(&me->realnames);
393                                 struct module_entry *m2;
394
395                                 DBG("probing %s by realname %s", me->modname, realname);
396                                 m2 = get_or_add_modentry(realname);
397                                 if (!(m2->flags & MODULE_FLAG_BLACKLISTED))
398                                         do_modprobe(m2);
399 //FIXME: error check?
400                                 free(realname);
401                         } while (me->realnames != NULL);
402                 }
403         }
404
405         return EXIT_SUCCESS;
406 }