Bernhard Reutner-Fischer | 55e547e | 2008-05-26 12:01:49 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * depmod - generate modules.dep |
Bernhard Reutner-Fischer | 6c4dade | 2008-09-25 12:13:34 +0000 | [diff] [blame] | 4 | * Copyright (c) 2008 Bernhard Reutner-Fischer |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 5 | * Copyrihgt (c) 2008 Timo Teras <timo.teras@iki.fi> |
| 6 | * Copyright (c) 2008 Vladimir Dronnikov |
Bernhard Reutner-Fischer | 55e547e | 2008-05-26 12:01:49 +0000 | [diff] [blame] | 7 | * |
| 8 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
| 9 | */ |
| 10 | |
| 11 | #undef _GNU_SOURCE |
| 12 | #define _GNU_SOURCE |
| 13 | #include <libbb.h> |
| 14 | #include <sys/utsname.h> /* uname() */ |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 15 | #include "modutils.h" |
| 16 | |
Bernhard Reutner-Fischer | cf18010 | 2008-05-26 15:12:01 +0000 | [diff] [blame] | 17 | /* |
| 18 | * Theory of operation: |
| 19 | * - iterate over all modules and record their full path |
| 20 | * - iterate over all modules looking for "depends=" entries |
| 21 | * for each depends, look through our list of full paths and emit if found |
| 22 | */ |
Bernhard Reutner-Fischer | beac1bd | 2008-06-02 13:28:47 +0000 | [diff] [blame] | 23 | |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 24 | typedef struct module_info { |
| 25 | struct module_info *next; |
| 26 | char *name, *modname; |
Bernhard Reutner-Fischer | beac1bd | 2008-06-02 13:28:47 +0000 | [diff] [blame] | 27 | llist_t *dependencies; |
Bernhard Reutner-Fischer | c2741e1 | 2008-06-02 18:59:16 +0000 | [diff] [blame] | 28 | llist_t *aliases; |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 29 | llist_t *symbols; |
| 30 | struct module_info *dnext, *dprev; |
| 31 | } module_info; |
Bernhard Reutner-Fischer | beac1bd | 2008-06-02 13:28:47 +0000 | [diff] [blame] | 32 | |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 33 | enum { |
| 34 | ARG_a = (1<<0), /* All modules, ignore mods in argv */ |
| 35 | ARG_A = (1<<1), /* Only emit .ko that are newer than modules.dep file */ |
Denis Vlasenko | 3526115 | 2009-01-12 22:12:22 +0000 | [diff] [blame^] | 36 | ARG_b = (1<<2), /* base directory when modules are in staging area */ |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 37 | ARG_e = (1<<3), /* with -F, print unresolved symbols */ |
| 38 | ARG_F = (1<<4), /* System.map that contains the symbols */ |
| 39 | ARG_n = (1<<5) /* dry-run, print to stdout only */ |
Bernhard Reutner-Fischer | 55e547e | 2008-05-26 12:01:49 +0000 | [diff] [blame] | 40 | }; |
Bernhard Reutner-Fischer | 55e547e | 2008-05-26 12:01:49 +0000 | [diff] [blame] | 41 | |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 42 | static int FAST_FUNC parse_module(const char *fname, struct stat *sb, |
| 43 | void *data, int UNUSED_PARAM depth) |
Bernhard Reutner-Fischer | 55e547e | 2008-05-26 12:01:49 +0000 | [diff] [blame] | 44 | { |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 45 | module_info **first = (module_info **) data; |
| 46 | char *image, *ptr; |
| 47 | module_info *info; |
Bernhard Reutner-Fischer | cf18010 | 2008-05-26 15:12:01 +0000 | [diff] [blame] | 48 | size_t len = sb->st_size; |
Bernhard Reutner-Fischer | 55e547e | 2008-05-26 12:01:49 +0000 | [diff] [blame] | 49 | |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 50 | if (strrstr(fname, ".ko") == NULL) |
| 51 | return TRUE; |
Bernhard Reutner-Fischer | 634b022 | 2008-05-28 14:20:20 +0000 | [diff] [blame] | 52 | |
Denis Vlasenko | fc66892 | 2008-11-06 02:32:31 +0000 | [diff] [blame] | 53 | image = xmalloc_open_zipped_read_close(fname, &len); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 54 | info = xzalloc(sizeof(module_info)); |
Bernhard Reutner-Fischer | c2741e1 | 2008-06-02 18:59:16 +0000 | [diff] [blame] | 55 | |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 56 | info->next = *first; |
| 57 | *first = info; |
| 58 | |
| 59 | info->dnext = info->dprev = info; |
Denis Vlasenko | 3526115 | 2009-01-12 22:12:22 +0000 | [diff] [blame^] | 60 | info->name = xasprintf("/%s", fname); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 61 | info->modname = filename2modname(fname, NULL); |
| 62 | for (ptr = image; ptr < image + len - 10; ptr++) { |
| 63 | if (strncmp(ptr, "depends=", 8) == 0) { |
| 64 | char *u; |
| 65 | |
| 66 | ptr += 8; |
| 67 | for (u = ptr; *u; u++) |
| 68 | if (*u == '-') |
| 69 | *u = '_'; |
| 70 | ptr += string_to_llist(ptr, &info->dependencies, ","); |
| 71 | } else if (ENABLE_FEATURE_MODUTILS_ALIAS && |
| 72 | strncmp(ptr, "alias=", 6) == 0) { |
| 73 | llist_add_to(&info->aliases, xstrdup(ptr + 6)); |
| 74 | ptr += strlen(ptr); |
| 75 | } else if (ENABLE_FEATURE_MODUTILS_SYMBOLS && |
| 76 | strncmp(ptr, "__ksymtab_", 10) == 0) { |
| 77 | ptr += 10; |
| 78 | if (strncmp(ptr, "gpl", 3) == 0 || |
| 79 | strcmp(ptr, "strings") == 0) |
| 80 | continue; |
| 81 | llist_add_to(&info->symbols, xstrdup(ptr)); |
| 82 | ptr += strlen(ptr); |
| 83 | } |
Bernhard Reutner-Fischer | 55e547e | 2008-05-26 12:01:49 +0000 | [diff] [blame] | 84 | } |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 85 | free(image); |
| 86 | |
Bernhard Reutner-Fischer | 55e547e | 2008-05-26 12:01:49 +0000 | [diff] [blame] | 87 | return TRUE; |
| 88 | } |
| 89 | |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 90 | static module_info *find_module(module_info *modules, const char *modname) |
| 91 | { |
| 92 | module_info *m; |
| 93 | |
| 94 | for (m = modules; m != NULL; m = m->next) |
| 95 | if (strcmp(m->modname, modname) == 0) |
| 96 | return m; |
| 97 | return NULL; |
| 98 | } |
| 99 | |
| 100 | static void order_dep_list(module_info *modules, module_info *start, |
| 101 | llist_t *add) |
| 102 | { |
| 103 | module_info *m; |
| 104 | llist_t *n; |
| 105 | |
| 106 | for (n = add; n != NULL; n = n->link) { |
| 107 | m = find_module(modules, n->data); |
| 108 | if (m == NULL) |
| 109 | continue; |
| 110 | |
| 111 | /* unlink current entry */ |
| 112 | m->dnext->dprev = m->dprev; |
| 113 | m->dprev->dnext = m->dnext; |
| 114 | |
| 115 | /* and add it to tail */ |
| 116 | m->dnext = start; |
| 117 | m->dprev = start->dprev; |
| 118 | start->dprev->dnext = m; |
| 119 | start->dprev = m; |
| 120 | |
| 121 | /* recurse */ |
| 122 | order_dep_list(modules, start, m->dependencies); |
| 123 | } |
| 124 | } |
| 125 | |
Denis Vlasenko | 3526115 | 2009-01-12 22:12:22 +0000 | [diff] [blame^] | 126 | static void xfreopen_write(const char *file, FILE *f) |
| 127 | { |
| 128 | if (freopen(file, "w", f) == NULL) |
| 129 | bb_perror_msg_and_die("can't open '%s'", file); |
| 130 | } |
| 131 | |
Bernhard Reutner-Fischer | 55e547e | 2008-05-26 12:01:49 +0000 | [diff] [blame] | 132 | int depmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 133 | int depmod_main(int argc UNUSED_PARAM, char **argv) |
Bernhard Reutner-Fischer | 55e547e | 2008-05-26 12:01:49 +0000 | [diff] [blame] | 134 | { |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 135 | module_info *modules = NULL, *m, *dep; |
Denis Vlasenko | 3526115 | 2009-01-12 22:12:22 +0000 | [diff] [blame^] | 136 | const char *moddir_base = "/"; |
| 137 | char *moddir, *version; |
| 138 | struct utsname uts; |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 139 | int tmp; |
Bernhard Reutner-Fischer | 55e547e | 2008-05-26 12:01:49 +0000 | [diff] [blame] | 140 | |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 141 | getopt32(argv, "aAb:eF:n", &moddir_base, NULL); |
Bernhard Reutner-Fischer | 6bb55cf | 2008-05-26 17:04:01 +0000 | [diff] [blame] | 142 | argv += optind; |
Bernhard Reutner-Fischer | c21d9c7 | 2008-05-28 10:35:51 +0000 | [diff] [blame] | 143 | |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 144 | /* goto modules location */ |
Denis Vlasenko | 3526115 | 2009-01-12 22:12:22 +0000 | [diff] [blame^] | 145 | xchdir(moddir_base); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 146 | |
| 147 | /* If a version is provided, then that kernel version's module directory |
Bernhard Reutner-Fischer | beac1bd | 2008-06-02 13:28:47 +0000 | [diff] [blame] | 148 | * is used, rather than the current kernel version (as returned by |
| 149 | * "uname -r"). */ |
Denis Vlasenko | 3526115 | 2009-01-12 22:12:22 +0000 | [diff] [blame^] | 150 | if (*argv && sscanf(*argv, "%d.%d.%d", &tmp, &tmp, &tmp) == 3) { |
| 151 | version = *argv++; |
Bernhard Reutner-Fischer | 1ea2568 | 2008-05-26 21:33:05 +0000 | [diff] [blame] | 152 | } else { |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 153 | uname(&uts); |
Denis Vlasenko | 3526115 | 2009-01-12 22:12:22 +0000 | [diff] [blame^] | 154 | version = uts.release; |
Bernhard Reutner-Fischer | 6bb55cf | 2008-05-26 17:04:01 +0000 | [diff] [blame] | 155 | } |
Denis Vlasenko | 3526115 | 2009-01-12 22:12:22 +0000 | [diff] [blame^] | 156 | moddir = concat_path_file(&CONFIG_DEFAULT_MODULES_DIR[1], version); |
Bernhard Reutner-Fischer | 1ea2568 | 2008-05-26 21:33:05 +0000 | [diff] [blame] | 157 | |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 158 | /* Scan modules */ |
Denis Vlasenko | 3526115 | 2009-01-12 22:12:22 +0000 | [diff] [blame^] | 159 | if (*argv) { |
| 160 | char *modfile; |
| 161 | struct stat sb; |
| 162 | do { |
| 163 | modfile = concat_path_file(moddir, *argv); |
| 164 | xstat(modfile, &sb); |
| 165 | parse_module(modfile, &sb, &modules, 0); |
| 166 | free(modfile); |
| 167 | } while (*(++argv)); |
| 168 | } else { |
| 169 | recursive_action(moddir, ACTION_RECURSE, |
| 170 | parse_module, NULL, &modules, 0); |
| 171 | } |
| 172 | |
| 173 | /* Prepare for writing out the dep files */ |
| 174 | xchdir(moddir); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 175 | if (ENABLE_FEATURE_CLEAN_UP) |
Denis Vlasenko | 3526115 | 2009-01-12 22:12:22 +0000 | [diff] [blame^] | 176 | free(moddir); |
Bernhard Reutner-Fischer | 6bb55cf | 2008-05-26 17:04:01 +0000 | [diff] [blame] | 177 | |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 178 | /* Generate dependency and alias files */ |
| 179 | if (!(option_mask32 & ARG_n)) |
Denis Vlasenko | 3526115 | 2009-01-12 22:12:22 +0000 | [diff] [blame^] | 180 | xfreopen_write(CONFIG_DEFAULT_DEPMOD_FILE, stdout); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 181 | for (m = modules; m != NULL; m = m->next) { |
| 182 | printf("%s:", m->name); |
| 183 | |
| 184 | order_dep_list(modules, m, m->dependencies); |
| 185 | while (m->dnext != m) { |
| 186 | dep = m->dnext; |
| 187 | printf(" %s", dep->name); |
| 188 | |
| 189 | /* unlink current entry */ |
| 190 | dep->dnext->dprev = dep->dprev; |
| 191 | dep->dprev->dnext = dep->dnext; |
| 192 | dep->dnext = dep->dprev = dep; |
Bernhard Reutner-Fischer | 55e547e | 2008-05-26 12:01:49 +0000 | [diff] [blame] | 193 | } |
Denis Vlasenko | 3526115 | 2009-01-12 22:12:22 +0000 | [diff] [blame^] | 194 | bb_putchar('\n'); |
Bernhard Reutner-Fischer | c2741e1 | 2008-06-02 18:59:16 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 197 | #if ENABLE_FEATURE_MODUTILS_ALIAS |
| 198 | if (!(option_mask32 & ARG_n)) |
Denis Vlasenko | 3526115 | 2009-01-12 22:12:22 +0000 | [diff] [blame^] | 199 | xfreopen_write("modules.alias", stdout); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 200 | for (m = modules; m != NULL; m = m->next) { |
| 201 | while (m->aliases) { |
| 202 | printf("alias %s %s\n", |
| 203 | (char*)llist_pop(&m->aliases), |
| 204 | m->modname); |
Bernhard Reutner-Fischer | beac1bd | 2008-06-02 13:28:47 +0000 | [diff] [blame] | 205 | } |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 206 | } |
| 207 | #endif |
| 208 | #if ENABLE_FEATURE_MODUTILS_SYMBOLS |
| 209 | if (!(option_mask32 & ARG_n)) |
Denis Vlasenko | 3526115 | 2009-01-12 22:12:22 +0000 | [diff] [blame^] | 210 | xfreopen_write("modules.symbols", stdout); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 211 | for (m = modules; m != NULL; m = m->next) { |
| 212 | while (m->symbols) { |
| 213 | printf("alias symbol:%s %s\n", |
| 214 | (char*)llist_pop(&m->symbols), |
| 215 | m->modname); |
| 216 | } |
Bernhard Reutner-Fischer | beac1bd | 2008-06-02 13:28:47 +0000 | [diff] [blame] | 217 | } |
Bernhard Reutner-Fischer | c2741e1 | 2008-06-02 18:59:16 +0000 | [diff] [blame] | 218 | #endif |
Bernhard Reutner-Fischer | beac1bd | 2008-06-02 13:28:47 +0000 | [diff] [blame] | 219 | |
Bernhard Reutner-Fischer | dc5d7fe | 2008-05-26 13:30:41 +0000 | [diff] [blame] | 220 | if (ENABLE_FEATURE_CLEAN_UP) { |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 221 | while (modules) { |
| 222 | module_info *old = modules; |
| 223 | modules = modules->next; |
Bernhard Reutner-Fischer | beac1bd | 2008-06-02 13:28:47 +0000 | [diff] [blame] | 224 | free(old->name); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 225 | free(old->modname); |
Bernhard Reutner-Fischer | beac1bd | 2008-06-02 13:28:47 +0000 | [diff] [blame] | 226 | free(old); |
| 227 | } |
Bernhard Reutner-Fischer | dc5d7fe | 2008-05-26 13:30:41 +0000 | [diff] [blame] | 228 | } |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 229 | |
| 230 | return EXIT_SUCCESS; |
Bernhard Reutner-Fischer | 55e547e | 2008-05-26 12:01:49 +0000 | [diff] [blame] | 231 | } |