- the moddir_base is supposed to be omitted frmo the .dep file
[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
14 /*
15  * Theory of operation:
16  * - iterate over all modules and record their full path
17  * - iterate over all modules looking for "depends=" entries
18  *   for each depends, look through our list of full paths and emit if found
19  */
20 struct globals {
21         llist_t *lst;
22         unsigned moddir_base_len;
23 };
24 #define G (*(struct globals*)&bb_common_bufsiz1)
25 /* We have to zero it out because of NOEXEC */
26 #define INIT_G() memset(&G, 0, sizeof(G))
27
28 static int fill_lst(const char *modulename, struct stat ATTRIBUTE_UNUSED *sb,
29                                         void ATTRIBUTE_UNUSED *data, int ATTRIBUTE_UNUSED depth)
30 {
31         if (last_char_is(modulename, 'o') != NULL) /* it is a module, remember it */
32                 llist_add_to(&G.lst, xstrdup(modulename + G.moddir_base_len));
33         return TRUE;
34 }
35
36 static int fileAction(const char *fname, struct stat *sb,
37                                         void *data, int ATTRIBUTE_UNUSED depth)
38 {
39         size_t len = sb->st_size;
40         void *the_module, *ptr;
41         int fd;
42         char *depends, *deps;
43
44 /*XXX: FIXME: does not handle compressed modules!
45  * There should be a function that looks at the extension and sets up
46  * open_transformer for us.
47  */
48         if (last_char_is(fname, 'o') == NULL) /* not a module */
49                 goto skip;
50
51         fd = xopen(fname, O_RDONLY);
52         the_module = mmap(NULL, len, PROT_READ, MAP_SHARED
53 #if defined MAP_POPULATE
54                                                 |MAP_POPULATE
55 #endif
56                                                 , fd, 0);
57         close(fd);
58         if (the_module == MAP_FAILED)
59                 bb_perror_msg_and_die("mmap");
60
61         ptr = the_module;
62
63         fprintf((FILE*)data, "%s:", fname + G.moddir_base_len);
64 //bb_info_msg("fname='%s'", fname + G.moddir_base_len);
65         do {
66                 /* search for a 'd' */
67                 ptr = memchr(ptr, 'd', len - (ptr - the_module));
68                 if (ptr == NULL) /* no d left, done */
69                         goto done;
70                 if (memcmp(ptr, "depends=", sizeof("depends=")-1) == 0)
71                         break;
72                 ++ptr;
73         } while (1);
74         deps = depends = xstrdup (ptr + sizeof("depends=")-1);
75 //bb_info_msg(" depends='%s'", depends);
76         while (*deps) {
77                 llist_t * _lst = G.lst;
78                 char *buf1;
79
80                 ptr = strchr(deps, ',');
81                 if (ptr != NULL)
82                         *(char*)ptr = '\0';
83                 /* remember the length of the current dependency plus eventual 0 byte */
84                 len = strlen(deps) + (ptr != NULL);
85                 buf1 = xasprintf("/%s.", deps); /* match the correct file */
86                 while (_lst) {
87                         ptr = strstr(_lst->data, buf1);
88                         if (ptr != NULL)
89                                 break; /* found it */
90                         _lst = _lst->link;
91                 }
92                 free(buf1);
93                 if (_lst /*&& _lst->data*/) {
94 //bb_info_msg("[%s] -> '%s'", deps, _lst->data);
95                         fprintf((FILE*)data, " %s", _lst->data);
96                         deps += len;
97                 }
98         }
99         free(depends);
100         fprintf((FILE*)data, "\n");
101 done:
102         munmap(the_module, sb->st_size);
103 skip:
104         return TRUE;
105 }
106
107 int depmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
108 int depmod_main(int ATTRIBUTE_UNUSED argc, char **argv)
109 {
110         int ret;
111         char *moddir_base = NULL, *moddir, *system_map, *chp;
112         FILE *filedes = stdout;
113         enum {
114                 ARG_a = (1<<0), /* All modules, ignore mods in argv */
115                 ARG_A = (1<<1), /* Only emit .ko that are newer than modules.dep file */
116                 ARG_b = (1<<2), /* not /lib/modules/$(uname -r)/ but this base-dir */
117                 ARG_e = (1<<3), /* with -F, print unresolved symbols */
118                 ARG_F = (1<<4), /* System.map that contains the symbols */
119                 ARG_n = (1<<5)  /* dry-run, print to stdout only */
120         };
121
122         getopt32(argv, "aAb:eF:n", &moddir_base, &system_map);
123         argv += optind;
124
125         /* got a version to use? */
126         if (*argv && (sscanf(*argv, "%u.%u.%u", &ret, &ret, &ret) == 3)) {
127                 moddir = concat_path_file(CONFIG_DEFAULT_MODULES_DIR, *argv++);
128         } else {
129                 struct utsname uts;
130                 if (uname(&uts) < 0)
131                         bb_simple_perror_msg_and_die("uname");
132                 moddir = concat_path_file(CONFIG_DEFAULT_MODULES_DIR, uts.release);
133         }
134         /* if no modules are given on the command-line, -a is on per default */
135         option_mask32 |= *argv == NULL;
136
137         if (option_mask32 & ARG_b) {
138                 G.moddir_base_len = strlen(moddir_base);
139                 if (ENABLE_FEATURE_CLEAN_UP) {
140                         chp = moddir;
141                         moddir = concat_path_file(moddir_base, moddir);
142                         free (chp);
143                 } else
144                         moddir = concat_path_file(moddir_base, moddir);
145         }
146
147         if (!(option_mask32 & ARG_n)) { /* --dry-run */
148                 chp = concat_path_file(moddir, CONFIG_DEFAULT_DEPMOD_FILE);
149                 filedes = xfopen(chp, "w");
150                 if (ENABLE_FEATURE_CLEAN_UP)
151                         free(chp);
152         }
153         ret = EXIT_SUCCESS;
154         /* have to do a full walk to collect all needed data */
155         if (!recursive_action(moddir,
156                         ACTION_RECURSE, /* flags */
157                         fill_lst, /* file action */
158                         NULL, /* dir action */
159                         NULL, /* user data */
160                         0)) {
161                 ret = EXIT_FAILURE;
162         } else
163         do {
164                 chp = option_mask32 & ARG_a ? moddir : *argv++;
165
166                 if (!recursive_action(chp,
167                                 ACTION_RECURSE, /* flags */
168                                 fileAction, /* file action */
169                                 NULL, /* dir action */
170                                 (void*)filedes, /* user data */
171                                 0)) { /* depth */
172                         ret = EXIT_FAILURE;
173                 }
174         } while (!(option_mask32 & ARG_a) && *argv);
175
176         if (ENABLE_FEATURE_CLEAN_UP) {
177                 fclose_if_not_stdin(filedes);
178                 llist_free(G.lst, free);
179                 free(moddir);
180         }
181         return ret;
182 }