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.
11 //applet:IF_DEPMOD(APPLET(depmod, _BB_DIR_SBIN, _BB_SUID_DROP))
13 //usage:#if !ENABLE_MODPROBE_SMALL
14 //usage:#define depmod_trivial_usage NOUSAGE_STR
15 //usage:#define depmod_full_usage ""
20 #include <sys/utsname.h> /* uname() */
23 * Theory of operation:
24 * - iterate over all modules and record their full path
25 * - iterate over all modules looking for "depends=" entries
26 * for each depends, look through our list of full paths and emit if found
29 typedef struct module_info {
30 struct module_info *next;
32 llist_t *dependencies;
35 struct module_info *dnext, *dprev;
38 static int FAST_FUNC parse_module(const char *fname, struct stat *sb UNUSED_PARAM,
39 void *data, int depth UNUSED_PARAM)
41 char modname[MODULE_NAME_LEN];
42 module_info **first = (module_info **) data;
45 /* Arbitrary. Was sb->st_size, but that breaks .gz etc */
46 size_t len = (64*1024*1024 - 4096);
48 if (strrstr(fname, ".ko") == NULL)
51 image = xmalloc_open_zipped_read_close(fname, &len);
52 info = xzalloc(sizeof(*info));
57 info->dnext = info->dprev = info;
58 info->name = xstrdup(fname + 2); /* skip "./" */
59 info->modname = xstrdup(filename2modname(fname, modname));
60 for (ptr = image; ptr < image + len - 10; ptr++) {
61 if (strncmp(ptr, "depends=", 8) == 0) {
65 for (u = ptr; *u; u++)
68 ptr += string_to_llist(ptr, &info->dependencies, ",");
69 } else if (ENABLE_FEATURE_MODUTILS_ALIAS
70 && strncmp(ptr, "alias=", 6) == 0
72 llist_add_to(&info->aliases, xstrdup(ptr + 6));
74 } else if (ENABLE_FEATURE_MODUTILS_SYMBOLS
75 && strncmp(ptr, "__ksymtab_", 10) == 0
78 if (strncmp(ptr, "gpl", 3) == 0
79 || strcmp(ptr, "strings") == 0
83 llist_add_to(&info->symbols, xstrdup(ptr));
92 static module_info *find_module(module_info *modules, const char *modname)
96 for (m = modules; m != NULL; m = m->next)
97 if (strcmp(m->modname, modname) == 0)
102 static void order_dep_list(module_info *modules, module_info *start,
108 for (n = add; n != NULL; n = n->link) {
109 m = find_module(modules, n->data);
113 /* unlink current entry */
114 m->dnext->dprev = m->dprev;
115 m->dprev->dnext = m->dnext;
117 /* and add it to tail */
119 m->dprev = start->dprev;
120 start->dprev->dnext = m;
124 order_dep_list(modules, start, m->dependencies);
128 static void xfreopen_write(const char *file, FILE *f)
130 if (freopen(file, "w", f) == NULL)
131 bb_perror_msg_and_die("can't open '%s'", file);
135 * [-aAenv] [-C FILE or DIR] [-b BASE] [-F System.map] [VERSION] [MODFILES]...
137 * Probe all modules. Default if no MODFILES.
139 * Check modules.dep's mtime against module files' mtimes.
141 * Use $BASE/lib/modules/VERSION
142 * -C --config FILE or DIR
143 * Path to /etc/depmod.conf or /etc/depmod.d/
145 * When combined with the -F option, this reports any symbols which
146 * which are not supplied by other modules or kernel.
147 * -F --filesyms System.map
149 * Print modules.dep etc to standard output
151 * Print to stdout all the symbols each module depends on
152 * and the module's file name which provides that symbol.
155 * So far we only support: [-rn] [-b BASE] [VERSION] [MODFILES]...
156 * -aAeF are accepted but ignored. -vC are not accepted.
159 //OPT_a = (1 << 0), /* All modules, ignore mods in argv */
160 //OPT_A = (1 << 1), /* Only emit .ko that are newer than modules.dep file */
161 OPT_b = (1 << 2), /* base directory when modules are in staging area */
162 //OPT_e = (1 << 3), /* with -F, print unresolved symbols */
163 //OPT_F = (1 << 4), /* System.map that contains the symbols */
164 OPT_n = (1 << 5), /* dry-run, print to stdout only */
165 OPT_r = (1 << 6) /* Compat dummy. Linux Makefile uses it */
168 int depmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
169 int depmod_main(int argc UNUSED_PARAM, char **argv)
171 module_info *modules, *m, *dep;
172 const char *moddir_base = "/";
173 char *moddir, *version;
177 getopt32(argv, "aAb:eF:nr", &moddir_base, NULL);
180 /* goto modules location */
183 /* If a version is provided, then that kernel version's module directory
184 * is used, rather than the current kernel version (as returned by
186 if (*argv && sscanf(*argv, "%u.%u.%u", &tmp, &tmp, &tmp) == 3) {
190 version = uts.release;
192 moddir = concat_path_file(&CONFIG_DEFAULT_MODULES_DIR[1], version);
194 if (ENABLE_FEATURE_CLEAN_UP)
201 parse_module(*argv, /*sb (unused):*/ NULL, &modules, 0);
204 recursive_action(".", ACTION_RECURSE,
205 parse_module, NULL, &modules, 0);
208 /* Generate dependency and alias files */
209 if (!(option_mask32 & OPT_n))
210 xfreopen_write(CONFIG_DEFAULT_DEPMOD_FILE, stdout);
211 for (m = modules; m != NULL; m = m->next) {
212 printf("%s:", m->name);
214 order_dep_list(modules, m, m->dependencies);
215 while (m->dnext != m) {
217 printf(" %s", dep->name);
219 /* unlink current entry */
220 dep->dnext->dprev = dep->dprev;
221 dep->dprev->dnext = dep->dnext;
222 dep->dnext = dep->dprev = dep;
227 #if ENABLE_FEATURE_MODUTILS_ALIAS
228 if (!(option_mask32 & OPT_n))
229 xfreopen_write("modules.alias", stdout);
230 for (m = modules; m != NULL; m = m->next) {
231 const char *fname = bb_basename(m->name);
232 int fnlen = strchrnul(fname, '.') - fname;
234 /* Last word can well be m->modname instead,
235 * but depmod from module-init-tools 3.4
236 * uses module basename, i.e., no s/-/_/g.
237 * (pathname and .ko.* are still stripped)
238 * Mimicking that... */
239 printf("alias %s %.*s\n",
240 (char*)llist_pop(&m->aliases),
245 #if ENABLE_FEATURE_MODUTILS_SYMBOLS
246 if (!(option_mask32 & OPT_n))
247 xfreopen_write("modules.symbols", stdout);
248 for (m = modules; m != NULL; m = m->next) {
249 const char *fname = bb_basename(m->name);
250 int fnlen = strchrnul(fname, '.') - fname;
252 printf("alias symbol:%s %.*s\n",
253 (char*)llist_pop(&m->symbols),
259 if (ENABLE_FEATURE_CLEAN_UP) {
261 module_info *old = modules;
262 modules = modules->next;