blob: e5f0e3d2b48e5d34fbe206003a90bdd27d9b2de6 [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 */
10
Denys Vlasenkob9f2d9f2011-01-18 13:58:01 +010011//applet:IF_DEPMOD(APPLET(depmod, BB_DIR_SBIN, BB_SUID_DROP))
Denys Vlasenkoc15613c2010-10-15 11:29:02 +020012
Denys Vlasenko043b1e52009-09-06 12:47:55 +020013#include "libbb.h"
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000014#include "modutils.h"
Denys Vlasenko043b1e52009-09-06 12:47:55 +020015#include <sys/utsname.h> /* uname() */
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000016
Bernhard Reutner-Fischercf180102008-05-26 15:12:01 +000017/*
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
22 */
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +000023
Denys Vlasenko16bda3b2009-05-18 13:08:04 +020024static int FAST_FUNC parse_module(const char *fname, struct stat *sb UNUSED_PARAM,
Denys Vlasenko69675782013-01-14 01:34:48 +010025 void *data, int depth UNUSED_PARAM)
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +000026{
Timo Teräs48dc80b2015-11-05 18:54:55 +010027 module_db *modules = data;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000028 char *image, *ptr;
Timo Teräs48dc80b2015-11-05 18:54:55 +010029 module_entry *e;
30
Denys Vlasenko16bda3b2009-05-18 13:08:04 +020031 /* Arbitrary. Was sb->st_size, but that breaks .gz etc */
32 size_t len = (64*1024*1024 - 4096);
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +000033
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000034 if (strrstr(fname, ".ko") == NULL)
35 return TRUE;
Bernhard Reutner-Fischer634b0222008-05-28 14:20:20 +000036
Denis Vlasenkofc668922008-11-06 02:32:31 +000037 image = xmalloc_open_zipped_read_close(fname, &len);
Bernhard Reutner-Fischerc2741e12008-06-02 18:59:16 +000038
Timo Teräs48dc80b2015-11-05 18:54:55 +010039 e = moddb_get_or_create(modules, bb_get_last_path_component_nostrip(fname));
40 e->name = xstrdup(fname + 2); /* skip "./" */
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000041
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000042 for (ptr = image; ptr < image + len - 10; ptr++) {
Denys Vlasenko8dff01d2015-03-12 17:48:34 +010043 if (is_prefixed_with(ptr, "depends=")) {
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000044 char *u;
45
46 ptr += 8;
47 for (u = ptr; *u; u++)
48 if (*u == '-')
49 *u = '_';
Timo Teräs48dc80b2015-11-05 18:54:55 +010050 ptr += string_to_llist(ptr, &e->deps, ",");
Denys Vlasenko16bda3b2009-05-18 13:08:04 +020051 } else if (ENABLE_FEATURE_MODUTILS_ALIAS
Denys Vlasenko8dff01d2015-03-12 17:48:34 +010052 && is_prefixed_with(ptr, "alias=")
Denys Vlasenko16bda3b2009-05-18 13:08:04 +020053 ) {
Timo Teräs48dc80b2015-11-05 18:54:55 +010054 llist_add_to(&e->aliases, xstrdup(ptr + 6));
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000055 ptr += strlen(ptr);
Denys Vlasenko16bda3b2009-05-18 13:08:04 +020056 } else if (ENABLE_FEATURE_MODUTILS_SYMBOLS
Denys Vlasenko8dff01d2015-03-12 17:48:34 +010057 && is_prefixed_with(ptr, "__ksymtab_")
Denys Vlasenko16bda3b2009-05-18 13:08:04 +020058 ) {
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000059 ptr += 10;
Denys Vlasenko8dff01d2015-03-12 17:48:34 +010060 if (is_prefixed_with(ptr, "gpl")
Denys Vlasenko6b9f1632010-01-28 02:24:24 +010061 || strcmp(ptr, "strings") == 0
62 ) {
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000063 continue;
Denys Vlasenko6b9f1632010-01-28 02:24:24 +010064 }
Timo Teräs48dc80b2015-11-05 18:54:55 +010065 llist_add_to(&e->symbols, xstrdup(ptr));
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000066 ptr += strlen(ptr);
67 }
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +000068 }
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000069 free(image);
70
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +000071 return TRUE;
72}
73
Timo Teräs48dc80b2015-11-05 18:54:55 +010074static void order_dep_list(module_db *modules, module_entry *start, llist_t *add)
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000075{
Timo Teräs48dc80b2015-11-05 18:54:55 +010076 module_entry *m;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000077 llist_t *n;
78
79 for (n = add; n != NULL; n = n->link) {
Timo Teräs48dc80b2015-11-05 18:54:55 +010080 m = moddb_get(modules, n->data);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000081 if (m == NULL)
82 continue;
83
84 /* unlink current entry */
85 m->dnext->dprev = m->dprev;
86 m->dprev->dnext = m->dnext;
87
88 /* and add it to tail */
89 m->dnext = start;
90 m->dprev = start->dprev;
91 start->dprev->dnext = m;
92 start->dprev = m;
93
94 /* recurse */
Timo Teräs48dc80b2015-11-05 18:54:55 +010095 order_dep_list(modules, start, m->deps);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000096 }
97}
98
Denis Vlasenko35261152009-01-12 22:12:22 +000099static void xfreopen_write(const char *file, FILE *f)
100{
101 if (freopen(file, "w", f) == NULL)
102 bb_perror_msg_and_die("can't open '%s'", file);
103}
104
Lauri Kasanen5a5db932012-02-19 16:33:37 +0100105//usage:#if !ENABLE_MODPROBE_SMALL
106//usage:#define depmod_trivial_usage "[-n] [-b BASE] [VERSION] [MODFILES]..."
107//usage:#define depmod_full_usage "\n\n"
108//usage: "Generate modules.dep, alias, and symbols files"
109//usage: "\n"
110//usage: "\n -b BASE Use BASE/lib/modules/VERSION"
111//usage: "\n -n Dry run: print files to stdout"
112//usage:#endif
113
114/* Upstream usage:
Denys Vlasenko4cab7f32010-07-18 17:41:17 +0200115 * [-aAenv] [-C FILE or DIR] [-b BASE] [-F System.map] [VERSION] [MODFILES]...
116 * -a --all
117 * Probe all modules. Default if no MODFILES.
118 * -A --quick
119 * Check modules.dep's mtime against module files' mtimes.
120 * -b --basedir BASE
121 * Use $BASE/lib/modules/VERSION
122 * -C --config FILE or DIR
123 * Path to /etc/depmod.conf or /etc/depmod.d/
124 * -e --errsyms
Lauri Kasanen5a5db932012-02-19 16:33:37 +0100125 * When combined with the -F option, this reports any symbols
Denys Vlasenko4cab7f32010-07-18 17:41:17 +0200126 * which are not supplied by other modules or kernel.
127 * -F --filesyms System.map
128 * -n --dry-run
129 * Print modules.dep etc to standard output
130 * -v --verbose
131 * Print to stdout all the symbols each module depends on
132 * and the module's file name which provides that symbol.
133 * -r No-op
Pascal Bellardcadf9012010-10-18 00:51:16 +0200134 * -u No-op
135 * -q No-op
Denys Vlasenko4cab7f32010-07-18 17:41:17 +0200136 *
Lauri Kasanen5a5db932012-02-19 16:33:37 +0100137 * So far we only support: [-n] [-b BASE] [VERSION] [MODFILES]...
138 * Accepted but ignored:
139 * -aAe
140 * -F System.map
141 * -C FILE/DIR
142 *
143 * Not accepted: -v
Denys Vlasenko4cab7f32010-07-18 17:41:17 +0200144 */
145enum {
146 //OPT_a = (1 << 0), /* All modules, ignore mods in argv */
147 //OPT_A = (1 << 1), /* Only emit .ko that are newer than modules.dep file */
148 OPT_b = (1 << 2), /* base directory when modules are in staging area */
149 //OPT_e = (1 << 3), /* with -F, print unresolved symbols */
150 //OPT_F = (1 << 4), /* System.map that contains the symbols */
151 OPT_n = (1 << 5), /* dry-run, print to stdout only */
Pascal Bellardcadf9012010-10-18 00:51:16 +0200152 OPT_r = (1 << 6), /* Compat dummy. Linux Makefile uses it */
153 OPT_u = (1 << 7), /* -u,--unresolved-error: ignored */
154 OPT_q = (1 << 8), /* -q,--quiet: ignored */
155 OPT_C = (1 << 9), /* -C,--config etc_modules_conf: ignored */
Denys Vlasenko4cab7f32010-07-18 17:41:17 +0200156};
157
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +0000158int depmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000159int depmod_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +0000160{
Timo Teräs48dc80b2015-11-05 18:54:55 +0100161 module_db modules;
162 module_entry *m, *dep;
Denis Vlasenko35261152009-01-12 22:12:22 +0000163 const char *moddir_base = "/";
164 char *moddir, *version;
165 struct utsname uts;
Timo Teräs48dc80b2015-11-05 18:54:55 +0100166 unsigned i;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000167 int tmp;
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +0000168
Pascal Bellardcadf9012010-10-18 00:51:16 +0200169 getopt32(argv, "aAb:eF:nruqC:", &moddir_base, NULL, NULL);
Bernhard Reutner-Fischer6bb55cf2008-05-26 17:04:01 +0000170 argv += optind;
Bernhard Reutner-Fischerc21d9c72008-05-28 10:35:51 +0000171
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000172 /* goto modules location */
Denis Vlasenko35261152009-01-12 22:12:22 +0000173 xchdir(moddir_base);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000174
175 /* If a version is provided, then that kernel version's module directory
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +0000176 * is used, rather than the current kernel version (as returned by
177 * "uname -r"). */
Denys Vlasenko4cab7f32010-07-18 17:41:17 +0200178 if (*argv && sscanf(*argv, "%u.%u.%u", &tmp, &tmp, &tmp) == 3) {
Denis Vlasenko35261152009-01-12 22:12:22 +0000179 version = *argv++;
Bernhard Reutner-Fischer1ea25682008-05-26 21:33:05 +0000180 } else {
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000181 uname(&uts);
Denis Vlasenko35261152009-01-12 22:12:22 +0000182 version = uts.release;
Bernhard Reutner-Fischer6bb55cf2008-05-26 17:04:01 +0000183 }
Denis Vlasenko35261152009-01-12 22:12:22 +0000184 moddir = concat_path_file(&CONFIG_DEFAULT_MODULES_DIR[1], version);
Denis Vlasenko35261152009-01-12 22:12:22 +0000185 xchdir(moddir);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000186 if (ENABLE_FEATURE_CLEAN_UP)
Denis Vlasenko35261152009-01-12 22:12:22 +0000187 free(moddir);
Bernhard Reutner-Fischer6bb55cf2008-05-26 17:04:01 +0000188
Denys Vlasenko4cab7f32010-07-18 17:41:17 +0200189 /* Scan modules */
Timo Teräs48dc80b2015-11-05 18:54:55 +0100190 memset(&modules, 0, sizeof(modules));
Denys Vlasenko4cab7f32010-07-18 17:41:17 +0200191 if (*argv) {
192 do {
193 parse_module(*argv, /*sb (unused):*/ NULL, &modules, 0);
194 } while (*++argv);
195 } else {
196 recursive_action(".", ACTION_RECURSE,
Denys Vlasenko69675782013-01-14 01:34:48 +0100197 parse_module, NULL, &modules, 0);
Denys Vlasenko4cab7f32010-07-18 17:41:17 +0200198 }
199
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000200 /* Generate dependency and alias files */
Denys Vlasenko4cab7f32010-07-18 17:41:17 +0200201 if (!(option_mask32 & OPT_n))
Denis Vlasenko35261152009-01-12 22:12:22 +0000202 xfreopen_write(CONFIG_DEFAULT_DEPMOD_FILE, stdout);
Timo Teräs48dc80b2015-11-05 18:54:55 +0100203
204 moddb_foreach_module(&modules, m, i) {
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000205 printf("%s:", m->name);
206
Timo Teräs48dc80b2015-11-05 18:54:55 +0100207 order_dep_list(&modules, m, m->deps);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000208 while (m->dnext != m) {
209 dep = m->dnext;
210 printf(" %s", dep->name);
211
212 /* unlink current entry */
213 dep->dnext->dprev = dep->dprev;
214 dep->dprev->dnext = dep->dnext;
215 dep->dnext = dep->dprev = dep;
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +0000216 }
Denis Vlasenko35261152009-01-12 22:12:22 +0000217 bb_putchar('\n');
Bernhard Reutner-Fischerc2741e12008-06-02 18:59:16 +0000218 }
219
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000220#if ENABLE_FEATURE_MODUTILS_ALIAS
Denys Vlasenko4cab7f32010-07-18 17:41:17 +0200221 if (!(option_mask32 & OPT_n))
Denis Vlasenko35261152009-01-12 22:12:22 +0000222 xfreopen_write("modules.alias", stdout);
Timo Teräs48dc80b2015-11-05 18:54:55 +0100223 moddb_foreach_module(&modules, m, i) {
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000224 while (m->aliases) {
Denys Vlasenkocc70b6f2015-01-24 22:30:30 +0100225 /*
226 * Last word used to be a basename
227 * (filename with path and .ko.* stripped)
228 * at the time of module-init-tools 3.4.
229 * kmod v.12 uses module name, i.e., s/-/_/g.
230 */
Denys Vlasenko08364da2015-01-07 14:39:01 +0100231 printf("alias %s %s\n",
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000232 (char*)llist_pop(&m->aliases),
Timo Teräs48dc80b2015-11-05 18:54:55 +0100233 m->modname);
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +0000234 }
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000235 }
236#endif
237#if ENABLE_FEATURE_MODUTILS_SYMBOLS
Denys Vlasenko4cab7f32010-07-18 17:41:17 +0200238 if (!(option_mask32 & OPT_n))
Denis Vlasenko35261152009-01-12 22:12:22 +0000239 xfreopen_write("modules.symbols", stdout);
Timo Teräs48dc80b2015-11-05 18:54:55 +0100240 moddb_foreach_module(&modules, m, i) {
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000241 while (m->symbols) {
Denys Vlasenko08364da2015-01-07 14:39:01 +0100242 printf("alias symbol:%s %s\n",
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000243 (char*)llist_pop(&m->symbols),
Timo Teräs48dc80b2015-11-05 18:54:55 +0100244 m->modname);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000245 }
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +0000246 }
Bernhard Reutner-Fischerc2741e12008-06-02 18:59:16 +0000247#endif
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +0000248
Timo Teräs48dc80b2015-11-05 18:54:55 +0100249 if (ENABLE_FEATURE_CLEAN_UP)
250 moddb_free(&modules);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000251
252 return EXIT_SUCCESS;
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +0000253}