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 * Theory of operation:
16 * - iterate over all modules and record their full path
17 * - iterate over all modules looking for "depends=" entries
18 * for each depends, look through our list of full paths and emit if found
22 unsigned moddir_base_len;
24 #define G (*(struct globals*)&bb_common_bufsiz1)
25 /* We have to zero it out because of NOEXEC */
26 #define INIT_G() memset(&G, 0, sizeof(G))
28 static int fill_lst(const char *modulename, struct stat ATTRIBUTE_UNUSED *sb,
29 void ATTRIBUTE_UNUSED *data, int ATTRIBUTE_UNUSED depth)
31 if (last_char_is(modulename, 'o') != NULL) /* it is a module, remember it */
32 llist_add_to(&G.lst, xstrdup(modulename + G.moddir_base_len));
36 static int fileAction(const char *fname, struct stat *sb,
37 void *data, int ATTRIBUTE_UNUSED depth)
39 size_t len = sb->st_size;
40 void *the_module, *ptr;
44 /*XXX: FIXME: does not handle compressed modules!
45 * There should be a function that looks at the extension and sets up
46 * open_transformer for us.
48 if (last_char_is(fname, 'o') == NULL) /* not a module */
51 fd = xopen(fname, O_RDONLY);
52 the_module = mmap(NULL, len, PROT_READ, MAP_SHARED
53 #if defined MAP_POPULATE
58 if (the_module == MAP_FAILED)
59 bb_perror_msg_and_die("mmap");
63 fprintf((FILE*)data, "%s:", fname + G.moddir_base_len);
64 //bb_info_msg("fname='%s'", fname + G.moddir_base_len);
66 /* search for a 'd' */
67 ptr = memchr(ptr, 'd', len - (ptr - the_module));
68 if (ptr == NULL) /* no d left, done */
70 if (memcmp(ptr, "depends=", sizeof("depends=")-1) == 0)
74 deps = depends = xstrdup (ptr + sizeof("depends=")-1);
75 //bb_info_msg(" depends='%s'", depends);
77 llist_t * _lst = G.lst;
80 ptr = strchr(deps, ',');
83 /* remember the length of the current dependency plus eventual 0 byte */
84 len = strlen(deps) + (ptr != NULL);
85 buf1 = xasprintf("/%s.", deps); /* match the correct file */
87 ptr = strstr(_lst->data, buf1);
93 if (_lst /*&& _lst->data*/) {
94 //bb_info_msg("[%s] -> '%s'", deps, _lst->data);
95 fprintf((FILE*)data, " %s", _lst->data);
100 fprintf((FILE*)data, "\n");
102 munmap(the_module, sb->st_size);
107 int depmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
108 int depmod_main(int ATTRIBUTE_UNUSED argc, char **argv)
111 char *moddir_base = NULL, *moddir, *system_map, *chp;
112 FILE *filedes = stdout;
114 ARG_a = (1<<0), /* All modules, ignore mods in argv */
115 ARG_A = (1<<1), /* Only emit .ko that are newer than modules.dep file */
116 ARG_b = (1<<2), /* not /lib/modules/$(uname -r)/ but this base-dir */
117 ARG_e = (1<<3), /* with -F, print unresolved symbols */
118 ARG_F = (1<<4), /* System.map that contains the symbols */
119 ARG_n = (1<<5) /* dry-run, print to stdout only */
122 getopt32(argv, "aAb:eF:n", &moddir_base, &system_map);
125 /* got a version to use? */
126 if (*argv && (sscanf(*argv, "%u.%u.%u", &ret, &ret, &ret) == 3)) {
127 moddir = concat_path_file(CONFIG_DEFAULT_MODULES_DIR, *argv++);
131 bb_simple_perror_msg_and_die("uname");
132 moddir = concat_path_file(CONFIG_DEFAULT_MODULES_DIR, uts.release);
134 /* if no modules are given on the command-line, -a is on per default */
135 option_mask32 |= *argv == NULL;
137 if (option_mask32 & ARG_b) {
138 G.moddir_base_len = strlen(moddir_base);
139 if (ENABLE_FEATURE_CLEAN_UP) {
141 moddir = concat_path_file(moddir_base, moddir);
144 moddir = concat_path_file(moddir_base, moddir);
147 if (!(option_mask32 & ARG_n)) { /* --dry-run */
148 chp = concat_path_file(moddir, CONFIG_DEFAULT_DEPMOD_FILE);
149 filedes = xfopen(chp, "w");
150 if (ENABLE_FEATURE_CLEAN_UP)
154 /* have to do a full walk to collect all needed data */
155 if (!recursive_action(moddir,
156 ACTION_RECURSE, /* flags */
157 fill_lst, /* file action */
158 NULL, /* dir action */
159 NULL, /* user data */
164 chp = option_mask32 & ARG_a ? moddir : *argv++;
166 if (!recursive_action(chp,
167 ACTION_RECURSE, /* flags */
168 fileAction, /* file action */
169 NULL, /* dir action */
170 (void*)filedes, /* user data */
174 } while (!(option_mask32 & ARG_a) && *argv);
176 if (ENABLE_FEATURE_CLEAN_UP) {
177 fclose_if_not_stdin(filedes);
178 llist_free(G.lst, free);