modprobe.c 20 KB

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