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;
59 #define main_options "acdklnqrst:vVC:"
60 #define INSERT_ALL 1 /* a */
61 #define DUMP_CONF_EXIT 2 /* c */
62 #define D_OPT_IGNORED 4 /* d */
63 #define AUTOCLEAN_FLG 8 /* k */
64 #define LIST_ALL 16 /* l */
65 #define SHOW_ONLY 32 /* n */
66 #define QUIET 64 /* q */
67 #define REMOVE_OPT 128 /* r */
68 #define DO_SYSLOG 256 /* s */
69 #define RESTRICT_DIR 512 /* t */
70 #define VERBOSE 1024 /* v */
71 #define VERSION_ONLY 2048 /* V */
72 #define CONFIG_FILE 4096 /* C */
74 #define autoclean (main_opts & AUTOCLEAN_FLG)
75 #define show_only (main_opts & SHOW_ONLY)
76 #define quiet (main_opts & QUIET)
77 #define remove_opt (main_opts & REMOVE_OPT)
78 #define do_syslog (main_opts & DO_SYSLOG)
79 #define verbose (main_opts & VERBOSE)
83 static int parse_tag_value ( char *buffer, char **ptag, char **pvalue )
87 while ( isspace ( *buffer ))
90 while ( !isspace ( *value ))
91 if (!*value) return 0;
94 while ( isspace ( *value ))
96 if (!*value) return 0;
104 /* Jump through hoops to simulate how fgets() grabs just one line at a
105 * time... Don't use any stdio since modprobe gets called from a kernel
106 * thread and stdio junk can overflow the limited stack...
108 static char *reads ( int fd, char *buffer, size_t len )
110 int n = read ( fd, buffer, len );
116 p = strchr ( buffer, '\n' );
121 offset = lseek ( fd, 0L, SEEK_CUR ); // Get the current file descriptor offset
122 lseek ( fd, offset-n + (p-buffer) + 1, SEEK_SET ); // Set the file descriptor offset to right after the \n
134 * This function appends an option to a list
136 static struct mod_opt_t *append_option( struct mod_opt_t *opt_list, char *opt )
138 struct mod_opt_t *ol = opt_list;
141 while( ol-> m_next ) {
144 ol-> m_next = xmalloc( sizeof( struct mod_opt_t ) );
147 ol = opt_list = xmalloc( sizeof( struct mod_opt_t ) );
150 ol-> m_opt_val = bb_xstrdup( opt );
156 #if ENABLE_FEATURE_MODPROBE_MULTIPLE_OPTIONS
157 /* static char* parse_command_string( char* src, char **dst );
158 * src: pointer to string containing argument
159 * dst: pointer to where to store the parsed argument
160 * return value: the pointer to the first char after the parsed argument,
161 * NULL if there was no argument parsed (only trailing spaces).
162 * Note that memory is allocated with bb_xstrdup when a new argument was
163 * parsed. Don't forget to free it!
165 #define ARG_EMPTY 0x00
166 #define ARG_IN_DQUOTES 0x01
167 #define ARG_IN_SQUOTES 0x02
168 static char *parse_command_string( char *src, char **dst )
170 int opt_status = ARG_EMPTY;
173 /* Dumb you, I have nothing to do... */
174 if( src == NULL ) return src;
176 /* Skip leading spaces */
177 while( *src == ' ' ) {
180 /* Is the end of string reached? */
184 /* Reached the start of an argument
185 * By the way, we duplicate a little too much
186 * here but what is too much is freed later. */
187 *dst = tmp_str = bb_xstrdup( src );
188 /* Get to the end of that argument */
189 while( ( *tmp_str != '\0' )
190 && ( ( *tmp_str != ' ' )
191 || ( opt_status & ( ARG_IN_DQUOTES | ARG_IN_SQUOTES ) ) ) ) {
194 if( opt_status & ARG_IN_DQUOTES ) {
195 /* Already in double quotes, keep current char as is */
197 /* shift left 1 char, until end of string: get rid of the opening/closing quotes */
198 memmove( tmp_str, tmp_str + 1, strlen( tmp_str ) );
199 /* mark me: we enter or leave single quotes */
200 opt_status ^= ARG_IN_SQUOTES;
201 /* Back one char, as we need to re-scan the new char there. */
206 if( opt_status & ARG_IN_SQUOTES ) {
207 /* Already in single quotes, keep current char as is */
209 /* shift left 1 char, until end of string: get rid of the opening/closing quotes */
210 memmove( tmp_str, tmp_str + 1, strlen( tmp_str ) );
211 /* mark me: we enter or leave double quotes */
212 opt_status ^= ARG_IN_DQUOTES;
213 /* Back one char, as we need to re-scan the new char there. */
218 if( opt_status & ARG_IN_SQUOTES ) {
219 /* Between single quotes: keep as is. */
221 switch( *(tmp_str+1) ) {
230 /* We escaped a special character. For now, keep
231 * both the back-slash and the following char. */
235 /* We escaped a space or a single or double quote,
236 * or a back-slash, or a non-escapable char. Remove
237 * the '\' and keep the new current char as is. */
238 memmove( tmp_str, tmp_str + 1, strlen( tmp_str ) );
243 /* Any other char that is special shall appear here.
244 * Example: $ starts a variable
246 do_variable_expansion();
250 /* any other char is kept as is. */
253 tmp_str++; /* Go to next char */
254 src++; /* Go to next char to find the end of the argument. */
256 /* End of string, but still no ending quote */
257 if( opt_status & ( ARG_IN_DQUOTES | ARG_IN_SQUOTES ) ) {
258 bb_error_msg_and_die( "unterminated (single or double) quote in options list: %s", src );
261 *dst = xrealloc( *dst, (tmp_str - *dst ) );
265 #define parse_command_string(src, dst) (0)
266 #endif /* ENABLE_FEATURE_MODPROBE_MULTIPLE_OPTIONS */
269 * This function builds a list of dependency rules from /lib/modules/`uname -r\modules.dep.
270 * It then fills every modules and aliases with their default options, found by parsing
271 * modprobe.conf (or modules.conf, or conf.modules).
273 static struct dep_t *build_dep ( void )
277 struct dep_t *first = 0;
278 struct dep_t *current = 0;
281 int continuation_line = 0;
286 bb_error_msg_and_die("can't determine kernel version");
288 if (un.release[0] == '2') {
289 k_version = un.release[2] - '0';
292 filename = bb_xasprintf("/lib/modules/%s/modules.dep", un.release );
294 if (( fd = open ( filename, O_RDONLY )) < 0 ) {
296 /* Ok, that didn't work. Fall back to looking in /lib/modules */
297 if (( fd = open ( "/lib/modules/modules.dep", O_RDONLY )) < 0 ) {
303 while ( reads ( fd, buffer, sizeof( buffer ))) {
304 int l = bb_strlen ( buffer );
307 while ( l > 0 && isspace ( buffer [l-1] )) {
313 continuation_line = 0;
317 /* Is this a new module dep description? */
318 if ( !continuation_line ) {
319 /* find the dep beginning */
320 char *col = strchr ( buffer, ':' );
324 /* This line is a dep description */
329 /* Find the beginning of the module file name */
331 mods = strrchr ( buffer, '/' );
334 mods = buffer; /* no path for this module */
336 mods++; /* there was a path for this module... */
338 /* find the path of the module */
339 modpath = strchr ( buffer, '/' ); /* ... and this is the path */
341 modpath = buffer; /* module with no path */
342 /* find the end of the module name in the file name */
343 if ( ENABLE_FEATURE_2_6_MODULES &&
344 (k_version > 4) && ( *(col-3) == '.' ) &&
345 ( *(col-2) == 'k' ) && ( *(col-1) == 'o' ) )
348 if (( *(col-2) == '.' ) && ( *(col-1) == 'o' ))
351 mod = bb_xstrndup ( mods, dot - mods );
353 /* enqueue new module */
355 first = current = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
358 current-> m_next = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
359 current = current-> m_next;
361 current-> m_name = mod;
362 current-> m_path = bb_xstrdup(modpath);
363 current-> m_options = NULL;
364 current-> m_isalias = 0;
365 current-> m_depcnt = 0;
366 current-> m_deparr = 0;
367 current-> m_next = 0;
372 /* this line is not a dep description */
376 /* It's a dep description continuation */
379 while ( p && *p && isblank(*p))
382 /* p points to the first dependable module; if NULL, no dependable module */
384 char *end = &buffer [l-1];
390 while ( isblank ( *end ) || ( *end == '\\' ))
395 /* search the end of the dependency */
396 next = strchr (p, ' ' );
405 /* find the beginning of the module file name */
406 deps = strrchr ( p, '/' );
408 if ( !deps || ( deps < p )) {
411 while ( isblank ( *deps ))
417 /* find the end of the module name in the file name */
418 if ( ENABLE_FEATURE_2_6_MODULES &&
419 (k_version > 4) && ( *(next-2) == '.' ) &&
420 ( *(next-1) == 'k' ) && ( *next == 'o' ) )
423 if (( *(next-1) == '.' ) && ( *next == 'o' ))
426 /* Cope with blank lines */
427 if ((next-deps-ext+1) <= 0)
429 dep = bb_xstrndup ( deps, next - deps - ext + 1 );
431 /* Add the new dependable module name */
432 current-> m_depcnt++;
433 current-> m_deparr = (char **) xrealloc ( current-> m_deparr,
434 sizeof ( char *) * current-> m_depcnt );
435 current-> m_deparr [current-> m_depcnt - 1] = dep;
438 } while (next < end);
441 /* is there other dependable module(s) ? */
442 if ( buffer [l-1] == '\\' )
443 continuation_line = 1;
445 continuation_line = 0;
449 // alias parsing is not 100% correct (no correct handling of continuation lines within an alias) !
451 if (!ENABLE_FEATURE_2_6_MODULES
452 || ( fd = open ( "/etc/modprobe.conf", O_RDONLY )) < 0 )
453 if (( fd = open ( "/etc/modules.conf", O_RDONLY )) < 0 )
454 if (( fd = open ( "/etc/conf.modules", O_RDONLY )) < 0 )
457 continuation_line = 0;
458 while ( reads ( fd, buffer, sizeof( buffer ))) {
462 p = strchr ( buffer, '#' );
466 l = bb_strlen ( buffer );
468 while ( l && isspace ( buffer [l-1] )) {
474 continuation_line = 0;
478 if ( !continuation_line ) {
479 if (( strncmp ( buffer, "alias", 5 ) == 0 ) && isspace ( buffer [5] )) {
482 if ( parse_tag_value ( buffer + 6, &alias, &mod )) {
483 /* handle alias as a module dependent on the aliased module */
485 first = current = (struct dep_t *) xcalloc ( 1, sizeof ( struct dep_t ));
488 current-> m_next = (struct dep_t *) xcalloc ( 1, sizeof ( struct dep_t ));
489 current = current-> m_next;
491 current-> m_name = bb_xstrdup ( alias );
492 current-> m_isalias = 1;
494 if (( strcmp ( mod, "off" ) == 0 ) || ( strcmp ( mod, "null" ) == 0 )) {
495 current-> m_depcnt = 0;
496 current-> m_deparr = 0;
499 current-> m_depcnt = 1;
500 current-> m_deparr = xmalloc ( 1 * sizeof( char * ));
501 current-> m_deparr[0] = bb_xstrdup ( mod );
503 current-> m_next = 0;
506 else if (( strncmp ( buffer, "options", 7 ) == 0 ) && isspace ( buffer [7] )) {
509 /* split the line in the module/alias name, and options */
510 if ( parse_tag_value ( buffer + 8, &mod, &opt )) {
513 /* find the corresponding module */
514 for ( dt = first; dt; dt = dt-> m_next ) {
515 if ( strcmp ( dt-> m_name, mod ) == 0 )
519 if ( ENABLE_FEATURE_MODPROBE_MULTIPLE_OPTIONS ) {
520 char* new_opt = NULL;
521 while( ( opt = parse_command_string( opt, &new_opt ) ) ) {
522 dt-> m_options = append_option( dt-> m_options, new_opt );
525 dt-> m_options = append_option( dt-> m_options, opt );
537 /* return 1 = loaded, 0 = not loaded, -1 = can't tell */
538 static int already_loaded (const char *name)
543 fd = open ("/proc/modules", O_RDONLY);
547 while ( reads ( fd, buffer, sizeof( buffer ))) {
550 p = strchr (buffer, ' ');
553 for( p = buffer; ENABLE_FEATURE_2_6_MODULES && *p; p++ ) {
554 *p = ((*p)=='-')?'_':*p;
556 if (strcmp (name, buffer) == 0) {
567 static int mod_process ( struct mod_list_t *list, int do_insert )
571 struct mod_opt_t *opts;
572 int argc_malloc; /* never used when CONFIG_FEATURE_CLEAN_UP not defined */
577 if( ENABLE_FEATURE_CLEAN_UP )
579 /* If CONFIG_FEATURE_CLEAN_UP is not defined, then we leak memory
580 * each time we allocate memory for argv.
581 * But it is (quite) small amounts of memory that leak each
582 * time a module is loaded, and it is reclaimed when modprobe
583 * exits anyway (even when standalone shell?).
584 * This could become a problem when loading a module with LOTS of
585 * dependencies, with LOTS of options for each dependencies, with
586 * very little memory on the target... But in that case, the module
587 * would not load because there is no more memory, so there's no
589 /* enough for minimal insmod (5 args + NULL) or rmmod (3 args + NULL) */
590 argv = (char**) malloc( 6 * sizeof( char* ) );
592 if (already_loaded (list->m_name) != 1) {
593 argv[argc++] = "insmod";
600 else if(verbose) /* verbose and quiet are mutually exclusive */
602 argv[argc++] = list-> m_path;
603 if( ENABLE_FEATURE_CLEAN_UP )
605 opts = list-> m_options;
607 /* Add one more option */
609 argv = (char**) xrealloc( argv, ( argc + 1 ) * sizeof( char* ) );
610 argv[argc-1] = opts-> m_opt_val;
611 opts = opts-> m_next;
615 /* modutils uses short name for removal */
616 if (already_loaded (list->m_name) != 0) {
617 argv[argc++] = "rmmod";
620 argv[argc++] = list->m_name;
621 if( ENABLE_FEATURE_CLEAN_UP )
629 printf("%s module %s\n", do_insert?"Loading":"Unloading", list-> m_name );
639 execvp(argv[0], argv);
640 bb_perror_msg_and_die("exec of %s", argv[0]);
643 if (wait(&status) == -1) {
647 if (WIFEXITED(status))
648 rc2 = WEXITSTATUS(status);
649 if (WIFSIGNALED(status))
650 rc2 = WTERMSIG(status);
654 rc = rc2; /* only last module matters */
657 rc = 0; /* success if remove any mod */
660 if( ENABLE_FEATURE_CLEAN_UP )
661 /* the last value in the array has index == argc, but
662 * it is the terminating NULL, so we must not free it. */
663 while( argc_malloc < argc ) {
664 free( argv[argc_malloc++] );
667 if( ENABLE_FEATURE_CLEAN_UP ) {
671 list = do_insert ? list-> m_prev : list-> m_next;
673 return (show_only) ? 0 : rc;
677 * Builds the dependency list (aka stack) of a module.
678 * head: the highest module in the stack (last to insmod, first to rmmod)
679 * tail: the lowest module in the stack (first to insmod, last to rmmod)
681 static void check_dep ( char *mod, struct mod_list_t **head, struct mod_list_t **tail )
683 struct mod_list_t *find;
685 struct mod_opt_t *opt = 0;
688 // check dependencies
689 for ( dt = depend; dt; dt = dt-> m_next ) {
690 if ( strcmp ( dt-> m_name, mod ) == 0) {
696 bb_error_msg ("module %s not found.", mod);
700 // resolve alias names
701 while ( dt-> m_isalias ) {
702 if ( dt-> m_depcnt == 1 ) {
705 for ( adt = depend; adt; adt = adt-> m_next ) {
706 if ( strcmp ( adt-> m_name, dt-> m_deparr [0] ) == 0 )
710 /* This is the module we are aliased to */
711 struct mod_opt_t *opts = dt-> m_options;
712 /* Option of the alias are appended to the options of the module */
714 adt-> m_options = append_option( adt-> m_options, opts-> m_opt_val );
715 opts = opts-> m_next;
720 bb_error_msg ("module %s not found.", mod);
725 bb_error_msg ("Bad alias %s", dt-> m_name);
732 opt = dt-> m_options;
734 // search for duplicates
735 for ( find = *head; find; find = find-> m_next ) {
736 if ( !strcmp ( mod, find-> m_name )) {
737 // found -> dequeue it
740 find-> m_prev-> m_next = find-> m_next;
742 *head = find-> m_next;
745 find-> m_next-> m_prev = find-> m_prev;
747 *tail = find-> m_prev;
749 break; // there can be only one duplicate
753 if ( !find ) { // did not find a duplicate
754 find = (struct mod_list_t *) xmalloc ( sizeof(struct mod_list_t));
756 find-> m_path = path;
757 find-> m_options = opt;
762 (*tail)-> m_next = find;
763 find-> m_prev = *tail;
773 /* Add all dependable module for that new module */
774 for ( i = 0; i < dt-> m_depcnt; i++ )
775 check_dep ( dt-> m_deparr [i], head, tail );
779 static int mod_insert ( char *mod, int argc, char **argv )
781 struct mod_list_t *tail = 0;
782 struct mod_list_t *head = 0;
785 // get dep list for module mod
786 check_dep ( mod, &head, &tail );
788 if ( head && tail ) {
791 // append module args
792 for ( i = 0; i < argc; i++ )
793 head->m_options = append_option( head->m_options, argv[i] );
796 // process tail ---> head
797 rc = mod_process ( tail, 1 );
805 static int mod_remove ( char *mod )
808 static struct mod_list_t rm_a_dummy = { "-a", NULL, NULL, NULL, NULL };
810 struct mod_list_t *head = 0;
811 struct mod_list_t *tail = 0;
814 check_dep ( mod, &head, &tail );
816 head = tail = &rm_a_dummy;
819 rc = mod_process ( head, 0 ); // process head ---> tail
826 int modprobe_main(int argc, char** argv)
828 int rc = EXIT_SUCCESS;
831 bb_opt_complementally = "?V-:q-v:v-q";
832 main_opts = bb_getopt_ulflags(argc, argv, "acdklnqrst:vVC:",
834 if((main_opts & (DUMP_CONF_EXIT | LIST_ALL)))
836 if((main_opts & (RESTRICT_DIR | CONFIG_FILE)))
837 bb_error_msg_and_die("-t and -C not supported");
839 depend = build_dep ( );
842 bb_error_msg_and_die ( "could not parse modules.dep\n" );
846 if (mod_remove ( optind < argc ?
847 argv [optind] : NULL )) {
848 bb_error_msg ("failed to remove module %s",
852 } while ( ++optind < argc );
855 bb_error_msg_and_die ( "No module or pattern provided\n" );
857 if ( mod_insert ( argv [optind], argc - optind - 1, argv + optind + 1 ))
858 bb_error_msg_and_die ( "failed to load module %s", argv [optind] );
861 /* Here would be a good place to free up memory allocated during the dependencies build. */