blob: d6cc8ed59e43e88858ab48aaa610fa0942f8e35b [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
Denys Vlasenko043b1e52009-09-06 12:47:55 +020013#include "libbb.h"
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000014#include "modutils.h"
Denys Vlasenko043b1e52009-09-06 12:47:55 +020015#include <sys/utsname.h> /* uname() */
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000016
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 */
Denis Vlasenko35261152009-01-12 22:12:22 +000036 ARG_b = (1<<2), /* base directory when modules are in staging area */
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000037 ARG_e = (1<<3), /* with -F, print unresolved symbols */
38 ARG_F = (1<<4), /* System.map that contains the symbols */
Ned Ludd039ed432009-02-20 04:56:00 +000039 ARG_n = (1<<5), /* dry-run, print to stdout only */
Denis Vlasenkoa430b692009-02-19 01:25:42 +000040 ARG_r = (1<<6) /* Compat dummy. Linux Makefile uses it */
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +000041};
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +000042
Denys Vlasenko16bda3b2009-05-18 13:08:04 +020043static int FAST_FUNC parse_module(const char *fname, struct stat *sb UNUSED_PARAM,
44 void *data, int depth UNUSED_PARAM)
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +000045{
Denys Vlasenko16bda3b2009-05-18 13:08:04 +020046 char modname[MODULE_NAME_LEN];
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000047 module_info **first = (module_info **) data;
48 char *image, *ptr;
49 module_info *info;
Denys Vlasenko16bda3b2009-05-18 13:08:04 +020050 /* Arbitrary. Was sb->st_size, but that breaks .gz etc */
51 size_t len = (64*1024*1024 - 4096);
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +000052
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000053 if (strrstr(fname, ".ko") == NULL)
54 return TRUE;
Bernhard Reutner-Fischer634b0222008-05-28 14:20:20 +000055
Denis Vlasenkofc668922008-11-06 02:32:31 +000056 image = xmalloc_open_zipped_read_close(fname, &len);
Denys Vlasenko16bda3b2009-05-18 13:08:04 +020057 info = xzalloc(sizeof(*info));
Bernhard Reutner-Fischerc2741e12008-06-02 18:59:16 +000058
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000059 info->next = *first;
60 *first = info;
61
62 info->dnext = info->dprev = info;
Denis Vlasenko35261152009-01-12 22:12:22 +000063 info->name = xasprintf("/%s", fname);
Denys Vlasenko16bda3b2009-05-18 13:08:04 +020064 info->modname = xstrdup(filename2modname(fname, modname));
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000065 for (ptr = image; ptr < image + len - 10; ptr++) {
66 if (strncmp(ptr, "depends=", 8) == 0) {
67 char *u;
68
69 ptr += 8;
70 for (u = ptr; *u; u++)
71 if (*u == '-')
72 *u = '_';
73 ptr += string_to_llist(ptr, &info->dependencies, ",");
Denys Vlasenko16bda3b2009-05-18 13:08:04 +020074 } else if (ENABLE_FEATURE_MODUTILS_ALIAS
75 && strncmp(ptr, "alias=", 6) == 0
76 ) {
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000077 llist_add_to(&info->aliases, xstrdup(ptr + 6));
78 ptr += strlen(ptr);
Denys Vlasenko16bda3b2009-05-18 13:08:04 +020079 } else if (ENABLE_FEATURE_MODUTILS_SYMBOLS
80 && strncmp(ptr, "__ksymtab_", 10) == 0
81 ) {
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000082 ptr += 10;
83 if (strncmp(ptr, "gpl", 3) == 0 ||
84 strcmp(ptr, "strings") == 0)
85 continue;
86 llist_add_to(&info->symbols, xstrdup(ptr));
87 ptr += strlen(ptr);
88 }
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +000089 }
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000090 free(image);
91
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +000092 return TRUE;
93}
94
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000095static module_info *find_module(module_info *modules, const char *modname)
96{
97 module_info *m;
98
99 for (m = modules; m != NULL; m = m->next)
100 if (strcmp(m->modname, modname) == 0)
101 return m;
102 return NULL;
103}
104
105static void order_dep_list(module_info *modules, module_info *start,
106 llist_t *add)
107{
108 module_info *m;
109 llist_t *n;
110
111 for (n = add; n != NULL; n = n->link) {
112 m = find_module(modules, n->data);
113 if (m == NULL)
114 continue;
115
116 /* unlink current entry */
117 m->dnext->dprev = m->dprev;
118 m->dprev->dnext = m->dnext;
119
120 /* and add it to tail */
121 m->dnext = start;
122 m->dprev = start->dprev;
123 start->dprev->dnext = m;
124 start->dprev = m;
125
126 /* recurse */
127 order_dep_list(modules, start, m->dependencies);
128 }
129}
130
Denis Vlasenko35261152009-01-12 22:12:22 +0000131static void xfreopen_write(const char *file, FILE *f)
132{
133 if (freopen(file, "w", f) == NULL)
134 bb_perror_msg_and_die("can't open '%s'", file);
135}
136
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +0000137int depmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +0000138int depmod_main(int argc UNUSED_PARAM, char **argv)
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +0000139{
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000140 module_info *modules = NULL, *m, *dep;
Denis Vlasenko35261152009-01-12 22:12:22 +0000141 const char *moddir_base = "/";
142 char *moddir, *version;
143 struct utsname uts;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000144 int tmp;
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +0000145
Denis Vlasenkoa430b692009-02-19 01:25:42 +0000146 getopt32(argv, "aAb:eF:nr", &moddir_base, NULL);
Bernhard Reutner-Fischer6bb55cf2008-05-26 17:04:01 +0000147 argv += optind;
Bernhard Reutner-Fischerc21d9c72008-05-28 10:35:51 +0000148
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000149 /* goto modules location */
Denis Vlasenko35261152009-01-12 22:12:22 +0000150 xchdir(moddir_base);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000151
152 /* If a version is provided, then that kernel version's module directory
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +0000153 * is used, rather than the current kernel version (as returned by
154 * "uname -r"). */
Denis Vlasenko35261152009-01-12 22:12:22 +0000155 if (*argv && sscanf(*argv, "%d.%d.%d", &tmp, &tmp, &tmp) == 3) {
156 version = *argv++;
Bernhard Reutner-Fischer1ea25682008-05-26 21:33:05 +0000157 } else {
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000158 uname(&uts);
Denis Vlasenko35261152009-01-12 22:12:22 +0000159 version = uts.release;
Bernhard Reutner-Fischer6bb55cf2008-05-26 17:04:01 +0000160 }
Denis Vlasenko35261152009-01-12 22:12:22 +0000161 moddir = concat_path_file(&CONFIG_DEFAULT_MODULES_DIR[1], version);
Bernhard Reutner-Fischer1ea25682008-05-26 21:33:05 +0000162
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000163 /* Scan modules */
Denis Vlasenko35261152009-01-12 22:12:22 +0000164 if (*argv) {
165 char *modfile;
166 struct stat sb;
167 do {
168 modfile = concat_path_file(moddir, *argv);
169 xstat(modfile, &sb);
170 parse_module(modfile, &sb, &modules, 0);
171 free(modfile);
172 } while (*(++argv));
173 } else {
174 recursive_action(moddir, ACTION_RECURSE,
175 parse_module, NULL, &modules, 0);
176 }
177
178 /* Prepare for writing out the dep files */
179 xchdir(moddir);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000180 if (ENABLE_FEATURE_CLEAN_UP)
Denis Vlasenko35261152009-01-12 22:12:22 +0000181 free(moddir);
Bernhard Reutner-Fischer6bb55cf2008-05-26 17:04:01 +0000182
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000183 /* Generate dependency and alias files */
184 if (!(option_mask32 & ARG_n))
Denis Vlasenko35261152009-01-12 22:12:22 +0000185 xfreopen_write(CONFIG_DEFAULT_DEPMOD_FILE, stdout);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000186 for (m = modules; m != NULL; m = m->next) {
187 printf("%s:", m->name);
188
189 order_dep_list(modules, m, m->dependencies);
190 while (m->dnext != m) {
191 dep = m->dnext;
192 printf(" %s", dep->name);
193
194 /* unlink current entry */
195 dep->dnext->dprev = dep->dprev;
196 dep->dprev->dnext = dep->dnext;
197 dep->dnext = dep->dprev = dep;
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +0000198 }
Denis Vlasenko35261152009-01-12 22:12:22 +0000199 bb_putchar('\n');
Bernhard Reutner-Fischerc2741e12008-06-02 18:59:16 +0000200 }
201
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000202#if ENABLE_FEATURE_MODUTILS_ALIAS
203 if (!(option_mask32 & ARG_n))
Denis Vlasenko35261152009-01-12 22:12:22 +0000204 xfreopen_write("modules.alias", stdout);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000205 for (m = modules; m != NULL; m = m->next) {
Denys Vlasenko16bda3b2009-05-18 13:08:04 +0200206 const char *fname = bb_basename(m->name);
207 int fnlen = strchrnul(fname, '.') - fname;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000208 while (m->aliases) {
Denys Vlasenko16bda3b2009-05-18 13:08:04 +0200209 /* Last word can well be m->modname instead,
210 * but depmod from module-init-tools 3.4
211 * uses module basename, i.e., no s/-/_/g.
212 * (pathname and .ko.* are still stripped)
213 * Mimicking that... */
214 printf("alias %s %.*s\n",
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000215 (char*)llist_pop(&m->aliases),
Denys Vlasenko16bda3b2009-05-18 13:08:04 +0200216 fnlen, fname);
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +0000217 }
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000218 }
219#endif
220#if ENABLE_FEATURE_MODUTILS_SYMBOLS
221 if (!(option_mask32 & ARG_n))
Denis Vlasenko35261152009-01-12 22:12:22 +0000222 xfreopen_write("modules.symbols", stdout);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000223 for (m = modules; m != NULL; m = m->next) {
Denys Vlasenko16bda3b2009-05-18 13:08:04 +0200224 const char *fname = bb_basename(m->name);
225 int fnlen = strchrnul(fname, '.') - fname;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000226 while (m->symbols) {
Denys Vlasenko16bda3b2009-05-18 13:08:04 +0200227 printf("alias symbol:%s %.*s\n",
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000228 (char*)llist_pop(&m->symbols),
Denys Vlasenko16bda3b2009-05-18 13:08:04 +0200229 fnlen, fname);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000230 }
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +0000231 }
Bernhard Reutner-Fischerc2741e12008-06-02 18:59:16 +0000232#endif
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +0000233
Bernhard Reutner-Fischerdc5d7fe2008-05-26 13:30:41 +0000234 if (ENABLE_FEATURE_CLEAN_UP) {
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000235 while (modules) {
236 module_info *old = modules;
237 modules = modules->next;
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +0000238 free(old->name);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000239 free(old->modname);
Bernhard Reutner-Fischerbeac1bd2008-06-02 13:28:47 +0000240 free(old);
241 }
Bernhard Reutner-Fischerdc5d7fe2008-05-26 13:30:41 +0000242 }
Denis Vlasenkoba1315d2008-09-13 14:59:38 +0000243
244 return EXIT_SUCCESS;
Bernhard Reutner-Fischer55e547e2008-05-26 12:01:49 +0000245}