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))
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
;
27 llist_t
*dependencies
;
30 struct module_info
*dnext
, *dprev
;
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
;
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
)
46 image
= xmalloc_open_zipped_read_close(fname
, &len
);
47 info
= xzalloc(sizeof(*info
));
52 info
->dnext
= info
->dprev
= info
;
53 info
->name
= xstrdup(fname
+ 2); /* skip "./" */
54 info
->modname
= xstrdup(
56 bb_get_last_path_component_nostrip(fname
),
59 for (ptr
= image
; ptr
< image
+ len
- 10; ptr
++) {
60 if (strncmp(ptr
, "depends=", 8) == 0) {
64 for (u
= ptr
; *u
; u
++)
67 ptr
+= string_to_llist(ptr
, &info
->dependencies
, ",");
68 } else if (ENABLE_FEATURE_MODUTILS_ALIAS
69 && strncmp(ptr
, "alias=", 6) == 0
71 llist_add_to(&info
->aliases
, xstrdup(ptr
+ 6));
73 } else if (ENABLE_FEATURE_MODUTILS_SYMBOLS
74 && strncmp(ptr
, "__ksymtab_", 10) == 0
77 if (strncmp(ptr
, "gpl", 3) == 0
78 || strcmp(ptr
, "strings") == 0
82 llist_add_to(&info
->symbols
, xstrdup(ptr
));
91 static module_info
*find_module(module_info
*modules
, const char *modname
)
95 for (m
= modules
; m
!= NULL
; m
= m
->next
)
96 if (strcmp(m
->modname
, modname
) == 0)
101 static void order_dep_list(module_info
*modules
, module_info
*start
,
107 for (n
= add
; n
!= NULL
; n
= n
->link
) {
108 m
= find_module(modules
, n
->data
);
112 /* unlink current entry */
113 m
->dnext
->dprev
= m
->dprev
;
114 m
->dprev
->dnext
= m
->dnext
;
116 /* and add it to tail */
118 m
->dprev
= start
->dprev
;
119 start
->dprev
->dnext
= m
;
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"
138 //usage: "\n -b BASE Use BASE/lib/modules/VERSION"
139 //usage: "\n -n Dry run: print files to stdout"
143 * [-aAenv] [-C FILE or DIR] [-b BASE] [-F System.map] [VERSION] [MODFILES]...
145 * Probe all modules. Default if no MODFILES.
147 * Check modules.dep's mtime against module files' mtimes.
149 * Use $BASE/lib/modules/VERSION
150 * -C --config FILE or DIR
151 * Path to /etc/depmod.conf or /etc/depmod.d/
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
157 * Print modules.dep etc to standard output
159 * Print to stdout all the symbols each module depends on
160 * and the module's file name which provides that symbol.
165 * So far we only support: [-n] [-b BASE] [VERSION] [MODFILES]...
166 * Accepted but ignored:
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
;
195 getopt32(argv
, "aAb:eF:nruqC:", &moddir_base
, NULL
, NULL
);
198 /* goto modules location */
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
204 if (*argv
&& sscanf(*argv
, "%u.%u.%u", &tmp
, &tmp
, &tmp
) == 3) {
208 version
= uts
.release
;
210 moddir
= concat_path_file(&CONFIG_DEFAULT_MODULES_DIR
[1], version
);
212 if (ENABLE_FEATURE_CLEAN_UP
)
219 parse_module(*argv
, /*sb (unused):*/ NULL
, &modules
, 0);
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
) {
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
;
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
);
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
),
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
);
272 printf("alias symbol:%s %s\n",
273 (char*)llist_pop(&m
->symbols
),
279 if (ENABLE_FEATURE_CLEAN_UP
) {
281 module_info
*old
= modules
;
282 modules
= modules
->next
;