1 /* vi: set sw=4 ts=4: */
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
8 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
13 #include <sys/utsname.h> /* uname() */
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
22 typedef struct module_info {
23 struct module_info *next;
25 llist_t *dependencies;
28 struct module_info *dnext, *dprev;
31 static int FAST_FUNC parse_module(const char *fname, struct stat *sb UNUSED_PARAM,
32 void *data, int depth UNUSED_PARAM)
34 char modname[MODULE_NAME_LEN];
35 module_info **first = (module_info **) data;
38 /* Arbitrary. Was sb->st_size, but that breaks .gz etc */
39 size_t len = (64*1024*1024 - 4096);
41 if (strrstr(fname, ".ko") == NULL)
44 image = xmalloc_open_zipped_read_close(fname, &len);
45 info = xzalloc(sizeof(*info));
50 info->dnext = info->dprev = info;
51 info->name = xstrdup(fname + 2); /* skip "./" */
52 info->modname = xstrdup(filename2modname(fname, modname));
53 for (ptr = image; ptr < image + len - 10; ptr++) {
54 if (strncmp(ptr, "depends=", 8) == 0) {
58 for (u = ptr; *u; u++)
61 ptr += string_to_llist(ptr, &info->dependencies, ",");
62 } else if (ENABLE_FEATURE_MODUTILS_ALIAS
63 && strncmp(ptr, "alias=", 6) == 0
65 llist_add_to(&info->aliases, xstrdup(ptr + 6));
67 } else if (ENABLE_FEATURE_MODUTILS_SYMBOLS
68 && strncmp(ptr, "__ksymtab_", 10) == 0
71 if (strncmp(ptr, "gpl", 3) == 0
72 || strcmp(ptr, "strings") == 0
76 llist_add_to(&info->symbols, xstrdup(ptr));
85 static module_info *find_module(module_info *modules, const char *modname)
89 for (m = modules; m != NULL; m = m->next)
90 if (strcmp(m->modname, modname) == 0)
95 static void order_dep_list(module_info *modules, module_info *start,
101 for (n = add; n != NULL; n = n->link) {
102 m = find_module(modules, n->data);
106 /* unlink current entry */
107 m->dnext->dprev = m->dprev;
108 m->dprev->dnext = m->dnext;
110 /* and add it to tail */
112 m->dprev = start->dprev;
113 start->dprev->dnext = m;
117 order_dep_list(modules, start, m->dependencies);
121 static void xfreopen_write(const char *file, FILE *f)
123 if (freopen(file, "w", f) == NULL)
124 bb_perror_msg_and_die("can't open '%s'", file);
128 * [-aAenv] [-C FILE or DIR] [-b BASE] [-F System.map] [VERSION] [MODFILES]...
130 * Probe all modules. Default if no MODFILES.
132 * Check modules.dep's mtime against module files' mtimes.
134 * Use $BASE/lib/modules/VERSION
135 * -C --config FILE or DIR
136 * Path to /etc/depmod.conf or /etc/depmod.d/
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
142 * Print modules.dep etc to standard output
144 * Print to stdout all the symbols each module depends on
145 * and the module's file name which provides that symbol.
148 * So far we only support: [-rn] [-b BASE] [VERSION] [MODFILES]...
149 * -aAeF are accepted but ignored. -vC are not accepted.
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 */
161 int depmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
162 int depmod_main(int argc UNUSED_PARAM, char **argv)
164 module_info *modules, *m, *dep;
165 const char *moddir_base = "/";
166 char *moddir, *version;
170 getopt32(argv, "aAb:eF:nr", &moddir_base, NULL);
173 /* goto modules location */
176 /* If a version is provided, then that kernel version's module directory
177 * is used, rather than the current kernel version (as returned by
179 if (*argv && sscanf(*argv, "%u.%u.%u", &tmp, &tmp, &tmp) == 3) {
183 version = uts.release;
185 moddir = concat_path_file(&CONFIG_DEFAULT_MODULES_DIR[1], version);
187 if (ENABLE_FEATURE_CLEAN_UP)
194 parse_module(*argv, /*sb (unused):*/ NULL, &modules, 0);
197 recursive_action(".", ACTION_RECURSE,
198 parse_module, NULL, &modules, 0);
201 /* Generate dependency and alias files */
202 if (!(option_mask32 & OPT_n))
203 xfreopen_write(CONFIG_DEFAULT_DEPMOD_FILE, stdout);
204 for (m = modules; m != NULL; m = m->next) {
205 printf("%s:", m->name);
207 order_dep_list(modules, m, m->dependencies);
208 while (m->dnext != m) {
210 printf(" %s", dep->name);
212 /* unlink current entry */
213 dep->dnext->dprev = dep->dprev;
214 dep->dprev->dnext = dep->dnext;
215 dep->dnext = dep->dprev = dep;
220 #if ENABLE_FEATURE_MODUTILS_ALIAS
221 if (!(option_mask32 & OPT_n))
222 xfreopen_write("modules.alias", stdout);
223 for (m = modules; m != NULL; m = m->next) {
224 const char *fname = bb_basename(m->name);
225 int fnlen = strchrnul(fname, '.') - fname;
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",
233 (char*)llist_pop(&m->aliases),
238 #if ENABLE_FEATURE_MODUTILS_SYMBOLS
239 if (!(option_mask32 & OPT_n))
240 xfreopen_write("modules.symbols", stdout);
241 for (m = modules; m != NULL; m = m->next) {
242 const char *fname = bb_basename(m->name);
243 int fnlen = strchrnul(fname, '.') - fname;
245 printf("alias symbol:%s %.*s\n",
246 (char*)llist_pop(&m->symbols),
252 if (ENABLE_FEATURE_CLEAN_UP) {
254 module_info *old = modules;
255 modules = modules->next;