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 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
12 #include <sys/utsname.h>
13 #include <sys/types.h>
37 struct dep_t * m_next;
45 struct mod_list_t * m_prev;
46 struct mod_list_t * m_next;
50 static struct dep_t *depend;
51 static int autoclean, show_only, quiet, do_syslog, verbose;
54 static int parse_tag_value ( char *buffer, char **ptag, char **pvalue )
58 while ( isspace ( *buffer ))
61 while ( !isspace ( *value ))
62 if (!*value) return 0;
65 while ( isspace ( *value ))
67 if (!*value) return 0;
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...
79 static char *reads ( int fd, char *buffer, size_t len )
81 int n = read ( fd, buffer, len );
87 p = strchr ( buffer, '\n' );
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
104 static struct dep_t *build_dep ( void )
108 struct dep_t *first = 0;
109 struct dep_t *current = 0;
111 char *filename = buffer;
112 int continuation_line = 0;
118 // check for buffer overflow in following code
119 if ( bb_strlen ( un.release ) > ( sizeof( buffer ) - 64 )) {
122 if (un.release[0] == '2') {
123 k_version = un.release[2] - '0';
126 strcpy ( filename, "/lib/modules/" );
127 strcat ( filename, un.release );
128 strcat ( filename, "/modules.dep" );
130 if (( fd = open ( filename, O_RDONLY )) < 0 ) {
132 /* Ok, that didn't work. Fall back to looking in /lib/modules */
133 if (( fd = open ( "/lib/modules/modules.dep", O_RDONLY )) < 0 ) {
138 while ( reads ( fd, buffer, sizeof( buffer ))) {
139 int l = bb_strlen ( buffer );
142 while ( isspace ( buffer [l-1] )) {
148 continuation_line = 0;
152 if ( !continuation_line ) {
153 char *col = strchr ( buffer, ':' );
162 mods = strrchr ( buffer, '/' );
169 modpath = strchr ( buffer, '/' );
172 #if defined(CONFIG_FEATURE_2_6_MODULES)
173 if ((k_version > 4) && ( *(col-3) == '.' ) &&
174 ( *(col-2) == 'k' ) && ( *(col-1) == 'o' ))
178 if (( *(col-2) == '.' ) && ( *(col-1) == 'o' ))
181 mod = bb_xstrndup ( mods, dot - mods );
184 first = current = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
187 current-> m_next = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
188 current = current-> m_next;
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;
198 //printf ( "%s:\n", mod );
207 while ( p && *p && isblank(*p))
211 char *end = &buffer [l-1];
217 while ( isblank ( *end ) || ( *end == '\\' ))
222 next = strchr (p, ' ' );
231 deps = strrchr ( p, '/' );
233 if ( !deps || ( deps < p )) {
236 while ( isblank ( *deps ))
242 #if defined(CONFIG_FEATURE_2_6_MODULES)
243 if ((k_version > 4) && ( *(next-2) == '.' ) && *(next-1) == 'k' &&
248 if (( *(next-1) == '.' ) && ( *next == 'o' ))
251 /* Cope with blank lines */
252 if ((next-deps-ext+1) <= 0)
254 dep = bb_xstrndup ( deps, next - deps - ext + 1 );
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;
261 //printf ( " %d) %s\n", current-> m_depcnt, current-> m_deparr [current-> m_depcnt -1] );
263 } while (next < end);
266 if ( buffer [l-1] == '\\' )
267 continuation_line = 1;
269 continuation_line = 0;
273 // alias parsing is not 100% correct (no correct handling of continuation lines within an alias) !
275 #if defined(CONFIG_FEATURE_2_6_MODULES)
276 if (( fd = open ( "/etc/modprobe.conf", O_RDONLY )) < 0 )
278 if (( fd = open ( "/etc/modules.conf", O_RDONLY )) < 0 )
279 if (( fd = open ( "/etc/conf.modules", O_RDONLY )) < 0 )
282 continuation_line = 0;
283 while ( reads ( fd, buffer, sizeof( buffer ))) {
287 p = strchr ( buffer, '#' );
291 l = bb_strlen ( buffer );
293 while ( l && isspace ( buffer [l-1] )) {
299 continuation_line = 0;
303 if ( !continuation_line ) {
304 if (( strncmp ( buffer, "alias", 5 ) == 0 ) && isspace ( buffer [5] )) {
307 if ( parse_tag_value ( buffer + 6, &alias, &mod )) {
308 // fprintf ( stderr, "ALIAS: '%s' -> '%s'\n", alias, mod );
311 first = current = (struct dep_t *) xcalloc ( 1, sizeof ( struct dep_t ));
314 current-> m_next = (struct dep_t *) xcalloc ( 1, sizeof ( struct dep_t ));
315 current = current-> m_next;
317 current-> m_name = bb_xstrdup ( alias );
318 current-> m_isalias = 1;
320 if (( strcmp ( mod, "off" ) == 0 ) || ( strcmp ( mod, "null" ) == 0 )) {
321 current-> m_depcnt = 0;
322 current-> m_deparr = 0;
325 current-> m_depcnt = 1;
326 current-> m_deparr = xmalloc ( 1 * sizeof( char * ));
327 current-> m_deparr[0] = bb_xstrdup ( mod );
329 current-> m_next = 0;
332 else if (( strncmp ( buffer, "options", 7 ) == 0 ) && isspace ( buffer [7] )) {
335 if ( parse_tag_value ( buffer + 8, &mod, &opt )) {
338 for ( dt = first; dt; dt = dt-> m_next ) {
339 if ( strcmp ( dt-> m_name, mod ) == 0 )
343 dt-> m_options = xrealloc ( dt-> m_options, bb_strlen( opt ) + 1 );
344 strcpy ( dt-> m_options, opt );
346 // fprintf ( stderr, "OPTION: '%s' -> '%s'\n", dt-> m_name, dt-> m_options );
357 /* return 1 = loaded, 0 = not loaded, -1 = can't tell */
358 static int already_loaded (const char *name)
363 fd = open ("/proc/modules", O_RDONLY);
367 while ( reads ( fd, buffer, sizeof( buffer ))) {
370 p = strchr (buffer, ' ');
373 if (strcmp (name, buffer) == 0) {
384 #ifdef CONFIG_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!
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 )
398 int opt_status = ARG_EMPTY;
401 /* Dumb you, I have nothing to do... */
402 if( src == NULL ) return src;
404 /* Skip leading spaces */
405 while( *src == ' ' ) {
408 /* Is the end of string reached? */
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 ) ) ) ) {
422 if( opt_status & ARG_IN_DQUOTES ) {
423 /* Already in double quotes, keep current char as is */
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. */
434 if( opt_status & ARG_IN_SQUOTES ) {
435 /* Already in single quotes, keep current char as is */
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. */
446 if( opt_status & ARG_IN_SQUOTES ) {
447 /* Between single quotes: keep as is. */
449 switch( *(tmp_str+1) ) {
458 /* We escaped a special character. For now, keep
459 * both the back-slash and the following char. */
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 ) );
471 /* Any other char that is special shall appear here.
472 * Example: $ starts a variable
474 do_variable_expansion();
478 /* any other char is kept as is. */
481 tmp_str++; /* Go to next char */
482 src++; /* Go to next char to find the end of the argument. */
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 );
491 #endif /* CONFIG_MODPROBE_MULTIPLE_OPTIONS */
493 static int mod_process ( struct mod_list_t *list, int do_insert )
496 #ifdef CONFIG_MODPROBE_MULTIPLE_OPTIONS
499 #ifdef CONFIG_FEATURE_CLEAN_UP
502 #else /* CONFIG_MODPROBE_MULTIPLE_OPTIONS */
509 #ifdef CONFIG_MODPROBE_MULTIPLE_OPTIONS
510 #ifdef CONFIG_FEATURE_CLEAN_UP
513 argv = (char**) malloc( 6 * sizeof( char* ) ); /* enough for minimal insmod (5 args + NULL) or rmmod (3 args + NULL) */
516 if (already_loaded (list->m_name) != 1) {
517 argv[argc++] = "insmod";
524 else if(verbose) /* verbose and quiet are mutually exclusive */
526 argv[argc++] = list-> m_path;
527 #ifdef CONFIG_MODPROBE_MULTIPLE_OPTIONS
528 #ifdef CONFIG_FEATURE_CLEAN_UP
531 opts = list-> m_options;
532 while( ( opts = parse_command_string( opts, &(argv[argc]) ) ) != NULL ) {
533 /* Increase the argv array by 1 */
535 argv = (char**) xrealloc( argv, ( argc + 1 ) * sizeof( char* ) );
537 #else /* CONFIG_MODPROBE_MULTIPLE_OPTIONS */
538 if (list-> m_options)
539 argv[argc++] = list-> m_options;
540 #endif /* CONFIG_MODPROBE_MULTIPLE_OPTIONS */
543 /* modutils uses short name for removal */
544 if (already_loaded (list->m_name) != 0) {
545 argv[argc++] = "rmmod";
548 argv[argc++] = list->m_name;
549 #if ( defined CONFIG_MODPROBE_MULTIPLE_OPTIONS ) && ( defined CONFIG_FEATURE_CLEAN_UP )
559 printf("argc=%d\n", argc );
560 for (i=0; i<argc; i++)
561 printf("argv[%02d]=\"%s\"\n", i, argv[i]);
571 execvp(argv[0], argv);
572 bb_perror_msg_and_die("exec of %s", argv[0]);
575 if (wait(&status) == -1) {
579 if (WIFEXITED(status))
580 rc2 = WEXITSTATUS(status);
581 if (WIFSIGNALED(status))
582 rc2 = WTERMSIG(status);
586 rc = rc2; /* only last module matters */
589 rc = 0; /* success if remove any mod */
592 #if ( defined CONFIG_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++] );
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
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? */
610 #else /* CONFIG_MODPROBE_MULTIPLE_OPTIONS && CONFIG_FEATURE_CLEAN_UP */
613 list = do_insert ? list-> m_prev : list-> m_next;
615 return (show_only) ? 0 : rc;
618 static void check_dep ( char *mod, struct mod_list_t **head, struct mod_list_t **tail )
620 struct mod_list_t *find;
625 // check dependencies
626 for ( dt = depend; dt; dt = dt-> m_next ) {
627 if ( strcmp ( dt-> m_name, mod ) == 0) {
630 opt = dt-> m_options;
635 // resolve alias names
636 while ( dt && dt-> m_isalias ) {
637 if ( dt-> m_depcnt == 1 ) {
640 for ( adt = depend; adt; adt = adt-> m_next ) {
641 if ( strcmp ( adt-> m_name, dt-> m_deparr [0] ) == 0 )
649 opt = dt-> m_options;
659 bb_error_msg ("module %s not found.", mod);
663 // search for duplicates
664 for ( find = *head; find; find = find-> m_next ) {
665 if ( !strcmp ( mod, find-> m_name )) {
666 // found -> dequeue it
669 find-> m_prev-> m_next = find-> m_next;
671 *head = find-> m_next;
674 find-> m_next-> m_prev = find-> m_prev;
676 *tail = find-> m_prev;
678 break; // there can be only one duplicate
682 if ( !find ) { // did not find a duplicate
683 find = (struct mod_list_t *) xmalloc ( sizeof(struct mod_list_t));
685 find-> m_path = path;
686 find-> m_options = opt;
691 (*tail)-> m_next = find;
692 find-> m_prev = *tail;
702 for ( i = 0; i < dt-> m_depcnt; i++ )
703 check_dep ( dt-> m_deparr [i], head, tail );
707 static int mod_insert ( char *mod, int argc, char **argv )
709 struct mod_list_t *tail = 0;
710 struct mod_list_t *head = 0;
713 // get dep list for module mod
714 check_dep ( mod, &head, &tail );
716 if ( head && tail ) {
717 #if defined(CONFIG_FEATURE_2_6_MODULES)
722 // append module args
723 for ( i = 0; i < argc; i++ )
724 l += ( bb_strlen ( argv [i] ) + 1 );
726 head-> m_options = xrealloc ( head-> m_options, l + 1 );
727 head-> m_options [0] = 0;
729 for ( i = 0; i < argc; i++ ) {
730 strcat ( head-> m_options, argv [i] );
731 strcat ( head-> m_options, " " );
736 // process tail ---> head
737 rc = mod_process ( tail, 1 );
745 static int mod_remove ( char *mod )
748 static struct mod_list_t rm_a_dummy = { "-a", 0, 0 };
750 struct mod_list_t *head = 0;
751 struct mod_list_t *tail = 0;
754 check_dep ( mod, &head, &tail );
756 head = tail = &rm_a_dummy;
759 rc = mod_process ( head, 0 ); // process head ---> tail
766 extern int modprobe_main(int argc, char** argv)
771 autoclean = show_only = quiet = do_syslog = verbose = 0;
773 while ((opt = getopt(argc, argv, "acdklnqrst:vVC:")) != -1) {
775 case 'c': // no config used
776 case 'l': // no pattern matching
779 case 'C': // no config used
780 case 't': // no pattern matching
781 bb_error_msg_and_die("-t and -C not supported");
811 depend = build_dep ( );
814 bb_error_msg_and_die ( "could not parse modules.dep\n" );
817 int rc = EXIT_SUCCESS;
819 if (mod_remove ( optind < argc ?
820 bb_xstrdup (argv [optind]) : NULL )) {
821 bb_error_msg ("failed to remove module %s",
825 } while ( ++optind < argc );
831 bb_error_msg_and_die ( "No module or pattern provided\n" );
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] );