1 /* vi: set sw=4 ts=4: */
3 * depmod - generate modules.dep
4 * Copyright (c) 2008 Bernhard Fischer
6 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
12 #include <sys/utsname.h> /* uname() */
14 * Theory of operation:
15 * - iterate over all modules and record their full path
16 * - iterate over all modules looking for "depends=" entries
17 * for each depends, look through our list of full paths and emit if found
20 typedef struct dep_lst_t {
22 llist_t *dependencies;
24 struct dep_lst_t *next;
28 dep_lst_t *lst; /* modules without their corresponding extension */
30 #define G (*(struct globals*)&bb_common_bufsiz1)
31 /* We have to zero it out because of NOEXEC */
32 #define INIT_G() memset(&G, 0, sizeof(G))
34 static char* find_keyword(void *the_module, size_t len, const char * const word)
36 char *ptr = the_module;
38 /* search for the first char in word */
39 ptr = memchr(ptr, *word, len - (ptr - (char*)the_module));
40 if (ptr == NULL) /* no occurance left, done */
42 if (!strncmp(ptr, word, strlen(word))) {
50 static int FAST_FUNC fileAction(const char *fname, struct stat *sb,
51 void UNUSED_PARAM *data, int UNUSED_PARAM depth)
53 size_t len = sb->st_size;
60 if (strrstr(fname, ".ko") == NULL) /* not a module */
63 /*XXX: FIXME: does not handle compressed modules!
64 * There should be a function that looks at the extension and sets up
65 * open_transformer for us.
67 fd = xopen(fname, O_RDONLY);
68 the_module = mmap(NULL, len, PROT_READ, MAP_SHARED
69 #if defined MAP_POPULATE
74 if (the_module == MAP_FAILED)
75 bb_perror_msg_and_die("mmap");
77 this = xzalloc(sizeof(dep_lst_t));
78 this->name = xstrdup(fname);
81 //bb_info_msg("fname='%s'", fname);
82 ptr = find_keyword(the_module, len, "depends=");
85 deps = depends = xstrdup(ptr);
86 //bb_info_msg(" depends='%s'", depends);
88 ptr = strsep(&deps, ",");
89 //bb_info_msg("[%s] -> '%s'", fname, (char*)ptr);
90 llist_add_to_end(&this->dependencies, xstrdup(ptr));
94 if (ENABLE_FEATURE_DEPMOD_ALIAS)
98 ptr = find_keyword(the_module + pos, len - pos, "alias=");
100 //bb_info_msg("[%s] alias '%s'", fname, (char*)ptr);
101 llist_add_to_end(&this->aliases, xstrdup(ptr));
104 pos = (ptr - (char*)the_module);
107 munmap(the_module, sb->st_size);
112 int depmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
113 int depmod_main(int argc UNUSED_PARAM, char **argv)
116 size_t moddir_base_len = 0; /* length of the "-b basedir" */
117 char *moddir_base = NULL, *moddir, *system_map, *chp;
118 FILE *filedes = stdout;
120 ARG_a = (1<<0), /* All modules, ignore mods in argv */
121 ARG_A = (1<<1), /* Only emit .ko that are newer than modules.dep file */
122 ARG_b = (1<<2), /* not /lib/modules/$(uname -r)/ but this base-dir */
123 ARG_e = (1<<3), /* with -F, print unresolved symbols */
124 ARG_F = (1<<4), /* System.map that contains the symbols */
125 ARG_n = (1<<5) /* dry-run, print to stdout only */
129 getopt32(argv, "aAb:eF:n", &moddir_base, &system_map);
132 /* If a version is provided, then that kernel version’s module directory
133 * is used, rather than the current kernel version (as returned by
135 if (*argv && (sscanf(*argv, "%d.%d.%d", &ret, &ret, &ret) == 3)) {
136 moddir = concat_path_file(CONFIG_DEFAULT_MODULES_DIR, *argv++);
140 bb_simple_perror_msg_and_die("uname");
141 moddir = concat_path_file(CONFIG_DEFAULT_MODULES_DIR, uts.release);
143 /* If no modules are given on the command-line, -a is on per default. */
144 option_mask32 |= *argv == NULL;
146 if (option_mask32 & ARG_b) {
147 moddir_base_len = strlen(moddir_base) + 1;
151 if (!(option_mask32 & ARG_n)) { /* --dry-run */
152 chp = concat_path_file(moddir, CONFIG_DEFAULT_DEPMOD_FILE);
153 filedes = xfopen(chp, "w");
154 if (ENABLE_FEATURE_CLEAN_UP)
159 chp = option_mask32 & ARG_a ? moddir : (*argv + moddir_base_len);
161 if (!recursive_action(chp,
162 ACTION_RECURSE, /* flags */
163 fileAction, /* file action */
164 NULL, /* dir action */
165 NULL, /* user data */
169 } while (!(option_mask32 & ARG_a) && *++argv);
172 dep_lst_t *mods = G.lst;
174 /* Fixup the module names in the depends list */
176 llist_t *deps = NULL, *old_deps = mods->dependencies;
179 dep_lst_t *all = G.lst;
180 char *longname = NULL;
181 char *shortname = llist_pop(&old_deps);
185 xstrdup(bb_get_last_path_component_nostrip(all->name));
186 char *tmp = strrstr(nam, ".ko");
189 if (!strcmp(nam, shortname)) {
190 if (ENABLE_FEATURE_CLEAN_UP)
192 longname = all->name;
198 llist_add_to_end(&deps, longname);
200 mods->dependencies = deps;
204 #if ENABLE_FEATURE_DEPMOD_PRUNE_FANCY
205 /* modprobe allegedly wants dependencies without duplicates, i.e.
209 * implies that mod1 directly depends on mod2 and _not_ mod3 as mod3 is
210 * already implicitely pulled in via mod2. This leaves us with:
217 llist_t *deps = mods->dependencies;
219 dep_lst_t *all = G.lst;
221 if (!strcmp(all->name, deps->data)) {
222 llist_t *implied = all->dependencies;
224 /* XXX:FIXME: erm, it would be nicer to just
225 * llist_unlink(&mods->dependencies, implied) */
226 llist_t *prune = mods->dependencies;
228 if (!strcmp(implied->data, prune->data))
232 //if (prune) bb_info_msg("[%s] '%s' implies '%s', removing", mods->name, all->name, implied->data);
233 llist_unlink(&mods->dependencies, prune);
234 implied = implied->link;
246 /* Finally print them. */
248 fprintf(filedes, "%s:", mods->name);
249 /* If we did not resolve all modules, then it's likely that we just did
250 * not see the names of all prerequisites (which will be NULL in this
252 while (mods->dependencies) {
253 char *the_dep = llist_pop(&mods->dependencies);
255 fprintf(filedes, " %s", the_dep);
257 fprintf(filedes, "\n");
258 if (ENABLE_FEATURE_DEPMOD_ALIAS)
261 xstrdup(bb_get_last_path_component_nostrip(mods->name));
262 char *tmp = strrstr(shortname, ".ko");
266 while (mods->aliases) {
267 fprintf(filedes, "alias %s %s\n",
268 (char*)llist_pop(&mods->aliases),
277 if (ENABLE_FEATURE_CLEAN_UP) {
278 fclose_if_not_stdin(filedes);
281 dep_lst_t *old = G.lst;