modprobe.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  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 source tree.
  9. */
  10. //config:config MODPROBE
  11. //config: bool "modprobe (28 kb)"
  12. //config: default y
  13. //config: help
  14. //config: Handle the loading of modules, and their dependencies on a high
  15. //config: level.
  16. //config:
  17. //config:config FEATURE_MODPROBE_BLACKLIST
  18. //config: bool "Blacklist support"
  19. //config: default y
  20. //config: depends on MODPROBE && !MODPROBE_SMALL
  21. //config: help
  22. //config: Say 'y' here to enable support for the 'blacklist' command in
  23. //config: modprobe.conf. This prevents the alias resolver to resolve
  24. //config: blacklisted modules. This is useful if you want to prevent your
  25. //config: hardware autodetection scripts to load modules like evdev, frame
  26. //config: buffer drivers etc.
  27. //applet:IF_MODPROBE(IF_NOT_MODPROBE_SMALL(APPLET_NOEXEC(modprobe, modprobe, BB_DIR_SBIN, BB_SUID_DROP, modprobe)))
  28. //kbuild:ifneq ($(CONFIG_MODPROBE_SMALL),y)
  29. //kbuild:lib-$(CONFIG_MODPROBE) += modprobe.o modutils.o
  30. //kbuild:endif
  31. #include "libbb.h"
  32. #include "modutils.h"
  33. #include <sys/utsname.h>
  34. #include <fnmatch.h>
  35. #if 1
  36. #define DBG(...) ((void)0)
  37. #else
  38. #define DBG(fmt, ...) bb_error_msg("%s: " fmt, __func__, ## __VA_ARGS__)
  39. #endif
  40. /* Note that unlike older versions of modules.dep/depmod (busybox and m-i-t),
  41. * we expect the full dependency list to be specified in modules.dep.
  42. * Older versions would only export the direct dependency list.
  43. */
  44. //usage:#if !ENABLE_MODPROBE_SMALL
  45. //usage:#define modprobe_notes_usage
  46. //usage: "modprobe can (un)load a stack of modules, passing each module options (when\n"
  47. //usage: "loading). modprobe uses a configuration file to determine what option(s) to\n"
  48. //usage: "pass each module it loads.\n"
  49. //usage: "\n"
  50. //usage: "The configuration file is searched (in this order):\n"
  51. //usage: "\n"
  52. //usage: " /etc/modprobe.conf (2.6 only)\n"
  53. //usage: " /etc/modules.conf\n"
  54. //usage: " /etc/conf.modules (deprecated)\n"
  55. //usage: "\n"
  56. //usage: "They all have the same syntax (see below). If none is present, it is\n"
  57. //usage: "_not_ an error; each loaded module is then expected to load without\n"
  58. //usage: "options. Once a file is found, the others are tested for.\n"
  59. //usage: "\n"
  60. //usage: "/etc/modules.conf entry format:\n"
  61. //usage: "\n"
  62. //usage: " alias <alias_name> <mod_name>\n"
  63. //usage: " Makes it possible to modprobe alias_name, when there is no such module.\n"
  64. //usage: " It makes sense if your mod_name is long, or you want a more representative\n"
  65. //usage: " name for that module (eg. 'scsi' in place of 'aha7xxx').\n"
  66. //usage: " This makes it also possible to use a different set of options (below) for\n"
  67. //usage: " the module and the alias.\n"
  68. //usage: " A module can be aliased more than once.\n"
  69. //usage: "\n"
  70. //usage: " options <mod_name|alias_name> <symbol=value...>\n"
  71. //usage: " When loading module mod_name (or the module aliased by alias_name), pass\n"
  72. //usage: " the \"symbol=value\" pairs as option to that module.\n"
  73. //usage: "\n"
  74. //usage: "Sample /etc/modules.conf file:\n"
  75. //usage: "\n"
  76. //usage: " options tulip irq=3\n"
  77. //usage: " alias tulip tulip2\n"
  78. //usage: " options tulip2 irq=4 io=0x308\n"
  79. //usage: "\n"
  80. //usage: "Other functionality offered by 'classic' modprobe is not available in\n"
  81. //usage: "this implementation.\n"
  82. //usage: "\n"
  83. //usage: "If module options are present both in the config file, and on the command line,\n"
  84. //usage: "then the options from the command line will be passed to the module _after_\n"
  85. //usage: "the options from the config file. That way, you can have defaults in the config\n"
  86. //usage: "file, and override them for a specific usage from the command line.\n"
  87. //usage:#define modprobe_example_usage
  88. //usage: "(with the above /etc/modules.conf):\n\n"
  89. //usage: "$ modprobe tulip\n"
  90. //usage: " will load the module 'tulip' with default option 'irq=3'\n\n"
  91. //usage: "$ modprobe tulip irq=5\n"
  92. //usage: " will load the module 'tulip' with option 'irq=5', thus overriding the default\n\n"
  93. //usage: "$ modprobe tulip2\n"
  94. //usage: " will load the module 'tulip' with default options 'irq=4 io=0x308',\n"
  95. //usage: " which are the default for alias 'tulip2'\n\n"
  96. //usage: "$ modprobe tulip2 irq=8\n"
  97. //usage: " will load the module 'tulip' with default options 'irq=4 io=0x308 irq=8',\n"
  98. //usage: " which are the default for alias 'tulip2' overridden by the option 'irq=8'\n\n"
  99. //usage: " from the command line\n\n"
  100. //usage: "$ modprobe tulip2 irq=2 io=0x210\n"
  101. //usage: " will load the module 'tulip' with default options 'irq=4 io=0x308 irq=4 io=0x210',\n"
  102. //usage: " which are the default for alias 'tulip2' overridden by the options 'irq=2 io=0x210'\n\n"
  103. //usage: " from the command line\n"
  104. //usage:
  105. //usage:#define modprobe_trivial_usage
  106. //usage: "[-alrqvsD" IF_FEATURE_MODPROBE_BLACKLIST("b") "]"
  107. //usage: " MODULE" IF_FEATURE_CMDLINE_MODULE_OPTIONS(" [SYMBOL=VALUE]...")
  108. //usage:#define modprobe_full_usage "\n\n"
  109. //usage: " -a Load multiple MODULEs"
  110. //usage: "\n -l List (MODULE is a pattern)"
  111. //usage: "\n -r Remove MODULE (stacks) or do autoclean"
  112. //usage: "\n -q Quiet"
  113. //usage: "\n -v Verbose"
  114. //usage: "\n -s Log to syslog"
  115. //usage: "\n -D Show dependencies"
  116. //usage: IF_FEATURE_MODPROBE_BLACKLIST(
  117. //usage: "\n -b Apply blacklist to module names too"
  118. //usage: )
  119. //usage:#endif /* !ENABLE_MODPROBE_SMALL */
  120. /* Note: usage text doesn't document various 2.4 options
  121. * we pull in through INSMOD_OPTS define
  122. * Note2: -b is always accepted, but if !FEATURE_MODPROBE_BLACKLIST,
  123. * it is a no-op.
  124. */
  125. #define MODPROBE_OPTS "alrDb"
  126. /* -a and -D _are_ in fact compatible */
  127. #define MODPROBE_COMPLEMENTARY "q-v:v-q:l--arD:r--alD:a--lr:D--rl"
  128. //#define MODPROBE_OPTS "acd:lnrt:C:b"
  129. //#define MODPROBE_COMPLEMENTARY "q-v:v-q:l--acr:a--lr:r--al"
  130. enum {
  131. OPT_INSERT_ALL = (INSMOD_OPT_UNUSED << 0), /* a */
  132. //OPT_DUMP_ONLY = (INSMOD_OPT_UNUSED << x), /* c */
  133. //OPT_DIRNAME = (INSMOD_OPT_UNUSED << x), /* d */
  134. OPT_LIST_ONLY = (INSMOD_OPT_UNUSED << 1), /* l */
  135. //OPT_SHOW_ONLY = (INSMOD_OPT_UNUSED << x), /* n */
  136. OPT_REMOVE = (INSMOD_OPT_UNUSED << 2), /* r */
  137. //OPT_RESTRICT = (INSMOD_OPT_UNUSED << x), /* t */
  138. //OPT_VERONLY = (INSMOD_OPT_UNUSED << x), /* V */
  139. //OPT_CONFIGFILE = (INSMOD_OPT_UNUSED << x), /* C */
  140. OPT_SHOW_DEPS = (INSMOD_OPT_UNUSED << 3), /* D */
  141. OPT_BLACKLIST = (INSMOD_OPT_UNUSED << 4) * ENABLE_FEATURE_MODPROBE_BLACKLIST,
  142. };
  143. #if ENABLE_LONG_OPTS
  144. static const char modprobe_longopts[] ALIGN1 =
  145. /* nobody asked for long opts (yet) */
  146. // "all\0" No_argument "a"
  147. // "list\0" No_argument "l"
  148. // "remove\0" No_argument "r"
  149. // "quiet\0" No_argument "q"
  150. // "verbose\0" No_argument "v"
  151. // "syslog\0" No_argument "s"
  152. /* module-init-tools 3.11.1 has only long opt --show-depends
  153. * but no short -D, we provide long opt for scripts which
  154. * were written for 3.11.1: */
  155. "show-depends\0" No_argument "D"
  156. // "use-blacklist\0" No_argument "b"
  157. ;
  158. #endif
  159. #define MODULE_FLAG_LOADED 0x0001
  160. #define MODULE_FLAG_NEED_DEPS 0x0002
  161. /* "was seen in modules.dep": */
  162. #define MODULE_FLAG_FOUND_IN_MODDEP 0x0004
  163. #define MODULE_FLAG_BLACKLISTED 0x0008
  164. #define MODULE_FLAG_BUILTIN 0x0010
  165. struct globals {
  166. llist_t *probes; /* MEs of module(s) requested on cmdline */
  167. #if ENABLE_FEATURE_CMDLINE_MODULE_OPTIONS
  168. char *cmdline_mopts; /* module options from cmdline */
  169. #endif
  170. int num_unresolved_deps;
  171. /* bool. "Did we have 'symbol:FOO' requested on cmdline?" */
  172. smallint need_symbols;
  173. struct utsname uts;
  174. module_db db;
  175. } FIX_ALIASING;
  176. #define G (*ptr_to_globals)
  177. #define INIT_G() do { \
  178. SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
  179. } while (0)
  180. static int read_config(const char *path);
  181. static char *gather_options_str(char *opts, const char *append)
  182. {
  183. /* Speed-optimized. We call gather_options_str many times. */
  184. if (append) {
  185. if (opts == NULL) {
  186. opts = xstrdup(append);
  187. } else {
  188. int optlen = strlen(opts);
  189. opts = xrealloc(opts, optlen + strlen(append) + 2);
  190. sprintf(opts + optlen, " %s", append);
  191. }
  192. }
  193. return opts;
  194. }
  195. static struct module_entry *get_or_add_modentry(const char *module)
  196. {
  197. return moddb_get_or_create(&G.db, module);
  198. }
  199. static void add_probe(const char *name)
  200. {
  201. struct module_entry *m;
  202. m = get_or_add_modentry(name);
  203. if (!(option_mask32 & (OPT_REMOVE | OPT_SHOW_DEPS))
  204. && (m->flags & (MODULE_FLAG_LOADED | MODULE_FLAG_BUILTIN))
  205. ) {
  206. DBG("skipping %s, it is already loaded", name);
  207. return;
  208. }
  209. DBG("queuing %s", name);
  210. m->probed_name = name;
  211. m->flags |= MODULE_FLAG_NEED_DEPS;
  212. llist_add_to_end(&G.probes, m);
  213. G.num_unresolved_deps++;
  214. if (ENABLE_FEATURE_MODUTILS_SYMBOLS
  215. && is_prefixed_with(m->modname, "symbol:")
  216. ) {
  217. G.need_symbols = 1;
  218. }
  219. }
  220. static int FAST_FUNC config_file_action(struct recursive_state *state,
  221. const char *filename,
  222. struct stat *statbuf UNUSED_PARAM)
  223. {
  224. char *tokens[3];
  225. parser_t *p;
  226. struct module_entry *m;
  227. int rc = TRUE;
  228. const char *base;
  229. /* Skip files that begin with a "." */
  230. base = bb_basename(filename);
  231. if (base[0] == '.')
  232. goto error;
  233. /* "man modprobe.d" from kmod version 22 suggests
  234. * that we shouldn't recurse into /etc/modprobe.d/dir/
  235. * _subdirectories_:
  236. */
  237. if (state->depth > 1)
  238. return SKIP; /* stop recursing */
  239. //TODO: instead, can use dirAction in recursive_action() to SKIP dirs
  240. //on depth == 1 level. But that's more code...
  241. /* In dir recursion, skip files that do not end with a ".conf"
  242. * depth==0: read_config("modules.{symbols,alias}") must work,
  243. * "include FILE_NOT_ENDING_IN_CONF" must work too.
  244. */
  245. if (state->depth != 0) {
  246. if (!is_suffixed_with(base, ".conf"))
  247. goto error;
  248. }
  249. p = config_open2(filename, fopen_for_read);
  250. if (p == NULL) {
  251. rc = FALSE;
  252. goto error;
  253. }
  254. while (config_read(p, tokens, 3, 2, "# \t", PARSE_NORMAL)) {
  255. //Use index_in_strings?
  256. if (strcmp(tokens[0], "alias") == 0) {
  257. /* alias <wildcard> <modulename> */
  258. llist_t *l;
  259. char wildcard[MODULE_NAME_LEN];
  260. char *rmod;
  261. if (tokens[2] == NULL)
  262. continue;
  263. filename2modname(tokens[1], wildcard);
  264. for (l = G.probes; l; l = l->link) {
  265. m = (struct module_entry *) l->data;
  266. if (fnmatch(wildcard, m->modname, 0) != 0)
  267. continue;
  268. rmod = filename2modname(tokens[2], NULL);
  269. llist_add_to(&m->realnames, rmod);
  270. if (m->flags & MODULE_FLAG_NEED_DEPS) {
  271. m->flags &= ~MODULE_FLAG_NEED_DEPS;
  272. G.num_unresolved_deps--;
  273. }
  274. m = get_or_add_modentry(rmod);
  275. if (!(m->flags & MODULE_FLAG_NEED_DEPS)) {
  276. m->flags |= MODULE_FLAG_NEED_DEPS;
  277. G.num_unresolved_deps++;
  278. }
  279. }
  280. } else if (strcmp(tokens[0], "options") == 0) {
  281. /* options <modulename> <option...> */
  282. if (tokens[2] == NULL)
  283. continue;
  284. m = get_or_add_modentry(tokens[1]);
  285. m->options = gather_options_str(m->options, tokens[2]);
  286. } else if (strcmp(tokens[0], "include") == 0) {
  287. /* include <filename>/<dirname> (yes, directories also must work) */
  288. read_config(tokens[1]);
  289. } else if (ENABLE_FEATURE_MODPROBE_BLACKLIST
  290. && strcmp(tokens[0], "blacklist") == 0
  291. ) {
  292. /* blacklist <modulename> */
  293. get_or_add_modentry(tokens[1])->flags |= MODULE_FLAG_BLACKLISTED;
  294. }
  295. }
  296. config_close(p);
  297. error:
  298. return rc;
  299. }
  300. static int read_config(const char *path)
  301. {
  302. return recursive_action(path, ACTION_RECURSE | ACTION_QUIET,
  303. config_file_action, NULL, NULL);
  304. }
  305. static const char *humanly_readable_name(struct module_entry *m)
  306. {
  307. /* probed_name may be NULL. modname always exists. */
  308. return m->probed_name ? m->probed_name : m->modname;
  309. }
  310. /* Like strsep(&stringp, "\n\t ") but quoted text goes to single token
  311. * even if it contains whitespace.
  312. */
  313. static char *strsep_quotes(char **stringp)
  314. {
  315. char *s, *start = *stringp;
  316. if (!start)
  317. return NULL;
  318. for (s = start; ; s++) {
  319. switch (*s) {
  320. case '"':
  321. s = strchrnul(s + 1, '"'); /* find trailing quote */
  322. if (*s != '\0')
  323. s++; /* skip trailing quote */
  324. /* fall through */
  325. case '\0':
  326. case '\n':
  327. case '\t':
  328. case ' ':
  329. if (*s != '\0') {
  330. *s = '\0';
  331. *stringp = s + 1;
  332. } else {
  333. *stringp = NULL;
  334. }
  335. return start;
  336. }
  337. }
  338. }
  339. static char *parse_and_add_kcmdline_module_options(char *options, const char *modulename)
  340. {
  341. char *kcmdline_buf;
  342. char *kcmdline;
  343. char *kptr;
  344. kcmdline_buf = xmalloc_open_read_close("/proc/cmdline", NULL);
  345. if (!kcmdline_buf)
  346. return options;
  347. kcmdline = kcmdline_buf;
  348. while ((kptr = strsep_quotes(&kcmdline)) != NULL) {
  349. char *after_modulename = is_prefixed_with(kptr, modulename);
  350. if (!after_modulename || *after_modulename != '.')
  351. continue;
  352. /* It is "modulename.xxxx" */
  353. kptr = after_modulename + 1;
  354. if (strchr(kptr, '=') != NULL) {
  355. /* It is "modulename.opt=[val]" */
  356. options = gather_options_str(options, kptr);
  357. }
  358. }
  359. free(kcmdline_buf);
  360. return options;
  361. }
  362. /* Return: similar to bb_init_module:
  363. * 0 on success,
  364. * -errno on open/read error,
  365. * errno on init_module() error
  366. */
  367. /* NB: INSMOD_OPT_SILENT bit suppresses ONLY non-existent modules,
  368. * not deleted ones (those are still listed in modules.dep).
  369. * module-init-tools version 3.4:
  370. * # modprobe bogus
  371. * FATAL: Module bogus not found. [exitcode 1]
  372. * # modprobe -q bogus [silent, exitcode still 1]
  373. * but:
  374. * # rm kernel/drivers/net/dummy.ko
  375. * # modprobe -q dummy
  376. * FATAL: Could not open '/lib/modules/xxx/kernel/drivers/net/dummy.ko': No such file or directory
  377. * [exitcode 1]
  378. */
  379. static int do_modprobe(struct module_entry *m)
  380. {
  381. int rc, first;
  382. if (!(m->flags & MODULE_FLAG_FOUND_IN_MODDEP)) {
  383. if (!(option_mask32 & INSMOD_OPT_SILENT))
  384. bb_error_msg((m->flags & MODULE_FLAG_BUILTIN) ?
  385. "module %s is builtin" :
  386. "module %s not found in modules.dep",
  387. humanly_readable_name(m));
  388. return -ENOENT;
  389. }
  390. DBG("do_modprob'ing %s", m->modname);
  391. if (!(option_mask32 & OPT_REMOVE))
  392. m->deps = llist_rev(m->deps);
  393. if (0) {
  394. llist_t *l;
  395. for (l = m->deps; l; l = l->link)
  396. DBG("dep: %s", l->data);
  397. }
  398. first = 1;
  399. rc = 0;
  400. while (m->deps) {
  401. struct module_entry *m2;
  402. char *fn, *options;
  403. rc = 0;
  404. fn = llist_pop(&m->deps); /* we leak it */
  405. m2 = get_or_add_modentry(bb_get_last_path_component_nostrip(fn));
  406. if (option_mask32 & OPT_REMOVE) {
  407. /* modprobe -r */
  408. if (m2->flags & MODULE_FLAG_LOADED) {
  409. rc = bb_delete_module(m2->modname, O_EXCL);
  410. if (rc) {
  411. if (first) {
  412. bb_perror_msg("can't unload module '%s'",
  413. humanly_readable_name(m2));
  414. break;
  415. }
  416. } else {
  417. m2->flags &= ~MODULE_FLAG_LOADED;
  418. }
  419. }
  420. /* do not error out if *deps* fail to unload */
  421. first = 0;
  422. continue;
  423. }
  424. options = m2->options;
  425. m2->options = NULL;
  426. options = parse_and_add_kcmdline_module_options(options, m2->modname);
  427. #if ENABLE_FEATURE_CMDLINE_MODULE_OPTIONS
  428. if (m == m2)
  429. options = gather_options_str(options, G.cmdline_mopts);
  430. #endif
  431. if (option_mask32 & OPT_SHOW_DEPS) {
  432. printf(options ? "insmod %s/%s/%s %s\n"
  433. : "insmod %s/%s/%s\n",
  434. CONFIG_DEFAULT_MODULES_DIR, G.uts.release, fn,
  435. options);
  436. free(options);
  437. continue;
  438. }
  439. if (m2->flags & MODULE_FLAG_LOADED) {
  440. DBG("%s is already loaded, skipping", fn);
  441. free(options);
  442. continue;
  443. }
  444. rc = bb_init_module(fn, options);
  445. DBG("loaded %s '%s', rc:%d", fn, options, rc);
  446. if (rc == EEXIST)
  447. rc = 0;
  448. free(options);
  449. if (rc) {
  450. bb_error_msg("can't load module %s (%s): %s",
  451. humanly_readable_name(m2),
  452. fn,
  453. moderror(rc)
  454. );
  455. break;
  456. }
  457. m2->flags |= MODULE_FLAG_LOADED;
  458. }
  459. return rc;
  460. }
  461. static void load_modules_dep(void)
  462. {
  463. struct module_entry *m;
  464. char *colon, *tokens[2];
  465. parser_t *p;
  466. /* Modprobe does not work at all without modules.dep,
  467. * even if the full module name is given. Returning error here
  468. * was making us later confuse user with this message:
  469. * "module /full/path/to/existing/file/module.ko not found".
  470. * It's better to die immediately, with good message.
  471. * xfopen_for_read provides that. */
  472. p = config_open2(CONFIG_DEFAULT_DEPMOD_FILE, xfopen_for_read);
  473. while (G.num_unresolved_deps
  474. && config_read(p, tokens, 2, 1, "# \t", PARSE_NORMAL)
  475. ) {
  476. colon = last_char_is(tokens[0], ':');
  477. if (colon == NULL)
  478. continue;
  479. *colon = '\0';
  480. m = moddb_get(&G.db, bb_get_last_path_component_nostrip(tokens[0]));
  481. if (m == NULL)
  482. continue;
  483. /* Optimization... */
  484. if ((m->flags & MODULE_FLAG_LOADED)
  485. && !(option_mask32 & (OPT_REMOVE | OPT_SHOW_DEPS))
  486. ) {
  487. DBG("skip deps of %s, it's already loaded", tokens[0]);
  488. continue;
  489. }
  490. m->flags |= MODULE_FLAG_FOUND_IN_MODDEP;
  491. if ((m->flags & MODULE_FLAG_NEED_DEPS) && (m->deps == NULL)) {
  492. G.num_unresolved_deps--;
  493. llist_add_to(&m->deps, xstrdup(tokens[0]));
  494. if (tokens[1])
  495. string_to_llist(tokens[1], &m->deps, " \t");
  496. } else
  497. DBG("skipping dep line");
  498. }
  499. config_close(p);
  500. }
  501. int modprobe_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  502. int modprobe_main(int argc UNUSED_PARAM, char **argv)
  503. {
  504. int rc;
  505. unsigned opt;
  506. struct module_entry *me;
  507. INIT_G();
  508. opt = getopt32long(argv, "^" INSMOD_OPTS MODPROBE_OPTS "\0" MODPROBE_COMPLEMENTARY,
  509. modprobe_longopts
  510. INSMOD_ARGS
  511. );
  512. argv += optind;
  513. /* Goto modules location */
  514. xchdir(CONFIG_DEFAULT_MODULES_DIR);
  515. uname(&G.uts);
  516. xchdir(G.uts.release);
  517. if (opt & OPT_LIST_ONLY) {
  518. int i;
  519. char *colon, *tokens[2];
  520. parser_t *p = config_open2(CONFIG_DEFAULT_DEPMOD_FILE, xfopen_for_read);
  521. for (i = 0; argv[i]; i++)
  522. replace(argv[i], '-', '_');
  523. while (config_read(p, tokens, 2, 1, "# \t", PARSE_NORMAL)) {
  524. colon = last_char_is(tokens[0], ':');
  525. if (!colon)
  526. continue;
  527. *colon = '\0';
  528. if (!argv[0])
  529. puts(tokens[0]);
  530. else {
  531. char name[MODULE_NAME_LEN];
  532. filename2modname(
  533. bb_get_last_path_component_nostrip(tokens[0]),
  534. name
  535. );
  536. for (i = 0; argv[i]; i++) {
  537. if (fnmatch(argv[i], name, 0) == 0) {
  538. puts(tokens[0]);
  539. }
  540. }
  541. }
  542. }
  543. return EXIT_SUCCESS;
  544. }
  545. /* Yes, for some reason -l ignores -s... */
  546. if (opt & INSMOD_OPT_SYSLOG)
  547. logmode = LOGMODE_SYSLOG;
  548. if (!argv[0]) {
  549. if (opt & OPT_REMOVE) {
  550. /* "modprobe -r" (w/o params).
  551. * "If name is NULL, all unused modules marked
  552. * autoclean will be removed".
  553. */
  554. if (bb_delete_module(NULL, O_NONBLOCK | O_EXCL) != 0)
  555. bb_perror_nomsg_and_die();
  556. }
  557. return EXIT_SUCCESS;
  558. }
  559. /* Retrieve module names of already loaded modules */
  560. {
  561. char *s;
  562. parser_t *parser = config_open2("/proc/modules", fopen_for_read);
  563. while (config_read(parser, &s, 1, 1, "# \t", PARSE_NORMAL & ~PARSE_GREEDY))
  564. get_or_add_modentry(s)->flags |= MODULE_FLAG_LOADED;
  565. config_close(parser);
  566. parser = config_open2("modules.builtin", fopen_for_read);
  567. /* this file contains lines like "kernel/fs/binfmt_script.ko" */
  568. while (config_read(parser, &s, 1, 1, "# \t", PARSE_NORMAL))
  569. get_or_add_modentry(bb_basename(s))->flags |= MODULE_FLAG_BUILTIN;
  570. config_close(parser);
  571. }
  572. if (opt & (OPT_INSERT_ALL | OPT_REMOVE)) {
  573. /* Each argument is a module name */
  574. do {
  575. DBG("adding module %s", *argv);
  576. add_probe(*argv++);
  577. } while (*argv);
  578. } else {
  579. /* First argument is module name, rest are parameters */
  580. DBG("probing just module %s", *argv);
  581. add_probe(argv[0]);
  582. #if ENABLE_FEATURE_CMDLINE_MODULE_OPTIONS
  583. G.cmdline_mopts = parse_cmdline_module_options(argv, /*quote_spaces:*/ 1);
  584. #endif
  585. }
  586. /* Happens if all requested modules are already loaded */
  587. if (G.probes == NULL)
  588. return EXIT_SUCCESS;
  589. read_config("/etc/modprobe.conf");
  590. read_config("/etc/modprobe.d");
  591. if (ENABLE_FEATURE_MODUTILS_SYMBOLS && G.need_symbols)
  592. read_config("modules.symbols");
  593. load_modules_dep();
  594. if (ENABLE_FEATURE_MODUTILS_ALIAS && G.num_unresolved_deps) {
  595. read_config("modules.alias");
  596. load_modules_dep();
  597. }
  598. /* Handle modprobe.blacklist=module1,module2,... */
  599. if (ENABLE_FEATURE_MODPROBE_BLACKLIST) {
  600. char *options;
  601. char *substr;
  602. options = parse_and_add_kcmdline_module_options(NULL, "modprobe");
  603. while ((substr = strsep(&options, " ")) != NULL) {
  604. char *fn = is_prefixed_with(substr, "blacklist=");
  605. if (!fn)
  606. continue;
  607. while ((substr = strsep(&fn, ",")) != NULL) {
  608. /* blacklist <modulename> */
  609. get_or_add_modentry(substr)->flags |= MODULE_FLAG_BLACKLISTED;
  610. DBG("blacklist: %s", substr);
  611. }
  612. }
  613. /*free(options); - WRONG, strsep may have advanced it */
  614. }
  615. rc = 0;
  616. while ((me = llist_pop(&G.probes)) != NULL) {
  617. if (me->realnames == NULL) {
  618. DBG("probing by module name");
  619. /* This is not an alias. Literal names are blacklisted
  620. * only if '-b' is given.
  621. */
  622. if (!(opt & OPT_BLACKLIST)
  623. || !(me->flags & MODULE_FLAG_BLACKLISTED)
  624. ) {
  625. rc |= do_modprobe(me);
  626. }
  627. continue;
  628. }
  629. /* Probe all real names for the alias */
  630. do {
  631. char *realname = llist_pop(&me->realnames);
  632. struct module_entry *m2;
  633. DBG("probing alias %s by realname %s", me->modname, realname);
  634. m2 = get_or_add_modentry(realname);
  635. if (!(m2->flags & MODULE_FLAG_BLACKLISTED)
  636. && (!(m2->flags & MODULE_FLAG_LOADED)
  637. || (opt & (OPT_REMOVE | OPT_SHOW_DEPS)))
  638. ) {
  639. //TODO: we can pass "me" as 2nd param to do_modprobe,
  640. //and make do_modprobe emit more meaningful error messages
  641. //with alias name included, not just module name alias resolves to.
  642. rc |= do_modprobe(m2);
  643. }
  644. free(realname);
  645. } while (me->realnames != NULL);
  646. }
  647. if (ENABLE_FEATURE_CLEAN_UP)
  648. moddb_free(&G.db);
  649. return (rc != 0);
  650. }