depmod.c 7.5 KB

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