modprobe.c 20 KB

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