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