modprobe-small.c 24 KB

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