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
23 #define G (*(struct globals*)&bb_common_bufsiz1)
24 /* We have to zero it out because of NOEXEC */
25 #define INIT_G() memset(&G, 0, sizeof(G))
27 static int fill_lst(const char *modulename, struct stat ATTRIBUTE_UNUSED *sb,
28 void ATTRIBUTE_UNUSED *data, int ATTRIBUTE_UNUSED depth)
30 llist_add_to(&G.lst, xstrdup(modulename));
34 static int fileAction(const char *fname, struct stat *sb,
35 void *data, int ATTRIBUTE_UNUSED depth)
37 size_t len = sb->st_size;
38 void *the_module, *ptr;
42 /*XXX: FIXME: does not handle compressed modules!
43 * There should be a function that looks at the extension and sets up
44 * open_transformer for us.
46 if (last_char_is(fname, 'o') == NULL) /* not a module */
49 fd = xopen(fname, O_RDONLY);
50 the_module = mmap(NULL, len, PROT_READ, MAP_SHARED
51 #if defined MAP_POPULATE
56 if (the_module == MAP_FAILED)
57 bb_perror_msg_and_die("mmap");
61 fprintf((FILE*)data, "\n%s:", fname);
62 //bb_info_msg("fname='%s'", fname);
64 /* search for a 'd' */
65 ptr = memchr(ptr, 'd', len);
66 if (ptr == NULL) /* no d left, done */
68 if (memcmp(ptr, "depends=", sizeof("depends=")-1) == 0)
70 len -= ++ptr - the_module;
72 deps = depends = xstrdup (ptr + sizeof("depends=")-1);
73 //bb_info_msg(" depends='%s'", depends);
75 llist_t * _lst = G.lst;
78 ptr = strchr(deps, ',');
81 /* remember the length of the current dependency plus eventual 0 byte */
82 len = strlen(deps) + (ptr != NULL);
83 buf1 = xmalloc(len + 3);
84 sprintf(buf1, "/%s.", deps); /* make sure we match the correct file */
86 ptr = strstr(_lst->data, buf1);
92 if (_lst /*&& _lst->data*/) {
93 //bb_info_msg("[%s] -> '%s'", deps, _lst->data);
94 fprintf((FILE*)data, " %s", _lst->data);
100 munmap(the_module, sb->st_size);
105 int depmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
106 int depmod_main(int ATTRIBUTE_UNUSED argc, char **argv)
108 int retval = EXIT_SUCCESS;
109 char *moddir_base = NULL, *moddir, *system_map, *chp;
110 FILE *filedes = stdout;
112 ARG_a = (1<<0), /* All modules, ignore mods in argv */
113 ARG_A = (1<<1), /* Only emit .ko that are newer than modules.dep file */
114 ARG_b = (1<<2), /* not /lib/modules/$(uname -r)/ but this base-dir */
115 ARG_e = (1<<3), /* with -F, print unresolved symbols */
116 ARG_F = (1<<4), /* System.map that contains the symbols */
117 ARG_n = (1<<5) /* dry-run, print to stdout only */
120 getopt32(argv, "aAb:eF:n", &moddir_base, &system_map);
122 if (*argv) {/* got a version to use? */
124 // if (memchr(chp, '/', strlen(chp)) != NULL) /* XXX: drop this */
129 bb_simple_perror_msg_and_die("uname");
130 chp = xstrdup(uts.release);
132 /* if no modules are given on the command-line, -a is on per default */
133 option_mask32 |= *argv == NULL;
135 moddir = concat_path_file(CONFIG_DEFAULT_MODULES_DIR, chp);
136 // if (ENABLE_FEATURE_CLEAN_UP)
138 if (option_mask32 & ARG_b) {
139 char *old_moddir = moddir;
140 moddir = concat_path_file(moddir_base, moddir);
141 if (ENABLE_FEATURE_CLEAN_UP)
145 if (!(option_mask32 & ARG_n)) { /* --dry-run */
146 chp = concat_path_file(moddir, CONFIG_DEFAULT_DEPMOD_FILE);
147 filedes = xfopen(chp, "w");
148 if (ENABLE_FEATURE_CLEAN_UP)
151 /* have to do a full walk to collect all needed data */
152 if (!recursive_action(moddir,
153 ACTION_RECURSE, /* flags */
154 fill_lst, /* file action */
155 NULL, /* dir action */
156 NULL, /* user data */
158 retval = EXIT_FAILURE;
161 chp = concat_path_file(option_mask32 & ARG_a ? moddir : moddir_base,
162 option_mask32 & ARG_a ? "" : *argv++);
164 if (!recursive_action(chp,
165 ACTION_RECURSE, /* flags */
166 fileAction, /* file action */
167 NULL, /* dir action */
168 (void*)filedes, /* user data */
170 retval = EXIT_FAILURE;
172 if (ENABLE_FEATURE_CLEAN_UP)
174 } while (!(option_mask32 & ARG_a) && *argv);
176 if (ENABLE_FEATURE_CLEAN_UP) {
178 llist_free(G.lst, free);