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;
61 int parse_tag_value ( char *buffer, char **ptag, char **pvalue )
65 while ( isspace ( *buffer ))
68 while ( !isspace ( *value ))
71 while ( isspace ( *value ))
77 return bb_strlen( tag ) && bb_strlen( value );
80 /* Jump through hoops to simulate how fgets() grabs just one line at a
81 * time... Don't use any stdio since modprobe gets called from a kernel
82 * thread and stdio junk can overflow the limited stack...
84 static char *reads ( int fd, char *buffer, size_t len )
86 int n = read ( fd, buffer, len );
92 p = strchr ( buffer, '\n' );
97 offset = lseek ( fd, 0L, SEEK_CUR ); // Get the current file descriptor offset
98 lseek ( fd, offset-n + (p-buffer) + 1, SEEK_SET ); // Set the file descriptor offset to right after the \n
109 static struct dep_t *build_dep ( void )
113 struct dep_t *first = 0;
114 struct dep_t *current = 0;
116 char *filename = buffer;
117 int continuation_line = 0;
122 // check for buffer overflow in following code
123 if ( bb_strlen ( un.release ) > ( sizeof( buffer ) - 64 )) {
127 strcpy ( filename, "/lib/modules/" );
128 strcat ( filename, un.release );
129 strcat ( filename, "/modules.dep" );
131 if (( fd = open ( filename, O_RDONLY )) < 0 ) {
133 /* Ok, that didn't work. Fall back to looking in /lib/modules */
134 if (( fd = open ( "/lib/modules/modules.dep", O_RDONLY )) < 0 ) {
139 while ( reads ( fd, buffer, sizeof( buffer ))) {
140 int l = bb_strlen ( buffer );
143 while ( isspace ( buffer [l-1] )) {
149 continuation_line = 0;
153 if ( !continuation_line ) {
154 char *col = strchr ( buffer, ':' );
162 mods = strrchr ( buffer, '/' );
169 if (( *(col-2) == '.' ) && ( *(col-1) == 'o' ))
172 mod = bb_xstrndup ( mods, col - mods - ext );
175 first = current = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
178 current-> m_next = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
179 current = current-> m_next;
181 current-> m_module = mod;
182 current-> m_options = 0;
183 current-> m_isalias = 0;
184 current-> m_depcnt = 0;
185 current-> m_deparr = 0;
186 current-> m_next = 0;
188 //printf ( "%s:\n", mod );
199 char *end = &buffer [l-1];
200 char *deps = strrchr ( end, '/' );
204 while ( isblank ( *end ) || ( *end == '\\' ))
207 deps = strrchr ( p, '/' );
209 if ( !deps || ( deps < p )) {
212 while ( isblank ( *deps ))
218 if (( *(end-1) == '.' ) && ( *end == 'o' ))
221 /* Cope with blank lines */
222 if ((end-deps-ext+1) <= 0)
225 dep = bb_xstrndup ( deps, end - deps - ext + 1 );
227 current-> m_depcnt++;
228 current-> m_deparr = (char **) xrealloc ( current-> m_deparr, sizeof ( char *) * current-> m_depcnt );
229 current-> m_deparr [current-> m_depcnt - 1] = dep;
231 //printf ( " %d) %s\n", current-> m_depcnt, current-> m_deparr [current-> m_depcnt -1] );
234 if ( buffer [l-1] == '\\' )
235 continuation_line = 1;
237 continuation_line = 0;
241 // alias parsing is not 100% correct (no correct handling of continuation lines within an alias) !
243 if (( fd = open ( "/etc/modules.conf", O_RDONLY )) < 0 )
244 if (( fd = open ( "/etc/conf.modules", O_RDONLY )) < 0 )
247 continuation_line = 0;
248 while ( reads ( fd, buffer, sizeof( buffer ))) {
252 p = strchr ( buffer, '#' );
256 l = bb_strlen ( buffer );
258 while ( l && isspace ( buffer [l-1] )) {
264 continuation_line = 0;
268 if ( !continuation_line ) {
269 if (( strncmp ( buffer, "alias", 5 ) == 0 ) && isspace ( buffer [5] )) {
272 if ( parse_tag_value ( buffer + 6, &alias, &mod )) {
273 // fprintf ( stderr, "ALIAS: '%s' -> '%s'\n", alias, mod );
276 first = current = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
279 current-> m_next = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
280 current = current-> m_next;
282 current-> m_module = bb_xstrdup ( alias );
283 current-> m_isalias = 1;
285 if (( strcmp ( alias, "off" ) == 0 ) || ( strcmp ( alias, "null" ) == 0 )) {
286 current-> m_depcnt = 0;
287 current-> m_deparr = 0;
290 current-> m_depcnt = 1;
291 current-> m_deparr = xmalloc ( 1 * sizeof( char * ));
292 current-> m_deparr[0] = bb_xstrdup ( mod );
294 current-> m_next = 0;
297 else if (( strncmp ( buffer, "options", 7 ) == 0 ) && isspace ( buffer [7] )) {
300 if ( parse_tag_value ( buffer + 8, &mod, &opt )) {
303 for ( dt = first; dt; dt = dt-> m_next ) {
304 if ( strcmp ( dt-> m_module, mod ) == 0 )
308 dt-> m_options = xrealloc ( dt-> m_options, bb_strlen( opt ) + 1 );
309 strcpy ( dt-> m_options, opt );
311 // fprintf ( stderr, "OPTION: '%s' -> '%s'\n", dt-> m_module, dt-> m_options );
322 /* return 1 = loaded, 0 = not loaded, -1 = can't tell */
323 static int already_loaded (const char *name)
328 fd = open ("/proc/modules", O_RDONLY);
332 while ( reads ( fd, buffer, sizeof( buffer ))) {
335 p = strchr (buffer, ' ');
338 if (strcmp (name, buffer) == 0) {
349 static int mod_process ( struct mod_list_t *list, int do_insert )
357 if (already_loaded (list->m_module) != 1)
358 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 : "" );
360 if (already_loaded (list->m_module) != 0)
361 snprintf ( lcmd, sizeof( lcmd ) - 1, "rmmod %s %s", do_syslog ? "-s" : "", list-> m_module );
365 printf ( "%s\n", lcmd );
366 if ( !show_only && *lcmd) {
367 int rc2 = system ( lcmd );
368 if (do_insert) rc = rc2; /* only last module matters */
369 else if (!rc2) rc = 0; /* success if remove any mod */
372 list = do_insert ? list-> m_prev : list-> m_next;
374 return (show_only) ? 0 : rc;
377 static void check_dep ( char *mod, struct mod_list_t **head, struct mod_list_t **tail )
379 struct mod_list_t *find;
384 // remove .o extension
385 lm = bb_strlen ( mod );
386 if (( mod [lm-2] == '.' ) && ( mod [lm-1] == 'o' ))
389 // check dependencies
390 for ( dt = depend; dt; dt = dt-> m_next ) {
391 if ( strcmp ( dt-> m_module, mod ) == 0 ) {
392 opt = dt-> m_options;
397 // resolve alias names
398 while ( dt && dt-> m_isalias ) {
399 if ( dt-> m_depcnt == 1 ) {
402 for ( adt = depend; adt; adt = adt-> m_next ) {
403 if ( strcmp ( adt-> m_module, dt-> m_deparr [0] ) == 0 )
410 opt = dt-> m_options;
419 // search for duplicates
420 for ( find = *head; find; find = find-> m_next ) {
421 if ( !strcmp ( mod, find-> m_module )) {
422 // found -> dequeue it
425 find-> m_prev-> m_next = find-> m_next;
427 *head = find-> m_next;
430 find-> m_next-> m_prev = find-> m_prev;
432 *tail = find-> m_prev;
434 break; // there can be only one duplicate
438 if ( !find ) { // did not find a duplicate
439 find = (struct mod_list_t *) xmalloc ( sizeof(struct mod_list_t));
440 find-> m_module = mod;
441 find-> m_options = opt;
446 (*tail)-> m_next = find;
447 find-> m_prev = *tail;
457 for ( i = 0; i < dt-> m_depcnt; i++ )
458 check_dep ( dt-> m_deparr [i], head, tail );
464 static int mod_insert ( char *mod, int argc, char **argv )
466 struct mod_list_t *tail = 0;
467 struct mod_list_t *head = 0;
470 // get dep list for module mod
471 check_dep ( mod, &head, &tail );
473 if ( head && tail ) {
478 // append module args
479 for ( i = 0; i < argc; i++ )
480 l += ( bb_strlen ( argv [i] ) + 1 );
482 head-> m_options = xrealloc ( head-> m_options, l + 1 );
483 head-> m_options [0] = 0;
485 for ( i = 0; i < argc; i++ ) {
486 strcat ( head-> m_options, argv [i] );
487 strcat ( head-> m_options, " " );
491 // process tail ---> head
492 rc = mod_process ( tail, 1 );
500 static int mod_remove ( char *mod )
503 static struct mod_list_t rm_a_dummy = { "-a", 0, 0 };
505 struct mod_list_t *head = 0;
506 struct mod_list_t *tail = 0;
509 check_dep ( mod, &head, &tail );
511 head = tail = &rm_a_dummy;
514 rc = mod_process ( head, 0 ); // process head ---> tail
523 extern int modprobe_main(int argc, char** argv)
528 autoclean = show_only = quiet = do_syslog = verbose = 0;
530 while ((opt = getopt(argc, argv, "acdklnqrst:vVC:")) != -1) {
532 case 'c': // no config used
533 case 'l': // no pattern matching
536 case 'C': // no config used
537 case 't': // no pattern matching
538 bb_error_msg_and_die("-t and -C not supported");
568 depend = build_dep ( );
571 bb_error_msg_and_die ( "could not parse modules.dep\n" );
574 int rc = EXIT_SUCCESS;
576 if (mod_remove ( optind < argc ?
577 bb_xstrdup (argv [optind]) : NULL )) {
578 bb_error_msg ("failed to remove module %s",
582 } while ( ++optind < argc );
588 bb_error_msg_and_die ( "No module or pattern provided\n" );
590 if ( mod_insert ( bb_xstrdup ( argv [optind] ), argc - optind - 1, argv + optind + 1 ))
591 bb_error_msg_and_die ( "failed to load module %s", argv [optind] );