depmod.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 tarball for details.
  9. */
  10. #undef _GNU_SOURCE
  11. #define _GNU_SOURCE
  12. #include <libbb.h>
  13. #include <sys/utsname.h> /* uname() */
  14. #include "modutils.h"
  15. /*
  16. * Theory of operation:
  17. * - iterate over all modules and record their full path
  18. * - iterate over all modules looking for "depends=" entries
  19. * for each depends, look through our list of full paths and emit if found
  20. */
  21. typedef struct module_info {
  22. struct module_info *next;
  23. char *name, *modname;
  24. llist_t *dependencies;
  25. llist_t *aliases;
  26. llist_t *symbols;
  27. struct module_info *dnext, *dprev;
  28. } module_info;
  29. enum {
  30. ARG_a = (1<<0), /* All modules, ignore mods in argv */
  31. ARG_A = (1<<1), /* Only emit .ko that are newer than modules.dep file */
  32. ARG_b = (1<<2), /* base directory when modules are in staging area */
  33. ARG_e = (1<<3), /* with -F, print unresolved symbols */
  34. ARG_F = (1<<4), /* System.map that contains the symbols */
  35. ARG_n = (1<<5), /* dry-run, print to stdout only */
  36. ARG_r = (1<<6) /* Compat dummy. Linux Makefile uses it */
  37. };
  38. static int FAST_FUNC parse_module(const char *fname, struct stat *sb UNUSED_PARAM,
  39. void *data, int depth UNUSED_PARAM)
  40. {
  41. char modname[MODULE_NAME_LEN];
  42. module_info **first = (module_info **) data;
  43. char *image, *ptr;
  44. module_info *info;
  45. /* Arbitrary. Was sb->st_size, but that breaks .gz etc */
  46. size_t len = (64*1024*1024 - 4096);
  47. if (strrstr(fname, ".ko") == NULL)
  48. return TRUE;
  49. image = xmalloc_open_zipped_read_close(fname, &len);
  50. info = xzalloc(sizeof(*info));
  51. info->next = *first;
  52. *first = info;
  53. info->dnext = info->dprev = info;
  54. info->name = xasprintf("/%s", fname);
  55. info->modname = xstrdup(filename2modname(fname, modname));
  56. for (ptr = image; ptr < image + len - 10; ptr++) {
  57. if (strncmp(ptr, "depends=", 8) == 0) {
  58. char *u;
  59. ptr += 8;
  60. for (u = ptr; *u; u++)
  61. if (*u == '-')
  62. *u = '_';
  63. ptr += string_to_llist(ptr, &info->dependencies, ",");
  64. } else if (ENABLE_FEATURE_MODUTILS_ALIAS
  65. && strncmp(ptr, "alias=", 6) == 0
  66. ) {
  67. llist_add_to(&info->aliases, xstrdup(ptr + 6));
  68. ptr += strlen(ptr);
  69. } else if (ENABLE_FEATURE_MODUTILS_SYMBOLS
  70. && strncmp(ptr, "__ksymtab_", 10) == 0
  71. ) {
  72. ptr += 10;
  73. if (strncmp(ptr, "gpl", 3) == 0 ||
  74. strcmp(ptr, "strings") == 0)
  75. continue;
  76. llist_add_to(&info->symbols, xstrdup(ptr));
  77. ptr += strlen(ptr);
  78. }
  79. }
  80. free(image);
  81. return TRUE;
  82. }
  83. static module_info *find_module(module_info *modules, const char *modname)
  84. {
  85. module_info *m;
  86. for (m = modules; m != NULL; m = m->next)
  87. if (strcmp(m->modname, modname) == 0)
  88. return m;
  89. return NULL;
  90. }
  91. static void order_dep_list(module_info *modules, module_info *start,
  92. llist_t *add)
  93. {
  94. module_info *m;
  95. llist_t *n;
  96. for (n = add; n != NULL; n = n->link) {
  97. m = find_module(modules, n->data);
  98. if (m == NULL)
  99. continue;
  100. /* unlink current entry */
  101. m->dnext->dprev = m->dprev;
  102. m->dprev->dnext = m->dnext;
  103. /* and add it to tail */
  104. m->dnext = start;
  105. m->dprev = start->dprev;
  106. start->dprev->dnext = m;
  107. start->dprev = m;
  108. /* recurse */
  109. order_dep_list(modules, start, m->dependencies);
  110. }
  111. }
  112. static void xfreopen_write(const char *file, FILE *f)
  113. {
  114. if (freopen(file, "w", f) == NULL)
  115. bb_perror_msg_and_die("can't open '%s'", file);
  116. }
  117. int depmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  118. int depmod_main(int argc UNUSED_PARAM, char **argv)
  119. {
  120. module_info *modules = NULL, *m, *dep;
  121. const char *moddir_base = "/";
  122. char *moddir, *version;
  123. struct utsname uts;
  124. int tmp;
  125. getopt32(argv, "aAb:eF:nr", &moddir_base, NULL);
  126. argv += optind;
  127. /* goto modules location */
  128. xchdir(moddir_base);
  129. /* If a version is provided, then that kernel version's module directory
  130. * is used, rather than the current kernel version (as returned by
  131. * "uname -r"). */
  132. if (*argv && sscanf(*argv, "%d.%d.%d", &tmp, &tmp, &tmp) == 3) {
  133. version = *argv++;
  134. } else {
  135. uname(&uts);
  136. version = uts.release;
  137. }
  138. moddir = concat_path_file(&CONFIG_DEFAULT_MODULES_DIR[1], version);
  139. /* Scan modules */
  140. if (*argv) {
  141. char *modfile;
  142. struct stat sb;
  143. do {
  144. modfile = concat_path_file(moddir, *argv);
  145. xstat(modfile, &sb);
  146. parse_module(modfile, &sb, &modules, 0);
  147. free(modfile);
  148. } while (*(++argv));
  149. } else {
  150. recursive_action(moddir, ACTION_RECURSE,
  151. parse_module, NULL, &modules, 0);
  152. }
  153. /* Prepare for writing out the dep files */
  154. xchdir(moddir);
  155. if (ENABLE_FEATURE_CLEAN_UP)
  156. free(moddir);
  157. /* Generate dependency and alias files */
  158. if (!(option_mask32 & ARG_n))
  159. xfreopen_write(CONFIG_DEFAULT_DEPMOD_FILE, stdout);
  160. for (m = modules; m != NULL; m = m->next) {
  161. printf("%s:", m->name);
  162. order_dep_list(modules, m, m->dependencies);
  163. while (m->dnext != m) {
  164. dep = m->dnext;
  165. printf(" %s", dep->name);
  166. /* unlink current entry */
  167. dep->dnext->dprev = dep->dprev;
  168. dep->dprev->dnext = dep->dnext;
  169. dep->dnext = dep->dprev = dep;
  170. }
  171. bb_putchar('\n');
  172. }
  173. #if ENABLE_FEATURE_MODUTILS_ALIAS
  174. if (!(option_mask32 & ARG_n))
  175. xfreopen_write("modules.alias", stdout);
  176. for (m = modules; m != NULL; m = m->next) {
  177. const char *fname = bb_basename(m->name);
  178. int fnlen = strchrnul(fname, '.') - fname;
  179. while (m->aliases) {
  180. /* Last word can well be m->modname instead,
  181. * but depmod from module-init-tools 3.4
  182. * uses module basename, i.e., no s/-/_/g.
  183. * (pathname and .ko.* are still stripped)
  184. * Mimicking that... */
  185. printf("alias %s %.*s\n",
  186. (char*)llist_pop(&m->aliases),
  187. fnlen, fname);
  188. }
  189. }
  190. #endif
  191. #if ENABLE_FEATURE_MODUTILS_SYMBOLS
  192. if (!(option_mask32 & ARG_n))
  193. xfreopen_write("modules.symbols", stdout);
  194. for (m = modules; m != NULL; m = m->next) {
  195. const char *fname = bb_basename(m->name);
  196. int fnlen = strchrnul(fname, '.') - fname;
  197. while (m->symbols) {
  198. printf("alias symbol:%s %.*s\n",
  199. (char*)llist_pop(&m->symbols),
  200. fnlen, fname);
  201. }
  202. }
  203. #endif
  204. if (ENABLE_FEATURE_CLEAN_UP) {
  205. while (modules) {
  206. module_info *old = modules;
  207. modules = modules->next;
  208. free(old->name);
  209. free(old->modname);
  210. free(old);
  211. }
  212. }
  213. return EXIT_SUCCESS;
  214. }