- just chdir to the basedir (-30b).
[oweals/busybox.git] / modutils / depmod.c
1 /* 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() */
13 #if ENABLE_DEBUG
14 #include <assert.h>
15 #define dbg_assert assert
16 #else
17 #define dbg_assert(stuff) do {} while (0)
18 #endif
19 /*
20  * Theory of operation:
21  * - iterate over all modules and record their full path
22  * - iterate over all modules looking for "depends=" entries
23  *   for each depends, look through our list of full paths and emit if found
24  */
25
26 typedef struct dep_lst_t {
27         char *name;
28         llist_t *dependencies;
29         struct dep_lst_t *next;
30 } dep_lst_t;
31
32 struct globals {
33         dep_lst_t *lst; /* modules without their corresponding extension */
34 };
35 #define G (*(struct globals*)&bb_common_bufsiz1)
36 /* We have to zero it out because of NOEXEC */
37 #define INIT_G() memset(&G, 0, sizeof(G))
38
39 static int fill_lst(const char *modulename, struct stat ATTRIBUTE_UNUSED *sb,
40                                         void ATTRIBUTE_UNUSED *data, int ATTRIBUTE_UNUSED depth)
41 {
42
43         /* We get a file here. If the file does not have ".ko" but an
44          * intermittent dentry has, it's just their fault.
45          */
46         if (strrstr(modulename, ".ko") != NULL) {
47                 dep_lst_t *new = xzalloc(sizeof(dep_lst_t));
48                 new->name = xstrdup(modulename);
49                 new->next = G.lst;
50                 G.lst = new;
51         }
52         return TRUE;
53 }
54
55 static int fileAction(const char *fname, struct stat *sb,
56                                         void ATTRIBUTE_UNUSED *data, int ATTRIBUTE_UNUSED depth)
57 {
58         size_t len = sb->st_size;
59         void *the_module;
60         char *ptr;
61         int fd;
62         char *depends, *deps;
63         dep_lst_t *this;
64
65         if (strrstr(fname, ".ko") == NULL) /* not a module */
66                 goto skip;
67
68 /*XXX: FIXME: does not handle compressed modules!
69  * There should be a function that looks at the extension and sets up
70  * open_transformer for us.
71  */
72         fd = xopen(fname, O_RDONLY);
73         the_module = mmap(NULL, len, PROT_READ, MAP_SHARED
74 #if defined MAP_POPULATE
75                                                 |MAP_POPULATE
76 #endif
77                                                 , fd, 0);
78         close(fd);
79         if (the_module == MAP_FAILED)
80                 bb_perror_msg_and_die("mmap");
81         ptr = the_module;
82         this = G.lst;
83         do {
84                 if (!strcmp(fname, this->name))
85                         break;
86                 this = this->next;
87         } while (this);
88         dbg_assert (this);
89 //bb_info_msg("fname='%s'", fname);
90         do {
91                 /* search for a 'd' */
92                 ptr = memchr(ptr, 'd', len - (ptr - (char*)the_module));
93                 if (ptr == NULL) /* no d left, done */
94                         goto none;
95                 if (!strncmp(ptr, "depends=", sizeof("depends=")-1))
96                         break;
97                 ++ptr;
98         } while (1);
99         deps = depends = xstrdup (ptr + sizeof("depends=")-1);
100 //bb_info_msg(" depends='%s'", depends);
101         while (deps) {
102                 dep_lst_t *all = G.lst;
103
104                 ptr = strsep(&deps, ",");
105                 while (all) {
106                         /* Compare the recorded filenames ignoring ".ko*" at the end.  */
107                         char *tmp = bb_get_last_path_component_nostrip(all->name);
108                         if (!strncmp(ptr, tmp, MAX(strlen(ptr),strrstr(tmp, ".ko") - tmp)))
109                                 break; /* found it */
110                         all = all->next;
111                 }
112                 if (all) {
113                         dbg_assert(all->name); /* this cannot be empty */
114 //bb_info_msg("[%s] -> '%s'", (char*)ptr, all->name);
115                         llist_add_to_end(&this->dependencies, all->name);
116                 }
117         }
118         free(depends);
119  none:
120         munmap(the_module, sb->st_size);
121  skip:
122         return TRUE;
123 }
124
125 int depmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
126 int depmod_main(int ATTRIBUTE_UNUSED argc, char **argv)
127 {
128         int ret;
129         char *moddir_base = NULL, *moddir, *system_map, *chp;
130         FILE *filedes = stdout;
131         enum {
132                 ARG_a = (1<<0), /* All modules, ignore mods in argv */
133                 ARG_A = (1<<1), /* Only emit .ko that are newer than modules.dep file */
134                 ARG_b = (1<<2), /* not /lib/modules/$(uname -r)/ but this base-dir */
135                 ARG_e = (1<<3), /* with -F, print unresolved symbols */
136                 ARG_F = (1<<4), /* System.map that contains the symbols */
137                 ARG_n = (1<<5)  /* dry-run, print to stdout only */
138         };
139         INIT_G();
140
141         getopt32(argv, "aAb:eF:n", &moddir_base, &system_map);
142         argv += optind;
143
144         /* If a version is provided, then that kernel version’s module directory
145          * is used, rather than the current kernel version (as returned by
146          * "uname -r").  */
147         if (*argv && (sscanf(*argv, "%d.%d.%d", &ret, &ret, &ret) == 3)) {
148                 moddir = concat_path_file(CONFIG_DEFAULT_MODULES_DIR, *argv++);
149         } else {
150                 struct utsname uts;
151                 if (uname(&uts) < 0)
152                         bb_simple_perror_msg_and_die("uname");
153                 moddir = concat_path_file(CONFIG_DEFAULT_MODULES_DIR, uts.release);
154         }
155         /* If no modules are given on the command-line, -a is on per default.  */
156         option_mask32 |= *argv == NULL;
157
158         if (option_mask32 & ARG_b) {
159                 xchdir(moddir_base);
160         }
161
162         if (!(option_mask32 & ARG_n)) { /* --dry-run */
163                 chp = concat_path_file(moddir, CONFIG_DEFAULT_DEPMOD_FILE);
164                 filedes = xfopen(chp, "w");
165                 if (ENABLE_FEATURE_CLEAN_UP)
166                         free(chp);
167         }
168         ret = EXIT_SUCCESS;
169         /* We have to do a full walk to collect all needed data.  */
170         if (!recursive_action(moddir,
171                         ACTION_RECURSE, /* flags */
172                         fill_lst, /* file action */
173                         NULL, /* dir action */
174                         NULL, /* user data */
175                         0)) { /* depth */
176                 if (ENABLE_FEATURE_CLEAN_UP)
177                         ret = EXIT_FAILURE;
178                 else
179                         return EXIT_FAILURE;
180         }
181 #if ENABLE_FEATURE_CLEAN_UP
182         else
183 #endif
184         do {
185                 chp = option_mask32 & ARG_a ? moddir : *argv++;
186
187                 if (!recursive_action(chp,
188                                 ACTION_RECURSE, /* flags */
189                                 fileAction, /* file action */
190                                 NULL, /* dir action */
191                                 NULL, /* user data */
192                                 0)) { /* depth */
193                         ret = EXIT_FAILURE;
194                 }
195         } while (!(option_mask32 & ARG_a) && *argv);
196
197         /* modprobe allegedly wants dependencies without duplicates, i.e.
198          * mod1: mod2 mod3
199          * mod2: mod3
200          * mod3:
201          * implies that mod1 directly depends on mod2 and _not_ mod3 as mod3 is
202          * already implicitely pulled in via mod2. This leaves us with:
203          * mod1: mod2
204          * mod2: mod3
205          * mod3:
206          */
207         {
208         dep_lst_t *mods = G.lst;
209 #if ENABLE_FEATURE_DEPMOD_PRUNE_FANCY
210         while (mods) {
211                 llist_t *deps = mods->dependencies;
212                 while (deps) {
213                         dep_lst_t *all = G.lst;
214                         while (all) {
215                                 if (!strcmp(all->name, deps->data)) {
216                                         llist_t *implied = all->dependencies;
217                                         while (implied) {
218                                                 /* erm, nicer would be to just
219                                                  * llist_unlink(&mods->dependencies, implied)  */
220                                                 llist_t *prune = mods->dependencies;
221                                                 while (prune) {
222                                                         if (!strcmp(implied->data, prune->data))
223                                                                 break;
224                                                         prune = prune->link;
225                                                 }
226 //if (prune) bb_info_msg("[%s] '%s' implies '%s', removing", mods->name, all->name, implied->data);
227                                                 llist_unlink(&mods->dependencies, prune);
228                                                 implied = implied->link;
229                                         }
230                                 }
231                                 all = all->next;
232                         }
233                         deps = deps->link;
234                 }
235                 mods = mods->next;
236         }
237
238         mods = G.lst;
239 #endif
240         /* Finally print them.  */
241         while (mods) {
242                 fprintf(filedes, "%s:", mods->name);
243                 while (mods->dependencies)
244                         fprintf(filedes, " %s", (char*)llist_pop(&mods->dependencies));
245                 fprintf(filedes, "\n");
246                 mods = mods->next;
247         }
248         }
249
250         if (ENABLE_FEATURE_CLEAN_UP) {
251                 fclose_if_not_stdin(filedes);
252                 free(moddir);
253                 while (G.lst) {
254                         dep_lst_t *old = G.lst;
255                         G.lst = G.lst->next;
256                         free(old->name);
257                         free(old);
258                 }
259         }
260         return ret;
261 }