1 /* vi: set sw=4 ts=4: */
2 /*
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
7  *
8  * Licensed under GPLv2 or later, see file LICENSE in this source tree.
9  */
10 //config:config DEPMOD
11 //config:	bool "depmod (27 kb)"
12 //config:	default y
13 //config:	help
14 //config:	depmod generates modules.dep (and potentially modules.alias
15 //config:	and modules.symbols) that contain dependency information
16 //config:	for modprobe.
17 
18 //applet:IF_DEPMOD(IF_NOT_MODPROBE_SMALL(APPLET(depmod, BB_DIR_SBIN, BB_SUID_DROP)))
19 
20 //kbuild:ifneq ($(CONFIG_MODPROBE_SMALL),y)
21 //kbuild:lib-$(CONFIG_DEPMOD) += depmod.o modutils.o
22 //kbuild:endif
23 
24 #include "libbb.h"
25 #include "modutils.h"
26 #include <sys/utsname.h> /* uname() */
27 
28 /*
29  * Theory of operation:
30  * - iterate over all modules and record their full path
31  * - iterate over all modules looking for "depends=" entries
32  *   for each depends, look through our list of full paths and emit if found
33  */
34 
parse_module(struct recursive_state * state,const char * fname,struct stat * sb UNUSED_PARAM)35 static int FAST_FUNC parse_module(struct recursive_state *state,
36 		const char *fname,
37 		struct stat *sb UNUSED_PARAM)
38 {
39 	module_db *modules = state->userData;
40 	char *image, *ptr;
41 	module_entry *e;
42 
43 	/* Arbitrary. Was sb->st_size, but that breaks .gz etc */
44 	size_t len = (64*1024*1024 - 4096);
45 
46 	if (strrstr(fname, ".ko") == NULL)
47 		return TRUE;
48 
49 	image = xmalloc_open_zipped_read_close(fname, &len);
50 
51 	e = moddb_get_or_create(modules, bb_get_last_path_component_nostrip(fname));
52 	e->name = xstrdup(fname + 2); /* skip "./" */
53 
54 	for (ptr = image; ptr < image + len - 10; ptr++) {
55 		if (is_prefixed_with(ptr, "depends=")) {
56 			char *u;
57 
58 			ptr += 8;
59 			for (u = ptr; *u; u++)
60 				if (*u == '-')
61 					*u = '_';
62 			ptr += string_to_llist(ptr, &e->deps, ",");
63 		} else if (ENABLE_FEATURE_MODUTILS_ALIAS
64 		 && is_prefixed_with(ptr, "alias=")
65 		) {
66 			llist_add_to(&e->aliases, xstrdup(ptr + 6));
67 			ptr += strlen(ptr);
68 		} else if (ENABLE_FEATURE_MODUTILS_SYMBOLS
69 		 && is_prefixed_with(ptr, "__ksymtab_")
70 		) {
71 			ptr += 10;
72 			if (is_prefixed_with(ptr, "gpl")
73 			 || strcmp(ptr, "strings") == 0
74 			) {
75 				continue;
76 			}
77 			llist_add_to(&e->symbols, xstrdup(ptr));
78 			ptr += strlen(ptr);
79 		}
80 	}
81 	free(image);
82 
83 	return TRUE;
84 }
85 
order_dep_list(module_db * modules,module_entry * start,llist_t * add)86 static void order_dep_list(module_db *modules, module_entry *start, llist_t *add)
87 {
88 	module_entry *m;
89 	llist_t *n;
90 
91 	for (n = add; n != NULL; n = n->link) {
92 		m = moddb_get(modules, n->data);
93 		if (m == NULL)
94 			continue;
95 
96 		/* unlink current entry */
97 		m->dnext->dprev = m->dprev;
98 		m->dprev->dnext = m->dnext;
99 
100 		/* and add it to tail */
101 		m->dnext = start;
102 		m->dprev = start->dprev;
103 		start->dprev->dnext = m;
104 		start->dprev = m;
105 
106 		/* recurse */
107 		order_dep_list(modules, start, m->deps);
108 	}
109 }
110 
xfreopen_write(const char * file,FILE * f)111 static void xfreopen_write(const char *file, FILE *f)
112 {
113 	if (freopen(file, "w", f) == NULL)
114 		bb_perror_msg_and_die("can't open '%s'", file);
115 }
116 
117 //usage:#if !ENABLE_MODPROBE_SMALL
118 //usage:#define depmod_trivial_usage "[-n] [-b BASE] [VERSION] [MODFILES]..."
119 //usage:#define depmod_full_usage "\n\n"
120 //usage:       "Generate modules.dep, alias, and symbols files"
121 //usage:     "\n"
122 //usage:     "\n	-b BASE	Use BASE/lib/modules/VERSION"
123 //usage:     "\n	-n	Dry run: print files to stdout"
124 //usage:#endif
125 
126 /* Upstream usage:
127  * [-aAenv] [-C FILE or DIR] [-b BASE] [-F System.map] [VERSION] [MODFILES]...
128  *	-a --all
129  *		Probe all modules. Default if no MODFILES.
130  *	-A --quick
131  *		Check modules.dep's mtime against module files' mtimes.
132  *	-b --basedir BASE
133  *		Use $BASE/lib/modules/VERSION
134  *	-C --config FILE or DIR
135  *		Path to /etc/depmod.conf or /etc/depmod.d/
136  *	-e --errsyms
137  *		When combined with the -F option, this reports any symbols
138  *		which are not supplied by other modules or kernel.
139  *	-F --filesyms System.map
140  *	-n --dry-run
141  *		Print modules.dep etc to standard output
142  *	-v --verbose
143  *		Print to stdout all the symbols each module depends on
144  *		and the module's file name which provides that symbol.
145  *	-r	No-op
146  *	-u	No-op
147  *	-q	No-op
148  *
149  * So far we only support: [-n] [-b BASE] [VERSION] [MODFILES]...
150  * Accepted but ignored:
151  * -aAe
152  * -F System.map
153  * -C FILE/DIR
154  *
155  * Not accepted: -v
156  */
157 enum {
158 	//OPT_a = (1 << 0), /* All modules, ignore mods in argv */
159 	//OPT_A = (1 << 1), /* Only emit .ko that are newer than modules.dep file */
160 	OPT_b = (1 << 2), /* base directory when modules are in staging area */
161 	//OPT_e = (1 << 3), /* with -F, print unresolved symbols */
162 	//OPT_F = (1 << 4), /* System.map that contains the symbols */
163 	OPT_n = (1 << 5), /* dry-run, print to stdout only */
164 	OPT_r = (1 << 6), /* Compat dummy. Linux Makefile uses it */
165 	OPT_u = (1 << 7), /* -u,--unresolved-error: ignored */
166 	OPT_q = (1 << 8), /* -q,--quiet: ignored */
167 	OPT_C = (1 << 9), /* -C,--config etc_modules_conf: ignored */
168 };
169 
170 int depmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
depmod_main(int argc UNUSED_PARAM,char ** argv)171 int depmod_main(int argc UNUSED_PARAM, char **argv)
172 {
173 	module_db modules;
174 	module_entry *m, *dep;
175 	const char *moddir_base = "/";
176 	char *moddir, *version;
177 	struct utsname uts;
178 	unsigned i;
179 	int tmp;
180 
181 	getopt32(argv, "aAb:eF:nruqC:", &moddir_base, NULL, NULL);
182 	argv += optind;
183 
184 	/* goto modules location */
185 	xchdir(moddir_base);
186 
187 	/* If a version is provided, then that kernel version's module directory
188 	 * is used, rather than the current kernel version (as returned by
189 	 * "uname -r").  */
190 	if (*argv && sscanf(*argv, "%u.%u.%u", &tmp, &tmp, &tmp) == 3) {
191 		version = *argv++;
192 	} else {
193 		uname(&uts);
194 		version = uts.release;
195 	}
196 	moddir = concat_path_file(&CONFIG_DEFAULT_MODULES_DIR[1], version);
197 	xchdir(moddir);
198 	if (ENABLE_FEATURE_CLEAN_UP)
199 		free(moddir);
200 
201 	/* Scan modules */
202 	memset(&modules, 0, sizeof(modules));
203 	if (*argv) {
204 		do {
205 			recursive_action(*argv, 0 /* no ACTION_RECURSE! */,
206 				parse_module, NULL, &modules);
207 		} while (*++argv);
208 	} else {
209 		recursive_action(".", ACTION_RECURSE,
210 				parse_module, NULL, &modules);
211 	}
212 
213 	/* Generate dependency and alias files */
214 	if (!(option_mask32 & OPT_n))
215 		xfreopen_write(CONFIG_DEFAULT_DEPMOD_FILE, stdout);
216 
217 	moddb_foreach_module(&modules, m, i) {
218 		printf("%s:", m->name);
219 
220 		order_dep_list(&modules, m, m->deps);
221 		while (m->dnext != m) {
222 			dep = m->dnext;
223 			printf(" %s", dep->name);
224 
225 			/* unlink current entry */
226 			dep->dnext->dprev = dep->dprev;
227 			dep->dprev->dnext = dep->dnext;
228 			dep->dnext = dep->dprev = dep;
229 		}
230 		bb_putchar('\n');
231 	}
232 
233 #if ENABLE_FEATURE_MODUTILS_ALIAS
234 	if (!(option_mask32 & OPT_n))
235 		xfreopen_write("modules.alias", stdout);
236 	moddb_foreach_module(&modules, m, i) {
237 		while (m->aliases) {
238 			/*
239 			 * Last word used to be a basename
240 			 * (filename with path and .ko.* stripped)
241 			 * at the time of module-init-tools 3.4.
242 			 * kmod v.12 uses module name, i.e., s/-/_/g.
243 			 */
244 			printf("alias %s %s\n",
245 				(char*)llist_pop(&m->aliases),
246 				m->modname);
247 		}
248 	}
249 #endif
250 #if ENABLE_FEATURE_MODUTILS_SYMBOLS
251 	if (!(option_mask32 & OPT_n))
252 		xfreopen_write("modules.symbols", stdout);
253 	moddb_foreach_module(&modules, m, i) {
254 		while (m->symbols) {
255 			printf("alias symbol:%s %s\n",
256 				(char*)llist_pop(&m->symbols),
257 				m->modname);
258 		}
259 	}
260 #endif
261 
262 	if (ENABLE_FEATURE_CLEAN_UP)
263 		moddb_free(&modules);
264 
265 	return EXIT_SUCCESS;
266 }
267