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() */
15 #define dbg_assert assert
17 #define dbg_assert(stuff) do {} while (0)
20 * Theory of operation:
21 * - iterate over all modules and record their full path
22 * - iterate over all modules looking for "depends=" entries
23 * for each depends, look through our list of full paths and emit if found
26 typedef struct dep_lst_t {
28 llist_t *dependencies;
29 struct dep_lst_t *next;
33 dep_lst_t *lst; /* modules without their corresponding extension */
34 size_t moddir_base_len; /* length of the "-b basedir" */
36 #define G (*(struct globals*)&bb_common_bufsiz1)
37 /* We have to zero it out because of NOEXEC */
38 #define INIT_G() memset(&G, 0, sizeof(G))
40 static int fill_lst(const char *modulename, struct stat ATTRIBUTE_UNUSED *sb,
41 void ATTRIBUTE_UNUSED *data, int ATTRIBUTE_UNUSED depth)
44 /* We get a file here. If the file does not have ".ko" but an
45 * intermittent dentry has, it's just their fault.
47 if (strrstr(modulename, ".ko") != NULL) {
48 dep_lst_t *new = xzalloc(sizeof(dep_lst_t));
49 new->name = xstrdup(modulename + G.moddir_base_len);
56 static int fileAction(const char *fname, struct stat *sb,
57 void ATTRIBUTE_UNUSED *data, int ATTRIBUTE_UNUSED depth)
59 size_t len = sb->st_size;
66 if (strrstr(fname, ".ko") == NULL) /* not a module */
69 /*XXX: FIXME: does not handle compressed modules!
70 * There should be a function that looks at the extension and sets up
71 * open_transformer for us.
73 fd = xopen(fname, O_RDONLY);
74 the_module = mmap(NULL, len, PROT_READ, MAP_SHARED
75 #if defined MAP_POPULATE
80 if (the_module == MAP_FAILED)
81 bb_perror_msg_and_die("mmap");
85 if (!strcmp(fname + G.moddir_base_len, this->name))
90 //bb_info_msg("fname='%s'", fname + G.moddir_base_len);
92 /* search for a 'd' */
93 ptr = memchr(ptr, 'd', len - (ptr - (char*)the_module));
94 if (ptr == NULL) /* no d left, done */
96 if (!strncmp(ptr, "depends=", sizeof("depends=")-1))
100 deps = depends = xstrdup (ptr + sizeof("depends=")-1);
101 //bb_info_msg(" depends='%s'", depends);
103 dep_lst_t *all = G.lst;
105 ptr = strsep(&deps, ",");
107 /* Compare the recorded filenames ignoring ".ko*" at the end. */
108 char *tmp = bb_get_last_path_component_nostrip(all->name);
109 if (!strncmp(ptr, tmp, MAX(strlen(ptr),strrstr(tmp, ".ko") - tmp)))
110 break; /* found it */
114 dbg_assert(all->name); /* this cannot be empty */
115 //bb_info_msg("[%s] -> '%s'", (char*)ptr, all->name);
116 llist_add_to_end(&this->dependencies, all->name);
121 munmap(the_module, sb->st_size);
126 int depmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
127 int depmod_main(int ATTRIBUTE_UNUSED argc, char **argv)
130 char *moddir_base = NULL, *moddir, *system_map, *chp;
131 FILE *filedes = stdout;
133 ARG_a = (1<<0), /* All modules, ignore mods in argv */
134 ARG_A = (1<<1), /* Only emit .ko that are newer than modules.dep file */
135 ARG_b = (1<<2), /* not /lib/modules/$(uname -r)/ but this base-dir */
136 ARG_e = (1<<3), /* with -F, print unresolved symbols */
137 ARG_F = (1<<4), /* System.map that contains the symbols */
138 ARG_n = (1<<5) /* dry-run, print to stdout only */
142 getopt32(argv, "aAb:eF:n", &moddir_base, &system_map);
145 /* If a version is provided, then that kernel version’s module directory
146 * is used, rather than the current kernel version (as returned by
148 if (*argv && (sscanf(*argv, "%d.%d.%d", &ret, &ret, &ret) == 3)) {
149 moddir = concat_path_file(CONFIG_DEFAULT_MODULES_DIR, *argv++);
153 bb_simple_perror_msg_and_die("uname");
154 moddir = concat_path_file(CONFIG_DEFAULT_MODULES_DIR, uts.release);
156 /* If no modules are given on the command-line, -a is on per default. */
157 option_mask32 |= *argv == NULL;
159 if (option_mask32 & ARG_b) {
160 G.moddir_base_len = strlen(moddir_base);
161 if (ENABLE_FEATURE_CLEAN_UP) {
163 moddir = concat_path_file(moddir_base, moddir);
166 moddir = concat_path_file(moddir_base, moddir);
169 if (!(option_mask32 & ARG_n)) { /* --dry-run */
170 chp = concat_path_file(moddir, CONFIG_DEFAULT_DEPMOD_FILE);
171 filedes = xfopen(chp, "w");
172 if (ENABLE_FEATURE_CLEAN_UP)
176 /* We have to do a full walk to collect all needed data. */
177 if (!recursive_action(moddir,
178 ACTION_RECURSE, /* flags */
179 fill_lst, /* file action */
180 NULL, /* dir action */
181 NULL, /* user data */
183 if (ENABLE_FEATURE_CLEAN_UP)
188 #if ENABLE_FEATURE_CLEAN_UP
192 chp = option_mask32 & ARG_a ? moddir : *argv++;
194 if (!recursive_action(chp,
195 ACTION_RECURSE, /* flags */
196 fileAction, /* file action */
197 NULL, /* dir action */
198 NULL, /* user data */
202 } while (!(option_mask32 & ARG_a) && *argv);
204 /* modprobe allegedly wants dependencies without duplicates, i.e.
208 * implies that mod1 directly depends on mod2 and _not_ mod3 as mod3 is
209 * already implicitely pulled in via mod2. This leaves us with:
215 dep_lst_t *mods = G.lst;
216 #if ENABLE_FEATURE_DEPMOD_PRUNE_FANCY
218 llist_t *deps = mods->dependencies;
220 dep_lst_t *all = G.lst;
222 if (!strcmp(all->name, deps->data)) {
223 llist_t *implied = all->dependencies;
225 /* erm, nicer would be to just
226 * llist_unlink(&mods->dependencies, implied) */
227 llist_t *prune = mods->dependencies;
229 if (!strcmp(implied->data, prune->data))
233 //if (prune) bb_info_msg("[%s] '%s' implies '%s', removing", mods->name, all->name, implied->data);
234 llist_unlink(&mods->dependencies, prune);
235 implied = implied->link;
247 /* Finally print them. */
249 fprintf(filedes, "%s:", mods->name);
250 while (mods->dependencies)
251 fprintf(filedes, " %s", (char*)llist_pop(&mods->dependencies));
252 fprintf(filedes, "\n");
257 if (ENABLE_FEATURE_CLEAN_UP) {
258 fclose_if_not_stdin(filedes);
261 dep_lst_t *old = G.lst;