3
0

modprobe.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702
  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 module_entry { /* I'll call it ME. */
  145. unsigned flags;
  146. char *modname; /* stripped of /path/, .ext and s/-/_/g */
  147. const char *probed_name; /* verbatim as seen on cmdline */
  148. char *options; /* options from config files */
  149. llist_t *realnames; /* strings. if this module is an alias, */
  150. /* real module name is one of these. */
  151. //Can there really be more than one? Example from real kernel?
  152. llist_t *deps; /* strings. modules we depend on */
  153. };
  154. #define DB_HASH_SIZE 256
  155. struct globals {
  156. llist_t *probes; /* MEs of module(s) requested on cmdline */
  157. char *cmdline_mopts; /* module options from cmdline */
  158. int num_unresolved_deps;
  159. /* bool. "Did we have 'symbol:FOO' requested on cmdline?" */
  160. smallint need_symbols;
  161. struct utsname uts;
  162. llist_t *db[DB_HASH_SIZE]; /* MEs of all modules ever seen (caching for speed) */
  163. } FIX_ALIASING;
  164. #define G (*ptr_to_globals)
  165. #define INIT_G() do { \
  166. SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
  167. } while (0)
  168. static int read_config(const char *path);
  169. static char *gather_options_str(char *opts, const char *append)
  170. {
  171. /* Speed-optimized. We call gather_options_str many times. */
  172. if (append) {
  173. if (opts == NULL) {
  174. opts = xstrdup(append);
  175. } else {
  176. int optlen = strlen(opts);
  177. opts = xrealloc(opts, optlen + strlen(append) + 2);
  178. sprintf(opts + optlen, " %s", append);
  179. }
  180. }
  181. return opts;
  182. }
  183. /* These three functions called many times, optimizing for speed.
  184. * Users reported minute-long delays when they runn iptables repeatedly
  185. * (iptables use modprobe to install needed kernel modules).
  186. */
  187. static struct module_entry *helper_get_module(const char *module, int create)
  188. {
  189. char modname[MODULE_NAME_LEN];
  190. struct module_entry *e;
  191. llist_t *l;
  192. unsigned i;
  193. unsigned hash;
  194. filename2modname(module, modname);
  195. hash = 0;
  196. for (i = 0; modname[i]; i++)
  197. hash = ((hash << 5) + hash) + modname[i];
  198. hash %= DB_HASH_SIZE;
  199. for (l = G.db[hash]; l; l = l->link) {
  200. e = (struct module_entry *) l->data;
  201. if (strcmp(e->modname, modname) == 0)
  202. return e;
  203. }
  204. if (!create)
  205. return NULL;
  206. e = xzalloc(sizeof(*e));
  207. e->modname = xstrdup(modname);
  208. llist_add_to(&G.db[hash], e);
  209. return e;
  210. }
  211. static ALWAYS_INLINE struct module_entry *get_or_add_modentry(const char *module)
  212. {
  213. return helper_get_module(module, 1);
  214. }
  215. /* So far this function always gets a module pathname, never an alias name.
  216. * The crucial difference is that pathname needs dirname stripping,
  217. * while alias name must NOT do it!
  218. * Testcase where dirname stripping is likely to go wrong: "modprobe devname:snd/timer"
  219. */
  220. static ALWAYS_INLINE struct module_entry *get_modentry(const char *pathname)
  221. {
  222. return helper_get_module(bb_get_last_path_component_nostrip(pathname), 0);
  223. }
  224. static void add_probe(const char *name)
  225. {
  226. struct module_entry *m;
  227. m = get_or_add_modentry(name);
  228. if (!(option_mask32 & (OPT_REMOVE | OPT_SHOW_DEPS))
  229. && (m->flags & MODULE_FLAG_LOADED)
  230. ) {
  231. DBG("skipping %s, it is already loaded", name);
  232. return;
  233. }
  234. DBG("queuing %s", name);
  235. m->probed_name = name;
  236. m->flags |= MODULE_FLAG_NEED_DEPS;
  237. llist_add_to_end(&G.probes, m);
  238. G.num_unresolved_deps++;
  239. if (ENABLE_FEATURE_MODUTILS_SYMBOLS
  240. && is_prefixed_with(m->modname, "symbol:")
  241. ) {
  242. G.need_symbols = 1;
  243. }
  244. }
  245. static int FAST_FUNC config_file_action(const char *filename,
  246. struct stat *statbuf UNUSED_PARAM,
  247. void *userdata UNUSED_PARAM,
  248. int depth UNUSED_PARAM)
  249. {
  250. char *tokens[3];
  251. parser_t *p;
  252. struct module_entry *m;
  253. int rc = TRUE;
  254. if (bb_basename(filename)[0] == '.')
  255. goto error;
  256. p = config_open2(filename, fopen_for_read);
  257. if (p == NULL) {
  258. rc = FALSE;
  259. goto error;
  260. }
  261. while (config_read(p, tokens, 3, 2, "# \t", PARSE_NORMAL)) {
  262. //Use index_in_strings?
  263. if (strcmp(tokens[0], "alias") == 0) {
  264. /* alias <wildcard> <modulename> */
  265. llist_t *l;
  266. char wildcard[MODULE_NAME_LEN];
  267. char *rmod;
  268. if (tokens[2] == NULL)
  269. continue;
  270. filename2modname(tokens[1], wildcard);
  271. for (l = G.probes; l; l = l->link) {
  272. m = (struct module_entry *) l->data;
  273. if (fnmatch(wildcard, m->modname, 0) != 0)
  274. continue;
  275. rmod = filename2modname(tokens[2], NULL);
  276. llist_add_to(&m->realnames, rmod);
  277. if (m->flags & MODULE_FLAG_NEED_DEPS) {
  278. m->flags &= ~MODULE_FLAG_NEED_DEPS;
  279. G.num_unresolved_deps--;
  280. }
  281. m = get_or_add_modentry(rmod);
  282. if (!(m->flags & MODULE_FLAG_NEED_DEPS)) {
  283. m->flags |= MODULE_FLAG_NEED_DEPS;
  284. G.num_unresolved_deps++;
  285. }
  286. }
  287. } else if (strcmp(tokens[0], "options") == 0) {
  288. /* options <modulename> <option...> */
  289. if (tokens[2] == NULL)
  290. continue;
  291. m = get_or_add_modentry(tokens[1]);
  292. m->options = gather_options_str(m->options, tokens[2]);
  293. } else if (strcmp(tokens[0], "include") == 0) {
  294. /* include <filename> */
  295. read_config(tokens[1]);
  296. } else if (ENABLE_FEATURE_MODPROBE_BLACKLIST
  297. && strcmp(tokens[0], "blacklist") == 0
  298. ) {
  299. /* blacklist <modulename> */
  300. get_or_add_modentry(tokens[1])->flags |= MODULE_FLAG_BLACKLISTED;
  301. }
  302. }
  303. config_close(p);
  304. error:
  305. return rc;
  306. }
  307. static int read_config(const char *path)
  308. {
  309. return recursive_action(path, ACTION_RECURSE | ACTION_QUIET,
  310. config_file_action, NULL, NULL, 1);
  311. }
  312. static const char *humanly_readable_name(struct module_entry *m)
  313. {
  314. /* probed_name may be NULL. modname always exists. */
  315. return m->probed_name ? m->probed_name : m->modname;
  316. }
  317. /* Like strsep(&stringp, "\n\t ") but quoted text goes to single token
  318. * even if it contains whitespace.
  319. */
  320. static char *strsep_quotes(char **stringp)
  321. {
  322. char *s, *start = *stringp;
  323. if (!start)
  324. return NULL;
  325. for (s = start; ; s++) {
  326. switch (*s) {
  327. case '"':
  328. s = strchrnul(s + 1, '"'); /* find trailing quote */
  329. if (*s != '\0')
  330. s++; /* skip trailing quote */
  331. /* fall through */
  332. case '\0':
  333. case '\n':
  334. case '\t':
  335. case ' ':
  336. if (*s != '\0') {
  337. *s = '\0';
  338. *stringp = s + 1;
  339. } else {
  340. *stringp = NULL;
  341. }
  342. return start;
  343. }
  344. }
  345. }
  346. static char *parse_and_add_kcmdline_module_options(char *options, const char *modulename)
  347. {
  348. char *kcmdline_buf;
  349. char *kcmdline;
  350. char *kptr;
  351. kcmdline_buf = xmalloc_open_read_close("/proc/cmdline", NULL);
  352. if (!kcmdline_buf)
  353. return options;
  354. kcmdline = kcmdline_buf;
  355. while ((kptr = strsep_quotes(&kcmdline)) != NULL) {
  356. char *after_modulename = is_prefixed_with(kptr, modulename);
  357. if (!after_modulename || *after_modulename != '.')
  358. continue;
  359. /* It is "modulename.xxxx" */
  360. kptr = after_modulename + 1;
  361. if (strchr(kptr, '=') != NULL) {
  362. /* It is "modulename.opt=[val]" */
  363. options = gather_options_str(options, kptr);
  364. }
  365. }
  366. free(kcmdline_buf);
  367. return options;
  368. }
  369. /* Return: similar to bb_init_module:
  370. * 0 on success,
  371. * -errno on open/read error,
  372. * errno on init_module() error
  373. */
  374. /* NB: INSMOD_OPT_SILENT bit suppresses ONLY non-existent modules,
  375. * not deleted ones (those are still listed in modules.dep).
  376. * module-init-tools version 3.4:
  377. * # modprobe bogus
  378. * FATAL: Module bogus not found. [exitcode 1]
  379. * # modprobe -q bogus [silent, exitcode still 1]
  380. * but:
  381. * # rm kernel/drivers/net/dummy.ko
  382. * # modprobe -q dummy
  383. * FATAL: Could not open '/lib/modules/xxx/kernel/drivers/net/dummy.ko': No such file or directory
  384. * [exitcode 1]
  385. */
  386. static int do_modprobe(struct module_entry *m)
  387. {
  388. int rc, first;
  389. if (!(m->flags & MODULE_FLAG_FOUND_IN_MODDEP)) {
  390. if (!(option_mask32 & INSMOD_OPT_SILENT))
  391. bb_error_msg("module %s not found in modules.dep",
  392. humanly_readable_name(m));
  393. return -ENOENT;
  394. }
  395. DBG("do_modprob'ing %s", m->modname);
  396. if (!(option_mask32 & OPT_REMOVE))
  397. m->deps = llist_rev(m->deps);
  398. if (0) {
  399. llist_t *l;
  400. for (l = m->deps; l; l = l->link)
  401. DBG("dep: %s", l->data);
  402. }
  403. first = 1;
  404. rc = 0;
  405. while (m->deps) {
  406. struct module_entry *m2;
  407. char *fn, *options;
  408. rc = 0;
  409. fn = llist_pop(&m->deps); /* we leak it */
  410. m2 = get_or_add_modentry(bb_get_last_path_component_nostrip(fn));
  411. if (option_mask32 & OPT_REMOVE) {
  412. /* modprobe -r */
  413. if (m2->flags & MODULE_FLAG_LOADED) {
  414. rc = bb_delete_module(m2->modname, O_EXCL);
  415. if (rc) {
  416. if (first) {
  417. bb_error_msg("can't unload module %s: %s",
  418. humanly_readable_name(m2),
  419. moderror(rc));
  420. break;
  421. }
  422. } else {
  423. m2->flags &= ~MODULE_FLAG_LOADED;
  424. }
  425. }
  426. /* do not error out if *deps* fail to unload */
  427. first = 0;
  428. continue;
  429. }
  430. options = m2->options;
  431. m2->options = NULL;
  432. options = parse_and_add_kcmdline_module_options(options, m2->modname);
  433. if (m == m2)
  434. options = gather_options_str(options, G.cmdline_mopts);
  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 = get_modentry(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_msg_and_die("rmmod");
  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. }
  570. if (opt & (OPT_INSERT_ALL | OPT_REMOVE)) {
  571. /* Each argument is a module name */
  572. do {
  573. DBG("adding module %s", *argv);
  574. add_probe(*argv++);
  575. } while (*argv);
  576. } else {
  577. /* First argument is module name, rest are parameters */
  578. DBG("probing just module %s", *argv);
  579. add_probe(argv[0]);
  580. G.cmdline_mopts = parse_cmdline_module_options(argv, /*quote_spaces:*/ 1);
  581. }
  582. /* Happens if all requested modules are already loaded */
  583. if (G.probes == NULL)
  584. return EXIT_SUCCESS;
  585. read_config("/etc/modprobe.conf");
  586. read_config("/etc/modprobe.d");
  587. if (ENABLE_FEATURE_MODUTILS_SYMBOLS && G.need_symbols)
  588. read_config("modules.symbols");
  589. load_modules_dep();
  590. if (ENABLE_FEATURE_MODUTILS_ALIAS && G.num_unresolved_deps) {
  591. read_config("modules.alias");
  592. load_modules_dep();
  593. }
  594. rc = 0;
  595. while ((me = llist_pop(&G.probes)) != NULL) {
  596. if (me->realnames == NULL) {
  597. DBG("probing by module name");
  598. /* This is not an alias. Literal names are blacklisted
  599. * only if '-b' is given.
  600. */
  601. if (!(opt & OPT_BLACKLIST)
  602. || !(me->flags & MODULE_FLAG_BLACKLISTED)
  603. ) {
  604. rc |= do_modprobe(me);
  605. }
  606. continue;
  607. }
  608. /* Probe all real names for the alias */
  609. do {
  610. char *realname = llist_pop(&me->realnames);
  611. struct module_entry *m2;
  612. DBG("probing alias %s by realname %s", me->modname, realname);
  613. m2 = get_or_add_modentry(realname);
  614. if (!(m2->flags & MODULE_FLAG_BLACKLISTED)
  615. && (!(m2->flags & MODULE_FLAG_LOADED)
  616. || (opt & (OPT_REMOVE | OPT_SHOW_DEPS)))
  617. ) {
  618. //TODO: we can pass "me" as 2nd param to do_modprobe,
  619. //and make do_modprobe emit more meaningful error messages
  620. //with alias name included, not just module name alias resolves to.
  621. rc |= do_modprobe(m2);
  622. }
  623. free(realname);
  624. } while (me->realnames != NULL);
  625. }
  626. return (rc != 0);
  627. }