modprobe.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  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. static ALWAYS_INLINE struct module_entry *get_modentry(const char *module)
  213. {
  214. return helper_get_module(module, 0);
  215. }
  216. static void add_probe(const char *name)
  217. {
  218. struct module_entry *m;
  219. m = get_or_add_modentry(name);
  220. if (!(option_mask32 & (OPT_REMOVE | OPT_SHOW_DEPS))
  221. && (m->flags & MODULE_FLAG_LOADED)
  222. ) {
  223. DBG("skipping %s, it is already loaded", name);
  224. return;
  225. }
  226. DBG("queuing %s", name);
  227. m->probed_name = name;
  228. m->flags |= MODULE_FLAG_NEED_DEPS;
  229. llist_add_to_end(&G.probes, m);
  230. G.num_unresolved_deps++;
  231. if (ENABLE_FEATURE_MODUTILS_SYMBOLS
  232. && strncmp(m->modname, "symbol:", 7) == 0
  233. ) {
  234. G.need_symbols = 1;
  235. }
  236. }
  237. static int FAST_FUNC config_file_action(const char *filename,
  238. struct stat *statbuf UNUSED_PARAM,
  239. void *userdata UNUSED_PARAM,
  240. int depth UNUSED_PARAM)
  241. {
  242. char *tokens[3];
  243. parser_t *p;
  244. struct module_entry *m;
  245. int rc = TRUE;
  246. if (bb_basename(filename)[0] == '.')
  247. goto error;
  248. p = config_open2(filename, fopen_for_read);
  249. if (p == NULL) {
  250. rc = FALSE;
  251. goto error;
  252. }
  253. while (config_read(p, tokens, 3, 2, "# \t", PARSE_NORMAL)) {
  254. //Use index_in_strings?
  255. if (strcmp(tokens[0], "alias") == 0) {
  256. /* alias <wildcard> <modulename> */
  257. llist_t *l;
  258. char wildcard[MODULE_NAME_LEN];
  259. char *rmod;
  260. if (tokens[2] == NULL)
  261. continue;
  262. filename2modname(tokens[1], wildcard);
  263. for (l = G.probes; l; l = l->link) {
  264. m = (struct module_entry *) l->data;
  265. if (fnmatch(wildcard, m->modname, 0) != 0)
  266. continue;
  267. rmod = filename2modname(tokens[2], NULL);
  268. llist_add_to(&m->realnames, rmod);
  269. if (m->flags & MODULE_FLAG_NEED_DEPS) {
  270. m->flags &= ~MODULE_FLAG_NEED_DEPS;
  271. G.num_unresolved_deps--;
  272. }
  273. m = get_or_add_modentry(rmod);
  274. if (!(m->flags & MODULE_FLAG_NEED_DEPS)) {
  275. m->flags |= MODULE_FLAG_NEED_DEPS;
  276. G.num_unresolved_deps++;
  277. }
  278. }
  279. } else if (strcmp(tokens[0], "options") == 0) {
  280. /* options <modulename> <option...> */
  281. if (tokens[2] == NULL)
  282. continue;
  283. m = get_or_add_modentry(tokens[1]);
  284. m->options = gather_options_str(m->options, tokens[2]);
  285. } else if (strcmp(tokens[0], "include") == 0) {
  286. /* include <filename> */
  287. read_config(tokens[1]);
  288. } else if (ENABLE_FEATURE_MODPROBE_BLACKLIST
  289. && strcmp(tokens[0], "blacklist") == 0
  290. ) {
  291. /* blacklist <modulename> */
  292. get_or_add_modentry(tokens[1])->flags |= MODULE_FLAG_BLACKLISTED;
  293. }
  294. }
  295. config_close(p);
  296. error:
  297. return rc;
  298. }
  299. static int read_config(const char *path)
  300. {
  301. return recursive_action(path, ACTION_RECURSE | ACTION_QUIET,
  302. config_file_action, NULL, NULL, 1);
  303. }
  304. static const char *humanly_readable_name(struct module_entry *m)
  305. {
  306. /* probed_name may be NULL. modname always exists. */
  307. return m->probed_name ? m->probed_name : m->modname;
  308. }
  309. static char *parse_and_add_kcmdline_module_options(char *options, const char *modulename)
  310. {
  311. char *kcmdline_buf;
  312. char *kcmdline;
  313. char *kptr;
  314. int len;
  315. kcmdline_buf = xmalloc_open_read_close("/proc/cmdline", NULL);
  316. if (!kcmdline_buf)
  317. return options;
  318. kcmdline = kcmdline_buf;
  319. len = strlen(modulename);
  320. while ((kptr = strsep(&kcmdline, "\n\t ")) != NULL) {
  321. if (strncmp(modulename, kptr, len) != 0)
  322. continue;
  323. kptr += len;
  324. if (*kptr != '.')
  325. continue;
  326. /* It is "modulename.xxxx" */
  327. kptr++;
  328. if (strchr(kptr, '=') != NULL) {
  329. /* It is "modulename.opt=[val]" */
  330. options = gather_options_str(options, kptr);
  331. }
  332. }
  333. free(kcmdline_buf);
  334. return options;
  335. }
  336. /* Return: similar to bb_init_module:
  337. * 0 on success,
  338. * -errno on open/read error,
  339. * errno on init_module() error
  340. */
  341. /* NB: INSMOD_OPT_SILENT bit suppresses ONLY non-existent modules,
  342. * not deleted ones (those are still listed in modules.dep).
  343. * module-init-tools version 3.4:
  344. * # modprobe bogus
  345. * FATAL: Module bogus not found. [exitcode 1]
  346. * # modprobe -q bogus [silent, exitcode still 1]
  347. * but:
  348. * # rm kernel/drivers/net/dummy.ko
  349. * # modprobe -q dummy
  350. * FATAL: Could not open '/lib/modules/xxx/kernel/drivers/net/dummy.ko': No such file or directory
  351. * [exitcode 1]
  352. */
  353. static int do_modprobe(struct module_entry *m)
  354. {
  355. int rc, first;
  356. if (!(m->flags & MODULE_FLAG_FOUND_IN_MODDEP)) {
  357. if (!(option_mask32 & INSMOD_OPT_SILENT))
  358. bb_error_msg("module %s not found in modules.dep",
  359. humanly_readable_name(m));
  360. return -ENOENT;
  361. }
  362. DBG("do_modprob'ing %s", m->modname);
  363. if (!(option_mask32 & OPT_REMOVE))
  364. m->deps = llist_rev(m->deps);
  365. if (0) {
  366. llist_t *l;
  367. for (l = m->deps; l; l = l->link)
  368. DBG("dep: %s", l->data);
  369. }
  370. first = 1;
  371. rc = 0;
  372. while (m->deps) {
  373. struct module_entry *m2;
  374. char *fn, *options;
  375. rc = 0;
  376. fn = llist_pop(&m->deps); /* we leak it */
  377. m2 = get_or_add_modentry(fn);
  378. if (option_mask32 & OPT_REMOVE) {
  379. /* modprobe -r */
  380. if (m2->flags & MODULE_FLAG_LOADED) {
  381. rc = bb_delete_module(m2->modname, O_EXCL);
  382. if (rc) {
  383. if (first) {
  384. bb_error_msg("can't unload module %s: %s",
  385. humanly_readable_name(m2),
  386. moderror(rc));
  387. break;
  388. }
  389. } else {
  390. m2->flags &= ~MODULE_FLAG_LOADED;
  391. }
  392. }
  393. /* do not error out if *deps* fail to unload */
  394. first = 0;
  395. continue;
  396. }
  397. options = m2->options;
  398. m2->options = NULL;
  399. options = parse_and_add_kcmdline_module_options(options, m2->modname);
  400. if (m == m2)
  401. options = gather_options_str(options, G.cmdline_mopts);
  402. if (option_mask32 & OPT_SHOW_DEPS) {
  403. printf(options ? "insmod %s/%s/%s %s\n"
  404. : "insmod %s/%s/%s\n",
  405. CONFIG_DEFAULT_MODULES_DIR, G.uts.release, fn,
  406. options);
  407. free(options);
  408. continue;
  409. }
  410. if (m2->flags & MODULE_FLAG_LOADED) {
  411. DBG("%s is already loaded, skipping", fn);
  412. free(options);
  413. continue;
  414. }
  415. rc = bb_init_module(fn, options);
  416. DBG("loaded %s '%s', rc:%d", fn, options, rc);
  417. if (rc == EEXIST)
  418. rc = 0;
  419. free(options);
  420. if (rc) {
  421. bb_error_msg("can't load module %s (%s): %s",
  422. humanly_readable_name(m2),
  423. fn,
  424. moderror(rc)
  425. );
  426. break;
  427. }
  428. m2->flags |= MODULE_FLAG_LOADED;
  429. }
  430. return rc;
  431. }
  432. static void load_modules_dep(void)
  433. {
  434. struct module_entry *m;
  435. char *colon, *tokens[2];
  436. parser_t *p;
  437. /* Modprobe does not work at all without modules.dep,
  438. * even if the full module name is given. Returning error here
  439. * was making us later confuse user with this message:
  440. * "module /full/path/to/existing/file/module.ko not found".
  441. * It's better to die immediately, with good message.
  442. * xfopen_for_read provides that. */
  443. p = config_open2(CONFIG_DEFAULT_DEPMOD_FILE, xfopen_for_read);
  444. while (G.num_unresolved_deps
  445. && config_read(p, tokens, 2, 1, "# \t", PARSE_NORMAL)
  446. ) {
  447. colon = last_char_is(tokens[0], ':');
  448. if (colon == NULL)
  449. continue;
  450. *colon = 0;
  451. m = get_modentry(tokens[0]);
  452. if (m == NULL)
  453. continue;
  454. /* Optimization... */
  455. if ((m->flags & MODULE_FLAG_LOADED)
  456. && !(option_mask32 & (OPT_REMOVE | OPT_SHOW_DEPS))
  457. ) {
  458. DBG("skip deps of %s, it's already loaded", tokens[0]);
  459. continue;
  460. }
  461. m->flags |= MODULE_FLAG_FOUND_IN_MODDEP;
  462. if ((m->flags & MODULE_FLAG_NEED_DEPS) && (m->deps == NULL)) {
  463. G.num_unresolved_deps--;
  464. llist_add_to(&m->deps, xstrdup(tokens[0]));
  465. if (tokens[1])
  466. string_to_llist(tokens[1], &m->deps, " \t");
  467. } else
  468. DBG("skipping dep line");
  469. }
  470. config_close(p);
  471. }
  472. int modprobe_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  473. int modprobe_main(int argc UNUSED_PARAM, char **argv)
  474. {
  475. int rc;
  476. unsigned opt;
  477. struct module_entry *me;
  478. INIT_G();
  479. IF_LONG_OPTS(applet_long_options = modprobe_longopts;)
  480. opt_complementary = MODPROBE_COMPLEMENTARY;
  481. opt = getopt32(argv, INSMOD_OPTS MODPROBE_OPTS INSMOD_ARGS);
  482. argv += optind;
  483. /* Goto modules location */
  484. xchdir(CONFIG_DEFAULT_MODULES_DIR);
  485. uname(&G.uts);
  486. xchdir(G.uts.release);
  487. if (opt & OPT_LIST_ONLY) {
  488. int i;
  489. char name[MODULE_NAME_LEN];
  490. char *colon, *tokens[2];
  491. parser_t *p = config_open2(CONFIG_DEFAULT_DEPMOD_FILE, xfopen_for_read);
  492. for (i = 0; argv[i]; i++)
  493. replace(argv[i], '-', '_');
  494. while (config_read(p, tokens, 2, 1, "# \t", PARSE_NORMAL)) {
  495. colon = last_char_is(tokens[0], ':');
  496. if (!colon)
  497. continue;
  498. *colon = '\0';
  499. filename2modname(tokens[0], name);
  500. if (!argv[0])
  501. puts(tokens[0]);
  502. else {
  503. for (i = 0; argv[i]; i++) {
  504. if (fnmatch(argv[i], name, 0) == 0) {
  505. puts(tokens[0]);
  506. }
  507. }
  508. }
  509. }
  510. return EXIT_SUCCESS;
  511. }
  512. /* Yes, for some reason -l ignores -s... */
  513. if (opt & INSMOD_OPT_SYSLOG)
  514. logmode = LOGMODE_SYSLOG;
  515. if (!argv[0]) {
  516. if (opt & OPT_REMOVE) {
  517. /* "modprobe -r" (w/o params).
  518. * "If name is NULL, all unused modules marked
  519. * autoclean will be removed".
  520. */
  521. if (bb_delete_module(NULL, O_NONBLOCK | O_EXCL) != 0)
  522. bb_perror_msg_and_die("rmmod");
  523. }
  524. return EXIT_SUCCESS;
  525. }
  526. /* Retrieve module names of already loaded modules */
  527. {
  528. char *s;
  529. parser_t *parser = config_open2("/proc/modules", fopen_for_read);
  530. while (config_read(parser, &s, 1, 1, "# \t", PARSE_NORMAL & ~PARSE_GREEDY))
  531. get_or_add_modentry(s)->flags |= MODULE_FLAG_LOADED;
  532. config_close(parser);
  533. }
  534. if (opt & (OPT_INSERT_ALL | OPT_REMOVE)) {
  535. /* Each argument is a module name */
  536. do {
  537. DBG("adding module %s", *argv);
  538. add_probe(*argv++);
  539. } while (*argv);
  540. } else {
  541. /* First argument is module name, rest are parameters */
  542. DBG("probing just module %s", *argv);
  543. add_probe(argv[0]);
  544. G.cmdline_mopts = parse_cmdline_module_options(argv, /*quote_spaces:*/ 1);
  545. }
  546. /* Happens if all requested modules are already loaded */
  547. if (G.probes == NULL)
  548. return EXIT_SUCCESS;
  549. read_config("/etc/modprobe.conf");
  550. read_config("/etc/modprobe.d");
  551. if (ENABLE_FEATURE_MODUTILS_SYMBOLS && G.need_symbols)
  552. read_config("modules.symbols");
  553. load_modules_dep();
  554. if (ENABLE_FEATURE_MODUTILS_ALIAS && G.num_unresolved_deps) {
  555. read_config("modules.alias");
  556. load_modules_dep();
  557. }
  558. rc = 0;
  559. while ((me = llist_pop(&G.probes)) != NULL) {
  560. if (me->realnames == NULL) {
  561. DBG("probing by module name");
  562. /* This is not an alias. Literal names are blacklisted
  563. * only if '-b' is given.
  564. */
  565. if (!(opt & OPT_BLACKLIST)
  566. || !(me->flags & MODULE_FLAG_BLACKLISTED)
  567. ) {
  568. rc |= do_modprobe(me);
  569. }
  570. continue;
  571. }
  572. /* Probe all real names for the alias */
  573. do {
  574. char *realname = llist_pop(&me->realnames);
  575. struct module_entry *m2;
  576. DBG("probing alias %s by realname %s", me->modname, realname);
  577. m2 = get_or_add_modentry(realname);
  578. if (!(m2->flags & MODULE_FLAG_BLACKLISTED)
  579. && (!(m2->flags & MODULE_FLAG_LOADED)
  580. || (opt & (OPT_REMOVE | OPT_SHOW_DEPS)))
  581. ) {
  582. //TODO: we can pass "me" as 2nd param to do_modprobe,
  583. //and make do_modprobe emit more meaningful error messages
  584. //with alias name included, not just module name alias resolves to.
  585. rc |= do_modprobe(m2);
  586. }
  587. free(realname);
  588. } while (me->realnames != NULL);
  589. }
  590. return (rc != 0);
  591. }