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>
29 struct dep_t * m_next;
35 struct mod_list_t * m_prev;
36 struct mod_list_t * m_next;
40 static struct dep_t *depend;
41 static int autoclean, show_only, quiet, do_syslog, verbose;
44 static struct dep_t *build_dep ( void )
48 struct dep_t *first = 0;
49 struct dep_t *current = 0;
51 char *filename = buffer;
52 int continuation_line = 0;
57 // check for buffer overflow in following code
58 if ( xstrlen ( un. release ) > ( sizeof( buffer ) - 64 ))
61 strcpy ( filename, "/lib/modules/" );
62 strcat ( filename, un. release );
63 strcat ( filename, "/modules.dep" );
65 f = fopen ( filename, "r" );
69 while ( fgets ( buffer, sizeof( buffer), f )) {
70 int l = xstrlen ( buffer );
73 if ( buffer [l-1] == '\n' ) {
79 continuation_line = 0;
83 if ( !continuation_line ) {
84 char *col = strchr ( buffer, ':' );
92 mods = strrchr ( buffer, '/' );
99 if (( *(col-2) == '.' ) && ( *(col-1) == 'o' ))
102 mod = xstrndup ( mods, col - mods - ext );
105 first = current = (struct dep_t *) malloc ( sizeof ( struct dep_t ));
108 current-> m_next = (struct dep_t *) malloc ( sizeof ( struct dep_t ));
109 current = current-> m_next;
111 current-> m_module = mod;
112 current-> m_depcnt = 0;
113 current-> m_deparr = 0;
114 current-> m_next = 0;
116 //printf ( "%s:\n", mod );
127 char *end = &buffer [l-1];
128 char *deps = strrchr ( end, '/' );
132 while ( isblank ( *end ) || ( *end == '\\' ))
135 deps = strrchr ( p, '/' );
137 if ( !deps || ( deps < p )) {
140 while ( isblank ( *deps ))
146 if (( *(end-1) == '.' ) && ( *end == 'o' ))
149 dep = xstrndup ( deps, end - deps - ext + 1 );
151 current-> m_depcnt++;
152 current-> m_deparr = (char **) xrealloc ( current-> m_deparr, sizeof ( char *) * current-> m_depcnt );
153 current-> m_deparr [current-> m_depcnt - 1] = dep;
155 //printf ( " %d) %s\n", current-> m_depcnt, current-> m_deparr [current-> m_depcnt -1] );
158 if ( buffer [l-1] == '\\' )
159 continuation_line = 1;
161 continuation_line = 0;
169 static int mod_process ( struct mod_list_t *list, int do_insert )
179 snprintf ( lcmd, sizeof( lcmd ) - 1, "insmod %s %s %s %s 2>/dev/null", do_syslog ? "-s" : "", autoclean ? "-k" : "", quiet ? "-q" : "", list-> m_module );
181 snprintf ( lcmd, sizeof( lcmd ) - 1, "rmmod %s %s 2>/dev/null", do_syslog ? "-s" : "", list-> m_module );
184 printf ( "%s\n", lcmd );
186 rc |= system ( lcmd );
188 list = do_insert ? list-> m_prev : list-> m_next;
193 static void check_dep ( char *mod, struct mod_list_t **head, struct mod_list_t **tail )
195 struct mod_list_t *find;
200 // remove .o extension
201 lm = xstrlen ( mod );
202 if (( mod [lm-2] == '.' ) && ( mod [lm-1] == 'o' ))
205 // search for duplicates
206 for ( find = *head; find; find = find-> m_next ) {
207 if ( !strcmp ( mod, find-> m_module )) {
208 // found -> dequeue it
211 find-> m_prev-> m_next = find-> m_next;
213 *head = find-> m_next;
216 find-> m_next-> m_prev = find-> m_prev;
218 *tail = find-> m_prev;
220 break; // there can be only one duplicate
224 if ( !find ) { // did not find a duplicate
225 find = (struct mod_list_t *) xmalloc ( sizeof(struct mod_list_t));
226 find-> m_module = mod;
231 (*tail)-> m_next = find;
232 find-> m_prev = *tail;
239 // check dependencies
240 for ( dt = depend; dt; dt = dt-> m_next ) {
241 if ( !strcmp ( dt-> m_module, mod )) {
244 for ( i = 0; i < dt-> m_depcnt; i++ )
245 check_dep ( dt-> m_deparr [i], head, tail );
252 static int mod_insert ( char *mod, int argc, char **argv )
254 struct mod_list_t *tail = 0;
255 struct mod_list_t *head = 0;
258 // get dep list for module mod
259 check_dep ( mod, &head, &tail );
261 if ( head && tail ) {
265 // append module args
267 for ( i = 0; i < argc; i++ )
268 l += ( xstrlen ( argv [i] ) + 1 );
270 head-> m_module = xstrndup ( mod, l );
272 for ( i = 0; i < argc; i++ ) {
273 strcat ( head-> m_module, " " );
274 strcat ( head-> m_module, argv [i] );
277 // process tail ---> head
278 rc |= mod_process ( tail, 1 );
286 static void mod_remove ( char *mod )
288 static struct mod_list_t rm_a_dummy = { "-a", 0, 0 };
290 struct mod_list_t *head = 0;
291 struct mod_list_t *tail = 0;
294 check_dep ( mod, &head, &tail );
296 head = tail = &rm_a_dummy;
299 mod_process ( head, 0 ); // process head ---> tail
304 extern int modprobe_main(int argc, char** argv)
309 autoclean = show_only = quiet = do_syslog = verbose = 0;
311 while ((opt = getopt(argc, argv, "acdklnqrst:vVC:")) != -1) {
313 case 'c': // no config used
314 case 'l': // no pattern matching
317 case 'C': // no config used
318 case 't': // no pattern matching
319 error_msg_and_die("-t and -C not supported");
349 depend = build_dep ( );
352 error_msg_and_die ( "could not parse modules.dep\n" );
356 mod_remove ( optind < argc ? argv [optind] : 0 );
357 } while ( ++optind < argc );
363 error_msg_and_die ( "No module or pattern provided\n" );
365 return mod_insert ( argv [optind], argc - optind - 1, argv + optind + 1 ) ? \
366 EXIT_FAILURE : EXIT_SUCCESS;