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 /* last path component */
365 const char *last_comp = strrchr (mod_path, '/');
366 const char *mod_ext = ".o";
368 #if defined(CONFIG_FEATURE_2_6_MODULES)
373 last_comp = last_comp ? last_comp + 1 : mod_path;
375 return (strncmp(last_comp,
377 strlen(mod_name)) == 0 ) &&
378 ((strcmp(last_comp + strlen (mod_name), mod_ext) == 0) || last_comp[strlen(mod_name)] == 0) &&
379 (strcmp(mod_path + strlen(mod_path) -
380 strlen(mod_ext), mod_ext) == 0);
383 /* return 1 = loaded, 0 = not loaded, -1 = can't tell */
384 static int already_loaded (const char *name)
389 fd = open ("/proc/modules", O_RDONLY);
393 while ( reads ( fd, buffer, sizeof( buffer ))) {
396 p = strchr (buffer, ' ');
399 if (mod_strcmp (name, buffer)) {
410 static int mod_process ( struct mod_list_t *list, int do_insert )
418 if (already_loaded (list->m_module) != 1)
419 snprintf ( lcmd, sizeof( lcmd ) - 1, "insmod %s %s %s %s %s",
420 do_syslog ? "-s" : "", autoclean ? "-k" : "",
421 quiet ? "-q" : "", list-> m_module, list-> m_options ?
422 list-> m_options : "" );
424 if (already_loaded (list->m_module) != 0)
425 snprintf ( lcmd, sizeof( lcmd ) - 1, "rmmod %s %s",
426 do_syslog ? "-s" : "", list-> m_module );
431 printf("%s\n", lcmd);
434 int rc2 = system(lcmd);
436 rc = rc2; /* only last module matters */
439 rc = 0; /* success if remove any mod */
443 list = do_insert ? list-> m_prev : list-> m_next;
445 return (show_only) ? 0 : rc;
448 static void check_dep ( char *mod, struct mod_list_t **head, struct mod_list_t **tail )
450 struct mod_list_t *find;
455 // remove .o extension
456 lm = bb_strlen ( mod );
458 #if defined(CONFIG_FEATURE_2_6_MODULES)
459 if ((k_version > 4) && ( mod [lm-3] == '.' ) &&
460 ( mod [lm-2] == 'k' ) && ( mod [lm-1] == 'o' ))
464 if (( mod [lm-2] == '.' ) && ( mod [lm-1] == 'o' ))
467 // check dependencies
468 for ( dt = depend; dt; dt = dt-> m_next ) {
469 if ( mod_strcmp ( dt-> m_module, mod )) {
471 opt = dt-> m_options;
476 // resolve alias names
477 while ( dt && dt-> m_isalias ) {
478 if ( dt-> m_depcnt == 1 ) {
481 for ( adt = depend; adt; adt = adt-> m_next ) {
482 if ( strcmp ( adt-> m_module, dt-> m_deparr [0] ) == 0 )
489 opt = dt-> m_options;
498 // search for duplicates
499 for ( find = *head; find; find = find-> m_next ) {
500 if ( !strcmp ( mod, find-> m_module )) {
501 // found -> dequeue it
504 find-> m_prev-> m_next = find-> m_next;
506 *head = find-> m_next;
509 find-> m_next-> m_prev = find-> m_prev;
511 *tail = find-> m_prev;
513 break; // there can be only one duplicate
517 if ( !find ) { // did not find a duplicate
518 find = (struct mod_list_t *) xmalloc ( sizeof(struct mod_list_t));
519 find-> m_module = mod;
520 find-> m_options = opt;
525 (*tail)-> m_next = find;
526 find-> m_prev = *tail;
536 for ( i = 0; i < dt-> m_depcnt; i++ )
537 check_dep ( dt-> m_deparr [i], head, tail );
543 static int mod_insert ( char *mod, int argc, char **argv )
545 struct mod_list_t *tail = 0;
546 struct mod_list_t *head = 0;
549 // get dep list for module mod
550 check_dep ( mod, &head, &tail );
552 if ( head && tail ) {
557 // append module args
558 for ( i = 0; i < argc; i++ )
559 l += ( bb_strlen ( argv [i] ) + 1 );
561 head-> m_options = xrealloc ( head-> m_options, l + 1 );
562 head-> m_options [0] = 0;
564 for ( i = 0; i < argc; i++ ) {
565 strcat ( head-> m_options, argv [i] );
566 strcat ( head-> m_options, " " );
570 // process tail ---> head
571 rc = mod_process ( tail, 1 );
579 static int mod_remove ( char *mod )
582 static struct mod_list_t rm_a_dummy = { "-a", 0, 0 };
584 struct mod_list_t *head = 0;
585 struct mod_list_t *tail = 0;
588 check_dep ( mod, &head, &tail );
590 head = tail = &rm_a_dummy;
593 rc = mod_process ( head, 0 ); // process head ---> tail
602 extern int modprobe_main(int argc, char** argv)
607 autoclean = show_only = quiet = do_syslog = verbose = 0;
609 while ((opt = getopt(argc, argv, "acdklnqrst:vVC:")) != -1) {
611 case 'c': // no config used
612 case 'l': // no pattern matching
615 case 'C': // no config used
616 case 't': // no pattern matching
617 bb_error_msg_and_die("-t and -C not supported");
647 depend = build_dep ( );
650 bb_error_msg_and_die ( "could not parse modules.dep\n" );
653 int rc = EXIT_SUCCESS;
655 if (mod_remove ( optind < argc ?
656 bb_xstrdup (argv [optind]) : NULL )) {
657 bb_error_msg ("failed to remove module %s",
661 } while ( ++optind < argc );
667 bb_error_msg_and_die ( "No module or pattern provided\n" );
669 if ( mod_insert ( bb_xstrdup ( argv [optind] ), argc - optind - 1, argv + optind + 1 ))
670 bb_error_msg_and_die ( "failed to load module %s", argv [optind] );