busybox: update to 1.23.2
[tomato.git] / release / src / router / busybox / modutils / depmod.c
bloba41b3e44097a27a6d88902577d0ebb35c8c0c3b2
1 /* vi: set sw=4 ts=4: */
2 /*
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.
9 */
11 //applet:IF_DEPMOD(APPLET(depmod, BB_DIR_SBIN, BB_SUID_DROP))
13 #include "libbb.h"
14 #include "modutils.h"
15 #include <sys/utsname.h> /* uname() */
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
24 typedef struct module_info {
25 struct module_info *next;
26 char *name, *modname;
27 llist_t *dependencies;
28 llist_t *aliases;
29 llist_t *symbols;
30 struct module_info *dnext, *dprev;
31 } module_info;
33 static int FAST_FUNC parse_module(const char *fname, struct stat *sb UNUSED_PARAM,
34 void *data, int depth UNUSED_PARAM)
36 char modname[MODULE_NAME_LEN];
37 module_info **first = (module_info **) data;
38 char *image, *ptr;
39 module_info *info;
40 /* Arbitrary. Was sb->st_size, but that breaks .gz etc */
41 size_t len = (64*1024*1024 - 4096);
43 if (strrstr(fname, ".ko") == NULL)
44 return TRUE;
46 image = xmalloc_open_zipped_read_close(fname, &len);
47 info = xzalloc(sizeof(*info));
49 info->next = *first;
50 *first = info;
52 info->dnext = info->dprev = info;
53 info->name = xstrdup(fname + 2); /* skip "./" */
54 info->modname = xstrdup(
55 filename2modname(
56 bb_get_last_path_component_nostrip(fname),
57 modname
58 ));
59 for (ptr = image; ptr < image + len - 10; ptr++) {
60 if (strncmp(ptr, "depends=", 8) == 0) {
61 char *u;
63 ptr += 8;
64 for (u = ptr; *u; u++)
65 if (*u == '-')
66 *u = '_';
67 ptr += string_to_llist(ptr, &info->dependencies, ",");
68 } else if (ENABLE_FEATURE_MODUTILS_ALIAS
69 && strncmp(ptr, "alias=", 6) == 0
70 ) {
71 llist_add_to(&info->aliases, xstrdup(ptr + 6));
72 ptr += strlen(ptr);
73 } else if (ENABLE_FEATURE_MODUTILS_SYMBOLS
74 && strncmp(ptr, "__ksymtab_", 10) == 0
75 ) {
76 ptr += 10;
77 if (strncmp(ptr, "gpl", 3) == 0
78 || strcmp(ptr, "strings") == 0
79 ) {
80 continue;
82 llist_add_to(&info->symbols, xstrdup(ptr));
83 ptr += strlen(ptr);
86 free(image);
88 return TRUE;
91 static module_info *find_module(module_info *modules, const char *modname)
93 module_info *m;
95 for (m = modules; m != NULL; m = m->next)
96 if (strcmp(m->modname, modname) == 0)
97 return m;
98 return NULL;
101 static void order_dep_list(module_info *modules, module_info *start,
102 llist_t *add)
104 module_info *m;
105 llist_t *n;
107 for (n = add; n != NULL; n = n->link) {
108 m = find_module(modules, n->data);
109 if (m == NULL)
110 continue;
112 /* unlink current entry */
113 m->dnext->dprev = m->dprev;
114 m->dprev->dnext = m->dnext;
116 /* and add it to tail */
117 m->dnext = start;
118 m->dprev = start->dprev;
119 start->dprev->dnext = m;
120 start->dprev = m;
122 /* recurse */
123 order_dep_list(modules, start, m->dependencies);
127 static void xfreopen_write(const char *file, FILE *f)
129 if (freopen(file, "w", f) == NULL)
130 bb_perror_msg_and_die("can't open '%s'", file);
133 //usage:#if !ENABLE_MODPROBE_SMALL
134 //usage:#define depmod_trivial_usage "[-n] [-b BASE] [VERSION] [MODFILES]..."
135 //usage:#define depmod_full_usage "\n\n"
136 //usage: "Generate modules.dep, alias, and symbols files"
137 //usage: "\n"
138 //usage: "\n -b BASE Use BASE/lib/modules/VERSION"
139 //usage: "\n -n Dry run: print files to stdout"
140 //usage:#endif
142 /* Upstream usage:
143 * [-aAenv] [-C FILE or DIR] [-b BASE] [-F System.map] [VERSION] [MODFILES]...
144 * -a --all
145 * Probe all modules. Default if no MODFILES.
146 * -A --quick
147 * Check modules.dep's mtime against module files' mtimes.
148 * -b --basedir BASE
149 * Use $BASE/lib/modules/VERSION
150 * -C --config FILE or DIR
151 * Path to /etc/depmod.conf or /etc/depmod.d/
152 * -e --errsyms
153 * When combined with the -F option, this reports any symbols
154 * which are not supplied by other modules or kernel.
155 * -F --filesyms System.map
156 * -n --dry-run
157 * Print modules.dep etc to standard output
158 * -v --verbose
159 * Print to stdout all the symbols each module depends on
160 * and the module's file name which provides that symbol.
161 * -r No-op
162 * -u No-op
163 * -q No-op
165 * So far we only support: [-n] [-b BASE] [VERSION] [MODFILES]...
166 * Accepted but ignored:
167 * -aAe
168 * -F System.map
169 * -C FILE/DIR
171 * Not accepted: -v
173 enum {
174 //OPT_a = (1 << 0), /* All modules, ignore mods in argv */
175 //OPT_A = (1 << 1), /* Only emit .ko that are newer than modules.dep file */
176 OPT_b = (1 << 2), /* base directory when modules are in staging area */
177 //OPT_e = (1 << 3), /* with -F, print unresolved symbols */
178 //OPT_F = (1 << 4), /* System.map that contains the symbols */
179 OPT_n = (1 << 5), /* dry-run, print to stdout only */
180 OPT_r = (1 << 6), /* Compat dummy. Linux Makefile uses it */
181 OPT_u = (1 << 7), /* -u,--unresolved-error: ignored */
182 OPT_q = (1 << 8), /* -q,--quiet: ignored */
183 OPT_C = (1 << 9), /* -C,--config etc_modules_conf: ignored */
186 int depmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
187 int depmod_main(int argc UNUSED_PARAM, char **argv)
189 module_info *modules, *m, *dep;
190 const char *moddir_base = "/";
191 char *moddir, *version;
192 struct utsname uts;
193 int tmp;
195 getopt32(argv, "aAb:eF:nruqC:", &moddir_base, NULL, NULL);
196 argv += optind;
198 /* goto modules location */
199 xchdir(moddir_base);
201 /* If a version is provided, then that kernel version's module directory
202 * is used, rather than the current kernel version (as returned by
203 * "uname -r"). */
204 if (*argv && sscanf(*argv, "%u.%u.%u", &tmp, &tmp, &tmp) == 3) {
205 version = *argv++;
206 } else {
207 uname(&uts);
208 version = uts.release;
210 moddir = concat_path_file(&CONFIG_DEFAULT_MODULES_DIR[1], version);
211 xchdir(moddir);
212 if (ENABLE_FEATURE_CLEAN_UP)
213 free(moddir);
215 /* Scan modules */
216 modules = NULL;
217 if (*argv) {
218 do {
219 parse_module(*argv, /*sb (unused):*/ NULL, &modules, 0);
220 } while (*++argv);
221 } else {
222 recursive_action(".", ACTION_RECURSE,
223 parse_module, NULL, &modules, 0);
226 /* Generate dependency and alias files */
227 if (!(option_mask32 & OPT_n))
228 xfreopen_write(CONFIG_DEFAULT_DEPMOD_FILE, stdout);
229 for (m = modules; m != NULL; m = m->next) {
230 printf("%s:", m->name);
232 order_dep_list(modules, m, m->dependencies);
233 while (m->dnext != m) {
234 dep = m->dnext;
235 printf(" %s", dep->name);
237 /* unlink current entry */
238 dep->dnext->dprev = dep->dprev;
239 dep->dprev->dnext = dep->dnext;
240 dep->dnext = dep->dprev = dep;
242 bb_putchar('\n');
245 #if ENABLE_FEATURE_MODUTILS_ALIAS
246 if (!(option_mask32 & OPT_n))
247 xfreopen_write("modules.alias", stdout);
248 for (m = modules; m != NULL; m = m->next) {
249 char modname[MODULE_NAME_LEN];
250 const char *fname = bb_basename(m->name);
251 filename2modname(fname, modname);
252 while (m->aliases) {
253 /* Last word can well be m->modname instead,
254 * but depmod from module-init-tools 3.4
255 * uses module basename, i.e., no s/-/_/g.
256 * (pathname and .ko.* are still stripped)
257 * Mimicking that... */
258 printf("alias %s %s\n",
259 (char*)llist_pop(&m->aliases),
260 modname);
263 #endif
264 #if ENABLE_FEATURE_MODUTILS_SYMBOLS
265 if (!(option_mask32 & OPT_n))
266 xfreopen_write("modules.symbols", stdout);
267 for (m = modules; m != NULL; m = m->next) {
268 char modname[MODULE_NAME_LEN];
269 const char *fname = bb_basename(m->name);
270 filename2modname(fname, modname);
271 while (m->symbols) {
272 printf("alias symbol:%s %s\n",
273 (char*)llist_pop(&m->symbols),
274 modname);
277 #endif
279 if (ENABLE_FEATURE_CLEAN_UP) {
280 while (modules) {
281 module_info *old = modules;
282 modules = modules->next;
283 free(old->name);
284 free(old->modname);
285 free(old);
289 return EXIT_SUCCESS;