31fc9baf269a620df50e828be9bc8fe618373768
[oweals/busybox.git] / modutils / modprobe.c
1 /* 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 <sys/utsname.h>
15 #include <sys/types.h>
16 #include <sys/wait.h>
17 #include <getopt.h>
18 #include <stdlib.h>
19 #include <unistd.h>
20 #include <syslog.h>
21 #include <string.h>
22 #include <ctype.h>
23 #include <fcntl.h>
24 #include "busybox.h"
25
26 struct 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 };
30
31 struct 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' */
39
40         int     m_depcnt   : 16;                /* the number of dependable module(s) */
41         char ** m_deparr;                       /* the list of dependable module(s) */
42
43         struct dep_t * m_next;                  /* the next dependency rule */
44 };
45
46 struct mod_list_t {     /* two-way list of modules to process */
47         /* a module description */
48         char *  m_name;
49         char *  m_path;
50         struct mod_opt_t *  m_options;
51
52         struct mod_list_t * m_prev;
53         struct mod_list_t * m_next;
54 };
55
56
57 static struct dep_t *depend;
58 static int autoclean, show_only, quiet, do_syslog, verbose;
59
60 static int parse_tag_value ( char *buffer, char **ptag, char **pvalue )
61 {
62         char *tag, *value;
63
64         while ( isspace ( *buffer ))
65                 buffer++;
66         tag = value = buffer;
67         while ( !isspace ( *value ))
68                 if (!*value) return 0;
69                 else value++;
70         *value++ = 0;
71         while ( isspace ( *value ))
72                 value++;
73         if (!*value) return 0;
74
75         *ptag = tag;
76         *pvalue = value;
77
78         return 1;
79 }
80
81 /* Jump through hoops to simulate how fgets() grabs just one line at a
82  * time... Don't use any stdio since modprobe gets called from a kernel
83  * thread and stdio junk can overflow the limited stack...
84  */
85 static char *reads ( int fd, char *buffer, size_t len )
86 {
87         int n = read ( fd, buffer, len );
88
89         if ( n > 0 ) {
90                 char *p;
91
92                 buffer [len-1] = 0;
93                 p = strchr ( buffer, '\n' );
94
95                 if ( p ) {
96                         off_t offset;
97
98                         offset = lseek ( fd, 0L, SEEK_CUR );               // Get the current file descriptor offset
99                         lseek ( fd, offset-n + (p-buffer) + 1, SEEK_SET ); // Set the file descriptor offset to right after the \n
100
101                         p[1] = 0;
102                 }
103                 return buffer;
104         }
105
106         else
107                 return 0;
108 }
109
110 /*
111  * This function appends an option to a list
112  */
113 struct 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
145 static 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  */
248 static struct dep_t *build_dep ( void )
249 {
250         int fd;
251         struct utsname un;
252         struct dep_t *first = 0;
253         struct dep_t *current = 0;
254         char buffer[2048];
255         char *filename = buffer;
256         int continuation_line = 0;
257         int k_version;
258
259         k_version = 0;
260         if ( uname ( &un ))
261                 bb_error_msg_and_die("can't determine kernel version");
262
263         // check for buffer overflow in following code
264         if ( bb_strlen ( un.release ) > ( sizeof( buffer ) - 64 )) {
265                 return 0;
266         }
267         if (un.release[0] == '2') {
268                 k_version = un.release[2] - '0';
269         }
270
271         strcpy ( filename, "/lib/modules/" );
272         strcat ( filename, un.release );
273         strcat ( filename, "/modules.dep" );
274
275         if (( fd = open ( filename, O_RDONLY )) < 0 ) {
276
277                 /* Ok, that didn't work.  Fall back to looking in /lib/modules */
278                 if (( fd = open ( "/lib/modules/modules.dep", O_RDONLY )) < 0 ) {
279                         return 0;
280                 }
281         }
282
283         while ( reads ( fd, buffer, sizeof( buffer ))) {
284                 int l = bb_strlen ( buffer );
285                 char *p = 0;
286
287                 while ( isspace ( buffer [l-1] )) {
288                         buffer [l-1] = 0;
289                         l--;
290                 }
291
292                 if ( l == 0 ) {
293                         continuation_line = 0;
294                         continue;
295                 }
296
297                 /* Is this a new module dep description? */
298                 if ( !continuation_line ) {
299                         /* find the dep begining */
300                         char *col = strchr ( buffer, ':' );
301                         char *dot = col;
302
303                         if ( col ) {
304                                 /* This line is a dep description */
305                                 char *mods;
306                                 char *modpath;
307                                 char *mod;
308
309                                 /* Find the beginning of the module file name */
310                                 *col = 0;
311                                 mods = strrchr ( buffer, '/' );
312
313                                 if ( !mods )
314                                         mods = buffer; /* no path for this module */
315                                 else
316                                         mods++; /* there was a path for this module... */
317
318                                 /* find the path of the module */
319                                 modpath = strchr ( buffer, '/' ); /* ... and this is the path */
320                                 if ( !modpath )
321                                         modpath = buffer; /* module with no path */
322                                 /* find the end of the module name in the file name */
323                                 if ( ENABLE_FEATURE_2_6_MODULES &&
324                                      (k_version > 4) && ( *(col-3) == '.' ) &&
325                                      ( *(col-2) == 'k' ) && ( *(col-1) == 'o' ) )
326                                         dot = col - 3;
327                                 else
328                                         if (( *(col-2) == '.' ) && ( *(col-1) == 'o' ))
329                                                 dot = col - 2;
330
331                                 mod = bb_xstrndup ( mods, dot - mods );
332
333                                 /* enqueue new module */
334                                 if ( !current ) {
335                                         first = current = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
336                                 }
337                                 else {
338                                         current-> m_next = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
339                                         current = current-> m_next;
340                                 }
341                                 current-> m_name  = mod;
342                                 current-> m_path  = bb_xstrdup(modpath);
343                                 current-> m_options = NULL;
344                                 current-> m_isalias = 0;
345                                 current-> m_depcnt  = 0;
346                                 current-> m_deparr  = 0;
347                                 current-> m_next    = 0;
348
349                                 p = col + 1;
350                         }
351                         else
352                                 /* this line is not a dep description */
353                                 p = 0;
354                 }
355                 else
356                         /* It's a dep description continuation */
357                         p = buffer;
358
359                 while ( p && *p && isblank(*p))
360                         p++;
361
362                 /* p points to the first dependable module; if NULL, no dependable module */
363                 if ( p && *p ) {
364                         char *end = &buffer [l-1];
365                         char *deps;
366                         char *dep;
367                         char *next;
368                         int ext = 0;
369
370                         while ( isblank ( *end ) || ( *end == '\\' ))
371                                 end--;
372
373                         do
374                         {
375                                 /* search the end of the dependency */
376                                 next = strchr (p, ' ' );
377                                 if (next)
378                                 {
379                                         *next = 0;
380                                         next--;
381                                 }
382                                 else
383                                         next = end;
384
385                                 /* find the begining of the module file name */
386                                 deps = strrchr ( p, '/' );
387
388                                 if ( !deps || ( deps < p )) {
389                                         deps = p;
390
391                                         while ( isblank ( *deps ))
392                                                 deps++;
393                                 }
394                                 else
395                                         deps++;
396
397                                 /* find the end of the module name in the file name */
398                                 if ( ENABLE_FEATURE_2_6_MODULES &&
399                                      (k_version > 4) && ( *(next-2) == '.' ) &&
400                                      ( *(next-1) == 'k' )  && ( *next == 'o' ) )
401                                         ext = 3;
402                                 else
403                                         if (( *(next-1) == '.' ) && ( *next == 'o' ))
404                                                 ext = 2;
405
406                                 /* Cope with blank lines */
407                                 if ((next-deps-ext+1) <= 0)
408                                         continue;
409                                 dep = bb_xstrndup ( deps, next - deps - ext + 1 );
410
411                                 /* Add the new dependable module name */
412                                 current-> m_depcnt++;
413                                 current-> m_deparr = (char **) xrealloc ( current-> m_deparr,
414                                                 sizeof ( char *) * current-> m_depcnt );
415                                 current-> m_deparr [current-> m_depcnt - 1] = dep;
416
417                                 p = next + 2;
418                         } while (next < end);
419                 }
420
421                 /* is there other dependable module(s) ? */
422                 if ( buffer [l-1] == '\\' )
423                         continuation_line = 1;
424                 else
425                         continuation_line = 0;
426         }
427         close ( fd );
428
429         // alias parsing is not 100% correct (no correct handling of continuation lines within an alias) !
430
431 #if defined(CONFIG_FEATURE_2_6_MODULES)
432         if (( fd = open ( "/etc/modprobe.conf", O_RDONLY )) < 0 )
433 #endif
434                 if (( fd = open ( "/etc/modules.conf", O_RDONLY )) < 0 )
435                         if (( fd = open ( "/etc/conf.modules", O_RDONLY )) < 0 )
436                                 return first;
437
438         continuation_line = 0;
439         while ( reads ( fd, buffer, sizeof( buffer ))) {
440                 int l;
441                 char *p;
442
443                 p = strchr ( buffer, '#' );
444                 if ( p )
445                         *p = 0;
446
447                 l = bb_strlen ( buffer );
448
449                 while ( l && isspace ( buffer [l-1] )) {
450                         buffer [l-1] = 0;
451                         l--;
452                 }
453
454                 if ( l == 0 ) {
455                         continuation_line = 0;
456                         continue;
457                 }
458
459                 if ( !continuation_line ) {
460                         if (( strncmp ( buffer, "alias", 5 ) == 0 ) && isspace ( buffer [5] )) {
461                                 char *alias, *mod;
462
463                                 if ( parse_tag_value ( buffer + 6, &alias, &mod )) {
464                                         /* handle alias as a module dependent on the aliased module */
465                                         if ( !current ) {
466                                                 first = current = (struct dep_t *) xcalloc ( 1, sizeof ( struct dep_t ));
467                                         }
468                                         else {
469                                                 current-> m_next = (struct dep_t *) xcalloc ( 1, sizeof ( struct dep_t ));
470                                                 current = current-> m_next;
471                                         }
472                                         current-> m_name  = bb_xstrdup ( alias );
473                                         current-> m_isalias = 1;
474
475                                         if (( strcmp ( mod, "off" ) == 0 ) || ( strcmp ( mod, "null" ) == 0 )) {
476                                                 current-> m_depcnt = 0;
477                                                 current-> m_deparr = 0;
478                                         }
479                                         else {
480                                                 current-> m_depcnt  = 1;
481                                                 current-> m_deparr  = xmalloc ( 1 * sizeof( char * ));
482                                                 current-> m_deparr[0] = bb_xstrdup ( mod );
483                                         }
484                                         current-> m_next    = 0;
485                                 }
486                         }
487                         else if (( strncmp ( buffer, "options", 7 ) == 0 ) && isspace ( buffer [7] )) {
488                                 char *mod, *opt;
489
490                                 /* split the line in the module/alias name, and options */
491                                 if ( parse_tag_value ( buffer + 8, &mod, &opt )) {
492                                         struct dep_t *dt;
493
494                                         /* find the corresponding module */
495                                         for ( dt = first; dt; dt = dt-> m_next ) {
496                                                 if ( strcmp ( dt-> m_name, mod ) == 0 )
497                                                         break;
498                                         }
499                                         if ( dt ) {
500                                                 if ( ENABLE_FEATURE_MODPROBE_MULTIPLE_OPTIONS ) {
501                                                         char* new_opt = NULL;
502                                                         while( ( opt = parse_command_string( opt, &new_opt ) ) ) {
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                                                 }
508                                         }
509                                 }
510                         }
511                 }
512         }
513         close ( fd );
514
515         return first;
516 }
517
518 /* return 1 = loaded, 0 = not loaded, -1 = can't tell */
519 static int already_loaded (const char *name)
520 {
521         int fd;
522         char buffer[4096];
523
524         fd = open ("/proc/modules", O_RDONLY);
525         if (fd < 0)
526                 return -1;
527
528         while ( reads ( fd, buffer, sizeof( buffer ))) {
529                 char *p;
530
531                 p = strchr (buffer, ' ');
532                 if (p) {
533                         *p = 0;
534                         for( p = buffer; ENABLE_FEATURE_2_6_MODULES && *p; p++ ) {
535                                 *p = ((*p)=='-')?'_':*p;
536                         }
537                         if (strcmp (name, buffer) == 0) {
538                                 close (fd);
539                                 return 1;
540                         }
541                 }
542         }
543
544         close (fd);
545         return 0;
546 }
547
548 static int mod_process ( struct mod_list_t *list, int do_insert )
549 {
550         int rc = 0;
551         char **argv = NULL;
552         struct mod_opt_t *opts;
553         int argc_malloc; /* never used when CONFIG_FEATURE_CLEAN_UP not defined */
554         int argc;
555
556         while ( list ) {
557                 argc = 0;
558                 if( ENABLE_FEATURE_CLEAN_UP )
559                         argc_malloc = 0;
560                 /* If CONFIG_FEATURE_CLEAN_UP is not defined, then we leak memory
561                  * each time we allocate memory for argv.
562                  * But it is (quite) small amounts of memory that leak each
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* ) );
572                 if ( do_insert ) {
573                         if (already_loaded (list->m_name) != 1) {
574                                 argv[argc++] = "insmod";
575                                 if (do_syslog)
576                                         argv[argc++] = "-s";
577                                 if (autoclean)
578                                         argv[argc++] = "-k";
579                                 if (quiet)
580                                         argv[argc++] = "-q";
581                                 else if(verbose) /* verbose and quiet are mutually exclusive */
582                                         argv[argc++] = "-v";
583                                 argv[argc++] = list-> m_path;
584                                 if( ENABLE_FEATURE_CLEAN_UP )
585                                         argc_malloc = argc;
586                                 opts = list-> m_options;
587                                 while( opts ) {
588                                         /* Add one more option */
589                                         argc++;
590                                         argv = (char**) xrealloc( argv, ( argc + 1 ) * sizeof( char* ) );
591                                         argv[argc-1] = opts-> m_opt_val;
592                                         opts = opts-> m_next;
593                                 }
594                         }
595                 } else {
596                         /* modutils uses short name for removal */
597                         if (already_loaded (list->m_name) != 0) {
598                                 argv[argc++] = "rmmod";
599                                 if (do_syslog)
600                                         argv[argc++] = "-s";
601                                 argv[argc++] = list->m_name;
602                                 if( ENABLE_FEATURE_CLEAN_UP )
603                                         argc_malloc = argc;
604                         }
605                 }
606                 argv[argc] = NULL;
607
608                 if (argc) {
609                         if (verbose) {
610                                 printf("%s module %s\n", do_insert?"Loading":"Unloading", list-> m_name );
611                         }
612                         if (!show_only) {
613                                 int rc2 = 0;
614                                 int status;
615                                 switch (fork()) {
616                                 case -1:
617                                         rc2 = 1;
618                                         break;
619                                 case 0: //child
620                                         execvp(argv[0], argv);
621                                         bb_perror_msg_and_die("exec of %s", argv[0]);
622                                         /* NOTREACHED */
623                                 default:
624                                         if (wait(&status) == -1) {
625                                                 rc2 = 1;
626                                                 break;
627                                         }
628                                         if (WIFEXITED(status))
629                                                 rc2 = WEXITSTATUS(status);
630                                         if (WIFSIGNALED(status))
631                                                 rc2 = WTERMSIG(status);
632                                         break;
633                                 }
634                                 if (do_insert) {
635                                         rc = rc2; /* only last module matters */
636                                 }
637                                 else if (!rc2) {
638                                         rc = 0; /* success if remove any mod */
639                                 }
640                         }
641                         if( ENABLE_FEATURE_CLEAN_UP )
642                                 /* the last value in the array has index == argc, but
643                                  * it is the terminating NULL, so we must not free it. */
644                                 while( argc_malloc < argc ) {
645                                         free( argv[argc_malloc++] );
646                         }
647                 }
648                 if( ENABLE_FEATURE_CLEAN_UP ) {
649                         free( argv );
650                         argv = NULL;
651                 }
652                 list = do_insert ? list-> m_prev : list-> m_next;
653         }
654         return (show_only) ? 0 : rc;
655 }
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  */
662 static void check_dep ( char *mod, struct mod_list_t **head, struct mod_list_t **tail )
663 {
664         struct mod_list_t *find;
665         struct dep_t *dt;
666         struct mod_opt_t *opt = 0;
667         char *path = 0;
668
669         // check dependencies
670         for ( dt = depend; dt; dt = dt-> m_next ) {
671                 if ( strcmp ( dt-> m_name, mod ) == 0) {
672                         break;
673                 }
674         }
675
676         if( !dt ) {
677                 bb_error_msg ("module %s not found.", mod);
678                 return;
679         }
680
681         // resolve alias names
682         while ( dt-> m_isalias ) {
683                 if ( dt-> m_depcnt == 1 ) {
684                         struct dep_t *adt;
685
686                         for ( adt = depend; adt; adt = adt-> m_next ) {
687                                 if ( strcmp ( adt-> m_name, dt-> m_deparr [0] ) == 0 )
688                                         break;
689                         }
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                                 }
698                                 dt = adt;
699                         }
700                         else {
701                                 bb_error_msg ("module %s not found.", mod);
702                                 return;
703                         }
704                 }
705                 else {
706                         bb_error_msg ("Bad alias %s", dt-> m_name);
707                         return;
708                 }
709         }
710
711         mod = dt-> m_name;
712         path = dt-> m_path;
713         opt = dt-> m_options;
714                         
715         // search for duplicates
716         for ( find = *head; find; find = find-> m_next ) {
717                 if ( !strcmp ( mod, find-> m_name )) {
718                         // found -> dequeue it
719
720                         if ( find-> m_prev )
721                                 find-> m_prev-> m_next = find-> m_next;
722                         else
723                                 *head = find-> m_next;
724
725                         if ( find-> m_next )
726                                 find-> m_next-> m_prev = find-> m_prev;
727                         else
728                                 *tail = find-> m_prev;
729
730                         break; // there can be only one duplicate
731                 }
732         }
733
734         if ( !find ) { // did not find a duplicate
735                 find = (struct mod_list_t *) xmalloc ( sizeof(struct mod_list_t));
736                 find-> m_name = mod;
737                 find-> m_path = path;
738                 find-> m_options = opt;
739         }
740
741         // enqueue at tail
742         if ( *tail )
743                 (*tail)-> m_next = find;
744         find-> m_prev   = *tail;
745         find-> m_next   = 0;
746
747         if ( !*head )
748                 *head = find;
749         *tail = find;
750
751         if ( dt ) {
752                 int i;
753
754                 /* Add all dependable module for that new module */
755                 for ( i = 0; i < dt-> m_depcnt; i++ )
756                         check_dep ( dt-> m_deparr [i], head, tail );
757         }
758 }
759
760 static int mod_insert ( char *mod, int argc, char **argv )
761 {
762         struct mod_list_t *tail = 0;
763         struct mod_list_t *head = 0;
764         int rc;
765
766         // get dep list for module mod
767         check_dep ( mod, &head, &tail );
768
769         if ( head && tail ) {
770                 if( argc ) {
771                         int i;
772                         // append module args
773                         for ( i = 0; i < argc; i++ )
774                                 head->m_options = append_option( head->m_options, argv[i] );
775                 }
776
777                 // process tail ---> head
778                 rc = mod_process ( tail, 1 );
779         }
780         else
781                 rc = 1;
782
783         return rc;
784 }
785
786 static int mod_remove ( char *mod )
787 {
788         int rc;
789         static struct mod_list_t rm_a_dummy = { "-a", 0, 0 };
790
791         struct mod_list_t *head = 0;
792         struct mod_list_t *tail = 0;
793
794         if ( mod )
795                 check_dep ( mod, &head, &tail );
796         else  // autoclean
797                 head = tail = &rm_a_dummy;
798
799         if ( head && tail )
800                 rc = mod_process ( head, 0 );  // process head ---> tail
801         else
802                 rc = 1;
803         return rc;
804
805 }
806
807 extern int modprobe_main(int argc, char** argv)
808 {
809         int     opt;
810         int rc = EXIT_SUCCESS;
811         int remove_opt = 0;
812
813         autoclean = show_only = quiet = do_syslog = verbose = 0;
814
815         while ((opt = getopt(argc, argv, "acdklnqrst:vVC:")) != -1) {
816                 switch(opt) {
817                         case 'c': // no config used
818                         case 'l': // no pattern matching
819                                 return EXIT_SUCCESS;
820                                 break;
821                         case 'C': // no config used
822                         case 't': // no pattern matching
823                                 bb_error_msg_and_die("-t and -C not supported");
824
825                         case 'a': // ignore
826                         case 'd': // ignore
827                                 break;
828                         case 'k':
829                                 autoclean++;
830                                 break;
831                         case 'n':
832                                 show_only++;
833                                 break;
834                         case 'q':
835                                 quiet++; verbose=0;
836                                 break;
837                         case 'r':
838                                 remove_opt++;
839                                 break;
840                         case 's':
841                                 do_syslog++;
842                                 break;
843                         case 'v':
844                                 verbose++; quiet=0;
845                                 break;
846                         case 'V':
847                         default:
848                                 bb_show_usage();
849                                 break;
850                 }
851         }
852
853         depend = build_dep ( );
854
855         if ( !depend )
856                 bb_error_msg_and_die ( "could not parse modules.dep\n" );
857
858         if (remove_opt) {
859                 do {
860                         if (mod_remove ( optind < argc ?
861                                                 bb_xstrdup (argv [optind]) : NULL )) {
862                                 bb_error_msg ("failed to remove module %s",
863                                                 argv [optind] );
864                                 rc = EXIT_FAILURE;
865                         }
866                 } while ( ++optind < argc );
867         } else {
868                 if (optind >= argc)
869                         bb_error_msg_and_die ( "No module or pattern provided\n" );
870
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] );
873         }
874
875         /* Here would be a good place to free up memory allocated during the dependencies build. */
876
877         return rc;
878 }