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 reads aliases and default module options from a configuration file
270 * (/etc/modprobe.conf syntax). It supports includes (only files, no directories).
272 static void include_conf ( struct dep_t **first, struct dep_t **current, char *buffer, int buflen, int fd )
274 int continuation_line = 0;
276 // alias parsing is not 100% correct (no correct handling of continuation lines within an alias) !
278 while ( reads ( fd, buffer, buflen)) {
282 p = strchr ( buffer, '#' );
286 l = bb_strlen ( buffer );
288 while ( l && isspace ( buffer [l-1] )) {
294 continuation_line = 0;
298 if ( !continuation_line ) {
299 if (( strncmp ( buffer, "alias", 5 ) == 0 ) && isspace ( buffer [5] )) {
302 if ( parse_tag_value ( buffer + 6, &alias, &mod )) {
303 /* handle alias as a module dependent on the aliased module */
305 (*first) = (*current) = (struct dep_t *) xcalloc ( 1, sizeof ( struct dep_t ));
308 (*current)-> m_next = (struct dep_t *) xcalloc ( 1, sizeof ( struct dep_t ));
309 (*current) = (*current)-> m_next;
311 (*current)-> m_name = bb_xstrdup ( alias );
312 (*current)-> m_isalias = 1;
314 if (( strcmp ( mod, "off" ) == 0 ) || ( strcmp ( mod, "null" ) == 0 )) {
315 (*current)-> m_depcnt = 0;
316 (*current)-> m_deparr = 0;
319 (*current)-> m_depcnt = 1;
320 (*current)-> m_deparr = xmalloc ( 1 * sizeof( char * ));
321 (*current)-> m_deparr[0] = bb_xstrdup ( mod );
323 (*current)-> m_next = 0;
326 else if (( strncmp ( buffer, "options", 7 ) == 0 ) && isspace ( buffer [7] )) {
329 /* split the line in the module/alias name, and options */
330 if ( parse_tag_value ( buffer + 8, &mod, &opt )) {
333 /* find the corresponding module */
334 for ( dt = *first; dt; dt = dt-> m_next ) {
335 if ( strcmp ( dt-> m_name, mod ) == 0 )
339 if ( ENABLE_FEATURE_MODPROBE_MULTIPLE_OPTIONS ) {
340 char* new_opt = NULL;
341 while( ( opt = parse_command_string( opt, &new_opt ) ) ) {
342 dt-> m_options = append_option( dt-> m_options, new_opt );
345 dt-> m_options = append_option( dt-> m_options, opt );
350 else if (( strncmp ( buffer, "include", 7 ) == 0 ) && isspace ( buffer [7] )) {
352 int fdi; char *filename = buffer + 8;
354 while ( isspace ( *filename ))
357 if (( fdi = open ( filename, O_RDONLY )) >= 0 ) {
358 include_conf(first, current, buffer, buflen, fdi);
367 * This function builds a list of dependency rules from /lib/modules/`uname -r\modules.dep.
368 * It then fills every modules and aliases with their default options, found by parsing
369 * modprobe.conf (or modules.conf, or conf.modules).
371 static struct dep_t *build_dep ( void )
375 struct dep_t *first = 0;
376 struct dep_t *current = 0;
379 int continuation_line = 0;
384 bb_error_msg_and_die("can't determine kernel version");
386 if (un.release[0] == '2') {
387 k_version = un.release[2] - '0';
390 filename = bb_xasprintf("/lib/modules/%s/modules.dep", un.release );
392 if (( fd = open ( filename, O_RDONLY )) < 0 ) {
394 /* Ok, that didn't work. Fall back to looking in /lib/modules */
395 if (( fd = open ( "/lib/modules/modules.dep", O_RDONLY )) < 0 ) {
401 while ( reads ( fd, buffer, sizeof( buffer ))) {
402 int l = bb_strlen ( buffer );
405 while ( l > 0 && isspace ( buffer [l-1] )) {
411 continuation_line = 0;
415 /* Is this a new module dep description? */
416 if ( !continuation_line ) {
417 /* find the dep beginning */
418 char *col = strchr ( buffer, ':' );
422 /* This line is a dep description */
427 /* Find the beginning of the module file name */
429 mods = strrchr ( buffer, '/' );
432 mods = buffer; /* no path for this module */
434 mods++; /* there was a path for this module... */
436 /* find the path of the module */
437 modpath = strchr ( buffer, '/' ); /* ... and this is the path */
439 modpath = buffer; /* module with no path */
440 /* find the end of the module name in the file name */
441 if ( ENABLE_FEATURE_2_6_MODULES &&
442 (k_version > 4) && ( *(col-3) == '.' ) &&
443 ( *(col-2) == 'k' ) && ( *(col-1) == 'o' ) )
446 if (( *(col-2) == '.' ) && ( *(col-1) == 'o' ))
449 mod = bb_xstrndup ( mods, dot - mods );
451 /* enqueue new module */
453 first = current = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
456 current-> m_next = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
457 current = current-> m_next;
459 current-> m_name = mod;
460 current-> m_path = bb_xstrdup(modpath);
461 current-> m_options = NULL;
462 current-> m_isalias = 0;
463 current-> m_depcnt = 0;
464 current-> m_deparr = 0;
465 current-> m_next = 0;
470 /* this line is not a dep description */
474 /* It's a dep description continuation */
477 while ( p && *p && isblank(*p))
480 /* p points to the first dependable module; if NULL, no dependable module */
482 char *end = &buffer [l-1];
488 while ( isblank ( *end ) || ( *end == '\\' ))
493 /* search the end of the dependency */
494 next = strchr (p, ' ' );
503 /* find the beginning of the module file name */
504 deps = strrchr ( p, '/' );
506 if ( !deps || ( deps < p )) {
509 while ( isblank ( *deps ))
515 /* find the end of the module name in the file name */
516 if ( ENABLE_FEATURE_2_6_MODULES &&
517 (k_version > 4) && ( *(next-2) == '.' ) &&
518 ( *(next-1) == 'k' ) && ( *next == 'o' ) )
521 if (( *(next-1) == '.' ) && ( *next == 'o' ))
524 /* Cope with blank lines */
525 if ((next-deps-ext+1) <= 0)
527 dep = bb_xstrndup ( deps, next - deps - ext + 1 );
529 /* Add the new dependable module name */
530 current-> m_depcnt++;
531 current-> m_deparr = (char **) xrealloc ( current-> m_deparr,
532 sizeof ( char *) * current-> m_depcnt );
533 current-> m_deparr [current-> m_depcnt - 1] = dep;
536 } while (next < end);
539 /* is there other dependable module(s) ? */
540 if ( buffer [l-1] == '\\' )
541 continuation_line = 1;
543 continuation_line = 0;
547 if (!ENABLE_FEATURE_2_6_MODULES
548 || ( fd = open ( "/etc/modprobe.conf", O_RDONLY )) < 0 )
549 if (( fd = open ( "/etc/modules.conf", O_RDONLY )) < 0 )
550 if (( fd = open ( "/etc/conf.modules", O_RDONLY )) < 0 )
553 include_conf (&first, ¤t, buffer, sizeof(buffer), fd);
556 filename = bb_xasprintf("/lib/modules/%s/modules.alias", un.release );
558 if (( fd = open ( filename, O_RDONLY )) < 0 ) {
560 /* Ok, that didn't work. Fall back to looking in /lib/modules */
561 if (( fd = open ( "/lib/modules/modules.alias", O_RDONLY )) < 0 ) {
567 include_conf (&first, ¤t, buffer, sizeof(buffer), fd);
573 /* return 1 = loaded, 0 = not loaded, -1 = can't tell */
574 static int already_loaded (const char *name)
579 fd = open ("/proc/modules", O_RDONLY);
583 while ( reads ( fd, buffer, sizeof( buffer ))) {
586 p = strchr (buffer, ' ');
589 for( p = buffer; ENABLE_FEATURE_2_6_MODULES && *p; p++ ) {
590 *p = ((*p)=='-')?'_':*p;
592 if (strcmp (name, buffer) == 0) {
603 static int mod_process ( struct mod_list_t *list, int do_insert )
607 struct mod_opt_t *opts;
608 int argc_malloc; /* never used when CONFIG_FEATURE_CLEAN_UP not defined */
613 if( ENABLE_FEATURE_CLEAN_UP )
615 /* If CONFIG_FEATURE_CLEAN_UP is not defined, then we leak memory
616 * each time we allocate memory for argv.
617 * But it is (quite) small amounts of memory that leak each
618 * time a module is loaded, and it is reclaimed when modprobe
619 * exits anyway (even when standalone shell?).
620 * This could become a problem when loading a module with LOTS of
621 * dependencies, with LOTS of options for each dependencies, with
622 * very little memory on the target... But in that case, the module
623 * would not load because there is no more memory, so there's no
625 /* enough for minimal insmod (5 args + NULL) or rmmod (3 args + NULL) */
626 argv = (char**) malloc( 6 * sizeof( char* ) );
628 if (already_loaded (list->m_name) != 1) {
629 argv[argc++] = "insmod";
636 else if(verbose) /* verbose and quiet are mutually exclusive */
638 argv[argc++] = list-> m_path;
639 if( ENABLE_FEATURE_CLEAN_UP )
641 opts = list-> m_options;
643 /* Add one more option */
645 argv = (char**) xrealloc( argv, ( argc + 1 ) * sizeof( char* ) );
646 argv[argc-1] = opts-> m_opt_val;
647 opts = opts-> m_next;
651 /* modutils uses short name for removal */
652 if (already_loaded (list->m_name) != 0) {
653 argv[argc++] = "rmmod";
656 argv[argc++] = list->m_name;
657 if( ENABLE_FEATURE_CLEAN_UP )
665 printf("%s module %s\n", do_insert?"Loading":"Unloading", list-> m_name );
675 execvp(argv[0], argv);
676 bb_perror_msg_and_die("exec of %s", argv[0]);
679 if (wait(&status) == -1) {
683 if (WIFEXITED(status))
684 rc2 = WEXITSTATUS(status);
685 if (WIFSIGNALED(status))
686 rc2 = WTERMSIG(status);
690 rc = rc2; /* only last module matters */
693 rc = 0; /* success if remove any mod */
696 if( ENABLE_FEATURE_CLEAN_UP )
697 /* the last value in the array has index == argc, but
698 * it is the terminating NULL, so we must not free it. */
699 while( argc_malloc < argc ) {
700 free( argv[argc_malloc++] );
703 if( ENABLE_FEATURE_CLEAN_UP ) {
707 list = do_insert ? list-> m_prev : list-> m_next;
709 return (show_only) ? 0 : rc;
713 * Builds the dependency list (aka stack) of a module.
714 * head: the highest module in the stack (last to insmod, first to rmmod)
715 * tail: the lowest module in the stack (first to insmod, last to rmmod)
717 static void check_dep ( char *mod, struct mod_list_t **head, struct mod_list_t **tail )
719 struct mod_list_t *find;
721 struct mod_opt_t *opt = 0;
724 // check dependencies
725 for ( dt = depend; dt; dt = dt-> m_next ) {
726 if ( strcmp ( dt-> m_name, mod ) == 0) {
732 bb_error_msg ("module %s not found.", mod);
736 // resolve alias names
737 while ( dt-> m_isalias ) {
738 if ( dt-> m_depcnt == 1 ) {
741 for ( adt = depend; adt; adt = adt-> m_next ) {
742 if ( strcmp ( adt-> m_name, dt-> m_deparr [0] ) == 0 )
746 /* This is the module we are aliased to */
747 struct mod_opt_t *opts = dt-> m_options;
748 /* Option of the alias are appended to the options of the module */
750 adt-> m_options = append_option( adt-> m_options, opts-> m_opt_val );
751 opts = opts-> m_next;
756 bb_error_msg ("module %s not found.", mod);
761 bb_error_msg ("Bad alias %s", dt-> m_name);
768 opt = dt-> m_options;
770 // search for duplicates
771 for ( find = *head; find; find = find-> m_next ) {
772 if ( !strcmp ( mod, find-> m_name )) {
773 // found -> dequeue it
776 find-> m_prev-> m_next = find-> m_next;
778 *head = find-> m_next;
781 find-> m_next-> m_prev = find-> m_prev;
783 *tail = find-> m_prev;
785 break; // there can be only one duplicate
789 if ( !find ) { // did not find a duplicate
790 find = (struct mod_list_t *) xmalloc ( sizeof(struct mod_list_t));
792 find-> m_path = path;
793 find-> m_options = opt;
798 (*tail)-> m_next = find;
799 find-> m_prev = *tail;
809 /* Add all dependable module for that new module */
810 for ( i = 0; i < dt-> m_depcnt; i++ )
811 check_dep ( dt-> m_deparr [i], head, tail );
815 static int mod_insert ( char *mod, int argc, char **argv )
817 struct mod_list_t *tail = 0;
818 struct mod_list_t *head = 0;
821 // get dep list for module mod
822 check_dep ( mod, &head, &tail );
824 if ( head && tail ) {
827 // append module args
828 for ( i = 0; i < argc; i++ )
829 head->m_options = append_option( head->m_options, argv[i] );
832 // process tail ---> head
833 rc = mod_process ( tail, 1 );
841 static int mod_remove ( char *mod )
844 static struct mod_list_t rm_a_dummy = { "-a", NULL, NULL, NULL, NULL };
846 struct mod_list_t *head = 0;
847 struct mod_list_t *tail = 0;
850 check_dep ( mod, &head, &tail );
852 head = tail = &rm_a_dummy;
855 rc = mod_process ( head, 0 ); // process head ---> tail
862 int modprobe_main(int argc, char** argv)
864 int rc = EXIT_SUCCESS;
867 bb_opt_complementally = "?V-:q-v:v-q";
868 main_opts = bb_getopt_ulflags(argc, argv, "acdklnqrst:vVC:",
870 if((main_opts & (DUMP_CONF_EXIT | LIST_ALL)))
872 if((main_opts & (RESTRICT_DIR | CONFIG_FILE)))
873 bb_error_msg_and_die("-t and -C not supported");
875 depend = build_dep ( );
878 bb_error_msg_and_die ( "could not parse modules.dep\n" );
882 if (mod_remove ( optind < argc ?
883 argv [optind] : NULL )) {
884 bb_error_msg ("failed to remove module %s",
888 } while ( ++optind < argc );
891 bb_error_msg_and_die ( "No module or pattern provided\n" );
893 if ( mod_insert ( argv [optind], argc - optind - 1, argv + optind + 1 ))
894 bb_error_msg_and_die ( "failed to load module %s", argv [optind] );
897 /* Here would be a good place to free up memory allocated during the dependencies build. */