1 /* vi: set sw=4 ts=4: */
3 * Modprobe written from scratch for BusyBox
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
9 * Portions Copyright (c) 2005 by Yann E. MORIN, yann.morin.1998@anciens.enib.fr
11 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
14 #include <sys/utsname.h>
15 #include <sys/types.h>
26 struct mod_opt_t { /* one-way list of options to pass to a module */
28 struct mod_opt_t * m_next;
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 */
37 int m_isalias : 1; /* the module is an alias */
38 int m_reserved : 15; /* stuffin' */
40 int m_depcnt : 16; /* the number of dependable module(s) */
41 char ** m_deparr; /* the list of dependable module(s) */
43 struct dep_t * m_next; /* the next dependency rule */
46 struct mod_list_t { /* two-way list of modules to process */
47 /* a module description */
50 struct mod_opt_t * m_options;
52 struct mod_list_t * m_prev;
53 struct mod_list_t * m_next;
57 static struct dep_t *depend;
58 static int autoclean, show_only, quiet, do_syslog, verbose;
60 static int parse_tag_value ( char *buffer, char **ptag, char **pvalue )
64 while ( isspace ( *buffer ))
67 while ( !isspace ( *value ))
68 if (!*value) return 0;
71 while ( isspace ( *value ))
73 if (!*value) return 0;
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...
85 static char *reads ( int fd, char *buffer, size_t len )
87 int n = read ( fd, buffer, len );
93 p = strchr ( buffer, '\n' );
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
111 * This function appends an option to a list
113 struct mod_opt_t *append_option( struct mod_opt_t *opt_list, char *opt )
115 struct mod_opt_t *ol = opt_list;
118 while( ol-> m_next ) {
121 ol-> m_next = xmalloc( sizeof( struct mod_opt_t ) );
124 ol = opt_list = xmalloc( sizeof( struct mod_opt_t ) );
127 ol-> m_opt_val = bb_xstrdup( opt );
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!
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 )
147 int opt_status = ARG_EMPTY;
150 /* Dumb you, I have nothing to do... */
151 if( src == NULL ) return src;
153 /* Skip leading spaces */
154 while( *src == ' ' ) {
157 /* Is the end of string reached? */
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 ) ) ) ) {
171 if( opt_status & ARG_IN_DQUOTES ) {
172 /* Already in double quotes, keep current char as is */
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. */
183 if( opt_status & ARG_IN_SQUOTES ) {
184 /* Already in single quotes, keep current char as is */
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. */
195 if( opt_status & ARG_IN_SQUOTES ) {
196 /* Between single quotes: keep as is. */
198 switch( *(tmp_str+1) ) {
207 /* We escaped a special character. For now, keep
208 * both the back-slash and the following char. */
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 ) );
220 /* Any other char that is special shall appear here.
221 * Example: $ starts a variable
223 do_variable_expansion();
227 /* any other char is kept as is. */
230 tmp_str++; /* Go to next char */
231 src++; /* Go to next char to find the end of the argument. */
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 );
238 *dst = xrealloc( *dst, strlen( *dst ) );
242 #define parse_command_string(src, dst) (0)
243 #endif /* ENABLE_FEATURE_MODPROBE_MULTIPLE_OPTIONS */
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).
250 static struct dep_t *build_dep ( void )
254 struct dep_t *first = 0;
255 struct dep_t *current = 0;
257 char *filename = buffer;
258 int continuation_line = 0;
263 bb_error_msg_and_die("can't determine kernel version");
265 // check for buffer overflow in following code
266 if ( bb_strlen ( un.release ) > ( sizeof( buffer ) - 64 )) {
269 if (un.release[0] == '2') {
270 k_version = un.release[2] - '0';
273 strcpy ( filename, "/lib/modules/" );
274 strcat ( filename, un.release );
275 strcat ( filename, "/modules.dep" );
277 if (( fd = open ( filename, O_RDONLY )) < 0 ) {
279 /* Ok, that didn't work. Fall back to looking in /lib/modules */
280 if (( fd = open ( "/lib/modules/modules.dep", O_RDONLY )) < 0 ) {
285 while ( reads ( fd, buffer, sizeof( buffer ))) {
286 int l = bb_strlen ( buffer );
289 while ( isspace ( buffer [l-1] )) {
295 continuation_line = 0;
299 /* Is this a new module dep description? */
300 if ( !continuation_line ) {
301 /* find the dep beginning */
302 char *col = strchr ( buffer, ':' );
306 /* This line is a dep description */
311 /* Find the beginning of the module file name */
313 mods = strrchr ( buffer, '/' );
316 mods = buffer; /* no path for this module */
318 mods++; /* there was a path for this module... */
320 /* find the path of the module */
321 modpath = strchr ( buffer, '/' ); /* ... and this is the path */
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' ) )
330 if (( *(col-2) == '.' ) && ( *(col-1) == 'o' ))
333 mod = bb_xstrndup ( mods, dot - mods );
335 /* enqueue new module */
337 first = current = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
340 current-> m_next = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
341 current = current-> m_next;
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;
354 /* this line is not a dep description */
358 /* It's a dep description continuation */
361 while ( p && *p && isblank(*p))
364 /* p points to the first dependable module; if NULL, no dependable module */
366 char *end = &buffer [l-1];
372 while ( isblank ( *end ) || ( *end == '\\' ))
377 /* search the end of the dependency */
378 next = strchr (p, ' ' );
387 /* find the beginning of the module file name */
388 deps = strrchr ( p, '/' );
390 if ( !deps || ( deps < p )) {
393 while ( isblank ( *deps ))
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' ) )
405 if (( *(next-1) == '.' ) && ( *next == 'o' ))
408 /* Cope with blank lines */
409 if ((next-deps-ext+1) <= 0)
411 dep = bb_xstrndup ( deps, next - deps - ext + 1 );
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;
420 } while (next < end);
423 /* is there other dependable module(s) ? */
424 if ( buffer [l-1] == '\\' )
425 continuation_line = 1;
427 continuation_line = 0;
431 // alias parsing is not 100% correct (no correct handling of continuation lines within an alias) !
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 )
439 continuation_line = 0;
440 while ( reads ( fd, buffer, sizeof( buffer ))) {
444 p = strchr ( buffer, '#' );
448 l = bb_strlen ( buffer );
450 while ( l && isspace ( buffer [l-1] )) {
456 continuation_line = 0;
460 if ( !continuation_line ) {
461 if (( strncmp ( buffer, "alias", 5 ) == 0 ) && isspace ( buffer [5] )) {
464 if ( parse_tag_value ( buffer + 6, &alias, &mod )) {
465 /* handle alias as a module dependent on the aliased module */
467 first = current = (struct dep_t *) xcalloc ( 1, sizeof ( struct dep_t ));
470 current-> m_next = (struct dep_t *) xcalloc ( 1, sizeof ( struct dep_t ));
471 current = current-> m_next;
473 current-> m_name = bb_xstrdup ( alias );
474 current-> m_isalias = 1;
476 if (( strcmp ( mod, "off" ) == 0 ) || ( strcmp ( mod, "null" ) == 0 )) {
477 current-> m_depcnt = 0;
478 current-> m_deparr = 0;
481 current-> m_depcnt = 1;
482 current-> m_deparr = xmalloc ( 1 * sizeof( char * ));
483 current-> m_deparr[0] = bb_xstrdup ( mod );
485 current-> m_next = 0;
488 else if (( strncmp ( buffer, "options", 7 ) == 0 ) && isspace ( buffer [7] )) {
491 /* split the line in the module/alias name, and options */
492 if ( parse_tag_value ( buffer + 8, &mod, &opt )) {
495 /* find the corresponding module */
496 for ( dt = first; dt; dt = dt-> m_next ) {
497 if ( strcmp ( dt-> m_name, mod ) == 0 )
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 );
507 dt-> m_options = append_option( dt-> m_options, opt );
519 /* return 1 = loaded, 0 = not loaded, -1 = can't tell */
520 static int already_loaded (const char *name)
525 fd = open ("/proc/modules", O_RDONLY);
529 while ( reads ( fd, buffer, sizeof( buffer ))) {
532 p = strchr (buffer, ' ');
535 for( p = buffer; ENABLE_FEATURE_2_6_MODULES && *p; p++ ) {
536 *p = ((*p)=='-')?'_':*p;
538 if (strcmp (name, buffer) == 0) {
549 static int mod_process ( struct mod_list_t *list, int do_insert )
553 struct mod_opt_t *opts;
554 int argc_malloc; /* never used when CONFIG_FEATURE_CLEAN_UP not defined */
559 if( ENABLE_FEATURE_CLEAN_UP )
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
571 /* enough for minimal insmod (5 args + NULL) or rmmod (3 args + NULL) */
572 argv = (char**) malloc( 6 * sizeof( char* ) );
574 if (already_loaded (list->m_name) != 1) {
575 argv[argc++] = "insmod";
582 else if(verbose) /* verbose and quiet are mutually exclusive */
584 argv[argc++] = list-> m_path;
585 if( ENABLE_FEATURE_CLEAN_UP )
587 opts = list-> m_options;
589 /* Add one more option */
591 argv = (char**) xrealloc( argv, ( argc + 1 ) * sizeof( char* ) );
592 argv[argc-1] = opts-> m_opt_val;
593 opts = opts-> m_next;
597 /* modutils uses short name for removal */
598 if (already_loaded (list->m_name) != 0) {
599 argv[argc++] = "rmmod";
602 argv[argc++] = list->m_name;
603 if( ENABLE_FEATURE_CLEAN_UP )
611 printf("%s module %s\n", do_insert?"Loading":"Unloading", list-> m_name );
621 execvp(argv[0], argv);
622 bb_perror_msg_and_die("exec of %s", argv[0]);
625 if (wait(&status) == -1) {
629 if (WIFEXITED(status))
630 rc2 = WEXITSTATUS(status);
631 if (WIFSIGNALED(status))
632 rc2 = WTERMSIG(status);
636 rc = rc2; /* only last module matters */
639 rc = 0; /* success if remove any mod */
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++] );
649 if( ENABLE_FEATURE_CLEAN_UP ) {
653 list = do_insert ? list-> m_prev : list-> m_next;
655 return (show_only) ? 0 : rc;
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)
663 static void check_dep ( char *mod, struct mod_list_t **head, struct mod_list_t **tail )
665 struct mod_list_t *find;
667 struct mod_opt_t *opt = 0;
670 // check dependencies
671 for ( dt = depend; dt; dt = dt-> m_next ) {
672 if ( strcmp ( dt-> m_name, mod ) == 0) {
678 bb_error_msg ("module %s not found.", mod);
682 // resolve alias names
683 while ( dt-> m_isalias ) {
684 if ( dt-> m_depcnt == 1 ) {
687 for ( adt = depend; adt; adt = adt-> m_next ) {
688 if ( strcmp ( adt-> m_name, dt-> m_deparr [0] ) == 0 )
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 */
696 adt-> m_options = append_option( adt-> m_options, opts-> m_opt_val );
697 opts = opts-> m_next;
702 bb_error_msg ("module %s not found.", mod);
707 bb_error_msg ("Bad alias %s", dt-> m_name);
714 opt = dt-> m_options;
716 // search for duplicates
717 for ( find = *head; find; find = find-> m_next ) {
718 if ( !strcmp ( mod, find-> m_name )) {
719 // found -> dequeue it
722 find-> m_prev-> m_next = find-> m_next;
724 *head = find-> m_next;
727 find-> m_next-> m_prev = find-> m_prev;
729 *tail = find-> m_prev;
731 break; // there can be only one duplicate
735 if ( !find ) { // did not find a duplicate
736 find = (struct mod_list_t *) xmalloc ( sizeof(struct mod_list_t));
738 find-> m_path = path;
739 find-> m_options = opt;
744 (*tail)-> m_next = find;
745 find-> m_prev = *tail;
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 );
761 static int mod_insert ( char *mod, int argc, char **argv )
763 struct mod_list_t *tail = 0;
764 struct mod_list_t *head = 0;
767 // get dep list for module mod
768 check_dep ( mod, &head, &tail );
770 if ( head && tail ) {
773 // append module args
774 for ( i = 0; i < argc; i++ )
775 head->m_options = append_option( head->m_options, argv[i] );
778 // process tail ---> head
779 rc = mod_process ( tail, 1 );
787 static int mod_remove ( char *mod )
790 static struct mod_list_t rm_a_dummy = { "-a", 0, 0 };
792 struct mod_list_t *head = 0;
793 struct mod_list_t *tail = 0;
796 check_dep ( mod, &head, &tail );
798 head = tail = &rm_a_dummy;
801 rc = mod_process ( head, 0 ); // process head ---> tail
808 extern int modprobe_main(int argc, char** argv)
811 int rc = EXIT_SUCCESS;
814 autoclean = show_only = quiet = do_syslog = verbose = 0;
816 while ((opt = getopt(argc, argv, "acdklnqrst:vVC:")) != -1) {
818 case 'c': // no config used
819 case 'l': // no pattern matching
822 case 'C': // no config used
823 case 't': // no pattern matching
824 bb_error_msg_and_die("-t and -C not supported");
854 depend = build_dep ( );
857 bb_error_msg_and_die ( "could not parse modules.dep\n" );
861 if (mod_remove ( optind < argc ?
862 bb_xstrdup (argv [optind]) : NULL )) {
863 bb_error_msg ("failed to remove module %s",
867 } while ( ++optind < argc );
870 bb_error_msg_and_die ( "No module or pattern provided\n" );
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] );
876 /* Here would be a good place to free up memory allocated during the dependencies build. */