3
0

depmod.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. //usage:#if !ENABLE_MODPROBE_SMALL
  12. //usage:#define depmod_trivial_usage NOUSAGE_STR
  13. //usage:#define depmod_full_usage ""
  14. //usage:#endif
  15. #include "libbb.h"
  16. #include "modutils.h"
  17. #include <sys/utsname.h> /* uname() */
  18. /*
  19. * Theory of operation:
  20. * - iterate over all modules and record their full path
  21. * - iterate over all modules looking for "depends=" entries
  22. * for each depends, look through our list of full paths and emit if found
  23. */
  24. typedef struct module_info {
  25. struct module_info *next;
  26. char *name, *modname;
  27. llist_t *dependencies;
  28. llist_t *aliases;
  29. llist_t *symbols;
  30. struct module_info *dnext, *dprev;
  31. } module_info;
  32. static int FAST_FUNC parse_module(const char *fname, struct stat *sb UNUSED_PARAM,
  33. void *data, int depth UNUSED_PARAM)
  34. {
  35. char modname[MODULE_NAME_LEN];
  36. module_info **first = (module_info **) data;
  37. char *image, *ptr;
  38. module_info *info;
  39. /* Arbitrary. Was sb->st_size, but that breaks .gz etc */
  40. size_t len = (64*1024*1024 - 4096);
  41. if (strrstr(fname, ".ko") == NULL)
  42. return TRUE;
  43. image = xmalloc_open_zipped_read_close(fname, &len);
  44. info = xzalloc(sizeof(*info));
  45. info->next = *first;
  46. *first = info;
  47. info->dnext = info->dprev = info;
  48. info->name = xstrdup(fname + 2); /* skip "./" */
  49. info->modname = xstrdup(filename2modname(fname, modname));
  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:
  114. * [-aAenv] [-C FILE or DIR] [-b BASE] [-F System.map] [VERSION] [MODFILES]...
  115. * -a --all
  116. * Probe all modules. Default if no MODFILES.
  117. * -A --quick
  118. * Check modules.dep's mtime against module files' mtimes.
  119. * -b --basedir BASE
  120. * Use $BASE/lib/modules/VERSION
  121. * -C --config FILE or DIR
  122. * Path to /etc/depmod.conf or /etc/depmod.d/
  123. * -e --errsyms
  124. * When combined with the -F option, this reports any symbols which
  125. * which are not supplied by other modules or kernel.
  126. * -F --filesyms System.map
  127. * -n --dry-run
  128. * Print modules.dep etc to standard output
  129. * -v --verbose
  130. * Print to stdout all the symbols each module depends on
  131. * and the module's file name which provides that symbol.
  132. * -r No-op
  133. * -u No-op
  134. * -q No-op
  135. *
  136. * So far we only support: [-rn] [-b BASE] [VERSION] [MODFILES]...
  137. * -aAeF are accepted but ignored. -vC are not accepted.
  138. */
  139. enum {
  140. //OPT_a = (1 << 0), /* All modules, ignore mods in argv */
  141. //OPT_A = (1 << 1), /* Only emit .ko that are newer than modules.dep file */
  142. OPT_b = (1 << 2), /* base directory when modules are in staging area */
  143. //OPT_e = (1 << 3), /* with -F, print unresolved symbols */
  144. //OPT_F = (1 << 4), /* System.map that contains the symbols */
  145. OPT_n = (1 << 5), /* dry-run, print to stdout only */
  146. OPT_r = (1 << 6), /* Compat dummy. Linux Makefile uses it */
  147. OPT_u = (1 << 7), /* -u,--unresolved-error: ignored */
  148. OPT_q = (1 << 8), /* -q,--quiet: ignored */
  149. OPT_C = (1 << 9), /* -C,--config etc_modules_conf: ignored */
  150. };
  151. int depmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  152. int depmod_main(int argc UNUSED_PARAM, char **argv)
  153. {
  154. module_info *modules, *m, *dep;
  155. const char *moddir_base = "/";
  156. char *moddir, *version;
  157. struct utsname uts;
  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. modules = NULL;
  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. for (m = modules; m != NULL; m = m->next) {
  190. printf("%s:", m->name);
  191. order_dep_list(modules, m, m->dependencies);
  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. for (m = modules; m != NULL; m = m->next) {
  206. const char *fname = bb_basename(m->name);
  207. int fnlen = strchrnul(fname, '.') - fname;
  208. while (m->aliases) {
  209. /* Last word can well be m->modname instead,
  210. * but depmod from module-init-tools 3.4
  211. * uses module basename, i.e., no s/-/_/g.
  212. * (pathname and .ko.* are still stripped)
  213. * Mimicking that... */
  214. printf("alias %s %.*s\n",
  215. (char*)llist_pop(&m->aliases),
  216. fnlen, fname);
  217. }
  218. }
  219. #endif
  220. #if ENABLE_FEATURE_MODUTILS_SYMBOLS
  221. if (!(option_mask32 & OPT_n))
  222. xfreopen_write("modules.symbols", stdout);
  223. for (m = modules; m != NULL; m = m->next) {
  224. const char *fname = bb_basename(m->name);
  225. int fnlen = strchrnul(fname, '.') - fname;
  226. while (m->symbols) {
  227. printf("alias symbol:%s %.*s\n",
  228. (char*)llist_pop(&m->symbols),
  229. fnlen, fname);
  230. }
  231. }
  232. #endif
  233. if (ENABLE_FEATURE_CLEAN_UP) {
  234. while (modules) {
  235. module_info *old = modules;
  236. modules = modules->next;
  237. free(old->name);
  238. free(old->modname);
  239. free(old);
  240. }
  241. }
  242. return EXIT_SUCCESS;
  243. }