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 |
Denys Vlasenko | 043b1e5 | 2009-09-06 12:47:55 +0200 | [diff] [blame] | 13 | #include "libbb.h" |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 14 | #include "modutils.h" |
Denys Vlasenko | 043b1e5 | 2009-09-06 12:47:55 +0200 | [diff] [blame] | 15 | #include <sys/utsname.h> /* uname() */ |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 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 */ |
Ned Ludd | 039ed43 | 2009-02-20 04:56:00 +0000 | [diff] [blame] | 39 | ARG_n = (1<<5), /* dry-run, print to stdout only */ |
Denis Vlasenko | a430b69 | 2009-02-19 01:25:42 +0000 | [diff] [blame] | 40 | ARG_r = (1<<6) /* Compat dummy. Linux Makefile uses it */ |
Bernhard Reutner-Fischer | 55e547e | 2008-05-26 12:01:49 +0000 | [diff] [blame] | 41 | }; |
Bernhard Reutner-Fischer | 55e547e | 2008-05-26 12:01:49 +0000 | [diff] [blame] | 42 | |
Denys Vlasenko | 16bda3b | 2009-05-18 13:08:04 +0200 | [diff] [blame] | 43 | static int FAST_FUNC parse_module(const char *fname, struct stat *sb UNUSED_PARAM, |
| 44 | void *data, int depth UNUSED_PARAM) |
Bernhard Reutner-Fischer | 55e547e | 2008-05-26 12:01:49 +0000 | [diff] [blame] | 45 | { |
Denys Vlasenko | 16bda3b | 2009-05-18 13:08:04 +0200 | [diff] [blame] | 46 | char modname[MODULE_NAME_LEN]; |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 47 | module_info **first = (module_info **) data; |
| 48 | char *image, *ptr; |
| 49 | module_info *info; |
Denys Vlasenko | 16bda3b | 2009-05-18 13:08:04 +0200 | [diff] [blame] | 50 | /* Arbitrary. Was sb->st_size, but that breaks .gz etc */ |
| 51 | size_t len = (64*1024*1024 - 4096); |
Bernhard Reutner-Fischer | 55e547e | 2008-05-26 12:01:49 +0000 | [diff] [blame] | 52 | |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 53 | if (strrstr(fname, ".ko") == NULL) |
| 54 | return TRUE; |
Bernhard Reutner-Fischer | 634b022 | 2008-05-28 14:20:20 +0000 | [diff] [blame] | 55 | |
Denis Vlasenko | fc66892 | 2008-11-06 02:32:31 +0000 | [diff] [blame] | 56 | image = xmalloc_open_zipped_read_close(fname, &len); |
Denys Vlasenko | 16bda3b | 2009-05-18 13:08:04 +0200 | [diff] [blame] | 57 | info = xzalloc(sizeof(*info)); |
Bernhard Reutner-Fischer | c2741e1 | 2008-06-02 18:59:16 +0000 | [diff] [blame] | 58 | |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 59 | info->next = *first; |
| 60 | *first = info; |
| 61 | |
| 62 | info->dnext = info->dprev = info; |
Denis Vlasenko | 3526115 | 2009-01-12 22:12:22 +0000 | [diff] [blame] | 63 | info->name = xasprintf("/%s", fname); |
Denys Vlasenko | 16bda3b | 2009-05-18 13:08:04 +0200 | [diff] [blame] | 64 | info->modname = xstrdup(filename2modname(fname, modname)); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 65 | for (ptr = image; ptr < image + len - 10; ptr++) { |
| 66 | if (strncmp(ptr, "depends=", 8) == 0) { |
| 67 | char *u; |
| 68 | |
| 69 | ptr += 8; |
| 70 | for (u = ptr; *u; u++) |
| 71 | if (*u == '-') |
| 72 | *u = '_'; |
| 73 | ptr += string_to_llist(ptr, &info->dependencies, ","); |
Denys Vlasenko | 16bda3b | 2009-05-18 13:08:04 +0200 | [diff] [blame] | 74 | } else if (ENABLE_FEATURE_MODUTILS_ALIAS |
| 75 | && strncmp(ptr, "alias=", 6) == 0 |
| 76 | ) { |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 77 | llist_add_to(&info->aliases, xstrdup(ptr + 6)); |
| 78 | ptr += strlen(ptr); |
Denys Vlasenko | 16bda3b | 2009-05-18 13:08:04 +0200 | [diff] [blame] | 79 | } else if (ENABLE_FEATURE_MODUTILS_SYMBOLS |
| 80 | && strncmp(ptr, "__ksymtab_", 10) == 0 |
| 81 | ) { |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 82 | ptr += 10; |
Denys Vlasenko | 6b9f163 | 2010-01-28 02:24:24 +0100 | [diff] [blame^] | 83 | if (strncmp(ptr, "gpl", 3) == 0 |
| 84 | || strcmp(ptr, "strings") == 0 |
| 85 | ) { |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 86 | continue; |
Denys Vlasenko | 6b9f163 | 2010-01-28 02:24:24 +0100 | [diff] [blame^] | 87 | } |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 88 | llist_add_to(&info->symbols, xstrdup(ptr)); |
| 89 | ptr += strlen(ptr); |
| 90 | } |
Bernhard Reutner-Fischer | 55e547e | 2008-05-26 12:01:49 +0000 | [diff] [blame] | 91 | } |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 92 | free(image); |
| 93 | |
Bernhard Reutner-Fischer | 55e547e | 2008-05-26 12:01:49 +0000 | [diff] [blame] | 94 | return TRUE; |
| 95 | } |
| 96 | |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 97 | static module_info *find_module(module_info *modules, const char *modname) |
| 98 | { |
| 99 | module_info *m; |
| 100 | |
| 101 | for (m = modules; m != NULL; m = m->next) |
| 102 | if (strcmp(m->modname, modname) == 0) |
| 103 | return m; |
| 104 | return NULL; |
| 105 | } |
| 106 | |
| 107 | static void order_dep_list(module_info *modules, module_info *start, |
| 108 | llist_t *add) |
| 109 | { |
| 110 | module_info *m; |
| 111 | llist_t *n; |
| 112 | |
| 113 | for (n = add; n != NULL; n = n->link) { |
| 114 | m = find_module(modules, n->data); |
| 115 | if (m == NULL) |
| 116 | continue; |
| 117 | |
| 118 | /* unlink current entry */ |
| 119 | m->dnext->dprev = m->dprev; |
| 120 | m->dprev->dnext = m->dnext; |
| 121 | |
| 122 | /* and add it to tail */ |
| 123 | m->dnext = start; |
| 124 | m->dprev = start->dprev; |
| 125 | start->dprev->dnext = m; |
| 126 | start->dprev = m; |
| 127 | |
| 128 | /* recurse */ |
| 129 | order_dep_list(modules, start, m->dependencies); |
| 130 | } |
| 131 | } |
| 132 | |
Denis Vlasenko | 3526115 | 2009-01-12 22:12:22 +0000 | [diff] [blame] | 133 | static void xfreopen_write(const char *file, FILE *f) |
| 134 | { |
| 135 | if (freopen(file, "w", f) == NULL) |
| 136 | bb_perror_msg_and_die("can't open '%s'", file); |
| 137 | } |
| 138 | |
Bernhard Reutner-Fischer | 55e547e | 2008-05-26 12:01:49 +0000 | [diff] [blame] | 139 | int depmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 140 | int depmod_main(int argc UNUSED_PARAM, char **argv) |
Bernhard Reutner-Fischer | 55e547e | 2008-05-26 12:01:49 +0000 | [diff] [blame] | 141 | { |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 142 | module_info *modules = NULL, *m, *dep; |
Denis Vlasenko | 3526115 | 2009-01-12 22:12:22 +0000 | [diff] [blame] | 143 | const char *moddir_base = "/"; |
| 144 | char *moddir, *version; |
| 145 | struct utsname uts; |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 146 | int tmp; |
Bernhard Reutner-Fischer | 55e547e | 2008-05-26 12:01:49 +0000 | [diff] [blame] | 147 | |
Denis Vlasenko | a430b69 | 2009-02-19 01:25:42 +0000 | [diff] [blame] | 148 | getopt32(argv, "aAb:eF:nr", &moddir_base, NULL); |
Bernhard Reutner-Fischer | 6bb55cf | 2008-05-26 17:04:01 +0000 | [diff] [blame] | 149 | argv += optind; |
Bernhard Reutner-Fischer | c21d9c7 | 2008-05-28 10:35:51 +0000 | [diff] [blame] | 150 | |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 151 | /* goto modules location */ |
Denis Vlasenko | 3526115 | 2009-01-12 22:12:22 +0000 | [diff] [blame] | 152 | xchdir(moddir_base); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 153 | |
| 154 | /* 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] | 155 | * is used, rather than the current kernel version (as returned by |
| 156 | * "uname -r"). */ |
Denis Vlasenko | 3526115 | 2009-01-12 22:12:22 +0000 | [diff] [blame] | 157 | if (*argv && sscanf(*argv, "%d.%d.%d", &tmp, &tmp, &tmp) == 3) { |
| 158 | version = *argv++; |
Bernhard Reutner-Fischer | 1ea2568 | 2008-05-26 21:33:05 +0000 | [diff] [blame] | 159 | } else { |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 160 | uname(&uts); |
Denis Vlasenko | 3526115 | 2009-01-12 22:12:22 +0000 | [diff] [blame] | 161 | version = uts.release; |
Bernhard Reutner-Fischer | 6bb55cf | 2008-05-26 17:04:01 +0000 | [diff] [blame] | 162 | } |
Denis Vlasenko | 3526115 | 2009-01-12 22:12:22 +0000 | [diff] [blame] | 163 | moddir = concat_path_file(&CONFIG_DEFAULT_MODULES_DIR[1], version); |
Bernhard Reutner-Fischer | 1ea2568 | 2008-05-26 21:33:05 +0000 | [diff] [blame] | 164 | |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 165 | /* Scan modules */ |
Denis Vlasenko | 3526115 | 2009-01-12 22:12:22 +0000 | [diff] [blame] | 166 | if (*argv) { |
| 167 | char *modfile; |
| 168 | struct stat sb; |
| 169 | do { |
| 170 | modfile = concat_path_file(moddir, *argv); |
| 171 | xstat(modfile, &sb); |
| 172 | parse_module(modfile, &sb, &modules, 0); |
| 173 | free(modfile); |
| 174 | } while (*(++argv)); |
| 175 | } else { |
| 176 | recursive_action(moddir, ACTION_RECURSE, |
| 177 | parse_module, NULL, &modules, 0); |
| 178 | } |
| 179 | |
| 180 | /* Prepare for writing out the dep files */ |
| 181 | xchdir(moddir); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 182 | if (ENABLE_FEATURE_CLEAN_UP) |
Denis Vlasenko | 3526115 | 2009-01-12 22:12:22 +0000 | [diff] [blame] | 183 | free(moddir); |
Bernhard Reutner-Fischer | 6bb55cf | 2008-05-26 17:04:01 +0000 | [diff] [blame] | 184 | |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 185 | /* Generate dependency and alias files */ |
| 186 | if (!(option_mask32 & ARG_n)) |
Denis Vlasenko | 3526115 | 2009-01-12 22:12:22 +0000 | [diff] [blame] | 187 | xfreopen_write(CONFIG_DEFAULT_DEPMOD_FILE, stdout); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 188 | for (m = modules; m != NULL; m = m->next) { |
| 189 | printf("%s:", m->name); |
| 190 | |
| 191 | order_dep_list(modules, m, m->dependencies); |
| 192 | while (m->dnext != m) { |
| 193 | dep = m->dnext; |
| 194 | printf(" %s", dep->name); |
| 195 | |
| 196 | /* unlink current entry */ |
| 197 | dep->dnext->dprev = dep->dprev; |
| 198 | dep->dprev->dnext = dep->dnext; |
| 199 | dep->dnext = dep->dprev = dep; |
Bernhard Reutner-Fischer | 55e547e | 2008-05-26 12:01:49 +0000 | [diff] [blame] | 200 | } |
Denis Vlasenko | 3526115 | 2009-01-12 22:12:22 +0000 | [diff] [blame] | 201 | bb_putchar('\n'); |
Bernhard Reutner-Fischer | c2741e1 | 2008-06-02 18:59:16 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 204 | #if ENABLE_FEATURE_MODUTILS_ALIAS |
| 205 | if (!(option_mask32 & ARG_n)) |
Denis Vlasenko | 3526115 | 2009-01-12 22:12:22 +0000 | [diff] [blame] | 206 | xfreopen_write("modules.alias", stdout); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 207 | for (m = modules; m != NULL; m = m->next) { |
Denys Vlasenko | 16bda3b | 2009-05-18 13:08:04 +0200 | [diff] [blame] | 208 | const char *fname = bb_basename(m->name); |
| 209 | int fnlen = strchrnul(fname, '.') - fname; |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 210 | while (m->aliases) { |
Denys Vlasenko | 16bda3b | 2009-05-18 13:08:04 +0200 | [diff] [blame] | 211 | /* Last word can well be m->modname instead, |
| 212 | * but depmod from module-init-tools 3.4 |
| 213 | * uses module basename, i.e., no s/-/_/g. |
| 214 | * (pathname and .ko.* are still stripped) |
| 215 | * Mimicking that... */ |
| 216 | printf("alias %s %.*s\n", |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 217 | (char*)llist_pop(&m->aliases), |
Denys Vlasenko | 16bda3b | 2009-05-18 13:08:04 +0200 | [diff] [blame] | 218 | fnlen, fname); |
Bernhard Reutner-Fischer | beac1bd | 2008-06-02 13:28:47 +0000 | [diff] [blame] | 219 | } |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 220 | } |
| 221 | #endif |
| 222 | #if ENABLE_FEATURE_MODUTILS_SYMBOLS |
| 223 | if (!(option_mask32 & ARG_n)) |
Denis Vlasenko | 3526115 | 2009-01-12 22:12:22 +0000 | [diff] [blame] | 224 | xfreopen_write("modules.symbols", stdout); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 225 | for (m = modules; m != NULL; m = m->next) { |
Denys Vlasenko | 16bda3b | 2009-05-18 13:08:04 +0200 | [diff] [blame] | 226 | const char *fname = bb_basename(m->name); |
| 227 | int fnlen = strchrnul(fname, '.') - fname; |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 228 | while (m->symbols) { |
Denys Vlasenko | 16bda3b | 2009-05-18 13:08:04 +0200 | [diff] [blame] | 229 | printf("alias symbol:%s %.*s\n", |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 230 | (char*)llist_pop(&m->symbols), |
Denys Vlasenko | 16bda3b | 2009-05-18 13:08:04 +0200 | [diff] [blame] | 231 | fnlen, fname); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 232 | } |
Bernhard Reutner-Fischer | beac1bd | 2008-06-02 13:28:47 +0000 | [diff] [blame] | 233 | } |
Bernhard Reutner-Fischer | c2741e1 | 2008-06-02 18:59:16 +0000 | [diff] [blame] | 234 | #endif |
Bernhard Reutner-Fischer | beac1bd | 2008-06-02 13:28:47 +0000 | [diff] [blame] | 235 | |
Bernhard Reutner-Fischer | dc5d7fe | 2008-05-26 13:30:41 +0000 | [diff] [blame] | 236 | if (ENABLE_FEATURE_CLEAN_UP) { |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 237 | while (modules) { |
| 238 | module_info *old = modules; |
| 239 | modules = modules->next; |
Bernhard Reutner-Fischer | beac1bd | 2008-06-02 13:28:47 +0000 | [diff] [blame] | 240 | free(old->name); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 241 | free(old->modname); |
Bernhard Reutner-Fischer | beac1bd | 2008-06-02 13:28:47 +0000 | [diff] [blame] | 242 | free(old); |
| 243 | } |
Bernhard Reutner-Fischer | dc5d7fe | 2008-05-26 13:30:41 +0000 | [diff] [blame] | 244 | } |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 245 | |
| 246 | return EXIT_SUCCESS; |
Bernhard Reutner-Fischer | 55e547e | 2008-05-26 12:01:49 +0000 | [diff] [blame] | 247 | } |