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 (defined CONFIG_FEATURE_MODPROBE_MULTIPLE_OPTIONS)
134 /* static char* parse_command_string( char* src, char **dst );
135 * src: pointer to string containing argument
136 * dst: pointer to where to store the parsed argument
137 * return value: the pointer to the first char after the parsed argument,
138 * NULL if there was no argument parsed (only trailing spaces).
139 * Note that memory is allocated with bb_xstrdup when a new argument was
140 * parsed. Don't forget to free it!
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 ) );
241 #endif /* CONFIG_FEATURE_MODPROBE_MULTIPLE_OPTIONS */
244 * This function builds a list of dependency rules from /lib/modules/`uname -r\modules.dep.
245 * It then fills every modules and aliases with their default options, found by parsing
246 * modprobe.conf (or modules.conf, or conf.modules).
248 static struct dep_t *build_dep ( void )
252 struct dep_t *first = 0;
253 struct dep_t *current = 0;
255 char *filename = buffer;
256 int continuation_line = 0;
261 bb_error_msg_and_die("can't determine kernel version");
263 // check for buffer overflow in following code
264 if ( bb_strlen ( un.release ) > ( sizeof( buffer ) - 64 )) {
267 if (un.release[0] == '2') {
268 k_version = un.release[2] - '0';
271 strcpy ( filename, "/lib/modules/" );
272 strcat ( filename, un.release );
273 strcat ( filename, "/modules.dep" );
275 if (( fd = open ( filename, O_RDONLY )) < 0 ) {
277 /* Ok, that didn't work. Fall back to looking in /lib/modules */
278 if (( fd = open ( "/lib/modules/modules.dep", O_RDONLY )) < 0 ) {
283 while ( reads ( fd, buffer, sizeof( buffer ))) {
284 int l = bb_strlen ( buffer );
287 while ( isspace ( buffer [l-1] )) {
293 continuation_line = 0;
297 /* Is this a new module dep description? */
298 if ( !continuation_line ) {
299 /* find the dep begining */
300 char *col = strchr ( buffer, ':' );
304 /* This line is a dep description */
309 /* Find the beginning of the module file name */
311 mods = strrchr ( buffer, '/' );
314 mods = buffer; /* no path for this module */
316 mods++; /* there was a path for this module... */
318 /* find the path of the module */
319 modpath = strchr ( buffer, '/' ); /* ... and this is the path */
321 modpath = buffer; /* module with no path */
322 /* find the end of the module name in the file name */
323 if ( ENABLE_FEATURE_2_6_MODULES &&
324 (k_version > 4) && ( *(col-3) == '.' ) &&
325 ( *(col-2) == 'k' ) && ( *(col-1) == 'o' ) )
328 if (( *(col-2) == '.' ) && ( *(col-1) == 'o' ))
331 mod = bb_xstrndup ( mods, dot - mods );
333 /* enqueue new module */
335 first = current = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
338 current-> m_next = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
339 current = current-> m_next;
341 current-> m_name = mod;
342 current-> m_path = bb_xstrdup(modpath);
343 current-> m_options = NULL;
344 current-> m_isalias = 0;
345 current-> m_depcnt = 0;
346 current-> m_deparr = 0;
347 current-> m_next = 0;
352 /* this line is not a dep description */
356 /* It's a dep description continuation */
359 while ( p && *p && isblank(*p))
362 /* p points to the first dependable module; if NULL, no dependable module */
364 char *end = &buffer [l-1];
370 while ( isblank ( *end ) || ( *end == '\\' ))
375 /* search the end of the dependency */
376 next = strchr (p, ' ' );
385 /* find the begining of the module file name */
386 deps = strrchr ( p, '/' );
388 if ( !deps || ( deps < p )) {
391 while ( isblank ( *deps ))
397 /* find the end of the module name in the file name */
398 if ( ENABLE_FEATURE_2_6_MODULES &&
399 (k_version > 4) && ( *(next-2) == '.' ) &&
400 ( *(next-1) == 'k' ) && ( *next == 'o' ) )
403 if (( *(next-1) == '.' ) && ( *next == 'o' ))
406 /* Cope with blank lines */
407 if ((next-deps-ext+1) <= 0)
409 dep = bb_xstrndup ( deps, next - deps - ext + 1 );
411 /* Add the new dependable module name */
412 current-> m_depcnt++;
413 current-> m_deparr = (char **) xrealloc ( current-> m_deparr,
414 sizeof ( char *) * current-> m_depcnt );
415 current-> m_deparr [current-> m_depcnt - 1] = dep;
418 } while (next < end);
421 /* is there other dependable module(s) ? */
422 if ( buffer [l-1] == '\\' )
423 continuation_line = 1;
425 continuation_line = 0;
429 // alias parsing is not 100% correct (no correct handling of continuation lines within an alias) !
431 #if defined(CONFIG_FEATURE_2_6_MODULES)
432 if (( fd = open ( "/etc/modprobe.conf", O_RDONLY )) < 0 )
434 if (( fd = open ( "/etc/modules.conf", O_RDONLY )) < 0 )
435 if (( fd = open ( "/etc/conf.modules", O_RDONLY )) < 0 )
438 continuation_line = 0;
439 while ( reads ( fd, buffer, sizeof( buffer ))) {
443 p = strchr ( buffer, '#' );
447 l = bb_strlen ( buffer );
449 while ( l && isspace ( buffer [l-1] )) {
455 continuation_line = 0;
459 if ( !continuation_line ) {
460 if (( strncmp ( buffer, "alias", 5 ) == 0 ) && isspace ( buffer [5] )) {
463 if ( parse_tag_value ( buffer + 6, &alias, &mod )) {
464 /* handle alias as a module dependent on the aliased module */
466 first = current = (struct dep_t *) xcalloc ( 1, sizeof ( struct dep_t ));
469 current-> m_next = (struct dep_t *) xcalloc ( 1, sizeof ( struct dep_t ));
470 current = current-> m_next;
472 current-> m_name = bb_xstrdup ( alias );
473 current-> m_isalias = 1;
475 if (( strcmp ( mod, "off" ) == 0 ) || ( strcmp ( mod, "null" ) == 0 )) {
476 current-> m_depcnt = 0;
477 current-> m_deparr = 0;
480 current-> m_depcnt = 1;
481 current-> m_deparr = xmalloc ( 1 * sizeof( char * ));
482 current-> m_deparr[0] = bb_xstrdup ( mod );
484 current-> m_next = 0;
487 else if (( strncmp ( buffer, "options", 7 ) == 0 ) && isspace ( buffer [7] )) {
490 /* split the line in the module/alias name, and options */
491 if ( parse_tag_value ( buffer + 8, &mod, &opt )) {
494 /* find the corresponding module */
495 for ( dt = first; dt; dt = dt-> m_next ) {
496 if ( strcmp ( dt-> m_name, mod ) == 0 )
500 if ( CONFIG_FEATURE_MODPROBE_MULTIPLE_OPTIONS ) {
501 char* new_opt = NULL;
502 while( ( opt = parse_command_string( opt, &new_opt ) ) ) {
503 dt-> m_options = append_option( dt-> m_options, new_opt );
506 dt-> m_options = append_option( dt-> m_options, opt );
518 /* return 1 = loaded, 0 = not loaded, -1 = can't tell */
519 static int already_loaded (const char *name)
524 fd = open ("/proc/modules", O_RDONLY);
528 while ( reads ( fd, buffer, sizeof( buffer ))) {
531 p = strchr (buffer, ' ');
534 for( p = buffer; ENABLE_FEATURE_2_6_MODULES && *p; p++ ) {
535 *p = ((*p)=='-')?'_':*p;
537 if (strcmp (name, buffer) == 0) {
548 static int mod_process ( struct mod_list_t *list, int do_insert )
552 struct mod_opt_t *opts;
553 int argc_malloc; /* never used when CONFIG_FEATURE_CLEAN_UP not defined */
558 if( ENABLE_FEATURE_CLEAN_UP )
560 /* If CONFIG_FEATURE_CLEAN_UP is not defined, then we leak memory
561 * each time we allocate memory for argv.
562 * But it is (quite) small amounts of memory that leak each
563 * time a module is loaded, and it is reclaimed when modprobe
564 * exits anyway (even when standalone shell?).
565 * This could become a problem when loading a module with LOTS of
566 * dependencies, with LOTS of options for each dependencies, with
567 * very little memory on the target... But in that case, the module
568 * would not load because there is no more memory, so there's no
570 /* enough for minimal insmod (5 args + NULL) or rmmod (3 args + NULL) */
571 argv = (char**) malloc( 6 * sizeof( char* ) );
573 if (already_loaded (list->m_name) != 1) {
574 argv[argc++] = "insmod";
581 else if(verbose) /* verbose and quiet are mutually exclusive */
583 argv[argc++] = list-> m_path;
584 if( ENABLE_FEATURE_CLEAN_UP )
586 opts = list-> m_options;
588 /* Add one more option */
590 argv = (char**) xrealloc( argv, ( argc + 1 ) * sizeof( char* ) );
591 argv[argc-1] = opts-> m_opt_val;
592 opts = opts-> m_next;
596 /* modutils uses short name for removal */
597 if (already_loaded (list->m_name) != 0) {
598 argv[argc++] = "rmmod";
601 argv[argc++] = list->m_name;
602 if( ENABLE_FEATURE_CLEAN_UP )
610 printf("%s module %s\n", do_insert?"Loading":"Unloading", list-> m_name );
620 execvp(argv[0], argv);
621 bb_perror_msg_and_die("exec of %s", argv[0]);
624 if (wait(&status) == -1) {
628 if (WIFEXITED(status))
629 rc2 = WEXITSTATUS(status);
630 if (WIFSIGNALED(status))
631 rc2 = WTERMSIG(status);
635 rc = rc2; /* only last module matters */
638 rc = 0; /* success if remove any mod */
641 if( ENABLE_FEATURE_CLEAN_UP )
642 /* the last value in the array has index == argc, but
643 * it is the terminating NULL, so we must not free it. */
644 while( argc_malloc < argc ) {
645 free( argv[argc_malloc++] );
648 if( ENABLE_FEATURE_CLEAN_UP ) {
652 list = do_insert ? list-> m_prev : list-> m_next;
654 return (show_only) ? 0 : rc;
658 * Builds the dependency list (aka stack) of a module.
659 * head: the highest module in the stack (last to insmod, first to rmmod)
660 * tail: the lowest module in the stack (first to insmod, last to rmmod)
662 static void check_dep ( char *mod, struct mod_list_t **head, struct mod_list_t **tail )
664 struct mod_list_t *find;
666 struct mod_opt_t *opt = 0;
669 // check dependencies
670 for ( dt = depend; dt; dt = dt-> m_next ) {
671 if ( strcmp ( dt-> m_name, mod ) == 0) {
677 bb_error_msg ("module %s not found.", mod);
681 // resolve alias names
682 while ( dt-> m_isalias ) {
683 if ( dt-> m_depcnt == 1 ) {
686 for ( adt = depend; adt; adt = adt-> m_next ) {
687 if ( strcmp ( adt-> m_name, dt-> m_deparr [0] ) == 0 )
691 /* This is the module we are aliased to */
692 struct mod_opt_t *opts = dt-> m_options;
693 /* Option of the alias are appended to the options of the module */
695 adt-> m_options = append_option( adt-> m_options, opts-> m_opt_val );
696 opts = opts-> m_next;
701 bb_error_msg ("module %s not found.", mod);
706 bb_error_msg ("Bad alias %s", dt-> m_name);
713 opt = dt-> m_options;
715 // search for duplicates
716 for ( find = *head; find; find = find-> m_next ) {
717 if ( !strcmp ( mod, find-> m_name )) {
718 // found -> dequeue it
721 find-> m_prev-> m_next = find-> m_next;
723 *head = find-> m_next;
726 find-> m_next-> m_prev = find-> m_prev;
728 *tail = find-> m_prev;
730 break; // there can be only one duplicate
734 if ( !find ) { // did not find a duplicate
735 find = (struct mod_list_t *) xmalloc ( sizeof(struct mod_list_t));
737 find-> m_path = path;
738 find-> m_options = opt;
743 (*tail)-> m_next = find;
744 find-> m_prev = *tail;
754 /* Add all dependable module for that new module */
755 for ( i = 0; i < dt-> m_depcnt; i++ )
756 check_dep ( dt-> m_deparr [i], head, tail );
760 static int mod_insert ( char *mod, int argc, char **argv )
762 struct mod_list_t *tail = 0;
763 struct mod_list_t *head = 0;
766 // get dep list for module mod
767 check_dep ( mod, &head, &tail );
769 if ( head && tail ) {
772 // append module args
773 for ( i = 0; i < argc; i++ )
774 head->m_options = append_option( head->m_options, argv[i] );
777 // process tail ---> head
778 rc = mod_process ( tail, 1 );
786 static int mod_remove ( char *mod )
789 static struct mod_list_t rm_a_dummy = { "-a", 0, 0 };
791 struct mod_list_t *head = 0;
792 struct mod_list_t *tail = 0;
795 check_dep ( mod, &head, &tail );
797 head = tail = &rm_a_dummy;
800 rc = mod_process ( head, 0 ); // process head ---> tail
807 extern int modprobe_main(int argc, char** argv)
810 int rc = EXIT_SUCCESS;
813 autoclean = show_only = quiet = do_syslog = verbose = 0;
815 while ((opt = getopt(argc, argv, "acdklnqrst:vVC:")) != -1) {
817 case 'c': // no config used
818 case 'l': // no pattern matching
821 case 'C': // no config used
822 case 't': // no pattern matching
823 bb_error_msg_and_die("-t and -C not supported");
853 depend = build_dep ( );
856 bb_error_msg_and_die ( "could not parse modules.dep\n" );
860 if (mod_remove ( optind < argc ?
861 bb_xstrdup (argv [optind]) : NULL )) {
862 bb_error_msg ("failed to remove module %s",
866 } while ( ++optind < argc );
869 bb_error_msg_and_die ( "No module or pattern provided\n" );
871 if ( mod_insert ( bb_xstrdup ( argv [optind] ), argc - optind - 1, argv + optind + 1 ))
872 bb_error_msg_and_die ( "failed to load module %s", argv [optind] );
875 /* Here would be a good place to free up memory allocated during the dependencies build. */