f6c0bf33aee96566e943547ee4f04f88beb931e7
[platform/upstream/busybox.git] / modutils / depmod.c
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
11 //applet:IF_DEPMOD(APPLET(depmod, BB_DIR_SBIN, BB_SUID_DROP))
12
13 //usage:#if !ENABLE_MODPROBE_SMALL
14 //usage:#define depmod_trivial_usage NOUSAGE_STR
15 //usage:#define depmod_full_usage ""
16 //usage:#endif
17
18 #include "libbb.h"
19 #include "modutils.h"
20 #include <sys/utsname.h> /* uname() */
21
22 /*
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  */
28
29 typedef struct module_info {
30         struct module_info *next;
31         char *name, *modname;
32         llist_t *dependencies;
33         llist_t *aliases;
34         llist_t *symbols;
35         struct module_info *dnext, *dprev;
36 } module_info;
37
38 static int FAST_FUNC parse_module(const char *fname, struct stat *sb UNUSED_PARAM,
39                                   void *data, int depth UNUSED_PARAM)
40 {
41         char modname[MODULE_NAME_LEN];
42         module_info **first = (module_info **) data;
43         char *image, *ptr;
44         module_info *info;
45         /* Arbitrary. Was sb->st_size, but that breaks .gz etc */
46         size_t len = (64*1024*1024 - 4096);
47
48         if (strrstr(fname, ".ko") == NULL)
49                 return TRUE;
50
51         image = xmalloc_open_zipped_read_close(fname, &len);
52         info = xzalloc(sizeof(*info));
53
54         info->next = *first;
55         *first = info;
56
57         info->dnext = info->dprev = info;
58         info->name = xstrdup(fname + 2); /* skip "./" */
59         info->modname = xstrdup(filename2modname(fname, modname));
60         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, ",");
69                 } else if (ENABLE_FEATURE_MODUTILS_ALIAS
70                  && strncmp(ptr, "alias=", 6) == 0
71                 ) {
72                         llist_add_to(&info->aliases, xstrdup(ptr + 6));
73                         ptr += strlen(ptr);
74                 } else if (ENABLE_FEATURE_MODUTILS_SYMBOLS
75                  && strncmp(ptr, "__ksymtab_", 10) == 0
76                 ) {
77                         ptr += 10;
78                         if (strncmp(ptr, "gpl", 3) == 0
79                          || strcmp(ptr, "strings") == 0
80                         ) {
81                                 continue;
82                         }
83                         llist_add_to(&info->symbols, xstrdup(ptr));
84                         ptr += strlen(ptr);
85                 }
86         }
87         free(image);
88
89         return TRUE;
90 }
91
92 static 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
102 static 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
128 static 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
134 /* 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
154  *      -u      No-op
155  *      -q      No-op
156  *
157  * So far we only support: [-rn] [-b BASE] [VERSION] [MODFILES]...
158  * -aAeF are accepted but ignored. -vC are not accepted.
159  */
160 enum {
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 */
167         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 */
171 };
172
173 int depmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
174 int depmod_main(int argc UNUSED_PARAM, char **argv)
175 {
176         module_info *modules, *m, *dep;
177         const char *moddir_base = "/";
178         char *moddir, *version;
179         struct utsname uts;
180         int tmp;
181
182         getopt32(argv, "aAb:eF:nruqC:", &moddir_base, NULL, NULL);
183         argv += optind;
184
185         /* goto modules location */
186         xchdir(moddir_base);
187
188         /* If a version is provided, then that kernel version's module directory
189          * is used, rather than the current kernel version (as returned by
190          * "uname -r").  */
191         if (*argv && sscanf(*argv, "%u.%u.%u", &tmp, &tmp, &tmp) == 3) {
192                 version = *argv++;
193         } else {
194                 uname(&uts);
195                 version = uts.release;
196         }
197         moddir = concat_path_file(&CONFIG_DEFAULT_MODULES_DIR[1], version);
198         xchdir(moddir);
199         if (ENABLE_FEATURE_CLEAN_UP)
200                 free(moddir);
201
202         /* 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
213         /* Generate dependency and alias files */
214         if (!(option_mask32 & OPT_n))
215                 xfreopen_write(CONFIG_DEFAULT_DEPMOD_FILE, stdout);
216         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;
228                 }
229                 bb_putchar('\n');
230         }
231
232 #if ENABLE_FEATURE_MODUTILS_ALIAS
233         if (!(option_mask32 & OPT_n))
234                 xfreopen_write("modules.alias", stdout);
235         for (m = modules; m != NULL; m = m->next) {
236                 const char *fname = bb_basename(m->name);
237                 int fnlen = strchrnul(fname, '.') - fname;
238                 while (m->aliases) {
239                         /* 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",
245                                 (char*)llist_pop(&m->aliases),
246                                 fnlen, fname);
247                 }
248         }
249 #endif
250 #if ENABLE_FEATURE_MODUTILS_SYMBOLS
251         if (!(option_mask32 & OPT_n))
252                 xfreopen_write("modules.symbols", stdout);
253         for (m = modules; m != NULL; m = m->next) {
254                 const char *fname = bb_basename(m->name);
255                 int fnlen = strchrnul(fname, '.') - fname;
256                 while (m->symbols) {
257                         printf("alias symbol:%s %.*s\n",
258                                 (char*)llist_pop(&m->symbols),
259                                 fnlen, fname);
260                 }
261         }
262 #endif
263
264         if (ENABLE_FEATURE_CLEAN_UP) {
265                 while (modules) {
266                         module_info *old = modules;
267                         modules = modules->next;
268                         free(old->name);
269                         free(old->modname);
270                         free(old);
271                 }
272         }
273
274         return EXIT_SUCCESS;
275 }