1 /* vi: set sw=4 ts=4: */
3 * Modprobe written from scratch for BusyBox
4 * copyright (c) 2002 by Robert Griebl, griebl@gmx.de
8 #include <sys/utsname.h>
30 struct dep_t * m_next;
37 struct mod_list_t * m_prev;
38 struct mod_list_t * m_next;
42 static struct dep_t *depend;
43 static int autoclean, show_only, quiet, do_syslog, verbose;
45 int parse_tag_value ( char *buffer, char **ptag, char **pvalue )
49 while ( isspace ( *buffer ))
52 while ( !isspace ( *value ))
55 while ( isspace ( *value ))
61 return xstrlen( tag ) && xstrlen( value );
64 /* Jump through hoops to simulate how fgets() grabs just one line at a
65 * time... Don't use any stdio since modprobe gets called from a kernel
66 * thread and stdio junk can overflow the limited stack...
68 static char *reads ( int fd, char *buffer, size_t len )
70 int n = read ( fd, buffer, len );
76 p = strchr ( buffer, '\n' );
81 offset = lseek ( fd, 0L, SEEK_CUR ); // Get the current file descriptor offset
82 lseek ( fd, offset-n + (p-buffer) + 1, SEEK_SET ); // Set the file descriptor offset to right after the \n
93 static struct dep_t *build_dep ( void )
97 struct dep_t *first = 0;
98 struct dep_t *current = 0;
100 char *filename = buffer;
101 int continuation_line = 0;
106 // check for buffer overflow in following code
107 if ( xstrlen ( un.release ) > ( sizeof( buffer ) - 64 )) {
111 strcpy ( filename, "/lib/modules/" );
112 strcat ( filename, un.release );
113 strcat ( filename, "/modules.dep" );
115 if (( fd = open ( filename, O_RDONLY )) < 0 )
118 while ( reads ( fd, buffer, sizeof( buffer ))) {
119 int l = xstrlen ( buffer );
122 while ( isspace ( buffer [l-1] )) {
128 continuation_line = 0;
132 if ( !continuation_line ) {
133 char *col = strchr ( buffer, ':' );
141 mods = strrchr ( buffer, '/' );
148 if (( *(col-2) == '.' ) && ( *(col-1) == 'o' ))
151 mod = xstrndup ( mods, col - mods - ext );
154 first = current = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
157 current-> m_next = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
158 current = current-> m_next;
160 current-> m_module = mod;
161 current-> m_options = 0;
162 current-> m_isalias = 0;
163 current-> m_depcnt = 0;
164 current-> m_deparr = 0;
165 current-> m_next = 0;
167 //printf ( "%s:\n", mod );
178 char *end = &buffer [l-1];
179 char *deps = strrchr ( end, '/' );
183 while ( isblank ( *end ) || ( *end == '\\' ))
186 deps = strrchr ( p, '/' );
188 if ( !deps || ( deps < p )) {
191 while ( isblank ( *deps ))
197 if (( *(end-1) == '.' ) && ( *end == 'o' ))
200 /* Cope with blank lines */
201 if ((end-deps-ext+1) <= 0)
204 dep = xstrndup ( deps, end - deps - ext + 1 );
206 current-> m_depcnt++;
207 current-> m_deparr = (char **) xrealloc ( current-> m_deparr, sizeof ( char *) * current-> m_depcnt );
208 current-> m_deparr [current-> m_depcnt - 1] = dep;
210 //printf ( " %d) %s\n", current-> m_depcnt, current-> m_deparr [current-> m_depcnt -1] );
213 if ( buffer [l-1] == '\\' )
214 continuation_line = 1;
216 continuation_line = 0;
220 // alias parsing is not 100% correct (no correct handling of continuation lines within an alias) !
222 if (( fd = open ( "/etc/modules.conf", O_RDONLY )) < 0 )
223 if (( fd = open ( "/etc/conf.modules", O_RDONLY )) < 0 )
226 continuation_line = 0;
227 while ( reads ( fd, buffer, sizeof( buffer ))) {
231 p = strchr ( buffer, '#' );
235 l = xstrlen ( buffer );
237 while ( l && isspace ( buffer [l-1] )) {
243 continuation_line = 0;
247 if ( !continuation_line ) {
248 if (( strncmp ( buffer, "alias", 5 ) == 0 ) && isspace ( buffer [5] )) {
251 if ( parse_tag_value ( buffer + 6, &alias, &mod )) {
252 // fprintf ( stderr, "ALIAS: '%s' -> '%s'\n", alias, mod );
255 first = current = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
258 current-> m_next = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
259 current = current-> m_next;
261 current-> m_module = xstrdup ( alias );
262 current-> m_isalias = 1;
264 if (( strcmp ( alias, "off" ) == 0 ) || ( strcmp ( alias, "null" ) == 0 )) {
265 current-> m_depcnt = 0;
266 current-> m_deparr = 0;
269 current-> m_depcnt = 1;
270 current-> m_deparr = xmalloc ( 1 * sizeof( char * ));
271 current-> m_deparr[0] = xstrdup ( mod );
273 current-> m_next = 0;
276 else if (( strncmp ( buffer, "options", 7 ) == 0 ) && isspace ( buffer [7] )) {
279 if ( parse_tag_value ( buffer + 8, &mod, &opt )) {
282 for ( dt = first; dt; dt = dt-> m_next ) {
283 if ( strcmp ( dt-> m_module, mod ) == 0 )
287 dt-> m_options = xrealloc ( dt-> m_options, xstrlen( opt ) + 1 );
288 strcpy ( dt-> m_options, opt );
290 // fprintf ( stderr, "OPTION: '%s' -> '%s'\n", dt-> m_module, dt-> m_options );
302 static int mod_process ( struct mod_list_t *list, int do_insert )
312 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 : "" );
314 snprintf ( lcmd, sizeof( lcmd ) - 1, "rmmod %s %s 2>/dev/null", do_syslog ? "-s" : "", list-> m_module );
317 printf ( "%s\n", lcmd );
319 rc |= system ( lcmd );
321 list = do_insert ? list-> m_prev : list-> m_next;
326 static void check_dep ( char *mod, struct mod_list_t **head, struct mod_list_t **tail )
328 struct mod_list_t *find;
333 // remove .o extension
334 lm = xstrlen ( mod );
335 if (( mod [lm-2] == '.' ) && ( mod [lm-1] == 'o' ))
338 // check dependencies
339 for ( dt = depend; dt; dt = dt-> m_next ) {
340 if ( strcmp ( dt-> m_module, mod ) == 0 ) {
341 opt = dt-> m_options;
346 // resolve alias names
347 while ( dt && dt-> m_isalias ) {
348 if ( dt-> m_depcnt == 1 ) {
351 for ( adt = depend; adt; adt = adt-> m_next ) {
352 if ( strcmp ( adt-> m_module, dt-> m_deparr [0] ) == 0 ) {
354 opt = adt-> m_options;
367 // search for duplicates
368 for ( find = *head; find; find = find-> m_next ) {
369 if ( !strcmp ( mod, find-> m_module )) {
370 // found -> dequeue it
373 find-> m_prev-> m_next = find-> m_next;
375 *head = find-> m_next;
378 find-> m_next-> m_prev = find-> m_prev;
380 *tail = find-> m_prev;
382 break; // there can be only one duplicate
386 if ( !find ) { // did not find a duplicate
387 find = (struct mod_list_t *) xmalloc ( sizeof(struct mod_list_t));
388 find-> m_module = mod;
389 find-> m_options = opt;
394 (*tail)-> m_next = find;
395 find-> m_prev = *tail;
405 for ( i = 0; i < dt-> m_depcnt; i++ )
406 check_dep ( dt-> m_deparr [i], head, tail );
412 static int mod_insert ( char *mod, int argc, char **argv )
414 struct mod_list_t *tail = 0;
415 struct mod_list_t *head = 0;
418 // get dep list for module mod
419 check_dep ( mod, &head, &tail );
421 if ( head && tail ) {
426 // append module args
427 for ( i = 0; i < argc; i++ )
428 l += ( xstrlen ( argv [i] ) + 1 );
430 head-> m_options = xrealloc ( head-> m_options, l + 1 );
431 head-> m_options [0] = 0;
433 for ( i = 0; i < argc; i++ ) {
434 strcat ( head-> m_options, argv [i] );
435 strcat ( head-> m_options, " " );
439 // process tail ---> head
440 rc |= mod_process ( tail, 1 );
448 static void mod_remove ( char *mod )
450 static struct mod_list_t rm_a_dummy = { "-a", 0, 0 };
452 struct mod_list_t *head = 0;
453 struct mod_list_t *tail = 0;
456 check_dep ( mod, &head, &tail );
458 head = tail = &rm_a_dummy;
461 mod_process ( head, 0 ); // process head ---> tail
466 extern int modprobe_main(int argc, char** argv)
471 autoclean = show_only = quiet = do_syslog = verbose = 0;
473 while ((opt = getopt(argc, argv, "acdklnqrst:vVC:")) != -1) {
475 case 'c': // no config used
476 case 'l': // no pattern matching
479 case 'C': // no config used
480 case 't': // no pattern matching
481 error_msg_and_die("-t and -C not supported");
511 depend = build_dep ( );
514 error_msg_and_die ( "could not parse modules.dep\n" );
518 mod_remove ( optind < argc ? xstrdup ( argv [optind] ) : 0 );
519 } while ( ++optind < argc );
525 error_msg_and_die ( "No module or pattern provided\n" );
527 return mod_insert ( xstrdup ( argv [optind] ), argc - optind - 1, argv + optind + 1 ) ? \
528 EXIT_FAILURE : EXIT_SUCCESS;