blob: 844eb99c5ee3d36a95b3939ca9158fa62a2b4697 [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
Robert Griebl52e8d062002-05-14 23:42:08 +000014#include <sys/utsname.h>
Paul Fox8eeb6552005-08-04 18:33:36 +000015#include <sys/types.h>
16#include <sys/wait.h>
Eric Andersen0139ca92001-07-22 23:01:03 +000017#include <getopt.h>
18#include <stdlib.h>
19#include <unistd.h>
20#include <syslog.h>
21#include <string.h>
Eric Andersen60e56f52002-04-26 06:04:01 +000022#include <ctype.h>
Eric Andersenc06391b2002-06-04 13:28:43 +000023#include <fcntl.h>
Eric Andersen0139ca92001-07-22 23:01:03 +000024#include "busybox.h"
25
Rob Landleye9190962005-12-12 19:38:44 +000026struct mod_opt_t { /* one-way list of options to pass to a module */
27 char * m_opt_val;
28 struct mod_opt_t * m_next;
Eric Andersen60e56f52002-04-26 06:04:01 +000029};
30
Rob Landleye9190962005-12-12 19:38:44 +000031struct dep_t { /* one-way list of dependency rules */
32 /* a dependency rule */
33 char * m_name; /* the module name*/
34 char * m_path; /* the module file path */
35 struct mod_opt_t * m_options; /* the module options */
Bernhard Reutner-Fischer17d355c2005-12-14 08:32:44 +000036
Rob Landleye9190962005-12-12 19:38:44 +000037 int m_isalias : 1; /* the module is an alias */
38 int m_reserved : 15; /* stuffin' */
39
40 int m_depcnt : 16; /* the number of dependable module(s) */
41 char ** m_deparr; /* the list of dependable module(s) */
42
43 struct dep_t * m_next; /* the next dependency rule */
44};
45
46struct mod_list_t { /* two-way list of modules to process */
47 /* a module description */
Eric Andersen807bd842004-08-19 18:30:31 +000048 char * m_name;
49 char * m_path;
Rob Landleye9190962005-12-12 19:38:44 +000050 struct mod_opt_t * m_options;
Eric Andersen3b1a7442003-12-24 20:30:45 +000051
Robert Griebl52e8d062002-05-14 23:42:08 +000052 struct mod_list_t * m_prev;
53 struct mod_list_t * m_next;
54};
55
56
Robert Griebl236abbf2002-05-22 23:34:35 +000057static struct dep_t *depend;
58static int autoclean, show_only, quiet, do_syslog, verbose;
Robert Griebl52e8d062002-05-14 23:42:08 +000059
Eric Andersen14f5c8d2005-04-16 19:39:00 +000060static int parse_tag_value ( char *buffer, char **ptag, char **pvalue )
Robert Grieblaead70b2002-07-26 15:54:20 +000061{
62 char *tag, *value;
63
64 while ( isspace ( *buffer ))
Eric Andersen716ccb22004-01-10 11:29:31 +000065 buffer++;
Robert Grieblaead70b2002-07-26 15:54:20 +000066 tag = value = buffer;
67 while ( !isspace ( *value ))
Eric Andersen7e496a72004-04-06 12:06:03 +000068 if (!*value) return 0;
69 else value++;
Robert Grieblaead70b2002-07-26 15:54:20 +000070 *value++ = 0;
71 while ( isspace ( *value ))
72 value++;
Eric Andersen7e496a72004-04-06 12:06:03 +000073 if (!*value) return 0;
Robert Grieblaead70b2002-07-26 15:54:20 +000074
75 *ptag = tag;
76 *pvalue = value;
Eric Andersen3b1a7442003-12-24 20:30:45 +000077
Eric Andersen7e496a72004-04-06 12:06:03 +000078 return 1;
Robert Grieblaead70b2002-07-26 15:54:20 +000079}
Eric Andersen60e56f52002-04-26 06:04:01 +000080
Robert Grieblbc28f7a2002-06-04 19:33:58 +000081/* Jump through hoops to simulate how fgets() grabs just one line at a
82 * time... Don't use any stdio since modprobe gets called from a kernel
Eric Andersen716ccb22004-01-10 11:29:31 +000083 * thread and stdio junk can overflow the limited stack...
Robert Grieblbc28f7a2002-06-04 19:33:58 +000084 */
85static char *reads ( int fd, char *buffer, size_t len )
86{
87 int n = read ( fd, buffer, len );
Eric Andersen3b1a7442003-12-24 20:30:45 +000088
Robert Grieblbc28f7a2002-06-04 19:33:58 +000089 if ( n > 0 ) {
90 char *p;
Eric Andersen3b1a7442003-12-24 20:30:45 +000091
Robert Grieblbc28f7a2002-06-04 19:33:58 +000092 buffer [len-1] = 0;
93 p = strchr ( buffer, '\n' );
Eric Andersen3b1a7442003-12-24 20:30:45 +000094
Robert Grieblbc28f7a2002-06-04 19:33:58 +000095 if ( p ) {
96 off_t offset;
Eric Andersen3b1a7442003-12-24 20:30:45 +000097
Eric Andersen716ccb22004-01-10 11:29:31 +000098 offset = lseek ( fd, 0L, SEEK_CUR ); // Get the current file descriptor offset
Robert Grieblbc28f7a2002-06-04 19:33:58 +000099 lseek ( fd, offset-n + (p-buffer) + 1, SEEK_SET ); // Set the file descriptor offset to right after the \n
100
101 p[1] = 0;
102 }
103 return buffer;
104 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000105
Robert Grieblbc28f7a2002-06-04 19:33:58 +0000106 else
107 return 0;
108}
109
Rob Landleye9190962005-12-12 19:38:44 +0000110/*
111 * This function appends an option to a list
112 */
113struct mod_opt_t *append_option( struct mod_opt_t *opt_list, char *opt )
Eric Andersen60e56f52002-04-26 06:04:01 +0000114{
Rob Landleye9190962005-12-12 19:38:44 +0000115 struct mod_opt_t *ol = opt_list;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000116
Rob Landleye9190962005-12-12 19:38:44 +0000117 if( ol ) {
118 while( ol-> m_next ) {
119 ol = ol-> m_next;
120 }
121 ol-> m_next = xmalloc( sizeof( struct mod_opt_t ) );
122 ol = ol-> m_next;
123 } else {
124 ol = opt_list = xmalloc( sizeof( struct mod_opt_t ) );
Eric Andersen03d80912003-12-19 21:04:19 +0000125 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000126
Rob Landleye9190962005-12-12 19:38:44 +0000127 ol-> m_opt_val = bb_xstrdup( opt );
128 ol-> m_next = NULL;
Eric Andersen60e56f52002-04-26 06:04:01 +0000129
Rob Landleye9190962005-12-12 19:38:44 +0000130 return opt_list;
Eric Andersen60e56f52002-04-26 06:04:01 +0000131}
132
Bernhard Reutner-Fischer17d355c2005-12-14 08:32:44 +0000133#if ENABLE_FEATURE_MODPROBE_MULTIPLE_OPTIONS
Rob Landley79e1cab2005-11-15 00:08:29 +0000134/* static char* parse_command_string( char* src, char **dst );
135 * src: pointer to string containing argument
136 * dst: pointer to where to store the parsed argument
137 * return value: the pointer to the first char after the parsed argument,
138 * NULL if there was no argument parsed (only trailing spaces).
139 * Note that memory is allocated with bb_xstrdup when a new argument was
140 * parsed. Don't forget to free it!
141 */
142#define ARG_EMPTY 0x00
143#define ARG_IN_DQUOTES 0x01
144#define ARG_IN_SQUOTES 0x02
145static char *parse_command_string( char *src, char **dst )
146{
147 int opt_status = ARG_EMPTY;
148 char* tmp_str;
149
150 /* Dumb you, I have nothing to do... */
151 if( src == NULL ) return src;
152
153 /* Skip leading spaces */
154 while( *src == ' ' ) {
155 src++;
156 }
157 /* Is the end of string reached? */
158 if( *src == '\0' ) {
159 return NULL;
160 }
161 /* Reached the start of an argument
Rob Landleye9190962005-12-12 19:38:44 +0000162 * By the way, we duplicate a little too much
163 * here but what is too much is freed later. */
Rob Landley79e1cab2005-11-15 00:08:29 +0000164 *dst = tmp_str = bb_xstrdup( src );
165 /* Get to the end of that argument */
166 while( ( *tmp_str != '\0' )
Rob Landleye9190962005-12-12 19:38:44 +0000167 && ( ( *tmp_str != ' ' )
168 || ( opt_status & ( ARG_IN_DQUOTES | ARG_IN_SQUOTES ) ) ) ) {
Rob Landley79e1cab2005-11-15 00:08:29 +0000169 switch( *tmp_str ) {
170 case '\'':
171 if( opt_status & ARG_IN_DQUOTES ) {
172 /* Already in double quotes, keep current char as is */
173 } else {
174 /* shift left 1 char, until end of string: get rid of the opening/closing quotes */
175 memmove( tmp_str, tmp_str + 1, strlen( tmp_str ) );
176 /* mark me: we enter or leave single quotes */
177 opt_status ^= ARG_IN_SQUOTES;
178 /* Back one char, as we need to re-scan the new char there. */
179 tmp_str--;
180 }
181 break;
182 case '"':
183 if( opt_status & ARG_IN_SQUOTES ) {
184 /* Already in single quotes, keep current char as is */
185 } else {
186 /* shift left 1 char, until end of string: get rid of the opening/closing quotes */
187 memmove( tmp_str, tmp_str + 1, strlen( tmp_str ) );
188 /* mark me: we enter or leave double quotes */
189 opt_status ^= ARG_IN_DQUOTES;
190 /* Back one char, as we need to re-scan the new char there. */
191 tmp_str--;
192 }
193 break;
194 case '\\':
195 if( opt_status & ARG_IN_SQUOTES ) {
196 /* Between single quotes: keep as is. */
197 } else {
198 switch( *(tmp_str+1) ) {
199 case 'a':
200 case 'b':
201 case 't':
202 case 'n':
203 case 'v':
204 case 'f':
205 case 'r':
206 case '0':
207 /* We escaped a special character. For now, keep
208 * both the back-slash and the following char. */
209 tmp_str++; src++;
210 break;
211 default:
212 /* We escaped a space or a single or double quote,
213 * or a back-slash, or a non-escapable char. Remove
214 * the '\' and keep the new current char as is. */
215 memmove( tmp_str, tmp_str + 1, strlen( tmp_str ) );
216 break;
217 }
218 }
219 break;
220 /* Any other char that is special shall appear here.
221 * Example: $ starts a variable
222 case '$':
223 do_variable_expansion();
224 break;
225 * */
226 default:
227 /* any other char is kept as is. */
228 break;
229 }
230 tmp_str++; /* Go to next char */
231 src++; /* Go to next char to find the end of the argument. */
232 }
233 /* End of string, but still no ending quote */
234 if( opt_status & ( ARG_IN_DQUOTES | ARG_IN_SQUOTES ) ) {
235 bb_error_msg_and_die( "unterminated (single or double) quote in options list: %s", src );
236 }
237 *tmp_str = '\0';
Rob Landleye9190962005-12-12 19:38:44 +0000238 *dst = xrealloc( *dst, strlen( *dst ) );
Rob Landley79e1cab2005-11-15 00:08:29 +0000239 return src;
240}
Rob Landleyae50c6d2005-12-15 07:42:13 +0000241#else
242#define parse_command_string(src, dst) (0)
Bernhard Reutner-Fischer17d355c2005-12-14 08:32:44 +0000243#endif /* ENABLE_FEATURE_MODPROBE_MULTIPLE_OPTIONS */
Rob Landley79e1cab2005-11-15 00:08:29 +0000244
Rob Landleye9190962005-12-12 19:38:44 +0000245/*
246 * This function builds a list of dependency rules from /lib/modules/`uname -r\modules.dep.
247 * It then fills every modules and aliases with their default options, found by parsing
248 * modprobe.conf (or modules.conf, or conf.modules).
249 */
250static struct dep_t *build_dep ( void )
251{
252 int fd;
253 struct utsname un;
254 struct dep_t *first = 0;
255 struct dep_t *current = 0;
256 char buffer[2048];
257 char *filename = buffer;
258 int continuation_line = 0;
259 int k_version;
260
261 k_version = 0;
262 if ( uname ( &un ))
263 bb_error_msg_and_die("can't determine kernel version");
264
265 // check for buffer overflow in following code
266 if ( bb_strlen ( un.release ) > ( sizeof( buffer ) - 64 )) {
267 return 0;
268 }
269 if (un.release[0] == '2') {
270 k_version = un.release[2] - '0';
271 }
272
273 strcpy ( filename, "/lib/modules/" );
274 strcat ( filename, un.release );
275 strcat ( filename, "/modules.dep" );
276
277 if (( fd = open ( filename, O_RDONLY )) < 0 ) {
278
279 /* Ok, that didn't work. Fall back to looking in /lib/modules */
280 if (( fd = open ( "/lib/modules/modules.dep", O_RDONLY )) < 0 ) {
281 return 0;
282 }
283 }
284
285 while ( reads ( fd, buffer, sizeof( buffer ))) {
286 int l = bb_strlen ( buffer );
287 char *p = 0;
288
289 while ( isspace ( buffer [l-1] )) {
290 buffer [l-1] = 0;
291 l--;
292 }
293
294 if ( l == 0 ) {
295 continuation_line = 0;
296 continue;
297 }
298
299 /* Is this a new module dep description? */
300 if ( !continuation_line ) {
Bernhard Reutner-Fischer17d355c2005-12-14 08:32:44 +0000301 /* find the dep beginning */
Rob Landleye9190962005-12-12 19:38:44 +0000302 char *col = strchr ( buffer, ':' );
303 char *dot = col;
304
305 if ( col ) {
306 /* This line is a dep description */
307 char *mods;
308 char *modpath;
309 char *mod;
310
311 /* Find the beginning of the module file name */
312 *col = 0;
313 mods = strrchr ( buffer, '/' );
314
315 if ( !mods )
316 mods = buffer; /* no path for this module */
317 else
318 mods++; /* there was a path for this module... */
319
320 /* find the path of the module */
321 modpath = strchr ( buffer, '/' ); /* ... and this is the path */
322 if ( !modpath )
323 modpath = buffer; /* module with no path */
324 /* find the end of the module name in the file name */
325 if ( ENABLE_FEATURE_2_6_MODULES &&
326 (k_version > 4) && ( *(col-3) == '.' ) &&
327 ( *(col-2) == 'k' ) && ( *(col-1) == 'o' ) )
328 dot = col - 3;
329 else
330 if (( *(col-2) == '.' ) && ( *(col-1) == 'o' ))
331 dot = col - 2;
332
333 mod = bb_xstrndup ( mods, dot - mods );
334
335 /* enqueue new module */
336 if ( !current ) {
337 first = current = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
338 }
339 else {
340 current-> m_next = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
341 current = current-> m_next;
342 }
343 current-> m_name = mod;
344 current-> m_path = bb_xstrdup(modpath);
345 current-> m_options = NULL;
346 current-> m_isalias = 0;
347 current-> m_depcnt = 0;
348 current-> m_deparr = 0;
349 current-> m_next = 0;
350
351 p = col + 1;
352 }
353 else
354 /* this line is not a dep description */
355 p = 0;
356 }
357 else
358 /* It's a dep description continuation */
359 p = buffer;
360
361 while ( p && *p && isblank(*p))
362 p++;
363
364 /* p points to the first dependable module; if NULL, no dependable module */
365 if ( p && *p ) {
366 char *end = &buffer [l-1];
367 char *deps;
368 char *dep;
369 char *next;
370 int ext = 0;
371
372 while ( isblank ( *end ) || ( *end == '\\' ))
373 end--;
374
375 do
376 {
377 /* search the end of the dependency */
378 next = strchr (p, ' ' );
379 if (next)
380 {
381 *next = 0;
382 next--;
383 }
384 else
385 next = end;
386
Bernhard Reutner-Fischer17d355c2005-12-14 08:32:44 +0000387 /* find the beginning of the module file name */
Rob Landleye9190962005-12-12 19:38:44 +0000388 deps = strrchr ( p, '/' );
389
390 if ( !deps || ( deps < p )) {
391 deps = p;
392
393 while ( isblank ( *deps ))
394 deps++;
395 }
396 else
397 deps++;
398
399 /* find the end of the module name in the file name */
400 if ( ENABLE_FEATURE_2_6_MODULES &&
401 (k_version > 4) && ( *(next-2) == '.' ) &&
402 ( *(next-1) == 'k' ) && ( *next == 'o' ) )
403 ext = 3;
404 else
405 if (( *(next-1) == '.' ) && ( *next == 'o' ))
406 ext = 2;
407
408 /* Cope with blank lines */
409 if ((next-deps-ext+1) <= 0)
410 continue;
411 dep = bb_xstrndup ( deps, next - deps - ext + 1 );
412
413 /* Add the new dependable module name */
414 current-> m_depcnt++;
415 current-> m_deparr = (char **) xrealloc ( current-> m_deparr,
416 sizeof ( char *) * current-> m_depcnt );
417 current-> m_deparr [current-> m_depcnt - 1] = dep;
418
419 p = next + 2;
420 } while (next < end);
421 }
422
423 /* is there other dependable module(s) ? */
424 if ( buffer [l-1] == '\\' )
425 continuation_line = 1;
426 else
427 continuation_line = 0;
428 }
429 close ( fd );
430
431 // alias parsing is not 100% correct (no correct handling of continuation lines within an alias) !
432
Rob Landleyae50c6d2005-12-15 07:42:13 +0000433 if (!ENABLE_FEATURE_2_6_MODULES
434 || ( fd = open ( "/etc/modprobe.conf", O_RDONLY )) < 0 )
Rob Landleye9190962005-12-12 19:38:44 +0000435 if (( fd = open ( "/etc/modules.conf", O_RDONLY )) < 0 )
436 if (( fd = open ( "/etc/conf.modules", O_RDONLY )) < 0 )
437 return first;
438
439 continuation_line = 0;
440 while ( reads ( fd, buffer, sizeof( buffer ))) {
441 int l;
442 char *p;
443
444 p = strchr ( buffer, '#' );
445 if ( p )
446 *p = 0;
447
448 l = bb_strlen ( buffer );
449
450 while ( l && isspace ( buffer [l-1] )) {
451 buffer [l-1] = 0;
452 l--;
453 }
454
455 if ( l == 0 ) {
456 continuation_line = 0;
457 continue;
458 }
459
460 if ( !continuation_line ) {
461 if (( strncmp ( buffer, "alias", 5 ) == 0 ) && isspace ( buffer [5] )) {
462 char *alias, *mod;
463
464 if ( parse_tag_value ( buffer + 6, &alias, &mod )) {
465 /* handle alias as a module dependent on the aliased module */
466 if ( !current ) {
467 first = current = (struct dep_t *) xcalloc ( 1, sizeof ( struct dep_t ));
468 }
469 else {
470 current-> m_next = (struct dep_t *) xcalloc ( 1, sizeof ( struct dep_t ));
471 current = current-> m_next;
472 }
473 current-> m_name = bb_xstrdup ( alias );
474 current-> m_isalias = 1;
475
476 if (( strcmp ( mod, "off" ) == 0 ) || ( strcmp ( mod, "null" ) == 0 )) {
477 current-> m_depcnt = 0;
478 current-> m_deparr = 0;
479 }
480 else {
481 current-> m_depcnt = 1;
482 current-> m_deparr = xmalloc ( 1 * sizeof( char * ));
483 current-> m_deparr[0] = bb_xstrdup ( mod );
484 }
485 current-> m_next = 0;
486 }
487 }
488 else if (( strncmp ( buffer, "options", 7 ) == 0 ) && isspace ( buffer [7] )) {
489 char *mod, *opt;
490
491 /* split the line in the module/alias name, and options */
492 if ( parse_tag_value ( buffer + 8, &mod, &opt )) {
493 struct dep_t *dt;
494
495 /* find the corresponding module */
496 for ( dt = first; dt; dt = dt-> m_next ) {
497 if ( strcmp ( dt-> m_name, mod ) == 0 )
498 break;
499 }
Rob Landley199501f2005-12-16 06:18:06 +0000500 if ( dt ) {
501 if ( ENABLE_FEATURE_MODPROBE_MULTIPLE_OPTIONS ) {
502 char* new_opt = NULL;
503 while( ( opt = parse_command_string( opt, &new_opt ) ) ) {
504 dt-> m_options = append_option( dt-> m_options, new_opt );
505 }
506 } else {
507 dt-> m_options = append_option( dt-> m_options, opt );
Rob Landleye9190962005-12-12 19:38:44 +0000508 }
509 }
510 }
511 }
512 }
513 }
514 close ( fd );
515
516 return first;
517}
518
519/* return 1 = loaded, 0 = not loaded, -1 = can't tell */
520static int already_loaded (const char *name)
521{
522 int fd;
523 char buffer[4096];
524
525 fd = open ("/proc/modules", O_RDONLY);
526 if (fd < 0)
527 return -1;
528
529 while ( reads ( fd, buffer, sizeof( buffer ))) {
530 char *p;
531
532 p = strchr (buffer, ' ');
533 if (p) {
534 *p = 0;
535 for( p = buffer; ENABLE_FEATURE_2_6_MODULES && *p; p++ ) {
536 *p = ((*p)=='-')?'_':*p;
537 }
538 if (strcmp (name, buffer) == 0) {
539 close (fd);
540 return 1;
541 }
542 }
543 }
544
545 close (fd);
546 return 0;
547}
548
Robert Griebl236abbf2002-05-22 23:34:35 +0000549static int mod_process ( struct mod_list_t *list, int do_insert )
Eric Andersen60e56f52002-04-26 06:04:01 +0000550{
Eric Andersen44b57582004-08-03 08:23:33 +0000551 int rc = 0;
Rob Landley79e1cab2005-11-15 00:08:29 +0000552 char **argv = NULL;
Rob Landleye9190962005-12-12 19:38:44 +0000553 struct mod_opt_t *opts;
554 int argc_malloc; /* never used when CONFIG_FEATURE_CLEAN_UP not defined */
Paul Fox8eeb6552005-08-04 18:33:36 +0000555 int argc;
Eric Andersen864b7972002-05-03 15:48:26 +0000556
Robert Griebl52e8d062002-05-14 23:42:08 +0000557 while ( list ) {
Paul Fox8eeb6552005-08-04 18:33:36 +0000558 argc = 0;
Rob Landleye9190962005-12-12 19:38:44 +0000559 if( ENABLE_FEATURE_CLEAN_UP )
560 argc_malloc = 0;
561 /* If CONFIG_FEATURE_CLEAN_UP is not defined, then we leak memory
562 * each time we allocate memory for argv.
563 * But it is (quite) small amounts of memory that leak each
564 * time a module is loaded, and it is reclaimed when modprobe
565 * exits anyway (even when standalone shell?).
566 * This could become a problem when loading a module with LOTS of
567 * dependencies, with LOTS of options for each dependencies, with
568 * very little memory on the target... But in that case, the module
569 * would not load because there is no more memory, so there's no
570 * problem. */
571 /* enough for minimal insmod (5 args + NULL) or rmmod (3 args + NULL) */
572 argv = (char**) malloc( 6 * sizeof( char* ) );
Glenn L McGrath350733a2003-09-08 00:32:49 +0000573 if ( do_insert ) {
Rob Landley79e1cab2005-11-15 00:08:29 +0000574 if (already_loaded (list->m_name) != 1) {
Paul Fox8eeb6552005-08-04 18:33:36 +0000575 argv[argc++] = "insmod";
576 if (do_syslog)
577 argv[argc++] = "-s";
578 if (autoclean)
579 argv[argc++] = "-k";
580 if (quiet)
581 argv[argc++] = "-q";
Rob Landley79e1cab2005-11-15 00:08:29 +0000582 else if(verbose) /* verbose and quiet are mutually exclusive */
583 argv[argc++] = "-v";
Paul Fox8eeb6552005-08-04 18:33:36 +0000584 argv[argc++] = list-> m_path;
Rob Landleye9190962005-12-12 19:38:44 +0000585 if( ENABLE_FEATURE_CLEAN_UP )
586 argc_malloc = argc;
Rob Landley79e1cab2005-11-15 00:08:29 +0000587 opts = list-> m_options;
Rob Landleye9190962005-12-12 19:38:44 +0000588 while( opts ) {
589 /* Add one more option */
Rob Landley79e1cab2005-11-15 00:08:29 +0000590 argc++;
591 argv = (char**) xrealloc( argv, ( argc + 1 ) * sizeof( char* ) );
Rob Landleye9190962005-12-12 19:38:44 +0000592 argv[argc-1] = opts-> m_opt_val;
593 opts = opts-> m_next;
Rob Landley79e1cab2005-11-15 00:08:29 +0000594 }
Paul Fox8eeb6552005-08-04 18:33:36 +0000595 }
Glenn L McGrath350733a2003-09-08 00:32:49 +0000596 } else {
Eric Andersen807bd842004-08-19 18:30:31 +0000597 /* modutils uses short name for removal */
Rob Landley79e1cab2005-11-15 00:08:29 +0000598 if (already_loaded (list->m_name) != 0) {
Paul Fox8eeb6552005-08-04 18:33:36 +0000599 argv[argc++] = "rmmod";
600 if (do_syslog)
601 argv[argc++] = "-s";
602 argv[argc++] = list->m_name;
Rob Landleye9190962005-12-12 19:38:44 +0000603 if( ENABLE_FEATURE_CLEAN_UP )
604 argc_malloc = argc;
Paul Fox8eeb6552005-08-04 18:33:36 +0000605 }
Glenn L McGrath350733a2003-09-08 00:32:49 +0000606 }
Paul Fox8eeb6552005-08-04 18:33:36 +0000607 argv[argc] = NULL;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000608
Paul Fox8eeb6552005-08-04 18:33:36 +0000609 if (argc) {
Glenn L McGrathfcf47322004-08-11 05:56:30 +0000610 if (verbose) {
Rob Landleye9190962005-12-12 19:38:44 +0000611 printf("%s module %s\n", do_insert?"Loading":"Unloading", list-> m_name );
Glenn L McGrathfcf47322004-08-11 05:56:30 +0000612 }
613 if (!show_only) {
Paul Fox8eeb6552005-08-04 18:33:36 +0000614 int rc2 = 0;
615 int status;
616 switch (fork()) {
617 case -1:
618 rc2 = 1;
619 break;
620 case 0: //child
621 execvp(argv[0], argv);
622 bb_perror_msg_and_die("exec of %s", argv[0]);
623 /* NOTREACHED */
624 default:
625 if (wait(&status) == -1) {
626 rc2 = 1;
627 break;
628 }
629 if (WIFEXITED(status))
630 rc2 = WEXITSTATUS(status);
631 if (WIFSIGNALED(status))
632 rc2 = WTERMSIG(status);
633 break;
634 }
Glenn L McGrathfcf47322004-08-11 05:56:30 +0000635 if (do_insert) {
636 rc = rc2; /* only last module matters */
637 }
638 else if (!rc2) {
639 rc = 0; /* success if remove any mod */
640 }
641 }
Rob Landleye9190962005-12-12 19:38:44 +0000642 if( ENABLE_FEATURE_CLEAN_UP )
643 /* the last value in the array has index == argc, but
644 * it is the terminating NULL, so we must not free it. */
645 while( argc_malloc < argc ) {
646 free( argv[argc_malloc++] );
Rob Landley79e1cab2005-11-15 00:08:29 +0000647 }
Eric Andersen908e3622003-06-20 09:56:37 +0000648 }
Rob Landleye9190962005-12-12 19:38:44 +0000649 if( ENABLE_FEATURE_CLEAN_UP ) {
650 free( argv );
651 argv = NULL;
Rob Landley79e1cab2005-11-15 00:08:29 +0000652 }
Robert Griebl52e8d062002-05-14 23:42:08 +0000653 list = do_insert ? list-> m_prev : list-> m_next;
Eric Andersen60e56f52002-04-26 06:04:01 +0000654 }
Eric Andersen908e3622003-06-20 09:56:37 +0000655 return (show_only) ? 0 : rc;
Eric Andersen60e56f52002-04-26 06:04:01 +0000656}
657
Rob Landleye9190962005-12-12 19:38:44 +0000658/*
659 * Builds the dependency list (aka stack) of a module.
660 * head: the highest module in the stack (last to insmod, first to rmmod)
661 * tail: the lowest module in the stack (first to insmod, last to rmmod)
662 */
Robert Griebl52e8d062002-05-14 23:42:08 +0000663static void check_dep ( char *mod, struct mod_list_t **head, struct mod_list_t **tail )
Eric Andersen60e56f52002-04-26 06:04:01 +0000664{
Robert Griebl52e8d062002-05-14 23:42:08 +0000665 struct mod_list_t *find;
Eric Andersen60e56f52002-04-26 06:04:01 +0000666 struct dep_t *dt;
Rob Landleye9190962005-12-12 19:38:44 +0000667 struct mod_opt_t *opt = 0;
Eric Andersen807bd842004-08-19 18:30:31 +0000668 char *path = 0;
Robert Griebl52e8d062002-05-14 23:42:08 +0000669
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000670 // check dependencies
671 for ( dt = depend; dt; dt = dt-> m_next ) {
Eric Andersen807bd842004-08-19 18:30:31 +0000672 if ( strcmp ( dt-> m_name, mod ) == 0) {
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000673 break;
Robert Grieblaead70b2002-07-26 15:54:20 +0000674 }
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000675 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000676
Rob Landleye9190962005-12-12 19:38:44 +0000677 if( !dt ) {
678 bb_error_msg ("module %s not found.", mod);
679 return;
680 }
681
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000682 // resolve alias names
Rob Landleye9190962005-12-12 19:38:44 +0000683 while ( dt-> m_isalias ) {
Robert Grieblaead70b2002-07-26 15:54:20 +0000684 if ( dt-> m_depcnt == 1 ) {
685 struct dep_t *adt;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000686
Robert Grieblaead70b2002-07-26 15:54:20 +0000687 for ( adt = depend; adt; adt = adt-> m_next ) {
Eric Andersen807bd842004-08-19 18:30:31 +0000688 if ( strcmp ( adt-> m_name, dt-> m_deparr [0] ) == 0 )
Robert Grieblaead70b2002-07-26 15:54:20 +0000689 break;
Robert Grieblaead70b2002-07-26 15:54:20 +0000690 }
Robert Griebl70112da2002-07-29 20:28:38 +0000691 if ( adt ) {
Rob Landleye9190962005-12-12 19:38:44 +0000692 /* This is the module we are aliased to */
693 struct mod_opt_t *opts = dt-> m_options;
694 /* Option of the alias are appended to the options of the module */
695 while( opts ) {
696 adt-> m_options = append_option( adt-> m_options, opts-> m_opt_val );
697 opts = opts-> m_next;
698 }
Eric Andersen716ccb22004-01-10 11:29:31 +0000699 dt = adt;
Robert Griebl70112da2002-07-29 20:28:38 +0000700 }
Rob Landleye9190962005-12-12 19:38:44 +0000701 else {
702 bb_error_msg ("module %s not found.", mod);
Robert Griebl70112da2002-07-29 20:28:38 +0000703 return;
Rob Landleye9190962005-12-12 19:38:44 +0000704 }
Robert Grieblaead70b2002-07-26 15:54:20 +0000705 }
Rob Landleye9190962005-12-12 19:38:44 +0000706 else {
707 bb_error_msg ("Bad alias %s", dt-> m_name);
Eric Andersen716ccb22004-01-10 11:29:31 +0000708 return;
Rob Landleye9190962005-12-12 19:38:44 +0000709 }
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000710 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000711
Rob Landleye9190962005-12-12 19:38:44 +0000712 mod = dt-> m_name;
713 path = dt-> m_path;
714 opt = dt-> m_options;
Bernhard Reutner-Fischer17d355c2005-12-14 08:32:44 +0000715
Robert Griebl52e8d062002-05-14 23:42:08 +0000716 // search for duplicates
717 for ( find = *head; find; find = find-> m_next ) {
Eric Andersen807bd842004-08-19 18:30:31 +0000718 if ( !strcmp ( mod, find-> m_name )) {
Robert Griebl52e8d062002-05-14 23:42:08 +0000719 // found -> dequeue it
720
721 if ( find-> m_prev )
722 find-> m_prev-> m_next = find-> m_next;
723 else
724 *head = find-> m_next;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000725
Robert Griebl52e8d062002-05-14 23:42:08 +0000726 if ( find-> m_next )
727 find-> m_next-> m_prev = find-> m_prev;
728 else
729 *tail = find-> m_prev;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000730
Robert Griebl52e8d062002-05-14 23:42:08 +0000731 break; // there can be only one duplicate
Eric Andersen716ccb22004-01-10 11:29:31 +0000732 }
Robert Griebl52e8d062002-05-14 23:42:08 +0000733 }
734
735 if ( !find ) { // did not find a duplicate
Eric Andersen716ccb22004-01-10 11:29:31 +0000736 find = (struct mod_list_t *) xmalloc ( sizeof(struct mod_list_t));
Eric Andersen807bd842004-08-19 18:30:31 +0000737 find-> m_name = mod;
738 find-> m_path = path;
Robert Grieblaead70b2002-07-26 15:54:20 +0000739 find-> m_options = opt;
Robert Griebl52e8d062002-05-14 23:42:08 +0000740 }
741
Eric Andersen716ccb22004-01-10 11:29:31 +0000742 // enqueue at tail
Robert Griebl52e8d062002-05-14 23:42:08 +0000743 if ( *tail )
744 (*tail)-> m_next = find;
745 find-> m_prev = *tail;
746 find-> m_next = 0;
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000747
Robert Griebl52e8d062002-05-14 23:42:08 +0000748 if ( !*head )
749 *head = find;
750 *tail = find;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000751
Eric Andersen716ccb22004-01-10 11:29:31 +0000752 if ( dt ) {
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000753 int i;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000754
Rob Landleye9190962005-12-12 19:38:44 +0000755 /* Add all dependable module for that new module */
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000756 for ( i = 0; i < dt-> m_depcnt; i++ )
757 check_dep ( dt-> m_deparr [i], head, tail );
758 }
Robert Griebl52e8d062002-05-14 23:42:08 +0000759}
760
Robert Griebl236abbf2002-05-22 23:34:35 +0000761static int mod_insert ( char *mod, int argc, char **argv )
Robert Griebl52e8d062002-05-14 23:42:08 +0000762{
763 struct mod_list_t *tail = 0;
Eric Andersen716ccb22004-01-10 11:29:31 +0000764 struct mod_list_t *head = 0;
Eric Andersen908e3622003-06-20 09:56:37 +0000765 int rc;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000766
Robert Griebl52e8d062002-05-14 23:42:08 +0000767 // get dep list for module mod
768 check_dep ( mod, &head, &tail );
Eric Andersen3b1a7442003-12-24 20:30:45 +0000769
Robert Griebl52e8d062002-05-14 23:42:08 +0000770 if ( head && tail ) {
Rob Landleye9190962005-12-12 19:38:44 +0000771 if( argc ) {
Eric Andersen716ccb22004-01-10 11:29:31 +0000772 int i;
Robert Grieblaead70b2002-07-26 15:54:20 +0000773 // append module args
Eric Andersen716ccb22004-01-10 11:29:31 +0000774 for ( i = 0; i < argc; i++ )
Rob Landleye9190962005-12-12 19:38:44 +0000775 head->m_options = append_option( head->m_options, argv[i] );
Robert Griebl52e8d062002-05-14 23:42:08 +0000776 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000777
Robert Griebl52e8d062002-05-14 23:42:08 +0000778 // process tail ---> head
Eric Andersen908e3622003-06-20 09:56:37 +0000779 rc = mod_process ( tail, 1 );
Robert Griebl52e8d062002-05-14 23:42:08 +0000780 }
781 else
782 rc = 1;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000783
Robert Griebl52e8d062002-05-14 23:42:08 +0000784 return rc;
785}
786
Eric Andersen908e3622003-06-20 09:56:37 +0000787static int mod_remove ( char *mod )
Robert Griebl52e8d062002-05-14 23:42:08 +0000788{
Eric Andersen908e3622003-06-20 09:56:37 +0000789 int rc;
Eric Andersen716ccb22004-01-10 11:29:31 +0000790 static struct mod_list_t rm_a_dummy = { "-a", 0, 0 };
Eric Andersen3b1a7442003-12-24 20:30:45 +0000791
Robert Griebl52e8d062002-05-14 23:42:08 +0000792 struct mod_list_t *head = 0;
793 struct mod_list_t *tail = 0;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000794
Robert Griebl52e8d062002-05-14 23:42:08 +0000795 if ( mod )
796 check_dep ( mod, &head, &tail );
797 else // autoclean
798 head = tail = &rm_a_dummy;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000799
Robert Griebl236abbf2002-05-22 23:34:35 +0000800 if ( head && tail )
Eric Andersen908e3622003-06-20 09:56:37 +0000801 rc = mod_process ( head, 0 ); // process head ---> tail
802 else
803 rc = 1;
804 return rc;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000805
Robert Griebl52e8d062002-05-14 23:42:08 +0000806}
807
Eric Andersen0139ca92001-07-22 23:01:03 +0000808extern int modprobe_main(int argc, char** argv)
809{
Robert Griebl236abbf2002-05-22 23:34:35 +0000810 int opt;
Rob Landleye9190962005-12-12 19:38:44 +0000811 int rc = EXIT_SUCCESS;
Robert Griebl236abbf2002-05-22 23:34:35 +0000812 int remove_opt = 0;
Eric Andersen0139ca92001-07-22 23:01:03 +0000813
Robert Griebl236abbf2002-05-22 23:34:35 +0000814 autoclean = show_only = quiet = do_syslog = verbose = 0;
815
816 while ((opt = getopt(argc, argv, "acdklnqrst:vVC:")) != -1) {
817 switch(opt) {
Eric Andersen3b1a7442003-12-24 20:30:45 +0000818 case 'c': // no config used
819 case 'l': // no pattern matching
820 return EXIT_SUCCESS;
821 break;
822 case 'C': // no config used
823 case 't': // no pattern matching
824 bb_error_msg_and_die("-t and -C not supported");
Robert Griebl236abbf2002-05-22 23:34:35 +0000825
Eric Andersen3b1a7442003-12-24 20:30:45 +0000826 case 'a': // ignore
827 case 'd': // ignore
828 break;
829 case 'k':
830 autoclean++;
831 break;
832 case 'n':
833 show_only++;
834 break;
835 case 'q':
Rob Landley79e1cab2005-11-15 00:08:29 +0000836 quiet++; verbose=0;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000837 break;
838 case 'r':
839 remove_opt++;
840 break;
841 case 's':
842 do_syslog++;
843 break;
844 case 'v':
Rob Landley79e1cab2005-11-15 00:08:29 +0000845 verbose++; quiet=0;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000846 break;
847 case 'V':
848 default:
849 bb_show_usage();
850 break;
Eric Andersen0139ca92001-07-22 23:01:03 +0000851 }
Eric Andersen0139ca92001-07-22 23:01:03 +0000852 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000853
Eric Andersen716ccb22004-01-10 11:29:31 +0000854 depend = build_dep ( );
Robert Griebl52e8d062002-05-14 23:42:08 +0000855
Eric Andersen716ccb22004-01-10 11:29:31 +0000856 if ( !depend )
Manuel Novoa III cad53642003-03-19 09:13:01 +0000857 bb_error_msg_and_die ( "could not parse modules.dep\n" );
Eric Andersen3b1a7442003-12-24 20:30:45 +0000858
Eric Andersen1b064192001-07-25 07:23:38 +0000859 if (remove_opt) {
Eric Andersen0139ca92001-07-22 23:01:03 +0000860 do {
Eric Andersen908e3622003-06-20 09:56:37 +0000861 if (mod_remove ( optind < argc ?
Eric Andersen3b1a7442003-12-24 20:30:45 +0000862 bb_xstrdup (argv [optind]) : NULL )) {
Eric Andersen908e3622003-06-20 09:56:37 +0000863 bb_error_msg ("failed to remove module %s",
Eric Andersen3b1a7442003-12-24 20:30:45 +0000864 argv [optind] );
Eric Andersen908e3622003-06-20 09:56:37 +0000865 rc = EXIT_FAILURE;
866 }
Robert Griebl52e8d062002-05-14 23:42:08 +0000867 } while ( ++optind < argc );
Rob Landleye9190962005-12-12 19:38:44 +0000868 } else {
869 if (optind >= argc)
870 bb_error_msg_and_die ( "No module or pattern provided\n" );
Eric Andersen3b1a7442003-12-24 20:30:45 +0000871
Rob Landleye9190962005-12-12 19:38:44 +0000872 if ( mod_insert ( bb_xstrdup ( argv [optind] ), argc - optind - 1, argv + optind + 1 ))
873 bb_error_msg_and_die ( "failed to load module %s", argv [optind] );
Eric Andersen0139ca92001-07-22 23:01:03 +0000874 }
875
Rob Landleye9190962005-12-12 19:38:44 +0000876 /* Here would be a good place to free up memory allocated during the dependencies build. */
Eric Andersen3b1a7442003-12-24 20:30:45 +0000877
Rob Landleye9190962005-12-12 19:38:44 +0000878 return rc;
Eric Andersen0139ca92001-07-22 23:01:03 +0000879}