e061501f6d722f76bf6b45ec930f737e85ac91af
[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; /* modules without their corresponding extension */
22         size_t moddir_base_len; /* length of the "-b basedir" */
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         /* We get a file here. If the file does not have ".ko" but an
32          * intermittent dentry has, it's just their fault.
33          */
34         if (strrstr(modulename, ".ko") != NULL)
35                 llist_add_to(&G.lst, xstrdup(modulename + G.moddir_base_len));
36         return TRUE;
37 }
38
39 static int fileAction(const char *fname, struct stat *sb,
40                                         void *data, int ATTRIBUTE_UNUSED depth)
41 {
42         size_t len = sb->st_size;
43         void *the_module;
44         char *ptr;
45         int fd;
46         char *depends, *deps;
47
48         if (strrstr(fname, ".ko") == NULL) /* not a module */
49                 goto skip;
50
51 /*XXX: FIXME: does not handle compressed modules!
52  * There should be a function that looks at the extension and sets up
53  * open_transformer for us.
54  */
55         fd = xopen(fname, O_RDONLY);
56         the_module = mmap(NULL, len, PROT_READ, MAP_SHARED
57 #if defined MAP_POPULATE
58                                                 |MAP_POPULATE
59 #endif
60                                                 , fd, 0);
61         close(fd);
62         if (the_module == MAP_FAILED)
63                 bb_perror_msg_and_die("mmap");
64
65         ptr = the_module;
66
67         fprintf((FILE*)data, "%s:", fname + G.moddir_base_len);
68 //bb_info_msg("fname='%s'", fname + G.moddir_base_len);
69         do {
70                 /* search for a 'd' */
71                 ptr = memchr(ptr, 'd', len - (ptr - (char*)the_module));
72                 if (ptr == NULL) /* no d left, done */
73                         goto none;
74                 if (strncmp(ptr, "depends=", sizeof("depends=")-1) == 0)
75                         break;
76                 ++ptr;
77         } while (1);
78         deps = depends = xstrdup (ptr + sizeof("depends=")-1);
79 //bb_info_msg(" depends='%s'", depends);
80         while (deps) {
81                 llist_t * _lst = G.lst;
82
83                 ptr = strsep(&deps, ",");
84                 while (_lst) {
85                         /* Compare the recorded filenames ignoring ".ko*" at the end.  */
86                         char *tmp = bb_get_last_path_component_nostrip(_lst->data);
87                         if (strncmp(ptr, tmp, strrstr(tmp, ".ko") - tmp) == 0)
88                                 break; /* found it */
89                         _lst = _lst->link;
90                 }
91                 if (_lst) {
92 //bb_info_msg("[%s] -> '%s'", (char*)ptr, _lst->data);
93                         fprintf((FILE*)data, " %s", _lst->data);
94                 }
95         }
96         free(depends);
97         fprintf((FILE*)data, "\n");
98  none:
99         munmap(the_module, sb->st_size);
100  skip:
101         return TRUE;
102 }
103
104 int depmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
105 int depmod_main(int ATTRIBUTE_UNUSED argc, char **argv)
106 {
107         int ret;
108         char *moddir_base = NULL, *moddir, *system_map, *chp;
109         FILE *filedes = stdout;
110         enum {
111                 ARG_a = (1<<0), /* All modules, ignore mods in argv */
112                 ARG_A = (1<<1), /* Only emit .ko that are newer than modules.dep file */
113                 ARG_b = (1<<2), /* not /lib/modules/$(uname -r)/ but this base-dir */
114                 ARG_e = (1<<3), /* with -F, print unresolved symbols */
115                 ARG_F = (1<<4), /* System.map that contains the symbols */
116                 ARG_n = (1<<5)  /* dry-run, print to stdout only */
117         };
118
119         getopt32(argv, "aAb:eF:n", &moddir_base, &system_map);
120         argv += optind;
121
122         /* got a version to use? */
123         if (*argv && (sscanf(*argv, "%d.%d.%d", &ret, &ret, &ret) == 3)) {
124                 moddir = concat_path_file(CONFIG_DEFAULT_MODULES_DIR, *argv++);
125         } else {
126                 struct utsname uts;
127                 if (uname(&uts) < 0)
128                         bb_simple_perror_msg_and_die("uname");
129                 moddir = concat_path_file(CONFIG_DEFAULT_MODULES_DIR, uts.release);
130         }
131         /* If no modules are given on the command-line, -a is on per default.  */
132         option_mask32 |= *argv == NULL;
133
134         if (option_mask32 & ARG_b) {
135                 G.moddir_base_len = strlen(moddir_base);
136                 if (ENABLE_FEATURE_CLEAN_UP) {
137                         chp = moddir;
138                         moddir = concat_path_file(moddir_base, moddir);
139                         free (chp);
140                 } else
141                         moddir = concat_path_file(moddir_base, moddir);
142         }
143
144         if (!(option_mask32 & ARG_n)) { /* --dry-run */
145                 chp = concat_path_file(moddir, CONFIG_DEFAULT_DEPMOD_FILE);
146                 filedes = xfopen(chp, "w");
147                 if (ENABLE_FEATURE_CLEAN_UP)
148                         free(chp);
149         }
150         ret = EXIT_SUCCESS;
151         /* We have to do a full walk to collect all needed data.  */
152         if (!recursive_action(moddir,
153                         ACTION_RECURSE, /* flags */
154                         fill_lst, /* file action */
155                         NULL, /* dir action */
156                         NULL, /* user data */
157                         0)) { /* depth */
158                 if (ENABLE_FEATURE_CLEAN_UP)
159                         ret = EXIT_FAILURE;
160                 else
161                         return EXIT_FAILURE;
162         }
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 }