Annotation of /trunk/mkinitrd-magellan/busybox/modutils/modprobe.c
Parent Directory | Revision Log
Revision 532 -
(hide annotations)
(download)
Sat Sep 1 22:45:15 2007 UTC (16 years, 9 months ago) by niro
File MIME type: text/plain
File size: 23012 byte(s)
Sat Sep 1 22:45:15 2007 UTC (16 years, 9 months ago) by niro
File MIME type: text/plain
File size: 23012 byte(s)
-import if magellan mkinitrd; it is a fork of redhats mkinitrd-5.0.8 with all magellan patches and features; deprecates magellan-src/mkinitrd
1 | niro | 532 | /* vi: set sw=4 ts=4: */ |
2 | /* | ||
3 | * Modprobe written from scratch for BusyBox | ||
4 | * | ||
5 | * Copyright (c) 2002 by Robert Griebl, griebl@gmx.de | ||
6 | * Copyright (c) 2003 by Andrew Dennison, andrew.dennison@motec.com.au | ||
7 | * Copyright (c) 2005 by Jim Bauer, jfbauer@nfr.com | ||
8 | * | ||
9 | * Portions Copyright (c) 2005 by Yann E. MORIN, yann.morin.1998@anciens.enib.fr | ||
10 | * | ||
11 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. | ||
12 | */ | ||
13 | |||
14 | #include "busybox.h" | ||
15 | #include <sys/utsname.h> | ||
16 | #include <fnmatch.h> | ||
17 | |||
18 | struct mod_opt_t { /* one-way list of options to pass to a module */ | ||
19 | char * m_opt_val; | ||
20 | struct mod_opt_t * m_next; | ||
21 | }; | ||
22 | |||
23 | struct dep_t { /* one-way list of dependency rules */ | ||
24 | /* a dependency rule */ | ||
25 | char * m_name; /* the module name*/ | ||
26 | char * m_path; /* the module file path */ | ||
27 | struct mod_opt_t * m_options; /* the module options */ | ||
28 | |||
29 | int m_isalias : 1; /* the module is an alias */ | ||
30 | int m_reserved : 15; /* stuffin' */ | ||
31 | |||
32 | int m_depcnt : 16; /* the number of dependable module(s) */ | ||
33 | char ** m_deparr; /* the list of dependable module(s) */ | ||
34 | |||
35 | struct dep_t * m_next; /* the next dependency rule */ | ||
36 | }; | ||
37 | |||
38 | struct mod_list_t { /* two-way list of modules to process */ | ||
39 | /* a module description */ | ||
40 | char * m_name; | ||
41 | char * m_path; | ||
42 | struct mod_opt_t * m_options; | ||
43 | |||
44 | struct mod_list_t * m_prev; | ||
45 | struct mod_list_t * m_next; | ||
46 | }; | ||
47 | |||
48 | |||
49 | static struct dep_t *depend; | ||
50 | |||
51 | #define main_options "acdklnqrst:vVC:" | ||
52 | #define INSERT_ALL 1 /* a */ | ||
53 | #define DUMP_CONF_EXIT 2 /* c */ | ||
54 | #define D_OPT_IGNORED 4 /* d */ | ||
55 | #define AUTOCLEAN_FLG 8 /* k */ | ||
56 | #define LIST_ALL 16 /* l */ | ||
57 | #define SHOW_ONLY 32 /* n */ | ||
58 | #define QUIET 64 /* q */ | ||
59 | #define REMOVE_OPT 128 /* r */ | ||
60 | #define DO_SYSLOG 256 /* s */ | ||
61 | #define RESTRICT_DIR 512 /* t */ | ||
62 | #define VERBOSE 1024 /* v */ | ||
63 | #define VERSION_ONLY 2048 /* V */ | ||
64 | #define CONFIG_FILE 4096 /* C */ | ||
65 | |||
66 | #define autoclean (main_opts & AUTOCLEAN_FLG) | ||
67 | #define show_only (main_opts & SHOW_ONLY) | ||
68 | #define quiet (main_opts & QUIET) | ||
69 | #define remove_opt (main_opts & REMOVE_OPT) | ||
70 | #define do_syslog (main_opts & DO_SYSLOG) | ||
71 | #define verbose (main_opts & VERBOSE) | ||
72 | |||
73 | static int main_opts; | ||
74 | |||
75 | static int parse_tag_value(char *buffer, char **ptag, char **pvalue) | ||
76 | { | ||
77 | char *tag, *value; | ||
78 | |||
79 | buffer = skip_whitespace(buffer); | ||
80 | tag = value = buffer; | ||
81 | while (!isspace(*value)) | ||
82 | if (!*value) return 0; | ||
83 | else value++; | ||
84 | *value++ = 0; | ||
85 | value = skip_whitespace(value); | ||
86 | if (!*value) return 0; | ||
87 | |||
88 | *ptag = tag; | ||
89 | *pvalue = value; | ||
90 | |||
91 | return 1; | ||
92 | } | ||
93 | |||
94 | /* | ||
95 | * This function appends an option to a list | ||
96 | */ | ||
97 | static struct mod_opt_t *append_option(struct mod_opt_t *opt_list, char *opt) | ||
98 | { | ||
99 | struct mod_opt_t *ol = opt_list; | ||
100 | |||
101 | if (ol) { | ||
102 | while (ol->m_next) { | ||
103 | ol = ol->m_next; | ||
104 | } | ||
105 | ol->m_next = xmalloc(sizeof(struct mod_opt_t)); | ||
106 | ol = ol->m_next; | ||
107 | } else { | ||
108 | ol = opt_list = xmalloc(sizeof(struct mod_opt_t)); | ||
109 | } | ||
110 | |||
111 | ol->m_opt_val = xstrdup(opt); | ||
112 | ol->m_next = NULL; | ||
113 | |||
114 | return opt_list; | ||
115 | } | ||
116 | |||
117 | #if ENABLE_FEATURE_MODPROBE_MULTIPLE_OPTIONS | ||
118 | /* static char* parse_command_string(char* src, char **dst); | ||
119 | * src: pointer to string containing argument | ||
120 | * dst: pointer to where to store the parsed argument | ||
121 | * return value: the pointer to the first char after the parsed argument, | ||
122 | * NULL if there was no argument parsed (only trailing spaces). | ||
123 | * Note that memory is allocated with xstrdup when a new argument was | ||
124 | * parsed. Don't forget to free it! | ||
125 | */ | ||
126 | #define ARG_EMPTY 0x00 | ||
127 | #define ARG_IN_DQUOTES 0x01 | ||
128 | #define ARG_IN_SQUOTES 0x02 | ||
129 | static char *parse_command_string(char *src, char **dst) | ||
130 | { | ||
131 | int opt_status = ARG_EMPTY; | ||
132 | char* tmp_str; | ||
133 | |||
134 | /* Dumb you, I have nothing to do... */ | ||
135 | if (src == NULL) return src; | ||
136 | |||
137 | /* Skip leading spaces */ | ||
138 | while (*src == ' ') { | ||
139 | src++; | ||
140 | } | ||
141 | /* Is the end of string reached? */ | ||
142 | if (*src == '\0') { | ||
143 | return NULL; | ||
144 | } | ||
145 | /* Reached the start of an argument | ||
146 | * By the way, we duplicate a little too much | ||
147 | * here but what is too much is freed later. */ | ||
148 | *dst = tmp_str = xstrdup(src); | ||
149 | /* Get to the end of that argument */ | ||
150 | while (*tmp_str != '\0' | ||
151 | && (*tmp_str != ' ' || (opt_status & (ARG_IN_DQUOTES | ARG_IN_SQUOTES))) | ||
152 | ) { | ||
153 | switch (*tmp_str) { | ||
154 | case '\'': | ||
155 | if (opt_status & ARG_IN_DQUOTES) { | ||
156 | /* Already in double quotes, keep current char as is */ | ||
157 | } else { | ||
158 | /* shift left 1 char, until end of string: get rid of the opening/closing quotes */ | ||
159 | memmove(tmp_str, tmp_str + 1, strlen(tmp_str)); | ||
160 | /* mark me: we enter or leave single quotes */ | ||
161 | opt_status ^= ARG_IN_SQUOTES; | ||
162 | /* Back one char, as we need to re-scan the new char there. */ | ||
163 | tmp_str--; | ||
164 | } | ||
165 | break; | ||
166 | case '"': | ||
167 | if (opt_status & ARG_IN_SQUOTES) { | ||
168 | /* Already in single quotes, keep current char as is */ | ||
169 | } else { | ||
170 | /* shift left 1 char, until end of string: get rid of the opening/closing quotes */ | ||
171 | memmove(tmp_str, tmp_str + 1, strlen(tmp_str)); | ||
172 | /* mark me: we enter or leave double quotes */ | ||
173 | opt_status ^= ARG_IN_DQUOTES; | ||
174 | /* Back one char, as we need to re-scan the new char there. */ | ||
175 | tmp_str--; | ||
176 | } | ||
177 | break; | ||
178 | case '\\': | ||
179 | if (opt_status & ARG_IN_SQUOTES) { | ||
180 | /* Between single quotes: keep as is. */ | ||
181 | } else { | ||
182 | switch (*(tmp_str+1)) { | ||
183 | case 'a': | ||
184 | case 'b': | ||
185 | case 't': | ||
186 | case 'n': | ||
187 | case 'v': | ||
188 | case 'f': | ||
189 | case 'r': | ||
190 | case '0': | ||
191 | /* We escaped a special character. For now, keep | ||
192 | * both the back-slash and the following char. */ | ||
193 | tmp_str++; src++; | ||
194 | break; | ||
195 | default: | ||
196 | /* We escaped a space or a single or double quote, | ||
197 | * or a back-slash, or a non-escapable char. Remove | ||
198 | * the '\' and keep the new current char as is. */ | ||
199 | memmove(tmp_str, tmp_str + 1, strlen(tmp_str)); | ||
200 | break; | ||
201 | } | ||
202 | } | ||
203 | break; | ||
204 | /* Any other char that is special shall appear here. | ||
205 | * Example: $ starts a variable | ||
206 | case '$': | ||
207 | do_variable_expansion(); | ||
208 | break; | ||
209 | * */ | ||
210 | default: | ||
211 | /* any other char is kept as is. */ | ||
212 | break; | ||
213 | } | ||
214 | tmp_str++; /* Go to next char */ | ||
215 | src++; /* Go to next char to find the end of the argument. */ | ||
216 | } | ||
217 | /* End of string, but still no ending quote */ | ||
218 | if (opt_status & (ARG_IN_DQUOTES | ARG_IN_SQUOTES)) { | ||
219 | bb_error_msg_and_die("unterminated (single or double) quote in options list: %s", src); | ||
220 | } | ||
221 | *tmp_str++ = '\0'; | ||
222 | *dst = xrealloc(*dst, (tmp_str - *dst)); | ||
223 | return src; | ||
224 | } | ||
225 | #else | ||
226 | #define parse_command_string(src, dst) (0) | ||
227 | #endif /* ENABLE_FEATURE_MODPROBE_MULTIPLE_OPTIONS */ | ||
228 | |||
229 | /* | ||
230 | * This function reads aliases and default module options from a configuration file | ||
231 | * (/etc/modprobe.conf syntax). It supports includes (only files, no directories). | ||
232 | */ | ||
233 | static void include_conf(struct dep_t **first, struct dep_t **current, char *buffer, int buflen, int fd) | ||
234 | { | ||
235 | int continuation_line = 0; | ||
236 | |||
237 | // alias parsing is not 100% correct (no correct handling of continuation lines within an alias) ! | ||
238 | |||
239 | while (reads(fd, buffer, buflen)) { | ||
240 | int l; | ||
241 | char *p; | ||
242 | |||
243 | p = strchr(buffer, '#'); | ||
244 | if (p) | ||
245 | *p = 0; | ||
246 | |||
247 | l = strlen(buffer); | ||
248 | |||
249 | while (l && isspace(buffer[l-1])) { | ||
250 | buffer[l-1] = 0; | ||
251 | l--; | ||
252 | } | ||
253 | |||
254 | if (l == 0) { | ||
255 | continuation_line = 0; | ||
256 | continue; | ||
257 | } | ||
258 | |||
259 | if (!continuation_line) { | ||
260 | if ((strncmp(buffer, "alias", 5) == 0) && isspace(buffer[5])) { | ||
261 | char *alias, *mod; | ||
262 | |||
263 | if (parse_tag_value(buffer + 6, &alias, &mod)) { | ||
264 | /* handle alias as a module dependent on the aliased module */ | ||
265 | if (!*current) { | ||
266 | (*first) = (*current) = xzalloc(sizeof(struct dep_t)); | ||
267 | } | ||
268 | else { | ||
269 | (*current)->m_next = xzalloc(sizeof(struct dep_t)); | ||
270 | (*current) = (*current)->m_next; | ||
271 | } | ||
272 | (*current)->m_name = xstrdup(alias); | ||
273 | (*current)->m_isalias = 1; | ||
274 | |||
275 | if ((strcmp(mod, "off") == 0) || (strcmp(mod, "null") == 0)) { | ||
276 | (*current)->m_depcnt = 0; | ||
277 | (*current)->m_deparr = 0; | ||
278 | } | ||
279 | else { | ||
280 | (*current)->m_depcnt = 1; | ||
281 | (*current)->m_deparr = xmalloc(1 * sizeof(char *)); | ||
282 | (*current)->m_deparr[0] = xstrdup(mod); | ||
283 | } | ||
284 | (*current)->m_next = 0; | ||
285 | } | ||
286 | } | ||
287 | else if ((strncmp(buffer, "options", 7) == 0) && isspace(buffer[7])) { | ||
288 | char *mod, *opt; | ||
289 | |||
290 | /* split the line in the module/alias name, and options */ | ||
291 | if (parse_tag_value(buffer + 8, &mod, &opt)) { | ||
292 | struct dep_t *dt; | ||
293 | |||
294 | /* find the corresponding module */ | ||
295 | for (dt = *first; dt; dt = dt->m_next) { | ||
296 | if (strcmp(dt->m_name, mod) == 0) | ||
297 | break; | ||
298 | } | ||
299 | if (dt) { | ||
300 | if (ENABLE_FEATURE_MODPROBE_MULTIPLE_OPTIONS) { | ||
301 | char* new_opt = NULL; | ||
302 | while ((opt = parse_command_string(opt, &new_opt))) { | ||
303 | dt->m_options = append_option(dt->m_options, new_opt); | ||
304 | } | ||
305 | } else { | ||
306 | dt->m_options = append_option(dt->m_options, opt); | ||
307 | } | ||
308 | } | ||
309 | } | ||
310 | } | ||
311 | else if ((strncmp(buffer, "include", 7) == 0) && isspace(buffer[7])) { | ||
312 | int fdi; char *filename; | ||
313 | |||
314 | filename = skip_whitespace(buffer + 8); | ||
315 | |||
316 | if ((fdi = open(filename, O_RDONLY)) >= 0) { | ||
317 | include_conf(first, current, buffer, buflen, fdi); | ||
318 | close(fdi); | ||
319 | } | ||
320 | } | ||
321 | } | ||
322 | } | ||
323 | } | ||
324 | |||
325 | /* | ||
326 | * This function builds a list of dependency rules from /lib/modules/`uname -r`\modules.dep. | ||
327 | * It then fills every modules and aliases with their default options, found by parsing | ||
328 | * modprobe.conf (or modules.conf, or conf.modules). | ||
329 | */ | ||
330 | static struct dep_t *build_dep(void) | ||
331 | { | ||
332 | int fd; | ||
333 | struct utsname un; | ||
334 | struct dep_t *first = 0; | ||
335 | struct dep_t *current = 0; | ||
336 | char buffer[2048]; | ||
337 | char *filename; | ||
338 | int continuation_line = 0; | ||
339 | int k_version; | ||
340 | |||
341 | if (uname(&un)) | ||
342 | bb_error_msg_and_die("can't determine kernel version"); | ||
343 | |||
344 | k_version = 0; | ||
345 | if (un.release[0] == '2') { | ||
346 | k_version = un.release[2] - '0'; | ||
347 | } | ||
348 | |||
349 | filename = xasprintf("/lib/modules/%s/modules.dep", un.release); | ||
350 | fd = open(filename, O_RDONLY); | ||
351 | if (ENABLE_FEATURE_CLEAN_UP) | ||
352 | free(filename); | ||
353 | if (fd < 0) { | ||
354 | /* Ok, that didn't work. Fall back to looking in /lib/modules */ | ||
355 | fd = open("/lib/modules/modules.dep", O_RDONLY); | ||
356 | if (fd < 0) { | ||
357 | return 0; | ||
358 | } | ||
359 | } | ||
360 | |||
361 | while (reads(fd, buffer, sizeof(buffer))) { | ||
362 | int l = strlen(buffer); | ||
363 | char *p = 0; | ||
364 | |||
365 | while (l > 0 && isspace(buffer[l-1])) { | ||
366 | buffer[l-1] = 0; | ||
367 | l--; | ||
368 | } | ||
369 | |||
370 | if (l == 0) { | ||
371 | continuation_line = 0; | ||
372 | continue; | ||
373 | } | ||
374 | |||
375 | /* Is this a new module dep description? */ | ||
376 | if (!continuation_line) { | ||
377 | /* find the dep beginning */ | ||
378 | char *col = strchr(buffer, ':'); | ||
379 | char *dot = col; | ||
380 | |||
381 | if (col) { | ||
382 | /* This line is a dep description */ | ||
383 | char *mods; | ||
384 | char *modpath; | ||
385 | char *mod; | ||
386 | |||
387 | /* Find the beginning of the module file name */ | ||
388 | *col = 0; | ||
389 | mods = strrchr(buffer, '/'); | ||
390 | |||
391 | if (!mods) | ||
392 | mods = buffer; /* no path for this module */ | ||
393 | else | ||
394 | mods++; /* there was a path for this module... */ | ||
395 | |||
396 | /* find the path of the module */ | ||
397 | modpath = strchr(buffer, '/'); /* ... and this is the path */ | ||
398 | if (!modpath) | ||
399 | modpath = buffer; /* module with no path */ | ||
400 | /* find the end of the module name in the file name */ | ||
401 | if (ENABLE_FEATURE_2_6_MODULES && | ||
402 | (k_version > 4) && (*(col-3) == '.') && | ||
403 | (*(col-2) == 'k') && (*(col-1) == 'o')) | ||
404 | dot = col - 3; | ||
405 | else | ||
406 | if ((*(col-2) == '.') && (*(col-1) == 'o')) | ||
407 | dot = col - 2; | ||
408 | |||
409 | mod = xstrndup(mods, dot - mods); | ||
410 | |||
411 | /* enqueue new module */ | ||
412 | if (!current) { | ||
413 | first = current = xmalloc(sizeof(struct dep_t)); | ||
414 | } | ||
415 | else { | ||
416 | current->m_next = xmalloc(sizeof(struct dep_t)); | ||
417 | current = current->m_next; | ||
418 | } | ||
419 | current->m_name = mod; | ||
420 | current->m_path = xstrdup(modpath); | ||
421 | current->m_options = NULL; | ||
422 | current->m_isalias = 0; | ||
423 | current->m_depcnt = 0; | ||
424 | current->m_deparr = 0; | ||
425 | current->m_next = 0; | ||
426 | |||
427 | p = col + 1; | ||
428 | } | ||
429 | else | ||
430 | /* this line is not a dep description */ | ||
431 | p = 0; | ||
432 | } | ||
433 | else | ||
434 | /* It's a dep description continuation */ | ||
435 | p = buffer; | ||
436 | |||
437 | while (p && *p && isblank(*p)) | ||
438 | p++; | ||
439 | |||
440 | /* p points to the first dependable module; if NULL, no dependable module */ | ||
441 | if (p && *p) { | ||
442 | char *end = &buffer[l-1]; | ||
443 | char *deps; | ||
444 | char *dep; | ||
445 | char *next; | ||
446 | int ext = 0; | ||
447 | |||
448 | while (isblank(*end) || (*end == '\\')) | ||
449 | end--; | ||
450 | |||
451 | do { | ||
452 | /* search the end of the dependency */ | ||
453 | next = strchr(p, ' '); | ||
454 | if (next) { | ||
455 | *next = 0; | ||
456 | next--; | ||
457 | } | ||
458 | else | ||
459 | next = end; | ||
460 | |||
461 | /* find the beginning of the module file name */ | ||
462 | deps = strrchr(p, '/'); | ||
463 | |||
464 | if (!deps || (deps < p)) { | ||
465 | deps = p; | ||
466 | |||
467 | while (isblank(*deps)) | ||
468 | deps++; | ||
469 | } else | ||
470 | deps++; | ||
471 | |||
472 | /* find the end of the module name in the file name */ | ||
473 | if (ENABLE_FEATURE_2_6_MODULES | ||
474 | && (k_version > 4) && (*(next-2) == '.') | ||
475 | && (*(next-1) == 'k') && (*next == 'o')) | ||
476 | ext = 3; | ||
477 | else | ||
478 | if ((*(next-1) == '.') && (*next == 'o')) | ||
479 | ext = 2; | ||
480 | |||
481 | /* Cope with blank lines */ | ||
482 | if ((next-deps-ext+1) <= 0) | ||
483 | continue; | ||
484 | dep = xstrndup(deps, next - deps - ext + 1); | ||
485 | |||
486 | /* Add the new dependable module name */ | ||
487 | current->m_depcnt++; | ||
488 | current->m_deparr = xrealloc(current->m_deparr, | ||
489 | sizeof(char *) * current->m_depcnt); | ||
490 | current->m_deparr[current->m_depcnt - 1] = dep; | ||
491 | |||
492 | p = next + 2; | ||
493 | } while (next < end); | ||
494 | } | ||
495 | |||
496 | /* is there other dependable module(s) ? */ | ||
497 | if (buffer[l-1] == '\\') | ||
498 | continuation_line = 1; | ||
499 | else | ||
500 | continuation_line = 0; | ||
501 | } | ||
502 | close(fd); | ||
503 | |||
504 | /* | ||
505 | * First parse system-specific options and aliases | ||
506 | * as they take precedence over the kernel ones. | ||
507 | */ | ||
508 | if (!ENABLE_FEATURE_2_6_MODULES | ||
509 | || (fd = open("/etc/modprobe.conf", O_RDONLY)) < 0) | ||
510 | if ((fd = open("/etc/modules.conf", O_RDONLY)) < 0) | ||
511 | fd = open("/etc/conf.modules", O_RDONLY); | ||
512 | |||
513 | if (fd >= 0) { | ||
514 | include_conf(&first, ¤t, buffer, sizeof(buffer), fd); | ||
515 | close(fd); | ||
516 | } | ||
517 | |||
518 | /* Only 2.6 has a modules.alias file */ | ||
519 | if (ENABLE_FEATURE_2_6_MODULES) { | ||
520 | /* Parse kernel-declared aliases */ | ||
521 | filename = xasprintf("/lib/modules/%s/modules.alias", un.release); | ||
522 | fd = open(filename, O_RDONLY); | ||
523 | if (fd < 0) { | ||
524 | /* Ok, that didn't work. Fall back to looking in /lib/modules */ | ||
525 | fd = open("/lib/modules/modules.alias", O_RDONLY); | ||
526 | } | ||
527 | if (ENABLE_FEATURE_CLEAN_UP) | ||
528 | free(filename); | ||
529 | |||
530 | if (fd >= 0) { | ||
531 | include_conf(&first, ¤t, buffer, sizeof(buffer), fd); | ||
532 | close(fd); | ||
533 | } | ||
534 | } | ||
535 | |||
536 | return first; | ||
537 | } | ||
538 | |||
539 | /* return 1 = loaded, 0 = not loaded, -1 = can't tell */ | ||
540 | static int already_loaded(const char *name) | ||
541 | { | ||
542 | int fd, ret = 0; | ||
543 | char buffer[4096]; | ||
544 | |||
545 | fd = open("/proc/modules", O_RDONLY); | ||
546 | if (fd < 0) | ||
547 | return -1; | ||
548 | |||
549 | while (reads(fd, buffer, sizeof(buffer))) { | ||
550 | char *p; | ||
551 | |||
552 | p = strchr (buffer, ' '); | ||
553 | if (p) { | ||
554 | const char *n; | ||
555 | |||
556 | // Truncate buffer at first space and check for matches, with | ||
557 | // the idiosyncrasy that _ and - are interchangeable because the | ||
558 | // 2.6 kernel does weird things. | ||
559 | |||
560 | *p = 0; | ||
561 | for (p = buffer, n = name; ; p++, n++) { | ||
562 | if (*p != *n) { | ||
563 | if ((*p == '_' || *p == '-') && (*n == '_' || *n == '-')) | ||
564 | continue; | ||
565 | break; | ||
566 | } | ||
567 | // If we made it to the end, that's a match. | ||
568 | if (!*p) { | ||
569 | ret = 1; | ||
570 | goto done; | ||
571 | } | ||
572 | } | ||
573 | } | ||
574 | } | ||
575 | done: | ||
576 | close (fd); | ||
577 | return ret; | ||
578 | } | ||
579 | |||
580 | static int mod_process(struct mod_list_t *list, int do_insert) | ||
581 | { | ||
582 | int rc = 0; | ||
583 | char **argv = NULL; | ||
584 | struct mod_opt_t *opts; | ||
585 | int argc_malloc; /* never used when CONFIG_FEATURE_CLEAN_UP not defined */ | ||
586 | int argc; | ||
587 | |||
588 | while (list) { | ||
589 | argc = 0; | ||
590 | if (ENABLE_FEATURE_CLEAN_UP) | ||
591 | argc_malloc = 0; | ||
592 | /* If CONFIG_FEATURE_CLEAN_UP is not defined, then we leak memory | ||
593 | * each time we allocate memory for argv. | ||
594 | * But it is (quite) small amounts of memory that leak each | ||
595 | * time a module is loaded, and it is reclaimed when modprobe | ||
596 | * exits anyway (even when standalone shell?). | ||
597 | * This could become a problem when loading a module with LOTS of | ||
598 | * dependencies, with LOTS of options for each dependencies, with | ||
599 | * very little memory on the target... But in that case, the module | ||
600 | * would not load because there is no more memory, so there's no | ||
601 | * problem. */ | ||
602 | /* enough for minimal insmod (5 args + NULL) or rmmod (3 args + NULL) */ | ||
603 | argv = xmalloc(6 * sizeof(char*)); | ||
604 | if (do_insert) { | ||
605 | if (already_loaded(list->m_name) != 1) { | ||
606 | argv[argc++] = "insmod"; | ||
607 | if (ENABLE_FEATURE_2_4_MODULES) { | ||
608 | if (do_syslog) | ||
609 | argv[argc++] = "-s"; | ||
610 | if (autoclean) | ||
611 | argv[argc++] = "-k"; | ||
612 | if (quiet) | ||
613 | argv[argc++] = "-q"; | ||
614 | else if (verbose) /* verbose and quiet are mutually exclusive */ | ||
615 | argv[argc++] = "-v"; | ||
616 | } | ||
617 | argv[argc++] = list->m_path; | ||
618 | if (ENABLE_FEATURE_CLEAN_UP) | ||
619 | argc_malloc = argc; | ||
620 | opts = list->m_options; | ||
621 | while (opts) { | ||
622 | /* Add one more option */ | ||
623 | argc++; | ||
624 | argv = xrealloc(argv,(argc + 1)* sizeof(char*)); | ||
625 | argv[argc-1] = opts->m_opt_val; | ||
626 | opts = opts->m_next; | ||
627 | } | ||
628 | } | ||
629 | } else { | ||
630 | /* modutils uses short name for removal */ | ||
631 | if (already_loaded(list->m_name) != 0) { | ||
632 | argv[argc++] = "rmmod"; | ||
633 | if (do_syslog) | ||
634 | argv[argc++] = "-s"; | ||
635 | argv[argc++] = list->m_name; | ||
636 | if (ENABLE_FEATURE_CLEAN_UP) | ||
637 | argc_malloc = argc; | ||
638 | } | ||
639 | } | ||
640 | argv[argc] = NULL; | ||
641 | |||
642 | if (argc) { | ||
643 | if (verbose) { | ||
644 | printf("%s module %s\n", do_insert?"Loading":"Unloading", list->m_name); | ||
645 | } | ||
646 | if (!show_only) { | ||
647 | int rc2 = wait4pid(spawn(argv)); | ||
648 | |||
649 | if (do_insert) { | ||
650 | rc = rc2; /* only last module matters */ | ||
651 | } | ||
652 | else if (!rc2) { | ||
653 | rc = 0; /* success if remove any mod */ | ||
654 | } | ||
655 | } | ||
656 | if (ENABLE_FEATURE_CLEAN_UP) { | ||
657 | /* the last value in the array has index == argc, but | ||
658 | * it is the terminating NULL, so we must not free it. */ | ||
659 | while (argc_malloc < argc) { | ||
660 | free(argv[argc_malloc++]); | ||
661 | } | ||
662 | } | ||
663 | } | ||
664 | if (ENABLE_FEATURE_CLEAN_UP) { | ||
665 | free(argv); | ||
666 | argv = NULL; | ||
667 | } | ||
668 | list = do_insert ? list->m_prev : list->m_next; | ||
669 | } | ||
670 | return (show_only) ? 0 : rc; | ||
671 | } | ||
672 | |||
673 | /* | ||
674 | * Check the matching between a pattern and a module name. | ||
675 | * We need this as *_* is equivalent to *-*, even in pattern matching. | ||
676 | */ | ||
677 | static int check_pattern(const char* pat_src, const char* mod_src) { | ||
678 | int ret; | ||
679 | |||
680 | if (ENABLE_FEATURE_MODPROBE_FANCY_ALIAS) { | ||
681 | char* pat; | ||
682 | char* mod; | ||
683 | char* p; | ||
684 | |||
685 | pat = xstrdup (pat_src); | ||
686 | mod = xstrdup (mod_src); | ||
687 | |||
688 | for (p = pat; (p = strchr(p, '-')); *p++ = '_'); | ||
689 | for (p = mod; (p = strchr(p, '-')); *p++ = '_'); | ||
690 | |||
691 | ret = fnmatch(pat, mod, 0); | ||
692 | |||
693 | if (ENABLE_FEATURE_CLEAN_UP) { | ||
694 | free (pat); | ||
695 | free (mod); | ||
696 | } | ||
697 | |||
698 | return ret; | ||
699 | } else { | ||
700 | return fnmatch(pat_src, mod_src, 0); | ||
701 | } | ||
702 | } | ||
703 | |||
704 | /* | ||
705 | * Builds the dependency list (aka stack) of a module. | ||
706 | * head: the highest module in the stack (last to insmod, first to rmmod) | ||
707 | * tail: the lowest module in the stack (first to insmod, last to rmmod) | ||
708 | */ | ||
709 | static void check_dep(char *mod, struct mod_list_t **head, struct mod_list_t **tail) | ||
710 | { | ||
711 | struct mod_list_t *find; | ||
712 | struct dep_t *dt; | ||
713 | struct mod_opt_t *opt = 0; | ||
714 | char *path = 0; | ||
715 | |||
716 | /* Search for the given module name amongst all dependency rules. | ||
717 | * The module name in a dependency rule can be a shell pattern, | ||
718 | * so try to match the given module name against such a pattern. | ||
719 | * Of course if the name in the dependency rule is a plain string, | ||
720 | * then we consider it a pattern, and matching will still work. */ | ||
721 | for (dt = depend; dt; dt = dt->m_next) { | ||
722 | if (check_pattern(dt->m_name, mod) == 0) { | ||
723 | break; | ||
724 | } | ||
725 | } | ||
726 | |||
727 | if (!dt) { | ||
728 | bb_error_msg("module %s not found", mod); | ||
729 | return; | ||
730 | } | ||
731 | |||
732 | // resolve alias names | ||
733 | while (dt->m_isalias) { | ||
734 | if (dt->m_depcnt == 1) { | ||
735 | struct dep_t *adt; | ||
736 | |||
737 | for (adt = depend; adt; adt = adt->m_next) { | ||
738 | if (check_pattern(adt->m_name, dt->m_deparr[0]) == 0) | ||
739 | break; | ||
740 | } | ||
741 | if (adt) { | ||
742 | /* This is the module we are aliased to */ | ||
743 | struct mod_opt_t *opts = dt->m_options; | ||
744 | /* Option of the alias are appended to the options of the module */ | ||
745 | while (opts) { | ||
746 | adt->m_options = append_option(adt->m_options, opts->m_opt_val); | ||
747 | opts = opts->m_next; | ||
748 | } | ||
749 | dt = adt; | ||
750 | } | ||
751 | else { | ||
752 | bb_error_msg("module %s not found", mod); | ||
753 | return; | ||
754 | } | ||
755 | } | ||
756 | else { | ||
757 | bb_error_msg("bad alias %s", dt->m_name); | ||
758 | return; | ||
759 | } | ||
760 | } | ||
761 | |||
762 | mod = dt->m_name; | ||
763 | path = dt->m_path; | ||
764 | opt = dt->m_options; | ||
765 | |||
766 | // search for duplicates | ||
767 | for (find = *head; find; find = find->m_next) { | ||
768 | if (!strcmp(mod, find->m_name)) { | ||
769 | // found ->dequeue it | ||
770 | |||
771 | if (find->m_prev) | ||
772 | find->m_prev->m_next = find->m_next; | ||
773 | else | ||
774 | *head = find->m_next; | ||
775 | |||
776 | if (find->m_next) | ||
777 | find->m_next->m_prev = find->m_prev; | ||
778 | else | ||
779 | *tail = find->m_prev; | ||
780 | |||
781 | break; // there can be only one duplicate | ||
782 | } | ||
783 | } | ||
784 | |||
785 | if (!find) { // did not find a duplicate | ||
786 | find = xmalloc(sizeof(struct mod_list_t)); | ||
787 | find->m_name = mod; | ||
788 | find->m_path = path; | ||
789 | find->m_options = opt; | ||
790 | } | ||
791 | |||
792 | // enqueue at tail | ||
793 | if (*tail) | ||
794 | (*tail)->m_next = find; | ||
795 | find->m_prev = *tail; | ||
796 | find->m_next = 0; | ||
797 | |||
798 | if (!*head) | ||
799 | *head = find; | ||
800 | *tail = find; | ||
801 | |||
802 | if (dt) { | ||
803 | int i; | ||
804 | |||
805 | /* Add all dependable module for that new module */ | ||
806 | for (i = 0; i < dt->m_depcnt; i++) | ||
807 | check_dep(dt->m_deparr[i], head, tail); | ||
808 | } | ||
809 | } | ||
810 | |||
811 | static int mod_insert(char *mod, int argc, char **argv) | ||
812 | { | ||
813 | struct mod_list_t *tail = 0; | ||
814 | struct mod_list_t *head = 0; | ||
815 | int rc; | ||
816 | |||
817 | // get dep list for module mod | ||
818 | check_dep(mod, &head, &tail); | ||
819 | |||
820 | if (head && tail) { | ||
821 | if (argc) { | ||
822 | int i; | ||
823 | // append module args | ||
824 | for (i = 0; i < argc; i++) | ||
825 | head->m_options = append_option(head->m_options, argv[i]); | ||
826 | } | ||
827 | |||
828 | // process tail ---> head | ||
829 | if ((rc = mod_process(tail, 1)) != 0) { | ||
830 | /* | ||
831 | * In case of using udev, multiple instances of modprobe can be | ||
832 | * spawned to load the same module (think of two same usb devices, | ||
833 | * for example; or cold-plugging at boot time). Thus we shouldn't | ||
834 | * fail if the module was loaded, and not by us. | ||
835 | */ | ||
836 | if (already_loaded(mod)) | ||
837 | rc = 0; | ||
838 | } | ||
839 | } | ||
840 | else | ||
841 | rc = 1; | ||
842 | |||
843 | return rc; | ||
844 | } | ||
845 | |||
846 | static int mod_remove(char *mod) | ||
847 | { | ||
848 | int rc; | ||
849 | static struct mod_list_t rm_a_dummy = { "-a", NULL, NULL, NULL, NULL }; | ||
850 | |||
851 | struct mod_list_t *head = 0; | ||
852 | struct mod_list_t *tail = 0; | ||
853 | |||
854 | if (mod) | ||
855 | check_dep(mod, &head, &tail); | ||
856 | else // autoclean | ||
857 | head = tail = &rm_a_dummy; | ||
858 | |||
859 | if (head && tail) | ||
860 | rc = mod_process(head, 0); // process head ---> tail | ||
861 | else | ||
862 | rc = 1; | ||
863 | return rc; | ||
864 | |||
865 | } | ||
866 | |||
867 | int modprobe_main(int argc, char** argv) | ||
868 | { | ||
869 | int rc = EXIT_SUCCESS; | ||
870 | char *unused; | ||
871 | |||
872 | opt_complementary = "?V-:q-v:v-q"; | ||
873 | main_opts = getopt32(argc, argv, "acdklnqrst:vVC:", | ||
874 | &unused, &unused); | ||
875 | if (main_opts & (DUMP_CONF_EXIT | LIST_ALL)) | ||
876 | return EXIT_SUCCESS; | ||
877 | if (main_opts & (RESTRICT_DIR | CONFIG_FILE)) | ||
878 | bb_error_msg_and_die("-t and -C not supported"); | ||
879 | |||
880 | depend = build_dep(); | ||
881 | |||
882 | if (!depend) | ||
883 | bb_error_msg_and_die("cannot parse modules.dep"); | ||
884 | |||
885 | if (remove_opt) { | ||
886 | do { | ||
887 | if (mod_remove(optind < argc ? | ||
888 | argv[optind] : NULL)) { | ||
889 | bb_error_msg("failed to remove module %s", | ||
890 | argv[optind]); | ||
891 | rc = EXIT_FAILURE; | ||
892 | } | ||
893 | } while (++optind < argc); | ||
894 | } else { | ||
895 | if (optind >= argc) | ||
896 | bb_error_msg_and_die("no module or pattern provided"); | ||
897 | |||
898 | if (mod_insert(argv[optind], argc - optind - 1, argv + optind + 1)) | ||
899 | bb_error_msg_and_die("failed to load module %s", argv[optind]); | ||
900 | } | ||
901 | |||
902 | /* Here would be a good place to free up memory allocated during the dependencies build. */ | ||
903 | |||
904 | return rc; | ||
905 | } |