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 ))
72 while ( isspace ( *value ))
78 return bb_strlen( tag ) && bb_strlen( value );
81 /* Jump through hoops to simulate how fgets() grabs just one line at a
82 * time... Don't use any stdio since modprobe gets called from a kernel
83 * thread and stdio junk can overflow the limited stack...
85 static char *reads ( int fd, char *buffer, size_t len )
87 int n = read ( fd, buffer, len );
93 p = strchr ( buffer, '\n' );
98 offset = lseek ( fd, 0L, SEEK_CUR ); // Get the current file descriptor offset
99 lseek ( fd, offset-n + (p-buffer) + 1, SEEK_SET ); // Set the file descriptor offset to right after the \n
110 static struct dep_t *build_dep ( void )
114 struct dep_t *first = 0;
115 struct dep_t *current = 0;
117 char *filename = buffer;
118 int continuation_line = 0;
124 // check for buffer overflow in following code
125 if ( bb_strlen ( un.release ) > ( sizeof( buffer ) - 64 )) {
128 if (un.release[0] == '2') {
129 k_version = un.release[2] - '0';
132 strcpy ( filename, "/lib/modules/" );
133 strcat ( filename, un.release );
134 strcat ( filename, "/modules.dep" );
136 if (( fd = open ( filename, O_RDONLY )) < 0 ) {
138 /* Ok, that didn't work. Fall back to looking in /lib/modules */
139 if (( fd = open ( "/lib/modules/modules.dep", O_RDONLY )) < 0 ) {
144 while ( reads ( fd, buffer, sizeof( buffer ))) {
145 int l = bb_strlen ( buffer );
148 while ( isspace ( buffer [l-1] )) {
154 continuation_line = 0;
158 if ( !continuation_line ) {
159 char *col = strchr ( buffer, ':' );
167 mods = strrchr ( buffer, '/' );
174 #if defined(CONFIG_FEATURE_2_6_MODULES)
175 if ((k_version > 4) && ( *(col-3) == '.' ) &&
176 ( *(col-2) == 'k' ) && ( *(col-1) == 'o' ))
180 if (( *(col-2) == '.' ) && ( *(col-1) == 'o' ))
183 mod = bb_xstrndup ( mods, col - mods - ext );
186 first = current = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
189 current-> m_next = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
190 current = current-> m_next;
192 current-> m_module = mod;
193 current-> m_options = 0;
194 current-> m_isalias = 0;
195 current-> m_depcnt = 0;
196 current-> m_deparr = 0;
197 current-> m_next = 0;
199 //printf ( "%s:\n", mod );
208 while ( p && *p && isblank(*p))
212 char *end = &buffer [l-1];
218 while ( isblank ( *end ) || ( *end == '\\' ))
223 next = strchr (p, ' ' );
232 deps = strrchr ( p, '/' );
234 if ( !deps || ( deps < p )) {
237 while ( isblank ( *deps ))
243 #if defined(CONFIG_FEATURE_2_6_MODULES)
244 if ((k_version > 4) && ( *(next-2) == '.' ) && *(next-1) == 'k' &&
249 if (( *(next-1) == '.' ) && ( *next == 'o' ))
252 /* Cope with blank lines */
253 if ((next-deps-ext+1) <= 0)
255 dep = bb_xstrndup ( deps, next - deps - ext + 1 );
257 current-> m_depcnt++;
258 current-> m_deparr = (char **) xrealloc ( current-> m_deparr, 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 (( fd = open ( "/etc/modules.conf", O_RDONLY )) < 0 )
276 if (( fd = open ( "/etc/conf.modules", O_RDONLY )) < 0 )
279 continuation_line = 0;
280 while ( reads ( fd, buffer, sizeof( buffer ))) {
284 p = strchr ( buffer, '#' );
288 l = bb_strlen ( buffer );
290 while ( l && isspace ( buffer [l-1] )) {
296 continuation_line = 0;
300 if ( !continuation_line ) {
301 if (( strncmp ( buffer, "alias", 5 ) == 0 ) && isspace ( buffer [5] )) {
304 if ( parse_tag_value ( buffer + 6, &alias, &mod )) {
305 // fprintf ( stderr, "ALIAS: '%s' -> '%s'\n", alias, mod );
308 first = current = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
311 current-> m_next = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
312 current = current-> m_next;
314 current-> m_module = bb_xstrdup ( alias );
315 current-> m_isalias = 1;
317 if (( strcmp ( alias, "off" ) == 0 ) || ( strcmp ( alias, "null" ) == 0 )) {
318 current-> m_depcnt = 0;
319 current-> m_deparr = 0;
322 current-> m_depcnt = 1;
323 current-> m_deparr = xmalloc ( 1 * sizeof( char * ));
324 current-> m_deparr[0] = bb_xstrdup ( mod );
326 current-> m_next = 0;
329 else if (( strncmp ( buffer, "options", 7 ) == 0 ) && isspace ( buffer [7] )) {
332 if ( parse_tag_value ( buffer + 8, &mod, &opt )) {
335 for ( dt = first; dt; dt = dt-> m_next ) {
336 if ( strcmp ( dt-> m_module, mod ) == 0 )
340 dt-> m_options = xrealloc ( dt-> m_options, bb_strlen( opt ) + 1 );
341 strcpy ( dt-> m_options, opt );
343 // fprintf ( stderr, "OPTION: '%s' -> '%s'\n", dt-> m_module, dt-> m_options );
354 /* return 1 = loaded, 0 = not loaded, -1 = can't tell */
355 static int already_loaded (const char *name)
360 fd = open ("/proc/modules", O_RDONLY);
364 while ( reads ( fd, buffer, sizeof( buffer ))) {
367 p = strchr (buffer, ' ');
370 if (strcmp (name, buffer) == 0) {
381 static int mod_process ( struct mod_list_t *list, int do_insert )
389 if (already_loaded (list->m_module) != 1)
390 snprintf ( lcmd, sizeof( lcmd ) - 1, "insmod %s %s %s %s %s", do_syslog ? "-s" : "", autoclean ? "-k" : "", quiet ? "-q" : "", list-> m_module, list-> m_options ? list-> m_options : "" );
392 if (already_loaded (list->m_module) != 0)
393 snprintf ( lcmd, sizeof( lcmd ) - 1, "rmmod %s %s", do_syslog ? "-s" : "", list-> m_module );
397 printf ( "%s\n", lcmd );
398 if ( !show_only && *lcmd) {
399 int rc2 = system ( lcmd );
400 if (do_insert) rc = rc2; /* only last module matters */
401 else if (!rc2) rc = 0; /* success if remove any mod */
404 list = do_insert ? list-> m_prev : list-> m_next;
406 return (show_only) ? 0 : rc;
409 static void check_dep ( char *mod, struct mod_list_t **head, struct mod_list_t **tail )
411 struct mod_list_t *find;
416 // remove .o extension
417 lm = bb_strlen ( mod );
419 #if defined(CONFIG_FEATURE_2_6_MODULES)
420 if ((k_version > 4) && ( mod [lm-3] == '.' ) &&
421 ( mod [lm-2] == 'k' ) && ( mod [lm-1] == 'o' ))
425 if (( mod [lm-2] == '.' ) && ( mod [lm-1] == 'o' ))
428 // check dependencies
429 for ( dt = depend; dt; dt = dt-> m_next ) {
430 if ( strcmp ( dt-> m_module, mod ) == 0 ) {
431 opt = dt-> m_options;
436 // resolve alias names
437 while ( dt && dt-> m_isalias ) {
438 if ( dt-> m_depcnt == 1 ) {
441 for ( adt = depend; adt; adt = adt-> m_next ) {
442 if ( strcmp ( adt-> m_module, dt-> m_deparr [0] ) == 0 )
449 opt = dt-> m_options;
458 // search for duplicates
459 for ( find = *head; find; find = find-> m_next ) {
460 if ( !strcmp ( mod, find-> m_module )) {
461 // found -> dequeue it
464 find-> m_prev-> m_next = find-> m_next;
466 *head = find-> m_next;
469 find-> m_next-> m_prev = find-> m_prev;
471 *tail = find-> m_prev;
473 break; // there can be only one duplicate
477 if ( !find ) { // did not find a duplicate
478 find = (struct mod_list_t *) xmalloc ( sizeof(struct mod_list_t));
479 find-> m_module = mod;
480 find-> m_options = opt;
485 (*tail)-> m_next = find;
486 find-> m_prev = *tail;
496 for ( i = 0; i < dt-> m_depcnt; i++ )
497 check_dep ( dt-> m_deparr [i], head, tail );
503 static int mod_insert ( char *mod, int argc, char **argv )
505 struct mod_list_t *tail = 0;
506 struct mod_list_t *head = 0;
509 // get dep list for module mod
510 check_dep ( mod, &head, &tail );
512 if ( head && tail ) {
517 // append module args
518 for ( i = 0; i < argc; i++ )
519 l += ( bb_strlen ( argv [i] ) + 1 );
521 head-> m_options = xrealloc ( head-> m_options, l + 1 );
522 head-> m_options [0] = 0;
524 for ( i = 0; i < argc; i++ ) {
525 strcat ( head-> m_options, argv [i] );
526 strcat ( head-> m_options, " " );
530 // process tail ---> head
531 rc = mod_process ( tail, 1 );
539 static int mod_remove ( char *mod )
542 static struct mod_list_t rm_a_dummy = { "-a", 0, 0 };
544 struct mod_list_t *head = 0;
545 struct mod_list_t *tail = 0;
548 check_dep ( mod, &head, &tail );
550 head = tail = &rm_a_dummy;
553 rc = mod_process ( head, 0 ); // process head ---> tail
562 extern int modprobe_main(int argc, char** argv)
567 autoclean = show_only = quiet = do_syslog = verbose = 0;
569 while ((opt = getopt(argc, argv, "acdklnqrst:vVC:")) != -1) {
571 case 'c': // no config used
572 case 'l': // no pattern matching
575 case 'C': // no config used
576 case 't': // no pattern matching
577 bb_error_msg_and_die("-t and -C not supported");
607 depend = build_dep ( );
610 bb_error_msg_and_die ( "could not parse modules.dep\n" );
613 int rc = EXIT_SUCCESS;
615 if (mod_remove ( optind < argc ?
616 bb_xstrdup (argv [optind]) : NULL )) {
617 bb_error_msg ("failed to remove module %s",
621 } while ( ++optind < argc );
627 bb_error_msg_and_die ( "No module or pattern provided\n" );
629 if ( mod_insert ( bb_xstrdup ( argv [optind] ), argc - optind - 1, argv + optind + 1 ))
630 bb_error_msg_and_die ( "failed to load module %s", argv [optind] );