modprobe.c 12 KB

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