aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenis Vlasenko <vda.linux@googlemail.com>2009-03-05 16:32:27 +0000
committerDenis Vlasenko <vda.linux@googlemail.com>2009-03-05 16:32:27 +0000
commit0e2f362a61a4ddbbae3f4372da9adf32bbce16e6 (patch)
tree8b21a74147522a999d869ea9a9036749ec052bff
parentc5741003e9cca3c0591ff44c4f27db71aab8be30 (diff)
downloadbusybox-w32-0e2f362a61a4ddbbae3f4372da9adf32bbce16e6.tar.gz
busybox-w32-0e2f362a61a4ddbbae3f4372da9adf32bbce16e6.tar.bz2
busybox-w32-0e2f362a61a4ddbbae3f4372da9adf32bbce16e6.zip
modprobe: rework/speedup by Timo Teras (timo.teras AT iki.fi)
function old new delta load_modules_dep - 175 +175 helper_get_module - 106 +106 gather_options - 89 +89 add_probe - 67 +67 config_file_action 345 388 +43 get_or_add_modentry - 10 +10 loaded 4 - -4 read_config 239 234 -5 llist_find 34 - -34 add_option 60 - -60 modprobe_main 567 494 -73 do_modprobe 601 273 -328 ------------------------------------------------------------------------------ (add/remove: 5/3 grow/shrink: 1/3 up/down: 490/-504) Total: -14 bytes text data bss dec hex filename 816778 476 7888 825142 c9736 busybox_old 816768 476 7880 825124 c9724 busybox_unstripped
-rw-r--r--modutils/modprobe.c362
1 files changed, 217 insertions, 145 deletions
diff --git a/modutils/modprobe.c b/modutils/modprobe.c
index 945483327..a5ade2add 100644
--- a/modutils/modprobe.c
+++ b/modutils/modprobe.c
@@ -13,26 +13,22 @@
13#include <sys/utsname.h> 13#include <sys/utsname.h>
14#include <fnmatch.h> 14#include <fnmatch.h>
15 15
16struct modprobe_option { 16#define MODULE_FLAG_LOADED 0x0001
17 char *module; 17#define MODULE_FLAG_NEED_DEPS 0x0002
18 char *option; 18//Misnomer? Seems to mean "was seen in modules.dep":
19#define MODULE_FLAG_EXISTS 0x0004
20#define MODULE_FLAG_BLACKLISTED 0x0008
21
22struct module_entry { /* I'll call it ME. */
23 unsigned flags;
24 char *modname; /* stripped of /path/, .ext and s/-/_/g */
25 const char *probed_name; /* verbatim as seen on cmdline */
26 llist_t *aliases; /* strings. aliases from config files */
27 llist_t *options; /* strings. options from config files */
28 llist_t *deps; /* strings. modules we depend on */
19}; 29};
20 30
21struct modprobe_conf { 31#define MODPROBE_OPTS "acdlnrt:VC:" USE_FEATURE_MODPROBE_BLACKLIST("b")
22 char probename[MODULE_NAME_LEN];
23 llist_t *options;
24 llist_t *aliases;
25#if ENABLE_FEATURE_MODPROBE_BLACKLIST
26#define add_to_blacklist(conf, name) llist_add_to(&conf->blacklist, name)
27#define check_blacklist(conf, name) (llist_find(conf->blacklist, name) == NULL)
28 llist_t *blacklist;
29#else
30#define add_to_blacklist(conf, name) do {} while (0)
31#define check_blacklist(conf, name) (1)
32#endif
33};
34
35#define MODPROBE_OPTS "acdlnrt:VC:" USE_FEATURE_MODPROBE_BLACKLIST("b")
36enum { 32enum {
37 MODPROBE_OPT_INSERT_ALL = (INSMOD_OPT_UNUSED << 0), /* a */ 33 MODPROBE_OPT_INSERT_ALL = (INSMOD_OPT_UNUSED << 0), /* a */
38 MODPROBE_OPT_DUMP_ONLY = (INSMOD_OPT_UNUSED << 1), /* c */ 34 MODPROBE_OPT_DUMP_ONLY = (INSMOD_OPT_UNUSED << 1), /* c */
@@ -46,30 +42,75 @@ enum {
46 MODPROBE_OPT_BLACKLIST = (INSMOD_OPT_UNUSED << 9) * ENABLE_FEATURE_MODPROBE_BLACKLIST, 42 MODPROBE_OPT_BLACKLIST = (INSMOD_OPT_UNUSED << 9) * ENABLE_FEATURE_MODPROBE_BLACKLIST,
47}; 43};
48 44
49static llist_t *loaded; 45struct globals {
46 llist_t *db; /* MEs of all modules ever seen (caching for speed) */
47 llist_t *probes; /* MEs of module(s) requested on cmdline */
48 llist_t *cmdline_mopts; /* strings. module options (from cmdline) */
49 int num_deps; /* what is this? */
50 /* bool. "Did we have 'symbol:FOO' requested on cmdline?" */
51 smallint need_symbols;
52};
53#define G (*(struct globals*)&bb_common_bufsiz1)
54#define INIT_G() do { } while (0)
50 55
51static int read_config(struct modprobe_conf *conf, const char *path);
52 56
53static void add_option(llist_t **all_opts, const char *module, const char *opts) 57static int read_config(const char *path);
58
59static struct module_entry *helper_get_module(const char *module, int create)
54{ 60{
55 struct modprobe_option *o; 61 char modname[MODULE_NAME_LEN];
62 struct module_entry *e;
63 llist_t *l;
64
65 filename2modname(module, modname);
66 for (l = G.db; l != NULL; l = l->link) {
67 e = (struct module_entry *) l->data;
68 if (strcmp(e->modname, modname) == 0)
69 return e;
70 }
71 if (!create)
72 return NULL;
56 73
57 o = xzalloc(sizeof(struct modprobe_option)); 74 e = xzalloc(sizeof(*e));
58 if (module) 75 e->modname = xstrdup(modname);
59 o->module = filename2modname(module, NULL); 76 llist_add_to(&G.db, e);
60 o->option = xstrdup(opts); 77
61 llist_add_to(all_opts, o); 78 return e;
79}
80static struct module_entry *get_or_add_modentry(const char *module)
81{
82 return helper_get_module(module, 1);
83}
84static struct module_entry *get_modentry(const char *module)
85{
86 return helper_get_module(module, 0);
87}
88
89static void add_probe(const char *name)
90{
91 struct module_entry *m;
92
93 m = get_or_add_modentry(name);
94 m->probed_name = name;
95 m->flags |= MODULE_FLAG_NEED_DEPS;
96 llist_add_to(&G.probes, m);
97
98 G.num_deps++;
99 if (ENABLE_FEATURE_MODUTILS_SYMBOLS
100 && strncmp(m->modname, "symbol:", 7) == 0)
101 G.need_symbols = 1;
62} 102}
63 103
64static int FAST_FUNC config_file_action(const char *filename, 104static int FAST_FUNC config_file_action(const char *filename,
65 struct stat *statbuf UNUSED_PARAM, 105 struct stat *statbuf UNUSED_PARAM,
66 void *userdata, 106 void *userdata UNUSED_PARAM,
67 int depth UNUSED_PARAM) 107 int depth UNUSED_PARAM)
68{ 108{
69 struct modprobe_conf *conf = (struct modprobe_conf *) userdata;
70 RESERVE_CONFIG_BUFFER(modname, MODULE_NAME_LEN); 109 RESERVE_CONFIG_BUFFER(modname, MODULE_NAME_LEN);
71 char *tokens[3]; 110 char *tokens[3], *rmod;
72 parser_t *p; 111 parser_t *p;
112 llist_t *l;
113 struct module_entry *m;
73 int rc = TRUE; 114 int rc = TRUE;
74 115
75 if (bb_basename(filename)[0] == '.') 116 if (bb_basename(filename)[0] == '.')
@@ -82,20 +123,39 @@ static int FAST_FUNC config_file_action(const char *filename,
82 } 123 }
83 124
84 while (config_read(p, tokens, 3, 2, "# \t", PARSE_NORMAL)) { 125 while (config_read(p, tokens, 3, 2, "# \t", PARSE_NORMAL)) {
126//Use index_in_strings?
85 if (strcmp(tokens[0], "alias") == 0) { 127 if (strcmp(tokens[0], "alias") == 0) {
86 filename2modname(tokens[1], modname); 128 filename2modname(tokens[1], modname);
87 if (tokens[2] && 129 if (tokens[2] == NULL)
88 fnmatch(modname, conf->probename, 0) == 0) 130 continue;
89 llist_add_to(&conf->aliases, 131
90 filename2modname(tokens[2], NULL)); 132 for (l = G.probes; l != NULL; l = l->link) {
133 m = (struct module_entry *) l->data;
134 if (fnmatch(modname, m->modname, 0) != 0)
135 continue;
136 rmod = filename2modname(tokens[2], NULL);
137 llist_add_to(&m->aliases, rmod);
138
139 if (m->flags & MODULE_FLAG_NEED_DEPS) {
140 m->flags &= ~MODULE_FLAG_NEED_DEPS;
141 G.num_deps--;
142 }
143
144 m = get_or_add_modentry(rmod);
145 m->flags |= MODULE_FLAG_NEED_DEPS;
146 G.num_deps++;
147 }
91 } else if (strcmp(tokens[0], "options") == 0) { 148 } else if (strcmp(tokens[0], "options") == 0) {
92 if (tokens[2]) 149 if (tokens[2] == NULL)
93 add_option(&conf->options, tokens[1], tokens[2]); 150 continue;
151 m = get_or_add_modentry(tokens[1]);
152 llist_add_to(&m->options, xstrdup(tokens[2]));
94 } else if (strcmp(tokens[0], "include") == 0) { 153 } else if (strcmp(tokens[0], "include") == 0) {
95 read_config(conf, tokens[1]); 154 read_config(tokens[1]);
96 } else if (ENABLE_FEATURE_MODPROBE_BLACKLIST && 155 } else if (ENABLE_FEATURE_MODPROBE_BLACKLIST
97 strcmp(tokens[0], "blacklist") == 0) { 156 && strcmp(tokens[0], "blacklist") == 0
98 add_to_blacklist(conf, xstrdup(tokens[1])); 157 ) {
158 get_or_add_modentry(tokens[1])->flags |= MODULE_FLAG_BLACKLISTED;
99 } 159 }
100 } 160 }
101 config_close(p); 161 config_close(p);
@@ -104,186 +164,198 @@ error:
104 return rc; 164 return rc;
105} 165}
106 166
107static int read_config(struct modprobe_conf *conf, const char *path) 167static int read_config(const char *path)
108{ 168{
109 return recursive_action(path, ACTION_RECURSE | ACTION_QUIET, 169 return recursive_action(path, ACTION_RECURSE | ACTION_QUIET,
110 config_file_action, NULL, conf, 1); 170 config_file_action, NULL, NULL, 1);
111} 171}
112 172
113static char *gather_options(llist_t *first, const char *module, int usecmdline) 173static char *gather_options(char *opts, llist_t *o)
114{ 174{
115 struct modprobe_option *opt; 175 int optlen;
116 llist_t *n;
117 char *opts = xstrdup("");
118 int optlen = 0;
119 176
120 for (n = first; n != NULL; n = n->link) { 177 if (opts == NULL)
121 opt = (struct modprobe_option *) n->data; 178 opts = xstrdup("");
179 optlen = strlen(opts);
122 180
123 if (opt->module == NULL && !usecmdline) 181 for (; o != NULL; o = o->link) {
124 continue; 182 opts = xrealloc(opts, optlen + strlen(o->data) + 2);
125 if (opt->module != NULL && strcmp(opt->module, module) != 0) 183 optlen += sprintf(opts + optlen, "%s ", o->data);
126 continue;
127
128 opts = xrealloc(opts, optlen + strlen(opt->option) + 2);
129 optlen += sprintf(opts + optlen, "%s ", opt->option);
130 } 184 }
131 return opts; 185 return opts;
132} 186}
133 187
134static int do_modprobe(struct modprobe_conf *conf, const char *module) 188static int do_modprobe(struct module_entry *m)
135{ 189{
136 RESERVE_CONFIG_BUFFER(modname, MODULE_NAME_LEN); 190 struct module_entry *m2;
137 llist_t *deps = NULL; 191 char *fn, *options;
138 char *fn, *options, *colon = NULL, *tokens[2];
139 parser_t *p;
140 int rc = -1; 192 int rc = -1;
141 193
142 p = config_open2(CONFIG_DEFAULT_DEPMOD_FILE, fopen_for_read); 194 if (!(m->flags & MODULE_FLAG_EXISTS))
143 /* Modprobe does not work at all without modprobe.dep, 195 return -ENOENT;
144 * even if the full module name is given. Returning error here
145 * was making us later confuse user with this message:
146 * "module /full/path/to/existing/file/module.ko not found".
147 * It's better to die immediately, with good message: */
148 if (p == NULL)
149 bb_perror_msg_and_die("can't open '%s'", CONFIG_DEFAULT_DEPMOD_FILE);
150
151 while (config_read(p, tokens, 2, 1, "# \t", PARSE_NORMAL)) {
152 colon = last_char_is(tokens[0], ':');
153 if (colon == NULL)
154 continue;
155
156 filename2modname(tokens[0], modname);
157 if (strcmp(modname, module) == 0)
158 break;
159
160 colon = NULL;
161 }
162 if (colon == NULL)
163 goto error_not_found;
164
165 colon[0] = '\0';
166 llist_add_to(&deps, xstrdup(tokens[0]));
167 if (tokens[1])
168 string_to_llist(tokens[1], &deps, " ");
169 196
170 if (!(option_mask32 & MODPROBE_OPT_REMOVE)) 197 if (!(option_mask32 & MODPROBE_OPT_REMOVE))
171 deps = llist_rev(deps); 198 m->deps = llist_rev(m->deps);
172 199
173 rc = 0; 200 rc = 0;
174 while (deps && rc == 0) { 201 while (m->deps && rc == 0) {
175 fn = llist_pop(&deps); 202 fn = llist_pop(&m->deps);
176 filename2modname(fn, modname); 203 m2 = get_or_add_modentry(fn);
177 if (option_mask32 & MODPROBE_OPT_REMOVE) { 204 if (option_mask32 & MODPROBE_OPT_REMOVE) {
178 if (bb_delete_module(modname, O_EXCL) != 0) 205 if (bb_delete_module(m->modname, O_EXCL) != 0)
179 rc = errno; 206 rc = errno;
180 } else if (llist_find(loaded, modname) == NULL) { 207 } else if (!(m2->flags & MODULE_FLAG_LOADED)) {
181 options = gather_options(conf->options, modname, 208 options = gather_options(NULL, m2->options);
182 strcmp(modname, module) == 0); 209 if (m == m2)
210 options = gather_options(options, G.cmdline_mopts);
211//TODO: looks like G.cmdline_mopts can contain either NULL or just one string, not more?
183 rc = bb_init_module(fn, options); 212 rc = bb_init_module(fn, options);
184 if (rc == 0) 213 if (rc == 0)
185 llist_add_to(&loaded, xstrdup(modname)); 214 m2->flags |= MODULE_FLAG_LOADED;
186 free(options); 215 free(options);
187 } 216 }
188 217
189 free(fn); 218 free(fn);
190 } 219 }
191 220
192 error_not_found: 221 if (rc > 0 && !(option_mask32 & INSMOD_OPT_SILENT)) {
193 config_close(p);
194
195 if (rc > 0 && !(option_mask32 & INSMOD_OPT_SILENT))
196 bb_error_msg("failed to %sload module %s: %s", 222 bb_error_msg("failed to %sload module %s: %s",
197 (option_mask32 & MODPROBE_OPT_REMOVE) ? "un" : "", 223 (option_mask32 & MODPROBE_OPT_REMOVE) ? "un" : "",
198 module, moderror(rc)); 224 m->probed_name ? m->probed_name : m->modname,
225 moderror(rc)
226 );
227 }
199 228
200 RELEASE_CONFIG_BUFFER(modname);
201 return rc; 229 return rc;
202} 230}
203 231
232static void load_modules_dep(void)
233{
234 struct module_entry *m;
235 char *colon, *tokens[2];
236 parser_t *p;
237
238 p = config_open2(CONFIG_DEFAULT_DEPMOD_FILE, xfopen_for_read);
239//Still true?
240 /* Modprobe does not work at all without modprobe.dep,
241 * even if the full module name is given. Returning error here
242 * was making us later confuse user with this message:
243 * "module /full/path/to/existing/file/module.ko not found".
244 * It's better to die immediately, with good message: */
245// if (p == NULL)
246// bb_perror_msg_and_die("can't open '%s'", CONFIG_DEFAULT_DEPMOD_FILE);
247
248 while (G.num_deps
249 && config_read(p, tokens, 2, 1, "# \t", PARSE_NORMAL)
250 ) {
251 colon = last_char_is(tokens[0], ':');
252 if (colon == NULL)
253 continue;
254 *colon = 0;
255
256 m = get_modentry(tokens[0]);
257 if (m == NULL)
258 continue;
259//Can we skip it too if it is MODULE_FLAG_LOADED?
260
261 m->flags |= MODULE_FLAG_EXISTS;
262 if ((m->flags & MODULE_FLAG_NEED_DEPS) && (m->deps == NULL)) {
263 G.num_deps--;
264 llist_add_to(&m->deps, xstrdup(tokens[0]));
265 if (tokens[1])
266 string_to_llist(tokens[1], &m->deps, " ");
267 }
268 }
269 config_close(p);
270}
271
204int modprobe_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; 272int modprobe_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
205int modprobe_main(int argc UNUSED_PARAM, char **argv) 273int modprobe_main(int argc UNUSED_PARAM, char **argv)
206{ 274{
207 struct utsname uts; 275 struct utsname uts;
208 int rc; 276 int rc;
209 unsigned opt; 277 unsigned opt;
210 llist_t *options = NULL; 278 struct module_entry *me;
211 279
212 opt_complementary = "q-v:v-q"; 280 opt_complementary = "q-v:v-q";
213 opt = getopt32(argv, INSMOD_OPTS MODPROBE_OPTS INSMOD_ARGS, 281 opt = getopt32(argv, INSMOD_OPTS MODPROBE_OPTS INSMOD_ARGS, NULL, NULL);
214 NULL, NULL);
215 argv += optind; 282 argv += optind;
216 283
217 if (opt & (MODPROBE_OPT_DUMP_ONLY | MODPROBE_OPT_LIST_ONLY | 284 if (opt & (MODPROBE_OPT_DUMP_ONLY | MODPROBE_OPT_LIST_ONLY |
218 MODPROBE_OPT_SHOW_ONLY)) 285 MODPROBE_OPT_SHOW_ONLY))
219 bb_error_msg_and_die("not supported"); 286 bb_error_msg_and_die("not supported");
220 287
221 /* goto modules location */ 288 /* Goto modules location */
222 xchdir(CONFIG_DEFAULT_MODULES_DIR); 289 xchdir(CONFIG_DEFAULT_MODULES_DIR);
223 uname(&uts); 290 uname(&uts);
224 xchdir(uts.release); 291 xchdir(uts.release);
225 292
226 if (!argv[0]) { 293 if (!argv[0]) {
227 if (opt & MODPROBE_OPT_REMOVE) { 294 if (opt & MODPROBE_OPT_REMOVE) {
295 /* TODO: comment here */
228 if (bb_delete_module(NULL, O_NONBLOCK|O_EXCL) != 0) 296 if (bb_delete_module(NULL, O_NONBLOCK|O_EXCL) != 0)
229 bb_perror_msg_and_die("rmmod"); 297 bb_perror_msg_and_die("rmmod");
230 } 298 }
231 return EXIT_SUCCESS; 299 return EXIT_SUCCESS;
232 } 300 }
233 if (!(opt & MODPROBE_OPT_INSERT_ALL)) { 301
234 /* If not -a, we have only one module name, 302 if (opt & MODPROBE_OPT_INSERT_ALL) {
235 * the rest of parameters are options */ 303 /* Each argument is a module name */
236 add_option(&options, NULL, parse_cmdline_module_options(argv)); 304 do {
237 argv[1] = NULL; 305 add_probe(*argv++);
306 } while (*argv);
307 G.probes = llist_rev(G.probes);
308 } else {
309 /* First argument is module name, rest are parameters */
310 add_probe(argv[0]);
311//TODO: looks like G.cmdline_mopts can contain either NULL or just one string, not more?
312//optimize it them
313 llist_add_to(&G.cmdline_mopts, parse_cmdline_module_options(argv));
238 } 314 }
239 315
240 /* cache modules */ 316 /* Retrieve module names of already loaded modules */
241 { 317 {
242 char *s; 318 char *s;
243 parser_t *parser = config_open2("/proc/modules", fopen_for_read); 319 parser_t *parser = config_open2("/proc/modules", fopen_for_read);
244 while (config_read(parser, &s, 1, 1, "# \t", PARSE_NORMAL & ~PARSE_GREEDY)) 320 while (config_read(parser, &s, 1, 1, "# \t", PARSE_NORMAL & ~PARSE_GREEDY))
245 llist_add_to(&loaded, xstrdup(s)); 321 get_or_add_modentry(s)->flags |= MODULE_FLAG_LOADED;
246 config_close(parser); 322 config_close(parser);
247 } 323 }
248 324
249 while (*argv) { 325 read_config("/etc/modprobe.conf");
250 const char *arg = *argv; 326 read_config("/etc/modprobe.d");
251 struct modprobe_conf *conf; 327 if (ENABLE_FEATURE_MODUTILS_SYMBOLS && G.need_symbols)
252 328 read_config("modules.symbols");
253 conf = xzalloc(sizeof(*conf)); 329 load_modules_dep();
254 conf->options = options; 330 if (ENABLE_FEATURE_MODUTILS_ALIAS && G.num_deps) {
255 filename2modname(arg, conf->probename); 331 read_config("modules.alias");
256 read_config(conf, "/etc/modprobe.conf"); 332 load_modules_dep();
257 read_config(conf, "/etc/modprobe.d"); 333 }
258 if (ENABLE_FEATURE_MODUTILS_SYMBOLS
259 && conf->aliases == NULL
260 && strncmp(arg, "symbol:", 7) == 0
261 ) {
262 read_config(conf, "modules.symbols");
263 }
264
265 if (ENABLE_FEATURE_MODUTILS_ALIAS && conf->aliases == NULL)
266 read_config(conf, "modules.alias");
267 334
268 if (conf->aliases == NULL) { 335 while ((me = llist_pop(&G.probes)) != NULL) {
336 if (me->aliases == NULL) {
269 /* Try if module by literal name is found; literal 337 /* Try if module by literal name is found; literal
270 * names are blacklist only if '-b' is given. */ 338 * names are blacklisted only if '-b' is given. */
271 if (!(opt & MODPROBE_OPT_BLACKLIST) || 339 if (!(opt & MODPROBE_OPT_BLACKLIST)
272 check_blacklist(conf, conf->probename)) { 340 || !(me->flags & MODULE_FLAG_BLACKLISTED)
273 rc = do_modprobe(conf, conf->probename); 341 ) {
342 rc = do_modprobe(me);
274 if (rc < 0 && !(opt & INSMOD_OPT_SILENT)) 343 if (rc < 0 && !(opt & INSMOD_OPT_SILENT))
275 bb_error_msg("module %s not found", arg); 344 bb_error_msg("module %s not found",
345 me->probed_name);
276 } 346 }
277 } else { 347 } else {
278 /* Probe all aliases */ 348 /* Probe all aliases */
279 while (conf->aliases != NULL) { 349 do {
280 char *realname = llist_pop(&conf->aliases); 350 char *realname = llist_pop(&me->aliases);
281 if (check_blacklist(conf, realname)) 351 struct module_entry *m2;
282 do_modprobe(conf, realname); 352
353 m2 = get_or_add_modentry(realname);
354 if (!(m2->flags & MODULE_FLAG_BLACKLISTED))
355 do_modprobe(m2);
283 free(realname); 356 free(realname);
284 } 357 } while (me->aliases != NULL);
285 } 358 }
286 argv++;
287 } 359 }
288 360
289 return EXIT_SUCCESS; 361 return EXIT_SUCCESS;