depmod.c 7.5 KB

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