blob: d128ac0ff4ead4e49826ebc06534c3343821fb5b [file] [log] [blame]
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +00001/* vi: set sw=4 ts=4: */
2/*
3 * depmod - generate modules.dep
4 * Copyright (c) 2008 Bernhard Fischer
5 *
6 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
7 */
8
9#undef _GNU_SOURCE
10#define _GNU_SOURCE
11#include <libbb.h>
12#include <sys/utsname.h> /* uname() */
Bernhard Reutner-Fischercf180102008-05-26 15:12:01 +000013/*
14 * Theory of operation:
15 * - iterate over all modules and record their full path
16 * - iterate over all modules looking for "depends=" entries
17 * for each depends, look through our list of full paths and emit if found
18 */
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +000019
20typedef struct dep_lst_t {
21 char *name;
22 llist_t *dependencies;
Bernhard Reutner-Fischerc2741e12008-06-02 18:59:16 +000023 llist_t *aliases;
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +000024 struct dep_lst_t *next;
25} dep_lst_t;
26
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +000027struct globals {
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +000028 dep_lst_t *lst; /* modules without their corresponding extension */
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +000029};
30#define G (*(struct globals*)&bb_common_bufsiz1)
31/* We have to zero it out because of NOEXEC */
32#define INIT_G() memset(&G, 0, sizeof(G))
33
Bernhard Reutner-Fischerc2741e12008-06-02 18:59:16 +000034static char* find_keyword(void *the_module, size_t len, const char * const word)
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +000035{
Bernhard Reutner-Fischerc2741e12008-06-02 18:59:16 +000036 char *ptr = the_module;
37 do {
38 /* search for the first char in word */
39 ptr = memchr(ptr, *word, len - (ptr - (char*)the_module));
40 if (ptr == NULL) /* no occurance left, done */
41 return NULL;
42 if (!strncmp(ptr, word, strlen(word))) {
43 ptr += strlen(word);
44 break;
45 }
46 ++ptr;
47 } while (1);
48 return ptr;
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +000049}
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +000050static int FAST_FUNC fileAction(const char *fname, struct stat *sb,
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000051 void UNUSED_PARAM *data, int UNUSED_PARAM depth)
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +000052{
Bernhard Reutner-Fischercf180102008-05-26 15:12:01 +000053 size_t len = sb->st_size;
Bernhard Reutner-Fischer634b0222008-05-28 14:20:20 +000054 void *the_module;
55 char *ptr;
Bernhard Reutner-Fischercf180102008-05-26 15:12:01 +000056 int fd;
57 char *depends, *deps;
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +000058 dep_lst_t *this;
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +000059
Bernhard Reutner-Fischer634b0222008-05-28 14:20:20 +000060 if (strrstr(fname, ".ko") == NULL) /* not a module */
61 goto skip;
62
Bernhard Reutner-Fischercf180102008-05-26 15:12:01 +000063/*XXX: FIXME: does not handle compressed modules!
64 * There should be a function that looks at the extension and sets up
65 * open_transformer for us.
66 */
Bernhard Reutner-Fischercf180102008-05-26 15:12:01 +000067 fd = xopen(fname, O_RDONLY);
68 the_module = mmap(NULL, len, PROT_READ, MAP_SHARED
69#if defined MAP_POPULATE
70 |MAP_POPULATE
71#endif
72 , fd, 0);
73 close(fd);
74 if (the_module == MAP_FAILED)
75 bb_perror_msg_and_die("mmap");
Bernhard Reutner-Fischerc2741e12008-06-02 18:59:16 +000076
77 this = xzalloc(sizeof(dep_lst_t));
78 this->name = xstrdup(fname);
79 this->next = G.lst;
80 G.lst = this;
81//bb_info_msg("fname='%s'", fname);
82 ptr = find_keyword(the_module, len, "depends=");
83 if (!*ptr)
84 goto d_none;
85 deps = depends = xstrdup(ptr);
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +000086//bb_info_msg(" depends='%s'", depends);
Bernhard Reutner-Fischer634b0222008-05-28 14:20:20 +000087 while (deps) {
Bernhard Reutner-Fischer634b0222008-05-28 14:20:20 +000088 ptr = strsep(&deps, ",");
Bernhard Reutner-Fischerc2741e12008-06-02 18:59:16 +000089//bb_info_msg("[%s] -> '%s'", fname, (char*)ptr);
90 llist_add_to_end(&this->dependencies, xstrdup(ptr));
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +000091 }
Bernhard Reutner-Fischercf180102008-05-26 15:12:01 +000092 free(depends);
Bernhard Reutner-Fischerc2741e12008-06-02 18:59:16 +000093 d_none:
94 if (ENABLE_FEATURE_DEPMOD_ALIAS)
95 {
96 size_t pos = 0;
97 do {
98 ptr = find_keyword(the_module + pos, len - pos, "alias=");
99 if (ptr) {
100//bb_info_msg("[%s] alias '%s'", fname, (char*)ptr);
101 llist_add_to_end(&this->aliases, xstrdup(ptr));
102 } else
103 break;
104 pos = (ptr - (char*)the_module);
105 } while (1);
106 }
Bernhard Reutner-Fischercf180102008-05-26 15:12:01 +0000107 munmap(the_module, sb->st_size);
Bernhard Reutner-Fischer634b0222008-05-28 14:20:20 +0000108 skip:
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +0000109 return TRUE;
110}
111
112int depmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000113int depmod_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +0000114{
Bernhard Reutner-Fischerc21d9c72008-05-28 10:35:51 +0000115 int ret;
Bernhard Reutner-Fischerc2741e12008-06-02 18:59:16 +0000116 size_t moddir_base_len = 0; /* length of the "-b basedir" */
Bernhard Reutner-Fischer1ea25682008-05-26 21:33:05 +0000117 char *moddir_base = NULL, *moddir, *system_map, *chp;
Bernhard Reutner-Fischer6bb55cf2008-05-26 17:04:01 +0000118 FILE *filedes = stdout;
119 enum {
120 ARG_a = (1<<0), /* All modules, ignore mods in argv */
121 ARG_A = (1<<1), /* Only emit .ko that are newer than modules.dep file */
122 ARG_b = (1<<2), /* not /lib/modules/$(uname -r)/ but this base-dir */
123 ARG_e = (1<<3), /* with -F, print unresolved symbols */
124 ARG_F = (1<<4), /* System.map that contains the symbols */
125 ARG_n = (1<<5) /* dry-run, print to stdout only */
126 };
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +0000127 INIT_G();
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +0000128
Bernhard Reutner-Fischer6bb55cf2008-05-26 17:04:01 +0000129 getopt32(argv, "aAb:eF:n", &moddir_base, &system_map);
130 argv += optind;
Bernhard Reutner-Fischerc21d9c72008-05-28 10:35:51 +0000131
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +0000132 /* If a version is provided, then that kernel version’s module directory
133 * is used, rather than the current kernel version (as returned by
134 * "uname -r"). */
Bernhard Reutner-Fischer634b0222008-05-28 14:20:20 +0000135 if (*argv && (sscanf(*argv, "%d.%d.%d", &ret, &ret, &ret) == 3)) {
Bernhard Reutner-Fischerc21d9c72008-05-28 10:35:51 +0000136 moddir = concat_path_file(CONFIG_DEFAULT_MODULES_DIR, *argv++);
Bernhard Reutner-Fischer1ea25682008-05-26 21:33:05 +0000137 } else {
Bernhard Reutner-Fischer6bb55cf2008-05-26 17:04:01 +0000138 struct utsname uts;
139 if (uname(&uts) < 0)
140 bb_simple_perror_msg_and_die("uname");
Bernhard Reutner-Fischerc21d9c72008-05-28 10:35:51 +0000141 moddir = concat_path_file(CONFIG_DEFAULT_MODULES_DIR, uts.release);
Bernhard Reutner-Fischer6bb55cf2008-05-26 17:04:01 +0000142 }
Bernhard Reutner-Fischer634b0222008-05-28 14:20:20 +0000143 /* If no modules are given on the command-line, -a is on per default. */
Bernhard Reutner-Fischer1ea25682008-05-26 21:33:05 +0000144 option_mask32 |= *argv == NULL;
145
Bernhard Reutner-Fischer1ea25682008-05-26 21:33:05 +0000146 if (option_mask32 & ARG_b) {
Bernhard Reutner-Fischerc2741e12008-06-02 18:59:16 +0000147 moddir_base_len = strlen(moddir_base) + 1;
148 xchdir(moddir_base);
Bernhard Reutner-Fischer6bb55cf2008-05-26 17:04:01 +0000149 }
Bernhard Reutner-Fischer1ea25682008-05-26 21:33:05 +0000150
151 if (!(option_mask32 & ARG_n)) { /* --dry-run */
Bernhard Reutner-Fischerb85fb692008-05-27 10:55:34 +0000152 chp = concat_path_file(moddir, CONFIG_DEFAULT_DEPMOD_FILE);
Bernhard Reutner-Fischer1ea25682008-05-26 21:33:05 +0000153 filedes = xfopen(chp, "w");
154 if (ENABLE_FEATURE_CLEAN_UP)
155 free(chp);
156 }
Bernhard Reutner-Fischerc21d9c72008-05-28 10:35:51 +0000157 ret = EXIT_SUCCESS;
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +0000158 do {
Bernhard Reutner-Fischerc2741e12008-06-02 18:59:16 +0000159 chp = option_mask32 & ARG_a ? moddir : (*argv + moddir_base_len);
Bernhard Reutner-Fischer6bb55cf2008-05-26 17:04:01 +0000160
161 if (!recursive_action(chp,
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +0000162 ACTION_RECURSE, /* flags */
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +0000163 fileAction, /* file action */
164 NULL, /* dir action */
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +0000165 NULL, /* user data */
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +0000166 0)) { /* depth */
Bernhard Reutner-Fischerc21d9c72008-05-28 10:35:51 +0000167 ret = EXIT_FAILURE;
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +0000168 }
Bernhard Reutner-Fischerc2741e12008-06-02 18:59:16 +0000169 } while (!(option_mask32 & ARG_a) && *++argv);
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +0000170
Bernhard Reutner-Fischerc2741e12008-06-02 18:59:16 +0000171 {
172 dep_lst_t *mods = G.lst;
173
174 /* Fixup the module names in the depends list */
175 while (mods) {
176 llist_t *deps = NULL, *old_deps = mods->dependencies;
Denis Vlasenko7049ff82008-06-25 09:53:17 +0000177
Bernhard Reutner-Fischerc2741e12008-06-02 18:59:16 +0000178 while (old_deps) {
179 dep_lst_t *all = G.lst;
180 char *longname = NULL;
181 char *shortname = llist_pop(&old_deps);
182
183 while (all) {
Denis Vlasenkof45c4f42008-06-16 04:09:25 +0000184 char *nam =
Bernhard Reutner-Fischerc2741e12008-06-02 18:59:16 +0000185 xstrdup(bb_get_last_path_component_nostrip(all->name));
186 char *tmp = strrstr(nam, ".ko");
187
188 *tmp = '\0';
189 if (!strcmp(nam, shortname)) {
190 if (ENABLE_FEATURE_CLEAN_UP)
191 free(nam);
192 longname = all->name;
193 break;
194 }
195 free(nam);
196 all = all->next;
197 }
198 llist_add_to_end(&deps, longname);
199 }
200 mods->dependencies = deps;
201 mods = mods->next;
202 }
203
204#if ENABLE_FEATURE_DEPMOD_PRUNE_FANCY
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +0000205 /* modprobe allegedly wants dependencies without duplicates, i.e.
206 * mod1: mod2 mod3
207 * mod2: mod3
208 * mod3:
209 * implies that mod1 directly depends on mod2 and _not_ mod3 as mod3 is
210 * already implicitely pulled in via mod2. This leaves us with:
211 * mod1: mod2
212 * mod2: mod3
213 * mod3:
214 */
Bernhard Reutner-Fischerc2741e12008-06-02 18:59:16 +0000215 mods = G.lst;
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +0000216 while (mods) {
217 llist_t *deps = mods->dependencies;
218 while (deps) {
219 dep_lst_t *all = G.lst;
220 while (all) {
221 if (!strcmp(all->name, deps->data)) {
222 llist_t *implied = all->dependencies;
223 while (implied) {
Bernhard Reutner-Fischer6075b2a2008-06-02 19:23:47 +0000224 /* XXX:FIXME: erm, it would be nicer to just
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +0000225 * llist_unlink(&mods->dependencies, implied) */
226 llist_t *prune = mods->dependencies;
227 while (prune) {
228 if (!strcmp(implied->data, prune->data))
229 break;
230 prune = prune->link;
231 }
232//if (prune) bb_info_msg("[%s] '%s' implies '%s', removing", mods->name, all->name, implied->data);
233 llist_unlink(&mods->dependencies, prune);
234 implied = implied->link;
235 }
236 }
237 all = all->next;
238 }
239 deps = deps->link;
240 }
241 mods = mods->next;
242 }
Bernhard Reutner-Fischerc2741e12008-06-02 18:59:16 +0000243#endif
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +0000244
245 mods = G.lst;
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +0000246 /* Finally print them. */
247 while (mods) {
248 fprintf(filedes, "%s:", mods->name);
Bernhard Reutner-Fischerc2741e12008-06-02 18:59:16 +0000249 /* If we did not resolve all modules, then it's likely that we just did
250 * not see the names of all prerequisites (which will be NULL in this
251 * case). */
252 while (mods->dependencies) {
253 char *the_dep = llist_pop(&mods->dependencies);
254 if (the_dep)
255 fprintf(filedes, " %s", the_dep);
256 }
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +0000257 fprintf(filedes, "\n");
Bernhard Reutner-Fischerc2741e12008-06-02 18:59:16 +0000258 if (ENABLE_FEATURE_DEPMOD_ALIAS)
259 {
260 char *shortname =
261 xstrdup(bb_get_last_path_component_nostrip(mods->name));
262 char *tmp = strrstr(shortname, ".ko");
263
264 *tmp = '\0';
Denis Vlasenko7049ff82008-06-25 09:53:17 +0000265
Bernhard Reutner-Fischerc2741e12008-06-02 18:59:16 +0000266 while (mods->aliases) {
267 fprintf(filedes, "alias %s %s\n",
268 (char*)llist_pop(&mods->aliases),
269 shortname);
270 }
271 free(shortname);
272 }
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +0000273 mods = mods->next;
274 }
275 }
276
Bernhard Reutner-Fischerdc5d7fe2008-05-26 13:30:41 +0000277 if (ENABLE_FEATURE_CLEAN_UP) {
Bernhard Reutner-Fischerc21d9c72008-05-28 10:35:51 +0000278 fclose_if_not_stdin(filedes);
Bernhard Reutner-Fischer1ea25682008-05-26 21:33:05 +0000279 free(moddir);
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +0000280 while (G.lst) {
281 dep_lst_t *old = G.lst;
282 G.lst = G.lst->next;
283 free(old->name);
284 free(old);
285 }
Bernhard Reutner-Fischerdc5d7fe2008-05-26 13:30:41 +0000286 }
Bernhard Reutner-Fischerc21d9c72008-05-28 10:35:51 +0000287 return ret;
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +0000288}