depmod.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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 (26 kb)"
  12. //config: default y
  13. //config: select PLATFORM_LINUX
  14. //config: help
  15. //config: depmod generates modules.dep (and potentially modules.alias
  16. //config: and modules.symbols) that contain dependency information
  17. //config: for modprobe.
  18. //applet:IF_DEPMOD(IF_NOT_MODPROBE_SMALL(APPLET(depmod, BB_DIR_SBIN, BB_SUID_DROP)))
  19. //kbuild:ifneq ($(CONFIG_MODPROBE_SMALL),y)
  20. //kbuild:lib-$(CONFIG_DEPMOD) += depmod.o modutils.o
  21. //kbuild:endif
  22. #include "libbb.h"
  23. #include "modutils.h"
  24. #include <sys/utsname.h> /* uname() */
  25. /*
  26. * Theory of operation:
  27. * - iterate over all modules and record their full path
  28. * - iterate over all modules looking for "depends=" entries
  29. * for each depends, look through our list of full paths and emit if found
  30. */
  31. static int FAST_FUNC parse_module(const char *fname, struct stat *sb UNUSED_PARAM,
  32. void *data, int depth UNUSED_PARAM)
  33. {
  34. module_db *modules = data;
  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. parse_module(*argv, /*sb (unused):*/ NULL, &modules, 0);
  181. } while (*++argv);
  182. } else {
  183. recursive_action(".", ACTION_RECURSE,
  184. parse_module, NULL, &modules, 0);
  185. }
  186. /* Generate dependency and alias files */
  187. if (!(option_mask32 & OPT_n))
  188. xfreopen_write(CONFIG_DEFAULT_DEPMOD_FILE, stdout);
  189. moddb_foreach_module(&modules, m, i) {
  190. printf("%s:", m->name);
  191. order_dep_list(&modules, m, m->deps);
  192. while (m->dnext != m) {
  193. dep = m->dnext;
  194. printf(" %s", dep->name);
  195. /* unlink current entry */
  196. dep->dnext->dprev = dep->dprev;
  197. dep->dprev->dnext = dep->dnext;
  198. dep->dnext = dep->dprev = dep;
  199. }
  200. bb_putchar('\n');
  201. }
  202. #if ENABLE_FEATURE_MODUTILS_ALIAS
  203. if (!(option_mask32 & OPT_n))
  204. xfreopen_write("modules.alias", stdout);
  205. moddb_foreach_module(&modules, m, i) {
  206. while (m->aliases) {
  207. /*
  208. * Last word used to be a basename
  209. * (filename with path and .ko.* stripped)
  210. * at the time of module-init-tools 3.4.
  211. * kmod v.12 uses module name, i.e., s/-/_/g.
  212. */
  213. printf("alias %s %s\n",
  214. (char*)llist_pop(&m->aliases),
  215. m->modname);
  216. }
  217. }
  218. #endif
  219. #if ENABLE_FEATURE_MODUTILS_SYMBOLS
  220. if (!(option_mask32 & OPT_n))
  221. xfreopen_write("modules.symbols", stdout);
  222. moddb_foreach_module(&modules, m, i) {
  223. while (m->symbols) {
  224. printf("alias symbol:%s %s\n",
  225. (char*)llist_pop(&m->symbols),
  226. m->modname);
  227. }
  228. }
  229. #endif
  230. if (ENABLE_FEATURE_CLEAN_UP)
  231. moddb_free(&modules);
  232. return EXIT_SUCCESS;
  233. }