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
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 #include <sys/utsname.h>
46 struct dep_t * m_next;
53 struct mod_list_t * m_prev;
54 struct mod_list_t * m_next;
58 static struct dep_t *depend;
59 static int autoclean, show_only, quiet, do_syslog, verbose;
62 int parse_tag_value ( char *buffer, char **ptag, char **pvalue )
66 while ( isspace ( *buffer ))
69 while ( !isspace ( *value ))
70 if (!*value) return 0;
73 while ( isspace ( *value ))
75 if (!*value) return 0;
83 /* Jump through hoops to simulate how fgets() grabs just one line at a
84 * time... Don't use any stdio since modprobe gets called from a kernel
85 * thread and stdio junk can overflow the limited stack...
87 static char *reads ( int fd, char *buffer, size_t len )
89 int n = read ( fd, buffer, len );
95 p = strchr ( buffer, '\n' );
100 offset = lseek ( fd, 0L, SEEK_CUR ); // Get the current file descriptor offset
101 lseek ( fd, offset-n + (p-buffer) + 1, SEEK_SET ); // Set the file descriptor offset to right after the \n
112 static struct dep_t *build_dep ( void )
116 struct dep_t *first = 0;
117 struct dep_t *current = 0;
119 char *filename = buffer;
120 int continuation_line = 0;
126 // check for buffer overflow in following code
127 if ( bb_strlen ( un.release ) > ( sizeof( buffer ) - 64 )) {
130 if (un.release[0] == '2') {
131 k_version = un.release[2] - '0';
134 strcpy ( filename, "/lib/modules/" );
135 strcat ( filename, un.release );
136 strcat ( filename, "/modules.dep" );
138 if (( fd = open ( filename, O_RDONLY )) < 0 ) {
140 /* Ok, that didn't work. Fall back to looking in /lib/modules */
141 if (( fd = open ( "/lib/modules/modules.dep", O_RDONLY )) < 0 ) {
146 while ( reads ( fd, buffer, sizeof( buffer ))) {
147 int l = bb_strlen ( buffer );
150 while ( isspace ( buffer [l-1] )) {
156 continuation_line = 0;
160 if ( !continuation_line ) {
161 char *col = strchr ( buffer, ':' );
169 mods = strrchr ( buffer, '/' );
176 #if defined(CONFIG_FEATURE_2_6_MODULES)
177 if ((k_version > 4) && ( *(col-3) == '.' ) &&
178 ( *(col-2) == 'k' ) && ( *(col-1) == 'o' ))
182 if (( *(col-2) == '.' ) && ( *(col-1) == 'o' ))
185 mod = bb_xstrndup ( buffer, col - buffer );
188 first = current = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
191 current-> m_next = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
192 current = current-> m_next;
194 current-> m_module = mod;
195 current-> m_options = 0;
196 current-> m_isalias = 0;
197 current-> m_depcnt = 0;
198 current-> m_deparr = 0;
199 current-> m_next = 0;
201 //printf ( "%s:\n", mod );
210 while ( p && *p && isblank(*p))
214 char *end = &buffer [l-1];
220 while ( isblank ( *end ) || ( *end == '\\' ))
225 next = strchr (p, ' ' );
234 deps = strrchr ( p, '/' );
236 if ( !deps || ( deps < p )) {
239 while ( isblank ( *deps ))
245 #if defined(CONFIG_FEATURE_2_6_MODULES)
246 if ((k_version > 4) && ( *(next-2) == '.' ) && *(next-1) == 'k' &&
251 if (( *(next-1) == '.' ) && ( *next == 'o' ))
254 /* Cope with blank lines */
255 if ((next-deps-ext+1) <= 0)
257 dep = bb_xstrndup ( deps, next - deps - ext + 1 );
259 current-> m_depcnt++;
260 current-> m_deparr = (char **) xrealloc ( current-> m_deparr,
261 sizeof ( char *) * current-> m_depcnt );
262 current-> m_deparr [current-> m_depcnt - 1] = dep;
264 //printf ( " %d) %s\n", current-> m_depcnt, current-> m_deparr [current-> m_depcnt -1] );
266 } while (next < end);
269 if ( buffer [l-1] == '\\' )
270 continuation_line = 1;
272 continuation_line = 0;
276 // alias parsing is not 100% correct (no correct handling of continuation lines within an alias) !
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_module = 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_module, 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_module, dt-> m_options );
357 /* check if /lib/modules/bar/foo.ko belongs to module foo */
358 /* return 1 = found, 0 = not found */
359 static int mod_strcmp ( const char *mod_path, const char *mod_name )
361 #if defined(CONFIG_FEATURE_2_6_MODULES)
362 #define MODULE_EXTENSION ".ko"
363 #define MOD_EXTENSION_LEN 3
365 #define MODULE_EXTENSION ".o"
366 #define MOD_EXTENSION_LEN 2
368 if ((strstr (mod_path, mod_name) ==
369 (mod_path + strlen(mod_path) -
370 strlen(mod_name) - MOD_EXTENSION_LEN))
371 && (!strcmp(mod_path + strlen(mod_path) -
372 MOD_EXTENSION_LEN, MODULE_EXTENSION)))
379 /* return 1 = loaded, 0 = not loaded, -1 = can't tell */
380 static int already_loaded (const char *name)
385 fd = open ("/proc/modules", O_RDONLY);
389 while ( reads ( fd, buffer, sizeof( buffer ))) {
392 p = strchr (buffer, ' ');
395 if (mod_strcmp (name, buffer)) {
406 static int mod_process ( struct mod_list_t *list, int do_insert )
414 if (already_loaded (list->m_module) != 1)
415 snprintf ( lcmd, sizeof( lcmd ) - 1, "insmod %s %s %s %s %s",
416 do_syslog ? "-s" : "", autoclean ? "-k" : "",
417 quiet ? "-q" : "", list-> m_module, list-> m_options ?
418 list-> m_options : "" );
420 if (already_loaded (list->m_module) != 0)
421 snprintf ( lcmd, sizeof( lcmd ) - 1, "rmmod %s %s",
422 do_syslog ? "-s" : "", list-> m_module );
426 printf ( "%s\n", lcmd );
427 if ( !show_only && *lcmd) {
428 int rc2 = system ( lcmd );
429 if (do_insert) rc = rc2; /* only last module matters */
430 else if (!rc2) rc = 0; /* success if remove any mod */
433 list = do_insert ? list-> m_prev : list-> m_next;
435 return (show_only) ? 0 : rc;
438 static void check_dep ( char *mod, struct mod_list_t **head, struct mod_list_t **tail )
440 struct mod_list_t *find;
445 // remove .o extension
446 lm = bb_strlen ( mod );
448 #if defined(CONFIG_FEATURE_2_6_MODULES)
449 if ((k_version > 4) && ( mod [lm-3] == '.' ) &&
450 ( mod [lm-2] == 'k' ) && ( mod [lm-1] == 'o' ))
454 if (( mod [lm-2] == '.' ) && ( mod [lm-1] == 'o' ))
457 // check dependencies
458 for ( dt = depend; dt; dt = dt-> m_next ) {
459 if ( mod_strcmp ( dt-> m_module, mod )) {
461 opt = dt-> m_options;
466 // resolve alias names
467 while ( dt && dt-> m_isalias ) {
468 if ( dt-> m_depcnt == 1 ) {
471 for ( adt = depend; adt; adt = adt-> m_next ) {
472 if ( strcmp ( adt-> m_module, dt-> m_deparr [0] ) == 0 )
479 opt = dt-> m_options;
488 // search for duplicates
489 for ( find = *head; find; find = find-> m_next ) {
490 if ( !strcmp ( mod, find-> m_module )) {
491 // found -> dequeue it
494 find-> m_prev-> m_next = find-> m_next;
496 *head = find-> m_next;
499 find-> m_next-> m_prev = find-> m_prev;
501 *tail = find-> m_prev;
503 break; // there can be only one duplicate
507 if ( !find ) { // did not find a duplicate
508 find = (struct mod_list_t *) xmalloc ( sizeof(struct mod_list_t));
509 find-> m_module = mod;
510 find-> m_options = opt;
515 (*tail)-> m_next = find;
516 find-> m_prev = *tail;
526 for ( i = 0; i < dt-> m_depcnt; i++ )
527 check_dep ( dt-> m_deparr [i], head, tail );
533 static int mod_insert ( char *mod, int argc, char **argv )
535 struct mod_list_t *tail = 0;
536 struct mod_list_t *head = 0;
539 // get dep list for module mod
540 check_dep ( mod, &head, &tail );
542 if ( head && tail ) {
547 // append module args
548 for ( i = 0; i < argc; i++ )
549 l += ( bb_strlen ( argv [i] ) + 1 );
551 head-> m_options = xrealloc ( head-> m_options, l + 1 );
552 head-> m_options [0] = 0;
554 for ( i = 0; i < argc; i++ ) {
555 strcat ( head-> m_options, argv [i] );
556 strcat ( head-> m_options, " " );
560 // process tail ---> head
561 rc = mod_process ( tail, 1 );
569 static int mod_remove ( char *mod )
572 static struct mod_list_t rm_a_dummy = { "-a", 0, 0 };
574 struct mod_list_t *head = 0;
575 struct mod_list_t *tail = 0;
578 check_dep ( mod, &head, &tail );
580 head = tail = &rm_a_dummy;
583 rc = mod_process ( head, 0 ); // process head ---> tail
592 extern int modprobe_main(int argc, char** argv)
597 autoclean = show_only = quiet = do_syslog = verbose = 0;
599 while ((opt = getopt(argc, argv, "acdklnqrst:vVC:")) != -1) {
601 case 'c': // no config used
602 case 'l': // no pattern matching
605 case 'C': // no config used
606 case 't': // no pattern matching
607 bb_error_msg_and_die("-t and -C not supported");
637 depend = build_dep ( );
640 bb_error_msg_and_die ( "could not parse modules.dep\n" );
643 int rc = EXIT_SUCCESS;
645 if (mod_remove ( optind < argc ?
646 bb_xstrdup (argv [optind]) : NULL )) {
647 bb_error_msg ("failed to remove module %s",
651 } while ( ++optind < argc );
657 bb_error_msg_and_die ( "No module or pattern provided\n" );
659 if ( mod_insert ( bb_xstrdup ( argv [optind] ), argc - optind - 1, argv + optind + 1 ))
660 bb_error_msg_and_die ( "failed to load module %s", argv [optind] );