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 defined(CONFIG_FEATURE_2_6_MODULES)
279 if (( fd = open ( "/etc/modprobe.conf", O_RDONLY )) < 0 )
281 if (( fd = open ( "/etc/modules.conf", O_RDONLY )) < 0 )
282 if (( fd = open ( "/etc/conf.modules", O_RDONLY )) < 0 )
285 continuation_line = 0;
286 while ( reads ( fd, buffer, sizeof( buffer ))) {
290 p = strchr ( buffer, '#' );
294 l = bb_strlen ( buffer );
296 while ( l && isspace ( buffer [l-1] )) {
302 continuation_line = 0;
306 if ( !continuation_line ) {
307 if (( strncmp ( buffer, "alias", 5 ) == 0 ) && isspace ( buffer [5] )) {
310 if ( parse_tag_value ( buffer + 6, &alias, &mod )) {
311 // fprintf ( stderr, "ALIAS: '%s' -> '%s'\n", alias, mod );
314 first = current = (struct dep_t *) xcalloc ( 1, sizeof ( struct dep_t ));
317 current-> m_next = (struct dep_t *) xcalloc ( 1, sizeof ( struct dep_t ));
318 current = current-> m_next;
320 current-> m_module = bb_xstrdup ( alias );
321 current-> m_isalias = 1;
323 if (( strcmp ( mod, "off" ) == 0 ) || ( strcmp ( mod, "null" ) == 0 )) {
324 current-> m_depcnt = 0;
325 current-> m_deparr = 0;
328 current-> m_depcnt = 1;
329 current-> m_deparr = xmalloc ( 1 * sizeof( char * ));
330 current-> m_deparr[0] = bb_xstrdup ( mod );
332 current-> m_next = 0;
335 else if (( strncmp ( buffer, "options", 7 ) == 0 ) && isspace ( buffer [7] )) {
338 if ( parse_tag_value ( buffer + 8, &mod, &opt )) {
341 for ( dt = first; dt; dt = dt-> m_next ) {
342 if ( strcmp ( dt-> m_module, mod ) == 0 )
346 dt-> m_options = xrealloc ( dt-> m_options, bb_strlen( opt ) + 1 );
347 strcpy ( dt-> m_options, opt );
349 // fprintf ( stderr, "OPTION: '%s' -> '%s'\n", dt-> m_module, dt-> m_options );
360 /* check if /lib/modules/bar/foo.ko belongs to module foo */
361 /* return 1 = found, 0 = not found */
362 static int mod_strcmp ( const char *mod_path, const char *mod_name )
364 #if defined(CONFIG_FEATURE_2_6_MODULES)
365 #define MODULE_EXTENSION ".ko"
366 #define MOD_EXTENSION_LEN 3
368 #define MODULE_EXTENSION ".o"
369 #define MOD_EXTENSION_LEN 2
371 /* last path component */
372 const char *last_comp = strrchr (mod_path, '/');
374 return (strncmp(last_comp ? last_comp + 1 : mod_path,
376 strlen(mod_name)) == 0 ) &&
377 (strcmp(mod_path + strlen(mod_path) -
378 MOD_EXTENSION_LEN, MODULE_EXTENSION) == 0);
381 /* return 1 = loaded, 0 = not loaded, -1 = can't tell */
382 static int already_loaded (const char *name)
387 fd = open ("/proc/modules", O_RDONLY);
391 while ( reads ( fd, buffer, sizeof( buffer ))) {
394 p = strchr (buffer, ' ');
397 if (mod_strcmp (name, buffer)) {
408 static int mod_process ( struct mod_list_t *list, int do_insert )
416 if (already_loaded (list->m_module) != 1)
417 snprintf ( lcmd, sizeof( lcmd ) - 1, "insmod %s %s %s %s %s",
418 do_syslog ? "-s" : "", autoclean ? "-k" : "",
419 quiet ? "-q" : "", list-> m_module, list-> m_options ?
420 list-> m_options : "" );
422 if (already_loaded (list->m_module) != 0)
423 snprintf ( lcmd, sizeof( lcmd ) - 1, "rmmod %s %s",
424 do_syslog ? "-s" : "", list-> m_module );
428 printf ( "%s\n", lcmd );
429 if ( !show_only && *lcmd) {
430 int rc2 = system ( lcmd );
431 if (do_insert) rc = rc2; /* only last module matters */
432 else if (!rc2) rc = 0; /* success if remove any mod */
435 list = do_insert ? list-> m_prev : list-> m_next;
437 return (show_only) ? 0 : rc;
440 static void check_dep ( char *mod, struct mod_list_t **head, struct mod_list_t **tail )
442 struct mod_list_t *find;
447 // remove .o extension
448 lm = bb_strlen ( mod );
450 #if defined(CONFIG_FEATURE_2_6_MODULES)
451 if ((k_version > 4) && ( mod [lm-3] == '.' ) &&
452 ( mod [lm-2] == 'k' ) && ( mod [lm-1] == 'o' ))
456 if (( mod [lm-2] == '.' ) && ( mod [lm-1] == 'o' ))
459 // check dependencies
460 for ( dt = depend; dt; dt = dt-> m_next ) {
461 if ( mod_strcmp ( dt-> m_module, mod )) {
463 opt = dt-> m_options;
468 // resolve alias names
469 while ( dt && dt-> m_isalias ) {
470 if ( dt-> m_depcnt == 1 ) {
473 for ( adt = depend; adt; adt = adt-> m_next ) {
474 if ( strcmp ( adt-> m_module, dt-> m_deparr [0] ) == 0 )
481 opt = dt-> m_options;
490 // search for duplicates
491 for ( find = *head; find; find = find-> m_next ) {
492 if ( !strcmp ( mod, find-> m_module )) {
493 // found -> dequeue it
496 find-> m_prev-> m_next = find-> m_next;
498 *head = find-> m_next;
501 find-> m_next-> m_prev = find-> m_prev;
503 *tail = find-> m_prev;
505 break; // there can be only one duplicate
509 if ( !find ) { // did not find a duplicate
510 find = (struct mod_list_t *) xmalloc ( sizeof(struct mod_list_t));
511 find-> m_module = mod;
512 find-> m_options = opt;
517 (*tail)-> m_next = find;
518 find-> m_prev = *tail;
528 for ( i = 0; i < dt-> m_depcnt; i++ )
529 check_dep ( dt-> m_deparr [i], head, tail );
535 static int mod_insert ( char *mod, int argc, char **argv )
537 struct mod_list_t *tail = 0;
538 struct mod_list_t *head = 0;
541 // get dep list for module mod
542 check_dep ( mod, &head, &tail );
544 if ( head && tail ) {
549 // append module args
550 for ( i = 0; i < argc; i++ )
551 l += ( bb_strlen ( argv [i] ) + 1 );
553 head-> m_options = xrealloc ( head-> m_options, l + 1 );
554 head-> m_options [0] = 0;
556 for ( i = 0; i < argc; i++ ) {
557 strcat ( head-> m_options, argv [i] );
558 strcat ( head-> m_options, " " );
562 // process tail ---> head
563 rc = mod_process ( tail, 1 );
571 static int mod_remove ( char *mod )
574 static struct mod_list_t rm_a_dummy = { "-a", 0, 0 };
576 struct mod_list_t *head = 0;
577 struct mod_list_t *tail = 0;
580 check_dep ( mod, &head, &tail );
582 head = tail = &rm_a_dummy;
585 rc = mod_process ( head, 0 ); // process head ---> tail
594 extern int modprobe_main(int argc, char** argv)
599 autoclean = show_only = quiet = do_syslog = verbose = 0;
601 while ((opt = getopt(argc, argv, "acdklnqrst:vVC:")) != -1) {
603 case 'c': // no config used
604 case 'l': // no pattern matching
607 case 'C': // no config used
608 case 't': // no pattern matching
609 bb_error_msg_and_die("-t and -C not supported");
639 depend = build_dep ( );
642 bb_error_msg_and_die ( "could not parse modules.dep\n" );
645 int rc = EXIT_SUCCESS;
647 if (mod_remove ( optind < argc ?
648 bb_xstrdup (argv [optind]) : NULL )) {
649 bb_error_msg ("failed to remove module %s",
653 } while ( ++optind < argc );
659 bb_error_msg_and_die ( "No module or pattern provided\n" );
661 if ( mod_insert ( bb_xstrdup ( argv [optind] ), argc - optind - 1, argv + optind + 1 ))
662 bb_error_msg_and_die ( "failed to load module %s", argv [optind] );