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