blob: 9713aef922783d9e53f2d89c310a9edffb7b84cf [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
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000024typedef struct module_info {
25 struct module_info *next;
26 char *name, *modname;
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +000027 llist_t *dependencies;
Bernhard Reutner-Fischerc2741e12008-06-02 18:59:16 +000028 llist_t *aliases;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000029 llist_t *symbols;
30 struct module_info *dnext, *dprev;
31} module_info;
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +000032
Denys Vlasenko16bda3b2009-05-18 13:08:04 +020033static int FAST_FUNC parse_module(const char *fname, struct stat *sb UNUSED_PARAM,
Denys Vlasenko69675782013-01-14 01:34:48 +010034 void *data, int depth UNUSED_PARAM)
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +000035{
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000036 module_info **first = (module_info **) data;
37 char *image, *ptr;
38 module_info *info;
Denys Vlasenko16bda3b2009-05-18 13:08:04 +020039 /* Arbitrary. Was sb->st_size, but that breaks .gz etc */
40 size_t len = (64*1024*1024 - 4096);
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +000041
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000042 if (strrstr(fname, ".ko") == NULL)
43 return TRUE;
Bernhard Reutner-Fischer634b0222008-05-28 14:20:20 +000044
Denis Vlasenkofc668922008-11-06 02:32:31 +000045 image = xmalloc_open_zipped_read_close(fname, &len);
Denys Vlasenko16bda3b2009-05-18 13:08:04 +020046 info = xzalloc(sizeof(*info));
Bernhard Reutner-Fischerc2741e12008-06-02 18:59:16 +000047
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000048 info->next = *first;
49 *first = info;
50
51 info->dnext = info->dprev = info;
Denys Vlasenko4cab7f32010-07-18 17:41:17 +020052 info->name = xstrdup(fname + 2); /* skip "./" */
Denys Vlasenkocc70b6f2015-01-24 22:30:30 +010053 info->modname = filename2modname(
Denys Vlasenko08364da2015-01-07 14:39:01 +010054 bb_get_last_path_component_nostrip(fname),
Denys Vlasenkocc70b6f2015-01-24 22:30:30 +010055 NULL
56 );
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000057 for (ptr = image; ptr < image + len - 10; ptr++) {
Denys Vlasenko8dff01d2015-03-12 17:48:34 +010058 if (is_prefixed_with(ptr, "depends=")) {
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000059 char *u;
60
61 ptr += 8;
62 for (u = ptr; *u; u++)
63 if (*u == '-')
64 *u = '_';
65 ptr += string_to_llist(ptr, &info->dependencies, ",");
Denys Vlasenko16bda3b2009-05-18 13:08:04 +020066 } else if (ENABLE_FEATURE_MODUTILS_ALIAS
Denys Vlasenko8dff01d2015-03-12 17:48:34 +010067 && is_prefixed_with(ptr, "alias=")
Denys Vlasenko16bda3b2009-05-18 13:08:04 +020068 ) {
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000069 llist_add_to(&info->aliases, xstrdup(ptr + 6));
70 ptr += strlen(ptr);
Denys Vlasenko16bda3b2009-05-18 13:08:04 +020071 } else if (ENABLE_FEATURE_MODUTILS_SYMBOLS
Denys Vlasenko8dff01d2015-03-12 17:48:34 +010072 && is_prefixed_with(ptr, "__ksymtab_")
Denys Vlasenko16bda3b2009-05-18 13:08:04 +020073 ) {
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000074 ptr += 10;
Denys Vlasenko8dff01d2015-03-12 17:48:34 +010075 if (is_prefixed_with(ptr, "gpl")
Denys Vlasenko6b9f1632010-01-28 02:24:24 +010076 || strcmp(ptr, "strings") == 0
77 ) {
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000078 continue;
Denys Vlasenko6b9f1632010-01-28 02:24:24 +010079 }
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000080 llist_add_to(&info->symbols, xstrdup(ptr));
81 ptr += strlen(ptr);
82 }
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +000083 }
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000084 free(image);
85
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +000086 return TRUE;
87}
88
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000089static module_info *find_module(module_info *modules, const char *modname)
90{
91 module_info *m;
92
93 for (m = modules; m != NULL; m = m->next)
94 if (strcmp(m->modname, modname) == 0)
95 return m;
96 return NULL;
97}
98
99static void order_dep_list(module_info *modules, module_info *start,
Denys Vlasenko69675782013-01-14 01:34:48 +0100100 llist_t *add)
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000101{
102 module_info *m;
103 llist_t *n;
104
105 for (n = add; n != NULL; n = n->link) {
106 m = find_module(modules, n->data);
107 if (m == NULL)
108 continue;
109
110 /* unlink current entry */
111 m->dnext->dprev = m->dprev;
112 m->dprev->dnext = m->dnext;
113
114 /* and add it to tail */
115 m->dnext = start;
116 m->dprev = start->dprev;
117 start->dprev->dnext = m;
118 start->dprev = m;
119
120 /* recurse */
121 order_dep_list(modules, start, m->dependencies);
122 }
123}
124
Denis Vlasenko35261152009-01-12 22:12:22 +0000125static void xfreopen_write(const char *file, FILE *f)
126{
127 if (freopen(file, "w", f) == NULL)
128 bb_perror_msg_and_die("can't open '%s'", file);
129}
130
Lauri Kasanen5a5db932012-02-19 16:33:37 +0100131//usage:#if !ENABLE_MODPROBE_SMALL
132//usage:#define depmod_trivial_usage "[-n] [-b BASE] [VERSION] [MODFILES]..."
133//usage:#define depmod_full_usage "\n\n"
134//usage: "Generate modules.dep, alias, and symbols files"
135//usage: "\n"
136//usage: "\n -b BASE Use BASE/lib/modules/VERSION"
137//usage: "\n -n Dry run: print files to stdout"
138//usage:#endif
139
140/* Upstream usage:
Denys Vlasenko4cab7f32010-07-18 17:41:17 +0200141 * [-aAenv] [-C FILE or DIR] [-b BASE] [-F System.map] [VERSION] [MODFILES]...
142 * -a --all
143 * Probe all modules. Default if no MODFILES.
144 * -A --quick
145 * Check modules.dep's mtime against module files' mtimes.
146 * -b --basedir BASE
147 * Use $BASE/lib/modules/VERSION
148 * -C --config FILE or DIR
149 * Path to /etc/depmod.conf or /etc/depmod.d/
150 * -e --errsyms
Lauri Kasanen5a5db932012-02-19 16:33:37 +0100151 * When combined with the -F option, this reports any symbols
Denys Vlasenko4cab7f32010-07-18 17:41:17 +0200152 * which are not supplied by other modules or kernel.
153 * -F --filesyms System.map
154 * -n --dry-run
155 * Print modules.dep etc to standard output
156 * -v --verbose
157 * Print to stdout all the symbols each module depends on
158 * and the module's file name which provides that symbol.
159 * -r No-op
Pascal Bellardcadf9012010-10-18 00:51:16 +0200160 * -u No-op
161 * -q No-op
Denys Vlasenko4cab7f32010-07-18 17:41:17 +0200162 *
Lauri Kasanen5a5db932012-02-19 16:33:37 +0100163 * So far we only support: [-n] [-b BASE] [VERSION] [MODFILES]...
164 * Accepted but ignored:
165 * -aAe
166 * -F System.map
167 * -C FILE/DIR
168 *
169 * Not accepted: -v
Denys Vlasenko4cab7f32010-07-18 17:41:17 +0200170 */
171enum {
172 //OPT_a = (1 << 0), /* All modules, ignore mods in argv */
173 //OPT_A = (1 << 1), /* Only emit .ko that are newer than modules.dep file */
174 OPT_b = (1 << 2), /* base directory when modules are in staging area */
175 //OPT_e = (1 << 3), /* with -F, print unresolved symbols */
176 //OPT_F = (1 << 4), /* System.map that contains the symbols */
177 OPT_n = (1 << 5), /* dry-run, print to stdout only */
Pascal Bellardcadf9012010-10-18 00:51:16 +0200178 OPT_r = (1 << 6), /* Compat dummy. Linux Makefile uses it */
179 OPT_u = (1 << 7), /* -u,--unresolved-error: ignored */
180 OPT_q = (1 << 8), /* -q,--quiet: ignored */
181 OPT_C = (1 << 9), /* -C,--config etc_modules_conf: ignored */
Denys Vlasenko4cab7f32010-07-18 17:41:17 +0200182};
183
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +0000184int depmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000185int depmod_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +0000186{
Denys Vlasenko4cab7f32010-07-18 17:41:17 +0200187 module_info *modules, *m, *dep;
Denis Vlasenko35261152009-01-12 22:12:22 +0000188 const char *moddir_base = "/";
189 char *moddir, *version;
190 struct utsname uts;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000191 int tmp;
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +0000192
Pascal Bellardcadf9012010-10-18 00:51:16 +0200193 getopt32(argv, "aAb:eF:nruqC:", &moddir_base, NULL, NULL);
Bernhard Reutner-Fischer6bb55cf2008-05-26 17:04:01 +0000194 argv += optind;
Bernhard Reutner-Fischerc21d9c72008-05-28 10:35:51 +0000195
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000196 /* goto modules location */
Denis Vlasenko35261152009-01-12 22:12:22 +0000197 xchdir(moddir_base);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000198
199 /* If a version is provided, then that kernel version's module directory
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +0000200 * is used, rather than the current kernel version (as returned by
201 * "uname -r"). */
Denys Vlasenko4cab7f32010-07-18 17:41:17 +0200202 if (*argv && sscanf(*argv, "%u.%u.%u", &tmp, &tmp, &tmp) == 3) {
Denis Vlasenko35261152009-01-12 22:12:22 +0000203 version = *argv++;
Bernhard Reutner-Fischer1ea25682008-05-26 21:33:05 +0000204 } else {
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000205 uname(&uts);
Denis Vlasenko35261152009-01-12 22:12:22 +0000206 version = uts.release;
Bernhard Reutner-Fischer6bb55cf2008-05-26 17:04:01 +0000207 }
Denis Vlasenko35261152009-01-12 22:12:22 +0000208 moddir = concat_path_file(&CONFIG_DEFAULT_MODULES_DIR[1], version);
Denis Vlasenko35261152009-01-12 22:12:22 +0000209 xchdir(moddir);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000210 if (ENABLE_FEATURE_CLEAN_UP)
Denis Vlasenko35261152009-01-12 22:12:22 +0000211 free(moddir);
Bernhard Reutner-Fischer6bb55cf2008-05-26 17:04:01 +0000212
Denys Vlasenko4cab7f32010-07-18 17:41:17 +0200213 /* Scan modules */
214 modules = NULL;
215 if (*argv) {
216 do {
217 parse_module(*argv, /*sb (unused):*/ NULL, &modules, 0);
218 } while (*++argv);
219 } else {
220 recursive_action(".", ACTION_RECURSE,
Denys Vlasenko69675782013-01-14 01:34:48 +0100221 parse_module, NULL, &modules, 0);
Denys Vlasenko4cab7f32010-07-18 17:41:17 +0200222 }
223
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000224 /* Generate dependency and alias files */
Denys Vlasenko4cab7f32010-07-18 17:41:17 +0200225 if (!(option_mask32 & OPT_n))
Denis Vlasenko35261152009-01-12 22:12:22 +0000226 xfreopen_write(CONFIG_DEFAULT_DEPMOD_FILE, stdout);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000227 for (m = modules; m != NULL; m = m->next) {
228 printf("%s:", m->name);
229
230 order_dep_list(modules, m, m->dependencies);
231 while (m->dnext != m) {
232 dep = m->dnext;
233 printf(" %s", dep->name);
234
235 /* unlink current entry */
236 dep->dnext->dprev = dep->dprev;
237 dep->dprev->dnext = dep->dnext;
238 dep->dnext = dep->dprev = dep;
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +0000239 }
Denis Vlasenko35261152009-01-12 22:12:22 +0000240 bb_putchar('\n');
Bernhard Reutner-Fischerc2741e12008-06-02 18:59:16 +0000241 }
242
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000243#if ENABLE_FEATURE_MODUTILS_ALIAS
Denys Vlasenko4cab7f32010-07-18 17:41:17 +0200244 if (!(option_mask32 & OPT_n))
Denis Vlasenko35261152009-01-12 22:12:22 +0000245 xfreopen_write("modules.alias", stdout);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000246 for (m = modules; m != NULL; m = m->next) {
Denys Vlasenko08364da2015-01-07 14:39:01 +0100247 char modname[MODULE_NAME_LEN];
Denys Vlasenko16bda3b2009-05-18 13:08:04 +0200248 const char *fname = bb_basename(m->name);
Denys Vlasenko08364da2015-01-07 14:39:01 +0100249 filename2modname(fname, modname);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000250 while (m->aliases) {
Denys Vlasenkocc70b6f2015-01-24 22:30:30 +0100251 /*
252 * Last word used to be a basename
253 * (filename with path and .ko.* stripped)
254 * at the time of module-init-tools 3.4.
255 * kmod v.12 uses module name, i.e., s/-/_/g.
256 */
Denys Vlasenko08364da2015-01-07 14:39:01 +0100257 printf("alias %s %s\n",
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000258 (char*)llist_pop(&m->aliases),
Denys Vlasenko08364da2015-01-07 14:39:01 +0100259 modname);
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +0000260 }
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000261 }
262#endif
263#if ENABLE_FEATURE_MODUTILS_SYMBOLS
Denys Vlasenko4cab7f32010-07-18 17:41:17 +0200264 if (!(option_mask32 & OPT_n))
Denis Vlasenko35261152009-01-12 22:12:22 +0000265 xfreopen_write("modules.symbols", stdout);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000266 for (m = modules; m != NULL; m = m->next) {
Denys Vlasenko08364da2015-01-07 14:39:01 +0100267 char modname[MODULE_NAME_LEN];
Denys Vlasenko16bda3b2009-05-18 13:08:04 +0200268 const char *fname = bb_basename(m->name);
Denys Vlasenko08364da2015-01-07 14:39:01 +0100269 filename2modname(fname, modname);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000270 while (m->symbols) {
Denys Vlasenko08364da2015-01-07 14:39:01 +0100271 printf("alias symbol:%s %s\n",
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000272 (char*)llist_pop(&m->symbols),
Denys Vlasenko08364da2015-01-07 14:39:01 +0100273 modname);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000274 }
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +0000275 }
Bernhard Reutner-Fischerc2741e12008-06-02 18:59:16 +0000276#endif
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +0000277
Bernhard Reutner-Fischerdc5d7fe2008-05-26 13:30:41 +0000278 if (ENABLE_FEATURE_CLEAN_UP) {
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000279 while (modules) {
280 module_info *old = modules;
281 modules = modules->next;
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +0000282 free(old->name);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000283 free(old->modname);
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +0000284 free(old);
285 }
Bernhard Reutner-Fischerdc5d7fe2008-05-26 13:30:41 +0000286 }
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000287
288 return EXIT_SUCCESS;
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +0000289}