Change CONFIG_MODPROBE_MULTIPOLE_OPTIONS to
[platform/upstream/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  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
10 */
11
12 #include <sys/utsname.h>
13 #include <sys/types.h>
14 #include <sys/wait.h>
15 #include <getopt.h>
16 #include <stdlib.h>
17 #include <unistd.h>
18 #include <syslog.h>
19 #include <string.h>
20 #include <ctype.h>
21 #include <fcntl.h>
22 #include "busybox.h"
23
24
25
26 struct dep_t {
27         char *  m_name;
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
37         struct dep_t * m_next;
38 };
39
40 struct mod_list_t {
41         char *  m_name;
42         char *  m_path;
43         char *  m_options;
44
45         struct mod_list_t * m_prev;
46         struct mod_list_t * m_next;
47 };
48
49
50 static struct dep_t *depend;
51 static int autoclean, show_only, quiet, do_syslog, verbose;
52 static int k_version;
53
54 static int parse_tag_value ( char *buffer, char **ptag, char **pvalue )
55 {
56         char *tag, *value;
57
58         while ( isspace ( *buffer ))
59                 buffer++;
60         tag = value = buffer;
61         while ( !isspace ( *value ))
62                 if (!*value) return 0;
63                 else value++;
64         *value++ = 0;
65         while ( isspace ( *value ))
66                 value++;
67         if (!*value) return 0;
68
69         *ptag = tag;
70         *pvalue = value;
71
72         return 1;
73 }
74
75 /* Jump through hoops to simulate how fgets() grabs just one line at a
76  * time... Don't use any stdio since modprobe gets called from a kernel
77  * thread and stdio junk can overflow the limited stack...
78  */
79 static char *reads ( int fd, char *buffer, size_t len )
80 {
81         int n = read ( fd, buffer, len );
82
83         if ( n > 0 ) {
84                 char *p;
85
86                 buffer [len-1] = 0;
87                 p = strchr ( buffer, '\n' );
88
89                 if ( p ) {
90                         off_t offset;
91
92                         offset = lseek ( fd, 0L, SEEK_CUR );               // Get the current file descriptor offset
93                         lseek ( fd, offset-n + (p-buffer) + 1, SEEK_SET ); // Set the file descriptor offset to right after the \n
94
95                         p[1] = 0;
96                 }
97                 return buffer;
98         }
99
100         else
101                 return 0;
102 }
103
104 static struct dep_t *build_dep ( void )
105 {
106         int fd;
107         struct utsname un;
108         struct dep_t *first = 0;
109         struct dep_t *current = 0;
110         char buffer[2048];
111         char *filename = buffer;
112         int continuation_line = 0;
113
114         k_version = 0;
115         if ( uname ( &un ))
116                 return 0;
117
118         // check for buffer overflow in following code
119         if ( bb_strlen ( un.release ) > ( sizeof( buffer ) - 64 )) {
120                 return 0;
121         }
122         if (un.release[0] == '2') {
123                 k_version = un.release[2] - '0';
124         }
125
126         strcpy ( filename, "/lib/modules/" );
127         strcat ( filename, un.release );
128         strcat ( filename, "/modules.dep" );
129
130         if (( fd = open ( filename, O_RDONLY )) < 0 ) {
131
132                 /* Ok, that didn't work.  Fall back to looking in /lib/modules */
133                 if (( fd = open ( "/lib/modules/modules.dep", O_RDONLY )) < 0 ) {
134                         return 0;
135                 }
136         }
137
138         while ( reads ( fd, buffer, sizeof( buffer ))) {
139                 int l = bb_strlen ( buffer );
140                 char *p = 0;
141
142                 while ( isspace ( buffer [l-1] )) {
143                         buffer [l-1] = 0;
144                         l--;
145                 }
146
147                 if ( l == 0 ) {
148                         continuation_line = 0;
149                         continue;
150                 }
151
152                 if ( !continuation_line ) {
153                         char *col = strchr ( buffer, ':' );
154                         char *dot = col;
155
156                         if ( col ) {
157                                 char *mods;
158                                 char *modpath;
159                                 char *mod;
160
161                                 *col = 0;
162                                 mods = strrchr ( buffer, '/' );
163
164                                 if ( !mods )
165                                         mods = buffer;
166                                 else
167                                         mods++;
168
169                                 modpath = strchr ( buffer, '/' );
170                                 if ( !modpath )
171                                         modpath = buffer;
172 #if defined(CONFIG_FEATURE_2_6_MODULES)
173                                 if ((k_version > 4) && ( *(col-3) == '.' ) &&
174                                                 ( *(col-2) == 'k' ) && ( *(col-1) == 'o' ))
175                                         dot = col - 3;
176                                 else
177 #endif
178                                         if (( *(col-2) == '.' ) && ( *(col-1) == 'o' ))
179                                                 dot = col - 2;
180
181                                 mod = bb_xstrndup ( mods, dot - mods );
182
183                                 if ( !current ) {
184                                         first = current = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
185                                 }
186                                 else {
187                                         current-> m_next = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
188                                         current = current-> m_next;
189                                 }
190                                 current-> m_name  = mod;
191                                 current-> m_path  = bb_xstrdup(modpath);
192                                 current-> m_options = 0;
193                                 current-> m_isalias = 0;
194                                 current-> m_depcnt  = 0;
195                                 current-> m_deparr  = 0;
196                                 current-> m_next    = 0;
197
198                                 //printf ( "%s:\n", mod );
199                                 p = col + 1;
200                         }
201                         else
202                                 p = 0;
203                 }
204                 else
205                         p = buffer;
206
207                 while ( p && *p && isblank(*p))
208                         p++;
209
210                 if ( p && *p ) {
211                         char *end = &buffer [l-1];
212                         char *deps;
213                         char *dep;
214                         char *next;
215                         int ext = 0;
216
217                         while ( isblank ( *end ) || ( *end == '\\' ))
218                                 end--;
219
220                         do
221                         {
222                                 next = strchr (p, ' ' );
223                                 if (next)
224                                 {
225                                         *next = 0;
226                                         next--;
227                                 }
228                                 else
229                                         next = end;
230
231                                 deps = strrchr ( p, '/' );
232
233                                 if ( !deps || ( deps < p )) {
234                                         deps = p;
235
236                                         while ( isblank ( *deps ))
237                                                 deps++;
238                                 }
239                                 else
240                                         deps++;
241
242 #if defined(CONFIG_FEATURE_2_6_MODULES)
243                                 if ((k_version > 4) && ( *(next-2) == '.' ) && *(next-1) == 'k'  &&
244                                                 ( *next == 'o' ))
245                                         ext = 3;
246                                 else
247 #endif
248                                         if (( *(next-1) == '.' ) && ( *next == 'o' ))
249                                                 ext = 2;
250
251                                 /* Cope with blank lines */
252                                 if ((next-deps-ext+1) <= 0)
253                                         continue;
254                                 dep = bb_xstrndup ( deps, next - deps - ext + 1 );
255
256                                 current-> m_depcnt++;
257                                 current-> m_deparr = (char **) xrealloc ( current-> m_deparr,
258                                                 sizeof ( char *) * current-> m_depcnt );
259                                 current-> m_deparr [current-> m_depcnt - 1] = dep;
260
261                                 //printf ( "    %d) %s\n", current-> m_depcnt, current-> m_deparr [current-> m_depcnt -1] );
262                                 p = next + 2;
263                         } while (next < end);
264                 }
265
266                 if ( buffer [l-1] == '\\' )
267                         continuation_line = 1;
268                 else
269                         continuation_line = 0;
270         }
271         close ( fd );
272
273         // alias parsing is not 100% correct (no correct handling of continuation lines within an alias) !
274
275 #if defined(CONFIG_FEATURE_2_6_MODULES)
276         if (( fd = open ( "/etc/modprobe.conf", O_RDONLY )) < 0 )
277 #endif
278                 if (( fd = open ( "/etc/modules.conf", O_RDONLY )) < 0 )
279                         if (( fd = open ( "/etc/conf.modules", O_RDONLY )) < 0 )
280                                 return first;
281
282         continuation_line = 0;
283         while ( reads ( fd, buffer, sizeof( buffer ))) {
284                 int l;
285                 char *p;
286
287                 p = strchr ( buffer, '#' );
288                 if ( p )
289                         *p = 0;
290
291                 l = bb_strlen ( buffer );
292
293                 while ( l && isspace ( buffer [l-1] )) {
294                         buffer [l-1] = 0;
295                         l--;
296                 }
297
298                 if ( l == 0 ) {
299                         continuation_line = 0;
300                         continue;
301                 }
302
303                 if ( !continuation_line ) {
304                         if (( strncmp ( buffer, "alias", 5 ) == 0 ) && isspace ( buffer [5] )) {
305                                 char *alias, *mod;
306
307                                 if ( parse_tag_value ( buffer + 6, &alias, &mod )) {
308                                         // fprintf ( stderr, "ALIAS: '%s' -> '%s'\n", alias, mod );
309
310                                         if ( !current ) {
311                                                 first = current = (struct dep_t *) xcalloc ( 1, sizeof ( struct dep_t ));
312                                         }
313                                         else {
314                                                 current-> m_next = (struct dep_t *) xcalloc ( 1, sizeof ( struct dep_t ));
315                                                 current = current-> m_next;
316                                         }
317                                         current-> m_name  = bb_xstrdup ( alias );
318                                         current-> m_isalias = 1;
319
320                                         if (( strcmp ( mod, "off" ) == 0 ) || ( strcmp ( mod, "null" ) == 0 )) {
321                                                 current-> m_depcnt = 0;
322                                                 current-> m_deparr = 0;
323                                         }
324                                         else {
325                                                 current-> m_depcnt  = 1;
326                                                 current-> m_deparr  = xmalloc ( 1 * sizeof( char * ));
327                                                 current-> m_deparr[0] = bb_xstrdup ( mod );
328                                         }
329                                         current-> m_next    = 0;
330                                 }
331                         }
332                         else if (( strncmp ( buffer, "options", 7 ) == 0 ) && isspace ( buffer [7] )) {
333                                 char *mod, *opt;
334
335                                 if ( parse_tag_value ( buffer + 8, &mod, &opt )) {
336                                         struct dep_t *dt;
337
338                                         for ( dt = first; dt; dt = dt-> m_next ) {
339                                                 if ( strcmp ( dt-> m_name, mod ) == 0 )
340                                                         break;
341                                         }
342                                         if ( dt ) {
343                                                 dt-> m_options = xrealloc ( dt-> m_options, bb_strlen( opt ) + 1 );
344                                                 strcpy ( dt-> m_options, opt );
345
346                                                 // fprintf ( stderr, "OPTION: '%s' -> '%s'\n", dt-> m_name, dt-> m_options );
347                                         }
348                                 }
349                         }
350                 }
351         }
352         close ( fd );
353
354         return first;
355 }
356
357 /* return 1 = loaded, 0 = not loaded, -1 = can't tell */
358 static int already_loaded (const char *name)
359 {
360         int fd;
361         char buffer[4096];
362
363         fd = open ("/proc/modules", O_RDONLY);
364         if (fd < 0)
365                 return -1;
366
367         while ( reads ( fd, buffer, sizeof( buffer ))) {
368                 char *p;
369
370                 p = strchr (buffer, ' ');
371                 if (p) {
372                         *p = 0;
373                         if (strcmp (name, buffer) == 0) {
374                                 close (fd);
375                                 return 1;
376                         }
377                 }
378         }
379
380         close (fd);
381         return 0;
382 }
383
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
396 static 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
493 static int mod_process ( struct mod_list_t *list, int do_insert )
494 {
495         int rc = 0;
496 #ifdef CONFIG_FEATURE_MODPROBE_MULTIPLE_OPTIONS
497         char **argv = NULL;
498         char *opts;
499 #ifdef CONFIG_FEATURE_CLEAN_UP
500         int argc_malloc;
501 #endif
502 #else /* CONFIG_FEATURE_MODPROBE_MULTIPLE_OPTIONS */
503         char *argv[10];
504 #endif
505         int argc;
506
507         while ( list ) {
508                 argc = 0;
509 #ifdef CONFIG_FEATURE_MODPROBE_MULTIPLE_OPTIONS
510 #ifdef CONFIG_FEATURE_CLEAN_UP
511                 argc_malloc = 0;
512 #endif
513                 argv = (char**) malloc( 6 * sizeof( char* ) ); /* enough for minimal insmod (5 args + NULL) or rmmod (3 args + NULL) */
514 #endif
515                 if ( do_insert ) {
516                         if (already_loaded (list->m_name) != 1) {
517                                 argv[argc++] = "insmod";
518                                 if (do_syslog)
519                                         argv[argc++] = "-s";
520                                 if (autoclean)
521                                         argv[argc++] = "-k";
522                                 if (quiet)
523                                         argv[argc++] = "-q";
524                                 else if(verbose) /* verbose and quiet are mutually exclusive */
525                                         argv[argc++] = "-v";
526                                 argv[argc++] = list-> m_path;
527 #ifdef CONFIG_FEATURE_MODPROBE_MULTIPLE_OPTIONS
528 #ifdef CONFIG_FEATURE_CLEAN_UP
529                                 argc_malloc = argc;
530 #endif
531                                 opts = list-> m_options;
532                                 while( ( opts = parse_command_string( opts, &(argv[argc]) ) ) != NULL ) {
533                                         /* Increase the argv array by 1 */
534                                         argc++;
535                                         argv = (char**) xrealloc( argv, ( argc + 1 ) * sizeof( char* ) );
536                                 }
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                         }
542                 } else {
543                         /* modutils uses short name for removal */
544                         if (already_loaded (list->m_name) != 0) {
545                                 argv[argc++] = "rmmod";
546                                 if (do_syslog)
547                                         argv[argc++] = "-s";
548                                 argv[argc++] = list->m_name;
549 #if ( defined CONFIG_FEATURE_MODPROBE_MULTIPLE_OPTIONS ) && ( defined CONFIG_FEATURE_CLEAN_UP )
550                                 argc_malloc = argc;
551 #endif
552                         }
553                 }
554                 argv[argc] = NULL;
555
556                 if (argc) {
557                         if (verbose) {
558                                 int i;
559                                 printf("argc=%d\n", argc );
560                                 for (i=0; i<argc; i++)
561                                   printf("argv[%02d]=\"%s\"\n", i, argv[i]);
562                         }
563                         if (!show_only) {
564                                 int rc2 = 0;
565                                 int status;
566                                 switch (fork()) {
567                                 case -1:
568                                         rc2 = 1;
569                                         break;
570                                 case 0: //child
571                                         execvp(argv[0], argv);
572                                         bb_perror_msg_and_die("exec of %s", argv[0]);
573                                         /* NOTREACHED */
574                                 default:
575                                         if (wait(&status) == -1) {
576                                                 rc2 = 1;
577                                                 break;
578                                         }
579                                         if (WIFEXITED(status))
580                                                 rc2 = WEXITSTATUS(status);
581                                         if (WIFSIGNALED(status))
582                                                 rc2 = WTERMSIG(status);
583                                         break;
584                                 }
585                                 if (do_insert) {
586                                         rc = rc2; /* only last module matters */
587                                 }
588                                 else if (!rc2) {
589                                         rc = 0; /* success if remove any mod */
590                                 }
591                         }
592 #if ( defined CONFIG_FEATURE_MODPROBE_MULTIPLE_OPTIONS ) && ( defined CONFIG_FEATURE_CLEAN_UP )
593                         /* the last value in the array has index == argc, but
594                          * it is the terminatign NULL, so we must not free it. */
595                         while( argc_malloc < argc ) {
596                                 free( argv[argc_malloc++] );
597                         }
598                 }
599                 free( argv );
600                 /* If CONFIG_FEATURE_CLEAN_UP is not defined, then we leak memory
601                  * here. But it is (quite) small amounts of memory that leak each
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                 }
612 #endif
613                 list = do_insert ? list-> m_prev : list-> m_next;
614         }
615         return (show_only) ? 0 : rc;
616 }
617
618 static void check_dep ( char *mod, struct mod_list_t **head, struct mod_list_t **tail )
619 {
620         struct mod_list_t *find;
621         struct dep_t *dt;
622         char *opt = 0;
623         char *path = 0;
624
625         // check dependencies
626         for ( dt = depend; dt; dt = dt-> m_next ) {
627                 if ( strcmp ( dt-> m_name, mod ) == 0) {
628                         mod = dt-> m_name;
629                         path = dt-> m_path;
630                         opt = dt-> m_options;
631                         break;
632                 }
633         }
634
635         // resolve alias names
636         while ( dt && dt-> m_isalias ) {
637                 if ( dt-> m_depcnt == 1 ) {
638                         struct dep_t *adt;
639
640                         for ( adt = depend; adt; adt = adt-> m_next ) {
641                                 if ( strcmp ( adt-> m_name, dt-> m_deparr [0] ) == 0 )
642                                         break;
643                         }
644                         if ( adt ) {
645                                 dt = adt;
646                                 mod = dt-> m_name;
647                                 path = dt-> m_path;
648                                 if ( !opt )
649                                         opt = dt-> m_options;
650                         }
651                         else
652                                 return;
653                 }
654                 else
655                         return;
656         }
657
658         if ( !path ) {
659                 bb_error_msg ("module %s not found.", mod);
660                 return;
661         }
662
663         // search for duplicates
664         for ( find = *head; find; find = find-> m_next ) {
665                 if ( !strcmp ( mod, find-> m_name )) {
666                         // found -> dequeue it
667
668                         if ( find-> m_prev )
669                                 find-> m_prev-> m_next = find-> m_next;
670                         else
671                                 *head = find-> m_next;
672
673                         if ( find-> m_next )
674                                 find-> m_next-> m_prev = find-> m_prev;
675                         else
676                                 *tail = find-> m_prev;
677
678                         break; // there can be only one duplicate
679                 }
680         }
681
682         if ( !find ) { // did not find a duplicate
683                 find = (struct mod_list_t *) xmalloc ( sizeof(struct mod_list_t));
684                 find-> m_name = mod;
685                 find-> m_path = path;
686                 find-> m_options = opt;
687         }
688
689         // enqueue at tail
690         if ( *tail )
691                 (*tail)-> m_next = find;
692         find-> m_prev   = *tail;
693         find-> m_next   = 0;
694
695         if ( !*head )
696                 *head = find;
697         *tail = find;
698
699         if ( dt ) {
700                 int i;
701
702                 for ( i = 0; i < dt-> m_depcnt; i++ )
703                         check_dep ( dt-> m_deparr [i], head, tail );
704         }
705 }
706
707 static int mod_insert ( char *mod, int argc, char **argv )
708 {
709         struct mod_list_t *tail = 0;
710         struct mod_list_t *head = 0;
711         int rc;
712
713         // get dep list for module mod
714         check_dep ( mod, &head, &tail );
715
716         if ( head && tail ) {
717 #if defined(CONFIG_FEATURE_2_6_MODULES)
718                 if ( argc ) {
719                         int i;
720                         int l = 0;
721
722                         // append module args
723                         for ( i = 0; i < argc; i++ )
724                                 l += ( bb_strlen ( argv [i] ) + 1 );
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                 }
734 #endif
735
736                 // process tail ---> head
737                 rc = mod_process ( tail, 1 );
738         }
739         else
740                 rc = 1;
741
742         return rc;
743 }
744
745 static int mod_remove ( char *mod )
746 {
747         int rc;
748         static struct mod_list_t rm_a_dummy = { "-a", 0, 0 };
749
750         struct mod_list_t *head = 0;
751         struct mod_list_t *tail = 0;
752
753         if ( mod )
754                 check_dep ( mod, &head, &tail );
755         else  // autoclean
756                 head = tail = &rm_a_dummy;
757
758         if ( head && tail )
759                 rc = mod_process ( head, 0 );  // process head ---> tail
760         else
761                 rc = 1;
762         return rc;
763
764 }
765
766 extern int modprobe_main(int argc, char** argv)
767 {
768         int     opt;
769         int remove_opt = 0;
770
771         autoclean = show_only = quiet = do_syslog = verbose = 0;
772
773         while ((opt = getopt(argc, argv, "acdklnqrst:vVC:")) != -1) {
774                 switch(opt) {
775                         case 'c': // no config used
776                         case 'l': // no pattern matching
777                                 return EXIT_SUCCESS;
778                                 break;
779                         case 'C': // no config used
780                         case 't': // no pattern matching
781                                 bb_error_msg_and_die("-t and -C not supported");
782
783                         case 'a': // ignore
784                         case 'd': // ignore
785                                 break;
786                         case 'k':
787                                 autoclean++;
788                                 break;
789                         case 'n':
790                                 show_only++;
791                                 break;
792                         case 'q':
793                                 quiet++; verbose=0;
794                                 break;
795                         case 'r':
796                                 remove_opt++;
797                                 break;
798                         case 's':
799                                 do_syslog++;
800                                 break;
801                         case 'v':
802                                 verbose++; quiet=0;
803                                 break;
804                         case 'V':
805                         default:
806                                 bb_show_usage();
807                                 break;
808                 }
809         }
810
811         depend = build_dep ( );
812
813         if ( !depend )
814                 bb_error_msg_and_die ( "could not parse modules.dep\n" );
815
816         if (remove_opt) {
817                 int rc = EXIT_SUCCESS;
818                 do {
819                         if (mod_remove ( optind < argc ?
820                                                 bb_xstrdup (argv [optind]) : NULL )) {
821                                 bb_error_msg ("failed to remove module %s",
822                                                 argv [optind] );
823                                 rc = EXIT_FAILURE;
824                         }
825                 } while ( ++optind < argc );
826
827                 return rc;
828         }
829
830         if (optind >= argc)
831                 bb_error_msg_and_die ( "No module or pattern provided\n" );
832
833         if ( mod_insert ( bb_xstrdup ( argv [optind] ), argc - optind - 1, argv + optind + 1 ))
834                 bb_error_msg_and_die ( "failed to load module %s", argv [optind] );
835
836         return EXIT_SUCCESS;
837 }