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