modprobe.c 21 KB

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