modprobe.c 20 KB

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