3
0

modprobe.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662
  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 that usage text doesn't document various 2.4 options
  98. * we pull in through INSMOD_OPTS define */
  99. #define MODPROBE_OPTS "alrD" IF_FEATURE_MODPROBE_BLACKLIST("b")
  100. /* -a and -D _are_ in fact compatible */
  101. #define MODPROBE_COMPLEMENTARY ("q-v:v-q:l--arD:r--alD:a--lr:D--rl")
  102. //#define MODPROBE_OPTS "acd:lnrt:C:" IF_FEATURE_MODPROBE_BLACKLIST("b")
  103. //#define MODPROBE_COMPLEMENTARY "q-v:v-q:l--acr:a--lr:r--al"
  104. enum {
  105. OPT_INSERT_ALL = (INSMOD_OPT_UNUSED << 0), /* a */
  106. //OPT_DUMP_ONLY = (INSMOD_OPT_UNUSED << x), /* c */
  107. //OPT_DIRNAME = (INSMOD_OPT_UNUSED << x), /* d */
  108. OPT_LIST_ONLY = (INSMOD_OPT_UNUSED << 1), /* l */
  109. //OPT_SHOW_ONLY = (INSMOD_OPT_UNUSED << x), /* n */
  110. OPT_REMOVE = (INSMOD_OPT_UNUSED << 2), /* r */
  111. //OPT_RESTRICT = (INSMOD_OPT_UNUSED << x), /* t */
  112. //OPT_VERONLY = (INSMOD_OPT_UNUSED << x), /* V */
  113. //OPT_CONFIGFILE = (INSMOD_OPT_UNUSED << x), /* C */
  114. OPT_SHOW_DEPS = (INSMOD_OPT_UNUSED << 3), /* D */
  115. OPT_BLACKLIST = (INSMOD_OPT_UNUSED << 4) * ENABLE_FEATURE_MODPROBE_BLACKLIST,
  116. };
  117. #if ENABLE_LONG_OPTS
  118. static const char modprobe_longopts[] ALIGN1 =
  119. /* nobody asked for long opts (yet) */
  120. // "all\0" No_argument "a"
  121. // "list\0" No_argument "l"
  122. // "remove\0" No_argument "r"
  123. // "quiet\0" No_argument "q"
  124. // "verbose\0" No_argument "v"
  125. // "syslog\0" No_argument "s"
  126. /* module-init-tools 3.11.1 has only long opt --show-depends
  127. * but no short -D, we provide long opt for scripts which
  128. * were written for 3.11.1: */
  129. "show-depends\0" No_argument "D"
  130. // IF_FEATURE_MODPROBE_BLACKLIST(
  131. // "use-blacklist\0" No_argument "b"
  132. // )
  133. ;
  134. #endif
  135. #define MODULE_FLAG_LOADED 0x0001
  136. #define MODULE_FLAG_NEED_DEPS 0x0002
  137. /* "was seen in modules.dep": */
  138. #define MODULE_FLAG_FOUND_IN_MODDEP 0x0004
  139. #define MODULE_FLAG_BLACKLISTED 0x0008
  140. struct module_entry { /* I'll call it ME. */
  141. unsigned flags;
  142. char *modname; /* stripped of /path/, .ext and s/-/_/g */
  143. const char *probed_name; /* verbatim as seen on cmdline */
  144. char *options; /* options from config files */
  145. llist_t *realnames; /* strings. if this module is an alias, */
  146. /* real module name is one of these. */
  147. //Can there really be more than one? Example from real kernel?
  148. llist_t *deps; /* strings. modules we depend on */
  149. };
  150. #define DB_HASH_SIZE 256
  151. struct globals {
  152. llist_t *probes; /* MEs of module(s) requested on cmdline */
  153. char *cmdline_mopts; /* module options from cmdline */
  154. int num_unresolved_deps;
  155. /* bool. "Did we have 'symbol:FOO' requested on cmdline?" */
  156. smallint need_symbols;
  157. struct utsname uts;
  158. llist_t *db[DB_HASH_SIZE]; /* MEs of all modules ever seen (caching for speed) */
  159. } FIX_ALIASING;
  160. #define G (*ptr_to_globals)
  161. #define INIT_G() do { \
  162. SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
  163. } while (0)
  164. static int read_config(const char *path);
  165. static char *gather_options_str(char *opts, const char *append)
  166. {
  167. /* Speed-optimized. We call gather_options_str many times. */
  168. if (append) {
  169. if (opts == NULL) {
  170. opts = xstrdup(append);
  171. } else {
  172. int optlen = strlen(opts);
  173. opts = xrealloc(opts, optlen + strlen(append) + 2);
  174. sprintf(opts + optlen, " %s", append);
  175. }
  176. }
  177. return opts;
  178. }
  179. /* These three functions called many times, optimizing for speed.
  180. * Users reported minute-long delays when they runn iptables repeatedly
  181. * (iptables use modprobe to install needed kernel modules).
  182. */
  183. static struct module_entry *helper_get_module(const char *module, int create)
  184. {
  185. char modname[MODULE_NAME_LEN];
  186. struct module_entry *e;
  187. llist_t *l;
  188. unsigned i;
  189. unsigned hash;
  190. filename2modname(module, modname);
  191. hash = 0;
  192. for (i = 0; modname[i]; i++)
  193. hash = ((hash << 5) + hash) + modname[i];
  194. hash %= DB_HASH_SIZE;
  195. for (l = G.db[hash]; l; l = l->link) {
  196. e = (struct module_entry *) l->data;
  197. if (strcmp(e->modname, modname) == 0)
  198. return e;
  199. }
  200. if (!create)
  201. return NULL;
  202. e = xzalloc(sizeof(*e));
  203. e->modname = xstrdup(modname);
  204. llist_add_to(&G.db[hash], e);
  205. return e;
  206. }
  207. static ALWAYS_INLINE struct module_entry *get_or_add_modentry(const char *module)
  208. {
  209. return helper_get_module(module, 1);
  210. }
  211. static ALWAYS_INLINE struct module_entry *get_modentry(const char *module)
  212. {
  213. return helper_get_module(module, 0);
  214. }
  215. static void add_probe(const char *name)
  216. {
  217. struct module_entry *m;
  218. m = get_or_add_modentry(name);
  219. if (!(option_mask32 & (OPT_REMOVE | OPT_SHOW_DEPS))
  220. && (m->flags & MODULE_FLAG_LOADED)
  221. ) {
  222. DBG("skipping %s, it is already loaded", name);
  223. return;
  224. }
  225. DBG("queuing %s", name);
  226. m->probed_name = name;
  227. m->flags |= MODULE_FLAG_NEED_DEPS;
  228. llist_add_to_end(&G.probes, m);
  229. G.num_unresolved_deps++;
  230. if (ENABLE_FEATURE_MODUTILS_SYMBOLS
  231. && strncmp(m->modname, "symbol:", 7) == 0
  232. ) {
  233. G.need_symbols = 1;
  234. }
  235. }
  236. static int FAST_FUNC config_file_action(const char *filename,
  237. struct stat *statbuf UNUSED_PARAM,
  238. void *userdata UNUSED_PARAM,
  239. int depth UNUSED_PARAM)
  240. {
  241. char *tokens[3];
  242. parser_t *p;
  243. struct module_entry *m;
  244. int rc = TRUE;
  245. if (bb_basename(filename)[0] == '.')
  246. goto error;
  247. p = config_open2(filename, fopen_for_read);
  248. if (p == NULL) {
  249. rc = FALSE;
  250. goto error;
  251. }
  252. while (config_read(p, tokens, 3, 2, "# \t", PARSE_NORMAL)) {
  253. //Use index_in_strings?
  254. if (strcmp(tokens[0], "alias") == 0) {
  255. /* alias <wildcard> <modulename> */
  256. llist_t *l;
  257. char wildcard[MODULE_NAME_LEN];
  258. char *rmod;
  259. if (tokens[2] == NULL)
  260. continue;
  261. filename2modname(tokens[1], wildcard);
  262. for (l = G.probes; l; l = l->link) {
  263. m = (struct module_entry *) l->data;
  264. if (fnmatch(wildcard, m->modname, 0) != 0)
  265. continue;
  266. rmod = filename2modname(tokens[2], NULL);
  267. llist_add_to(&m->realnames, rmod);
  268. if (m->flags & MODULE_FLAG_NEED_DEPS) {
  269. m->flags &= ~MODULE_FLAG_NEED_DEPS;
  270. G.num_unresolved_deps--;
  271. }
  272. m = get_or_add_modentry(rmod);
  273. if (!(m->flags & MODULE_FLAG_NEED_DEPS)) {
  274. m->flags |= MODULE_FLAG_NEED_DEPS;
  275. G.num_unresolved_deps++;
  276. }
  277. }
  278. } else if (strcmp(tokens[0], "options") == 0) {
  279. /* options <modulename> <option...> */
  280. if (tokens[2] == NULL)
  281. continue;
  282. m = get_or_add_modentry(tokens[1]);
  283. m->options = gather_options_str(m->options, tokens[2]);
  284. } else if (strcmp(tokens[0], "include") == 0) {
  285. /* include <filename> */
  286. read_config(tokens[1]);
  287. } else if (ENABLE_FEATURE_MODPROBE_BLACKLIST
  288. && strcmp(tokens[0], "blacklist") == 0
  289. ) {
  290. /* blacklist <modulename> */
  291. get_or_add_modentry(tokens[1])->flags |= MODULE_FLAG_BLACKLISTED;
  292. }
  293. }
  294. config_close(p);
  295. error:
  296. return rc;
  297. }
  298. static int read_config(const char *path)
  299. {
  300. return recursive_action(path, ACTION_RECURSE | ACTION_QUIET,
  301. config_file_action, NULL, NULL, 1);
  302. }
  303. static const char *humanly_readable_name(struct module_entry *m)
  304. {
  305. /* probed_name may be NULL. modname always exists. */
  306. return m->probed_name ? m->probed_name : m->modname;
  307. }
  308. static char *parse_and_add_kcmdline_module_options(char *options, const char *modulename)
  309. {
  310. char *kcmdline_buf;
  311. char *kcmdline;
  312. char *kptr;
  313. int len;
  314. kcmdline_buf = xmalloc_open_read_close("/proc/cmdline", NULL);
  315. if (!kcmdline_buf)
  316. return options;
  317. kcmdline = kcmdline_buf;
  318. len = strlen(modulename);
  319. while ((kptr = strsep(&kcmdline, "\n\t ")) != NULL) {
  320. if (strncmp(modulename, kptr, len) != 0)
  321. continue;
  322. kptr += len;
  323. if (*kptr != '.')
  324. continue;
  325. /* It is "modulename.xxxx" */
  326. kptr++;
  327. if (strchr(kptr, '=') != NULL) {
  328. /* It is "modulename.opt=[val]" */
  329. options = gather_options_str(options, kptr);
  330. }
  331. }
  332. free(kcmdline_buf);
  333. return options;
  334. }
  335. /* Return: similar to bb_init_module:
  336. * 0 on success,
  337. * -errno on open/read error,
  338. * errno on init_module() error
  339. */
  340. /* NB: INSMOD_OPT_SILENT bit suppresses ONLY non-existent modules,
  341. * not deleted ones (those are still listed in modules.dep).
  342. * module-init-tools version 3.4:
  343. * # modprobe bogus
  344. * FATAL: Module bogus not found. [exitcode 1]
  345. * # modprobe -q bogus [silent, exitcode still 1]
  346. * but:
  347. * # rm kernel/drivers/net/dummy.ko
  348. * # modprobe -q dummy
  349. * FATAL: Could not open '/lib/modules/xxx/kernel/drivers/net/dummy.ko': No such file or directory
  350. * [exitcode 1]
  351. */
  352. static int do_modprobe(struct module_entry *m)
  353. {
  354. int rc, first;
  355. if (!(m->flags & MODULE_FLAG_FOUND_IN_MODDEP)) {
  356. if (!(option_mask32 & INSMOD_OPT_SILENT))
  357. bb_error_msg("module %s not found in modules.dep",
  358. humanly_readable_name(m));
  359. return -ENOENT;
  360. }
  361. DBG("do_modprob'ing %s", m->modname);
  362. if (!(option_mask32 & OPT_REMOVE))
  363. m->deps = llist_rev(m->deps);
  364. if (0) {
  365. llist_t *l;
  366. for (l = m->deps; l; l = l->link)
  367. DBG("dep: %s", l->data);
  368. }
  369. first = 1;
  370. rc = 0;
  371. while (m->deps) {
  372. struct module_entry *m2;
  373. char *fn, *options;
  374. rc = 0;
  375. fn = llist_pop(&m->deps); /* we leak it */
  376. m2 = get_or_add_modentry(fn);
  377. if (option_mask32 & OPT_REMOVE) {
  378. /* modprobe -r */
  379. if (m2->flags & MODULE_FLAG_LOADED) {
  380. rc = bb_delete_module(m2->modname, O_EXCL);
  381. if (rc) {
  382. if (first) {
  383. bb_error_msg("can't unload module %s: %s",
  384. humanly_readable_name(m2),
  385. moderror(rc));
  386. break;
  387. }
  388. } else {
  389. m2->flags &= ~MODULE_FLAG_LOADED;
  390. }
  391. }
  392. /* do not error out if *deps* fail to unload */
  393. first = 0;
  394. continue;
  395. }
  396. options = m2->options;
  397. m2->options = NULL;
  398. options = parse_and_add_kcmdline_module_options(options, m2->modname);
  399. if (m == m2)
  400. options = gather_options_str(options, G.cmdline_mopts);
  401. if (option_mask32 & OPT_SHOW_DEPS) {
  402. printf(options ? "insmod %s/%s/%s %s\n"
  403. : "insmod %s/%s/%s\n",
  404. CONFIG_DEFAULT_MODULES_DIR, G.uts.release, fn,
  405. options);
  406. free(options);
  407. continue;
  408. }
  409. if (m2->flags & MODULE_FLAG_LOADED) {
  410. DBG("%s is already loaded, skipping", fn);
  411. free(options);
  412. continue;
  413. }
  414. rc = bb_init_module(fn, options);
  415. DBG("loaded %s '%s', rc:%d", fn, options, rc);
  416. if (rc == EEXIST)
  417. rc = 0;
  418. free(options);
  419. if (rc) {
  420. bb_error_msg("can't load module %s (%s): %s",
  421. humanly_readable_name(m2),
  422. fn,
  423. moderror(rc)
  424. );
  425. break;
  426. }
  427. m2->flags |= MODULE_FLAG_LOADED;
  428. }
  429. return rc;
  430. }
  431. static void load_modules_dep(void)
  432. {
  433. struct module_entry *m;
  434. char *colon, *tokens[2];
  435. parser_t *p;
  436. /* Modprobe does not work at all without modules.dep,
  437. * even if the full module name is given. Returning error here
  438. * was making us later confuse user with this message:
  439. * "module /full/path/to/existing/file/module.ko not found".
  440. * It's better to die immediately, with good message.
  441. * xfopen_for_read provides that. */
  442. p = config_open2(CONFIG_DEFAULT_DEPMOD_FILE, xfopen_for_read);
  443. while (G.num_unresolved_deps
  444. && config_read(p, tokens, 2, 1, "# \t", PARSE_NORMAL)
  445. ) {
  446. colon = last_char_is(tokens[0], ':');
  447. if (colon == NULL)
  448. continue;
  449. *colon = 0;
  450. m = get_modentry(tokens[0]);
  451. if (m == NULL)
  452. continue;
  453. /* Optimization... */
  454. if ((m->flags & MODULE_FLAG_LOADED)
  455. && !(option_mask32 & (OPT_REMOVE | OPT_SHOW_DEPS))
  456. ) {
  457. DBG("skip deps of %s, it's already loaded", tokens[0]);
  458. continue;
  459. }
  460. m->flags |= MODULE_FLAG_FOUND_IN_MODDEP;
  461. if ((m->flags & MODULE_FLAG_NEED_DEPS) && (m->deps == NULL)) {
  462. G.num_unresolved_deps--;
  463. llist_add_to(&m->deps, xstrdup(tokens[0]));
  464. if (tokens[1])
  465. string_to_llist(tokens[1], &m->deps, " \t");
  466. } else
  467. DBG("skipping dep line");
  468. }
  469. config_close(p);
  470. }
  471. int modprobe_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  472. int modprobe_main(int argc UNUSED_PARAM, char **argv)
  473. {
  474. int rc;
  475. unsigned opt;
  476. struct module_entry *me;
  477. INIT_G();
  478. IF_LONG_OPTS(applet_long_options = modprobe_longopts;)
  479. opt_complementary = MODPROBE_COMPLEMENTARY;
  480. opt = getopt32(argv, INSMOD_OPTS MODPROBE_OPTS INSMOD_ARGS);
  481. argv += optind;
  482. /* Goto modules location */
  483. xchdir(CONFIG_DEFAULT_MODULES_DIR);
  484. uname(&G.uts);
  485. xchdir(G.uts.release);
  486. if (opt & OPT_LIST_ONLY) {
  487. int i;
  488. char name[MODULE_NAME_LEN];
  489. char *colon, *tokens[2];
  490. parser_t *p = config_open2(CONFIG_DEFAULT_DEPMOD_FILE, xfopen_for_read);
  491. for (i = 0; argv[i]; i++)
  492. replace(argv[i], '-', '_');
  493. while (config_read(p, tokens, 2, 1, "# \t", PARSE_NORMAL)) {
  494. colon = last_char_is(tokens[0], ':');
  495. if (!colon)
  496. continue;
  497. *colon = '\0';
  498. filename2modname(tokens[0], name);
  499. if (!argv[0])
  500. puts(tokens[0]);
  501. else {
  502. for (i = 0; argv[i]; i++) {
  503. if (fnmatch(argv[i], name, 0) == 0) {
  504. puts(tokens[0]);
  505. }
  506. }
  507. }
  508. }
  509. return EXIT_SUCCESS;
  510. }
  511. /* Yes, for some reason -l ignores -s... */
  512. if (opt & INSMOD_OPT_SYSLOG)
  513. logmode = LOGMODE_SYSLOG;
  514. if (!argv[0]) {
  515. if (opt & OPT_REMOVE) {
  516. /* "modprobe -r" (w/o params).
  517. * "If name is NULL, all unused modules marked
  518. * autoclean will be removed".
  519. */
  520. if (bb_delete_module(NULL, O_NONBLOCK | O_EXCL) != 0)
  521. bb_perror_msg_and_die("rmmod");
  522. }
  523. return EXIT_SUCCESS;
  524. }
  525. /* Retrieve module names of already loaded modules */
  526. {
  527. char *s;
  528. parser_t *parser = config_open2("/proc/modules", fopen_for_read);
  529. while (config_read(parser, &s, 1, 1, "# \t", PARSE_NORMAL & ~PARSE_GREEDY))
  530. get_or_add_modentry(s)->flags |= MODULE_FLAG_LOADED;
  531. config_close(parser);
  532. }
  533. if (opt & (OPT_INSERT_ALL | OPT_REMOVE)) {
  534. /* Each argument is a module name */
  535. do {
  536. DBG("adding module %s", *argv);
  537. add_probe(*argv++);
  538. } while (*argv);
  539. } else {
  540. /* First argument is module name, rest are parameters */
  541. DBG("probing just module %s", *argv);
  542. add_probe(argv[0]);
  543. G.cmdline_mopts = parse_cmdline_module_options(argv, /*quote_spaces:*/ 1);
  544. }
  545. /* Happens if all requested modules are already loaded */
  546. if (G.probes == NULL)
  547. return EXIT_SUCCESS;
  548. read_config("/etc/modprobe.conf");
  549. read_config("/etc/modprobe.d");
  550. if (ENABLE_FEATURE_MODUTILS_SYMBOLS && G.need_symbols)
  551. read_config("modules.symbols");
  552. load_modules_dep();
  553. if (ENABLE_FEATURE_MODUTILS_ALIAS && G.num_unresolved_deps) {
  554. read_config("modules.alias");
  555. load_modules_dep();
  556. }
  557. rc = 0;
  558. while ((me = llist_pop(&G.probes)) != NULL) {
  559. if (me->realnames == NULL) {
  560. DBG("probing by module name");
  561. /* This is not an alias. Literal names are blacklisted
  562. * only if '-b' is given.
  563. */
  564. if (!(opt & OPT_BLACKLIST)
  565. || !(me->flags & MODULE_FLAG_BLACKLISTED)
  566. ) {
  567. rc |= do_modprobe(me);
  568. }
  569. continue;
  570. }
  571. /* Probe all real names for the alias */
  572. do {
  573. char *realname = llist_pop(&me->realnames);
  574. struct module_entry *m2;
  575. DBG("probing alias %s by realname %s", me->modname, realname);
  576. m2 = get_or_add_modentry(realname);
  577. if (!(m2->flags & MODULE_FLAG_BLACKLISTED)
  578. && (!(m2->flags & MODULE_FLAG_LOADED)
  579. || (opt & (OPT_REMOVE | OPT_SHOW_DEPS)))
  580. ) {
  581. //TODO: we can pass "me" as 2nd param to do_modprobe,
  582. //and make do_modprobe emit more meaningful error messages
  583. //with alias name included, not just module name alias resolves to.
  584. rc |= do_modprobe(m2);
  585. }
  586. free(realname);
  587. } while (me->realnames != NULL);
  588. }
  589. return (rc != 0);
  590. }