8caa3769c68ac7e35995d8893920ecd0d161c722
[oweals/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 tarball for details.
9  */
10
11 #undef _GNU_SOURCE
12 #define _GNU_SOURCE
13 #include <libbb.h>
14 #include <sys/utsname.h> /* uname() */
15 #include "modutils.h"
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 enum {
34         ARG_a = (1<<0), /* All modules, ignore mods in argv */
35         ARG_A = (1<<1), /* Only emit .ko that are newer than modules.dep file */
36         ARG_b = (1<<2), /* base directory when modules are in staging area */
37         ARG_e = (1<<3), /* with -F, print unresolved symbols */
38         ARG_F = (1<<4), /* System.map that contains the symbols */
39         ARG_n = (1<<5)  /* dry-run, print to stdout only */
40         ARG_r = (1<<6)  /* Compat dummy. Linux Makefile uses it */
41 };
42
43 static int FAST_FUNC parse_module(const char *fname, struct stat *sb,
44                                   void *data, int UNUSED_PARAM depth)
45 {
46         module_info **first = (module_info **) data;
47         char *image, *ptr;
48         module_info *info;
49         size_t len = sb->st_size;
50
51         if (strrstr(fname, ".ko") == NULL)
52                 return TRUE;
53
54         image = xmalloc_open_zipped_read_close(fname, &len);
55         info = xzalloc(sizeof(module_info));
56
57         info->next = *first;
58         *first = info;
59
60         info->dnext = info->dprev = info;
61         info->name = xasprintf("/%s", fname);
62         info->modname = filename2modname(fname, NULL);
63         for (ptr = image; ptr < image + len - 10; ptr++) {
64                 if (strncmp(ptr, "depends=", 8) == 0) {
65                         char *u;
66
67                         ptr += 8;
68                         for (u = ptr; *u; u++)
69                                 if (*u == '-')
70                                         *u = '_';
71                         ptr += string_to_llist(ptr, &info->dependencies, ",");
72                 } else if (ENABLE_FEATURE_MODUTILS_ALIAS &&
73                            strncmp(ptr, "alias=", 6) == 0) {
74                         llist_add_to(&info->aliases, xstrdup(ptr + 6));
75                         ptr += strlen(ptr);
76                 } else if (ENABLE_FEATURE_MODUTILS_SYMBOLS &&
77                            strncmp(ptr, "__ksymtab_", 10) == 0) {
78                         ptr += 10;
79                         if (strncmp(ptr, "gpl", 3) == 0 ||
80                             strcmp(ptr, "strings") == 0)
81                                 continue;
82                         llist_add_to(&info->symbols, xstrdup(ptr));
83                         ptr += strlen(ptr);
84                 }
85         }
86         free(image);
87
88         return TRUE;
89 }
90
91 static module_info *find_module(module_info *modules, const char *modname)
92 {
93         module_info *m;
94
95         for (m = modules; m != NULL; m = m->next)
96                 if (strcmp(m->modname, modname) == 0)
97                         return m;
98         return NULL;
99 }
100
101 static void order_dep_list(module_info *modules, module_info *start,
102                            llist_t *add)
103 {
104         module_info *m;
105         llist_t *n;
106
107         for (n = add; n != NULL; n = n->link) {
108                 m = find_module(modules, n->data);
109                 if (m == NULL)
110                         continue;
111
112                 /* unlink current entry */
113                 m->dnext->dprev = m->dprev;
114                 m->dprev->dnext = m->dnext;
115
116                 /* and add it to tail */
117                 m->dnext = start;
118                 m->dprev = start->dprev;
119                 start->dprev->dnext = m;
120                 start->dprev = m;
121
122                 /* recurse */
123                 order_dep_list(modules, start, m->dependencies);
124         }
125 }
126
127 static void xfreopen_write(const char *file, FILE *f)
128 {
129         if (freopen(file, "w", f) == NULL)
130                 bb_perror_msg_and_die("can't open '%s'", file);
131 }
132
133 int depmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
134 int depmod_main(int argc UNUSED_PARAM, char **argv)
135 {
136         module_info *modules = NULL, *m, *dep;
137         const char *moddir_base = "/";
138         char *moddir, *version;
139         struct utsname uts;
140         int tmp;
141
142         getopt32(argv, "aAb:eF:nr", &moddir_base, NULL);
143         argv += optind;
144
145         /* goto modules location */
146         xchdir(moddir_base);
147
148         /* If a version is provided, then that kernel version's module directory
149          * is used, rather than the current kernel version (as returned by
150          * "uname -r").  */
151         if (*argv && sscanf(*argv, "%d.%d.%d", &tmp, &tmp, &tmp) == 3) {
152                 version = *argv++;
153         } else {
154                 uname(&uts);
155                 version = uts.release;
156         }
157         moddir = concat_path_file(&CONFIG_DEFAULT_MODULES_DIR[1], version);
158
159         /* Scan modules */
160         if (*argv) {
161                 char *modfile;
162                 struct stat sb;
163                 do {
164                         modfile = concat_path_file(moddir, *argv);
165                         xstat(modfile, &sb);
166                         parse_module(modfile, &sb, &modules, 0);
167                         free(modfile);
168                 } while (*(++argv));
169         } else {
170                 recursive_action(moddir, ACTION_RECURSE,
171                                  parse_module, NULL, &modules, 0);
172         }
173
174         /* Prepare for writing out the dep files */
175         xchdir(moddir);
176         if (ENABLE_FEATURE_CLEAN_UP)
177                 free(moddir);
178
179         /* Generate dependency and alias files */
180         if (!(option_mask32 & ARG_n))
181                 xfreopen_write(CONFIG_DEFAULT_DEPMOD_FILE, stdout);
182         for (m = modules; m != NULL; m = m->next) {
183                 printf("%s:", m->name);
184
185                 order_dep_list(modules, m, m->dependencies);
186                 while (m->dnext != m) {
187                         dep = m->dnext;
188                         printf(" %s", dep->name);
189
190                         /* unlink current entry */
191                         dep->dnext->dprev = dep->dprev;
192                         dep->dprev->dnext = dep->dnext;
193                         dep->dnext = dep->dprev = dep;
194                 }
195                 bb_putchar('\n');
196         }
197
198 #if ENABLE_FEATURE_MODUTILS_ALIAS
199         if (!(option_mask32 & ARG_n))
200                 xfreopen_write("modules.alias", stdout);
201         for (m = modules; m != NULL; m = m->next) {
202                 while (m->aliases) {
203                         printf("alias %s %s\n",
204                                 (char*)llist_pop(&m->aliases),
205                                 m->modname);
206                 }
207         }
208 #endif
209 #if ENABLE_FEATURE_MODUTILS_SYMBOLS
210         if (!(option_mask32 & ARG_n))
211                 xfreopen_write("modules.symbols", stdout);
212         for (m = modules; m != NULL; m = m->next) {
213                 while (m->symbols) {
214                         printf("alias symbol:%s %s\n",
215                                 (char*)llist_pop(&m->symbols),
216                                 m->modname);
217                 }
218         }
219 #endif
220
221         if (ENABLE_FEATURE_CLEAN_UP) {
222                 while (modules) {
223                         module_info *old = modules;
224                         modules = modules->next;
225                         free(old->name);
226                         free(old->modname);
227                         free(old);
228                 }
229         }
230
231         return EXIT_SUCCESS;
232 }