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>
47 struct dep_t * m_next;
55 struct mod_list_t * m_prev;
56 struct mod_list_t * m_next;
60 static struct dep_t *depend;
61 static int autoclean, show_only, quiet, do_syslog, verbose;
64 int parse_tag_value ( char *buffer, char **ptag, char **pvalue )
68 while ( isspace ( *buffer ))
71 while ( !isspace ( *value ))
72 if (!*value) return 0;
75 while ( isspace ( *value ))
77 if (!*value) return 0;
85 /* Jump through hoops to simulate how fgets() grabs just one line at a
86 * time... Don't use any stdio since modprobe gets called from a kernel
87 * thread and stdio junk can overflow the limited stack...
89 static char *reads ( int fd, char *buffer, size_t len )
91 int n = read ( fd, buffer, len );
97 p = strchr ( buffer, '\n' );
102 offset = lseek ( fd, 0L, SEEK_CUR ); // Get the current file descriptor offset
103 lseek ( fd, offset-n + (p-buffer) + 1, SEEK_SET ); // Set the file descriptor offset to right after the \n
114 static struct dep_t *build_dep ( void )
118 struct dep_t *first = 0;
119 struct dep_t *current = 0;
121 char *filename = buffer;
122 int continuation_line = 0;
128 // check for buffer overflow in following code
129 if ( bb_strlen ( un.release ) > ( sizeof( buffer ) - 64 )) {
132 if (un.release[0] == '2') {
133 k_version = un.release[2] - '0';
136 strcpy ( filename, "/lib/modules/" );
137 strcat ( filename, un.release );
138 strcat ( filename, "/modules.dep" );
140 if (( fd = open ( filename, O_RDONLY )) < 0 ) {
142 /* Ok, that didn't work. Fall back to looking in /lib/modules */
143 if (( fd = open ( "/lib/modules/modules.dep", O_RDONLY )) < 0 ) {
148 while ( reads ( fd, buffer, sizeof( buffer ))) {
149 int l = bb_strlen ( buffer );
152 while ( isspace ( buffer [l-1] )) {
158 continuation_line = 0;
162 if ( !continuation_line ) {
163 char *col = strchr ( buffer, ':' );
172 mods = strrchr ( buffer, '/' );
179 modpath = strchr ( buffer, '/' );
182 #if defined(CONFIG_FEATURE_2_6_MODULES)
183 if ((k_version > 4) && ( *(col-3) == '.' ) &&
184 ( *(col-2) == 'k' ) && ( *(col-1) == 'o' ))
188 if (( *(col-2) == '.' ) && ( *(col-1) == 'o' ))
191 mod = bb_xstrndup ( mods, dot - mods );
194 first = current = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
197 current-> m_next = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
198 current = current-> m_next;
200 current-> m_name = mod;
201 current-> m_path = bb_xstrdup(modpath);
202 current-> m_options = 0;
203 current-> m_isalias = 0;
204 current-> m_depcnt = 0;
205 current-> m_deparr = 0;
206 current-> m_next = 0;
208 //printf ( "%s:\n", mod );
217 while ( p && *p && isblank(*p))
221 char *end = &buffer [l-1];
227 while ( isblank ( *end ) || ( *end == '\\' ))
232 next = strchr (p, ' ' );
241 deps = strrchr ( p, '/' );
243 if ( !deps || ( deps < p )) {
246 while ( isblank ( *deps ))
252 #if defined(CONFIG_FEATURE_2_6_MODULES)
253 if ((k_version > 4) && ( *(next-2) == '.' ) && *(next-1) == 'k' &&
258 if (( *(next-1) == '.' ) && ( *next == 'o' ))
261 /* Cope with blank lines */
262 if ((next-deps-ext+1) <= 0)
264 dep = bb_xstrndup ( deps, next - deps - ext + 1 );
266 current-> m_depcnt++;
267 current-> m_deparr = (char **) xrealloc ( current-> m_deparr,
268 sizeof ( char *) * current-> m_depcnt );
269 current-> m_deparr [current-> m_depcnt - 1] = dep;
271 //printf ( " %d) %s\n", current-> m_depcnt, current-> m_deparr [current-> m_depcnt -1] );
273 } while (next < end);
276 if ( buffer [l-1] == '\\' )
277 continuation_line = 1;
279 continuation_line = 0;
283 // alias parsing is not 100% correct (no correct handling of continuation lines within an alias) !
285 #if defined(CONFIG_FEATURE_2_6_MODULES)
286 if (( fd = open ( "/etc/modprobe.conf", O_RDONLY )) < 0 )
288 if (( fd = open ( "/etc/modules.conf", O_RDONLY )) < 0 )
289 if (( fd = open ( "/etc/conf.modules", O_RDONLY )) < 0 )
292 continuation_line = 0;
293 while ( reads ( fd, buffer, sizeof( buffer ))) {
297 p = strchr ( buffer, '#' );
301 l = bb_strlen ( buffer );
303 while ( l && isspace ( buffer [l-1] )) {
309 continuation_line = 0;
313 if ( !continuation_line ) {
314 if (( strncmp ( buffer, "alias", 5 ) == 0 ) && isspace ( buffer [5] )) {
317 if ( parse_tag_value ( buffer + 6, &alias, &mod )) {
318 // fprintf ( stderr, "ALIAS: '%s' -> '%s'\n", alias, mod );
321 first = current = (struct dep_t *) xcalloc ( 1, sizeof ( struct dep_t ));
324 current-> m_next = (struct dep_t *) xcalloc ( 1, sizeof ( struct dep_t ));
325 current = current-> m_next;
327 current-> m_name = bb_xstrdup ( alias );
328 current-> m_isalias = 1;
330 if (( strcmp ( mod, "off" ) == 0 ) || ( strcmp ( mod, "null" ) == 0 )) {
331 current-> m_depcnt = 0;
332 current-> m_deparr = 0;
335 current-> m_depcnt = 1;
336 current-> m_deparr = xmalloc ( 1 * sizeof( char * ));
337 current-> m_deparr[0] = bb_xstrdup ( mod );
339 current-> m_next = 0;
342 else if (( strncmp ( buffer, "options", 7 ) == 0 ) && isspace ( buffer [7] )) {
345 if ( parse_tag_value ( buffer + 8, &mod, &opt )) {
348 for ( dt = first; dt; dt = dt-> m_next ) {
349 if ( strcmp ( dt-> m_name, mod ) == 0 )
353 dt-> m_options = xrealloc ( dt-> m_options, bb_strlen( opt ) + 1 );
354 strcpy ( dt-> m_options, opt );
356 // fprintf ( stderr, "OPTION: '%s' -> '%s'\n", dt-> m_name, dt-> m_options );
367 /* return 1 = loaded, 0 = not loaded, -1 = can't tell */
368 static int already_loaded (const char *name)
373 fd = open ("/proc/modules", O_RDONLY);
377 while ( reads ( fd, buffer, sizeof( buffer ))) {
380 p = strchr (buffer, ' ');
383 if (strcmp (name, buffer) == 0) {
394 static int mod_process ( struct mod_list_t *list, int do_insert )
402 if (already_loaded (list->m_name) != 1)
403 snprintf ( lcmd, sizeof( lcmd ) - 1, "insmod %s %s %s %s %s",
404 do_syslog ? "-s" : "", autoclean ? "-k" : "",
405 quiet ? "-q" : "", list-> m_path, list-> m_options ?
406 list-> m_options : "" );
408 /* modutils uses short name for removal */
409 if (already_loaded (list->m_name) != 0)
410 snprintf ( lcmd, sizeof( lcmd ) - 1, "rmmod %s %s",
411 do_syslog ? "-s" : "", list-> m_name );
416 printf("%s\n", lcmd);
419 int rc2 = system(lcmd);
421 rc = rc2; /* only last module matters */
424 rc = 0; /* success if remove any mod */
428 list = do_insert ? list-> m_prev : list-> m_next;
430 return (show_only) ? 0 : rc;
433 static void check_dep ( char *mod, struct mod_list_t **head, struct mod_list_t **tail )
435 struct mod_list_t *find;
440 // check dependencies
441 for ( dt = depend; dt; dt = dt-> m_next ) {
442 if ( strcmp ( dt-> m_name, mod ) == 0) {
445 opt = dt-> m_options;
450 // resolve alias names
451 while ( dt && dt-> m_isalias ) {
452 if ( dt-> m_depcnt == 1 ) {
455 for ( adt = depend; adt; adt = adt-> m_next ) {
456 if ( strcmp ( adt-> m_name, dt-> m_deparr [0] ) == 0 )
464 opt = dt-> m_options;
473 // search for duplicates
474 for ( find = *head; find; find = find-> m_next ) {
475 if ( !strcmp ( mod, find-> m_name )) {
476 // found -> dequeue it
479 find-> m_prev-> m_next = find-> m_next;
481 *head = find-> m_next;
484 find-> m_next-> m_prev = find-> m_prev;
486 *tail = find-> m_prev;
488 break; // there can be only one duplicate
492 if ( !find ) { // did not find a duplicate
493 find = (struct mod_list_t *) xmalloc ( sizeof(struct mod_list_t));
495 find-> m_path = path;
496 find-> m_options = opt;
501 (*tail)-> m_next = find;
502 find-> m_prev = *tail;
512 for ( i = 0; i < dt-> m_depcnt; i++ )
513 check_dep ( dt-> m_deparr [i], head, tail );
519 static int mod_insert ( char *mod, int argc, char **argv )
521 struct mod_list_t *tail = 0;
522 struct mod_list_t *head = 0;
525 // get dep list for module mod
526 check_dep ( mod, &head, &tail );
528 if ( head && tail ) {
529 #if defined(CONFIG_FEATURE_2_6_MODULES)
534 // append module args
535 for ( i = 0; i < argc; i++ )
536 l += ( bb_strlen ( argv [i] ) + 1 );
538 head-> m_options = xrealloc ( head-> m_options, l + 1 );
539 head-> m_options [0] = 0;
541 for ( i = 0; i < argc; i++ ) {
542 strcat ( head-> m_options, argv [i] );
543 strcat ( head-> m_options, " " );
548 // process tail ---> head
549 rc = mod_process ( tail, 1 );
557 static int mod_remove ( char *mod )
560 static struct mod_list_t rm_a_dummy = { "-a", 0, 0 };
562 struct mod_list_t *head = 0;
563 struct mod_list_t *tail = 0;
566 check_dep ( mod, &head, &tail );
568 head = tail = &rm_a_dummy;
571 rc = mod_process ( head, 0 ); // process head ---> tail
578 extern int modprobe_main(int argc, char** argv)
583 autoclean = show_only = quiet = do_syslog = verbose = 0;
585 while ((opt = getopt(argc, argv, "acdklnqrst:vVC:")) != -1) {
587 case 'c': // no config used
588 case 'l': // no pattern matching
591 case 'C': // no config used
592 case 't': // no pattern matching
593 bb_error_msg_and_die("-t and -C not supported");
623 depend = build_dep ( );
626 bb_error_msg_and_die ( "could not parse modules.dep\n" );
629 int rc = EXIT_SUCCESS;
631 if (mod_remove ( optind < argc ?
632 bb_xstrdup (argv [optind]) : NULL )) {
633 bb_error_msg ("failed to remove module %s",
637 } while ( ++optind < argc );
643 bb_error_msg_and_die ( "No module or pattern provided\n" );
645 if ( mod_insert ( bb_xstrdup ( argv [optind] ), argc - optind - 1, argv + optind + 1 ))
646 bb_error_msg_and_die ( "failed to load module %s", argv [optind] );