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
21 llist_t *lst; /* modules without their corresponding extension */
22 size_t moddir_base_len; /* length of the "-b basedir" */
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 /* We get a file here. If the file does not have ".ko" but an
32 * intermittent dentry has, it's just their fault.
34 if (strrstr(modulename, ".ko") != NULL)
35 llist_add_to(&G.lst, xstrdup(modulename + G.moddir_base_len));
39 static int fileAction(const char *fname, struct stat *sb,
40 void *data, int ATTRIBUTE_UNUSED depth)
42 size_t len = sb->st_size;
48 if (strrstr(fname, ".ko") == NULL) /* not a module */
51 /*XXX: FIXME: does not handle compressed modules!
52 * There should be a function that looks at the extension and sets up
53 * open_transformer for us.
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, "%s:", fname + G.moddir_base_len);
68 //bb_info_msg("fname='%s'", fname + G.moddir_base_len);
70 /* search for a 'd' */
71 ptr = memchr(ptr, 'd', len - (ptr - (char*)the_module));
72 if (ptr == NULL) /* no d left, done */
74 if (strncmp(ptr, "depends=", sizeof("depends=")-1) == 0)
78 deps = depends = xstrdup (ptr + sizeof("depends=")-1);
79 //bb_info_msg(" depends='%s'", depends);
81 llist_t * _lst = G.lst;
83 ptr = strsep(&deps, ",");
85 /* Compare the recorded filenames ignoring ".ko*" at the end. */
86 char *tmp = bb_get_last_path_component_nostrip(_lst->data);
87 if (strncmp(ptr, tmp, strrstr(tmp, ".ko") - tmp) == 0)
92 //bb_info_msg("[%s] -> '%s'", (char*)ptr, _lst->data);
93 fprintf((FILE*)data, " %s", _lst->data);
97 fprintf((FILE*)data, "\n");
99 munmap(the_module, sb->st_size);
104 int depmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
105 int depmod_main(int ATTRIBUTE_UNUSED argc, char **argv)
108 char *moddir_base = NULL, *moddir, *system_map, *chp;
109 FILE *filedes = stdout;
111 ARG_a = (1<<0), /* All modules, ignore mods in argv */
112 ARG_A = (1<<1), /* Only emit .ko that are newer than modules.dep file */
113 ARG_b = (1<<2), /* not /lib/modules/$(uname -r)/ but this base-dir */
114 ARG_e = (1<<3), /* with -F, print unresolved symbols */
115 ARG_F = (1<<4), /* System.map that contains the symbols */
116 ARG_n = (1<<5) /* dry-run, print to stdout only */
119 getopt32(argv, "aAb:eF:n", &moddir_base, &system_map);
122 /* got a version to use? */
123 if (*argv && (sscanf(*argv, "%d.%d.%d", &ret, &ret, &ret) == 3)) {
124 moddir = concat_path_file(CONFIG_DEFAULT_MODULES_DIR, *argv++);
128 bb_simple_perror_msg_and_die("uname");
129 moddir = concat_path_file(CONFIG_DEFAULT_MODULES_DIR, uts.release);
131 /* If no modules are given on the command-line, -a is on per default. */
132 option_mask32 |= *argv == NULL;
134 if (option_mask32 & ARG_b) {
135 G.moddir_base_len = strlen(moddir_base);
136 if (ENABLE_FEATURE_CLEAN_UP) {
138 moddir = concat_path_file(moddir_base, moddir);
141 moddir = concat_path_file(moddir_base, moddir);
144 if (!(option_mask32 & ARG_n)) { /* --dry-run */
145 chp = concat_path_file(moddir, CONFIG_DEFAULT_DEPMOD_FILE);
146 filedes = xfopen(chp, "w");
147 if (ENABLE_FEATURE_CLEAN_UP)
151 /* We 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 if (ENABLE_FEATURE_CLEAN_UP)
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);