blob: f51de56be5cee056678caa454335e4223e91ffd4 [file] [log] [blame]
Eric Andersen0139ca92001-07-22 23:01:03 +00001/* vi: set sw=4 ts=4: */
2/*
Robert Grieblaead70b2002-07-26 15:54:20 +00003 * Modprobe written from scratch for BusyBox
Eric Andersen60e56f52002-04-26 06:04:01 +00004 *
Robert Griebl6859d762002-08-05 02:57:12 +00005 * Copyright (c) 2002 by Robert Griebl, griebl@gmx.de
Eric Andersen908e3622003-06-20 09:56:37 +00006 * Copyright (c) 2003 by Andrew Dennison, andrew.dennison@motec.com.au
Paul Fox8eeb6552005-08-04 18:33:36 +00007 * Copyright (c) 2005 by Jim Bauer, jfbauer@nfr.com
Bernhard Reutner-Fischer17d355c2005-12-14 08:32:44 +00008 *
Rob Landleye9190962005-12-12 19:38:44 +00009 * Portions Copyright (c) 2005 by Yann E. MORIN, yann.morin.1998@anciens.enib.fr
Robert Griebl6859d762002-08-05 02:57:12 +000010 *
Rob Landley79e1cab2005-11-15 00:08:29 +000011 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
Robert Griebl6859d762002-08-05 02:57:12 +000012*/
Eric Andersen0139ca92001-07-22 23:01:03 +000013
Bernhard Reutner-Fischerdeda6a52006-06-03 19:35:15 +000014#include "busybox.h"
Robert Griebl52e8d062002-05-14 23:42:08 +000015#include <sys/utsname.h>
Bernhard Reutner-Fischer2c351a82006-06-03 19:08:49 +000016#include <fnmatch.h>
Eric Andersen0139ca92001-07-22 23:01:03 +000017
Rob Landleye9190962005-12-12 19:38:44 +000018struct mod_opt_t { /* one-way list of options to pass to a module */
19 char * m_opt_val;
20 struct mod_opt_t * m_next;
Eric Andersen60e56f52002-04-26 06:04:01 +000021};
22
Rob Landleye9190962005-12-12 19:38:44 +000023struct dep_t { /* one-way list of dependency rules */
24 /* a dependency rule */
25 char * m_name; /* the module name*/
26 char * m_path; /* the module file path */
27 struct mod_opt_t * m_options; /* the module options */
Bernhard Reutner-Fischer17d355c2005-12-14 08:32:44 +000028
Rob Landleye9190962005-12-12 19:38:44 +000029 int m_isalias : 1; /* the module is an alias */
30 int m_reserved : 15; /* stuffin' */
31
32 int m_depcnt : 16; /* the number of dependable module(s) */
33 char ** m_deparr; /* the list of dependable module(s) */
34
35 struct dep_t * m_next; /* the next dependency rule */
36};
37
38struct mod_list_t { /* two-way list of modules to process */
39 /* a module description */
Eric Andersen807bd842004-08-19 18:30:31 +000040 char * m_name;
41 char * m_path;
Rob Landleye9190962005-12-12 19:38:44 +000042 struct mod_opt_t * m_options;
Eric Andersen3b1a7442003-12-24 20:30:45 +000043
Robert Griebl52e8d062002-05-14 23:42:08 +000044 struct mod_list_t * m_prev;
45 struct mod_list_t * m_next;
46};
47
48
Robert Griebl236abbf2002-05-22 23:34:35 +000049static struct dep_t *depend;
"Vladimir N. Oleynik"4fc92202006-02-02 14:48:54 +000050
51#define main_options "acdklnqrst:vVC:"
52#define INSERT_ALL 1 /* a */
53#define DUMP_CONF_EXIT 2 /* c */
54#define D_OPT_IGNORED 4 /* d */
55#define AUTOCLEAN_FLG 8 /* k */
56#define LIST_ALL 16 /* l */
57#define SHOW_ONLY 32 /* n */
58#define QUIET 64 /* q */
59#define REMOVE_OPT 128 /* r */
60#define DO_SYSLOG 256 /* s */
61#define RESTRICT_DIR 512 /* t */
62#define VERBOSE 1024 /* v */
63#define VERSION_ONLY 2048 /* V */
64#define CONFIG_FILE 4096 /* C */
65
66#define autoclean (main_opts & AUTOCLEAN_FLG)
67#define show_only (main_opts & SHOW_ONLY)
68#define quiet (main_opts & QUIET)
69#define remove_opt (main_opts & REMOVE_OPT)
70#define do_syslog (main_opts & DO_SYSLOG)
71#define verbose (main_opts & VERBOSE)
72
73static int main_opts;
Robert Griebl52e8d062002-05-14 23:42:08 +000074
Eric Andersen14f5c8d2005-04-16 19:39:00 +000075static int parse_tag_value ( char *buffer, char **ptag, char **pvalue )
Robert Grieblaead70b2002-07-26 15:54:20 +000076{
77 char *tag, *value;
78
79 while ( isspace ( *buffer ))
Eric Andersen716ccb22004-01-10 11:29:31 +000080 buffer++;
Robert Grieblaead70b2002-07-26 15:54:20 +000081 tag = value = buffer;
82 while ( !isspace ( *value ))
Eric Andersen7e496a72004-04-06 12:06:03 +000083 if (!*value) return 0;
84 else value++;
Robert Grieblaead70b2002-07-26 15:54:20 +000085 *value++ = 0;
86 while ( isspace ( *value ))
87 value++;
Eric Andersen7e496a72004-04-06 12:06:03 +000088 if (!*value) return 0;
Robert Grieblaead70b2002-07-26 15:54:20 +000089
90 *ptag = tag;
91 *pvalue = value;
Eric Andersen3b1a7442003-12-24 20:30:45 +000092
Eric Andersen7e496a72004-04-06 12:06:03 +000093 return 1;
Robert Grieblaead70b2002-07-26 15:54:20 +000094}
Eric Andersen60e56f52002-04-26 06:04:01 +000095
Robert Grieblbc28f7a2002-06-04 19:33:58 +000096/* Jump through hoops to simulate how fgets() grabs just one line at a
97 * time... Don't use any stdio since modprobe gets called from a kernel
Eric Andersen716ccb22004-01-10 11:29:31 +000098 * thread and stdio junk can overflow the limited stack...
Robert Grieblbc28f7a2002-06-04 19:33:58 +000099 */
100static char *reads ( int fd, char *buffer, size_t len )
101{
102 int n = read ( fd, buffer, len );
Eric Andersen3b1a7442003-12-24 20:30:45 +0000103
Robert Grieblbc28f7a2002-06-04 19:33:58 +0000104 if ( n > 0 ) {
105 char *p;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000106
Robert Grieblbc28f7a2002-06-04 19:33:58 +0000107 buffer [len-1] = 0;
108 p = strchr ( buffer, '\n' );
Eric Andersen3b1a7442003-12-24 20:30:45 +0000109
Robert Grieblbc28f7a2002-06-04 19:33:58 +0000110 if ( p ) {
111 off_t offset;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000112
Eric Andersen716ccb22004-01-10 11:29:31 +0000113 offset = lseek ( fd, 0L, SEEK_CUR ); // Get the current file descriptor offset
Robert Grieblbc28f7a2002-06-04 19:33:58 +0000114 lseek ( fd, offset-n + (p-buffer) + 1, SEEK_SET ); // Set the file descriptor offset to right after the \n
115
116 p[1] = 0;
117 }
118 return buffer;
119 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000120
Robert Grieblbc28f7a2002-06-04 19:33:58 +0000121 else
122 return 0;
123}
124
Rob Landleye9190962005-12-12 19:38:44 +0000125/*
126 * This function appends an option to a list
127 */
Bernhard Reutner-Fischer101a4702006-04-03 15:46:14 +0000128static struct mod_opt_t *append_option( struct mod_opt_t *opt_list, char *opt )
Eric Andersen60e56f52002-04-26 06:04:01 +0000129{
Rob Landleye9190962005-12-12 19:38:44 +0000130 struct mod_opt_t *ol = opt_list;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000131
Rob Landleye9190962005-12-12 19:38:44 +0000132 if( ol ) {
133 while( ol-> m_next ) {
134 ol = ol-> m_next;
135 }
136 ol-> m_next = xmalloc( sizeof( struct mod_opt_t ) );
137 ol = ol-> m_next;
138 } else {
139 ol = opt_list = xmalloc( sizeof( struct mod_opt_t ) );
Eric Andersen03d80912003-12-19 21:04:19 +0000140 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000141
Rob Landleyd921b2e2006-08-03 15:41:12 +0000142 ol-> m_opt_val = xstrdup( opt );
Rob Landleye9190962005-12-12 19:38:44 +0000143 ol-> m_next = NULL;
Eric Andersen60e56f52002-04-26 06:04:01 +0000144
Rob Landleye9190962005-12-12 19:38:44 +0000145 return opt_list;
Eric Andersen60e56f52002-04-26 06:04:01 +0000146}
147
Bernhard Reutner-Fischer17d355c2005-12-14 08:32:44 +0000148#if ENABLE_FEATURE_MODPROBE_MULTIPLE_OPTIONS
Rob Landley79e1cab2005-11-15 00:08:29 +0000149/* static char* parse_command_string( char* src, char **dst );
150 * src: pointer to string containing argument
151 * dst: pointer to where to store the parsed argument
152 * return value: the pointer to the first char after the parsed argument,
153 * NULL if there was no argument parsed (only trailing spaces).
Rob Landleyd921b2e2006-08-03 15:41:12 +0000154 * Note that memory is allocated with xstrdup when a new argument was
Rob Landley79e1cab2005-11-15 00:08:29 +0000155 * parsed. Don't forget to free it!
156 */
157#define ARG_EMPTY 0x00
158#define ARG_IN_DQUOTES 0x01
159#define ARG_IN_SQUOTES 0x02
160static char *parse_command_string( char *src, char **dst )
161{
162 int opt_status = ARG_EMPTY;
163 char* tmp_str;
164
165 /* Dumb you, I have nothing to do... */
166 if( src == NULL ) return src;
167
168 /* Skip leading spaces */
169 while( *src == ' ' ) {
170 src++;
171 }
172 /* Is the end of string reached? */
173 if( *src == '\0' ) {
174 return NULL;
175 }
176 /* Reached the start of an argument
Rob Landleye9190962005-12-12 19:38:44 +0000177 * By the way, we duplicate a little too much
178 * here but what is too much is freed later. */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000179 *dst = tmp_str = xstrdup( src );
Rob Landley79e1cab2005-11-15 00:08:29 +0000180 /* Get to the end of that argument */
181 while( ( *tmp_str != '\0' )
Rob Landleye9190962005-12-12 19:38:44 +0000182 && ( ( *tmp_str != ' ' )
183 || ( opt_status & ( ARG_IN_DQUOTES | ARG_IN_SQUOTES ) ) ) ) {
Rob Landley79e1cab2005-11-15 00:08:29 +0000184 switch( *tmp_str ) {
185 case '\'':
186 if( opt_status & ARG_IN_DQUOTES ) {
187 /* Already in double quotes, keep current char as is */
188 } else {
189 /* shift left 1 char, until end of string: get rid of the opening/closing quotes */
190 memmove( tmp_str, tmp_str + 1, strlen( tmp_str ) );
191 /* mark me: we enter or leave single quotes */
192 opt_status ^= ARG_IN_SQUOTES;
193 /* Back one char, as we need to re-scan the new char there. */
194 tmp_str--;
195 }
196 break;
197 case '"':
198 if( opt_status & ARG_IN_SQUOTES ) {
199 /* Already in single quotes, keep current char as is */
200 } else {
201 /* shift left 1 char, until end of string: get rid of the opening/closing quotes */
202 memmove( tmp_str, tmp_str + 1, strlen( tmp_str ) );
203 /* mark me: we enter or leave double quotes */
204 opt_status ^= ARG_IN_DQUOTES;
205 /* Back one char, as we need to re-scan the new char there. */
206 tmp_str--;
207 }
208 break;
209 case '\\':
210 if( opt_status & ARG_IN_SQUOTES ) {
211 /* Between single quotes: keep as is. */
212 } else {
213 switch( *(tmp_str+1) ) {
214 case 'a':
215 case 'b':
216 case 't':
217 case 'n':
218 case 'v':
219 case 'f':
220 case 'r':
221 case '0':
222 /* We escaped a special character. For now, keep
223 * both the back-slash and the following char. */
224 tmp_str++; src++;
225 break;
226 default:
227 /* We escaped a space or a single or double quote,
228 * or a back-slash, or a non-escapable char. Remove
229 * the '\' and keep the new current char as is. */
230 memmove( tmp_str, tmp_str + 1, strlen( tmp_str ) );
231 break;
232 }
233 }
234 break;
235 /* Any other char that is special shall appear here.
236 * Example: $ starts a variable
237 case '$':
238 do_variable_expansion();
239 break;
240 * */
241 default:
242 /* any other char is kept as is. */
243 break;
244 }
245 tmp_str++; /* Go to next char */
246 src++; /* Go to next char to find the end of the argument. */
247 }
248 /* End of string, but still no ending quote */
249 if( opt_status & ( ARG_IN_DQUOTES | ARG_IN_SQUOTES ) ) {
250 bb_error_msg_and_die( "unterminated (single or double) quote in options list: %s", src );
251 }
"Vladimir N. Oleynik"4fc92202006-02-02 14:48:54 +0000252 *tmp_str++ = '\0';
253 *dst = xrealloc( *dst, (tmp_str - *dst ) );
Rob Landley79e1cab2005-11-15 00:08:29 +0000254 return src;
255}
Rob Landleyae50c6d2005-12-15 07:42:13 +0000256#else
257#define parse_command_string(src, dst) (0)
Bernhard Reutner-Fischer17d355c2005-12-14 08:32:44 +0000258#endif /* ENABLE_FEATURE_MODPROBE_MULTIPLE_OPTIONS */
Rob Landley79e1cab2005-11-15 00:08:29 +0000259
Rob Landleye9190962005-12-12 19:38:44 +0000260/*
Rob Landley72615752006-04-10 16:09:52 +0000261 * This function reads aliases and default module options from a configuration file
262 * (/etc/modprobe.conf syntax). It supports includes (only files, no directories).
263 */
264static void include_conf ( struct dep_t **first, struct dep_t **current, char *buffer, int buflen, int fd )
265{
266 int continuation_line = 0;
267
268 // alias parsing is not 100% correct (no correct handling of continuation lines within an alias) !
269
270 while ( reads ( fd, buffer, buflen)) {
271 int l;
272 char *p;
273
274 p = strchr ( buffer, '#' );
275 if ( p )
276 *p = 0;
277
Rob Landleya3896512006-05-07 20:20:34 +0000278 l = strlen ( buffer );
Rob Landley72615752006-04-10 16:09:52 +0000279
280 while ( l && isspace ( buffer [l-1] )) {
281 buffer [l-1] = 0;
282 l--;
283 }
284
285 if ( l == 0 ) {
286 continuation_line = 0;
287 continue;
288 }
289
290 if ( !continuation_line ) {
291 if (( strncmp ( buffer, "alias", 5 ) == 0 ) && isspace ( buffer [5] )) {
292 char *alias, *mod;
293
294 if ( parse_tag_value ( buffer + 6, &alias, &mod )) {
295 /* handle alias as a module dependent on the aliased module */
296 if ( !*current ) {
Rob Landley081e3842006-08-03 20:07:35 +0000297 (*first) = (*current) = (struct dep_t *) xzalloc (sizeof ( struct dep_t ));
Rob Landley72615752006-04-10 16:09:52 +0000298 }
299 else {
Rob Landley081e3842006-08-03 20:07:35 +0000300 (*current)-> m_next = (struct dep_t *) xzalloc (sizeof ( struct dep_t ));
Rob Landley72615752006-04-10 16:09:52 +0000301 (*current) = (*current)-> m_next;
302 }
Rob Landleyd921b2e2006-08-03 15:41:12 +0000303 (*current)-> m_name = xstrdup ( alias );
Rob Landley72615752006-04-10 16:09:52 +0000304 (*current)-> m_isalias = 1;
305
306 if (( strcmp ( mod, "off" ) == 0 ) || ( strcmp ( mod, "null" ) == 0 )) {
307 (*current)-> m_depcnt = 0;
308 (*current)-> m_deparr = 0;
309 }
310 else {
311 (*current)-> m_depcnt = 1;
312 (*current)-> m_deparr = xmalloc ( 1 * sizeof( char * ));
Rob Landleyd921b2e2006-08-03 15:41:12 +0000313 (*current)-> m_deparr[0] = xstrdup ( mod );
Rob Landley72615752006-04-10 16:09:52 +0000314 }
315 (*current)-> m_next = 0;
316 }
317 }
318 else if (( strncmp ( buffer, "options", 7 ) == 0 ) && isspace ( buffer [7] )) {
319 char *mod, *opt;
320
321 /* split the line in the module/alias name, and options */
322 if ( parse_tag_value ( buffer + 8, &mod, &opt )) {
323 struct dep_t *dt;
324
325 /* find the corresponding module */
326 for ( dt = *first; dt; dt = dt-> m_next ) {
327 if ( strcmp ( dt-> m_name, mod ) == 0 )
328 break;
329 }
330 if ( dt ) {
331 if ( ENABLE_FEATURE_MODPROBE_MULTIPLE_OPTIONS ) {
332 char* new_opt = NULL;
333 while( ( opt = parse_command_string( opt, &new_opt ) ) ) {
334 dt-> m_options = append_option( dt-> m_options, new_opt );
335 }
336 } else {
337 dt-> m_options = append_option( dt-> m_options, opt );
338 }
339 }
340 }
341 }
342 else if (( strncmp ( buffer, "include", 7 ) == 0 ) && isspace ( buffer [7] )) {
343
344 int fdi; char *filename = buffer + 8;
345
346 while ( isspace ( *filename ))
347 filename++;
348
349 if (( fdi = open ( filename, O_RDONLY )) >= 0 ) {
350 include_conf(first, current, buffer, buflen, fdi);
351 close(fdi);
352 }
353 }
354 }
355 }
356}
357
358/*
Bernhard Reutner-Fischere3c150b2006-05-19 11:24:28 +0000359 * This function builds a list of dependency rules from /lib/modules/`uname -r`\modules.dep.
360 * It then fills every modules and aliases with their default options, found by parsing
Rob Landleye9190962005-12-12 19:38:44 +0000361 * modprobe.conf (or modules.conf, or conf.modules).
362 */
363static struct dep_t *build_dep ( void )
364{
365 int fd;
366 struct utsname un;
367 struct dep_t *first = 0;
368 struct dep_t *current = 0;
369 char buffer[2048];
"Vladimir N. Oleynik"4fc92202006-02-02 14:48:54 +0000370 char *filename;
Rob Landleye9190962005-12-12 19:38:44 +0000371 int continuation_line = 0;
372 int k_version;
373
374 k_version = 0;
375 if ( uname ( &un ))
376 bb_error_msg_and_die("can't determine kernel version");
377
Rob Landleye9190962005-12-12 19:38:44 +0000378 if (un.release[0] == '2') {
379 k_version = un.release[2] - '0';
380 }
381
Rob Landleyd921b2e2006-08-03 15:41:12 +0000382 filename = xasprintf("/lib/modules/%s/modules.dep", un.release );
Rob Landley3afb0702006-05-18 20:41:43 +0000383 fd = open ( filename, O_RDONLY );
Bernhard Reutner-Fischere3c150b2006-05-19 11:24:28 +0000384 if (ENABLE_FEATURE_CLEAN_UP)
385 free(filename);
Rob Landley3afb0702006-05-18 20:41:43 +0000386 if (fd < 0) {
Rob Landleye9190962005-12-12 19:38:44 +0000387 /* Ok, that didn't work. Fall back to looking in /lib/modules */
388 if (( fd = open ( "/lib/modules/modules.dep", O_RDONLY )) < 0 ) {
389 return 0;
390 }
391 }
392
393 while ( reads ( fd, buffer, sizeof( buffer ))) {
Rob Landleya3896512006-05-07 20:20:34 +0000394 int l = strlen ( buffer );
Rob Landleye9190962005-12-12 19:38:44 +0000395 char *p = 0;
396
"Vladimir N. Oleynik"4fc92202006-02-02 14:48:54 +0000397 while ( l > 0 && isspace ( buffer [l-1] )) {
Rob Landleye9190962005-12-12 19:38:44 +0000398 buffer [l-1] = 0;
399 l--;
400 }
401
402 if ( l == 0 ) {
403 continuation_line = 0;
404 continue;
405 }
406
407 /* Is this a new module dep description? */
408 if ( !continuation_line ) {
Bernhard Reutner-Fischer17d355c2005-12-14 08:32:44 +0000409 /* find the dep beginning */
Rob Landleye9190962005-12-12 19:38:44 +0000410 char *col = strchr ( buffer, ':' );
411 char *dot = col;
412
413 if ( col ) {
414 /* This line is a dep description */
415 char *mods;
416 char *modpath;
417 char *mod;
418
419 /* Find the beginning of the module file name */
420 *col = 0;
421 mods = strrchr ( buffer, '/' );
422
423 if ( !mods )
424 mods = buffer; /* no path for this module */
425 else
426 mods++; /* there was a path for this module... */
427
428 /* find the path of the module */
429 modpath = strchr ( buffer, '/' ); /* ... and this is the path */
430 if ( !modpath )
431 modpath = buffer; /* module with no path */
432 /* find the end of the module name in the file name */
433 if ( ENABLE_FEATURE_2_6_MODULES &&
434 (k_version > 4) && ( *(col-3) == '.' ) &&
435 ( *(col-2) == 'k' ) && ( *(col-1) == 'o' ) )
436 dot = col - 3;
437 else
438 if (( *(col-2) == '.' ) && ( *(col-1) == 'o' ))
439 dot = col - 2;
440
Rob Landleyd921b2e2006-08-03 15:41:12 +0000441 mod = xstrndup ( mods, dot - mods );
Rob Landleye9190962005-12-12 19:38:44 +0000442
443 /* enqueue new module */
444 if ( !current ) {
445 first = current = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
446 }
447 else {
448 current-> m_next = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
449 current = current-> m_next;
450 }
451 current-> m_name = mod;
Rob Landleyd921b2e2006-08-03 15:41:12 +0000452 current-> m_path = xstrdup(modpath);
Rob Landleye9190962005-12-12 19:38:44 +0000453 current-> m_options = NULL;
454 current-> m_isalias = 0;
455 current-> m_depcnt = 0;
456 current-> m_deparr = 0;
457 current-> m_next = 0;
458
459 p = col + 1;
460 }
461 else
462 /* this line is not a dep description */
463 p = 0;
464 }
465 else
466 /* It's a dep description continuation */
467 p = buffer;
468
469 while ( p && *p && isblank(*p))
470 p++;
471
472 /* p points to the first dependable module; if NULL, no dependable module */
473 if ( p && *p ) {
474 char *end = &buffer [l-1];
475 char *deps;
476 char *dep;
477 char *next;
478 int ext = 0;
479
480 while ( isblank ( *end ) || ( *end == '\\' ))
481 end--;
482
483 do
484 {
485 /* search the end of the dependency */
486 next = strchr (p, ' ' );
487 if (next)
488 {
489 *next = 0;
490 next--;
491 }
492 else
493 next = end;
494
Bernhard Reutner-Fischer17d355c2005-12-14 08:32:44 +0000495 /* find the beginning of the module file name */
Rob Landleye9190962005-12-12 19:38:44 +0000496 deps = strrchr ( p, '/' );
497
498 if ( !deps || ( deps < p )) {
499 deps = p;
500
501 while ( isblank ( *deps ))
502 deps++;
503 }
504 else
505 deps++;
506
507 /* find the end of the module name in the file name */
508 if ( ENABLE_FEATURE_2_6_MODULES &&
509 (k_version > 4) && ( *(next-2) == '.' ) &&
510 ( *(next-1) == 'k' ) && ( *next == 'o' ) )
511 ext = 3;
512 else
513 if (( *(next-1) == '.' ) && ( *next == 'o' ))
514 ext = 2;
515
516 /* Cope with blank lines */
517 if ((next-deps-ext+1) <= 0)
518 continue;
Rob Landleyd921b2e2006-08-03 15:41:12 +0000519 dep = xstrndup ( deps, next - deps - ext + 1 );
Rob Landleye9190962005-12-12 19:38:44 +0000520
521 /* Add the new dependable module name */
522 current-> m_depcnt++;
523 current-> m_deparr = (char **) xrealloc ( current-> m_deparr,
524 sizeof ( char *) * current-> m_depcnt );
525 current-> m_deparr [current-> m_depcnt - 1] = dep;
526
527 p = next + 2;
528 } while (next < end);
529 }
530
531 /* is there other dependable module(s) ? */
532 if ( buffer [l-1] == '\\' )
533 continuation_line = 1;
534 else
535 continuation_line = 0;
536 }
537 close ( fd );
538
Rob Landley3b0cfb42006-07-19 21:33:42 +0000539 /*
540 * First parse system-specific options and aliases
541 * as they take precedence over the kernel ones.
542 */
Rob Landleyae50c6d2005-12-15 07:42:13 +0000543 if (!ENABLE_FEATURE_2_6_MODULES
544 || ( fd = open ( "/etc/modprobe.conf", O_RDONLY )) < 0 )
Rob Landleye9190962005-12-12 19:38:44 +0000545 if (( fd = open ( "/etc/modules.conf", O_RDONLY )) < 0 )
Rob Landley3b0cfb42006-07-19 21:33:42 +0000546 fd = open ( "/etc/conf.modules", O_RDONLY );
Rob Landleye9190962005-12-12 19:38:44 +0000547
Rob Landley3b0cfb42006-07-19 21:33:42 +0000548 if (fd >= 0) {
549 include_conf (&first, &current, buffer, sizeof(buffer), fd);
550 close(fd);
Rob Landleye9190962005-12-12 19:38:44 +0000551 }
Rob Landley72615752006-04-10 16:09:52 +0000552
Rob Landley3b0cfb42006-07-19 21:33:42 +0000553 /* Only 2.6 has a modules.alias file */
554 if (ENABLE_FEATURE_2_6_MODULES) {
555 /* Parse kernel-declared aliases */
Rob Landleyd921b2e2006-08-03 15:41:12 +0000556 filename = xasprintf("/lib/modules/%s/modules.alias", un.release);
Rob Landley3b0cfb42006-07-19 21:33:42 +0000557 if ((fd = open ( filename, O_RDONLY )) < 0) {
558 /* Ok, that didn't work. Fall back to looking in /lib/modules */
559 fd = open ( "/lib/modules/modules.alias", O_RDONLY );
560 }
561 if (ENABLE_FEATURE_CLEAN_UP)
562 free(filename);
563
564 if (fd >= 0) {
565 include_conf (&first, &current, buffer, sizeof(buffer), fd);
566 close(fd);
567 }
568 }
Rob Landleye9190962005-12-12 19:38:44 +0000569
570 return first;
571}
572
573/* return 1 = loaded, 0 = not loaded, -1 = can't tell */
574static int already_loaded (const char *name)
575{
Rob Landleyd7605602006-06-14 01:51:16 +0000576 int fd, ret = 0;
Rob Landleye9190962005-12-12 19:38:44 +0000577 char buffer[4096];
578
579 fd = open ("/proc/modules", O_RDONLY);
580 if (fd < 0)
581 return -1;
582
583 while ( reads ( fd, buffer, sizeof( buffer ))) {
584 char *p;
585
586 p = strchr (buffer, ' ');
587 if (p) {
Rob Landleyd7605602006-06-14 01:51:16 +0000588 const char *n;
589
590 // Truncate buffer at first space and check for matches, with
591 // the idiosyncrasy that _ and - are interchangeable because the
592 // 2.6 kernel does weird things.
593
Rob Landleye9190962005-12-12 19:38:44 +0000594 *p = 0;
Rob Landleyd7605602006-06-14 01:51:16 +0000595 for (p = buffer, n = name; ; p++, n++) {
596 if (*p != *n) {
597 if ((*p == '_' || *p == '-') && (*n == '_' || *n == '-'))
598 continue;
599 break;
600 }
601 // If we made it to the end, that's a match.
602 if (!*p) {
603 ret = 1;
604 goto done;
605 }
Rob Landleye9190962005-12-12 19:38:44 +0000606 }
607 }
608 }
Rob Landleyd7605602006-06-14 01:51:16 +0000609done:
Rob Landleye9190962005-12-12 19:38:44 +0000610 close (fd);
Mike Frysinger135cee32006-06-21 23:03:37 +0000611 return ret;
Rob Landleye9190962005-12-12 19:38:44 +0000612}
613
Robert Griebl236abbf2002-05-22 23:34:35 +0000614static int mod_process ( struct mod_list_t *list, int do_insert )
Eric Andersen60e56f52002-04-26 06:04:01 +0000615{
Eric Andersen44b57582004-08-03 08:23:33 +0000616 int rc = 0;
Rob Landley79e1cab2005-11-15 00:08:29 +0000617 char **argv = NULL;
Rob Landleye9190962005-12-12 19:38:44 +0000618 struct mod_opt_t *opts;
619 int argc_malloc; /* never used when CONFIG_FEATURE_CLEAN_UP not defined */
Paul Fox8eeb6552005-08-04 18:33:36 +0000620 int argc;
Eric Andersen864b7972002-05-03 15:48:26 +0000621
Robert Griebl52e8d062002-05-14 23:42:08 +0000622 while ( list ) {
Paul Fox8eeb6552005-08-04 18:33:36 +0000623 argc = 0;
Rob Landleye9190962005-12-12 19:38:44 +0000624 if( ENABLE_FEATURE_CLEAN_UP )
625 argc_malloc = 0;
626 /* If CONFIG_FEATURE_CLEAN_UP is not defined, then we leak memory
627 * each time we allocate memory for argv.
628 * But it is (quite) small amounts of memory that leak each
629 * time a module is loaded, and it is reclaimed when modprobe
630 * exits anyway (even when standalone shell?).
631 * This could become a problem when loading a module with LOTS of
632 * dependencies, with LOTS of options for each dependencies, with
633 * very little memory on the target... But in that case, the module
634 * would not load because there is no more memory, so there's no
635 * problem. */
636 /* enough for minimal insmod (5 args + NULL) or rmmod (3 args + NULL) */
637 argv = (char**) malloc( 6 * sizeof( char* ) );
Glenn L McGrath350733a2003-09-08 00:32:49 +0000638 if ( do_insert ) {
Rob Landley79e1cab2005-11-15 00:08:29 +0000639 if (already_loaded (list->m_name) != 1) {
Paul Fox8eeb6552005-08-04 18:33:36 +0000640 argv[argc++] = "insmod";
Rob Landleyd7605602006-06-14 01:51:16 +0000641 if (ENABLE_FEATURE_2_4_MODULES) {
642 if (do_syslog)
643 argv[argc++] = "-s";
644 if (autoclean)
645 argv[argc++] = "-k";
646 if (quiet)
647 argv[argc++] = "-q";
648 else if(verbose) /* verbose and quiet are mutually exclusive */
649 argv[argc++] = "-v";
650 }
Paul Fox8eeb6552005-08-04 18:33:36 +0000651 argv[argc++] = list-> m_path;
Rob Landleye9190962005-12-12 19:38:44 +0000652 if( ENABLE_FEATURE_CLEAN_UP )
653 argc_malloc = argc;
Rob Landley79e1cab2005-11-15 00:08:29 +0000654 opts = list-> m_options;
Rob Landleye9190962005-12-12 19:38:44 +0000655 while( opts ) {
656 /* Add one more option */
Rob Landley79e1cab2005-11-15 00:08:29 +0000657 argc++;
658 argv = (char**) xrealloc( argv, ( argc + 1 ) * sizeof( char* ) );
Rob Landleye9190962005-12-12 19:38:44 +0000659 argv[argc-1] = opts-> m_opt_val;
660 opts = opts-> m_next;
Rob Landley79e1cab2005-11-15 00:08:29 +0000661 }
Paul Fox8eeb6552005-08-04 18:33:36 +0000662 }
Glenn L McGrath350733a2003-09-08 00:32:49 +0000663 } else {
Eric Andersen807bd842004-08-19 18:30:31 +0000664 /* modutils uses short name for removal */
Rob Landley79e1cab2005-11-15 00:08:29 +0000665 if (already_loaded (list->m_name) != 0) {
Paul Fox8eeb6552005-08-04 18:33:36 +0000666 argv[argc++] = "rmmod";
667 if (do_syslog)
668 argv[argc++] = "-s";
669 argv[argc++] = list->m_name;
Rob Landleye9190962005-12-12 19:38:44 +0000670 if( ENABLE_FEATURE_CLEAN_UP )
671 argc_malloc = argc;
Paul Fox8eeb6552005-08-04 18:33:36 +0000672 }
Glenn L McGrath350733a2003-09-08 00:32:49 +0000673 }
Paul Fox8eeb6552005-08-04 18:33:36 +0000674 argv[argc] = NULL;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000675
Paul Fox8eeb6552005-08-04 18:33:36 +0000676 if (argc) {
Glenn L McGrathfcf47322004-08-11 05:56:30 +0000677 if (verbose) {
Rob Landleye9190962005-12-12 19:38:44 +0000678 printf("%s module %s\n", do_insert?"Loading":"Unloading", list-> m_name );
Glenn L McGrathfcf47322004-08-11 05:56:30 +0000679 }
680 if (!show_only) {
Rob Landleyd921b2e2006-08-03 15:41:12 +0000681 int rc2 = wait4pid(spawn(argv));
Rob Landley4b5827a2006-08-22 23:50:11 +0000682
Glenn L McGrathfcf47322004-08-11 05:56:30 +0000683 if (do_insert) {
684 rc = rc2; /* only last module matters */
685 }
686 else if (!rc2) {
687 rc = 0; /* success if remove any mod */
688 }
689 }
Rob Landleye9190962005-12-12 19:38:44 +0000690 if( ENABLE_FEATURE_CLEAN_UP )
691 /* the last value in the array has index == argc, but
692 * it is the terminating NULL, so we must not free it. */
693 while( argc_malloc < argc ) {
694 free( argv[argc_malloc++] );
Rob Landley79e1cab2005-11-15 00:08:29 +0000695 }
Eric Andersen908e3622003-06-20 09:56:37 +0000696 }
Rob Landleye9190962005-12-12 19:38:44 +0000697 if( ENABLE_FEATURE_CLEAN_UP ) {
698 free( argv );
699 argv = NULL;
Rob Landley79e1cab2005-11-15 00:08:29 +0000700 }
Robert Griebl52e8d062002-05-14 23:42:08 +0000701 list = do_insert ? list-> m_prev : list-> m_next;
Eric Andersen60e56f52002-04-26 06:04:01 +0000702 }
Eric Andersen908e3622003-06-20 09:56:37 +0000703 return (show_only) ? 0 : rc;
Eric Andersen60e56f52002-04-26 06:04:01 +0000704}
705
Rob Landleye9190962005-12-12 19:38:44 +0000706/*
Rob Landleybf30c692006-07-20 17:36:18 +0000707 * Check the matching between a pattern and a module name.
708 * We need this as *_* is equivalent to *-*, even in pattern matching.
709 */
710static int check_pattern( const char* pat_src, const char* mod_src ) {
711 int ret;
712
713 if (ENABLE_FEATURE_MODPROBE_FANCY_ALIAS) {
714 char* pat;
715 char* mod;
716 char* p;
717
Rob Landleyd921b2e2006-08-03 15:41:12 +0000718 pat = xstrdup (pat_src);
719 mod = xstrdup (mod_src);
Rob Landleybf30c692006-07-20 17:36:18 +0000720
721 for (p = pat; (p = strchr(p, '-')); *p++ = '_' );
722 for (p = mod; (p = strchr(p, '-')); *p++ = '_' );
723
724 ret = fnmatch ( pat, mod, 0 );
725
726 if (ENABLE_FEATURE_CLEAN_UP) {
727 free (pat);
728 free (mod);
729 }
730
731 return ret;
732 } else {
733 return fnmatch ( pat_src, mod_src, 0 );
734 }
735}
736
737/*
Rob Landleye9190962005-12-12 19:38:44 +0000738 * Builds the dependency list (aka stack) of a module.
739 * head: the highest module in the stack (last to insmod, first to rmmod)
740 * tail: the lowest module in the stack (first to insmod, last to rmmod)
741 */
Robert Griebl52e8d062002-05-14 23:42:08 +0000742static void check_dep ( char *mod, struct mod_list_t **head, struct mod_list_t **tail )
Eric Andersen60e56f52002-04-26 06:04:01 +0000743{
Robert Griebl52e8d062002-05-14 23:42:08 +0000744 struct mod_list_t *find;
Eric Andersen60e56f52002-04-26 06:04:01 +0000745 struct dep_t *dt;
Rob Landleye9190962005-12-12 19:38:44 +0000746 struct mod_opt_t *opt = 0;
Eric Andersen807bd842004-08-19 18:30:31 +0000747 char *path = 0;
Robert Griebl52e8d062002-05-14 23:42:08 +0000748
Bernhard Reutner-Fischer2c351a82006-06-03 19:08:49 +0000749 /* Search for the given module name amongst all dependency rules.
750 * The module name in a dependency rule can be a shell pattern,
751 * so try to match the given module name against such a pattern.
752 * Of course if the name in the dependency rule is a plain string,
753 * then we consider it a pattern, and matching will still work. */
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000754 for ( dt = depend; dt; dt = dt-> m_next ) {
Rob Landleybf30c692006-07-20 17:36:18 +0000755 if ( check_pattern ( dt-> m_name, mod ) == 0) {
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000756 break;
Robert Grieblaead70b2002-07-26 15:54:20 +0000757 }
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000758 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000759
Rob Landleye9190962005-12-12 19:38:44 +0000760 if( !dt ) {
761 bb_error_msg ("module %s not found.", mod);
762 return;
763 }
764
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000765 // resolve alias names
Rob Landleye9190962005-12-12 19:38:44 +0000766 while ( dt-> m_isalias ) {
Robert Grieblaead70b2002-07-26 15:54:20 +0000767 if ( dt-> m_depcnt == 1 ) {
768 struct dep_t *adt;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000769
Robert Grieblaead70b2002-07-26 15:54:20 +0000770 for ( adt = depend; adt; adt = adt-> m_next ) {
Rob Landleybf30c692006-07-20 17:36:18 +0000771 if ( check_pattern ( adt-> m_name, dt-> m_deparr [0] ) == 0 )
Robert Grieblaead70b2002-07-26 15:54:20 +0000772 break;
Robert Grieblaead70b2002-07-26 15:54:20 +0000773 }
Robert Griebl70112da2002-07-29 20:28:38 +0000774 if ( adt ) {
Rob Landleye9190962005-12-12 19:38:44 +0000775 /* This is the module we are aliased to */
776 struct mod_opt_t *opts = dt-> m_options;
777 /* Option of the alias are appended to the options of the module */
778 while( opts ) {
779 adt-> m_options = append_option( adt-> m_options, opts-> m_opt_val );
780 opts = opts-> m_next;
781 }
Eric Andersen716ccb22004-01-10 11:29:31 +0000782 dt = adt;
Robert Griebl70112da2002-07-29 20:28:38 +0000783 }
Rob Landleye9190962005-12-12 19:38:44 +0000784 else {
785 bb_error_msg ("module %s not found.", mod);
Robert Griebl70112da2002-07-29 20:28:38 +0000786 return;
Rob Landleye9190962005-12-12 19:38:44 +0000787 }
Robert Grieblaead70b2002-07-26 15:54:20 +0000788 }
Rob Landleye9190962005-12-12 19:38:44 +0000789 else {
790 bb_error_msg ("Bad alias %s", dt-> m_name);
Eric Andersen716ccb22004-01-10 11:29:31 +0000791 return;
Rob Landleye9190962005-12-12 19:38:44 +0000792 }
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000793 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000794
Rob Landleye9190962005-12-12 19:38:44 +0000795 mod = dt-> m_name;
796 path = dt-> m_path;
797 opt = dt-> m_options;
Bernhard Reutner-Fischer17d355c2005-12-14 08:32:44 +0000798
Robert Griebl52e8d062002-05-14 23:42:08 +0000799 // search for duplicates
800 for ( find = *head; find; find = find-> m_next ) {
Eric Andersen807bd842004-08-19 18:30:31 +0000801 if ( !strcmp ( mod, find-> m_name )) {
Robert Griebl52e8d062002-05-14 23:42:08 +0000802 // found -> dequeue it
803
804 if ( find-> m_prev )
805 find-> m_prev-> m_next = find-> m_next;
806 else
807 *head = find-> m_next;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000808
Robert Griebl52e8d062002-05-14 23:42:08 +0000809 if ( find-> m_next )
810 find-> m_next-> m_prev = find-> m_prev;
811 else
812 *tail = find-> m_prev;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000813
Robert Griebl52e8d062002-05-14 23:42:08 +0000814 break; // there can be only one duplicate
Eric Andersen716ccb22004-01-10 11:29:31 +0000815 }
Robert Griebl52e8d062002-05-14 23:42:08 +0000816 }
817
818 if ( !find ) { // did not find a duplicate
Eric Andersen716ccb22004-01-10 11:29:31 +0000819 find = (struct mod_list_t *) xmalloc ( sizeof(struct mod_list_t));
Eric Andersen807bd842004-08-19 18:30:31 +0000820 find-> m_name = mod;
821 find-> m_path = path;
Robert Grieblaead70b2002-07-26 15:54:20 +0000822 find-> m_options = opt;
Robert Griebl52e8d062002-05-14 23:42:08 +0000823 }
824
Eric Andersen716ccb22004-01-10 11:29:31 +0000825 // enqueue at tail
Robert Griebl52e8d062002-05-14 23:42:08 +0000826 if ( *tail )
827 (*tail)-> m_next = find;
828 find-> m_prev = *tail;
829 find-> m_next = 0;
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000830
Robert Griebl52e8d062002-05-14 23:42:08 +0000831 if ( !*head )
832 *head = find;
833 *tail = find;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000834
Eric Andersen716ccb22004-01-10 11:29:31 +0000835 if ( dt ) {
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000836 int i;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000837
Rob Landleye9190962005-12-12 19:38:44 +0000838 /* Add all dependable module for that new module */
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000839 for ( i = 0; i < dt-> m_depcnt; i++ )
840 check_dep ( dt-> m_deparr [i], head, tail );
841 }
Robert Griebl52e8d062002-05-14 23:42:08 +0000842}
843
Robert Griebl236abbf2002-05-22 23:34:35 +0000844static int mod_insert ( char *mod, int argc, char **argv )
Robert Griebl52e8d062002-05-14 23:42:08 +0000845{
846 struct mod_list_t *tail = 0;
Eric Andersen716ccb22004-01-10 11:29:31 +0000847 struct mod_list_t *head = 0;
Eric Andersen908e3622003-06-20 09:56:37 +0000848 int rc;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000849
Robert Griebl52e8d062002-05-14 23:42:08 +0000850 // get dep list for module mod
851 check_dep ( mod, &head, &tail );
Eric Andersen3b1a7442003-12-24 20:30:45 +0000852
Robert Griebl52e8d062002-05-14 23:42:08 +0000853 if ( head && tail ) {
Rob Landleye9190962005-12-12 19:38:44 +0000854 if( argc ) {
Eric Andersen716ccb22004-01-10 11:29:31 +0000855 int i;
Robert Grieblaead70b2002-07-26 15:54:20 +0000856 // append module args
Eric Andersen716ccb22004-01-10 11:29:31 +0000857 for ( i = 0; i < argc; i++ )
Rob Landleye9190962005-12-12 19:38:44 +0000858 head->m_options = append_option( head->m_options, argv[i] );
Robert Griebl52e8d062002-05-14 23:42:08 +0000859 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000860
Robert Griebl52e8d062002-05-14 23:42:08 +0000861 // process tail ---> head
Rob Landley4b5827a2006-08-22 23:50:11 +0000862 if ((rc = mod_process ( tail, 1 )) != 0) {
863 /*
864 * In case of using udev, multiple instances of modprobe can be
865 * spawned to load the same module (think of two same usb devices,
866 * for example; or cold-plugging at boot time). Thus we shouldn't
867 * fail if the module was loaded, and not by us.
868 */
869 if (already_loaded (mod) )
870 rc = 0;
871 }
Robert Griebl52e8d062002-05-14 23:42:08 +0000872 }
873 else
874 rc = 1;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000875
Robert Griebl52e8d062002-05-14 23:42:08 +0000876 return rc;
877}
878
Eric Andersen908e3622003-06-20 09:56:37 +0000879static int mod_remove ( char *mod )
Robert Griebl52e8d062002-05-14 23:42:08 +0000880{
Eric Andersen908e3622003-06-20 09:56:37 +0000881 int rc;
"Vladimir N. Oleynik"4fc92202006-02-02 14:48:54 +0000882 static struct mod_list_t rm_a_dummy = { "-a", NULL, NULL, NULL, NULL };
Eric Andersen3b1a7442003-12-24 20:30:45 +0000883
Robert Griebl52e8d062002-05-14 23:42:08 +0000884 struct mod_list_t *head = 0;
885 struct mod_list_t *tail = 0;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000886
Robert Griebl52e8d062002-05-14 23:42:08 +0000887 if ( mod )
888 check_dep ( mod, &head, &tail );
889 else // autoclean
890 head = tail = &rm_a_dummy;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000891
Robert Griebl236abbf2002-05-22 23:34:35 +0000892 if ( head && tail )
Eric Andersen908e3622003-06-20 09:56:37 +0000893 rc = mod_process ( head, 0 ); // process head ---> tail
894 else
895 rc = 1;
896 return rc;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000897
Robert Griebl52e8d062002-05-14 23:42:08 +0000898}
899
Rob Landleydfba7412006-03-06 20:47:33 +0000900int modprobe_main(int argc, char** argv)
Eric Andersen0139ca92001-07-22 23:01:03 +0000901{
Rob Landleye9190962005-12-12 19:38:44 +0000902 int rc = EXIT_SUCCESS;
"Vladimir N. Oleynik"4fc92202006-02-02 14:48:54 +0000903 char *unused;
Eric Andersen0139ca92001-07-22 23:01:03 +0000904
"Vladimir N. Oleynik"4fc92202006-02-02 14:48:54 +0000905 bb_opt_complementally = "?V-:q-v:v-q";
906 main_opts = bb_getopt_ulflags(argc, argv, "acdklnqrst:vVC:",
907 &unused, &unused);
908 if((main_opts & (DUMP_CONF_EXIT | LIST_ALL)))
Eric Andersen3b1a7442003-12-24 20:30:45 +0000909 return EXIT_SUCCESS;
"Vladimir N. Oleynik"4fc92202006-02-02 14:48:54 +0000910 if((main_opts & (RESTRICT_DIR | CONFIG_FILE)))
Eric Andersen3b1a7442003-12-24 20:30:45 +0000911 bb_error_msg_and_die("-t and -C not supported");
Robert Griebl236abbf2002-05-22 23:34:35 +0000912
Eric Andersen716ccb22004-01-10 11:29:31 +0000913 depend = build_dep ( );
Robert Griebl52e8d062002-05-14 23:42:08 +0000914
Eric Andersen716ccb22004-01-10 11:29:31 +0000915 if ( !depend )
Bernhard Reutner-Fischerdeda6a52006-06-03 19:35:15 +0000916 bb_error_msg_and_die ( "could not parse modules.dep" );
Eric Andersen3b1a7442003-12-24 20:30:45 +0000917
Eric Andersen1b064192001-07-25 07:23:38 +0000918 if (remove_opt) {
Eric Andersen0139ca92001-07-22 23:01:03 +0000919 do {
Eric Andersen908e3622003-06-20 09:56:37 +0000920 if (mod_remove ( optind < argc ?
"Vladimir N. Oleynik"4fc92202006-02-02 14:48:54 +0000921 argv [optind] : NULL )) {
Eric Andersen908e3622003-06-20 09:56:37 +0000922 bb_error_msg ("failed to remove module %s",
Eric Andersen3b1a7442003-12-24 20:30:45 +0000923 argv [optind] );
Eric Andersen908e3622003-06-20 09:56:37 +0000924 rc = EXIT_FAILURE;
925 }
Robert Griebl52e8d062002-05-14 23:42:08 +0000926 } while ( ++optind < argc );
Rob Landleye9190962005-12-12 19:38:44 +0000927 } else {
928 if (optind >= argc)
Bernhard Reutner-Fischerdeda6a52006-06-03 19:35:15 +0000929 bb_error_msg_and_die ( "No module or pattern provided" );
Eric Andersen3b1a7442003-12-24 20:30:45 +0000930
"Vladimir N. Oleynik"4fc92202006-02-02 14:48:54 +0000931 if ( mod_insert ( argv [optind], argc - optind - 1, argv + optind + 1 ))
Rob Landleye9190962005-12-12 19:38:44 +0000932 bb_error_msg_and_die ( "failed to load module %s", argv [optind] );
Eric Andersen0139ca92001-07-22 23:01:03 +0000933 }
934
Rob Landleye9190962005-12-12 19:38:44 +0000935 /* Here would be a good place to free up memory allocated during the dependencies build. */
Eric Andersen3b1a7442003-12-24 20:30:45 +0000936
Rob Landleye9190962005-12-12 19:38:44 +0000937 return rc;
Eric Andersen0139ca92001-07-22 23:01:03 +0000938}