blob: 5b9e9e8644f5f11cefd8ec0212ab797600050e6b [file] [log] [blame]
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +00001/* vi: set sw=4 ts=4: */
2/*
3 * depmod - generate modules.dep
Bernhard Reutner-Fischer6c4dade2008-09-25 12:13:34 +00004 * Copyright (c) 2008 Bernhard Reutner-Fischer
Denis Vlasenkoba1315d2008-09-13 14:59:38 +00005 * Copyrihgt (c) 2008 Timo Teras <timo.teras@iki.fi>
6 * Copyright (c) 2008 Vladimir Dronnikov
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +00007 *
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() */
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000015#include "modutils.h"
16
Bernhard Reutner-Fischercf180102008-05-26 15:12:01 +000017/*
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 */
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +000023
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000024typedef struct module_info {
25 struct module_info *next;
26 char *name, *modname;
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +000027 llist_t *dependencies;
Bernhard Reutner-Fischerc2741e12008-06-02 18:59:16 +000028 llist_t *aliases;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000029 llist_t *symbols;
30 struct module_info *dnext, *dprev;
31} module_info;
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +000032
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000033enum {
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), /* not /lib/modules/$(uname -r)/ but this base-dir */
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 */
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +000040};
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +000041
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000042static int FAST_FUNC parse_module(const char *fname, struct stat *sb,
43 void *data, int UNUSED_PARAM depth)
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +000044{
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000045 module_info **first = (module_info **) data;
46 char *image, *ptr;
47 module_info *info;
Bernhard Reutner-Fischercf180102008-05-26 15:12:01 +000048 size_t len = sb->st_size;
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +000049
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000050 if (strrstr(fname, ".ko") == NULL)
51 return TRUE;
Bernhard Reutner-Fischer634b0222008-05-28 14:20:20 +000052
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000053 image = (char *) xmalloc_open_zipped_read_close(fname, &len);
54 info = xzalloc(sizeof(module_info));
Bernhard Reutner-Fischerc2741e12008-06-02 18:59:16 +000055
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000056 info->next = *first;
57 *first = info;
58
59 info->dnext = info->dprev = info;
60 info->name = xstrdup(fname);
61 info->modname = filename2modname(fname, NULL);
62 for (ptr = image; ptr < image + len - 10; ptr++) {
63 if (strncmp(ptr, "depends=", 8) == 0) {
64 char *u;
65
66 ptr += 8;
67 for (u = ptr; *u; u++)
68 if (*u == '-')
69 *u = '_';
70 ptr += string_to_llist(ptr, &info->dependencies, ",");
71 } else if (ENABLE_FEATURE_MODUTILS_ALIAS &&
72 strncmp(ptr, "alias=", 6) == 0) {
73 llist_add_to(&info->aliases, xstrdup(ptr + 6));
74 ptr += strlen(ptr);
75 } else if (ENABLE_FEATURE_MODUTILS_SYMBOLS &&
76 strncmp(ptr, "__ksymtab_", 10) == 0) {
77 ptr += 10;
78 if (strncmp(ptr, "gpl", 3) == 0 ||
79 strcmp(ptr, "strings") == 0)
80 continue;
81 llist_add_to(&info->symbols, xstrdup(ptr));
82 ptr += strlen(ptr);
83 }
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +000084 }
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000085 free(image);
86
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +000087 return TRUE;
88}
89
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000090static module_info *find_module(module_info *modules, const char *modname)
91{
92 module_info *m;
93
94 for (m = modules; m != NULL; m = m->next)
95 if (strcmp(m->modname, modname) == 0)
96 return m;
97 return NULL;
98}
99
100static void order_dep_list(module_info *modules, module_info *start,
101 llist_t *add)
102{
103 module_info *m;
104 llist_t *n;
105
106 for (n = add; n != NULL; n = n->link) {
107 m = find_module(modules, n->data);
108 if (m == NULL)
109 continue;
110
111 /* unlink current entry */
112 m->dnext->dprev = m->dprev;
113 m->dprev->dnext = m->dnext;
114
115 /* and add it to tail */
116 m->dnext = start;
117 m->dprev = start->dprev;
118 start->dprev->dnext = m;
119 start->dprev = m;
120
121 /* recurse */
122 order_dep_list(modules, start, m->dependencies);
123 }
124}
125
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +0000126int depmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000127int depmod_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +0000128{
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000129 module_info *modules = NULL, *m, *dep;
130 char *moddir_base = (char *)CONFIG_DEFAULT_MODULES_DIR;
131 int tmp;
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +0000132
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000133 getopt32(argv, "aAb:eF:n", &moddir_base, NULL);
Bernhard Reutner-Fischer6bb55cf2008-05-26 17:04:01 +0000134 argv += optind;
Bernhard Reutner-Fischerc21d9c72008-05-28 10:35:51 +0000135
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000136 /* goto modules location */
137
138 /* If a version is provided, then that kernel version's module directory
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +0000139 * is used, rather than the current kernel version (as returned by
140 * "uname -r"). */
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000141 xchdir(moddir_base);
142 if (*argv && (sscanf(*argv, "%d.%d.%d", &tmp, &tmp, &tmp) == 3)) {
143 xchdir(*argv++);
Bernhard Reutner-Fischer1ea25682008-05-26 21:33:05 +0000144 } else {
Bernhard Reutner-Fischer6bb55cf2008-05-26 17:04:01 +0000145 struct utsname uts;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000146 uname(&uts);
147 xchdir(uts.release);
Bernhard Reutner-Fischer6bb55cf2008-05-26 17:04:01 +0000148 }
Bernhard Reutner-Fischer634b0222008-05-28 14:20:20 +0000149 /* If no modules are given on the command-line, -a is on per default. */
Bernhard Reutner-Fischer1ea25682008-05-26 21:33:05 +0000150 option_mask32 |= *argv == NULL;
151
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000152 /* Scan modules */
153 moddir_base = xrealloc_getcwd_or_warn(NULL);
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +0000154 do {
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000155 recursive_action((option_mask32 & ARG_a) ? moddir_base : *argv,
156 ACTION_RECURSE, parse_module, NULL, &modules, 0);
157 } while (!(option_mask32 & ARG_a) && *(++argv));
158 if (ENABLE_FEATURE_CLEAN_UP)
159 free(moddir_base);
Bernhard Reutner-Fischer6bb55cf2008-05-26 17:04:01 +0000160
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000161 /* Generate dependency and alias files */
162 if (!(option_mask32 & ARG_n))
163 freopen(CONFIG_DEFAULT_DEPMOD_FILE, "w", stdout);
164 for (m = modules; m != NULL; m = m->next) {
165 printf("%s:", m->name);
166
167 order_dep_list(modules, m, m->dependencies);
168 while (m->dnext != m) {
169 dep = m->dnext;
170 printf(" %s", dep->name);
171
172 /* unlink current entry */
173 dep->dnext->dprev = dep->dprev;
174 dep->dprev->dnext = dep->dnext;
175 dep->dnext = dep->dprev = dep;
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +0000176 }
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000177 puts("");
Bernhard Reutner-Fischerc2741e12008-06-02 18:59:16 +0000178 }
179
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000180#if ENABLE_FEATURE_MODUTILS_ALIAS
181 if (!(option_mask32 & ARG_n))
182 freopen("modules.alias", "w", stdout);
183 for (m = modules; m != NULL; m = m->next) {
184 while (m->aliases) {
185 printf("alias %s %s\n",
186 (char*)llist_pop(&m->aliases),
187 m->modname);
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +0000188 }
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000189 }
190#endif
191#if ENABLE_FEATURE_MODUTILS_SYMBOLS
192 if (!(option_mask32 & ARG_n))
193 freopen("modules.symbols", "w", stdout);
194 for (m = modules; m != NULL; m = m->next) {
195 while (m->symbols) {
196 printf("alias symbol:%s %s\n",
197 (char*)llist_pop(&m->symbols),
198 m->modname);
199 }
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +0000200 }
Bernhard Reutner-Fischerc2741e12008-06-02 18:59:16 +0000201#endif
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +0000202
Bernhard Reutner-Fischerdc5d7fe2008-05-26 13:30:41 +0000203 if (ENABLE_FEATURE_CLEAN_UP) {
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000204 while (modules) {
205 module_info *old = modules;
206 modules = modules->next;
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +0000207 free(old->name);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000208 free(old->modname);
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +0000209 free(old);
210 }
Bernhard Reutner-Fischerdc5d7fe2008-05-26 13:30:41 +0000211 }
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000212
213 return EXIT_SUCCESS;
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +0000214}