1 /* vi: set sw=4 ts=4: */
3 * depmod - generate modules.dep
4 * Copyright (c) 2008 Bernhard Reutner-Fischer
5 * Copyrihgt (c) 2008 Timo Teras <timo.teras@iki.fi>
6 * Copyright (c) 2008 Vladimir Dronnikov
8 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
14 #include <sys/utsname.h> /* uname() */
18 * Theory of operation:
19 * - iterate over all modules and record their full path
20 * - iterate over all modules looking for "depends=" entries
21 * for each depends, look through our list of full paths and emit if found
24 typedef struct module_info {
25 struct module_info *next;
27 llist_t *dependencies;
30 struct module_info *dnext, *dprev;
34 ARG_a = (1<<0), /* All modules, ignore mods in argv */
35 ARG_A = (1<<1), /* Only emit .ko that are newer than modules.dep file */
36 ARG_b = (1<<2), /* not /lib/modules/$(uname -r)/ but this base-dir */
37 ARG_e = (1<<3), /* with -F, print unresolved symbols */
38 ARG_F = (1<<4), /* System.map that contains the symbols */
39 ARG_n = (1<<5) /* dry-run, print to stdout only */
42 static int FAST_FUNC parse_module(const char *fname, struct stat *sb,
43 void *data, int UNUSED_PARAM depth)
45 module_info **first = (module_info **) data;
48 size_t len = sb->st_size;
50 if (strrstr(fname, ".ko") == NULL)
53 image = xmalloc_open_zipped_read_close(fname, &len);
54 info = xzalloc(sizeof(module_info));
59 info->dnext = info->dprev = info;
60 info->name = xstrdup(fname);
61 info->modname = filename2modname(fname, NULL);
62 for (ptr = image; ptr < image + len - 10; ptr++) {
63 if (strncmp(ptr, "depends=", 8) == 0) {
67 for (u = ptr; *u; u++)
70 ptr += string_to_llist(ptr, &info->dependencies, ",");
71 } else if (ENABLE_FEATURE_MODUTILS_ALIAS &&
72 strncmp(ptr, "alias=", 6) == 0) {
73 llist_add_to(&info->aliases, xstrdup(ptr + 6));
75 } else if (ENABLE_FEATURE_MODUTILS_SYMBOLS &&
76 strncmp(ptr, "__ksymtab_", 10) == 0) {
78 if (strncmp(ptr, "gpl", 3) == 0 ||
79 strcmp(ptr, "strings") == 0)
81 llist_add_to(&info->symbols, xstrdup(ptr));
90 static module_info *find_module(module_info *modules, const char *modname)
94 for (m = modules; m != NULL; m = m->next)
95 if (strcmp(m->modname, modname) == 0)
100 static void order_dep_list(module_info *modules, module_info *start,
106 for (n = add; n != NULL; n = n->link) {
107 m = find_module(modules, n->data);
111 /* unlink current entry */
112 m->dnext->dprev = m->dprev;
113 m->dprev->dnext = m->dnext;
115 /* and add it to tail */
117 m->dprev = start->dprev;
118 start->dprev->dnext = m;
122 order_dep_list(modules, start, m->dependencies);
126 int depmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
127 int depmod_main(int argc UNUSED_PARAM, char **argv)
129 module_info *modules = NULL, *m, *dep;
130 char *moddir_base = (char *)CONFIG_DEFAULT_MODULES_DIR;
133 getopt32(argv, "aAb:eF:n", &moddir_base, NULL);
136 /* goto modules location */
138 /* If a version is provided, then that kernel version's module directory
139 * is used, rather than the current kernel version (as returned by
142 if (*argv && (sscanf(*argv, "%d.%d.%d", &tmp, &tmp, &tmp) == 3)) {
149 /* If no modules are given on the command-line, -a is on per default. */
150 option_mask32 |= *argv == NULL;
153 moddir_base = xrealloc_getcwd_or_warn(NULL);
155 recursive_action((option_mask32 & ARG_a) ? moddir_base : *argv,
156 ACTION_RECURSE, parse_module, NULL, &modules, 0);
157 } while (!(option_mask32 & ARG_a) && *(++argv));
158 if (ENABLE_FEATURE_CLEAN_UP)
161 /* Generate dependency and alias files */
162 if (!(option_mask32 & ARG_n))
163 freopen(CONFIG_DEFAULT_DEPMOD_FILE, "w", stdout);
164 for (m = modules; m != NULL; m = m->next) {
165 printf("%s:", m->name);
167 order_dep_list(modules, m, m->dependencies);
168 while (m->dnext != m) {
170 printf(" %s", dep->name);
172 /* unlink current entry */
173 dep->dnext->dprev = dep->dprev;
174 dep->dprev->dnext = dep->dnext;
175 dep->dnext = dep->dprev = dep;
180 #if ENABLE_FEATURE_MODUTILS_ALIAS
181 if (!(option_mask32 & ARG_n))
182 freopen("modules.alias", "w", stdout);
183 for (m = modules; m != NULL; m = m->next) {
185 printf("alias %s %s\n",
186 (char*)llist_pop(&m->aliases),
191 #if ENABLE_FEATURE_MODUTILS_SYMBOLS
192 if (!(option_mask32 & ARG_n))
193 freopen("modules.symbols", "w", stdout);
194 for (m = modules; m != NULL; m = m->next) {
196 printf("alias symbol:%s %s\n",
197 (char*)llist_pop(&m->symbols),
203 if (ENABLE_FEATURE_CLEAN_UP) {
205 module_info *old = modules;
206 modules = modules->next;