blob: f6c0bf33aee96566e943547ee4f04f88beb931e7 [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 Vlasenko1a5e11c2010-10-16 01:56:41 +020013//usage:#if !ENABLE_MODPROBE_SMALL
14//usage:#define depmod_trivial_usage NOUSAGE_STR
15//usage:#define depmod_full_usage ""
16//usage:#endif
17
Denys Vlasenko043b1e52009-09-06 12:47:55 +020018#include "libbb.h"
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000019#include "modutils.h"
Denys Vlasenko043b1e52009-09-06 12:47:55 +020020#include <sys/utsname.h> /* uname() */
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000021
Bernhard Reutner-Fischercf180102008-05-26 15:12:01 +000022/*
23 * Theory of operation:
24 * - iterate over all modules and record their full path
25 * - iterate over all modules looking for "depends=" entries
26 * for each depends, look through our list of full paths and emit if found
27 */
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +000028
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000029typedef struct module_info {
30 struct module_info *next;
31 char *name, *modname;
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +000032 llist_t *dependencies;
Bernhard Reutner-Fischerc2741e12008-06-02 18:59:16 +000033 llist_t *aliases;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000034 llist_t *symbols;
35 struct module_info *dnext, *dprev;
36} module_info;
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +000037
Denys Vlasenko16bda3b2009-05-18 13:08:04 +020038static int FAST_FUNC parse_module(const char *fname, struct stat *sb UNUSED_PARAM,
39 void *data, int depth UNUSED_PARAM)
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +000040{
Denys Vlasenko16bda3b2009-05-18 13:08:04 +020041 char modname[MODULE_NAME_LEN];
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000042 module_info **first = (module_info **) data;
43 char *image, *ptr;
44 module_info *info;
Denys Vlasenko16bda3b2009-05-18 13:08:04 +020045 /* Arbitrary. Was sb->st_size, but that breaks .gz etc */
46 size_t len = (64*1024*1024 - 4096);
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +000047
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000048 if (strrstr(fname, ".ko") == NULL)
49 return TRUE;
Bernhard Reutner-Fischer634b0222008-05-28 14:20:20 +000050
Denis Vlasenkofc668922008-11-06 02:32:31 +000051 image = xmalloc_open_zipped_read_close(fname, &len);
Denys Vlasenko16bda3b2009-05-18 13:08:04 +020052 info = xzalloc(sizeof(*info));
Bernhard Reutner-Fischerc2741e12008-06-02 18:59:16 +000053
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000054 info->next = *first;
55 *first = info;
56
57 info->dnext = info->dprev = info;
Denys Vlasenko4cab7f32010-07-18 17:41:17 +020058 info->name = xstrdup(fname + 2); /* skip "./" */
Denys Vlasenko16bda3b2009-05-18 13:08:04 +020059 info->modname = xstrdup(filename2modname(fname, modname));
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000060 for (ptr = image; ptr < image + len - 10; ptr++) {
61 if (strncmp(ptr, "depends=", 8) == 0) {
62 char *u;
63
64 ptr += 8;
65 for (u = ptr; *u; u++)
66 if (*u == '-')
67 *u = '_';
68 ptr += string_to_llist(ptr, &info->dependencies, ",");
Denys Vlasenko16bda3b2009-05-18 13:08:04 +020069 } else if (ENABLE_FEATURE_MODUTILS_ALIAS
70 && strncmp(ptr, "alias=", 6) == 0
71 ) {
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000072 llist_add_to(&info->aliases, xstrdup(ptr + 6));
73 ptr += strlen(ptr);
Denys Vlasenko16bda3b2009-05-18 13:08:04 +020074 } else if (ENABLE_FEATURE_MODUTILS_SYMBOLS
75 && strncmp(ptr, "__ksymtab_", 10) == 0
76 ) {
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000077 ptr += 10;
Denys Vlasenko6b9f1632010-01-28 02:24:24 +010078 if (strncmp(ptr, "gpl", 3) == 0
79 || strcmp(ptr, "strings") == 0
80 ) {
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000081 continue;
Denys Vlasenko6b9f1632010-01-28 02:24:24 +010082 }
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000083 llist_add_to(&info->symbols, xstrdup(ptr));
84 ptr += strlen(ptr);
85 }
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +000086 }
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000087 free(image);
88
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +000089 return TRUE;
90}
91
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000092static module_info *find_module(module_info *modules, const char *modname)
93{
94 module_info *m;
95
96 for (m = modules; m != NULL; m = m->next)
97 if (strcmp(m->modname, modname) == 0)
98 return m;
99 return NULL;
100}
101
102static void order_dep_list(module_info *modules, module_info *start,
103 llist_t *add)
104{
105 module_info *m;
106 llist_t *n;
107
108 for (n = add; n != NULL; n = n->link) {
109 m = find_module(modules, n->data);
110 if (m == NULL)
111 continue;
112
113 /* unlink current entry */
114 m->dnext->dprev = m->dprev;
115 m->dprev->dnext = m->dnext;
116
117 /* and add it to tail */
118 m->dnext = start;
119 m->dprev = start->dprev;
120 start->dprev->dnext = m;
121 start->dprev = m;
122
123 /* recurse */
124 order_dep_list(modules, start, m->dependencies);
125 }
126}
127
Denis Vlasenko35261152009-01-12 22:12:22 +0000128static void xfreopen_write(const char *file, FILE *f)
129{
130 if (freopen(file, "w", f) == NULL)
131 bb_perror_msg_and_die("can't open '%s'", file);
132}
133
Denys Vlasenko4cab7f32010-07-18 17:41:17 +0200134/* Usage:
135 * [-aAenv] [-C FILE or DIR] [-b BASE] [-F System.map] [VERSION] [MODFILES]...
136 * -a --all
137 * Probe all modules. Default if no MODFILES.
138 * -A --quick
139 * Check modules.dep's mtime against module files' mtimes.
140 * -b --basedir BASE
141 * Use $BASE/lib/modules/VERSION
142 * -C --config FILE or DIR
143 * Path to /etc/depmod.conf or /etc/depmod.d/
144 * -e --errsyms
145 * When combined with the -F option, this reports any symbols which
146 * which are not supplied by other modules or kernel.
147 * -F --filesyms System.map
148 * -n --dry-run
149 * Print modules.dep etc to standard output
150 * -v --verbose
151 * Print to stdout all the symbols each module depends on
152 * and the module's file name which provides that symbol.
153 * -r No-op
Pascal Bellardcadf9012010-10-18 00:51:16 +0200154 * -u No-op
155 * -q No-op
Denys Vlasenko4cab7f32010-07-18 17:41:17 +0200156 *
157 * So far we only support: [-rn] [-b BASE] [VERSION] [MODFILES]...
158 * -aAeF are accepted but ignored. -vC are not accepted.
159 */
160enum {
161 //OPT_a = (1 << 0), /* All modules, ignore mods in argv */
162 //OPT_A = (1 << 1), /* Only emit .ko that are newer than modules.dep file */
163 OPT_b = (1 << 2), /* base directory when modules are in staging area */
164 //OPT_e = (1 << 3), /* with -F, print unresolved symbols */
165 //OPT_F = (1 << 4), /* System.map that contains the symbols */
166 OPT_n = (1 << 5), /* dry-run, print to stdout only */
Pascal Bellardcadf9012010-10-18 00:51:16 +0200167 OPT_r = (1 << 6), /* Compat dummy. Linux Makefile uses it */
168 OPT_u = (1 << 7), /* -u,--unresolved-error: ignored */
169 OPT_q = (1 << 8), /* -q,--quiet: ignored */
170 OPT_C = (1 << 9), /* -C,--config etc_modules_conf: ignored */
Denys Vlasenko4cab7f32010-07-18 17:41:17 +0200171};
172
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +0000173int depmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000174int depmod_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +0000175{
Denys Vlasenko4cab7f32010-07-18 17:41:17 +0200176 module_info *modules, *m, *dep;
Denis Vlasenko35261152009-01-12 22:12:22 +0000177 const char *moddir_base = "/";
178 char *moddir, *version;
179 struct utsname uts;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000180 int tmp;
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +0000181
Pascal Bellardcadf9012010-10-18 00:51:16 +0200182 getopt32(argv, "aAb:eF:nruqC:", &moddir_base, NULL, NULL);
Bernhard Reutner-Fischer6bb55cf2008-05-26 17:04:01 +0000183 argv += optind;
Bernhard Reutner-Fischerc21d9c72008-05-28 10:35:51 +0000184
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000185 /* goto modules location */
Denis Vlasenko35261152009-01-12 22:12:22 +0000186 xchdir(moddir_base);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000187
188 /* If a version is provided, then that kernel version's module directory
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +0000189 * is used, rather than the current kernel version (as returned by
190 * "uname -r"). */
Denys Vlasenko4cab7f32010-07-18 17:41:17 +0200191 if (*argv && sscanf(*argv, "%u.%u.%u", &tmp, &tmp, &tmp) == 3) {
Denis Vlasenko35261152009-01-12 22:12:22 +0000192 version = *argv++;
Bernhard Reutner-Fischer1ea25682008-05-26 21:33:05 +0000193 } else {
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000194 uname(&uts);
Denis Vlasenko35261152009-01-12 22:12:22 +0000195 version = uts.release;
Bernhard Reutner-Fischer6bb55cf2008-05-26 17:04:01 +0000196 }
Denis Vlasenko35261152009-01-12 22:12:22 +0000197 moddir = concat_path_file(&CONFIG_DEFAULT_MODULES_DIR[1], version);
Denis Vlasenko35261152009-01-12 22:12:22 +0000198 xchdir(moddir);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000199 if (ENABLE_FEATURE_CLEAN_UP)
Denis Vlasenko35261152009-01-12 22:12:22 +0000200 free(moddir);
Bernhard Reutner-Fischer6bb55cf2008-05-26 17:04:01 +0000201
Denys Vlasenko4cab7f32010-07-18 17:41:17 +0200202 /* Scan modules */
203 modules = NULL;
204 if (*argv) {
205 do {
206 parse_module(*argv, /*sb (unused):*/ NULL, &modules, 0);
207 } while (*++argv);
208 } else {
209 recursive_action(".", ACTION_RECURSE,
210 parse_module, NULL, &modules, 0);
211 }
212
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000213 /* Generate dependency and alias files */
Denys Vlasenko4cab7f32010-07-18 17:41:17 +0200214 if (!(option_mask32 & OPT_n))
Denis Vlasenko35261152009-01-12 22:12:22 +0000215 xfreopen_write(CONFIG_DEFAULT_DEPMOD_FILE, stdout);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000216 for (m = modules; m != NULL; m = m->next) {
217 printf("%s:", m->name);
218
219 order_dep_list(modules, m, m->dependencies);
220 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);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000235 for (m = modules; m != NULL; m = m->next) {
Denys Vlasenko16bda3b2009-05-18 13:08:04 +0200236 const char *fname = bb_basename(m->name);
237 int fnlen = strchrnul(fname, '.') - fname;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000238 while (m->aliases) {
Denys Vlasenko16bda3b2009-05-18 13:08:04 +0200239 /* Last word can well be m->modname instead,
240 * but depmod from module-init-tools 3.4
241 * uses module basename, i.e., no s/-/_/g.
242 * (pathname and .ko.* are still stripped)
243 * Mimicking that... */
244 printf("alias %s %.*s\n",
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000245 (char*)llist_pop(&m->aliases),
Denys Vlasenko16bda3b2009-05-18 13:08:04 +0200246 fnlen, fname);
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +0000247 }
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000248 }
249#endif
250#if ENABLE_FEATURE_MODUTILS_SYMBOLS
Denys Vlasenko4cab7f32010-07-18 17:41:17 +0200251 if (!(option_mask32 & OPT_n))
Denis Vlasenko35261152009-01-12 22:12:22 +0000252 xfreopen_write("modules.symbols", stdout);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000253 for (m = modules; m != NULL; m = m->next) {
Denys Vlasenko16bda3b2009-05-18 13:08:04 +0200254 const char *fname = bb_basename(m->name);
255 int fnlen = strchrnul(fname, '.') - fname;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000256 while (m->symbols) {
Denys Vlasenko16bda3b2009-05-18 13:08:04 +0200257 printf("alias symbol:%s %.*s\n",
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000258 (char*)llist_pop(&m->symbols),
Denys Vlasenko16bda3b2009-05-18 13:08:04 +0200259 fnlen, fname);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000260 }
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +0000261 }
Bernhard Reutner-Fischerc2741e12008-06-02 18:59:16 +0000262#endif
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +0000263
Bernhard Reutner-Fischerdc5d7fe2008-05-26 13:30:41 +0000264 if (ENABLE_FEATURE_CLEAN_UP) {
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000265 while (modules) {
266 module_info *old = modules;
267 modules = modules->next;
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +0000268 free(old->name);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000269 free(old->modname);
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +0000270 free(old);
271 }
Bernhard Reutner-Fischerdc5d7fe2008-05-26 13:30:41 +0000272 }
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000273
274 return EXIT_SUCCESS;
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +0000275}