depmod.c 7.6 KB

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