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