depmod.c 6.6 KB

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