modprobe-small.c 21 KB

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