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 /* Jump through hoops to simulate how fgets() grabs just one line at a
48 * time... Don't use any stdio since modprobe gets called from a kernel
49 * thread and stdio junk can overflow the limited stack...
51 static char *reads ( int fd, char *buffer, size_t len )
53 int n = read ( fd, buffer, len );
59 p = strchr ( buffer, '\n' );
64 offset = lseek ( fd, 0L, SEEK_CUR ); // Get the current file descriptor offset
65 lseek ( fd, offset-n + (p-buffer) + 1, SEEK_SET ); // Set the file descriptor offset to right after the \n
76 static struct dep_t *build_dep ( void )
80 struct dep_t *first = 0;
81 struct dep_t *current = 0;
83 char *filename = buffer;
84 int continuation_line = 0;
89 // check for buffer overflow in following code
90 if ( xstrlen ( un.release ) > ( sizeof( buffer ) - 64 )) {
94 strcpy ( filename, "/lib/modules/" );
95 strcat ( filename, un.release );
96 strcat ( filename, "/modules.dep" );
98 if (( fd = open ( filename, O_RDONLY )) < 0 )
101 while ( reads ( fd, buffer, sizeof( buffer ))) {
102 int l = xstrlen ( buffer );
105 while ( isspace ( buffer [l-1] )) {
111 continuation_line = 0;
115 if ( !continuation_line ) {
116 char *col = strchr ( buffer, ':' );
124 mods = strrchr ( buffer, '/' );
131 if (( *(col-2) == '.' ) && ( *(col-1) == 'o' ))
134 mod = xstrndup ( mods, col - mods - ext );
137 first = current = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
140 current-> m_next = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
141 current = current-> m_next;
143 current-> m_module = mod;
144 current-> m_isalias = 0;
145 current-> m_depcnt = 0;
146 current-> m_deparr = 0;
147 current-> m_next = 0;
149 //printf ( "%s:\n", mod );
160 char *end = &buffer [l-1];
161 char *deps = strrchr ( end, '/' );
165 while ( isblank ( *end ) || ( *end == '\\' ))
168 deps = strrchr ( p, '/' );
170 if ( !deps || ( deps < p )) {
173 while ( isblank ( *deps ))
179 if (( *(end-1) == '.' ) && ( *end == 'o' ))
182 /* Cope with blank lines */
183 if ((end-deps-ext+1) <= 0)
186 dep = xstrndup ( deps, end - deps - ext + 1 );
188 current-> m_depcnt++;
189 current-> m_deparr = (char **) xrealloc ( current-> m_deparr, sizeof ( char *) * current-> m_depcnt );
190 current-> m_deparr [current-> m_depcnt - 1] = dep;
192 //printf ( " %d) %s\n", current-> m_depcnt, current-> m_deparr [current-> m_depcnt -1] );
195 if ( buffer [l-1] == '\\' )
196 continuation_line = 1;
198 continuation_line = 0;
202 // alias parsing is not 100% correct (no correct handling of continuation lines within an alias) !
204 if (( fd = open ( "/etc/modules.conf", O_RDONLY )) < 0 )
205 if (( fd = open ( "/etc/conf.modules", O_RDONLY )) < 0 )
208 continuation_line = 0;
209 while ( reads ( fd, buffer, sizeof( buffer ))) {
213 p = strchr ( buffer, '#' );
217 l = xstrlen ( buffer );
219 while ( l && isspace ( buffer [l-1] )) {
225 continuation_line = 0;
229 if ( !continuation_line ) {
230 if (( strncmp ( buffer, "alias", 5 ) == 0 ) && isspace ( buffer [5] )) {
235 while ( isspace ( *alias ))
238 while ( !isspace ( *mod ))
242 while ( isspace ( *mod ))
245 // fprintf ( stderr, "ALIAS: '%s' -> '%s'\n", alias, mod );
248 first = current = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
251 current-> m_next = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
252 current = current-> m_next;
254 current-> m_module = xstrdup ( alias );
255 current-> m_isalias = 1;
257 if (( strcmp ( alias, "off" ) == 0 ) || ( strcmp ( alias, "null" ) == 0 )) {
258 current-> m_depcnt = 0;
259 current-> m_deparr = 0;
262 current-> m_depcnt = 1;
263 current-> m_deparr = xmalloc ( 1 * sizeof( char * ));
264 current-> m_deparr[0] = xstrdup ( mod );
266 current-> m_next = 0;
276 static int mod_process ( struct mod_list_t *list, int do_insert )
286 snprintf ( lcmd, sizeof( lcmd ) - 1, "insmod %s %s %s %s 2>/dev/null", do_syslog ? "-s" : "", autoclean ? "-k" : "", quiet ? "-q" : "", list-> m_module );
288 snprintf ( lcmd, sizeof( lcmd ) - 1, "rmmod %s %s 2>/dev/null", do_syslog ? "-s" : "", list-> m_module );
291 printf ( "%s\n", lcmd );
293 rc |= system ( lcmd );
295 list = do_insert ? list-> m_prev : list-> m_next;
300 static void check_dep ( char *mod, struct mod_list_t **head, struct mod_list_t **tail )
302 struct mod_list_t *find;
307 // remove .o extension
308 lm = xstrlen ( mod );
309 if (( mod [lm-2] == '.' ) && ( mod [lm-1] == 'o' ))
312 // check dependencies
313 for ( dt = depend; dt; dt = dt-> m_next ) {
314 if ( strcmp ( dt-> m_module, mod ) == 0 )
317 // resolve alias names
318 if ( dt && dt-> m_isalias ) {
319 if ( dt-> m_depcnt == 1 )
320 check_dep ( dt-> m_deparr [0], head, tail );
321 printf ( "Got alias: %s -> %s\n", mod, dt-> m_deparr [0] );
326 // search for duplicates
327 for ( find = *head; find; find = find-> m_next ) {
328 if ( !strcmp ( mod, find-> m_module )) {
329 // found -> dequeue it
332 find-> m_prev-> m_next = find-> m_next;
334 *head = find-> m_next;
337 find-> m_next-> m_prev = find-> m_prev;
339 *tail = find-> m_prev;
341 break; // there can be only one duplicate
345 if ( !find ) { // did not find a duplicate
346 find = (struct mod_list_t *) xmalloc ( sizeof(struct mod_list_t));
347 find-> m_module = mod;
352 (*tail)-> m_next = find;
353 find-> m_prev = *tail;
363 for ( i = 0; i < dt-> m_depcnt; i++ )
364 check_dep ( dt-> m_deparr [i], head, tail );
370 static int mod_insert ( char *mod, int argc, char **argv )
372 struct mod_list_t *tail = 0;
373 struct mod_list_t *head = 0;
376 // get dep list for module mod
377 check_dep ( mod, &head, &tail );
379 if ( head && tail ) {
383 // append module args
384 l = xstrlen ( head-> m_module );
385 for ( i = 0; i < argc; i++ )
386 l += ( xstrlen ( argv [i] ) + 1 );
388 head-> m_module = xrealloc ( head-> m_module, l + 1 );
390 for ( i = 0; i < argc; i++ ) {
391 strcat ( head-> m_module, " " );
392 strcat ( head-> m_module, argv [i] );
395 // process tail ---> head
396 rc |= mod_process ( tail, 1 );
404 static void mod_remove ( char *mod )
406 static struct mod_list_t rm_a_dummy = { "-a", 0, 0 };
408 struct mod_list_t *head = 0;
409 struct mod_list_t *tail = 0;
412 check_dep ( mod, &head, &tail );
414 head = tail = &rm_a_dummy;
417 mod_process ( head, 0 ); // process head ---> tail
422 extern int modprobe_main(int argc, char** argv)
427 autoclean = show_only = quiet = do_syslog = verbose = 0;
429 while ((opt = getopt(argc, argv, "acdklnqrst:vVC:")) != -1) {
431 case 'c': // no config used
432 case 'l': // no pattern matching
435 case 'C': // no config used
436 case 't': // no pattern matching
437 error_msg_and_die("-t and -C not supported");
467 depend = build_dep ( );
470 error_msg_and_die ( "could not parse modules.dep\n" );
474 mod_remove ( optind < argc ? xstrdup ( argv [optind] ) : 0 );
475 } while ( ++optind < argc );
481 error_msg_and_die ( "No module or pattern provided\n" );
483 return mod_insert ( xstrdup ( argv [optind] ), argc - optind - 1, argv + optind + 1 ) ? \
484 EXIT_FAILURE : EXIT_SUCCESS;