1 /* vi: set sw=4 ts=4: */
3 * really dumb modprobe implementation for busybox
4 * Copyright (C) 2001 Lineo, davidm@lineo.com
6 * dependency specific stuff completly rewritten and
7 * copyright (c) 2002 by Robert Griebl, griebl@gmx.de
11 #include <sys/utsname.h>
32 struct dep_t * m_next;
38 struct mod_list_t * m_prev;
39 struct mod_list_t * m_next;
43 static struct dep_t *depend;
44 static int autoclean, show_only, quiet, do_syslog, verbose;
47 static struct dep_t *build_dep ( void )
51 struct dep_t *first = 0;
52 struct dep_t *current = 0;
54 char *filename = buffer;
55 int continuation_line = 0;
60 // check for buffer overflow in following code
61 if ( xstrlen ( un. release ) > ( sizeof( buffer ) - 64 ))
64 strcpy ( filename, "/lib/modules/" );
65 strcat ( filename, un. release );
66 strcat ( filename, "/modules.dep" );
68 f = fopen ( filename, "r" );
72 while ( fgets ( buffer, sizeof( buffer), f )) {
73 int l = xstrlen ( buffer );
76 while ( isspace ( buffer [l-1] )) {
82 continuation_line = 0;
86 if ( !continuation_line ) {
87 char *col = strchr ( buffer, ':' );
95 mods = strrchr ( buffer, '/' );
102 if (( *(col-2) == '.' ) && ( *(col-1) == 'o' ))
105 mod = xstrndup ( mods, col - mods - ext );
108 first = current = (struct dep_t *) malloc ( sizeof ( struct dep_t ));
111 current-> m_next = (struct dep_t *) malloc ( sizeof ( struct dep_t ));
112 current = current-> m_next;
114 current-> m_module = mod;
115 current-> m_isalias = 0;
116 current-> m_depcnt = 0;
117 current-> m_deparr = 0;
118 current-> m_next = 0;
120 //printf ( "%s:\n", mod );
131 char *end = &buffer [l-1];
132 char *deps = strrchr ( end, '/' );
136 while ( isblank ( *end ) || ( *end == '\\' ))
139 deps = strrchr ( p, '/' );
141 if ( !deps || ( deps < p )) {
144 while ( isblank ( *deps ))
150 if (( *(end-1) == '.' ) && ( *end == 'o' ))
153 dep = xstrndup ( deps, end - deps - ext + 1 );
155 current-> m_depcnt++;
156 current-> m_deparr = (char **) xrealloc ( current-> m_deparr, sizeof ( char *) * current-> m_depcnt );
157 current-> m_deparr [current-> m_depcnt - 1] = dep;
159 //printf ( " %d) %s\n", current-> m_depcnt, current-> m_deparr [current-> m_depcnt -1] );
162 if ( buffer [l-1] == '\\' )
163 continuation_line = 1;
165 continuation_line = 0;
169 // alias parsing is not 100% correct (no correct handling of continuation lines within an alias) !
171 f = fopen ( "/etc/modules.conf", "r" );
173 f = fopen ( "/etc/conf.modules", "r" );
175 continuation_line = 0;
177 while ( fgets ( buffer, sizeof( buffer), f )) {
181 p = strchr ( buffer, '#' );
185 l = xstrlen ( buffer );
187 while ( l && isspace ( buffer [l-1] )) {
193 continuation_line = 0;
197 if ( !continuation_line ) {
198 if (( strncmp ( buffer, "alias", 5 ) == 0 ) && isspace ( buffer [5] )) {
203 while ( isspace ( *alias ))
206 while ( !isspace ( *mod ))
210 while ( isspace ( *mod ))
213 // fprintf ( stderr, "ALIAS: '%s' -> '%s'\n", alias, mod );
216 first = current = (struct dep_t *) malloc ( sizeof ( struct dep_t ));
219 current-> m_next = (struct dep_t *) malloc ( sizeof ( struct dep_t ));
220 current = current-> m_next;
222 current-> m_module = xstrdup ( alias );
223 current-> m_isalias = 1;
225 if (( strcmp ( alias, "off" ) == 0 ) || ( strcmp ( alias, "null" ) == 0 )) {
226 current-> m_depcnt = 0;
227 current-> m_deparr = 0;
230 current-> m_depcnt = 1;
231 current-> m_deparr = xmalloc ( 1 * sizeof( char * ));
232 current-> m_deparr[0] = xstrdup ( mod );
234 current-> m_next = 0;
245 static int mod_process ( struct mod_list_t *list, int do_insert )
255 snprintf ( lcmd, sizeof( lcmd ) - 1, "insmod %s %s %s %s 2>/dev/null", do_syslog ? "-s" : "", autoclean ? "-k" : "", quiet ? "-q" : "", list-> m_module );
257 snprintf ( lcmd, sizeof( lcmd ) - 1, "rmmod %s %s 2>/dev/null", do_syslog ? "-s" : "", list-> m_module );
260 printf ( "%s\n", lcmd );
262 rc |= system ( lcmd );
264 list = do_insert ? list-> m_prev : list-> m_next;
269 static void check_dep ( char *mod, struct mod_list_t **head, struct mod_list_t **tail )
271 struct mod_list_t *find;
276 // remove .o extension
277 lm = xstrlen ( mod );
278 if (( mod [lm-2] == '.' ) && ( mod [lm-1] == 'o' ))
281 // check dependencies
282 for ( dt = depend; dt; dt = dt-> m_next ) {
283 if ( strcmp ( dt-> m_module, mod ) == 0 )
286 // resolve alias names
287 if ( dt && dt-> m_isalias ) {
288 if ( dt-> m_depcnt == 1 )
289 check_dep ( dt-> m_deparr [0], head, tail );
290 printf ( "Got alias: %s -> %s\n", mod, dt-> m_deparr [0] );
295 // search for duplicates
296 for ( find = *head; find; find = find-> m_next ) {
297 if ( !strcmp ( mod, find-> m_module )) {
298 // found -> dequeue it
301 find-> m_prev-> m_next = find-> m_next;
303 *head = find-> m_next;
306 find-> m_next-> m_prev = find-> m_prev;
308 *tail = find-> m_prev;
310 break; // there can be only one duplicate
314 if ( !find ) { // did not find a duplicate
315 find = (struct mod_list_t *) xmalloc ( sizeof(struct mod_list_t));
316 find-> m_module = mod;
321 (*tail)-> m_next = find;
322 find-> m_prev = *tail;
332 for ( i = 0; i < dt-> m_depcnt; i++ )
333 check_dep ( dt-> m_deparr [i], head, tail );
339 static int mod_insert ( char *mod, int argc, char **argv )
341 struct mod_list_t *tail = 0;
342 struct mod_list_t *head = 0;
345 // get dep list for module mod
346 check_dep ( mod, &head, &tail );
348 if ( head && tail ) {
352 // append module args
353 l = xstrlen ( head-> m_module );
354 for ( i = 0; i < argc; i++ )
355 l += ( xstrlen ( argv [i] ) + 1 );
357 head-> m_module = realloc ( head-> m_module, l + 1 );
359 for ( i = 0; i < argc; i++ ) {
360 strcat ( head-> m_module, " " );
361 strcat ( head-> m_module, argv [i] );
364 // process tail ---> head
365 rc |= mod_process ( tail, 1 );
373 static void mod_remove ( char *mod )
375 static struct mod_list_t rm_a_dummy = { "-a", 0, 0 };
377 struct mod_list_t *head = 0;
378 struct mod_list_t *tail = 0;
381 check_dep ( mod, &head, &tail );
383 head = tail = &rm_a_dummy;
386 mod_process ( head, 0 ); // process head ---> tail
391 extern int modprobe_main(int argc, char** argv)
396 autoclean = show_only = quiet = do_syslog = verbose = 0;
398 while ((opt = getopt(argc, argv, "acdklnqrst:vVC:")) != -1) {
400 case 'c': // no config used
401 case 'l': // no pattern matching
404 case 'C': // no config used
405 case 't': // no pattern matching
406 error_msg_and_die("-t and -C not supported");
436 depend = build_dep ( );
439 error_msg_and_die ( "could not parse modules.dep\n" );
443 mod_remove ( optind < argc ? argv [optind] : 0 );
444 } while ( ++optind < argc );
450 error_msg_and_die ( "No module or pattern provided\n" );
452 return mod_insert ( argv [optind], argc - optind - 1, argv + optind + 1 ) ? \
453 EXIT_FAILURE : EXIT_SUCCESS;