blob: 004c67a11dee54019adcad2af64aa33d38b8d028 [file] [log] [blame]
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +00001/* vi: set sw=4 ts=4: */
2/*
3 * depmod - generate modules.dep
Bernhard Reutner-Fischer6c4dade2008-09-25 12:13:34 +00004 * Copyright (c) 2008 Bernhard Reutner-Fischer
Denis Vlasenkoba1315d2008-09-13 14:59:38 +00005 * Copyrihgt (c) 2008 Timo Teras <timo.teras@iki.fi>
6 * Copyright (c) 2008 Vladimir Dronnikov
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +00007 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02008 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +00009 */
Denys Vlasenkoe32b64c2016-11-23 07:54:52 +010010//config:config DEPMOD
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020011//config: bool "depmod (26 kb)"
Denys Vlasenkoa1cd0d92016-12-23 15:12:27 +010012//config: default y
Denys Vlasenkoe32b64c2016-11-23 07:54:52 +010013//config: select PLATFORM_LINUX
14//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020015//config: depmod generates modules.dep (and potentially modules.alias
16//config: and modules.symbols) that contain dependency information
17//config: for modprobe.
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +000018
Denys Vlasenkoa1cd0d92016-12-23 15:12:27 +010019//applet:IF_DEPMOD(IF_NOT_MODPROBE_SMALL(APPLET(depmod, BB_DIR_SBIN, BB_SUID_DROP)))
Denys Vlasenkoc15613c2010-10-15 11:29:02 +020020
Denys Vlasenko1f710142017-01-01 13:12:48 +010021//kbuild:ifneq ($(CONFIG_MODPROBE_SMALL),y)
Denys Vlasenkoe32b64c2016-11-23 07:54:52 +010022//kbuild:lib-$(CONFIG_DEPMOD) += depmod.o modutils.o
Denys Vlasenko1f710142017-01-01 13:12:48 +010023//kbuild:endif
Denys Vlasenkoe32b64c2016-11-23 07:54:52 +010024
Denys Vlasenko043b1e52009-09-06 12:47:55 +020025#include "libbb.h"
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000026#include "modutils.h"
Denys Vlasenko043b1e52009-09-06 12:47:55 +020027#include <sys/utsname.h> /* uname() */
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000028
Bernhard Reutner-Fischercf180102008-05-26 15:12:01 +000029/*
30 * Theory of operation:
31 * - iterate over all modules and record their full path
32 * - iterate over all modules looking for "depends=" entries
33 * for each depends, look through our list of full paths and emit if found
34 */
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +000035
Denys Vlasenko16bda3b2009-05-18 13:08:04 +020036static int FAST_FUNC parse_module(const char *fname, struct stat *sb UNUSED_PARAM,
Denys Vlasenko69675782013-01-14 01:34:48 +010037 void *data, int depth UNUSED_PARAM)
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +000038{
Timo Teräs48dc80b2015-11-05 18:54:55 +010039 module_db *modules = data;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000040 char *image, *ptr;
Timo Teräs48dc80b2015-11-05 18:54:55 +010041 module_entry *e;
42
Denys Vlasenko16bda3b2009-05-18 13:08:04 +020043 /* Arbitrary. Was sb->st_size, but that breaks .gz etc */
44 size_t len = (64*1024*1024 - 4096);
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +000045
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000046 if (strrstr(fname, ".ko") == NULL)
47 return TRUE;
Bernhard Reutner-Fischer634b0222008-05-28 14:20:20 +000048
Denis Vlasenkofc668922008-11-06 02:32:31 +000049 image = xmalloc_open_zipped_read_close(fname, &len);
Bernhard Reutner-Fischerc2741e12008-06-02 18:59:16 +000050
Timo Teräs48dc80b2015-11-05 18:54:55 +010051 e = moddb_get_or_create(modules, bb_get_last_path_component_nostrip(fname));
52 e->name = xstrdup(fname + 2); /* skip "./" */
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000053
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000054 for (ptr = image; ptr < image + len - 10; ptr++) {
Denys Vlasenko8dff01d2015-03-12 17:48:34 +010055 if (is_prefixed_with(ptr, "depends=")) {
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000056 char *u;
57
58 ptr += 8;
59 for (u = ptr; *u; u++)
60 if (*u == '-')
61 *u = '_';
Timo Teräs48dc80b2015-11-05 18:54:55 +010062 ptr += string_to_llist(ptr, &e->deps, ",");
Denys Vlasenko16bda3b2009-05-18 13:08:04 +020063 } else if (ENABLE_FEATURE_MODUTILS_ALIAS
Denys Vlasenko8dff01d2015-03-12 17:48:34 +010064 && is_prefixed_with(ptr, "alias=")
Denys Vlasenko16bda3b2009-05-18 13:08:04 +020065 ) {
Timo Teräs48dc80b2015-11-05 18:54:55 +010066 llist_add_to(&e->aliases, xstrdup(ptr + 6));
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000067 ptr += strlen(ptr);
Denys Vlasenko16bda3b2009-05-18 13:08:04 +020068 } else if (ENABLE_FEATURE_MODUTILS_SYMBOLS
Denys Vlasenko8dff01d2015-03-12 17:48:34 +010069 && is_prefixed_with(ptr, "__ksymtab_")
Denys Vlasenko16bda3b2009-05-18 13:08:04 +020070 ) {
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000071 ptr += 10;
Denys Vlasenko8dff01d2015-03-12 17:48:34 +010072 if (is_prefixed_with(ptr, "gpl")
Denys Vlasenko6b9f1632010-01-28 02:24:24 +010073 || strcmp(ptr, "strings") == 0
74 ) {
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000075 continue;
Denys Vlasenko6b9f1632010-01-28 02:24:24 +010076 }
Timo Teräs48dc80b2015-11-05 18:54:55 +010077 llist_add_to(&e->symbols, xstrdup(ptr));
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000078 ptr += strlen(ptr);
79 }
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +000080 }
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000081 free(image);
82
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +000083 return TRUE;
84}
85
Timo Teräs48dc80b2015-11-05 18:54:55 +010086static void order_dep_list(module_db *modules, module_entry *start, llist_t *add)
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000087{
Timo Teräs48dc80b2015-11-05 18:54:55 +010088 module_entry *m;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000089 llist_t *n;
90
91 for (n = add; n != NULL; n = n->link) {
Timo Teräs48dc80b2015-11-05 18:54:55 +010092 m = moddb_get(modules, n->data);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000093 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 */
Timo Teräs48dc80b2015-11-05 18:54:55 +0100107 order_dep_list(modules, start, m->deps);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000108 }
109}
110
Denis Vlasenko35261152009-01-12 22:12:22 +0000111static 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
Lauri Kasanen5a5db932012-02-19 16:33:37 +0100117//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:
Denys Vlasenko4cab7f32010-07-18 17:41:17 +0200127 * [-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
Lauri Kasanen5a5db932012-02-19 16:33:37 +0100137 * When combined with the -F option, this reports any symbols
Denys Vlasenko4cab7f32010-07-18 17:41:17 +0200138 * 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
Pascal Bellardcadf9012010-10-18 00:51:16 +0200146 * -u No-op
147 * -q No-op
Denys Vlasenko4cab7f32010-07-18 17:41:17 +0200148 *
Lauri Kasanen5a5db932012-02-19 16:33:37 +0100149 * 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
Denys Vlasenko4cab7f32010-07-18 17:41:17 +0200156 */
157enum {
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 */
Pascal Bellardcadf9012010-10-18 00:51:16 +0200164 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 */
Denys Vlasenko4cab7f32010-07-18 17:41:17 +0200168};
169
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +0000170int depmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000171int depmod_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +0000172{
Timo Teräs48dc80b2015-11-05 18:54:55 +0100173 module_db modules;
174 module_entry *m, *dep;
Denis Vlasenko35261152009-01-12 22:12:22 +0000175 const char *moddir_base = "/";
176 char *moddir, *version;
177 struct utsname uts;
Timo Teräs48dc80b2015-11-05 18:54:55 +0100178 unsigned i;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000179 int tmp;
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +0000180
Pascal Bellardcadf9012010-10-18 00:51:16 +0200181 getopt32(argv, "aAb:eF:nruqC:", &moddir_base, NULL, NULL);
Bernhard Reutner-Fischer6bb55cf2008-05-26 17:04:01 +0000182 argv += optind;
Bernhard Reutner-Fischerc21d9c72008-05-28 10:35:51 +0000183
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000184 /* goto modules location */
Denis Vlasenko35261152009-01-12 22:12:22 +0000185 xchdir(moddir_base);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000186
187 /* If a version is provided, then that kernel version's module directory
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +0000188 * is used, rather than the current kernel version (as returned by
189 * "uname -r"). */
Denys Vlasenko4cab7f32010-07-18 17:41:17 +0200190 if (*argv && sscanf(*argv, "%u.%u.%u", &tmp, &tmp, &tmp) == 3) {
Denis Vlasenko35261152009-01-12 22:12:22 +0000191 version = *argv++;
Bernhard Reutner-Fischer1ea25682008-05-26 21:33:05 +0000192 } else {
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000193 uname(&uts);
Denis Vlasenko35261152009-01-12 22:12:22 +0000194 version = uts.release;
Bernhard Reutner-Fischer6bb55cf2008-05-26 17:04:01 +0000195 }
Denis Vlasenko35261152009-01-12 22:12:22 +0000196 moddir = concat_path_file(&CONFIG_DEFAULT_MODULES_DIR[1], version);
Denis Vlasenko35261152009-01-12 22:12:22 +0000197 xchdir(moddir);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000198 if (ENABLE_FEATURE_CLEAN_UP)
Denis Vlasenko35261152009-01-12 22:12:22 +0000199 free(moddir);
Bernhard Reutner-Fischer6bb55cf2008-05-26 17:04:01 +0000200
Denys Vlasenko4cab7f32010-07-18 17:41:17 +0200201 /* Scan modules */
Timo Teräs48dc80b2015-11-05 18:54:55 +0100202 memset(&modules, 0, sizeof(modules));
Denys Vlasenko4cab7f32010-07-18 17:41:17 +0200203 if (*argv) {
204 do {
205 parse_module(*argv, /*sb (unused):*/ NULL, &modules, 0);
206 } while (*++argv);
207 } else {
208 recursive_action(".", ACTION_RECURSE,
Denys Vlasenko69675782013-01-14 01:34:48 +0100209 parse_module, NULL, &modules, 0);
Denys Vlasenko4cab7f32010-07-18 17:41:17 +0200210 }
211
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000212 /* Generate dependency and alias files */
Denys Vlasenko4cab7f32010-07-18 17:41:17 +0200213 if (!(option_mask32 & OPT_n))
Denis Vlasenko35261152009-01-12 22:12:22 +0000214 xfreopen_write(CONFIG_DEFAULT_DEPMOD_FILE, stdout);
Timo Teräs48dc80b2015-11-05 18:54:55 +0100215
216 moddb_foreach_module(&modules, m, i) {
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000217 printf("%s:", m->name);
218
Timo Teräs48dc80b2015-11-05 18:54:55 +0100219 order_dep_list(&modules, m, m->deps);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000220 while (m->dnext != m) {
221 dep = m->dnext;
222 printf(" %s", dep->name);
223
224 /* unlink current entry */
225 dep->dnext->dprev = dep->dprev;
226 dep->dprev->dnext = dep->dnext;
227 dep->dnext = dep->dprev = dep;
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +0000228 }
Denis Vlasenko35261152009-01-12 22:12:22 +0000229 bb_putchar('\n');
Bernhard Reutner-Fischerc2741e12008-06-02 18:59:16 +0000230 }
231
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000232#if ENABLE_FEATURE_MODUTILS_ALIAS
Denys Vlasenko4cab7f32010-07-18 17:41:17 +0200233 if (!(option_mask32 & OPT_n))
Denis Vlasenko35261152009-01-12 22:12:22 +0000234 xfreopen_write("modules.alias", stdout);
Timo Teräs48dc80b2015-11-05 18:54:55 +0100235 moddb_foreach_module(&modules, m, i) {
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000236 while (m->aliases) {
Denys Vlasenkocc70b6f2015-01-24 22:30:30 +0100237 /*
238 * Last word used to be a basename
239 * (filename with path and .ko.* stripped)
240 * at the time of module-init-tools 3.4.
241 * kmod v.12 uses module name, i.e., s/-/_/g.
242 */
Denys Vlasenko08364da2015-01-07 14:39:01 +0100243 printf("alias %s %s\n",
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000244 (char*)llist_pop(&m->aliases),
Timo Teräs48dc80b2015-11-05 18:54:55 +0100245 m->modname);
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +0000246 }
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000247 }
248#endif
249#if ENABLE_FEATURE_MODUTILS_SYMBOLS
Denys Vlasenko4cab7f32010-07-18 17:41:17 +0200250 if (!(option_mask32 & OPT_n))
Denis Vlasenko35261152009-01-12 22:12:22 +0000251 xfreopen_write("modules.symbols", stdout);
Timo Teräs48dc80b2015-11-05 18:54:55 +0100252 moddb_foreach_module(&modules, m, i) {
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000253 while (m->symbols) {
Denys Vlasenko08364da2015-01-07 14:39:01 +0100254 printf("alias symbol:%s %s\n",
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000255 (char*)llist_pop(&m->symbols),
Timo Teräs48dc80b2015-11-05 18:54:55 +0100256 m->modname);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000257 }
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +0000258 }
Bernhard Reutner-Fischerc2741e12008-06-02 18:59:16 +0000259#endif
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +0000260
Timo Teräs48dc80b2015-11-05 18:54:55 +0100261 if (ENABLE_FEATURE_CLEAN_UP)
262 moddb_free(&modules);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000263
264 return EXIT_SUCCESS;
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +0000265}