aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--modutils/modprobe.c505
1 files changed, 273 insertions, 232 deletions
diff --git a/modutils/modprobe.c b/modutils/modprobe.c
index 74fadc326..96b442cab 100644
--- a/modutils/modprobe.c
+++ b/modutils/modprobe.c
@@ -5,6 +5,8 @@
5 * Copyright (c) 2002 by Robert Griebl, griebl@gmx.de 5 * Copyright (c) 2002 by Robert Griebl, griebl@gmx.de
6 * Copyright (c) 2003 by Andrew Dennison, andrew.dennison@motec.com.au 6 * Copyright (c) 2003 by Andrew Dennison, andrew.dennison@motec.com.au
7 * Copyright (c) 2005 by Jim Bauer, jfbauer@nfr.com 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
8 * 10 *
9 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. 11 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
10*/ 12*/
@@ -21,26 +23,31 @@
21#include <fcntl.h> 23#include <fcntl.h>
22#include "busybox.h" 24#include "busybox.h"
23 25
26struct mod_opt_t { /* one-way list of options to pass to a module */
27 char * m_opt_val;
28 struct mod_opt_t * m_next;
29};
24 30
31struct dep_t { /* one-way list of dependency rules */
32 /* a dependency rule */
33 char * m_name; /* the module name*/
34 char * m_path; /* the module file path */
35 struct mod_opt_t * m_options; /* the module options */
36
37 int m_isalias : 1; /* the module is an alias */
38 int m_reserved : 15; /* stuffin' */
25 39
26struct dep_t { 40 int m_depcnt : 16; /* the number of dependable module(s) */
27 char * m_name; 41 char ** m_deparr; /* the list of dependable module(s) */
28 char * m_path;
29 char * m_options;
30
31 int m_isalias : 1;
32 int m_reserved : 15;
33
34 int m_depcnt : 16;
35 char ** m_deparr;
36 42
37 struct dep_t * m_next; 43 struct dep_t * m_next; /* the next dependency rule */
38}; 44};
39 45
40struct mod_list_t { 46struct mod_list_t { /* two-way list of modules to process */
47 /* a module description */
41 char * m_name; 48 char * m_name;
42 char * m_path; 49 char * m_path;
43 char * m_options; 50 struct mod_opt_t * m_options;
44 51
45 struct mod_list_t * m_prev; 52 struct mod_list_t * m_prev;
46 struct mod_list_t * m_next; 53 struct mod_list_t * m_next;
@@ -49,7 +56,6 @@ struct mod_list_t {
49 56
50static struct dep_t *depend; 57static struct dep_t *depend;
51static int autoclean, show_only, quiet, do_syslog, verbose; 58static int autoclean, show_only, quiet, do_syslog, verbose;
52static int k_version;
53 59
54static int parse_tag_value ( char *buffer, char **ptag, char **pvalue ) 60static int parse_tag_value ( char *buffer, char **ptag, char **pvalue )
55{ 61{
@@ -101,6 +107,144 @@ static char *reads ( int fd, char *buffer, size_t len )
101 return 0; 107 return 0;
102} 108}
103 109
110/*
111 * This function appends an option to a list
112 */
113struct mod_opt_t *append_option( struct mod_opt_t *opt_list, char *opt )
114{
115 struct mod_opt_t *ol = opt_list;
116
117 if( ol ) {
118 while( ol-> m_next ) {
119 ol = ol-> m_next;
120 }
121 ol-> m_next = xmalloc( sizeof( struct mod_opt_t ) );
122 ol = ol-> m_next;
123 } else {
124 ol = opt_list = xmalloc( sizeof( struct mod_opt_t ) );
125 }
126
127 ol-> m_opt_val = bb_xstrdup( opt );
128 ol-> m_next = NULL;
129
130 return opt_list;
131}
132
133#if (defined CONFIG_FEATURE_MODPROBE_MULTIPLE_OPTIONS)
134/* static char* parse_command_string( char* src, char **dst );
135 * src: pointer to string containing argument
136 * dst: pointer to where to store the parsed argument
137 * return value: the pointer to the first char after the parsed argument,
138 * NULL if there was no argument parsed (only trailing spaces).
139 * Note that memory is allocated with bb_xstrdup when a new argument was
140 * parsed. Don't forget to free it!
141 */
142#define ARG_EMPTY 0x00
143#define ARG_IN_DQUOTES 0x01
144#define ARG_IN_SQUOTES 0x02
145static char *parse_command_string( char *src, char **dst )
146{
147 int opt_status = ARG_EMPTY;
148 char* tmp_str;
149
150 /* Dumb you, I have nothing to do... */
151 if( src == NULL ) return src;
152
153 /* Skip leading spaces */
154 while( *src == ' ' ) {
155 src++;
156 }
157 /* Is the end of string reached? */
158 if( *src == '\0' ) {
159 return NULL;
160 }
161 /* Reached the start of an argument
162 * By the way, we duplicate a little too much
163 * here but what is too much is freed later. */
164 *dst = tmp_str = bb_xstrdup( src );
165 /* Get to the end of that argument */
166 while( ( *tmp_str != '\0' )
167 && ( ( *tmp_str != ' ' )
168 || ( opt_status & ( ARG_IN_DQUOTES | ARG_IN_SQUOTES ) ) ) ) {
169 switch( *tmp_str ) {
170 case '\'':
171 if( opt_status & ARG_IN_DQUOTES ) {
172 /* Already in double quotes, keep current char as is */
173 } else {
174 /* shift left 1 char, until end of string: get rid of the opening/closing quotes */
175 memmove( tmp_str, tmp_str + 1, strlen( tmp_str ) );
176 /* mark me: we enter or leave single quotes */
177 opt_status ^= ARG_IN_SQUOTES;
178 /* Back one char, as we need to re-scan the new char there. */
179 tmp_str--;
180 }
181 break;
182 case '"':
183 if( opt_status & ARG_IN_SQUOTES ) {
184 /* Already in single quotes, keep current char as is */
185 } else {
186 /* shift left 1 char, until end of string: get rid of the opening/closing quotes */
187 memmove( tmp_str, tmp_str + 1, strlen( tmp_str ) );
188 /* mark me: we enter or leave double quotes */
189 opt_status ^= ARG_IN_DQUOTES;
190 /* Back one char, as we need to re-scan the new char there. */
191 tmp_str--;
192 }
193 break;
194 case '\\':
195 if( opt_status & ARG_IN_SQUOTES ) {
196 /* Between single quotes: keep as is. */
197 } else {
198 switch( *(tmp_str+1) ) {
199 case 'a':
200 case 'b':
201 case 't':
202 case 'n':
203 case 'v':
204 case 'f':
205 case 'r':
206 case '0':
207 /* We escaped a special character. For now, keep
208 * both the back-slash and the following char. */
209 tmp_str++; src++;
210 break;
211 default:
212 /* We escaped a space or a single or double quote,
213 * or a back-slash, or a non-escapable char. Remove
214 * the '\' and keep the new current char as is. */
215 memmove( tmp_str, tmp_str + 1, strlen( tmp_str ) );
216 break;
217 }
218 }
219 break;
220 /* Any other char that is special shall appear here.
221 * Example: $ starts a variable
222 case '$':
223 do_variable_expansion();
224 break;
225 * */
226 default:
227 /* any other char is kept as is. */
228 break;
229 }
230 tmp_str++; /* Go to next char */
231 src++; /* Go to next char to find the end of the argument. */
232 }
233 /* End of string, but still no ending quote */
234 if( opt_status & ( ARG_IN_DQUOTES | ARG_IN_SQUOTES ) ) {
235 bb_error_msg_and_die( "unterminated (single or double) quote in options list: %s", src );
236 }
237 *tmp_str = '\0';
238 *dst = xrealloc( *dst, strlen( *dst ) );
239 return src;
240}
241#endif /* CONFIG_FEATURE_MODPROBE_MULTIPLE_OPTIONS */
242
243/*
244 * This function builds a list of dependency rules from /lib/modules/`uname -r\modules.dep.
245 * It then fills every modules and aliases with their default options, found by parsing
246 * modprobe.conf (or modules.conf, or conf.modules).
247 */
104static struct dep_t *build_dep ( void ) 248static struct dep_t *build_dep ( void )
105{ 249{
106 int fd; 250 int fd;
@@ -110,10 +254,11 @@ static struct dep_t *build_dep ( void )
110 char buffer[2048]; 254 char buffer[2048];
111 char *filename = buffer; 255 char *filename = buffer;
112 int continuation_line = 0; 256 int continuation_line = 0;
257 int k_version;
113 258
114 k_version = 0; 259 k_version = 0;
115 if ( uname ( &un )) 260 if ( uname ( &un ))
116 return 0; 261 bb_error_msg_and_die("can't determine kernel version");
117 262
118 // check for buffer overflow in following code 263 // check for buffer overflow in following code
119 if ( bb_strlen ( un.release ) > ( sizeof( buffer ) - 64 )) { 264 if ( bb_strlen ( un.release ) > ( sizeof( buffer ) - 64 )) {
@@ -149,37 +294,43 @@ static struct dep_t *build_dep ( void )
149 continue; 294 continue;
150 } 295 }
151 296
297 /* Is this a new module dep description? */
152 if ( !continuation_line ) { 298 if ( !continuation_line ) {
299 /* find the dep begining */
153 char *col = strchr ( buffer, ':' ); 300 char *col = strchr ( buffer, ':' );
154 char *dot = col; 301 char *dot = col;
155 302
156 if ( col ) { 303 if ( col ) {
304 /* This line is a dep description */
157 char *mods; 305 char *mods;
158 char *modpath; 306 char *modpath;
159 char *mod; 307 char *mod;
160 308
309 /* Find the beginning of the module file name */
161 *col = 0; 310 *col = 0;
162 mods = strrchr ( buffer, '/' ); 311 mods = strrchr ( buffer, '/' );
163 312
164 if ( !mods ) 313 if ( !mods )
165 mods = buffer; 314 mods = buffer; /* no path for this module */
166 else 315 else
167 mods++; 316 mods++; /* there was a path for this module... */
168 317
169 modpath = strchr ( buffer, '/' ); 318 /* find the path of the module */
319 modpath = strchr ( buffer, '/' ); /* ... and this is the path */
170 if ( !modpath ) 320 if ( !modpath )
171 modpath = buffer; 321 modpath = buffer; /* module with no path */
172#if defined(CONFIG_FEATURE_2_6_MODULES) 322 /* find the end of the module name in the file name */
173 if ((k_version > 4) && ( *(col-3) == '.' ) && 323 if ( ENABLE_FEATURE_2_6_MODULES &&
174 ( *(col-2) == 'k' ) && ( *(col-1) == 'o' )) 324 (k_version > 4) && ( *(col-3) == '.' ) &&
325 ( *(col-2) == 'k' ) && ( *(col-1) == 'o' ) )
175 dot = col - 3; 326 dot = col - 3;
176 else 327 else
177#endif
178 if (( *(col-2) == '.' ) && ( *(col-1) == 'o' )) 328 if (( *(col-2) == '.' ) && ( *(col-1) == 'o' ))
179 dot = col - 2; 329 dot = col - 2;
180 330
181 mod = bb_xstrndup ( mods, dot - mods ); 331 mod = bb_xstrndup ( mods, dot - mods );
182 332
333 /* enqueue new module */
183 if ( !current ) { 334 if ( !current ) {
184 first = current = (struct dep_t *) xmalloc ( sizeof ( struct dep_t )); 335 first = current = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
185 } 336 }
@@ -189,24 +340,26 @@ static struct dep_t *build_dep ( void )
189 } 340 }
190 current-> m_name = mod; 341 current-> m_name = mod;
191 current-> m_path = bb_xstrdup(modpath); 342 current-> m_path = bb_xstrdup(modpath);
192 current-> m_options = 0; 343 current-> m_options = NULL;
193 current-> m_isalias = 0; 344 current-> m_isalias = 0;
194 current-> m_depcnt = 0; 345 current-> m_depcnt = 0;
195 current-> m_deparr = 0; 346 current-> m_deparr = 0;
196 current-> m_next = 0; 347 current-> m_next = 0;
197 348
198 //printf ( "%s:\n", mod );
199 p = col + 1; 349 p = col + 1;
200 } 350 }
201 else 351 else
352 /* this line is not a dep description */
202 p = 0; 353 p = 0;
203 } 354 }
204 else 355 else
356 /* It's a dep description continuation */
205 p = buffer; 357 p = buffer;
206 358
207 while ( p && *p && isblank(*p)) 359 while ( p && *p && isblank(*p))
208 p++; 360 p++;
209 361
362 /* p points to the first dependable module; if NULL, no dependable module */
210 if ( p && *p ) { 363 if ( p && *p ) {
211 char *end = &buffer [l-1]; 364 char *end = &buffer [l-1];
212 char *deps; 365 char *deps;
@@ -219,6 +372,7 @@ static struct dep_t *build_dep ( void )
219 372
220 do 373 do
221 { 374 {
375 /* search the end of the dependency */
222 next = strchr (p, ' ' ); 376 next = strchr (p, ' ' );
223 if (next) 377 if (next)
224 { 378 {
@@ -228,6 +382,7 @@ static struct dep_t *build_dep ( void )
228 else 382 else
229 next = end; 383 next = end;
230 384
385 /* find the begining of the module file name */
231 deps = strrchr ( p, '/' ); 386 deps = strrchr ( p, '/' );
232 387
233 if ( !deps || ( deps < p )) { 388 if ( !deps || ( deps < p )) {
@@ -239,12 +394,12 @@ static struct dep_t *build_dep ( void )
239 else 394 else
240 deps++; 395 deps++;
241 396
242#if defined(CONFIG_FEATURE_2_6_MODULES) 397 /* find the end of the module name in the file name */
243 if ((k_version > 4) && ( *(next-2) == '.' ) && *(next-1) == 'k' && 398 if ( ENABLE_FEATURE_2_6_MODULES &&
244 ( *next == 'o' )) 399 (k_version > 4) && ( *(next-2) == '.' ) &&
400 ( *(next-1) == 'k' ) && ( *next == 'o' ) )
245 ext = 3; 401 ext = 3;
246 else 402 else
247#endif
248 if (( *(next-1) == '.' ) && ( *next == 'o' )) 403 if (( *(next-1) == '.' ) && ( *next == 'o' ))
249 ext = 2; 404 ext = 2;
250 405
@@ -253,16 +408,17 @@ static struct dep_t *build_dep ( void )
253 continue; 408 continue;
254 dep = bb_xstrndup ( deps, next - deps - ext + 1 ); 409 dep = bb_xstrndup ( deps, next - deps - ext + 1 );
255 410
411 /* Add the new dependable module name */
256 current-> m_depcnt++; 412 current-> m_depcnt++;
257 current-> m_deparr = (char **) xrealloc ( current-> m_deparr, 413 current-> m_deparr = (char **) xrealloc ( current-> m_deparr,
258 sizeof ( char *) * current-> m_depcnt ); 414 sizeof ( char *) * current-> m_depcnt );
259 current-> m_deparr [current-> m_depcnt - 1] = dep; 415 current-> m_deparr [current-> m_depcnt - 1] = dep;
260 416
261 //printf ( " %d) %s\n", current-> m_depcnt, current-> m_deparr [current-> m_depcnt -1] );
262 p = next + 2; 417 p = next + 2;
263 } while (next < end); 418 } while (next < end);
264 } 419 }
265 420
421 /* is there other dependable module(s) ? */
266 if ( buffer [l-1] == '\\' ) 422 if ( buffer [l-1] == '\\' )
267 continuation_line = 1; 423 continuation_line = 1;
268 else 424 else
@@ -305,8 +461,7 @@ static struct dep_t *build_dep ( void )
305 char *alias, *mod; 461 char *alias, *mod;
306 462
307 if ( parse_tag_value ( buffer + 6, &alias, &mod )) { 463 if ( parse_tag_value ( buffer + 6, &alias, &mod )) {
308 // fprintf ( stderr, "ALIAS: '%s' -> '%s'\n", alias, mod ); 464 /* handle alias as a module dependent on the aliased module */
309
310 if ( !current ) { 465 if ( !current ) {
311 first = current = (struct dep_t *) xcalloc ( 1, sizeof ( struct dep_t )); 466 first = current = (struct dep_t *) xcalloc ( 1, sizeof ( struct dep_t ));
312 } 467 }
@@ -332,18 +487,24 @@ static struct dep_t *build_dep ( void )
332 else if (( strncmp ( buffer, "options", 7 ) == 0 ) && isspace ( buffer [7] )) { 487 else if (( strncmp ( buffer, "options", 7 ) == 0 ) && isspace ( buffer [7] )) {
333 char *mod, *opt; 488 char *mod, *opt;
334 489
490 /* split the line in the module/alias name, and options */
335 if ( parse_tag_value ( buffer + 8, &mod, &opt )) { 491 if ( parse_tag_value ( buffer + 8, &mod, &opt )) {
336 struct dep_t *dt; 492 struct dep_t *dt;
337 493
494 /* find the corresponding module */
338 for ( dt = first; dt; dt = dt-> m_next ) { 495 for ( dt = first; dt; dt = dt-> m_next ) {
339 if ( strcmp ( dt-> m_name, mod ) == 0 ) 496 if ( strcmp ( dt-> m_name, mod ) == 0 )
340 break; 497 break;
341 } 498 }
342 if ( dt ) { 499 if ( dt ) {
343 dt-> m_options = xrealloc ( dt-> m_options, bb_strlen( opt ) + 1 ); 500 if ( CONFIG_FEATURE_MODPROBE_MULTIPLE_OPTIONS ) {
344 strcpy ( dt-> m_options, opt ); 501 char* new_opt = NULL;
345 502 while( ( opt = parse_command_string( opt, &new_opt ) ) ) {
346 // fprintf ( stderr, "OPTION: '%s' -> '%s'\n", dt-> m_name, dt-> m_options ); 503 dt-> m_options = append_option( dt-> m_options, new_opt );
504 }
505 } else {
506 dt-> m_options = append_option( dt-> m_options, opt );
507 }
347 } 508 }
348 } 509 }
349 } 510 }
@@ -370,6 +531,9 @@ static int already_loaded (const char *name)
370 p = strchr (buffer, ' '); 531 p = strchr (buffer, ' ');
371 if (p) { 532 if (p) {
372 *p = 0; 533 *p = 0;
534 for( p = buffer; ENABLE_FEATURE_2_6_MODULES && *p; p++ ) {
535 *p = ((*p)=='-')?'_':*p;
536 }
373 if (strcmp (name, buffer) == 0) { 537 if (strcmp (name, buffer) == 0) {
374 close (fd); 538 close (fd);
375 return 1; 539 return 1;
@@ -381,137 +545,30 @@ static int already_loaded (const char *name)
381 return 0; 545 return 0;
382} 546}
383 547
384#ifdef CONFIG_FEATURE_MODPROBE_MULTIPLE_OPTIONS
385/* static char* parse_command_string( char* src, char **dst );
386 * src: pointer to string containing argument
387 * dst: pointer to where to store the parsed argument
388 * return value: the pointer to the first char after the parsed argument,
389 * NULL if there was no argument parsed (only trailing spaces).
390 * Note that memory is allocated with bb_xstrdup when a new argument was
391 * parsed. Don't forget to free it!
392 */
393#define ARG_EMPTY 0x00
394#define ARG_IN_DQUOTES 0x01
395#define ARG_IN_SQUOTES 0x02
396static char *parse_command_string( char *src, char **dst )
397{
398 int opt_status = ARG_EMPTY;
399 char* tmp_str;
400
401 /* Dumb you, I have nothing to do... */
402 if( src == NULL ) return src;
403
404 /* Skip leading spaces */
405 while( *src == ' ' ) {
406 src++;
407 }
408 /* Is the end of string reached? */
409 if( *src == '\0' ) {
410 return NULL;
411 }
412 /* Reached the start of an argument
413 * By the way, we duplicate a little too much here :-/ but that's the easy way:
414 * cost effective wrt code, cost consumming wrt memory usage. */
415 *dst = tmp_str = bb_xstrdup( src );
416 /* Get to the end of that argument */
417 while( ( *tmp_str != '\0' )
418 && ( ( *tmp_str != ' ' )
419 || ( opt_status & ( ARG_IN_DQUOTES | ARG_IN_SQUOTES ) ) ) ) {
420 switch( *tmp_str ) {
421 case '\'':
422 if( opt_status & ARG_IN_DQUOTES ) {
423 /* Already in double quotes, keep current char as is */
424 } else {
425 /* shift left 1 char, until end of string: get rid of the opening/closing quotes */
426 memmove( tmp_str, tmp_str + 1, strlen( tmp_str ) );
427 /* mark me: we enter or leave single quotes */
428 opt_status ^= ARG_IN_SQUOTES;
429 /* Back one char, as we need to re-scan the new char there. */
430 tmp_str--;
431 }
432 break;
433 case '"':
434 if( opt_status & ARG_IN_SQUOTES ) {
435 /* Already in single quotes, keep current char as is */
436 } else {
437 /* shift left 1 char, until end of string: get rid of the opening/closing quotes */
438 memmove( tmp_str, tmp_str + 1, strlen( tmp_str ) );
439 /* mark me: we enter or leave double quotes */
440 opt_status ^= ARG_IN_DQUOTES;
441 /* Back one char, as we need to re-scan the new char there. */
442 tmp_str--;
443 }
444 break;
445 case '\\':
446 if( opt_status & ARG_IN_SQUOTES ) {
447 /* Between single quotes: keep as is. */
448 } else {
449 switch( *(tmp_str+1) ) {
450 case 'a':
451 case 'b':
452 case 't':
453 case 'n':
454 case 'v':
455 case 'f':
456 case 'r':
457 case '0':
458 /* We escaped a special character. For now, keep
459 * both the back-slash and the following char. */
460 tmp_str++; src++;
461 break;
462 default:
463 /* We escaped a space or a single or double quote,
464 * or a back-slash, or a non-escapable char. Remove
465 * the '\' and keep the new current char as is. */
466 memmove( tmp_str, tmp_str + 1, strlen( tmp_str ) );
467 break;
468 }
469 }
470 break;
471 /* Any other char that is special shall appear here.
472 * Example: $ starts a variable
473 case '$':
474 do_variable_expansion();
475 break;
476 * */
477 default:
478 /* any other char is kept as is. */
479 break;
480 }
481 tmp_str++; /* Go to next char */
482 src++; /* Go to next char to find the end of the argument. */
483 }
484 /* End of string, but still no ending quote */
485 if( opt_status & ( ARG_IN_DQUOTES | ARG_IN_SQUOTES ) ) {
486 bb_error_msg_and_die( "unterminated (single or double) quote in options list: %s", src );
487 }
488 *tmp_str = '\0';
489 return src;
490}
491#endif /* CONFIG_FEATURE_MODPROBE_MULTIPLE_OPTIONS */
492
493static int mod_process ( struct mod_list_t *list, int do_insert ) 548static int mod_process ( struct mod_list_t *list, int do_insert )
494{ 549{
495 int rc = 0; 550 int rc = 0;
496#ifdef CONFIG_FEATURE_MODPROBE_MULTIPLE_OPTIONS
497 char **argv = NULL; 551 char **argv = NULL;
498 char *opts; 552 struct mod_opt_t *opts;
499#ifdef CONFIG_FEATURE_CLEAN_UP 553 int argc_malloc; /* never used when CONFIG_FEATURE_CLEAN_UP not defined */
500 int argc_malloc;
501#endif
502#else /* CONFIG_FEATURE_MODPROBE_MULTIPLE_OPTIONS */
503 char *argv[10];
504#endif
505 int argc; 554 int argc;
506 555
507 while ( list ) { 556 while ( list ) {
508 argc = 0; 557 argc = 0;
509#ifdef CONFIG_FEATURE_MODPROBE_MULTIPLE_OPTIONS 558 if( ENABLE_FEATURE_CLEAN_UP )
510#ifdef CONFIG_FEATURE_CLEAN_UP 559 argc_malloc = 0;
511 argc_malloc = 0; 560 /* If CONFIG_FEATURE_CLEAN_UP is not defined, then we leak memory
512#endif 561 * each time we allocate memory for argv.
513 argv = (char**) malloc( 6 * sizeof( char* ) ); /* enough for minimal insmod (5 args + NULL) or rmmod (3 args + NULL) */ 562 * But it is (quite) small amounts of memory that leak each
514#endif 563 * time a module is loaded, and it is reclaimed when modprobe
564 * exits anyway (even when standalone shell?).
565 * This could become a problem when loading a module with LOTS of
566 * dependencies, with LOTS of options for each dependencies, with
567 * very little memory on the target... But in that case, the module
568 * would not load because there is no more memory, so there's no
569 * problem. */
570 /* enough for minimal insmod (5 args + NULL) or rmmod (3 args + NULL) */
571 argv = (char**) malloc( 6 * sizeof( char* ) );
515 if ( do_insert ) { 572 if ( do_insert ) {
516 if (already_loaded (list->m_name) != 1) { 573 if (already_loaded (list->m_name) != 1) {
517 argv[argc++] = "insmod"; 574 argv[argc++] = "insmod";
@@ -524,20 +581,16 @@ static int mod_process ( struct mod_list_t *list, int do_insert )
524 else if(verbose) /* verbose and quiet are mutually exclusive */ 581 else if(verbose) /* verbose and quiet are mutually exclusive */
525 argv[argc++] = "-v"; 582 argv[argc++] = "-v";
526 argv[argc++] = list-> m_path; 583 argv[argc++] = list-> m_path;
527#ifdef CONFIG_FEATURE_MODPROBE_MULTIPLE_OPTIONS 584 if( ENABLE_FEATURE_CLEAN_UP )
528#ifdef CONFIG_FEATURE_CLEAN_UP 585 argc_malloc = argc;
529 argc_malloc = argc;
530#endif
531 opts = list-> m_options; 586 opts = list-> m_options;
532 while( ( opts = parse_command_string( opts, &(argv[argc]) ) ) != NULL ) { 587 while( opts ) {
533 /* Increase the argv array by 1 */ 588 /* Add one more option */
534 argc++; 589 argc++;
535 argv = (char**) xrealloc( argv, ( argc + 1 ) * sizeof( char* ) ); 590 argv = (char**) xrealloc( argv, ( argc + 1 ) * sizeof( char* ) );
591 argv[argc-1] = opts-> m_opt_val;
592 opts = opts-> m_next;
536 } 593 }
537#else /* CONFIG_FEATURE_MODPROBE_MULTIPLE_OPTIONS */
538 if (list-> m_options)
539 argv[argc++] = list-> m_options;
540#endif /* CONFIG_FEATURE_MODPROBE_MULTIPLE_OPTIONS */
541 } 594 }
542 } else { 595 } else {
543 /* modutils uses short name for removal */ 596 /* modutils uses short name for removal */
@@ -546,19 +599,15 @@ static int mod_process ( struct mod_list_t *list, int do_insert )
546 if (do_syslog) 599 if (do_syslog)
547 argv[argc++] = "-s"; 600 argv[argc++] = "-s";
548 argv[argc++] = list->m_name; 601 argv[argc++] = list->m_name;
549#if ( defined CONFIG_FEATURE_MODPROBE_MULTIPLE_OPTIONS ) && ( defined CONFIG_FEATURE_CLEAN_UP ) 602 if( ENABLE_FEATURE_CLEAN_UP )
550 argc_malloc = argc; 603 argc_malloc = argc;
551#endif
552 } 604 }
553 } 605 }
554 argv[argc] = NULL; 606 argv[argc] = NULL;
555 607
556 if (argc) { 608 if (argc) {
557 if (verbose) { 609 if (verbose) {
558 int i; 610 printf("%s module %s\n", do_insert?"Loading":"Unloading", list-> m_name );
559 printf("argc=%d\n", argc );
560 for (i=0; i<argc; i++)
561 printf("argv[%02d]=\"%s\"\n", i, argv[i]);
562 } 611 }
563 if (!show_only) { 612 if (!show_only) {
564 int rc2 = 0; 613 int rc2 = 0;
@@ -589,51 +638,48 @@ static int mod_process ( struct mod_list_t *list, int do_insert )
589 rc = 0; /* success if remove any mod */ 638 rc = 0; /* success if remove any mod */
590 } 639 }
591 } 640 }
592#if ( defined CONFIG_FEATURE_MODPROBE_MULTIPLE_OPTIONS ) && ( defined CONFIG_FEATURE_CLEAN_UP ) 641 if( ENABLE_FEATURE_CLEAN_UP )
593 /* the last value in the array has index == argc, but 642 /* the last value in the array has index == argc, but
594 * it is the terminatign NULL, so we must not free it. */ 643 * it is the terminating NULL, so we must not free it. */
595 while( argc_malloc < argc ) { 644 while( argc_malloc < argc ) {
596 free( argv[argc_malloc++] ); 645 free( argv[argc_malloc++] );
597 } 646 }
598 } 647 }
599 free( argv ); 648 if( ENABLE_FEATURE_CLEAN_UP ) {
600 /* If CONFIG_FEATURE_CLEAN_UP is not defined, then we leak memory 649 free( argv );
601 * here. But it is (quite) small amounts of memory that leak each 650 argv = NULL;
602 * time a module is loaded, and it is reclaimed when modprobe
603 * exits anyway.
604 * This could become a problem when loading a module with LOTS of
605 * dependencies, with LOTS of options for each dependencies, with
606 * very little memory on the target... But in that case, the module
607 * would not load because there is no more memory, so there's no
608 * problem. Hmm, wait... Is this true, whatever the allocation policy? */
609 argv = NULL;
610#else /* CONFIG_FEATURE_MODPROBE_MULTIPLE_OPTIONS && CONFIG_FEATURE_CLEAN_UP */
611 } 651 }
612#endif
613 list = do_insert ? list-> m_prev : list-> m_next; 652 list = do_insert ? list-> m_prev : list-> m_next;
614 } 653 }
615 return (show_only) ? 0 : rc; 654 return (show_only) ? 0 : rc;
616} 655}
617 656
657/*
658 * Builds the dependency list (aka stack) of a module.
659 * head: the highest module in the stack (last to insmod, first to rmmod)
660 * tail: the lowest module in the stack (first to insmod, last to rmmod)
661 */
618static void check_dep ( char *mod, struct mod_list_t **head, struct mod_list_t **tail ) 662static void check_dep ( char *mod, struct mod_list_t **head, struct mod_list_t **tail )
619{ 663{
620 struct mod_list_t *find; 664 struct mod_list_t *find;
621 struct dep_t *dt; 665 struct dep_t *dt;
622 char *opt = 0; 666 struct mod_opt_t *opt = 0;
623 char *path = 0; 667 char *path = 0;
624 668
625 // check dependencies 669 // check dependencies
626 for ( dt = depend; dt; dt = dt-> m_next ) { 670 for ( dt = depend; dt; dt = dt-> m_next ) {
627 if ( strcmp ( dt-> m_name, mod ) == 0) { 671 if ( strcmp ( dt-> m_name, mod ) == 0) {
628 mod = dt-> m_name;
629 path = dt-> m_path;
630 opt = dt-> m_options;
631 break; 672 break;
632 } 673 }
633 } 674 }
634 675
676 if( !dt ) {
677 bb_error_msg ("module %s not found.", mod);
678 return;
679 }
680
635 // resolve alias names 681 // resolve alias names
636 while ( dt && dt-> m_isalias ) { 682 while ( dt-> m_isalias ) {
637 if ( dt-> m_depcnt == 1 ) { 683 if ( dt-> m_depcnt == 1 ) {
638 struct dep_t *adt; 684 struct dep_t *adt;
639 685
@@ -642,24 +688,30 @@ static void check_dep ( char *mod, struct mod_list_t **head, struct mod_list_t *
642 break; 688 break;
643 } 689 }
644 if ( adt ) { 690 if ( adt ) {
691 /* This is the module we are aliased to */
692 struct mod_opt_t *opts = dt-> m_options;
693 /* Option of the alias are appended to the options of the module */
694 while( opts ) {
695 adt-> m_options = append_option( adt-> m_options, opts-> m_opt_val );
696 opts = opts-> m_next;
697 }
645 dt = adt; 698 dt = adt;
646 mod = dt-> m_name;
647 path = dt-> m_path;
648 if ( !opt )
649 opt = dt-> m_options;
650 } 699 }
651 else 700 else {
701 bb_error_msg ("module %s not found.", mod);
652 return; 702 return;
703 }
653 } 704 }
654 else 705 else {
706 bb_error_msg ("Bad alias %s", dt-> m_name);
655 return; 707 return;
708 }
656 } 709 }
657 710
658 if ( !path ) { 711 mod = dt-> m_name;
659 bb_error_msg ("module %s not found.", mod); 712 path = dt-> m_path;
660 return; 713 opt = dt-> m_options;
661 } 714
662
663 // search for duplicates 715 // search for duplicates
664 for ( find = *head; find; find = find-> m_next ) { 716 for ( find = *head; find; find = find-> m_next ) {
665 if ( !strcmp ( mod, find-> m_name )) { 717 if ( !strcmp ( mod, find-> m_name )) {
@@ -699,6 +751,7 @@ static void check_dep ( char *mod, struct mod_list_t **head, struct mod_list_t *
699 if ( dt ) { 751 if ( dt ) {
700 int i; 752 int i;
701 753
754 /* Add all dependable module for that new module */
702 for ( i = 0; i < dt-> m_depcnt; i++ ) 755 for ( i = 0; i < dt-> m_depcnt; i++ )
703 check_dep ( dt-> m_deparr [i], head, tail ); 756 check_dep ( dt-> m_deparr [i], head, tail );
704 } 757 }
@@ -714,24 +767,12 @@ static int mod_insert ( char *mod, int argc, char **argv )
714 check_dep ( mod, &head, &tail ); 767 check_dep ( mod, &head, &tail );
715 768
716 if ( head && tail ) { 769 if ( head && tail ) {
717#if defined(CONFIG_FEATURE_2_6_MODULES) 770 if( argc ) {
718 if ( argc ) {
719 int i; 771 int i;
720 int l = 0;
721
722 // append module args 772 // append module args
723 for ( i = 0; i < argc; i++ ) 773 for ( i = 0; i < argc; i++ )
724 l += ( bb_strlen ( argv [i] ) + 1 ); 774 head->m_options = append_option( head->m_options, argv[i] );
725
726 head-> m_options = xrealloc ( head-> m_options, l + 1 );
727 head-> m_options [0] = 0;
728
729 for ( i = 0; i < argc; i++ ) {
730 strcat ( head-> m_options, argv [i] );
731 strcat ( head-> m_options, " " );
732 }
733 } 775 }
734#endif
735 776
736 // process tail ---> head 777 // process tail ---> head
737 rc = mod_process ( tail, 1 ); 778 rc = mod_process ( tail, 1 );
@@ -766,6 +807,7 @@ static int mod_remove ( char *mod )
766extern int modprobe_main(int argc, char** argv) 807extern int modprobe_main(int argc, char** argv)
767{ 808{
768 int opt; 809 int opt;
810 int rc = EXIT_SUCCESS;
769 int remove_opt = 0; 811 int remove_opt = 0;
770 812
771 autoclean = show_only = quiet = do_syslog = verbose = 0; 813 autoclean = show_only = quiet = do_syslog = verbose = 0;
@@ -814,7 +856,6 @@ extern int modprobe_main(int argc, char** argv)
814 bb_error_msg_and_die ( "could not parse modules.dep\n" ); 856 bb_error_msg_and_die ( "could not parse modules.dep\n" );
815 857
816 if (remove_opt) { 858 if (remove_opt) {
817 int rc = EXIT_SUCCESS;
818 do { 859 do {
819 if (mod_remove ( optind < argc ? 860 if (mod_remove ( optind < argc ?
820 bb_xstrdup (argv [optind]) : NULL )) { 861 bb_xstrdup (argv [optind]) : NULL )) {
@@ -823,15 +864,15 @@ extern int modprobe_main(int argc, char** argv)
823 rc = EXIT_FAILURE; 864 rc = EXIT_FAILURE;
824 } 865 }
825 } while ( ++optind < argc ); 866 } while ( ++optind < argc );
867 } else {
868 if (optind >= argc)
869 bb_error_msg_and_die ( "No module or pattern provided\n" );
826 870
827 return rc; 871 if ( mod_insert ( bb_xstrdup ( argv [optind] ), argc - optind - 1, argv + optind + 1 ))
872 bb_error_msg_and_die ( "failed to load module %s", argv [optind] );
828 } 873 }
829 874
830 if (optind >= argc) 875 /* Here would be a good place to free up memory allocated during the dependencies build. */
831 bb_error_msg_and_die ( "No module or pattern provided\n" );
832 876
833 if ( mod_insert ( bb_xstrdup ( argv [optind] ), argc - optind - 1, argv + optind + 1 )) 877 return rc;
834 bb_error_msg_and_die ( "failed to load module %s", argv [optind] );
835
836 return EXIT_SUCCESS;
837} 878}