blob: 44460391aec873e94fe26836b40d7938aafc7933 [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>
Bernhard Reutner-Fischer2c351a82006-06-03 19:08:49 +000024#include <fnmatch.h>
Eric Andersen0139ca92001-07-22 23:01:03 +000025#include "busybox.h"
26
Rob Landleye9190962005-12-12 19:38:44 +000027struct mod_opt_t { /* one-way list of options to pass to a module */
28 char * m_opt_val;
29 struct mod_opt_t * m_next;
Eric Andersen60e56f52002-04-26 06:04:01 +000030};
31
Rob Landleye9190962005-12-12 19:38:44 +000032struct dep_t { /* one-way list of dependency rules */
33 /* a dependency rule */
34 char * m_name; /* the module name*/
35 char * m_path; /* the module file path */
36 struct mod_opt_t * m_options; /* the module options */
Bernhard Reutner-Fischer17d355c2005-12-14 08:32:44 +000037
Rob Landleye9190962005-12-12 19:38:44 +000038 int m_isalias : 1; /* the module is an alias */
39 int m_reserved : 15; /* stuffin' */
40
41 int m_depcnt : 16; /* the number of dependable module(s) */
42 char ** m_deparr; /* the list of dependable module(s) */
43
44 struct dep_t * m_next; /* the next dependency rule */
45};
46
47struct mod_list_t { /* two-way list of modules to process */
48 /* a module description */
Eric Andersen807bd842004-08-19 18:30:31 +000049 char * m_name;
50 char * m_path;
Rob Landleye9190962005-12-12 19:38:44 +000051 struct mod_opt_t * m_options;
Eric Andersen3b1a7442003-12-24 20:30:45 +000052
Robert Griebl52e8d062002-05-14 23:42:08 +000053 struct mod_list_t * m_prev;
54 struct mod_list_t * m_next;
55};
56
57
Robert Griebl236abbf2002-05-22 23:34:35 +000058static struct dep_t *depend;
"Vladimir N. Oleynik"4fc92202006-02-02 14:48:54 +000059
60#define main_options "acdklnqrst:vVC:"
61#define INSERT_ALL 1 /* a */
62#define DUMP_CONF_EXIT 2 /* c */
63#define D_OPT_IGNORED 4 /* d */
64#define AUTOCLEAN_FLG 8 /* k */
65#define LIST_ALL 16 /* l */
66#define SHOW_ONLY 32 /* n */
67#define QUIET 64 /* q */
68#define REMOVE_OPT 128 /* r */
69#define DO_SYSLOG 256 /* s */
70#define RESTRICT_DIR 512 /* t */
71#define VERBOSE 1024 /* v */
72#define VERSION_ONLY 2048 /* V */
73#define CONFIG_FILE 4096 /* C */
74
75#define autoclean (main_opts & AUTOCLEAN_FLG)
76#define show_only (main_opts & SHOW_ONLY)
77#define quiet (main_opts & QUIET)
78#define remove_opt (main_opts & REMOVE_OPT)
79#define do_syslog (main_opts & DO_SYSLOG)
80#define verbose (main_opts & VERBOSE)
81
82static int main_opts;
Robert Griebl52e8d062002-05-14 23:42:08 +000083
Eric Andersen14f5c8d2005-04-16 19:39:00 +000084static int parse_tag_value ( char *buffer, char **ptag, char **pvalue )
Robert Grieblaead70b2002-07-26 15:54:20 +000085{
86 char *tag, *value;
87
88 while ( isspace ( *buffer ))
Eric Andersen716ccb22004-01-10 11:29:31 +000089 buffer++;
Robert Grieblaead70b2002-07-26 15:54:20 +000090 tag = value = buffer;
91 while ( !isspace ( *value ))
Eric Andersen7e496a72004-04-06 12:06:03 +000092 if (!*value) return 0;
93 else value++;
Robert Grieblaead70b2002-07-26 15:54:20 +000094 *value++ = 0;
95 while ( isspace ( *value ))
96 value++;
Eric Andersen7e496a72004-04-06 12:06:03 +000097 if (!*value) return 0;
Robert Grieblaead70b2002-07-26 15:54:20 +000098
99 *ptag = tag;
100 *pvalue = value;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000101
Eric Andersen7e496a72004-04-06 12:06:03 +0000102 return 1;
Robert Grieblaead70b2002-07-26 15:54:20 +0000103}
Eric Andersen60e56f52002-04-26 06:04:01 +0000104
Robert Grieblbc28f7a2002-06-04 19:33:58 +0000105/* Jump through hoops to simulate how fgets() grabs just one line at a
106 * time... Don't use any stdio since modprobe gets called from a kernel
Eric Andersen716ccb22004-01-10 11:29:31 +0000107 * thread and stdio junk can overflow the limited stack...
Robert Grieblbc28f7a2002-06-04 19:33:58 +0000108 */
109static char *reads ( int fd, char *buffer, size_t len )
110{
111 int n = read ( fd, buffer, len );
Eric Andersen3b1a7442003-12-24 20:30:45 +0000112
Robert Grieblbc28f7a2002-06-04 19:33:58 +0000113 if ( n > 0 ) {
114 char *p;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000115
Robert Grieblbc28f7a2002-06-04 19:33:58 +0000116 buffer [len-1] = 0;
117 p = strchr ( buffer, '\n' );
Eric Andersen3b1a7442003-12-24 20:30:45 +0000118
Robert Grieblbc28f7a2002-06-04 19:33:58 +0000119 if ( p ) {
120 off_t offset;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000121
Eric Andersen716ccb22004-01-10 11:29:31 +0000122 offset = lseek ( fd, 0L, SEEK_CUR ); // Get the current file descriptor offset
Robert Grieblbc28f7a2002-06-04 19:33:58 +0000123 lseek ( fd, offset-n + (p-buffer) + 1, SEEK_SET ); // Set the file descriptor offset to right after the \n
124
125 p[1] = 0;
126 }
127 return buffer;
128 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000129
Robert Grieblbc28f7a2002-06-04 19:33:58 +0000130 else
131 return 0;
132}
133
Rob Landleye9190962005-12-12 19:38:44 +0000134/*
135 * This function appends an option to a list
136 */
Bernhard Reutner-Fischer101a4702006-04-03 15:46:14 +0000137static struct mod_opt_t *append_option( struct mod_opt_t *opt_list, char *opt )
Eric Andersen60e56f52002-04-26 06:04:01 +0000138{
Rob Landleye9190962005-12-12 19:38:44 +0000139 struct mod_opt_t *ol = opt_list;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000140
Rob Landleye9190962005-12-12 19:38:44 +0000141 if( ol ) {
142 while( ol-> m_next ) {
143 ol = ol-> m_next;
144 }
145 ol-> m_next = xmalloc( sizeof( struct mod_opt_t ) );
146 ol = ol-> m_next;
147 } else {
148 ol = opt_list = xmalloc( sizeof( struct mod_opt_t ) );
Eric Andersen03d80912003-12-19 21:04:19 +0000149 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000150
Rob Landleye9190962005-12-12 19:38:44 +0000151 ol-> m_opt_val = bb_xstrdup( opt );
152 ol-> m_next = NULL;
Eric Andersen60e56f52002-04-26 06:04:01 +0000153
Rob Landleye9190962005-12-12 19:38:44 +0000154 return opt_list;
Eric Andersen60e56f52002-04-26 06:04:01 +0000155}
156
Bernhard Reutner-Fischer17d355c2005-12-14 08:32:44 +0000157#if ENABLE_FEATURE_MODPROBE_MULTIPLE_OPTIONS
Rob Landley79e1cab2005-11-15 00:08:29 +0000158/* static char* parse_command_string( char* src, char **dst );
159 * src: pointer to string containing argument
160 * dst: pointer to where to store the parsed argument
161 * return value: the pointer to the first char after the parsed argument,
162 * NULL if there was no argument parsed (only trailing spaces).
163 * Note that memory is allocated with bb_xstrdup when a new argument was
164 * parsed. Don't forget to free it!
165 */
166#define ARG_EMPTY 0x00
167#define ARG_IN_DQUOTES 0x01
168#define ARG_IN_SQUOTES 0x02
169static char *parse_command_string( char *src, char **dst )
170{
171 int opt_status = ARG_EMPTY;
172 char* tmp_str;
173
174 /* Dumb you, I have nothing to do... */
175 if( src == NULL ) return src;
176
177 /* Skip leading spaces */
178 while( *src == ' ' ) {
179 src++;
180 }
181 /* Is the end of string reached? */
182 if( *src == '\0' ) {
183 return NULL;
184 }
185 /* Reached the start of an argument
Rob Landleye9190962005-12-12 19:38:44 +0000186 * By the way, we duplicate a little too much
187 * here but what is too much is freed later. */
Rob Landley79e1cab2005-11-15 00:08:29 +0000188 *dst = tmp_str = bb_xstrdup( src );
189 /* Get to the end of that argument */
190 while( ( *tmp_str != '\0' )
Rob Landleye9190962005-12-12 19:38:44 +0000191 && ( ( *tmp_str != ' ' )
192 || ( opt_status & ( ARG_IN_DQUOTES | ARG_IN_SQUOTES ) ) ) ) {
Rob Landley79e1cab2005-11-15 00:08:29 +0000193 switch( *tmp_str ) {
194 case '\'':
195 if( opt_status & ARG_IN_DQUOTES ) {
196 /* Already in double quotes, keep current char as is */
197 } else {
198 /* shift left 1 char, until end of string: get rid of the opening/closing quotes */
199 memmove( tmp_str, tmp_str + 1, strlen( tmp_str ) );
200 /* mark me: we enter or leave single quotes */
201 opt_status ^= ARG_IN_SQUOTES;
202 /* Back one char, as we need to re-scan the new char there. */
203 tmp_str--;
204 }
205 break;
206 case '"':
207 if( opt_status & ARG_IN_SQUOTES ) {
208 /* Already in single quotes, keep current char as is */
209 } else {
210 /* shift left 1 char, until end of string: get rid of the opening/closing quotes */
211 memmove( tmp_str, tmp_str + 1, strlen( tmp_str ) );
212 /* mark me: we enter or leave double quotes */
213 opt_status ^= ARG_IN_DQUOTES;
214 /* Back one char, as we need to re-scan the new char there. */
215 tmp_str--;
216 }
217 break;
218 case '\\':
219 if( opt_status & ARG_IN_SQUOTES ) {
220 /* Between single quotes: keep as is. */
221 } else {
222 switch( *(tmp_str+1) ) {
223 case 'a':
224 case 'b':
225 case 't':
226 case 'n':
227 case 'v':
228 case 'f':
229 case 'r':
230 case '0':
231 /* We escaped a special character. For now, keep
232 * both the back-slash and the following char. */
233 tmp_str++; src++;
234 break;
235 default:
236 /* We escaped a space or a single or double quote,
237 * or a back-slash, or a non-escapable char. Remove
238 * the '\' and keep the new current char as is. */
239 memmove( tmp_str, tmp_str + 1, strlen( tmp_str ) );
240 break;
241 }
242 }
243 break;
244 /* Any other char that is special shall appear here.
245 * Example: $ starts a variable
246 case '$':
247 do_variable_expansion();
248 break;
249 * */
250 default:
251 /* any other char is kept as is. */
252 break;
253 }
254 tmp_str++; /* Go to next char */
255 src++; /* Go to next char to find the end of the argument. */
256 }
257 /* End of string, but still no ending quote */
258 if( opt_status & ( ARG_IN_DQUOTES | ARG_IN_SQUOTES ) ) {
259 bb_error_msg_and_die( "unterminated (single or double) quote in options list: %s", src );
260 }
"Vladimir N. Oleynik"4fc92202006-02-02 14:48:54 +0000261 *tmp_str++ = '\0';
262 *dst = xrealloc( *dst, (tmp_str - *dst ) );
Rob Landley79e1cab2005-11-15 00:08:29 +0000263 return src;
264}
Rob Landleyae50c6d2005-12-15 07:42:13 +0000265#else
266#define parse_command_string(src, dst) (0)
Bernhard Reutner-Fischer17d355c2005-12-14 08:32:44 +0000267#endif /* ENABLE_FEATURE_MODPROBE_MULTIPLE_OPTIONS */
Rob Landley79e1cab2005-11-15 00:08:29 +0000268
Rob Landleye9190962005-12-12 19:38:44 +0000269/*
Rob Landley72615752006-04-10 16:09:52 +0000270 * This function reads aliases and default module options from a configuration file
271 * (/etc/modprobe.conf syntax). It supports includes (only files, no directories).
272 */
273static void include_conf ( struct dep_t **first, struct dep_t **current, char *buffer, int buflen, int fd )
274{
275 int continuation_line = 0;
276
277 // alias parsing is not 100% correct (no correct handling of continuation lines within an alias) !
278
279 while ( reads ( fd, buffer, buflen)) {
280 int l;
281 char *p;
282
283 p = strchr ( buffer, '#' );
284 if ( p )
285 *p = 0;
286
Rob Landleya3896512006-05-07 20:20:34 +0000287 l = strlen ( buffer );
Rob Landley72615752006-04-10 16:09:52 +0000288
289 while ( l && 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 if ( !continuation_line ) {
300 if (( strncmp ( buffer, "alias", 5 ) == 0 ) && isspace ( buffer [5] )) {
301 char *alias, *mod;
302
303 if ( parse_tag_value ( buffer + 6, &alias, &mod )) {
304 /* handle alias as a module dependent on the aliased module */
305 if ( !*current ) {
306 (*first) = (*current) = (struct dep_t *) xcalloc ( 1, sizeof ( struct dep_t ));
307 }
308 else {
309 (*current)-> m_next = (struct dep_t *) xcalloc ( 1, sizeof ( struct dep_t ));
310 (*current) = (*current)-> m_next;
311 }
312 (*current)-> m_name = bb_xstrdup ( alias );
313 (*current)-> m_isalias = 1;
314
315 if (( strcmp ( mod, "off" ) == 0 ) || ( strcmp ( mod, "null" ) == 0 )) {
316 (*current)-> m_depcnt = 0;
317 (*current)-> m_deparr = 0;
318 }
319 else {
320 (*current)-> m_depcnt = 1;
321 (*current)-> m_deparr = xmalloc ( 1 * sizeof( char * ));
322 (*current)-> m_deparr[0] = bb_xstrdup ( mod );
323 }
324 (*current)-> m_next = 0;
325 }
326 }
327 else if (( strncmp ( buffer, "options", 7 ) == 0 ) && isspace ( buffer [7] )) {
328 char *mod, *opt;
329
330 /* split the line in the module/alias name, and options */
331 if ( parse_tag_value ( buffer + 8, &mod, &opt )) {
332 struct dep_t *dt;
333
334 /* find the corresponding module */
335 for ( dt = *first; dt; dt = dt-> m_next ) {
336 if ( strcmp ( dt-> m_name, mod ) == 0 )
337 break;
338 }
339 if ( dt ) {
340 if ( ENABLE_FEATURE_MODPROBE_MULTIPLE_OPTIONS ) {
341 char* new_opt = NULL;
342 while( ( opt = parse_command_string( opt, &new_opt ) ) ) {
343 dt-> m_options = append_option( dt-> m_options, new_opt );
344 }
345 } else {
346 dt-> m_options = append_option( dt-> m_options, opt );
347 }
348 }
349 }
350 }
351 else if (( strncmp ( buffer, "include", 7 ) == 0 ) && isspace ( buffer [7] )) {
352
353 int fdi; char *filename = buffer + 8;
354
355 while ( isspace ( *filename ))
356 filename++;
357
358 if (( fdi = open ( filename, O_RDONLY )) >= 0 ) {
359 include_conf(first, current, buffer, buflen, fdi);
360 close(fdi);
361 }
362 }
363 }
364 }
365}
366
367/*
Bernhard Reutner-Fischere3c150b2006-05-19 11:24:28 +0000368 * This function builds a list of dependency rules from /lib/modules/`uname -r`\modules.dep.
369 * It then fills every modules and aliases with their default options, found by parsing
Rob Landleye9190962005-12-12 19:38:44 +0000370 * modprobe.conf (or modules.conf, or conf.modules).
371 */
372static struct dep_t *build_dep ( void )
373{
374 int fd;
375 struct utsname un;
376 struct dep_t *first = 0;
377 struct dep_t *current = 0;
378 char buffer[2048];
"Vladimir N. Oleynik"4fc92202006-02-02 14:48:54 +0000379 char *filename;
Rob Landleye9190962005-12-12 19:38:44 +0000380 int continuation_line = 0;
381 int k_version;
382
383 k_version = 0;
384 if ( uname ( &un ))
385 bb_error_msg_and_die("can't determine kernel version");
386
Rob Landleye9190962005-12-12 19:38:44 +0000387 if (un.release[0] == '2') {
388 k_version = un.release[2] - '0';
389 }
390
"Vladimir N. Oleynik"4fc92202006-02-02 14:48:54 +0000391 filename = bb_xasprintf("/lib/modules/%s/modules.dep", un.release );
Rob Landley3afb0702006-05-18 20:41:43 +0000392 fd = open ( filename, O_RDONLY );
Bernhard Reutner-Fischere3c150b2006-05-19 11:24:28 +0000393 if (ENABLE_FEATURE_CLEAN_UP)
394 free(filename);
Rob Landley3afb0702006-05-18 20:41:43 +0000395 if (fd < 0) {
Rob Landleye9190962005-12-12 19:38:44 +0000396 /* Ok, that didn't work. Fall back to looking in /lib/modules */
397 if (( fd = open ( "/lib/modules/modules.dep", O_RDONLY )) < 0 ) {
398 return 0;
399 }
400 }
401
402 while ( reads ( fd, buffer, sizeof( buffer ))) {
Rob Landleya3896512006-05-07 20:20:34 +0000403 int l = strlen ( buffer );
Rob Landleye9190962005-12-12 19:38:44 +0000404 char *p = 0;
405
"Vladimir N. Oleynik"4fc92202006-02-02 14:48:54 +0000406 while ( l > 0 && isspace ( buffer [l-1] )) {
Rob Landleye9190962005-12-12 19:38:44 +0000407 buffer [l-1] = 0;
408 l--;
409 }
410
411 if ( l == 0 ) {
412 continuation_line = 0;
413 continue;
414 }
415
416 /* Is this a new module dep description? */
417 if ( !continuation_line ) {
Bernhard Reutner-Fischer17d355c2005-12-14 08:32:44 +0000418 /* find the dep beginning */
Rob Landleye9190962005-12-12 19:38:44 +0000419 char *col = strchr ( buffer, ':' );
420 char *dot = col;
421
422 if ( col ) {
423 /* This line is a dep description */
424 char *mods;
425 char *modpath;
426 char *mod;
427
428 /* Find the beginning of the module file name */
429 *col = 0;
430 mods = strrchr ( buffer, '/' );
431
432 if ( !mods )
433 mods = buffer; /* no path for this module */
434 else
435 mods++; /* there was a path for this module... */
436
437 /* find the path of the module */
438 modpath = strchr ( buffer, '/' ); /* ... and this is the path */
439 if ( !modpath )
440 modpath = buffer; /* module with no path */
441 /* find the end of the module name in the file name */
442 if ( ENABLE_FEATURE_2_6_MODULES &&
443 (k_version > 4) && ( *(col-3) == '.' ) &&
444 ( *(col-2) == 'k' ) && ( *(col-1) == 'o' ) )
445 dot = col - 3;
446 else
447 if (( *(col-2) == '.' ) && ( *(col-1) == 'o' ))
448 dot = col - 2;
449
450 mod = bb_xstrndup ( mods, dot - mods );
451
452 /* enqueue new module */
453 if ( !current ) {
454 first = current = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
455 }
456 else {
457 current-> m_next = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
458 current = current-> m_next;
459 }
460 current-> m_name = mod;
461 current-> m_path = bb_xstrdup(modpath);
462 current-> m_options = NULL;
463 current-> m_isalias = 0;
464 current-> m_depcnt = 0;
465 current-> m_deparr = 0;
466 current-> m_next = 0;
467
468 p = col + 1;
469 }
470 else
471 /* this line is not a dep description */
472 p = 0;
473 }
474 else
475 /* It's a dep description continuation */
476 p = buffer;
477
478 while ( p && *p && isblank(*p))
479 p++;
480
481 /* p points to the first dependable module; if NULL, no dependable module */
482 if ( p && *p ) {
483 char *end = &buffer [l-1];
484 char *deps;
485 char *dep;
486 char *next;
487 int ext = 0;
488
489 while ( isblank ( *end ) || ( *end == '\\' ))
490 end--;
491
492 do
493 {
494 /* search the end of the dependency */
495 next = strchr (p, ' ' );
496 if (next)
497 {
498 *next = 0;
499 next--;
500 }
501 else
502 next = end;
503
Bernhard Reutner-Fischer17d355c2005-12-14 08:32:44 +0000504 /* find the beginning of the module file name */
Rob Landleye9190962005-12-12 19:38:44 +0000505 deps = strrchr ( p, '/' );
506
507 if ( !deps || ( deps < p )) {
508 deps = p;
509
510 while ( isblank ( *deps ))
511 deps++;
512 }
513 else
514 deps++;
515
516 /* find the end of the module name in the file name */
517 if ( ENABLE_FEATURE_2_6_MODULES &&
518 (k_version > 4) && ( *(next-2) == '.' ) &&
519 ( *(next-1) == 'k' ) && ( *next == 'o' ) )
520 ext = 3;
521 else
522 if (( *(next-1) == '.' ) && ( *next == 'o' ))
523 ext = 2;
524
525 /* Cope with blank lines */
526 if ((next-deps-ext+1) <= 0)
527 continue;
528 dep = bb_xstrndup ( deps, next - deps - ext + 1 );
529
530 /* Add the new dependable module name */
531 current-> m_depcnt++;
532 current-> m_deparr = (char **) xrealloc ( current-> m_deparr,
533 sizeof ( char *) * current-> m_depcnt );
534 current-> m_deparr [current-> m_depcnt - 1] = dep;
535
536 p = next + 2;
537 } while (next < end);
538 }
539
540 /* is there other dependable module(s) ? */
541 if ( buffer [l-1] == '\\' )
542 continuation_line = 1;
543 else
544 continuation_line = 0;
545 }
546 close ( fd );
547
Rob Landleyae50c6d2005-12-15 07:42:13 +0000548 if (!ENABLE_FEATURE_2_6_MODULES
549 || ( fd = open ( "/etc/modprobe.conf", O_RDONLY )) < 0 )
Rob Landleye9190962005-12-12 19:38:44 +0000550 if (( fd = open ( "/etc/modules.conf", O_RDONLY )) < 0 )
551 if (( fd = open ( "/etc/conf.modules", O_RDONLY )) < 0 )
552 return first;
553
Rob Landley72615752006-04-10 16:09:52 +0000554 include_conf (&first, &current, buffer, sizeof(buffer), fd);
555 close(fd);
Rob Landleye9190962005-12-12 19:38:44 +0000556
Bernhard Reutner-Fischere3c150b2006-05-19 11:24:28 +0000557 filename = bb_xasprintf("/lib/modules/%s/modules.alias", un.release);
558 fd = open ( filename, O_RDONLY );
559 if (ENABLE_FEATURE_CLEAN_UP)
560 free(filename);
561 if (fd < 0) {
Rob Landley72615752006-04-10 16:09:52 +0000562 /* Ok, that didn't work. Fall back to looking in /lib/modules */
563 if (( fd = open ( "/lib/modules/modules.alias", O_RDONLY )) < 0 ) {
564 return first;
Rob Landleye9190962005-12-12 19:38:44 +0000565 }
566 }
Rob Landley72615752006-04-10 16:09:52 +0000567
568 include_conf (&first, &current, buffer, sizeof(buffer), fd);
569 close(fd);
Rob Landleye9190962005-12-12 19:38:44 +0000570
571 return first;
572}
573
574/* return 1 = loaded, 0 = not loaded, -1 = can't tell */
575static int already_loaded (const char *name)
576{
577 int fd;
578 char buffer[4096];
579
580 fd = open ("/proc/modules", O_RDONLY);
581 if (fd < 0)
582 return -1;
583
584 while ( reads ( fd, buffer, sizeof( buffer ))) {
585 char *p;
586
587 p = strchr (buffer, ' ');
588 if (p) {
589 *p = 0;
590 for( p = buffer; ENABLE_FEATURE_2_6_MODULES && *p; p++ ) {
591 *p = ((*p)=='-')?'_':*p;
592 }
593 if (strcmp (name, buffer) == 0) {
594 close (fd);
595 return 1;
596 }
597 }
598 }
599
600 close (fd);
601 return 0;
602}
603
Robert Griebl236abbf2002-05-22 23:34:35 +0000604static int mod_process ( struct mod_list_t *list, int do_insert )
Eric Andersen60e56f52002-04-26 06:04:01 +0000605{
Eric Andersen44b57582004-08-03 08:23:33 +0000606 int rc = 0;
Rob Landley79e1cab2005-11-15 00:08:29 +0000607 char **argv = NULL;
Rob Landleye9190962005-12-12 19:38:44 +0000608 struct mod_opt_t *opts;
609 int argc_malloc; /* never used when CONFIG_FEATURE_CLEAN_UP not defined */
Paul Fox8eeb6552005-08-04 18:33:36 +0000610 int argc;
Eric Andersen864b7972002-05-03 15:48:26 +0000611
Robert Griebl52e8d062002-05-14 23:42:08 +0000612 while ( list ) {
Paul Fox8eeb6552005-08-04 18:33:36 +0000613 argc = 0;
Rob Landleye9190962005-12-12 19:38:44 +0000614 if( ENABLE_FEATURE_CLEAN_UP )
615 argc_malloc = 0;
616 /* If CONFIG_FEATURE_CLEAN_UP is not defined, then we leak memory
617 * each time we allocate memory for argv.
618 * But it is (quite) small amounts of memory that leak each
619 * time a module is loaded, and it is reclaimed when modprobe
620 * exits anyway (even when standalone shell?).
621 * This could become a problem when loading a module with LOTS of
622 * dependencies, with LOTS of options for each dependencies, with
623 * very little memory on the target... But in that case, the module
624 * would not load because there is no more memory, so there's no
625 * problem. */
626 /* enough for minimal insmod (5 args + NULL) or rmmod (3 args + NULL) */
627 argv = (char**) malloc( 6 * sizeof( char* ) );
Glenn L McGrath350733a2003-09-08 00:32:49 +0000628 if ( do_insert ) {
Rob Landley79e1cab2005-11-15 00:08:29 +0000629 if (already_loaded (list->m_name) != 1) {
Paul Fox8eeb6552005-08-04 18:33:36 +0000630 argv[argc++] = "insmod";
631 if (do_syslog)
632 argv[argc++] = "-s";
633 if (autoclean)
634 argv[argc++] = "-k";
635 if (quiet)
636 argv[argc++] = "-q";
Rob Landley79e1cab2005-11-15 00:08:29 +0000637 else if(verbose) /* verbose and quiet are mutually exclusive */
638 argv[argc++] = "-v";
Paul Fox8eeb6552005-08-04 18:33:36 +0000639 argv[argc++] = list-> m_path;
Rob Landleye9190962005-12-12 19:38:44 +0000640 if( ENABLE_FEATURE_CLEAN_UP )
641 argc_malloc = argc;
Rob Landley79e1cab2005-11-15 00:08:29 +0000642 opts = list-> m_options;
Rob Landleye9190962005-12-12 19:38:44 +0000643 while( opts ) {
644 /* Add one more option */
Rob Landley79e1cab2005-11-15 00:08:29 +0000645 argc++;
646 argv = (char**) xrealloc( argv, ( argc + 1 ) * sizeof( char* ) );
Rob Landleye9190962005-12-12 19:38:44 +0000647 argv[argc-1] = opts-> m_opt_val;
648 opts = opts-> m_next;
Rob Landley79e1cab2005-11-15 00:08:29 +0000649 }
Paul Fox8eeb6552005-08-04 18:33:36 +0000650 }
Glenn L McGrath350733a2003-09-08 00:32:49 +0000651 } else {
Eric Andersen807bd842004-08-19 18:30:31 +0000652 /* modutils uses short name for removal */
Rob Landley79e1cab2005-11-15 00:08:29 +0000653 if (already_loaded (list->m_name) != 0) {
Paul Fox8eeb6552005-08-04 18:33:36 +0000654 argv[argc++] = "rmmod";
655 if (do_syslog)
656 argv[argc++] = "-s";
657 argv[argc++] = list->m_name;
Rob Landleye9190962005-12-12 19:38:44 +0000658 if( ENABLE_FEATURE_CLEAN_UP )
659 argc_malloc = argc;
Paul Fox8eeb6552005-08-04 18:33:36 +0000660 }
Glenn L McGrath350733a2003-09-08 00:32:49 +0000661 }
Paul Fox8eeb6552005-08-04 18:33:36 +0000662 argv[argc] = NULL;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000663
Paul Fox8eeb6552005-08-04 18:33:36 +0000664 if (argc) {
Glenn L McGrathfcf47322004-08-11 05:56:30 +0000665 if (verbose) {
Rob Landleye9190962005-12-12 19:38:44 +0000666 printf("%s module %s\n", do_insert?"Loading":"Unloading", list-> m_name );
Glenn L McGrathfcf47322004-08-11 05:56:30 +0000667 }
668 if (!show_only) {
Paul Fox8eeb6552005-08-04 18:33:36 +0000669 int rc2 = 0;
670 int status;
671 switch (fork()) {
672 case -1:
673 rc2 = 1;
674 break;
675 case 0: //child
676 execvp(argv[0], argv);
677 bb_perror_msg_and_die("exec of %s", argv[0]);
678 /* NOTREACHED */
679 default:
680 if (wait(&status) == -1) {
681 rc2 = 1;
682 break;
683 }
684 if (WIFEXITED(status))
685 rc2 = WEXITSTATUS(status);
686 if (WIFSIGNALED(status))
687 rc2 = WTERMSIG(status);
688 break;
689 }
Glenn L McGrathfcf47322004-08-11 05:56:30 +0000690 if (do_insert) {
691 rc = rc2; /* only last module matters */
692 }
693 else if (!rc2) {
694 rc = 0; /* success if remove any mod */
695 }
696 }
Rob Landleye9190962005-12-12 19:38:44 +0000697 if( ENABLE_FEATURE_CLEAN_UP )
698 /* the last value in the array has index == argc, but
699 * it is the terminating NULL, so we must not free it. */
700 while( argc_malloc < argc ) {
701 free( argv[argc_malloc++] );
Rob Landley79e1cab2005-11-15 00:08:29 +0000702 }
Eric Andersen908e3622003-06-20 09:56:37 +0000703 }
Rob Landleye9190962005-12-12 19:38:44 +0000704 if( ENABLE_FEATURE_CLEAN_UP ) {
705 free( argv );
706 argv = NULL;
Rob Landley79e1cab2005-11-15 00:08:29 +0000707 }
Robert Griebl52e8d062002-05-14 23:42:08 +0000708 list = do_insert ? list-> m_prev : list-> m_next;
Eric Andersen60e56f52002-04-26 06:04:01 +0000709 }
Eric Andersen908e3622003-06-20 09:56:37 +0000710 return (show_only) ? 0 : rc;
Eric Andersen60e56f52002-04-26 06:04:01 +0000711}
712
Rob Landleye9190962005-12-12 19:38:44 +0000713/*
714 * Builds the dependency list (aka stack) of a module.
715 * head: the highest module in the stack (last to insmod, first to rmmod)
716 * tail: the lowest module in the stack (first to insmod, last to rmmod)
717 */
Robert Griebl52e8d062002-05-14 23:42:08 +0000718static void check_dep ( char *mod, struct mod_list_t **head, struct mod_list_t **tail )
Eric Andersen60e56f52002-04-26 06:04:01 +0000719{
Robert Griebl52e8d062002-05-14 23:42:08 +0000720 struct mod_list_t *find;
Eric Andersen60e56f52002-04-26 06:04:01 +0000721 struct dep_t *dt;
Rob Landleye9190962005-12-12 19:38:44 +0000722 struct mod_opt_t *opt = 0;
Eric Andersen807bd842004-08-19 18:30:31 +0000723 char *path = 0;
Robert Griebl52e8d062002-05-14 23:42:08 +0000724
Bernhard Reutner-Fischer2c351a82006-06-03 19:08:49 +0000725 /* Search for the given module name amongst all dependency rules.
726 * The module name in a dependency rule can be a shell pattern,
727 * so try to match the given module name against such a pattern.
728 * Of course if the name in the dependency rule is a plain string,
729 * then we consider it a pattern, and matching will still work. */
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000730 for ( dt = depend; dt; dt = dt-> m_next ) {
Bernhard Reutner-Fischer2c351a82006-06-03 19:08:49 +0000731 if ( fnmatch ( dt-> m_name, mod, 0 ) == 0) {
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000732 break;
Robert Grieblaead70b2002-07-26 15:54:20 +0000733 }
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000734 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000735
Rob Landleye9190962005-12-12 19:38:44 +0000736 if( !dt ) {
737 bb_error_msg ("module %s not found.", mod);
738 return;
739 }
740
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000741 // resolve alias names
Rob Landleye9190962005-12-12 19:38:44 +0000742 while ( dt-> m_isalias ) {
Robert Grieblaead70b2002-07-26 15:54:20 +0000743 if ( dt-> m_depcnt == 1 ) {
744 struct dep_t *adt;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000745
Robert Grieblaead70b2002-07-26 15:54:20 +0000746 for ( adt = depend; adt; adt = adt-> m_next ) {
Eric Andersen807bd842004-08-19 18:30:31 +0000747 if ( strcmp ( adt-> m_name, dt-> m_deparr [0] ) == 0 )
Robert Grieblaead70b2002-07-26 15:54:20 +0000748 break;
Robert Grieblaead70b2002-07-26 15:54:20 +0000749 }
Robert Griebl70112da2002-07-29 20:28:38 +0000750 if ( adt ) {
Rob Landleye9190962005-12-12 19:38:44 +0000751 /* This is the module we are aliased to */
752 struct mod_opt_t *opts = dt-> m_options;
753 /* Option of the alias are appended to the options of the module */
754 while( opts ) {
755 adt-> m_options = append_option( adt-> m_options, opts-> m_opt_val );
756 opts = opts-> m_next;
757 }
Eric Andersen716ccb22004-01-10 11:29:31 +0000758 dt = adt;
Robert Griebl70112da2002-07-29 20:28:38 +0000759 }
Rob Landleye9190962005-12-12 19:38:44 +0000760 else {
761 bb_error_msg ("module %s not found.", mod);
Robert Griebl70112da2002-07-29 20:28:38 +0000762 return;
Rob Landleye9190962005-12-12 19:38:44 +0000763 }
Robert Grieblaead70b2002-07-26 15:54:20 +0000764 }
Rob Landleye9190962005-12-12 19:38:44 +0000765 else {
766 bb_error_msg ("Bad alias %s", dt-> m_name);
Eric Andersen716ccb22004-01-10 11:29:31 +0000767 return;
Rob Landleye9190962005-12-12 19:38:44 +0000768 }
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000769 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000770
Rob Landleye9190962005-12-12 19:38:44 +0000771 mod = dt-> m_name;
772 path = dt-> m_path;
773 opt = dt-> m_options;
Bernhard Reutner-Fischer17d355c2005-12-14 08:32:44 +0000774
Robert Griebl52e8d062002-05-14 23:42:08 +0000775 // search for duplicates
776 for ( find = *head; find; find = find-> m_next ) {
Eric Andersen807bd842004-08-19 18:30:31 +0000777 if ( !strcmp ( mod, find-> m_name )) {
Robert Griebl52e8d062002-05-14 23:42:08 +0000778 // found -> dequeue it
779
780 if ( find-> m_prev )
781 find-> m_prev-> m_next = find-> m_next;
782 else
783 *head = find-> m_next;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000784
Robert Griebl52e8d062002-05-14 23:42:08 +0000785 if ( find-> m_next )
786 find-> m_next-> m_prev = find-> m_prev;
787 else
788 *tail = find-> m_prev;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000789
Robert Griebl52e8d062002-05-14 23:42:08 +0000790 break; // there can be only one duplicate
Eric Andersen716ccb22004-01-10 11:29:31 +0000791 }
Robert Griebl52e8d062002-05-14 23:42:08 +0000792 }
793
794 if ( !find ) { // did not find a duplicate
Eric Andersen716ccb22004-01-10 11:29:31 +0000795 find = (struct mod_list_t *) xmalloc ( sizeof(struct mod_list_t));
Eric Andersen807bd842004-08-19 18:30:31 +0000796 find-> m_name = mod;
797 find-> m_path = path;
Robert Grieblaead70b2002-07-26 15:54:20 +0000798 find-> m_options = opt;
Robert Griebl52e8d062002-05-14 23:42:08 +0000799 }
800
Eric Andersen716ccb22004-01-10 11:29:31 +0000801 // enqueue at tail
Robert Griebl52e8d062002-05-14 23:42:08 +0000802 if ( *tail )
803 (*tail)-> m_next = find;
804 find-> m_prev = *tail;
805 find-> m_next = 0;
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000806
Robert Griebl52e8d062002-05-14 23:42:08 +0000807 if ( !*head )
808 *head = find;
809 *tail = find;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000810
Eric Andersen716ccb22004-01-10 11:29:31 +0000811 if ( dt ) {
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000812 int i;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000813
Rob Landleye9190962005-12-12 19:38:44 +0000814 /* Add all dependable module for that new module */
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000815 for ( i = 0; i < dt-> m_depcnt; i++ )
816 check_dep ( dt-> m_deparr [i], head, tail );
817 }
Robert Griebl52e8d062002-05-14 23:42:08 +0000818}
819
Robert Griebl236abbf2002-05-22 23:34:35 +0000820static int mod_insert ( char *mod, int argc, char **argv )
Robert Griebl52e8d062002-05-14 23:42:08 +0000821{
822 struct mod_list_t *tail = 0;
Eric Andersen716ccb22004-01-10 11:29:31 +0000823 struct mod_list_t *head = 0;
Eric Andersen908e3622003-06-20 09:56:37 +0000824 int rc;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000825
Robert Griebl52e8d062002-05-14 23:42:08 +0000826 // get dep list for module mod
827 check_dep ( mod, &head, &tail );
Eric Andersen3b1a7442003-12-24 20:30:45 +0000828
Robert Griebl52e8d062002-05-14 23:42:08 +0000829 if ( head && tail ) {
Rob Landleye9190962005-12-12 19:38:44 +0000830 if( argc ) {
Eric Andersen716ccb22004-01-10 11:29:31 +0000831 int i;
Robert Grieblaead70b2002-07-26 15:54:20 +0000832 // append module args
Eric Andersen716ccb22004-01-10 11:29:31 +0000833 for ( i = 0; i < argc; i++ )
Rob Landleye9190962005-12-12 19:38:44 +0000834 head->m_options = append_option( head->m_options, argv[i] );
Robert Griebl52e8d062002-05-14 23:42:08 +0000835 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000836
Robert Griebl52e8d062002-05-14 23:42:08 +0000837 // process tail ---> head
Eric Andersen908e3622003-06-20 09:56:37 +0000838 rc = mod_process ( tail, 1 );
Robert Griebl52e8d062002-05-14 23:42:08 +0000839 }
840 else
841 rc = 1;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000842
Robert Griebl52e8d062002-05-14 23:42:08 +0000843 return rc;
844}
845
Eric Andersen908e3622003-06-20 09:56:37 +0000846static int mod_remove ( char *mod )
Robert Griebl52e8d062002-05-14 23:42:08 +0000847{
Eric Andersen908e3622003-06-20 09:56:37 +0000848 int rc;
"Vladimir N. Oleynik"4fc92202006-02-02 14:48:54 +0000849 static struct mod_list_t rm_a_dummy = { "-a", NULL, NULL, NULL, NULL };
Eric Andersen3b1a7442003-12-24 20:30:45 +0000850
Robert Griebl52e8d062002-05-14 23:42:08 +0000851 struct mod_list_t *head = 0;
852 struct mod_list_t *tail = 0;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000853
Robert Griebl52e8d062002-05-14 23:42:08 +0000854 if ( mod )
855 check_dep ( mod, &head, &tail );
856 else // autoclean
857 head = tail = &rm_a_dummy;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000858
Robert Griebl236abbf2002-05-22 23:34:35 +0000859 if ( head && tail )
Eric Andersen908e3622003-06-20 09:56:37 +0000860 rc = mod_process ( head, 0 ); // process head ---> tail
861 else
862 rc = 1;
863 return rc;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000864
Robert Griebl52e8d062002-05-14 23:42:08 +0000865}
866
Rob Landleydfba7412006-03-06 20:47:33 +0000867int modprobe_main(int argc, char** argv)
Eric Andersen0139ca92001-07-22 23:01:03 +0000868{
Rob Landleye9190962005-12-12 19:38:44 +0000869 int rc = EXIT_SUCCESS;
"Vladimir N. Oleynik"4fc92202006-02-02 14:48:54 +0000870 char *unused;
Eric Andersen0139ca92001-07-22 23:01:03 +0000871
"Vladimir N. Oleynik"4fc92202006-02-02 14:48:54 +0000872 bb_opt_complementally = "?V-:q-v:v-q";
873 main_opts = bb_getopt_ulflags(argc, argv, "acdklnqrst:vVC:",
874 &unused, &unused);
875 if((main_opts & (DUMP_CONF_EXIT | LIST_ALL)))
Eric Andersen3b1a7442003-12-24 20:30:45 +0000876 return EXIT_SUCCESS;
"Vladimir N. Oleynik"4fc92202006-02-02 14:48:54 +0000877 if((main_opts & (RESTRICT_DIR | CONFIG_FILE)))
Eric Andersen3b1a7442003-12-24 20:30:45 +0000878 bb_error_msg_and_die("-t and -C not supported");
Robert Griebl236abbf2002-05-22 23:34:35 +0000879
Eric Andersen716ccb22004-01-10 11:29:31 +0000880 depend = build_dep ( );
Robert Griebl52e8d062002-05-14 23:42:08 +0000881
Eric Andersen716ccb22004-01-10 11:29:31 +0000882 if ( !depend )
Manuel Novoa III cad53642003-03-19 09:13:01 +0000883 bb_error_msg_and_die ( "could not parse modules.dep\n" );
Eric Andersen3b1a7442003-12-24 20:30:45 +0000884
Eric Andersen1b064192001-07-25 07:23:38 +0000885 if (remove_opt) {
Eric Andersen0139ca92001-07-22 23:01:03 +0000886 do {
Eric Andersen908e3622003-06-20 09:56:37 +0000887 if (mod_remove ( optind < argc ?
"Vladimir N. Oleynik"4fc92202006-02-02 14:48:54 +0000888 argv [optind] : NULL )) {
Eric Andersen908e3622003-06-20 09:56:37 +0000889 bb_error_msg ("failed to remove module %s",
Eric Andersen3b1a7442003-12-24 20:30:45 +0000890 argv [optind] );
Eric Andersen908e3622003-06-20 09:56:37 +0000891 rc = EXIT_FAILURE;
892 }
Robert Griebl52e8d062002-05-14 23:42:08 +0000893 } while ( ++optind < argc );
Rob Landleye9190962005-12-12 19:38:44 +0000894 } else {
895 if (optind >= argc)
896 bb_error_msg_and_die ( "No module or pattern provided\n" );
Eric Andersen3b1a7442003-12-24 20:30:45 +0000897
"Vladimir N. Oleynik"4fc92202006-02-02 14:48:54 +0000898 if ( mod_insert ( argv [optind], argc - optind - 1, argv + optind + 1 ))
Rob Landleye9190962005-12-12 19:38:44 +0000899 bb_error_msg_and_die ( "failed to load module %s", argv [optind] );
Eric Andersen0139ca92001-07-22 23:01:03 +0000900 }
901
Rob Landleye9190962005-12-12 19:38:44 +0000902 /* Here would be a good place to free up memory allocated during the dependencies build. */
Eric Andersen3b1a7442003-12-24 20:30:45 +0000903
Rob Landleye9190962005-12-12 19:38:44 +0000904 return rc;
Eric Andersen0139ca92001-07-22 23:01:03 +0000905}