1 /* vi: set sw=4 ts=4: */
3 * Modprobe written from scratch for BusyBox
5 * Copyright (c) 2002 by Robert Griebl, griebl@gmx.de
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 #include <sys/utsname.h>
45 struct dep_t * m_next;
52 struct mod_list_t * m_prev;
53 struct mod_list_t * m_next;
57 static struct dep_t *depend;
58 static int autoclean, show_only, quiet, do_syslog, verbose;
60 int parse_tag_value ( char *buffer, char **ptag, char **pvalue )
64 while ( isspace ( *buffer ))
67 while ( !isspace ( *value ))
70 while ( isspace ( *value ))
76 return xstrlen( tag ) && xstrlen( value );
79 /* Jump through hoops to simulate how fgets() grabs just one line at a
80 * time... Don't use any stdio since modprobe gets called from a kernel
81 * thread and stdio junk can overflow the limited stack...
83 static char *reads ( int fd, char *buffer, size_t len )
85 int n = read ( fd, buffer, len );
91 p = strchr ( buffer, '\n' );
96 offset = lseek ( fd, 0L, SEEK_CUR ); // Get the current file descriptor offset
97 lseek ( fd, offset-n + (p-buffer) + 1, SEEK_SET ); // Set the file descriptor offset to right after the \n
108 static struct dep_t *build_dep ( void )
112 struct dep_t *first = 0;
113 struct dep_t *current = 0;
115 char *filename = buffer;
116 int continuation_line = 0;
121 // check for buffer overflow in following code
122 if ( xstrlen ( un.release ) > ( sizeof( buffer ) - 64 )) {
126 strcpy ( filename, "/lib/modules/" );
127 strcat ( filename, un.release );
128 strcat ( filename, "/modules.dep" );
130 if (( fd = open ( filename, O_RDONLY )) < 0 )
133 while ( reads ( fd, buffer, sizeof( buffer ))) {
134 int l = xstrlen ( buffer );
137 while ( isspace ( buffer [l-1] )) {
143 continuation_line = 0;
147 if ( !continuation_line ) {
148 char *col = strchr ( buffer, ':' );
156 mods = strrchr ( buffer, '/' );
163 if (( *(col-2) == '.' ) && ( *(col-1) == 'o' ))
166 mod = xstrndup ( mods, col - mods - ext );
169 first = current = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
172 current-> m_next = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
173 current = current-> m_next;
175 current-> m_module = mod;
176 current-> m_options = 0;
177 current-> m_isalias = 0;
178 current-> m_depcnt = 0;
179 current-> m_deparr = 0;
180 current-> m_next = 0;
182 //printf ( "%s:\n", mod );
193 char *end = &buffer [l-1];
194 char *deps = strrchr ( end, '/' );
198 while ( isblank ( *end ) || ( *end == '\\' ))
201 deps = strrchr ( p, '/' );
203 if ( !deps || ( deps < p )) {
206 while ( isblank ( *deps ))
212 if (( *(end-1) == '.' ) && ( *end == 'o' ))
215 /* Cope with blank lines */
216 if ((end-deps-ext+1) <= 0)
219 dep = xstrndup ( deps, end - deps - ext + 1 );
221 current-> m_depcnt++;
222 current-> m_deparr = (char **) xrealloc ( current-> m_deparr, sizeof ( char *) * current-> m_depcnt );
223 current-> m_deparr [current-> m_depcnt - 1] = dep;
225 //printf ( " %d) %s\n", current-> m_depcnt, current-> m_deparr [current-> m_depcnt -1] );
228 if ( buffer [l-1] == '\\' )
229 continuation_line = 1;
231 continuation_line = 0;
235 // alias parsing is not 100% correct (no correct handling of continuation lines within an alias) !
237 if (( fd = open ( "/etc/modules.conf", O_RDONLY )) < 0 )
238 if (( fd = open ( "/etc/conf.modules", O_RDONLY )) < 0 )
241 continuation_line = 0;
242 while ( reads ( fd, buffer, sizeof( buffer ))) {
246 p = strchr ( buffer, '#' );
250 l = xstrlen ( buffer );
252 while ( l && isspace ( buffer [l-1] )) {
258 continuation_line = 0;
262 if ( !continuation_line ) {
263 if (( strncmp ( buffer, "alias", 5 ) == 0 ) && isspace ( buffer [5] )) {
266 if ( parse_tag_value ( buffer + 6, &alias, &mod )) {
267 // fprintf ( stderr, "ALIAS: '%s' -> '%s'\n", alias, mod );
270 first = current = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
273 current-> m_next = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
274 current = current-> m_next;
276 current-> m_module = xstrdup ( alias );
277 current-> m_isalias = 1;
279 if (( strcmp ( alias, "off" ) == 0 ) || ( strcmp ( alias, "null" ) == 0 )) {
280 current-> m_depcnt = 0;
281 current-> m_deparr = 0;
284 current-> m_depcnt = 1;
285 current-> m_deparr = xmalloc ( 1 * sizeof( char * ));
286 current-> m_deparr[0] = xstrdup ( mod );
288 current-> m_next = 0;
291 else if (( strncmp ( buffer, "options", 7 ) == 0 ) && isspace ( buffer [7] )) {
294 if ( parse_tag_value ( buffer + 8, &mod, &opt )) {
297 for ( dt = first; dt; dt = dt-> m_next ) {
298 if ( strcmp ( dt-> m_module, mod ) == 0 )
302 dt-> m_options = xrealloc ( dt-> m_options, xstrlen( opt ) + 1 );
303 strcpy ( dt-> m_options, opt );
305 // fprintf ( stderr, "OPTION: '%s' -> '%s'\n", dt-> m_module, dt-> m_options );
317 static int mod_process ( struct mod_list_t *list, int do_insert )
327 snprintf ( lcmd, sizeof( lcmd ) - 1, "insmod %s %s %s %s %s 2>/dev/null", do_syslog ? "-s" : "", autoclean ? "-k" : "", quiet ? "-q" : "", list-> m_module, list-> m_options ? list-> m_options : "" );
329 snprintf ( lcmd, sizeof( lcmd ) - 1, "rmmod %s %s 2>/dev/null", do_syslog ? "-s" : "", list-> m_module );
332 printf ( "%s\n", lcmd );
334 rc |= system ( lcmd );
336 list = do_insert ? list-> m_prev : list-> m_next;
341 static void check_dep ( char *mod, struct mod_list_t **head, struct mod_list_t **tail )
343 struct mod_list_t *find;
348 // remove .o extension
349 lm = xstrlen ( mod );
350 if (( mod [lm-2] == '.' ) && ( mod [lm-1] == 'o' ))
353 // check dependencies
354 for ( dt = depend; dt; dt = dt-> m_next ) {
355 if ( strcmp ( dt-> m_module, mod ) == 0 ) {
356 opt = dt-> m_options;
361 // resolve alias names
362 while ( dt && dt-> m_isalias ) {
363 if ( dt-> m_depcnt == 1 ) {
366 for ( adt = depend; adt; adt = adt-> m_next ) {
367 if ( strcmp ( adt-> m_module, dt-> m_deparr [0] ) == 0 )
374 opt = dt-> m_options;
383 // search for duplicates
384 for ( find = *head; find; find = find-> m_next ) {
385 if ( !strcmp ( mod, find-> m_module )) {
386 // found -> dequeue it
389 find-> m_prev-> m_next = find-> m_next;
391 *head = find-> m_next;
394 find-> m_next-> m_prev = find-> m_prev;
396 *tail = find-> m_prev;
398 break; // there can be only one duplicate
402 if ( !find ) { // did not find a duplicate
403 find = (struct mod_list_t *) xmalloc ( sizeof(struct mod_list_t));
404 find-> m_module = mod;
405 find-> m_options = opt;
410 (*tail)-> m_next = find;
411 find-> m_prev = *tail;
421 for ( i = 0; i < dt-> m_depcnt; i++ )
422 check_dep ( dt-> m_deparr [i], head, tail );
428 static int mod_insert ( char *mod, int argc, char **argv )
430 struct mod_list_t *tail = 0;
431 struct mod_list_t *head = 0;
434 // get dep list for module mod
435 check_dep ( mod, &head, &tail );
437 if ( head && tail ) {
442 // append module args
443 for ( i = 0; i < argc; i++ )
444 l += ( xstrlen ( argv [i] ) + 1 );
446 head-> m_options = xrealloc ( head-> m_options, l + 1 );
447 head-> m_options [0] = 0;
449 for ( i = 0; i < argc; i++ ) {
450 strcat ( head-> m_options, argv [i] );
451 strcat ( head-> m_options, " " );
455 // process tail ---> head
456 rc |= mod_process ( tail, 1 );
464 static void mod_remove ( char *mod )
466 static struct mod_list_t rm_a_dummy = { "-a", 0, 0 };
468 struct mod_list_t *head = 0;
469 struct mod_list_t *tail = 0;
472 check_dep ( mod, &head, &tail );
474 head = tail = &rm_a_dummy;
477 mod_process ( head, 0 ); // process head ---> tail
482 extern int modprobe_main(int argc, char** argv)
487 autoclean = show_only = quiet = do_syslog = verbose = 0;
489 while ((opt = getopt(argc, argv, "acdklnqrst:vVC:")) != -1) {
491 case 'c': // no config used
492 case 'l': // no pattern matching
495 case 'C': // no config used
496 case 't': // no pattern matching
497 error_msg_and_die("-t and -C not supported");
527 depend = build_dep ( );
530 error_msg_and_die ( "could not parse modules.dep\n" );
534 mod_remove ( optind < argc ? xstrdup ( argv [optind] ) : NULL );
535 } while ( ++optind < argc );
541 error_msg_and_die ( "No module or pattern provided\n" );
543 return mod_insert ( xstrdup ( argv [optind] ), argc - optind - 1, argv + optind + 1 ) ? \
544 EXIT_FAILURE : EXIT_SUCCESS;