modutils/*: move applet bits into corresponding *.c files
[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 #include "libbb.h"
14 #include "modutils.h"
15 #include <sys/utsname.h> /* uname() */
16
17 /*
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  */
23
24 typedef struct module_info {
25         struct module_info *next;
26         char *name, *modname;
27         llist_t *dependencies;
28         llist_t *aliases;
29         llist_t *symbols;
30         struct module_info *dnext, *dprev;
31 } module_info;
32
33 static int FAST_FUNC parse_module(const char *fname, struct stat *sb UNUSED_PARAM,
34                                   void *data, int depth UNUSED_PARAM)
35 {
36         char modname[MODULE_NAME_LEN];
37         module_info **first = (module_info **) data;
38         char *image, *ptr;
39         module_info *info;
40         /* Arbitrary. Was sb->st_size, but that breaks .gz etc */
41         size_t len = (64*1024*1024 - 4096);
42
43         if (strrstr(fname, ".ko") == NULL)
44                 return TRUE;
45
46         image = xmalloc_open_zipped_read_close(fname, &len);
47         info = xzalloc(sizeof(*info));
48
49         info->next = *first;
50         *first = info;
51
52         info->dnext = info->dprev = info;
53         info->name = xstrdup(fname + 2); /* skip "./" */
54         info->modname = xstrdup(filename2modname(fname, modname));
55         for (ptr = image; ptr < image + len - 10; ptr++) {
56                 if (strncmp(ptr, "depends=", 8) == 0) {
57                         char *u;
58
59                         ptr += 8;
60                         for (u = ptr; *u; u++)
61                                 if (*u == '-')
62                                         *u = '_';
63                         ptr += string_to_llist(ptr, &info->dependencies, ",");
64                 } else if (ENABLE_FEATURE_MODUTILS_ALIAS
65                  && strncmp(ptr, "alias=", 6) == 0
66                 ) {
67                         llist_add_to(&info->aliases, xstrdup(ptr + 6));
68                         ptr += strlen(ptr);
69                 } else if (ENABLE_FEATURE_MODUTILS_SYMBOLS
70                  && strncmp(ptr, "__ksymtab_", 10) == 0
71                 ) {
72                         ptr += 10;
73                         if (strncmp(ptr, "gpl", 3) == 0
74                          || strcmp(ptr, "strings") == 0
75                         ) {
76                                 continue;
77                         }
78                         llist_add_to(&info->symbols, xstrdup(ptr));
79                         ptr += strlen(ptr);
80                 }
81         }
82         free(image);
83
84         return TRUE;
85 }
86
87 static module_info *find_module(module_info *modules, const char *modname)
88 {
89         module_info *m;
90
91         for (m = modules; m != NULL; m = m->next)
92                 if (strcmp(m->modname, modname) == 0)
93                         return m;
94         return NULL;
95 }
96
97 static void order_dep_list(module_info *modules, module_info *start,
98                            llist_t *add)
99 {
100         module_info *m;
101         llist_t *n;
102
103         for (n = add; n != NULL; n = n->link) {
104                 m = find_module(modules, n->data);
105                 if (m == NULL)
106                         continue;
107
108                 /* unlink current entry */
109                 m->dnext->dprev = m->dprev;
110                 m->dprev->dnext = m->dnext;
111
112                 /* and add it to tail */
113                 m->dnext = start;
114                 m->dprev = start->dprev;
115                 start->dprev->dnext = m;
116                 start->dprev = m;
117
118                 /* recurse */
119                 order_dep_list(modules, start, m->dependencies);
120         }
121 }
122
123 static void xfreopen_write(const char *file, FILE *f)
124 {
125         if (freopen(file, "w", f) == NULL)
126                 bb_perror_msg_and_die("can't open '%s'", file);
127 }
128
129 /* Usage:
130  * [-aAenv] [-C FILE or DIR] [-b BASE] [-F System.map] [VERSION] [MODFILES]...
131  *      -a --all
132  *              Probe all modules. Default if no MODFILES.
133  *      -A --quick
134  *              Check modules.dep's mtime against module files' mtimes.
135  *      -b --basedir BASE
136  *              Use $BASE/lib/modules/VERSION
137  *      -C --config FILE or DIR
138  *              Path to /etc/depmod.conf or /etc/depmod.d/
139  *      -e --errsyms
140  *              When combined with the -F option, this reports any symbols which
141  *              which are not supplied by other modules or kernel.
142  *      -F --filesyms System.map
143  *      -n --dry-run
144  *              Print modules.dep etc to standard output
145  *      -v --verbose
146  *              Print to stdout all the symbols each module depends on
147  *              and the module's file name which provides that symbol.
148  *      -r      No-op
149  *
150  * So far we only support: [-rn] [-b BASE] [VERSION] [MODFILES]...
151  * -aAeF are accepted but ignored. -vC are not accepted.
152  */
153 enum {
154         //OPT_a = (1 << 0), /* All modules, ignore mods in argv */
155         //OPT_A = (1 << 1), /* Only emit .ko that are newer than modules.dep file */
156         OPT_b = (1 << 2), /* base directory when modules are in staging area */
157         //OPT_e = (1 << 3), /* with -F, print unresolved symbols */
158         //OPT_F = (1 << 4), /* System.map that contains the symbols */
159         OPT_n = (1 << 5), /* dry-run, print to stdout only */
160         OPT_r = (1 << 6)  /* Compat dummy. Linux Makefile uses it */
161 };
162
163 int depmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
164 int depmod_main(int argc UNUSED_PARAM, char **argv)
165 {
166         module_info *modules, *m, *dep;
167         const char *moddir_base = "/";
168         char *moddir, *version;
169         struct utsname uts;
170         int tmp;
171
172         getopt32(argv, "aAb:eF:nr", &moddir_base, NULL);
173         argv += optind;
174
175         /* goto modules location */
176         xchdir(moddir_base);
177
178         /* If a version is provided, then that kernel version's module directory
179          * is used, rather than the current kernel version (as returned by
180          * "uname -r").  */
181         if (*argv && sscanf(*argv, "%u.%u.%u", &tmp, &tmp, &tmp) == 3) {
182                 version = *argv++;
183         } else {
184                 uname(&uts);
185                 version = uts.release;
186         }
187         moddir = concat_path_file(&CONFIG_DEFAULT_MODULES_DIR[1], version);
188         xchdir(moddir);
189         if (ENABLE_FEATURE_CLEAN_UP)
190                 free(moddir);
191
192         /* Scan modules */
193         modules = NULL;
194         if (*argv) {
195                 do {
196                         parse_module(*argv, /*sb (unused):*/ NULL, &modules, 0);
197                 } while (*++argv);
198         } else {
199                 recursive_action(".", ACTION_RECURSE,
200                                  parse_module, NULL, &modules, 0);
201         }
202
203         /* Generate dependency and alias files */
204         if (!(option_mask32 & OPT_n))
205                 xfreopen_write(CONFIG_DEFAULT_DEPMOD_FILE, stdout);
206         for (m = modules; m != NULL; m = m->next) {
207                 printf("%s:", m->name);
208
209                 order_dep_list(modules, m, m->dependencies);
210                 while (m->dnext != m) {
211                         dep = m->dnext;
212                         printf(" %s", dep->name);
213
214                         /* unlink current entry */
215                         dep->dnext->dprev = dep->dprev;
216                         dep->dprev->dnext = dep->dnext;
217                         dep->dnext = dep->dprev = dep;
218                 }
219                 bb_putchar('\n');
220         }
221
222 #if ENABLE_FEATURE_MODUTILS_ALIAS
223         if (!(option_mask32 & OPT_n))
224                 xfreopen_write("modules.alias", stdout);
225         for (m = modules; m != NULL; m = m->next) {
226                 const char *fname = bb_basename(m->name);
227                 int fnlen = strchrnul(fname, '.') - fname;
228                 while (m->aliases) {
229                         /* Last word can well be m->modname instead,
230                          * but depmod from module-init-tools 3.4
231                          * uses module basename, i.e., no s/-/_/g.
232                          * (pathname and .ko.* are still stripped)
233                          * Mimicking that... */
234                         printf("alias %s %.*s\n",
235                                 (char*)llist_pop(&m->aliases),
236                                 fnlen, fname);
237                 }
238         }
239 #endif
240 #if ENABLE_FEATURE_MODUTILS_SYMBOLS
241         if (!(option_mask32 & OPT_n))
242                 xfreopen_write("modules.symbols", stdout);
243         for (m = modules; m != NULL; m = m->next) {
244                 const char *fname = bb_basename(m->name);
245                 int fnlen = strchrnul(fname, '.') - fname;
246                 while (m->symbols) {
247                         printf("alias symbol:%s %.*s\n",
248                                 (char*)llist_pop(&m->symbols),
249                                 fnlen, fname);
250                 }
251         }
252 #endif
253
254         if (ENABLE_FEATURE_CLEAN_UP) {
255                 while (modules) {
256                         module_info *old = modules;
257                         modules = modules->next;
258                         free(old->name);
259                         free(old->modname);
260                         free(old);
261                 }
262         }
263
264         return EXIT_SUCCESS;
265 }