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 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include <sys/utsname.h>
26 #include <sys/types.h>
50 struct dep_t * m_next;
58 struct mod_list_t * m_prev;
59 struct mod_list_t * m_next;
63 static struct dep_t *depend;
64 static int autoclean, show_only, quiet, do_syslog, verbose;
67 static int parse_tag_value ( char *buffer, char **ptag, char **pvalue )
71 while ( isspace ( *buffer ))
74 while ( !isspace ( *value ))
75 if (!*value) return 0;
78 while ( isspace ( *value ))
80 if (!*value) return 0;
88 /* Jump through hoops to simulate how fgets() grabs just one line at a
89 * time... Don't use any stdio since modprobe gets called from a kernel
90 * thread and stdio junk can overflow the limited stack...
92 static char *reads ( int fd, char *buffer, size_t len )
94 int n = read ( fd, buffer, len );
100 p = strchr ( buffer, '\n' );
105 offset = lseek ( fd, 0L, SEEK_CUR ); // Get the current file descriptor offset
106 lseek ( fd, offset-n + (p-buffer) + 1, SEEK_SET ); // Set the file descriptor offset to right after the \n
117 static struct dep_t *build_dep ( void )
121 struct dep_t *first = 0;
122 struct dep_t *current = 0;
124 char *filename = buffer;
125 int continuation_line = 0;
131 // check for buffer overflow in following code
132 if ( bb_strlen ( un.release ) > ( sizeof( buffer ) - 64 )) {
135 if (un.release[0] == '2') {
136 k_version = un.release[2] - '0';
139 strcpy ( filename, "/lib/modules/" );
140 strcat ( filename, un.release );
141 strcat ( filename, "/modules.dep" );
143 if (( fd = open ( filename, O_RDONLY )) < 0 ) {
145 /* Ok, that didn't work. Fall back to looking in /lib/modules */
146 if (( fd = open ( "/lib/modules/modules.dep", O_RDONLY )) < 0 ) {
151 while ( reads ( fd, buffer, sizeof( buffer ))) {
152 int l = bb_strlen ( buffer );
155 while ( isspace ( buffer [l-1] )) {
161 continuation_line = 0;
165 if ( !continuation_line ) {
166 char *col = strchr ( buffer, ':' );
175 mods = strrchr ( buffer, '/' );
182 modpath = strchr ( buffer, '/' );
185 #if defined(CONFIG_FEATURE_2_6_MODULES)
186 if ((k_version > 4) && ( *(col-3) == '.' ) &&
187 ( *(col-2) == 'k' ) && ( *(col-1) == 'o' ))
191 if (( *(col-2) == '.' ) && ( *(col-1) == 'o' ))
194 mod = bb_xstrndup ( mods, dot - mods );
197 first = current = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
200 current-> m_next = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
201 current = current-> m_next;
203 current-> m_name = mod;
204 current-> m_path = bb_xstrdup(modpath);
205 current-> m_options = 0;
206 current-> m_isalias = 0;
207 current-> m_depcnt = 0;
208 current-> m_deparr = 0;
209 current-> m_next = 0;
211 //printf ( "%s:\n", mod );
220 while ( p && *p && isblank(*p))
224 char *end = &buffer [l-1];
230 while ( isblank ( *end ) || ( *end == '\\' ))
235 next = strchr (p, ' ' );
244 deps = strrchr ( p, '/' );
246 if ( !deps || ( deps < p )) {
249 while ( isblank ( *deps ))
255 #if defined(CONFIG_FEATURE_2_6_MODULES)
256 if ((k_version > 4) && ( *(next-2) == '.' ) && *(next-1) == 'k' &&
261 if (( *(next-1) == '.' ) && ( *next == 'o' ))
264 /* Cope with blank lines */
265 if ((next-deps-ext+1) <= 0)
267 dep = bb_xstrndup ( deps, next - deps - ext + 1 );
269 current-> m_depcnt++;
270 current-> m_deparr = (char **) xrealloc ( current-> m_deparr,
271 sizeof ( char *) * current-> m_depcnt );
272 current-> m_deparr [current-> m_depcnt - 1] = dep;
274 //printf ( " %d) %s\n", current-> m_depcnt, current-> m_deparr [current-> m_depcnt -1] );
276 } while (next < end);
279 if ( buffer [l-1] == '\\' )
280 continuation_line = 1;
282 continuation_line = 0;
286 // alias parsing is not 100% correct (no correct handling of continuation lines within an alias) !
288 #if defined(CONFIG_FEATURE_2_6_MODULES)
289 if (( fd = open ( "/etc/modprobe.conf", O_RDONLY )) < 0 )
291 if (( fd = open ( "/etc/modules.conf", O_RDONLY )) < 0 )
292 if (( fd = open ( "/etc/conf.modules", O_RDONLY )) < 0 )
295 continuation_line = 0;
296 while ( reads ( fd, buffer, sizeof( buffer ))) {
300 p = strchr ( buffer, '#' );
304 l = bb_strlen ( buffer );
306 while ( l && isspace ( buffer [l-1] )) {
312 continuation_line = 0;
316 if ( !continuation_line ) {
317 if (( strncmp ( buffer, "alias", 5 ) == 0 ) && isspace ( buffer [5] )) {
320 if ( parse_tag_value ( buffer + 6, &alias, &mod )) {
321 // fprintf ( stderr, "ALIAS: '%s' -> '%s'\n", alias, mod );
324 first = current = (struct dep_t *) xcalloc ( 1, sizeof ( struct dep_t ));
327 current-> m_next = (struct dep_t *) xcalloc ( 1, sizeof ( struct dep_t ));
328 current = current-> m_next;
330 current-> m_name = bb_xstrdup ( alias );
331 current-> m_isalias = 1;
333 if (( strcmp ( mod, "off" ) == 0 ) || ( strcmp ( mod, "null" ) == 0 )) {
334 current-> m_depcnt = 0;
335 current-> m_deparr = 0;
338 current-> m_depcnt = 1;
339 current-> m_deparr = xmalloc ( 1 * sizeof( char * ));
340 current-> m_deparr[0] = bb_xstrdup ( mod );
342 current-> m_next = 0;
345 else if (( strncmp ( buffer, "options", 7 ) == 0 ) && isspace ( buffer [7] )) {
348 if ( parse_tag_value ( buffer + 8, &mod, &opt )) {
351 for ( dt = first; dt; dt = dt-> m_next ) {
352 if ( strcmp ( dt-> m_name, mod ) == 0 )
356 dt-> m_options = xrealloc ( dt-> m_options, bb_strlen( opt ) + 1 );
357 strcpy ( dt-> m_options, opt );
359 // fprintf ( stderr, "OPTION: '%s' -> '%s'\n", dt-> m_name, dt-> m_options );
370 /* return 1 = loaded, 0 = not loaded, -1 = can't tell */
371 static int already_loaded (const char *name)
376 fd = open ("/proc/modules", O_RDONLY);
380 while ( reads ( fd, buffer, sizeof( buffer ))) {
383 p = strchr (buffer, ' ');
386 if (strcmp (name, buffer) == 0) {
397 static int mod_process ( struct mod_list_t *list, int do_insert )
406 if (already_loaded (list->m_name) != 1) {
407 argv[argc++] = "insmod";
414 argv[argc++] = list-> m_path;
415 if (list-> m_options)
416 argv[argc++] = list-> m_options;
419 /* modutils uses short name for removal */
420 if (already_loaded (list->m_name) != 0) {
421 argv[argc++] = "rmmod";
424 argv[argc++] = list->m_name;
432 for (i=0; i<argc; i++)
433 printf("%s ", argv[i]);
444 execvp(argv[0], argv);
445 bb_perror_msg_and_die("exec of %s", argv[0]);
448 if (wait(&status) == -1) {
452 if (WIFEXITED(status))
453 rc2 = WEXITSTATUS(status);
454 if (WIFSIGNALED(status))
455 rc2 = WTERMSIG(status);
459 rc = rc2; /* only last module matters */
462 rc = 0; /* success if remove any mod */
466 list = do_insert ? list-> m_prev : list-> m_next;
468 return (show_only) ? 0 : rc;
471 static void check_dep ( char *mod, struct mod_list_t **head, struct mod_list_t **tail )
473 struct mod_list_t *find;
478 // check dependencies
479 for ( dt = depend; dt; dt = dt-> m_next ) {
480 if ( strcmp ( dt-> m_name, mod ) == 0) {
483 opt = dt-> m_options;
488 // resolve alias names
489 while ( dt && dt-> m_isalias ) {
490 if ( dt-> m_depcnt == 1 ) {
493 for ( adt = depend; adt; adt = adt-> m_next ) {
494 if ( strcmp ( adt-> m_name, dt-> m_deparr [0] ) == 0 )
502 opt = dt-> m_options;
512 bb_error_msg ("module %s not found.", mod);
516 // search for duplicates
517 for ( find = *head; find; find = find-> m_next ) {
518 if ( !strcmp ( mod, find-> m_name )) {
519 // found -> dequeue it
522 find-> m_prev-> m_next = find-> m_next;
524 *head = find-> m_next;
527 find-> m_next-> m_prev = find-> m_prev;
529 *tail = find-> m_prev;
531 break; // there can be only one duplicate
535 if ( !find ) { // did not find a duplicate
536 find = (struct mod_list_t *) xmalloc ( sizeof(struct mod_list_t));
538 find-> m_path = path;
539 find-> m_options = opt;
544 (*tail)-> m_next = find;
545 find-> m_prev = *tail;
555 for ( i = 0; i < dt-> m_depcnt; i++ )
556 check_dep ( dt-> m_deparr [i], head, tail );
562 static int mod_insert ( char *mod, int argc, char **argv )
564 struct mod_list_t *tail = 0;
565 struct mod_list_t *head = 0;
568 // get dep list for module mod
569 check_dep ( mod, &head, &tail );
571 if ( head && tail ) {
572 #if defined(CONFIG_FEATURE_2_6_MODULES)
577 // append module args
578 for ( i = 0; i < argc; i++ )
579 l += ( bb_strlen ( argv [i] ) + 1 );
581 head-> m_options = xrealloc ( head-> m_options, l + 1 );
582 head-> m_options [0] = 0;
584 for ( i = 0; i < argc; i++ ) {
585 strcat ( head-> m_options, argv [i] );
586 strcat ( head-> m_options, " " );
591 // process tail ---> head
592 rc = mod_process ( tail, 1 );
600 static int mod_remove ( char *mod )
603 static struct mod_list_t rm_a_dummy = { "-a", 0, 0 };
605 struct mod_list_t *head = 0;
606 struct mod_list_t *tail = 0;
609 check_dep ( mod, &head, &tail );
611 head = tail = &rm_a_dummy;
614 rc = mod_process ( head, 0 ); // process head ---> tail
621 extern int modprobe_main(int argc, char** argv)
626 autoclean = show_only = quiet = do_syslog = verbose = 0;
628 while ((opt = getopt(argc, argv, "acdklnqrst:vVC:")) != -1) {
630 case 'c': // no config used
631 case 'l': // no pattern matching
634 case 'C': // no config used
635 case 't': // no pattern matching
636 bb_error_msg_and_die("-t and -C not supported");
666 depend = build_dep ( );
669 bb_error_msg_and_die ( "could not parse modules.dep\n" );
672 int rc = EXIT_SUCCESS;
674 if (mod_remove ( optind < argc ?
675 bb_xstrdup (argv [optind]) : NULL )) {
676 bb_error_msg ("failed to remove module %s",
680 } while ( ++optind < argc );
686 bb_error_msg_and_die ( "No module or pattern provided\n" );
688 if ( mod_insert ( bb_xstrdup ( argv [optind] ), argc - optind - 1, argv + optind + 1 ))
689 bb_error_msg_and_die ( "failed to load module %s", argv [optind] );