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"
  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(modprobe, BB_DIR_SBIN, BB_SUID_DROP)))
  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, *ext;
  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. ext = strrchr(base, '.');
  249. if (ext == NULL || strcmp(ext + 1, "conf"))
  250. goto error;
  251. }
  252. p = config_open2(filename, fopen_for_read);
  253. if (p == NULL) {
  254. rc = FALSE;
  255. goto error;
  256. }
  257. while (config_read(p, tokens, 3, 2, "# \t", PARSE_NORMAL)) {
  258. //Use index_in_strings?
  259. if (strcmp(tokens[0], "alias") == 0) {
  260. /* alias <wildcard> <modulename> */
  261. llist_t *l;
  262. char wildcard[MODULE_NAME_LEN];
  263. char *rmod;
  264. if (tokens[2] == NULL)
  265. continue;
  266. filename2modname(tokens[1], wildcard);
  267. for (l = G.probes; l; l = l->link) {
  268. m = (struct module_entry *) l->data;
  269. if (fnmatch(wildcard, m->modname, 0) != 0)
  270. continue;
  271. rmod = filename2modname(tokens[2], NULL);
  272. llist_add_to(&m->realnames, rmod);
  273. if (m->flags & MODULE_FLAG_NEED_DEPS) {
  274. m->flags &= ~MODULE_FLAG_NEED_DEPS;
  275. G.num_unresolved_deps--;
  276. }
  277. m = get_or_add_modentry(rmod);
  278. if (!(m->flags & MODULE_FLAG_NEED_DEPS)) {
  279. m->flags |= MODULE_FLAG_NEED_DEPS;
  280. G.num_unresolved_deps++;
  281. }
  282. }
  283. } else if (strcmp(tokens[0], "options") == 0) {
  284. /* options <modulename> <option...> */
  285. if (tokens[2] == NULL)
  286. continue;
  287. m = get_or_add_modentry(tokens[1]);
  288. m->options = gather_options_str(m->options, tokens[2]);
  289. } else if (strcmp(tokens[0], "include") == 0) {
  290. /* include <filename>/<dirname> (yes, directories also must work) */
  291. read_config(tokens[1]);
  292. } else if (ENABLE_FEATURE_MODPROBE_BLACKLIST
  293. && strcmp(tokens[0], "blacklist") == 0
  294. ) {
  295. /* blacklist <modulename> */
  296. get_or_add_modentry(tokens[1])->flags |= MODULE_FLAG_BLACKLISTED;
  297. }
  298. }
  299. config_close(p);
  300. error:
  301. return rc;
  302. }
  303. static int read_config(const char *path)
  304. {
  305. return recursive_action(path, ACTION_RECURSE | ACTION_QUIET,
  306. config_file_action, NULL, NULL,
  307. /*depth:*/ 0);
  308. }
  309. static const char *humanly_readable_name(struct module_entry *m)
  310. {
  311. /* probed_name may be NULL. modname always exists. */
  312. return m->probed_name ? m->probed_name : m->modname;
  313. }
  314. /* Like strsep(&stringp, "\n\t ") but quoted text goes to single token
  315. * even if it contains whitespace.
  316. */
  317. static char *strsep_quotes(char **stringp)
  318. {
  319. char *s, *start = *stringp;
  320. if (!start)
  321. return NULL;
  322. for (s = start; ; s++) {
  323. switch (*s) {
  324. case '"':
  325. s = strchrnul(s + 1, '"'); /* find trailing quote */
  326. if (*s != '\0')
  327. s++; /* skip trailing quote */
  328. /* fall through */
  329. case '\0':
  330. case '\n':
  331. case '\t':
  332. case ' ':
  333. if (*s != '\0') {
  334. *s = '\0';
  335. *stringp = s + 1;
  336. } else {
  337. *stringp = NULL;
  338. }
  339. return start;
  340. }
  341. }
  342. }
  343. static char *parse_and_add_kcmdline_module_options(char *options, const char *modulename)
  344. {
  345. char *kcmdline_buf;
  346. char *kcmdline;
  347. char *kptr;
  348. kcmdline_buf = xmalloc_open_read_close("/proc/cmdline", NULL);
  349. if (!kcmdline_buf)
  350. return options;
  351. kcmdline = kcmdline_buf;
  352. while ((kptr = strsep_quotes(&kcmdline)) != NULL) {
  353. char *after_modulename = is_prefixed_with(kptr, modulename);
  354. if (!after_modulename || *after_modulename != '.')
  355. continue;
  356. /* It is "modulename.xxxx" */
  357. kptr = after_modulename + 1;
  358. if (strchr(kptr, '=') != NULL) {
  359. /* It is "modulename.opt=[val]" */
  360. options = gather_options_str(options, kptr);
  361. }
  362. }
  363. free(kcmdline_buf);
  364. return options;
  365. }
  366. /* Return: similar to bb_init_module:
  367. * 0 on success,
  368. * -errno on open/read error,
  369. * errno on init_module() error
  370. */
  371. /* NB: INSMOD_OPT_SILENT bit suppresses ONLY non-existent modules,
  372. * not deleted ones (those are still listed in modules.dep).
  373. * module-init-tools version 3.4:
  374. * # modprobe bogus
  375. * FATAL: Module bogus not found. [exitcode 1]
  376. * # modprobe -q bogus [silent, exitcode still 1]
  377. * but:
  378. * # rm kernel/drivers/net/dummy.ko
  379. * # modprobe -q dummy
  380. * FATAL: Could not open '/lib/modules/xxx/kernel/drivers/net/dummy.ko': No such file or directory
  381. * [exitcode 1]
  382. */
  383. static int do_modprobe(struct module_entry *m)
  384. {
  385. int rc, first;
  386. if (!(m->flags & MODULE_FLAG_FOUND_IN_MODDEP)) {
  387. if (!(option_mask32 & INSMOD_OPT_SILENT))
  388. bb_error_msg((m->flags & MODULE_FLAG_BUILTIN) ?
  389. "module %s is builtin" :
  390. "module %s not found in modules.dep",
  391. humanly_readable_name(m));
  392. return -ENOENT;
  393. }
  394. DBG("do_modprob'ing %s", m->modname);
  395. if (!(option_mask32 & OPT_REMOVE))
  396. m->deps = llist_rev(m->deps);
  397. if (0) {
  398. llist_t *l;
  399. for (l = m->deps; l; l = l->link)
  400. DBG("dep: %s", l->data);
  401. }
  402. first = 1;
  403. rc = 0;
  404. while (m->deps) {
  405. struct module_entry *m2;
  406. char *fn, *options;
  407. rc = 0;
  408. fn = llist_pop(&m->deps); /* we leak it */
  409. m2 = get_or_add_modentry(bb_get_last_path_component_nostrip(fn));
  410. if (option_mask32 & OPT_REMOVE) {
  411. /* modprobe -r */
  412. if (m2->flags & MODULE_FLAG_LOADED) {
  413. rc = bb_delete_module(m2->modname, O_EXCL);
  414. if (rc) {
  415. if (first) {
  416. bb_perror_msg("can't unload module '%s'",
  417. humanly_readable_name(m2));
  418. break;
  419. }
  420. } else {
  421. m2->flags &= ~MODULE_FLAG_LOADED;
  422. }
  423. }
  424. /* do not error out if *deps* fail to unload */
  425. first = 0;
  426. continue;
  427. }
  428. options = m2->options;
  429. m2->options = NULL;
  430. options = parse_and_add_kcmdline_module_options(options, m2->modname);
  431. #if ENABLE_FEATURE_CMDLINE_MODULE_OPTIONS
  432. if (m == m2)
  433. options = gather_options_str(options, G.cmdline_mopts);
  434. #endif
  435. if (option_mask32 & OPT_SHOW_DEPS) {
  436. printf(options ? "insmod %s/%s/%s %s\n"
  437. : "insmod %s/%s/%s\n",
  438. CONFIG_DEFAULT_MODULES_DIR, G.uts.release, fn,
  439. options);
  440. free(options);
  441. continue;
  442. }
  443. if (m2->flags & MODULE_FLAG_LOADED) {
  444. DBG("%s is already loaded, skipping", fn);
  445. free(options);
  446. continue;
  447. }
  448. rc = bb_init_module(fn, options);
  449. DBG("loaded %s '%s', rc:%d", fn, options, rc);
  450. if (rc == EEXIST)
  451. rc = 0;
  452. free(options);
  453. if (rc) {
  454. bb_error_msg("can't load module %s (%s): %s",
  455. humanly_readable_name(m2),
  456. fn,
  457. moderror(rc)
  458. );
  459. break;
  460. }
  461. m2->flags |= MODULE_FLAG_LOADED;
  462. }
  463. return rc;
  464. }
  465. static void load_modules_dep(void)
  466. {
  467. struct module_entry *m;
  468. char *colon, *tokens[2];
  469. parser_t *p;
  470. /* Modprobe does not work at all without modules.dep,
  471. * even if the full module name is given. Returning error here
  472. * was making us later confuse user with this message:
  473. * "module /full/path/to/existing/file/module.ko not found".
  474. * It's better to die immediately, with good message.
  475. * xfopen_for_read provides that. */
  476. p = config_open2(CONFIG_DEFAULT_DEPMOD_FILE, xfopen_for_read);
  477. while (G.num_unresolved_deps
  478. && config_read(p, tokens, 2, 1, "# \t", PARSE_NORMAL)
  479. ) {
  480. colon = last_char_is(tokens[0], ':');
  481. if (colon == NULL)
  482. continue;
  483. *colon = '\0';
  484. m = moddb_get(&G.db, bb_get_last_path_component_nostrip(tokens[0]));
  485. if (m == NULL)
  486. continue;
  487. /* Optimization... */
  488. if ((m->flags & MODULE_FLAG_LOADED)
  489. && !(option_mask32 & (OPT_REMOVE | OPT_SHOW_DEPS))
  490. ) {
  491. DBG("skip deps of %s, it's already loaded", tokens[0]);
  492. continue;
  493. }
  494. m->flags |= MODULE_FLAG_FOUND_IN_MODDEP;
  495. if ((m->flags & MODULE_FLAG_NEED_DEPS) && (m->deps == NULL)) {
  496. G.num_unresolved_deps--;
  497. llist_add_to(&m->deps, xstrdup(tokens[0]));
  498. if (tokens[1])
  499. string_to_llist(tokens[1], &m->deps, " \t");
  500. } else
  501. DBG("skipping dep line");
  502. }
  503. config_close(p);
  504. }
  505. int modprobe_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  506. int modprobe_main(int argc UNUSED_PARAM, char **argv)
  507. {
  508. int rc;
  509. unsigned opt;
  510. struct module_entry *me;
  511. INIT_G();
  512. IF_LONG_OPTS(applet_long_options = modprobe_longopts;)
  513. opt_complementary = MODPROBE_COMPLEMENTARY;
  514. opt = getopt32(argv, INSMOD_OPTS MODPROBE_OPTS INSMOD_ARGS);
  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. }