3
0

depmod.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * depmod - generate modules.dep
  4. * Copyright (c) 2008 Bernhard Reutner-Fischer
  5. * Copyrihgt (c) 2008 Timo Teras <timo.teras@iki.fi>
  6. * Copyright (c) 2008 Vladimir Dronnikov
  7. *
  8. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  9. */
  10. //config:config DEPMOD
  11. //config: bool "depmod (27 kb)"
  12. //config: default y
  13. //config: help
  14. //config: depmod generates modules.dep (and potentially modules.alias
  15. //config: and modules.symbols) that contain dependency information
  16. //config: for modprobe.
  17. //applet:IF_DEPMOD(IF_NOT_MODPROBE_SMALL(APPLET(depmod, BB_DIR_SBIN, BB_SUID_DROP)))
  18. //kbuild:ifneq ($(CONFIG_MODPROBE_SMALL),y)
  19. //kbuild:lib-$(CONFIG_DEPMOD) += depmod.o modutils.o
  20. //kbuild:endif
  21. #include "libbb.h"
  22. #include "modutils.h"
  23. #include <sys/utsname.h> /* uname() */
  24. /*
  25. * Theory of operation:
  26. * - iterate over all modules and record their full path
  27. * - iterate over all modules looking for "depends=" entries
  28. * for each depends, look through our list of full paths and emit if found
  29. */
  30. static int FAST_FUNC parse_module(struct recursive_state *state,
  31. const char *fname,
  32. struct stat *sb UNUSED_PARAM)
  33. {
  34. module_db *modules = state->userData;
  35. char *image, *ptr;
  36. module_entry *e;
  37. /* Arbitrary. Was sb->st_size, but that breaks .gz etc */
  38. size_t len = (64*1024*1024 - 4096);
  39. if (strrstr(fname, ".ko") == NULL)
  40. return TRUE;
  41. image = xmalloc_open_zipped_read_close(fname, &len);
  42. e = moddb_get_or_create(modules, bb_get_last_path_component_nostrip(fname));
  43. e->name = xstrdup(fname + 2); /* skip "./" */
  44. for (ptr = image; ptr < image + len - 10; ptr++) {
  45. if (is_prefixed_with(ptr, "depends=")) {
  46. char *u;
  47. ptr += 8;
  48. for (u = ptr; *u; u++)
  49. if (*u == '-')
  50. *u = '_';
  51. ptr += string_to_llist(ptr, &e->deps, ",");
  52. } else if (ENABLE_FEATURE_MODUTILS_ALIAS
  53. && is_prefixed_with(ptr, "alias=")
  54. ) {
  55. llist_add_to(&e->aliases, xstrdup(ptr + 6));
  56. ptr += strlen(ptr);
  57. } else if (ENABLE_FEATURE_MODUTILS_SYMBOLS
  58. && is_prefixed_with(ptr, "__ksymtab_")
  59. ) {
  60. ptr += 10;
  61. if (is_prefixed_with(ptr, "gpl")
  62. || strcmp(ptr, "strings") == 0
  63. ) {
  64. continue;
  65. }
  66. llist_add_to(&e->symbols, xstrdup(ptr));
  67. ptr += strlen(ptr);
  68. }
  69. }
  70. free(image);
  71. return TRUE;
  72. }
  73. static void order_dep_list(module_db *modules, module_entry *start, llist_t *add)
  74. {
  75. module_entry *m;
  76. llist_t *n;
  77. for (n = add; n != NULL; n = n->link) {
  78. m = moddb_get(modules, n->data);
  79. if (m == NULL)
  80. continue;
  81. /* unlink current entry */
  82. m->dnext->dprev = m->dprev;
  83. m->dprev->dnext = m->dnext;
  84. /* and add it to tail */
  85. m->dnext = start;
  86. m->dprev = start->dprev;
  87. start->dprev->dnext = m;
  88. start->dprev = m;
  89. /* recurse */
  90. order_dep_list(modules, start, m->deps);
  91. }
  92. }
  93. static void xfreopen_write(const char *file, FILE *f)
  94. {
  95. if (freopen(file, "w", f) == NULL)
  96. bb_perror_msg_and_die("can't open '%s'", file);
  97. }
  98. //usage:#if !ENABLE_MODPROBE_SMALL
  99. //usage:#define depmod_trivial_usage "[-n] [-b BASE] [VERSION] [MODFILES]..."
  100. //usage:#define depmod_full_usage "\n\n"
  101. //usage: "Generate modules.dep, alias, and symbols files"
  102. //usage: "\n"
  103. //usage: "\n -b BASE Use BASE/lib/modules/VERSION"
  104. //usage: "\n -n Dry run: print files to stdout"
  105. //usage:#endif
  106. /* Upstream usage:
  107. * [-aAenv] [-C FILE or DIR] [-b BASE] [-F System.map] [VERSION] [MODFILES]...
  108. * -a --all
  109. * Probe all modules. Default if no MODFILES.
  110. * -A --quick
  111. * Check modules.dep's mtime against module files' mtimes.
  112. * -b --basedir BASE
  113. * Use $BASE/lib/modules/VERSION
  114. * -C --config FILE or DIR
  115. * Path to /etc/depmod.conf or /etc/depmod.d/
  116. * -e --errsyms
  117. * When combined with the -F option, this reports any symbols
  118. * which are not supplied by other modules or kernel.
  119. * -F --filesyms System.map
  120. * -n --dry-run
  121. * Print modules.dep etc to standard output
  122. * -v --verbose
  123. * Print to stdout all the symbols each module depends on
  124. * and the module's file name which provides that symbol.
  125. * -r No-op
  126. * -u No-op
  127. * -q No-op
  128. *
  129. * So far we only support: [-n] [-b BASE] [VERSION] [MODFILES]...
  130. * Accepted but ignored:
  131. * -aAe
  132. * -F System.map
  133. * -C FILE/DIR
  134. *
  135. * Not accepted: -v
  136. */
  137. enum {
  138. //OPT_a = (1 << 0), /* All modules, ignore mods in argv */
  139. //OPT_A = (1 << 1), /* Only emit .ko that are newer than modules.dep file */
  140. OPT_b = (1 << 2), /* base directory when modules are in staging area */
  141. //OPT_e = (1 << 3), /* with -F, print unresolved symbols */
  142. //OPT_F = (1 << 4), /* System.map that contains the symbols */
  143. OPT_n = (1 << 5), /* dry-run, print to stdout only */
  144. OPT_r = (1 << 6), /* Compat dummy. Linux Makefile uses it */
  145. OPT_u = (1 << 7), /* -u,--unresolved-error: ignored */
  146. OPT_q = (1 << 8), /* -q,--quiet: ignored */
  147. OPT_C = (1 << 9), /* -C,--config etc_modules_conf: ignored */
  148. };
  149. int depmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  150. int depmod_main(int argc UNUSED_PARAM, char **argv)
  151. {
  152. module_db modules;
  153. module_entry *m, *dep;
  154. const char *moddir_base = "/";
  155. char *moddir, *version;
  156. struct utsname uts;
  157. unsigned i;
  158. int tmp;
  159. getopt32(argv, "aAb:eF:nruqC:", &moddir_base, NULL, NULL);
  160. argv += optind;
  161. /* goto modules location */
  162. xchdir(moddir_base);
  163. /* If a version is provided, then that kernel version's module directory
  164. * is used, rather than the current kernel version (as returned by
  165. * "uname -r"). */
  166. if (*argv && sscanf(*argv, "%u.%u.%u", &tmp, &tmp, &tmp) == 3) {
  167. version = *argv++;
  168. } else {
  169. uname(&uts);
  170. version = uts.release;
  171. }
  172. moddir = concat_path_file(&CONFIG_DEFAULT_MODULES_DIR[1], version);
  173. xchdir(moddir);
  174. if (ENABLE_FEATURE_CLEAN_UP)
  175. free(moddir);
  176. /* Scan modules */
  177. memset(&modules, 0, sizeof(modules));
  178. if (*argv) {
  179. do {
  180. recursive_action(*argv, 0 /* no ACTION_RECURSE! */,
  181. parse_module, NULL, &modules);
  182. } while (*++argv);
  183. } else {
  184. recursive_action(".", ACTION_RECURSE,
  185. parse_module, NULL, &modules);
  186. }
  187. /* Generate dependency and alias files */
  188. if (!(option_mask32 & OPT_n))
  189. xfreopen_write(CONFIG_DEFAULT_DEPMOD_FILE, stdout);
  190. moddb_foreach_module(&modules, m, i) {
  191. printf("%s:", m->name);
  192. order_dep_list(&modules, m, m->deps);
  193. while (m->dnext != m) {
  194. dep = m->dnext;
  195. printf(" %s", dep->name);
  196. /* unlink current entry */
  197. dep->dnext->dprev = dep->dprev;
  198. dep->dprev->dnext = dep->dnext;
  199. dep->dnext = dep->dprev = dep;
  200. }
  201. bb_putchar('\n');
  202. }
  203. #if ENABLE_FEATURE_MODUTILS_ALIAS
  204. if (!(option_mask32 & OPT_n))
  205. xfreopen_write("modules.alias", stdout);
  206. moddb_foreach_module(&modules, m, i) {
  207. while (m->aliases) {
  208. /*
  209. * Last word used to be a basename
  210. * (filename with path and .ko.* stripped)
  211. * at the time of module-init-tools 3.4.
  212. * kmod v.12 uses module name, i.e., s/-/_/g.
  213. */
  214. printf("alias %s %s\n",
  215. (char*)llist_pop(&m->aliases),
  216. m->modname);
  217. }
  218. }
  219. #endif
  220. #if ENABLE_FEATURE_MODUTILS_SYMBOLS
  221. if (!(option_mask32 & OPT_n))
  222. xfreopen_write("modules.symbols", stdout);
  223. moddb_foreach_module(&modules, m, i) {
  224. while (m->symbols) {
  225. printf("alias symbol:%s %s\n",
  226. (char*)llist_pop(&m->symbols),
  227. m->modname);
  228. }
  229. }
  230. #endif
  231. if (ENABLE_FEATURE_CLEAN_UP)
  232. moddb_free(&modules);
  233. return EXIT_SUCCESS;
  234. }