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 #ifndef DEFAULT_MODULES_DIR
15 #define DEFAULT_MODULES_DIR "/lib/modules"
17 #ifndef DEFAULT_DEPMOD_FILE
18 #define DEFAULT_DEPMOD_FILE "modules.dep"
21 * Theory of operation:
22 * - iterate over all modules and record their full path
23 * - iterate over all modules looking for "depends=" entries
24 * for each depends, look through our list of full paths and emit if found
29 #define G (*(struct globals*)&bb_common_bufsiz1)
30 /* We have to zero it out because of NOEXEC */
31 #define INIT_G() memset(&G, 0, sizeof(G))
33 static int fill_lst(const char *modulename, struct stat ATTRIBUTE_UNUSED *sb,
34 void ATTRIBUTE_UNUSED *data, int ATTRIBUTE_UNUSED depth)
36 llist_add_to(&G.lst, xstrdup(modulename));
40 static int fileAction(const char *fname, struct stat *sb,
41 void *data, int ATTRIBUTE_UNUSED depth)
43 size_t len = sb->st_size;
44 void *the_module, *ptr;
48 /*XXX: FIXME: does not handle compressed modules!
49 * There should be a function that looks at the extension and sets up
50 * open_transformer for us.
52 if (last_char_is(fname, 'o') == NULL) /* not a module */
55 fd = xopen(fname, O_RDONLY);
56 the_module = mmap(NULL, len, PROT_READ, MAP_SHARED
57 #if defined MAP_POPULATE
62 if (the_module == MAP_FAILED)
63 bb_perror_msg_and_die("mmap");
67 fprintf((FILE*)data, "\n%s:", fname);
68 //bb_info_msg("fname='%s'", fname);
70 /* search for a 'd' */
71 ptr = memchr(ptr, 'd', len);
72 if (ptr == NULL) /* no d left, done */
74 if (memcmp(ptr, "depends=", sizeof("depends=")-1) == 0)
76 len -= ++ptr - the_module;
78 deps = depends = xstrdup (ptr + sizeof("depends=")-1);
79 //bb_info_msg(" depends='%s'", depends);
81 llist_t * _lst = G.lst;
84 ptr = strchr(deps, ',');
87 /* remember the length of the current dependency plus eventual 0 byte */
88 len = strlen(deps) + (ptr != NULL);
89 buf1 = xmalloc(len + 3);
90 sprintf(buf1, "/%s.", deps); /* make sure we match the correct file */
92 ptr = strstr(_lst->data, buf1);
98 if (_lst /*&& _lst->data*/) {
99 //bb_info_msg("[%s] -> '%s'", deps, _lst->data);
100 fprintf((FILE*)data, " %s", _lst->data);
106 munmap(the_module, sb->st_size);
111 int depmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
112 int depmod_main(int ATTRIBUTE_UNUSED argc, char **argv)
114 int retval = EXIT_SUCCESS;
115 char *moddir_base = NULL, *moddir, *system_map, *chp;
116 FILE *filedes = stdout;
118 ARG_a = (1<<0), /* All modules, ignore mods in argv */
119 ARG_A = (1<<1), /* Only emit .ko that are newer than modules.dep file */
120 ARG_b = (1<<2), /* not /lib/modules/$(uname -r)/ but this base-dir */
121 ARG_e = (1<<3), /* with -F, print unresolved symbols */
122 ARG_F = (1<<4), /* System.map that contains the symbols */
123 ARG_n = (1<<5) /* dry-run, print to stdout only */
126 getopt32(argv, "aAb:eF:n", &moddir_base, &system_map);
128 if (*argv) {/* got a version to use? */
130 // if (memchr(chp, '/', strlen(chp)) != NULL) /* XXX: drop this */
135 bb_simple_perror_msg_and_die("uname");
136 chp = xstrdup(uts.release);
138 /* if no modules are given on the command-line, -a is on per default */
139 option_mask32 |= *argv == NULL;
141 moddir = concat_path_file(DEFAULT_MODULES_DIR, chp);
142 // if (ENABLE_FEATURE_CLEAN_UP)
144 if (option_mask32 & ARG_b) {
145 char *old_moddir = moddir;
146 moddir = concat_path_file(moddir_base, moddir);
147 if (ENABLE_FEATURE_CLEAN_UP)
151 if (!(option_mask32 & ARG_n)) { /* --dry-run */
152 chp = concat_path_file(moddir, DEFAULT_DEPMOD_FILE);
153 filedes = xfopen(chp, "w");
154 if (ENABLE_FEATURE_CLEAN_UP)
157 /* have to do a full walk to collect all needed data */
158 if (!recursive_action(moddir,
159 ACTION_RECURSE, /* flags */
160 fill_lst, /* file action */
161 NULL, /* dir action */
162 NULL, /* user data */
164 retval = EXIT_FAILURE;
167 chp = concat_path_file(option_mask32 & ARG_a ? moddir : moddir_base,
168 option_mask32 & ARG_a ? "" : *argv++);
170 if (!recursive_action(chp,
171 ACTION_RECURSE, /* flags */
172 fileAction, /* file action */
173 NULL, /* dir action */
174 (void*)filedes, /* user data */
176 retval = EXIT_FAILURE;
178 if (ENABLE_FEATURE_CLEAN_UP)
180 } while (!(option_mask32 & ARG_a) && *argv);
182 if (ENABLE_FEATURE_CLEAN_UP) {
184 llist_free(G.lst, free);