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(filename2modname(fname
, modname
));
55 for (ptr
= image
; ptr
< image
+ len
- 10; ptr
++) {
56 if (strncmp(ptr
, "depends=", 8) == 0) {
60 for (u
= ptr
; *u
; u
++)
63 ptr
+= string_to_llist(ptr
, &info
->dependencies
, ",");
64 } else if (ENABLE_FEATURE_MODUTILS_ALIAS
65 && strncmp(ptr
, "alias=", 6) == 0
67 llist_add_to(&info
->aliases
, xstrdup(ptr
+ 6));
69 } else if (ENABLE_FEATURE_MODUTILS_SYMBOLS
70 && strncmp(ptr
, "__ksymtab_", 10) == 0
73 if (strncmp(ptr
, "gpl", 3) == 0
74 || strcmp(ptr
, "strings") == 0
78 llist_add_to(&info
->symbols
, xstrdup(ptr
));
87 static module_info
*find_module(module_info
*modules
, const char *modname
)
91 for (m
= modules
; m
!= NULL
; m
= m
->next
)
92 if (strcmp(m
->modname
, modname
) == 0)
97 static void order_dep_list(module_info
*modules
, module_info
*start
,
103 for (n
= add
; n
!= NULL
; n
= n
->link
) {
104 m
= find_module(modules
, n
->data
);
108 /* unlink current entry */
109 m
->dnext
->dprev
= m
->dprev
;
110 m
->dprev
->dnext
= m
->dnext
;
112 /* and add it to tail */
114 m
->dprev
= start
->dprev
;
115 start
->dprev
->dnext
= m
;
119 order_dep_list(modules
, start
, m
->dependencies
);
123 static void xfreopen_write(const char *file
, FILE *f
)
125 if (freopen(file
, "w", f
) == NULL
)
126 bb_perror_msg_and_die("can't open '%s'", file
);
129 //usage:#if !ENABLE_MODPROBE_SMALL
130 //usage:#define depmod_trivial_usage "[-n] [-b BASE] [VERSION] [MODFILES]..."
131 //usage:#define depmod_full_usage "\n\n"
132 //usage: "Generate modules.dep, alias, and symbols files"
134 //usage: "\n -b BASE Use BASE/lib/modules/VERSION"
135 //usage: "\n -n Dry run: print files to stdout"
139 * [-aAenv] [-C FILE or DIR] [-b BASE] [-F System.map] [VERSION] [MODFILES]...
141 * Probe all modules. Default if no MODFILES.
143 * Check modules.dep's mtime against module files' mtimes.
145 * Use $BASE/lib/modules/VERSION
146 * -C --config FILE or DIR
147 * Path to /etc/depmod.conf or /etc/depmod.d/
149 * When combined with the -F option, this reports any symbols
150 * which are not supplied by other modules or kernel.
151 * -F --filesyms System.map
153 * Print modules.dep etc to standard output
155 * Print to stdout all the symbols each module depends on
156 * and the module's file name which provides that symbol.
161 * So far we only support: [-n] [-b BASE] [VERSION] [MODFILES]...
162 * Accepted but ignored:
170 //OPT_a = (1 << 0), /* All modules, ignore mods in argv */
171 //OPT_A = (1 << 1), /* Only emit .ko that are newer than modules.dep file */
172 OPT_b
= (1 << 2), /* base directory when modules are in staging area */
173 //OPT_e = (1 << 3), /* with -F, print unresolved symbols */
174 //OPT_F = (1 << 4), /* System.map that contains the symbols */
175 OPT_n
= (1 << 5), /* dry-run, print to stdout only */
176 OPT_r
= (1 << 6), /* Compat dummy. Linux Makefile uses it */
177 OPT_u
= (1 << 7), /* -u,--unresolved-error: ignored */
178 OPT_q
= (1 << 8), /* -q,--quiet: ignored */
179 OPT_C
= (1 << 9), /* -C,--config etc_modules_conf: ignored */
182 int depmod_main(int argc
, char **argv
) MAIN_EXTERNALLY_VISIBLE
;
183 int depmod_main(int argc UNUSED_PARAM
, char **argv
)
185 module_info
*modules
, *m
, *dep
;
186 const char *moddir_base
= "/";
187 char *moddir
, *version
;
191 getopt32(argv
, "aAb:eF:nruqC:", &moddir_base
, NULL
, NULL
);
194 /* goto modules location */
197 /* If a version is provided, then that kernel version's module directory
198 * is used, rather than the current kernel version (as returned by
200 if (*argv
&& sscanf(*argv
, "%u.%u.%u", &tmp
, &tmp
, &tmp
) == 3) {
204 version
= uts
.release
;
206 moddir
= concat_path_file(&CONFIG_DEFAULT_MODULES_DIR
[1], version
);
208 if (ENABLE_FEATURE_CLEAN_UP
)
215 parse_module(*argv
, /*sb (unused):*/ NULL
, &modules
, 0);
218 recursive_action(".", ACTION_RECURSE
,
219 parse_module
, NULL
, &modules
, 0);
222 /* Generate dependency and alias files */
223 if (!(option_mask32
& OPT_n
))
224 xfreopen_write(CONFIG_DEFAULT_DEPMOD_FILE
, stdout
);
225 for (m
= modules
; m
!= NULL
; m
= m
->next
) {
226 printf("%s:", m
->name
);
228 order_dep_list(modules
, m
, m
->dependencies
);
229 while (m
->dnext
!= m
) {
231 printf(" %s", dep
->name
);
233 /* unlink current entry */
234 dep
->dnext
->dprev
= dep
->dprev
;
235 dep
->dprev
->dnext
= dep
->dnext
;
236 dep
->dnext
= dep
->dprev
= dep
;
241 #if ENABLE_FEATURE_MODUTILS_ALIAS
242 if (!(option_mask32
& OPT_n
))
243 xfreopen_write("modules.alias", stdout
);
244 for (m
= modules
; m
!= NULL
; m
= m
->next
) {
245 const char *fname
= bb_basename(m
->name
);
246 int fnlen
= strchrnul(fname
, '.') - fname
;
248 /* Last word can well be m->modname instead,
249 * but depmod from module-init-tools 3.4
250 * uses module basename, i.e., no s/-/_/g.
251 * (pathname and .ko.* are still stripped)
252 * Mimicking that... */
253 printf("alias %s %.*s\n",
254 (char*)llist_pop(&m
->aliases
),
259 #if ENABLE_FEATURE_MODUTILS_SYMBOLS
260 if (!(option_mask32
& OPT_n
))
261 xfreopen_write("modules.symbols", stdout
);
262 for (m
= modules
; m
!= NULL
; m
= m
->next
) {
263 const char *fname
= bb_basename(m
->name
);
264 int fnlen
= strchrnul(fname
, '.') - fname
;
266 printf("alias symbol:%s %.*s\n",
267 (char*)llist_pop(&m
->symbols
),
273 if (ENABLE_FEATURE_CLEAN_UP
) {
275 module_info
*old
= modules
;
276 modules
= modules
->next
;