blob: b9347027e779fc7333030766ed94230a1262d456
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 |
7 | * |
8 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
9 | */ |
10 | //config:config DEPMOD |
11 | //config: bool "depmod" |
12 | //config: default n |
13 | //config: depends on !MODPROBE_SMALL |
14 | //config: select PLATFORM_LINUX |
15 | //config: help |
16 | //config: depmod generates modules.dep (and potentially modules.alias |
17 | //config: and modules.symbols) that contain dependency information |
18 | //config: for modprobe. |
19 | |
20 | //applet:IF_DEPMOD(APPLET(depmod, BB_DIR_SBIN, BB_SUID_DROP)) |
21 | |
22 | //kbuild:lib-$(CONFIG_DEPMOD) += depmod.o modutils.o |
23 | |
24 | #include "libbb.h" |
25 | #include "modutils.h" |
26 | #include <sys/utsname.h> /* uname() */ |
27 | |
28 | /* |
29 | * Theory of operation: |
30 | * - iterate over all modules and record their full path |
31 | * - iterate over all modules looking for "depends=" entries |
32 | * for each depends, look through our list of full paths and emit if found |
33 | */ |
34 | |
35 | static int FAST_FUNC parse_module(const char *fname, struct stat *sb UNUSED_PARAM, |
36 | void *data, int depth UNUSED_PARAM) |
37 | { |
38 | module_db *modules = data; |
39 | char *image, *ptr; |
40 | module_entry *e; |
41 | |
42 | /* Arbitrary. Was sb->st_size, but that breaks .gz etc */ |
43 | size_t len = (64*1024*1024 - 4096); |
44 | |
45 | if (strrstr(fname, ".ko") == NULL) |
46 | return TRUE; |
47 | |
48 | image = xmalloc_open_zipped_read_close(fname, &len); |
49 | |
50 | e = moddb_get_or_create(modules, bb_get_last_path_component_nostrip(fname)); |
51 | e->name = xstrdup(fname + 2); /* skip "./" */ |
52 | |
53 | for (ptr = image; ptr < image + len - 10; ptr++) { |
54 | if (is_prefixed_with(ptr, "depends=")) { |
55 | char *u; |
56 | |
57 | ptr += 8; |
58 | for (u = ptr; *u; u++) |
59 | if (*u == '-') |
60 | *u = '_'; |
61 | ptr += string_to_llist(ptr, &e->deps, ","); |
62 | } else if (ENABLE_FEATURE_MODUTILS_ALIAS |
63 | && is_prefixed_with(ptr, "alias=") |
64 | ) { |
65 | llist_add_to(&e->aliases, xstrdup(ptr + 6)); |
66 | ptr += strlen(ptr); |
67 | } else if (ENABLE_FEATURE_MODUTILS_SYMBOLS |
68 | && is_prefixed_with(ptr, "__ksymtab_") |
69 | ) { |
70 | ptr += 10; |
71 | if (is_prefixed_with(ptr, "gpl") |
72 | || strcmp(ptr, "strings") == 0 |
73 | ) { |
74 | continue; |
75 | } |
76 | llist_add_to(&e->symbols, xstrdup(ptr)); |
77 | ptr += strlen(ptr); |
78 | } |
79 | } |
80 | free(image); |
81 | |
82 | return TRUE; |
83 | } |
84 | |
85 | static void order_dep_list(module_db *modules, module_entry *start, llist_t *add) |
86 | { |
87 | module_entry *m; |
88 | llist_t *n; |
89 | |
90 | for (n = add; n != NULL; n = n->link) { |
91 | m = moddb_get(modules, n->data); |
92 | if (m == NULL) |
93 | continue; |
94 | |
95 | /* unlink current entry */ |
96 | m->dnext->dprev = m->dprev; |
97 | m->dprev->dnext = m->dnext; |
98 | |
99 | /* and add it to tail */ |
100 | m->dnext = start; |
101 | m->dprev = start->dprev; |
102 | start->dprev->dnext = m; |
103 | start->dprev = m; |
104 | |
105 | /* recurse */ |
106 | order_dep_list(modules, start, m->deps); |
107 | } |
108 | } |
109 | |
110 | static void xfreopen_write(const char *file, FILE *f) |
111 | { |
112 | if (freopen(file, "w", f) == NULL) |
113 | bb_perror_msg_and_die("can't open '%s'", file); |
114 | } |
115 | |
116 | //usage:#if !ENABLE_MODPROBE_SMALL |
117 | //usage:#define depmod_trivial_usage "[-n] [-b BASE] [VERSION] [MODFILES]..." |
118 | //usage:#define depmod_full_usage "\n\n" |
119 | //usage: "Generate modules.dep, alias, and symbols files" |
120 | //usage: "\n" |
121 | //usage: "\n -b BASE Use BASE/lib/modules/VERSION" |
122 | //usage: "\n -n Dry run: print files to stdout" |
123 | //usage:#endif |
124 | |
125 | /* Upstream usage: |
126 | * [-aAenv] [-C FILE or DIR] [-b BASE] [-F System.map] [VERSION] [MODFILES]... |
127 | * -a --all |
128 | * Probe all modules. Default if no MODFILES. |
129 | * -A --quick |
130 | * Check modules.dep's mtime against module files' mtimes. |
131 | * -b --basedir BASE |
132 | * Use $BASE/lib/modules/VERSION |
133 | * -C --config FILE or DIR |
134 | * Path to /etc/depmod.conf or /etc/depmod.d/ |
135 | * -e --errsyms |
136 | * When combined with the -F option, this reports any symbols |
137 | * which are not supplied by other modules or kernel. |
138 | * -F --filesyms System.map |
139 | * -n --dry-run |
140 | * Print modules.dep etc to standard output |
141 | * -v --verbose |
142 | * Print to stdout all the symbols each module depends on |
143 | * and the module's file name which provides that symbol. |
144 | * -r No-op |
145 | * -u No-op |
146 | * -q No-op |
147 | * |
148 | * So far we only support: [-n] [-b BASE] [VERSION] [MODFILES]... |
149 | * Accepted but ignored: |
150 | * -aAe |
151 | * -F System.map |
152 | * -C FILE/DIR |
153 | * |
154 | * Not accepted: -v |
155 | */ |
156 | enum { |
157 | //OPT_a = (1 << 0), /* All modules, ignore mods in argv */ |
158 | //OPT_A = (1 << 1), /* Only emit .ko that are newer than modules.dep file */ |
159 | OPT_b = (1 << 2), /* base directory when modules are in staging area */ |
160 | //OPT_e = (1 << 3), /* with -F, print unresolved symbols */ |
161 | //OPT_F = (1 << 4), /* System.map that contains the symbols */ |
162 | OPT_n = (1 << 5), /* dry-run, print to stdout only */ |
163 | OPT_r = (1 << 6), /* Compat dummy. Linux Makefile uses it */ |
164 | OPT_u = (1 << 7), /* -u,--unresolved-error: ignored */ |
165 | OPT_q = (1 << 8), /* -q,--quiet: ignored */ |
166 | OPT_C = (1 << 9), /* -C,--config etc_modules_conf: ignored */ |
167 | }; |
168 | |
169 | int depmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
170 | int depmod_main(int argc UNUSED_PARAM, char **argv) |
171 | { |
172 | module_db modules; |
173 | module_entry *m, *dep; |
174 | const char *moddir_base = "/"; |
175 | char *moddir, *version; |
176 | struct utsname uts; |
177 | unsigned i; |
178 | int tmp; |
179 | |
180 | getopt32(argv, "aAb:eF:nruqC:", &moddir_base, NULL, NULL); |
181 | argv += optind; |
182 | |
183 | /* goto modules location */ |
184 | xchdir(moddir_base); |
185 | |
186 | /* If a version is provided, then that kernel version's module directory |
187 | * is used, rather than the current kernel version (as returned by |
188 | * "uname -r"). */ |
189 | if (*argv && sscanf(*argv, "%u.%u.%u", &tmp, &tmp, &tmp) == 3) { |
190 | version = *argv++; |
191 | } else { |
192 | uname(&uts); |
193 | version = uts.release; |
194 | } |
195 | moddir = concat_path_file(&CONFIG_DEFAULT_MODULES_DIR[1], version); |
196 | xchdir(moddir); |
197 | if (ENABLE_FEATURE_CLEAN_UP) |
198 | free(moddir); |
199 | |
200 | /* Scan modules */ |
201 | memset(&modules, 0, sizeof(modules)); |
202 | if (*argv) { |
203 | do { |
204 | parse_module(*argv, /*sb (unused):*/ NULL, &modules, 0); |
205 | } while (*++argv); |
206 | } else { |
207 | recursive_action(".", ACTION_RECURSE, |
208 | parse_module, NULL, &modules, 0); |
209 | } |
210 | |
211 | /* Generate dependency and alias files */ |
212 | if (!(option_mask32 & OPT_n)) |
213 | xfreopen_write(CONFIG_DEFAULT_DEPMOD_FILE, stdout); |
214 | |
215 | moddb_foreach_module(&modules, m, i) { |
216 | printf("%s:", m->name); |
217 | |
218 | order_dep_list(&modules, m, m->deps); |
219 | while (m->dnext != m) { |
220 | dep = m->dnext; |
221 | printf(" %s", dep->name); |
222 | |
223 | /* unlink current entry */ |
224 | dep->dnext->dprev = dep->dprev; |
225 | dep->dprev->dnext = dep->dnext; |
226 | dep->dnext = dep->dprev = dep; |
227 | } |
228 | bb_putchar('\n'); |
229 | } |
230 | |
231 | #if ENABLE_FEATURE_MODUTILS_ALIAS |
232 | if (!(option_mask32 & OPT_n)) |
233 | xfreopen_write("modules.alias", stdout); |
234 | moddb_foreach_module(&modules, m, i) { |
235 | while (m->aliases) { |
236 | /* |
237 | * Last word used to be a basename |
238 | * (filename with path and .ko.* stripped) |
239 | * at the time of module-init-tools 3.4. |
240 | * kmod v.12 uses module name, i.e., s/-/_/g. |
241 | */ |
242 | printf("alias %s %s\n", |
243 | (char*)llist_pop(&m->aliases), |
244 | m->modname); |
245 | } |
246 | } |
247 | #endif |
248 | #if ENABLE_FEATURE_MODUTILS_SYMBOLS |
249 | if (!(option_mask32 & OPT_n)) |
250 | xfreopen_write("modules.symbols", stdout); |
251 | moddb_foreach_module(&modules, m, i) { |
252 | while (m->symbols) { |
253 | printf("alias symbol:%s %s\n", |
254 | (char*)llist_pop(&m->symbols), |
255 | m->modname); |
256 | } |
257 | } |
258 | #endif |
259 | |
260 | if (ENABLE_FEATURE_CLEAN_UP) |
261 | moddb_free(&modules); |
262 | |
263 | return EXIT_SUCCESS; |
264 | } |
265 |