modprobe-small.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * simplified modprobe
  4. *
  5. * Copyright (c) 2008 Vladimir Dronnikov
  6. * Copyright (c) 2008 Bernhard Fischer (initial depmod code)
  7. *
  8. * Licensed under GPLv2, see file LICENSE in this tarball for details.
  9. */
  10. #include "libbb.h"
  11. #include <sys/utsname.h> /* uname() */
  12. #include <fnmatch.h>
  13. extern int init_module(void *module, unsigned long len, const char *options);
  14. extern int delete_module(const char *module, unsigned flags);
  15. extern int query_module(const char *name, int which, void *buf, size_t bufsize, size_t *ret);
  16. #define dbg1_error_msg(...) ((void)0)
  17. #define dbg2_error_msg(...) ((void)0)
  18. //#define dbg1_error_msg(...) bb_error_msg(__VA_ARGS__)
  19. //#define dbg2_error_msg(...) bb_error_msg(__VA_ARGS__)
  20. #define DEPFILE_BB CONFIG_DEFAULT_DEPMOD_FILE".bb"
  21. enum {
  22. OPT_q = (1 << 0), /* be quiet */
  23. OPT_r = (1 << 1), /* module removal instead of loading */
  24. };
  25. typedef struct module_info {
  26. char *pathname;
  27. char *aliases;
  28. char *deps;
  29. } module_info;
  30. /*
  31. * GLOBALS
  32. */
  33. struct globals {
  34. module_info *modinfo;
  35. char *module_load_options;
  36. smallint dep_bb_seen;
  37. smallint wrote_dep_bb_ok;
  38. int module_count;
  39. int module_found_idx;
  40. int stringbuf_idx;
  41. char stringbuf[32 * 1024]; /* some modules have lots of stuff */
  42. /* for example, drivers/media/video/saa7134/saa7134.ko */
  43. };
  44. #define G (*ptr_to_globals)
  45. #define modinfo (G.modinfo )
  46. #define dep_bb_seen (G.dep_bb_seen )
  47. #define wrote_dep_bb_ok (G.wrote_dep_bb_ok )
  48. #define module_count (G.module_count )
  49. #define module_found_idx (G.module_found_idx )
  50. #define module_load_options (G.module_load_options)
  51. #define stringbuf_idx (G.stringbuf_idx )
  52. #define stringbuf (G.stringbuf )
  53. #define INIT_G() do { \
  54. SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
  55. } while (0)
  56. static void appendc(char c)
  57. {
  58. if (stringbuf_idx < sizeof(stringbuf))
  59. stringbuf[stringbuf_idx++] = c;
  60. }
  61. static void bksp(void)
  62. {
  63. if (stringbuf_idx)
  64. stringbuf_idx--;
  65. }
  66. static void append(const char *s)
  67. {
  68. size_t len = strlen(s);
  69. if (stringbuf_idx + len < sizeof(stringbuf)) {
  70. memcpy(stringbuf + stringbuf_idx, s, len);
  71. stringbuf_idx += len;
  72. }
  73. }
  74. static void reset_stringbuf(void)
  75. {
  76. stringbuf_idx = 0;
  77. }
  78. static char* copy_stringbuf(void)
  79. {
  80. char *copy = xmalloc(stringbuf_idx);
  81. return memcpy(copy, stringbuf, stringbuf_idx);
  82. }
  83. static char* find_keyword(char *ptr, size_t len, const char *word)
  84. {
  85. int wlen;
  86. if (!ptr) /* happens if xmalloc_open_zipped_read_close cannot read it */
  87. return NULL;
  88. wlen = strlen(word);
  89. len -= wlen - 1;
  90. while ((ssize_t)len > 0) {
  91. char *old = ptr;
  92. /* search for the first char in word */
  93. ptr = memchr(ptr, *word, len);
  94. if (ptr == NULL) /* no occurance left, done */
  95. break;
  96. if (strncmp(ptr, word, wlen) == 0)
  97. return ptr + wlen; /* found, return ptr past it */
  98. ++ptr;
  99. len -= (ptr - old);
  100. }
  101. return NULL;
  102. }
  103. static void replace(char *s, char what, char with)
  104. {
  105. while (*s) {
  106. if (what == *s)
  107. *s = with;
  108. ++s;
  109. }
  110. }
  111. /* Take "word word", return malloced "word",NUL,"word",NUL,NUL */
  112. static char* str_2_list(const char *str)
  113. {
  114. int len = strlen(str) + 1;
  115. char *dst = xmalloc(len + 1);
  116. dst[len] = '\0';
  117. memcpy(dst, str, len);
  118. //TODO: protect against 2+ spaces: "word word"
  119. replace(dst, ' ', '\0');
  120. return dst;
  121. }
  122. /* We use error numbers in a loose translation... */
  123. static const char *moderror(int err)
  124. {
  125. switch (err) {
  126. case ENOEXEC:
  127. return "invalid module format";
  128. case ENOENT:
  129. return "unknown symbol in module or invalid parameter";
  130. case ESRCH:
  131. return "module has wrong symbol version";
  132. case EINVAL: /* "invalid parameter" */
  133. return "unknown symbol in module or invalid parameter"
  134. + sizeof("unknown symbol in module or");
  135. default:
  136. return strerror(err);
  137. }
  138. }
  139. static int load_module(const char *fname, const char *options)
  140. {
  141. #if 1
  142. int r;
  143. size_t len = MAXINT(ssize_t);
  144. char *module_image;
  145. dbg1_error_msg("load_module('%s','%s')", fname, options);
  146. module_image = xmalloc_open_zipped_read_close(fname, &len);
  147. r = (!module_image || init_module(module_image, len, options ? options : "") != 0);
  148. free(module_image);
  149. dbg1_error_msg("load_module:%d", r);
  150. return r; /* 0 = success */
  151. #else
  152. /* For testing */
  153. dbg1_error_msg("load_module('%s','%s')", fname, options);
  154. return 1;
  155. #endif
  156. }
  157. static void parse_module(module_info *info, const char *pathname)
  158. {
  159. char *module_image;
  160. char *ptr;
  161. size_t len;
  162. size_t pos;
  163. dbg1_error_msg("parse_module('%s')", pathname);
  164. /* Read (possibly compressed) module */
  165. len = 64 * 1024 * 1024; /* 64 Mb at most */
  166. module_image = xmalloc_open_zipped_read_close(pathname, &len);
  167. //TODO: optimize redundant module body reads
  168. /* "alias1 symbol:sym1 alias2 symbol:sym2" */
  169. reset_stringbuf();
  170. pos = 0;
  171. while (1) {
  172. ptr = find_keyword(module_image + pos, len - pos, "alias=");
  173. if (!ptr) {
  174. ptr = find_keyword(module_image + pos, len - pos, "__ksymtab_");
  175. if (!ptr)
  176. break;
  177. /* DOCME: __ksymtab_gpl and __ksymtab_strings occur
  178. * in many modules. What do they mean? */
  179. if (strcmp(ptr, "gpl") == 0 || strcmp(ptr, "strings") == 0)
  180. goto skip;
  181. dbg2_error_msg("alias:'symbol:%s'", ptr);
  182. append("symbol:");
  183. } else {
  184. dbg2_error_msg("alias:'%s'", ptr);
  185. }
  186. append(ptr);
  187. appendc(' ');
  188. skip:
  189. pos = (ptr - module_image);
  190. }
  191. bksp(); /* remove last ' ' */
  192. appendc('\0');
  193. info->aliases = copy_stringbuf();
  194. /* "dependency1 depandency2" */
  195. reset_stringbuf();
  196. ptr = find_keyword(module_image, len, "depends=");
  197. if (ptr && *ptr) {
  198. replace(ptr, ',', ' ');
  199. replace(ptr, '-', '_');
  200. dbg2_error_msg("dep:'%s'", ptr);
  201. append(ptr);
  202. }
  203. appendc('\0');
  204. info->deps = copy_stringbuf();
  205. free(module_image);
  206. }
  207. static int pathname_matches_modname(const char *pathname, const char *modname)
  208. {
  209. const char *fname = bb_get_last_path_component_nostrip(pathname);
  210. const char *suffix = strrstr(fname, ".ko");
  211. //TODO: can do without malloc?
  212. char *name = xstrndup(fname, suffix - fname);
  213. int r;
  214. replace(name, '-', '_');
  215. r = (strcmp(name, modname) == 0);
  216. free(name);
  217. return r;
  218. }
  219. static FAST_FUNC int fileAction(const char *pathname,
  220. struct stat *sb UNUSED_PARAM,
  221. void *modname_to_match,
  222. int depth UNUSED_PARAM)
  223. {
  224. int cur;
  225. const char *fname;
  226. pathname += 2; /* skip "./" */
  227. fname = bb_get_last_path_component_nostrip(pathname);
  228. if (!strrstr(fname, ".ko")) {
  229. dbg1_error_msg("'%s' is not a module", pathname);
  230. return TRUE; /* not a module, continue search */
  231. }
  232. cur = module_count++;
  233. modinfo = xrealloc_vector(modinfo, 12, cur);
  234. modinfo[cur].pathname = xstrdup(pathname);
  235. /*modinfo[cur].aliases = NULL; - xrealloc_vector did it */
  236. /*modinfo[cur+1].pathname = NULL;*/
  237. if (!pathname_matches_modname(fname, modname_to_match)) {
  238. dbg1_error_msg("'%s' module name doesn't match", pathname);
  239. return TRUE; /* module name doesn't match, continue search */
  240. }
  241. dbg1_error_msg("'%s' module name matches", pathname);
  242. module_found_idx = cur;
  243. parse_module(&modinfo[cur], pathname);
  244. if (!(option_mask32 & OPT_r)) {
  245. if (load_module(pathname, module_load_options) == 0) {
  246. /* Load was successful, there is nothing else to do.
  247. * This can happen ONLY for "top-level" module load,
  248. * not a dep, because deps dont do dirscan. */
  249. exit(EXIT_SUCCESS);
  250. }
  251. }
  252. return TRUE;
  253. }
  254. static int load_dep_bb(void)
  255. {
  256. char *line;
  257. FILE *fp = fopen_for_read(DEPFILE_BB);
  258. if (!fp)
  259. return 0;
  260. dep_bb_seen = 1;
  261. dbg1_error_msg("loading "DEPFILE_BB);
  262. /* Why? There is a rare scenario: we did not find modprobe.dep.bb,
  263. * we scanned the dir and found no module by name, then we search
  264. * for alias (full scan), and we decided to generate modprobe.dep.bb.
  265. * But we see modprobe.dep.bb.new! Other modprobe is at work!
  266. * We wait and other modprobe renames it to modprobe.dep.bb.
  267. * Now we can use it.
  268. * But we already have modinfo[] filled, and "module_count = 0"
  269. * makes us start anew. Yes, we leak modinfo[].xxx pointers -
  270. * there is not much of data there anyway. */
  271. module_count = 0;
  272. memset(&modinfo[0], 0, sizeof(modinfo[0]));
  273. while ((line = xmalloc_fgetline(fp)) != NULL) {
  274. char* space;
  275. int cur;
  276. if (!line[0]) {
  277. free(line);
  278. continue;
  279. }
  280. space = strchrnul(line, ' ');
  281. cur = module_count++;
  282. modinfo = xrealloc_vector(modinfo, 12, cur);
  283. /*modinfo[cur+1].pathname = NULL; - xrealloc_vector did it */
  284. modinfo[cur].pathname = line; /* we take ownership of malloced block here */
  285. if (*space)
  286. *space++ = '\0';
  287. modinfo[cur].aliases = space;
  288. modinfo[cur].deps = xmalloc_fgetline(fp) ? : xzalloc(1);
  289. if (modinfo[cur].deps[0]) {
  290. /* deps are not "", so next line must be empty */
  291. line = xmalloc_fgetline(fp);
  292. /* Refuse to work with damaged config file */
  293. if (line && line[0])
  294. bb_error_msg_and_die("error in %s at '%s'", DEPFILE_BB, line);
  295. free(line);
  296. }
  297. }
  298. return 1;
  299. }
  300. static int start_dep_bb_writeout(void)
  301. {
  302. int fd;
  303. /* depmod -n: write result to stdout */
  304. if (applet_name[0] == 'd' && (option_mask32 & 1))
  305. return STDOUT_FILENO;
  306. fd = open(DEPFILE_BB".new", O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0644);
  307. if (fd < 0) {
  308. if (errno == EEXIST) {
  309. int count = 5 * 20;
  310. dbg1_error_msg(DEPFILE_BB".new exists, waiting for "DEPFILE_BB);
  311. while (1) {
  312. usleep(1000*1000 / 20);
  313. if (load_dep_bb()) {
  314. dbg1_error_msg(DEPFILE_BB" appeared");
  315. return -2; /* magic number */
  316. }
  317. if (!--count)
  318. break;
  319. }
  320. bb_error_msg("deleting stale %s", DEPFILE_BB".new");
  321. fd = open_or_warn(DEPFILE_BB".new", O_WRONLY | O_CREAT | O_TRUNC);
  322. }
  323. }
  324. dbg1_error_msg("opened "DEPFILE_BB".new:%d", fd);
  325. return fd;
  326. }
  327. static void write_out_dep_bb(int fd)
  328. {
  329. int i;
  330. FILE *fp;
  331. /* We want good error reporting. fdprintf is not good enough. */
  332. fp = fdopen(fd, "w");
  333. if (!fp) {
  334. close(fd);
  335. goto err;
  336. }
  337. i = 0;
  338. while (modinfo[i].pathname) {
  339. fprintf(fp, "%s%s%s\n" "%s%s\n",
  340. modinfo[i].pathname, modinfo[i].aliases[0] ? " " : "", modinfo[i].aliases,
  341. modinfo[i].deps, modinfo[i].deps[0] ? "\n" : "");
  342. i++;
  343. }
  344. /* Badly formatted depfile is a no-no. Be paranoid. */
  345. errno = 0;
  346. if (ferror(fp) | fclose(fp)) /* | instead of || is intended */
  347. goto err;
  348. if (fd == STDOUT_FILENO) /* it was depmod -n */
  349. goto ok;
  350. if (rename(DEPFILE_BB".new", DEPFILE_BB) != 0) {
  351. err:
  352. bb_perror_msg("can't create %s", DEPFILE_BB);
  353. unlink(DEPFILE_BB".new");
  354. } else {
  355. ok:
  356. wrote_dep_bb_ok = 1;
  357. dbg1_error_msg("created "DEPFILE_BB);
  358. }
  359. }
  360. static module_info* find_alias(const char *alias)
  361. {
  362. int i;
  363. int dep_bb_fd;
  364. module_info *result;
  365. dbg1_error_msg("find_alias('%s')", alias);
  366. try_again:
  367. /* First try to find by name (cheaper) */
  368. i = 0;
  369. while (modinfo[i].pathname) {
  370. if (pathname_matches_modname(modinfo[i].pathname, alias)) {
  371. dbg1_error_msg("found '%s' in module '%s'",
  372. alias, modinfo[i].pathname);
  373. if (!modinfo[i].aliases) {
  374. parse_module(&modinfo[i], modinfo[i].pathname);
  375. }
  376. return &modinfo[i];
  377. }
  378. i++;
  379. }
  380. /* Ok, we definitely have to scan module bodies. This is a good
  381. * moment to generate modprobe.dep.bb, if it does not exist yet */
  382. dep_bb_fd = dep_bb_seen ? -1 : start_dep_bb_writeout();
  383. if (dep_bb_fd == -2) /* modprobe.dep.bb appeared? */
  384. goto try_again;
  385. /* Scan all module bodies, extract modinfo (it contains aliases) */
  386. i = 0;
  387. result = NULL;
  388. while (modinfo[i].pathname) {
  389. char *desc, *s;
  390. if (!modinfo[i].aliases) {
  391. parse_module(&modinfo[i], modinfo[i].pathname);
  392. }
  393. if (result) {
  394. i++;
  395. continue;
  396. }
  397. /* "alias1 symbol:sym1 alias2 symbol:sym2" */
  398. desc = str_2_list(modinfo[i].aliases);
  399. /* Does matching substring exist? */
  400. for (s = desc; *s; s += strlen(s) + 1) {
  401. /* Aliases in module bodies can be defined with
  402. * shell patterns. Example:
  403. * "pci:v000010DEd000000D9sv*sd*bc*sc*i*".
  404. * Plain strcmp() won't catch that */
  405. if (fnmatch(s, alias, 0) == 0) {
  406. dbg1_error_msg("found alias '%s' in module '%s'",
  407. alias, modinfo[i].pathname);
  408. result = &modinfo[i];
  409. break;
  410. }
  411. }
  412. free(desc);
  413. if (result && dep_bb_fd < 0)
  414. return result;
  415. i++;
  416. }
  417. /* Create module.dep.bb if needed */
  418. if (dep_bb_fd >= 0) {
  419. write_out_dep_bb(dep_bb_fd);
  420. }
  421. dbg1_error_msg("find_alias '%s' returns %p", alias, result);
  422. return result;
  423. }
  424. #if ENABLE_FEATURE_MODPROBE_SMALL_CHECK_ALREADY_LOADED
  425. // TODO: open only once, invent config_rewind()
  426. static int already_loaded(const char *name)
  427. {
  428. int ret = 0;
  429. char *s;
  430. parser_t *parser = config_open2("/proc/modules", xfopen_for_read);
  431. while (config_read(parser, &s, 1, 1, "# \t", PARSE_NORMAL & ~PARSE_GREEDY)) {
  432. if (strcmp(s, name) == 0) {
  433. ret = 1;
  434. break;
  435. }
  436. }
  437. config_close(parser);
  438. return ret;
  439. }
  440. #else
  441. #define already_loaded(name) is_rmmod
  442. #endif
  443. /*
  444. * Given modules definition and module name (or alias, or symbol)
  445. * load/remove the module respecting dependencies.
  446. * NB: also called by depmod with bogus name "/",
  447. * just in order to force modprobe.dep.bb creation.
  448. */
  449. #if !ENABLE_FEATURE_MODPROBE_SMALL_OPTIONS_ON_CMDLINE
  450. #define process_module(a,b) process_module(a)
  451. #define cmdline_options ""
  452. #endif
  453. static void process_module(char *name, const char *cmdline_options)
  454. {
  455. char *s, *deps, *options;
  456. module_info *info;
  457. int is_rmmod = (option_mask32 & OPT_r) != 0;
  458. dbg1_error_msg("process_module('%s','%s')", name, cmdline_options);
  459. replace(name, '-', '_');
  460. dbg1_error_msg("already_loaded:%d is_rmmod:%d", already_loaded(name), is_rmmod);
  461. if (already_loaded(name) != is_rmmod) {
  462. dbg1_error_msg("nothing to do for '%s'", name);
  463. return;
  464. }
  465. options = NULL;
  466. if (!is_rmmod) {
  467. char *opt_filename = xasprintf("/etc/modules/%s", name);
  468. options = xmalloc_open_read_close(opt_filename, NULL);
  469. if (options)
  470. replace(options, '\n', ' ');
  471. #if ENABLE_FEATURE_MODPROBE_SMALL_OPTIONS_ON_CMDLINE
  472. if (cmdline_options) {
  473. /* NB: cmdline_options always have one leading ' '
  474. * (see main()), we remove it here */
  475. char *op = xasprintf(options ? "%s %s" : "%s %s" + 3,
  476. cmdline_options + 1, options);
  477. free(options);
  478. options = op;
  479. }
  480. #endif
  481. free(opt_filename);
  482. module_load_options = options;
  483. dbg1_error_msg("process_module('%s'): options:'%s'", name, options);
  484. }
  485. if (!module_count) {
  486. /* Scan module directory. This is done only once.
  487. * It will attempt module load, and will exit(EXIT_SUCCESS)
  488. * on success. */
  489. module_found_idx = -1;
  490. recursive_action(".",
  491. ACTION_RECURSE, /* flags */
  492. fileAction, /* file action */
  493. NULL, /* dir action */
  494. name, /* user data */
  495. 0); /* depth */
  496. dbg1_error_msg("dirscan complete");
  497. /* Module was not found, or load failed, or is_rmmod */
  498. if (module_found_idx >= 0) { /* module was found */
  499. info = &modinfo[module_found_idx];
  500. } else { /* search for alias, not a plain module name */
  501. info = find_alias(name);
  502. }
  503. } else {
  504. info = find_alias(name);
  505. }
  506. /* rmmod? unload it by name */
  507. if (is_rmmod) {
  508. if (delete_module(name, O_NONBLOCK | O_EXCL) != 0
  509. && !(option_mask32 & OPT_q)
  510. ) {
  511. bb_perror_msg("remove '%s'", name);
  512. goto ret;
  513. }
  514. /* N.B. we do not stop here -
  515. * continue to unload modules on which the module depends:
  516. * "-r --remove: option causes modprobe to remove a module.
  517. * If the modules it depends on are also unused, modprobe
  518. * will try to remove them, too." */
  519. }
  520. if (!info) {
  521. /* both dirscan and find_alias found nothing */
  522. if (applet_name[0] != 'd') /* it wasn't depmod */
  523. bb_error_msg("module '%s' not found", name);
  524. //TODO: _and_die()?
  525. goto ret;
  526. }
  527. /* Iterate thru dependencies, trying to (un)load them */
  528. deps = str_2_list(info->deps);
  529. for (s = deps; *s; s += strlen(s) + 1) {
  530. //if (strcmp(name, s) != 0) // N.B. do loops exist?
  531. dbg1_error_msg("recurse on dep '%s'", s);
  532. process_module(s, NULL);
  533. dbg1_error_msg("recurse on dep '%s' done", s);
  534. }
  535. free(deps);
  536. /* insmod -> load it */
  537. if (!is_rmmod) {
  538. errno = 0;
  539. if (load_module(info->pathname, options) != 0) {
  540. if (EEXIST != errno) {
  541. bb_error_msg("'%s': %s",
  542. info->pathname,
  543. moderror(errno));
  544. } else {
  545. dbg1_error_msg("'%s': %s",
  546. info->pathname,
  547. moderror(errno));
  548. }
  549. }
  550. }
  551. ret:
  552. free(options);
  553. //TODO: return load attempt result from process_module.
  554. //If dep didn't load ok, continuing makes little sense.
  555. }
  556. #undef cmdline_options
  557. /* For reference, module-init-tools v3.4 options:
  558. # insmod
  559. Usage: insmod filename [args]
  560. # rmmod --help
  561. Usage: rmmod [-fhswvV] modulename ...
  562. -f (or --force) forces a module unload, and may crash your
  563. machine. This requires the Forced Module Removal option
  564. when the kernel was compiled.
  565. -h (or --help) prints this help text
  566. -s (or --syslog) says use syslog, not stderr
  567. -v (or --verbose) enables more messages
  568. -V (or --version) prints the version code
  569. -w (or --wait) begins module removal even if it is used
  570. and will stop new users from accessing the module (so it
  571. should eventually fall to zero).
  572. # modprobe
  573. Usage: modprobe [-v] [-V] [-C config-file] [-n] [-i] [-q] [-b]
  574. [-o <modname>] [ --dump-modversions ] <modname> [parameters...]
  575. modprobe -r [-n] [-i] [-v] <modulename> ...
  576. modprobe -l -t <dirname> [ -a <modulename> ...]
  577. # depmod --help
  578. depmod 3.4 -- part of module-init-tools
  579. depmod -[aA] [-n -e -v -q -V -r -u]
  580. [-b basedirectory] [forced_version]
  581. depmod [-n -e -v -q -r -u] [-F kernelsyms] module1.ko module2.ko ...
  582. If no arguments (except options) are given, "depmod -a" is assumed.
  583. depmod will output a dependancy list suitable for the modprobe utility.
  584. Options:
  585. -a, --all Probe all modules
  586. -A, --quick Only does the work if there's a new module
  587. -n, --show Write the dependency file on stdout only
  588. -e, --errsyms Report not supplied symbols
  589. -V, --version Print the release version
  590. -v, --verbose Enable verbose mode
  591. -h, --help Print this usage message
  592. The following options are useful for people managing distributions:
  593. -b basedirectory
  594. --basedir basedirectory
  595. Use an image of a module tree
  596. -F kernelsyms
  597. --filesyms kernelsyms
  598. Use the file instead of the current kernel symbols
  599. */
  600. int modprobe_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  601. int modprobe_main(int argc UNUSED_PARAM, char **argv)
  602. {
  603. struct utsname uts;
  604. char applet0 = applet_name[0];
  605. USE_FEATURE_MODPROBE_SMALL_OPTIONS_ON_CMDLINE(char *options;)
  606. /* are we lsmod? -> just dump /proc/modules */
  607. if ('l' == applet0) {
  608. xprint_and_close_file(xfopen_for_read("/proc/modules"));
  609. return EXIT_SUCCESS;
  610. }
  611. INIT_G();
  612. /* Prevent ugly corner cases with no modules at all */
  613. modinfo = xzalloc(sizeof(modinfo[0]));
  614. /* Goto modules directory */
  615. xchdir(CONFIG_DEFAULT_MODULES_DIR);
  616. uname(&uts); /* never fails */
  617. /* depmod? */
  618. if ('d' == applet0) {
  619. /* Supported:
  620. * -n: print result to stdout
  621. * -a: process all modules (default)
  622. * optional VERSION parameter
  623. * Ignored:
  624. * -A: do work only if a module is newer than depfile
  625. * -e: report any symbols which a module needs
  626. * which are not supplied by other modules or the kernel
  627. * -F FILE: System.map (symbols for -e)
  628. * -q, -r, -u: noop?
  629. * Not supported:
  630. * -b BASEDIR: (TODO!) modules are in
  631. * $BASEDIR/lib/modules/$VERSION
  632. * -v: human readable deps to stdout
  633. * -V: version (don't want to support it - people may depend
  634. * on it as an indicator of "standard" depmod)
  635. * -h: help (well duh)
  636. * module1.o module2.o parameters (just ignored for now)
  637. */
  638. getopt32(argv, "na" "AeF:qru" /* "b:vV", NULL */, NULL);
  639. argv += optind;
  640. /* if (argv[0] && argv[1]) bb_show_usage(); */
  641. /* Goto $VERSION directory */
  642. xchdir(argv[0] ? argv[0] : uts.release);
  643. /* Force full module scan by asking to find a bogus module.
  644. * This will generate modules.dep.bb as a side effect. */
  645. process_module((char*)"/", NULL);
  646. return !wrote_dep_bb_ok;
  647. }
  648. /* insmod, modprobe, rmmod require at least one argument */
  649. opt_complementary = "-1";
  650. /* only -q (quiet) and -r (rmmod),
  651. * the rest are accepted and ignored (compat) */
  652. getopt32(argv, "qrfsvw");
  653. argv += optind;
  654. /* are we rmmod? -> simulate modprobe -r */
  655. if ('r' == applet0) {
  656. option_mask32 |= OPT_r;
  657. }
  658. /* Goto $VERSION directory */
  659. xchdir(uts.release);
  660. #if ENABLE_FEATURE_MODPROBE_SMALL_OPTIONS_ON_CMDLINE
  661. /* If not rmmod, parse possible module options given on command line.
  662. * insmod/modprobe takes one module name, the rest are parameters. */
  663. options = NULL;
  664. if ('r' != applet0) {
  665. char **arg = argv;
  666. while (*++arg) {
  667. /* Enclose options in quotes */
  668. char *s = options;
  669. options = xasprintf("%s \"%s\"", s ? s : "", *arg);
  670. free(s);
  671. *arg = NULL;
  672. }
  673. }
  674. #else
  675. if ('r' != applet0)
  676. argv[1] = NULL;
  677. #endif
  678. /* Try to load modprobe.dep.bb */
  679. load_dep_bb();
  680. /* Load/remove modules.
  681. * Only rmmod loops here, insmod/modprobe has only argv[0] */
  682. do {
  683. process_module(*argv++, options);
  684. } while (*argv);
  685. if (ENABLE_FEATURE_CLEAN_UP) {
  686. USE_FEATURE_MODPROBE_SMALL_OPTIONS_ON_CMDLINE(free(options);)
  687. }
  688. return EXIT_SUCCESS;
  689. }