blob: 32a37ce1711226742284cf82a5d2c8dd93f070a6 [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;
"Vladimir N. Oleynik"4fc92202006-02-02 14:48:54 +000058
59#define main_options "acdklnqrst:vVC:"
60#define INSERT_ALL 1 /* a */
61#define DUMP_CONF_EXIT 2 /* c */
62#define D_OPT_IGNORED 4 /* d */
63#define AUTOCLEAN_FLG 8 /* k */
64#define LIST_ALL 16 /* l */
65#define SHOW_ONLY 32 /* n */
66#define QUIET 64 /* q */
67#define REMOVE_OPT 128 /* r */
68#define DO_SYSLOG 256 /* s */
69#define RESTRICT_DIR 512 /* t */
70#define VERBOSE 1024 /* v */
71#define VERSION_ONLY 2048 /* V */
72#define CONFIG_FILE 4096 /* C */
73
74#define autoclean (main_opts & AUTOCLEAN_FLG)
75#define show_only (main_opts & SHOW_ONLY)
76#define quiet (main_opts & QUIET)
77#define remove_opt (main_opts & REMOVE_OPT)
78#define do_syslog (main_opts & DO_SYSLOG)
79#define verbose (main_opts & VERBOSE)
80
81static int main_opts;
Robert Griebl52e8d062002-05-14 23:42:08 +000082
Eric Andersen14f5c8d2005-04-16 19:39:00 +000083static int parse_tag_value ( char *buffer, char **ptag, char **pvalue )
Robert Grieblaead70b2002-07-26 15:54:20 +000084{
85 char *tag, *value;
86
87 while ( isspace ( *buffer ))
Eric Andersen716ccb22004-01-10 11:29:31 +000088 buffer++;
Robert Grieblaead70b2002-07-26 15:54:20 +000089 tag = value = buffer;
90 while ( !isspace ( *value ))
Eric Andersen7e496a72004-04-06 12:06:03 +000091 if (!*value) return 0;
92 else value++;
Robert Grieblaead70b2002-07-26 15:54:20 +000093 *value++ = 0;
94 while ( isspace ( *value ))
95 value++;
Eric Andersen7e496a72004-04-06 12:06:03 +000096 if (!*value) return 0;
Robert Grieblaead70b2002-07-26 15:54:20 +000097
98 *ptag = tag;
99 *pvalue = value;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000100
Eric Andersen7e496a72004-04-06 12:06:03 +0000101 return 1;
Robert Grieblaead70b2002-07-26 15:54:20 +0000102}
Eric Andersen60e56f52002-04-26 06:04:01 +0000103
Robert Grieblbc28f7a2002-06-04 19:33:58 +0000104/* Jump through hoops to simulate how fgets() grabs just one line at a
105 * time... Don't use any stdio since modprobe gets called from a kernel
Eric Andersen716ccb22004-01-10 11:29:31 +0000106 * thread and stdio junk can overflow the limited stack...
Robert Grieblbc28f7a2002-06-04 19:33:58 +0000107 */
108static char *reads ( int fd, char *buffer, size_t len )
109{
110 int n = read ( fd, buffer, len );
Eric Andersen3b1a7442003-12-24 20:30:45 +0000111
Robert Grieblbc28f7a2002-06-04 19:33:58 +0000112 if ( n > 0 ) {
113 char *p;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000114
Robert Grieblbc28f7a2002-06-04 19:33:58 +0000115 buffer [len-1] = 0;
116 p = strchr ( buffer, '\n' );
Eric Andersen3b1a7442003-12-24 20:30:45 +0000117
Robert Grieblbc28f7a2002-06-04 19:33:58 +0000118 if ( p ) {
119 off_t offset;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000120
Eric Andersen716ccb22004-01-10 11:29:31 +0000121 offset = lseek ( fd, 0L, SEEK_CUR ); // Get the current file descriptor offset
Robert Grieblbc28f7a2002-06-04 19:33:58 +0000122 lseek ( fd, offset-n + (p-buffer) + 1, SEEK_SET ); // Set the file descriptor offset to right after the \n
123
124 p[1] = 0;
125 }
126 return buffer;
127 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000128
Robert Grieblbc28f7a2002-06-04 19:33:58 +0000129 else
130 return 0;
131}
132
Rob Landleye9190962005-12-12 19:38:44 +0000133/*
134 * This function appends an option to a list
135 */
Bernhard Reutner-Fischer101a4702006-04-03 15:46:14 +0000136static struct mod_opt_t *append_option( struct mod_opt_t *opt_list, char *opt )
Eric Andersen60e56f52002-04-26 06:04:01 +0000137{
Rob Landleye9190962005-12-12 19:38:44 +0000138 struct mod_opt_t *ol = opt_list;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000139
Rob Landleye9190962005-12-12 19:38:44 +0000140 if( ol ) {
141 while( ol-> m_next ) {
142 ol = ol-> m_next;
143 }
144 ol-> m_next = xmalloc( sizeof( struct mod_opt_t ) );
145 ol = ol-> m_next;
146 } else {
147 ol = opt_list = xmalloc( sizeof( struct mod_opt_t ) );
Eric Andersen03d80912003-12-19 21:04:19 +0000148 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000149
Rob Landleye9190962005-12-12 19:38:44 +0000150 ol-> m_opt_val = bb_xstrdup( opt );
151 ol-> m_next = NULL;
Eric Andersen60e56f52002-04-26 06:04:01 +0000152
Rob Landleye9190962005-12-12 19:38:44 +0000153 return opt_list;
Eric Andersen60e56f52002-04-26 06:04:01 +0000154}
155
Bernhard Reutner-Fischer17d355c2005-12-14 08:32:44 +0000156#if ENABLE_FEATURE_MODPROBE_MULTIPLE_OPTIONS
Rob Landley79e1cab2005-11-15 00:08:29 +0000157/* static char* parse_command_string( char* src, char **dst );
158 * src: pointer to string containing argument
159 * dst: pointer to where to store the parsed argument
160 * return value: the pointer to the first char after the parsed argument,
161 * NULL if there was no argument parsed (only trailing spaces).
162 * Note that memory is allocated with bb_xstrdup when a new argument was
163 * parsed. Don't forget to free it!
164 */
165#define ARG_EMPTY 0x00
166#define ARG_IN_DQUOTES 0x01
167#define ARG_IN_SQUOTES 0x02
168static char *parse_command_string( char *src, char **dst )
169{
170 int opt_status = ARG_EMPTY;
171 char* tmp_str;
172
173 /* Dumb you, I have nothing to do... */
174 if( src == NULL ) return src;
175
176 /* Skip leading spaces */
177 while( *src == ' ' ) {
178 src++;
179 }
180 /* Is the end of string reached? */
181 if( *src == '\0' ) {
182 return NULL;
183 }
184 /* Reached the start of an argument
Rob Landleye9190962005-12-12 19:38:44 +0000185 * By the way, we duplicate a little too much
186 * here but what is too much is freed later. */
Rob Landley79e1cab2005-11-15 00:08:29 +0000187 *dst = tmp_str = bb_xstrdup( src );
188 /* Get to the end of that argument */
189 while( ( *tmp_str != '\0' )
Rob Landleye9190962005-12-12 19:38:44 +0000190 && ( ( *tmp_str != ' ' )
191 || ( opt_status & ( ARG_IN_DQUOTES | ARG_IN_SQUOTES ) ) ) ) {
Rob Landley79e1cab2005-11-15 00:08:29 +0000192 switch( *tmp_str ) {
193 case '\'':
194 if( opt_status & ARG_IN_DQUOTES ) {
195 /* Already in double quotes, keep current char as is */
196 } else {
197 /* shift left 1 char, until end of string: get rid of the opening/closing quotes */
198 memmove( tmp_str, tmp_str + 1, strlen( tmp_str ) );
199 /* mark me: we enter or leave single quotes */
200 opt_status ^= ARG_IN_SQUOTES;
201 /* Back one char, as we need to re-scan the new char there. */
202 tmp_str--;
203 }
204 break;
205 case '"':
206 if( opt_status & ARG_IN_SQUOTES ) {
207 /* Already in single quotes, keep current char as is */
208 } else {
209 /* shift left 1 char, until end of string: get rid of the opening/closing quotes */
210 memmove( tmp_str, tmp_str + 1, strlen( tmp_str ) );
211 /* mark me: we enter or leave double quotes */
212 opt_status ^= ARG_IN_DQUOTES;
213 /* Back one char, as we need to re-scan the new char there. */
214 tmp_str--;
215 }
216 break;
217 case '\\':
218 if( opt_status & ARG_IN_SQUOTES ) {
219 /* Between single quotes: keep as is. */
220 } else {
221 switch( *(tmp_str+1) ) {
222 case 'a':
223 case 'b':
224 case 't':
225 case 'n':
226 case 'v':
227 case 'f':
228 case 'r':
229 case '0':
230 /* We escaped a special character. For now, keep
231 * both the back-slash and the following char. */
232 tmp_str++; src++;
233 break;
234 default:
235 /* We escaped a space or a single or double quote,
236 * or a back-slash, or a non-escapable char. Remove
237 * the '\' and keep the new current char as is. */
238 memmove( tmp_str, tmp_str + 1, strlen( tmp_str ) );
239 break;
240 }
241 }
242 break;
243 /* Any other char that is special shall appear here.
244 * Example: $ starts a variable
245 case '$':
246 do_variable_expansion();
247 break;
248 * */
249 default:
250 /* any other char is kept as is. */
251 break;
252 }
253 tmp_str++; /* Go to next char */
254 src++; /* Go to next char to find the end of the argument. */
255 }
256 /* End of string, but still no ending quote */
257 if( opt_status & ( ARG_IN_DQUOTES | ARG_IN_SQUOTES ) ) {
258 bb_error_msg_and_die( "unterminated (single or double) quote in options list: %s", src );
259 }
"Vladimir N. Oleynik"4fc92202006-02-02 14:48:54 +0000260 *tmp_str++ = '\0';
261 *dst = xrealloc( *dst, (tmp_str - *dst ) );
Rob Landley79e1cab2005-11-15 00:08:29 +0000262 return src;
263}
Rob Landleyae50c6d2005-12-15 07:42:13 +0000264#else
265#define parse_command_string(src, dst) (0)
Bernhard Reutner-Fischer17d355c2005-12-14 08:32:44 +0000266#endif /* ENABLE_FEATURE_MODPROBE_MULTIPLE_OPTIONS */
Rob Landley79e1cab2005-11-15 00:08:29 +0000267
Rob Landleye9190962005-12-12 19:38:44 +0000268/*
Rob Landley72615752006-04-10 16:09:52 +0000269 * This function reads aliases and default module options from a configuration file
270 * (/etc/modprobe.conf syntax). It supports includes (only files, no directories).
271 */
272static void include_conf ( struct dep_t **first, struct dep_t **current, char *buffer, int buflen, int fd )
273{
274 int continuation_line = 0;
275
276 // alias parsing is not 100% correct (no correct handling of continuation lines within an alias) !
277
278 while ( reads ( fd, buffer, buflen)) {
279 int l;
280 char *p;
281
282 p = strchr ( buffer, '#' );
283 if ( p )
284 *p = 0;
285
286 l = bb_strlen ( buffer );
287
288 while ( l && isspace ( buffer [l-1] )) {
289 buffer [l-1] = 0;
290 l--;
291 }
292
293 if ( l == 0 ) {
294 continuation_line = 0;
295 continue;
296 }
297
298 if ( !continuation_line ) {
299 if (( strncmp ( buffer, "alias", 5 ) == 0 ) && isspace ( buffer [5] )) {
300 char *alias, *mod;
301
302 if ( parse_tag_value ( buffer + 6, &alias, &mod )) {
303 /* handle alias as a module dependent on the aliased module */
304 if ( !*current ) {
305 (*first) = (*current) = (struct dep_t *) xcalloc ( 1, sizeof ( struct dep_t ));
306 }
307 else {
308 (*current)-> m_next = (struct dep_t *) xcalloc ( 1, sizeof ( struct dep_t ));
309 (*current) = (*current)-> m_next;
310 }
311 (*current)-> m_name = bb_xstrdup ( alias );
312 (*current)-> m_isalias = 1;
313
314 if (( strcmp ( mod, "off" ) == 0 ) || ( strcmp ( mod, "null" ) == 0 )) {
315 (*current)-> m_depcnt = 0;
316 (*current)-> m_deparr = 0;
317 }
318 else {
319 (*current)-> m_depcnt = 1;
320 (*current)-> m_deparr = xmalloc ( 1 * sizeof( char * ));
321 (*current)-> m_deparr[0] = bb_xstrdup ( mod );
322 }
323 (*current)-> m_next = 0;
324 }
325 }
326 else if (( strncmp ( buffer, "options", 7 ) == 0 ) && isspace ( buffer [7] )) {
327 char *mod, *opt;
328
329 /* split the line in the module/alias name, and options */
330 if ( parse_tag_value ( buffer + 8, &mod, &opt )) {
331 struct dep_t *dt;
332
333 /* find the corresponding module */
334 for ( dt = *first; dt; dt = dt-> m_next ) {
335 if ( strcmp ( dt-> m_name, mod ) == 0 )
336 break;
337 }
338 if ( dt ) {
339 if ( ENABLE_FEATURE_MODPROBE_MULTIPLE_OPTIONS ) {
340 char* new_opt = NULL;
341 while( ( opt = parse_command_string( opt, &new_opt ) ) ) {
342 dt-> m_options = append_option( dt-> m_options, new_opt );
343 }
344 } else {
345 dt-> m_options = append_option( dt-> m_options, opt );
346 }
347 }
348 }
349 }
350 else if (( strncmp ( buffer, "include", 7 ) == 0 ) && isspace ( buffer [7] )) {
351
352 int fdi; char *filename = buffer + 8;
353
354 while ( isspace ( *filename ))
355 filename++;
356
357 if (( fdi = open ( filename, O_RDONLY )) >= 0 ) {
358 include_conf(first, current, buffer, buflen, fdi);
359 close(fdi);
360 }
361 }
362 }
363 }
364}
365
366/*
Rob Landleye9190962005-12-12 19:38:44 +0000367 * This function builds a list of dependency rules from /lib/modules/`uname -r\modules.dep.
368 * It then fills every modules and aliases with their default options, found by parsing
369 * modprobe.conf (or modules.conf, or conf.modules).
370 */
371static struct dep_t *build_dep ( void )
372{
373 int fd;
374 struct utsname un;
375 struct dep_t *first = 0;
376 struct dep_t *current = 0;
377 char buffer[2048];
"Vladimir N. Oleynik"4fc92202006-02-02 14:48:54 +0000378 char *filename;
Rob Landleye9190962005-12-12 19:38:44 +0000379 int continuation_line = 0;
380 int k_version;
381
382 k_version = 0;
383 if ( uname ( &un ))
384 bb_error_msg_and_die("can't determine kernel version");
385
Rob Landleye9190962005-12-12 19:38:44 +0000386 if (un.release[0] == '2') {
387 k_version = un.release[2] - '0';
388 }
389
"Vladimir N. Oleynik"4fc92202006-02-02 14:48:54 +0000390 filename = bb_xasprintf("/lib/modules/%s/modules.dep", un.release );
Rob Landleye9190962005-12-12 19:38:44 +0000391
392 if (( fd = open ( filename, O_RDONLY )) < 0 ) {
393
394 /* Ok, that didn't work. Fall back to looking in /lib/modules */
395 if (( fd = open ( "/lib/modules/modules.dep", O_RDONLY )) < 0 ) {
396 return 0;
397 }
398 }
"Vladimir N. Oleynik"4fc92202006-02-02 14:48:54 +0000399 free(filename);
Rob Landleye9190962005-12-12 19:38:44 +0000400
401 while ( reads ( fd, buffer, sizeof( buffer ))) {
402 int l = bb_strlen ( buffer );
403 char *p = 0;
404
"Vladimir N. Oleynik"4fc92202006-02-02 14:48:54 +0000405 while ( l > 0 && isspace ( buffer [l-1] )) {
Rob Landleye9190962005-12-12 19:38:44 +0000406 buffer [l-1] = 0;
407 l--;
408 }
409
410 if ( l == 0 ) {
411 continuation_line = 0;
412 continue;
413 }
414
415 /* Is this a new module dep description? */
416 if ( !continuation_line ) {
Bernhard Reutner-Fischer17d355c2005-12-14 08:32:44 +0000417 /* find the dep beginning */
Rob Landleye9190962005-12-12 19:38:44 +0000418 char *col = strchr ( buffer, ':' );
419 char *dot = col;
420
421 if ( col ) {
422 /* This line is a dep description */
423 char *mods;
424 char *modpath;
425 char *mod;
426
427 /* Find the beginning of the module file name */
428 *col = 0;
429 mods = strrchr ( buffer, '/' );
430
431 if ( !mods )
432 mods = buffer; /* no path for this module */
433 else
434 mods++; /* there was a path for this module... */
435
436 /* find the path of the module */
437 modpath = strchr ( buffer, '/' ); /* ... and this is the path */
438 if ( !modpath )
439 modpath = buffer; /* module with no path */
440 /* find the end of the module name in the file name */
441 if ( ENABLE_FEATURE_2_6_MODULES &&
442 (k_version > 4) && ( *(col-3) == '.' ) &&
443 ( *(col-2) == 'k' ) && ( *(col-1) == 'o' ) )
444 dot = col - 3;
445 else
446 if (( *(col-2) == '.' ) && ( *(col-1) == 'o' ))
447 dot = col - 2;
448
449 mod = bb_xstrndup ( mods, dot - mods );
450
451 /* enqueue new module */
452 if ( !current ) {
453 first = current = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
454 }
455 else {
456 current-> m_next = (struct dep_t *) xmalloc ( sizeof ( struct dep_t ));
457 current = current-> m_next;
458 }
459 current-> m_name = mod;
460 current-> m_path = bb_xstrdup(modpath);
461 current-> m_options = NULL;
462 current-> m_isalias = 0;
463 current-> m_depcnt = 0;
464 current-> m_deparr = 0;
465 current-> m_next = 0;
466
467 p = col + 1;
468 }
469 else
470 /* this line is not a dep description */
471 p = 0;
472 }
473 else
474 /* It's a dep description continuation */
475 p = buffer;
476
477 while ( p && *p && isblank(*p))
478 p++;
479
480 /* p points to the first dependable module; if NULL, no dependable module */
481 if ( p && *p ) {
482 char *end = &buffer [l-1];
483 char *deps;
484 char *dep;
485 char *next;
486 int ext = 0;
487
488 while ( isblank ( *end ) || ( *end == '\\' ))
489 end--;
490
491 do
492 {
493 /* search the end of the dependency */
494 next = strchr (p, ' ' );
495 if (next)
496 {
497 *next = 0;
498 next--;
499 }
500 else
501 next = end;
502
Bernhard Reutner-Fischer17d355c2005-12-14 08:32:44 +0000503 /* find the beginning of the module file name */
Rob Landleye9190962005-12-12 19:38:44 +0000504 deps = strrchr ( p, '/' );
505
506 if ( !deps || ( deps < p )) {
507 deps = p;
508
509 while ( isblank ( *deps ))
510 deps++;
511 }
512 else
513 deps++;
514
515 /* find the end of the module name in the file name */
516 if ( ENABLE_FEATURE_2_6_MODULES &&
517 (k_version > 4) && ( *(next-2) == '.' ) &&
518 ( *(next-1) == 'k' ) && ( *next == 'o' ) )
519 ext = 3;
520 else
521 if (( *(next-1) == '.' ) && ( *next == 'o' ))
522 ext = 2;
523
524 /* Cope with blank lines */
525 if ((next-deps-ext+1) <= 0)
526 continue;
527 dep = bb_xstrndup ( deps, next - deps - ext + 1 );
528
529 /* Add the new dependable module name */
530 current-> m_depcnt++;
531 current-> m_deparr = (char **) xrealloc ( current-> m_deparr,
532 sizeof ( char *) * current-> m_depcnt );
533 current-> m_deparr [current-> m_depcnt - 1] = dep;
534
535 p = next + 2;
536 } while (next < end);
537 }
538
539 /* is there other dependable module(s) ? */
540 if ( buffer [l-1] == '\\' )
541 continuation_line = 1;
542 else
543 continuation_line = 0;
544 }
545 close ( fd );
546
Rob Landleyae50c6d2005-12-15 07:42:13 +0000547 if (!ENABLE_FEATURE_2_6_MODULES
548 || ( fd = open ( "/etc/modprobe.conf", O_RDONLY )) < 0 )
Rob Landleye9190962005-12-12 19:38:44 +0000549 if (( fd = open ( "/etc/modules.conf", O_RDONLY )) < 0 )
550 if (( fd = open ( "/etc/conf.modules", O_RDONLY )) < 0 )
551 return first;
552
Rob Landley72615752006-04-10 16:09:52 +0000553 include_conf (&first, &current, buffer, sizeof(buffer), fd);
554 close(fd);
Rob Landleye9190962005-12-12 19:38:44 +0000555
Rob Landley72615752006-04-10 16:09:52 +0000556 filename = bb_xasprintf("/lib/modules/%s/modules.alias", un.release );
Rob Landleye9190962005-12-12 19:38:44 +0000557
Rob Landley72615752006-04-10 16:09:52 +0000558 if (( fd = open ( filename, O_RDONLY )) < 0 ) {
Rob Landleye9190962005-12-12 19:38:44 +0000559
Rob Landley72615752006-04-10 16:09:52 +0000560 /* Ok, that didn't work. Fall back to looking in /lib/modules */
561 if (( fd = open ( "/lib/modules/modules.alias", O_RDONLY )) < 0 ) {
562 return first;
Rob Landleye9190962005-12-12 19:38:44 +0000563 }
564 }
Rob Landley72615752006-04-10 16:09:52 +0000565 free(filename);
566
567 include_conf (&first, &current, buffer, sizeof(buffer), fd);
568 close(fd);
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{
576 int fd;
577 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) {
588 *p = 0;
589 for( p = buffer; ENABLE_FEATURE_2_6_MODULES && *p; p++ ) {
590 *p = ((*p)=='-')?'_':*p;
591 }
592 if (strcmp (name, buffer) == 0) {
593 close (fd);
594 return 1;
595 }
596 }
597 }
598
599 close (fd);
600 return 0;
601}
602
Robert Griebl236abbf2002-05-22 23:34:35 +0000603static int mod_process ( struct mod_list_t *list, int do_insert )
Eric Andersen60e56f52002-04-26 06:04:01 +0000604{
Eric Andersen44b57582004-08-03 08:23:33 +0000605 int rc = 0;
Rob Landley79e1cab2005-11-15 00:08:29 +0000606 char **argv = NULL;
Rob Landleye9190962005-12-12 19:38:44 +0000607 struct mod_opt_t *opts;
608 int argc_malloc; /* never used when CONFIG_FEATURE_CLEAN_UP not defined */
Paul Fox8eeb6552005-08-04 18:33:36 +0000609 int argc;
Eric Andersen864b7972002-05-03 15:48:26 +0000610
Robert Griebl52e8d062002-05-14 23:42:08 +0000611 while ( list ) {
Paul Fox8eeb6552005-08-04 18:33:36 +0000612 argc = 0;
Rob Landleye9190962005-12-12 19:38:44 +0000613 if( ENABLE_FEATURE_CLEAN_UP )
614 argc_malloc = 0;
615 /* If CONFIG_FEATURE_CLEAN_UP is not defined, then we leak memory
616 * each time we allocate memory for argv.
617 * But it is (quite) small amounts of memory that leak each
618 * time a module is loaded, and it is reclaimed when modprobe
619 * exits anyway (even when standalone shell?).
620 * This could become a problem when loading a module with LOTS of
621 * dependencies, with LOTS of options for each dependencies, with
622 * very little memory on the target... But in that case, the module
623 * would not load because there is no more memory, so there's no
624 * problem. */
625 /* enough for minimal insmod (5 args + NULL) or rmmod (3 args + NULL) */
626 argv = (char**) malloc( 6 * sizeof( char* ) );
Glenn L McGrath350733a2003-09-08 00:32:49 +0000627 if ( do_insert ) {
Rob Landley79e1cab2005-11-15 00:08:29 +0000628 if (already_loaded (list->m_name) != 1) {
Paul Fox8eeb6552005-08-04 18:33:36 +0000629 argv[argc++] = "insmod";
630 if (do_syslog)
631 argv[argc++] = "-s";
632 if (autoclean)
633 argv[argc++] = "-k";
634 if (quiet)
635 argv[argc++] = "-q";
Rob Landley79e1cab2005-11-15 00:08:29 +0000636 else if(verbose) /* verbose and quiet are mutually exclusive */
637 argv[argc++] = "-v";
Paul Fox8eeb6552005-08-04 18:33:36 +0000638 argv[argc++] = list-> m_path;
Rob Landleye9190962005-12-12 19:38:44 +0000639 if( ENABLE_FEATURE_CLEAN_UP )
640 argc_malloc = argc;
Rob Landley79e1cab2005-11-15 00:08:29 +0000641 opts = list-> m_options;
Rob Landleye9190962005-12-12 19:38:44 +0000642 while( opts ) {
643 /* Add one more option */
Rob Landley79e1cab2005-11-15 00:08:29 +0000644 argc++;
645 argv = (char**) xrealloc( argv, ( argc + 1 ) * sizeof( char* ) );
Rob Landleye9190962005-12-12 19:38:44 +0000646 argv[argc-1] = opts-> m_opt_val;
647 opts = opts-> m_next;
Rob Landley79e1cab2005-11-15 00:08:29 +0000648 }
Paul Fox8eeb6552005-08-04 18:33:36 +0000649 }
Glenn L McGrath350733a2003-09-08 00:32:49 +0000650 } else {
Eric Andersen807bd842004-08-19 18:30:31 +0000651 /* modutils uses short name for removal */
Rob Landley79e1cab2005-11-15 00:08:29 +0000652 if (already_loaded (list->m_name) != 0) {
Paul Fox8eeb6552005-08-04 18:33:36 +0000653 argv[argc++] = "rmmod";
654 if (do_syslog)
655 argv[argc++] = "-s";
656 argv[argc++] = list->m_name;
Rob Landleye9190962005-12-12 19:38:44 +0000657 if( ENABLE_FEATURE_CLEAN_UP )
658 argc_malloc = argc;
Paul Fox8eeb6552005-08-04 18:33:36 +0000659 }
Glenn L McGrath350733a2003-09-08 00:32:49 +0000660 }
Paul Fox8eeb6552005-08-04 18:33:36 +0000661 argv[argc] = NULL;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000662
Paul Fox8eeb6552005-08-04 18:33:36 +0000663 if (argc) {
Glenn L McGrathfcf47322004-08-11 05:56:30 +0000664 if (verbose) {
Rob Landleye9190962005-12-12 19:38:44 +0000665 printf("%s module %s\n", do_insert?"Loading":"Unloading", list-> m_name );
Glenn L McGrathfcf47322004-08-11 05:56:30 +0000666 }
667 if (!show_only) {
Paul Fox8eeb6552005-08-04 18:33:36 +0000668 int rc2 = 0;
669 int status;
670 switch (fork()) {
671 case -1:
672 rc2 = 1;
673 break;
674 case 0: //child
675 execvp(argv[0], argv);
676 bb_perror_msg_and_die("exec of %s", argv[0]);
677 /* NOTREACHED */
678 default:
679 if (wait(&status) == -1) {
680 rc2 = 1;
681 break;
682 }
683 if (WIFEXITED(status))
684 rc2 = WEXITSTATUS(status);
685 if (WIFSIGNALED(status))
686 rc2 = WTERMSIG(status);
687 break;
688 }
Glenn L McGrathfcf47322004-08-11 05:56:30 +0000689 if (do_insert) {
690 rc = rc2; /* only last module matters */
691 }
692 else if (!rc2) {
693 rc = 0; /* success if remove any mod */
694 }
695 }
Rob Landleye9190962005-12-12 19:38:44 +0000696 if( ENABLE_FEATURE_CLEAN_UP )
697 /* the last value in the array has index == argc, but
698 * it is the terminating NULL, so we must not free it. */
699 while( argc_malloc < argc ) {
700 free( argv[argc_malloc++] );
Rob Landley79e1cab2005-11-15 00:08:29 +0000701 }
Eric Andersen908e3622003-06-20 09:56:37 +0000702 }
Rob Landleye9190962005-12-12 19:38:44 +0000703 if( ENABLE_FEATURE_CLEAN_UP ) {
704 free( argv );
705 argv = NULL;
Rob Landley79e1cab2005-11-15 00:08:29 +0000706 }
Robert Griebl52e8d062002-05-14 23:42:08 +0000707 list = do_insert ? list-> m_prev : list-> m_next;
Eric Andersen60e56f52002-04-26 06:04:01 +0000708 }
Eric Andersen908e3622003-06-20 09:56:37 +0000709 return (show_only) ? 0 : rc;
Eric Andersen60e56f52002-04-26 06:04:01 +0000710}
711
Rob Landleye9190962005-12-12 19:38:44 +0000712/*
713 * Builds the dependency list (aka stack) of a module.
714 * head: the highest module in the stack (last to insmod, first to rmmod)
715 * tail: the lowest module in the stack (first to insmod, last to rmmod)
716 */
Robert Griebl52e8d062002-05-14 23:42:08 +0000717static void check_dep ( char *mod, struct mod_list_t **head, struct mod_list_t **tail )
Eric Andersen60e56f52002-04-26 06:04:01 +0000718{
Robert Griebl52e8d062002-05-14 23:42:08 +0000719 struct mod_list_t *find;
Eric Andersen60e56f52002-04-26 06:04:01 +0000720 struct dep_t *dt;
Rob Landleye9190962005-12-12 19:38:44 +0000721 struct mod_opt_t *opt = 0;
Eric Andersen807bd842004-08-19 18:30:31 +0000722 char *path = 0;
Robert Griebl52e8d062002-05-14 23:42:08 +0000723
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000724 // check dependencies
725 for ( dt = depend; dt; dt = dt-> m_next ) {
Eric Andersen807bd842004-08-19 18:30:31 +0000726 if ( strcmp ( dt-> m_name, mod ) == 0) {
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000727 break;
Robert Grieblaead70b2002-07-26 15:54:20 +0000728 }
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000729 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000730
Rob Landleye9190962005-12-12 19:38:44 +0000731 if( !dt ) {
732 bb_error_msg ("module %s not found.", mod);
733 return;
734 }
735
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000736 // resolve alias names
Rob Landleye9190962005-12-12 19:38:44 +0000737 while ( dt-> m_isalias ) {
Robert Grieblaead70b2002-07-26 15:54:20 +0000738 if ( dt-> m_depcnt == 1 ) {
739 struct dep_t *adt;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000740
Robert Grieblaead70b2002-07-26 15:54:20 +0000741 for ( adt = depend; adt; adt = adt-> m_next ) {
Eric Andersen807bd842004-08-19 18:30:31 +0000742 if ( strcmp ( adt-> m_name, dt-> m_deparr [0] ) == 0 )
Robert Grieblaead70b2002-07-26 15:54:20 +0000743 break;
Robert Grieblaead70b2002-07-26 15:54:20 +0000744 }
Robert Griebl70112da2002-07-29 20:28:38 +0000745 if ( adt ) {
Rob Landleye9190962005-12-12 19:38:44 +0000746 /* This is the module we are aliased to */
747 struct mod_opt_t *opts = dt-> m_options;
748 /* Option of the alias are appended to the options of the module */
749 while( opts ) {
750 adt-> m_options = append_option( adt-> m_options, opts-> m_opt_val );
751 opts = opts-> m_next;
752 }
Eric Andersen716ccb22004-01-10 11:29:31 +0000753 dt = adt;
Robert Griebl70112da2002-07-29 20:28:38 +0000754 }
Rob Landleye9190962005-12-12 19:38:44 +0000755 else {
756 bb_error_msg ("module %s not found.", mod);
Robert Griebl70112da2002-07-29 20:28:38 +0000757 return;
Rob Landleye9190962005-12-12 19:38:44 +0000758 }
Robert Grieblaead70b2002-07-26 15:54:20 +0000759 }
Rob Landleye9190962005-12-12 19:38:44 +0000760 else {
761 bb_error_msg ("Bad alias %s", dt-> m_name);
Eric Andersen716ccb22004-01-10 11:29:31 +0000762 return;
Rob Landleye9190962005-12-12 19:38:44 +0000763 }
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000764 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000765
Rob Landleye9190962005-12-12 19:38:44 +0000766 mod = dt-> m_name;
767 path = dt-> m_path;
768 opt = dt-> m_options;
Bernhard Reutner-Fischer17d355c2005-12-14 08:32:44 +0000769
Robert Griebl52e8d062002-05-14 23:42:08 +0000770 // search for duplicates
771 for ( find = *head; find; find = find-> m_next ) {
Eric Andersen807bd842004-08-19 18:30:31 +0000772 if ( !strcmp ( mod, find-> m_name )) {
Robert Griebl52e8d062002-05-14 23:42:08 +0000773 // found -> dequeue it
774
775 if ( find-> m_prev )
776 find-> m_prev-> m_next = find-> m_next;
777 else
778 *head = find-> m_next;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000779
Robert Griebl52e8d062002-05-14 23:42:08 +0000780 if ( find-> m_next )
781 find-> m_next-> m_prev = find-> m_prev;
782 else
783 *tail = find-> m_prev;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000784
Robert Griebl52e8d062002-05-14 23:42:08 +0000785 break; // there can be only one duplicate
Eric Andersen716ccb22004-01-10 11:29:31 +0000786 }
Robert Griebl52e8d062002-05-14 23:42:08 +0000787 }
788
789 if ( !find ) { // did not find a duplicate
Eric Andersen716ccb22004-01-10 11:29:31 +0000790 find = (struct mod_list_t *) xmalloc ( sizeof(struct mod_list_t));
Eric Andersen807bd842004-08-19 18:30:31 +0000791 find-> m_name = mod;
792 find-> m_path = path;
Robert Grieblaead70b2002-07-26 15:54:20 +0000793 find-> m_options = opt;
Robert Griebl52e8d062002-05-14 23:42:08 +0000794 }
795
Eric Andersen716ccb22004-01-10 11:29:31 +0000796 // enqueue at tail
Robert Griebl52e8d062002-05-14 23:42:08 +0000797 if ( *tail )
798 (*tail)-> m_next = find;
799 find-> m_prev = *tail;
800 find-> m_next = 0;
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000801
Robert Griebl52e8d062002-05-14 23:42:08 +0000802 if ( !*head )
803 *head = find;
804 *tail = find;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000805
Eric Andersen716ccb22004-01-10 11:29:31 +0000806 if ( dt ) {
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000807 int i;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000808
Rob Landleye9190962005-12-12 19:38:44 +0000809 /* Add all dependable module for that new module */
Robert Griebl1d4ef2a2002-05-28 21:32:10 +0000810 for ( i = 0; i < dt-> m_depcnt; i++ )
811 check_dep ( dt-> m_deparr [i], head, tail );
812 }
Robert Griebl52e8d062002-05-14 23:42:08 +0000813}
814
Robert Griebl236abbf2002-05-22 23:34:35 +0000815static int mod_insert ( char *mod, int argc, char **argv )
Robert Griebl52e8d062002-05-14 23:42:08 +0000816{
817 struct mod_list_t *tail = 0;
Eric Andersen716ccb22004-01-10 11:29:31 +0000818 struct mod_list_t *head = 0;
Eric Andersen908e3622003-06-20 09:56:37 +0000819 int rc;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000820
Robert Griebl52e8d062002-05-14 23:42:08 +0000821 // get dep list for module mod
822 check_dep ( mod, &head, &tail );
Eric Andersen3b1a7442003-12-24 20:30:45 +0000823
Robert Griebl52e8d062002-05-14 23:42:08 +0000824 if ( head && tail ) {
Rob Landleye9190962005-12-12 19:38:44 +0000825 if( argc ) {
Eric Andersen716ccb22004-01-10 11:29:31 +0000826 int i;
Robert Grieblaead70b2002-07-26 15:54:20 +0000827 // append module args
Eric Andersen716ccb22004-01-10 11:29:31 +0000828 for ( i = 0; i < argc; i++ )
Rob Landleye9190962005-12-12 19:38:44 +0000829 head->m_options = append_option( head->m_options, argv[i] );
Robert Griebl52e8d062002-05-14 23:42:08 +0000830 }
Eric Andersen3b1a7442003-12-24 20:30:45 +0000831
Robert Griebl52e8d062002-05-14 23:42:08 +0000832 // process tail ---> head
Eric Andersen908e3622003-06-20 09:56:37 +0000833 rc = mod_process ( tail, 1 );
Robert Griebl52e8d062002-05-14 23:42:08 +0000834 }
835 else
836 rc = 1;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000837
Robert Griebl52e8d062002-05-14 23:42:08 +0000838 return rc;
839}
840
Eric Andersen908e3622003-06-20 09:56:37 +0000841static int mod_remove ( char *mod )
Robert Griebl52e8d062002-05-14 23:42:08 +0000842{
Eric Andersen908e3622003-06-20 09:56:37 +0000843 int rc;
"Vladimir N. Oleynik"4fc92202006-02-02 14:48:54 +0000844 static struct mod_list_t rm_a_dummy = { "-a", NULL, NULL, NULL, NULL };
Eric Andersen3b1a7442003-12-24 20:30:45 +0000845
Robert Griebl52e8d062002-05-14 23:42:08 +0000846 struct mod_list_t *head = 0;
847 struct mod_list_t *tail = 0;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000848
Robert Griebl52e8d062002-05-14 23:42:08 +0000849 if ( mod )
850 check_dep ( mod, &head, &tail );
851 else // autoclean
852 head = tail = &rm_a_dummy;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000853
Robert Griebl236abbf2002-05-22 23:34:35 +0000854 if ( head && tail )
Eric Andersen908e3622003-06-20 09:56:37 +0000855 rc = mod_process ( head, 0 ); // process head ---> tail
856 else
857 rc = 1;
858 return rc;
Eric Andersen3b1a7442003-12-24 20:30:45 +0000859
Robert Griebl52e8d062002-05-14 23:42:08 +0000860}
861
Rob Landleydfba7412006-03-06 20:47:33 +0000862int modprobe_main(int argc, char** argv)
Eric Andersen0139ca92001-07-22 23:01:03 +0000863{
Rob Landleye9190962005-12-12 19:38:44 +0000864 int rc = EXIT_SUCCESS;
"Vladimir N. Oleynik"4fc92202006-02-02 14:48:54 +0000865 char *unused;
Eric Andersen0139ca92001-07-22 23:01:03 +0000866
"Vladimir N. Oleynik"4fc92202006-02-02 14:48:54 +0000867 bb_opt_complementally = "?V-:q-v:v-q";
868 main_opts = bb_getopt_ulflags(argc, argv, "acdklnqrst:vVC:",
869 &unused, &unused);
870 if((main_opts & (DUMP_CONF_EXIT | LIST_ALL)))
Eric Andersen3b1a7442003-12-24 20:30:45 +0000871 return EXIT_SUCCESS;
"Vladimir N. Oleynik"4fc92202006-02-02 14:48:54 +0000872 if((main_opts & (RESTRICT_DIR | CONFIG_FILE)))
Eric Andersen3b1a7442003-12-24 20:30:45 +0000873 bb_error_msg_and_die("-t and -C not supported");
Robert Griebl236abbf2002-05-22 23:34:35 +0000874
Eric Andersen716ccb22004-01-10 11:29:31 +0000875 depend = build_dep ( );
Robert Griebl52e8d062002-05-14 23:42:08 +0000876
Eric Andersen716ccb22004-01-10 11:29:31 +0000877 if ( !depend )
Manuel Novoa III cad53642003-03-19 09:13:01 +0000878 bb_error_msg_and_die ( "could not parse modules.dep\n" );
Eric Andersen3b1a7442003-12-24 20:30:45 +0000879
Eric Andersen1b064192001-07-25 07:23:38 +0000880 if (remove_opt) {
Eric Andersen0139ca92001-07-22 23:01:03 +0000881 do {
Eric Andersen908e3622003-06-20 09:56:37 +0000882 if (mod_remove ( optind < argc ?
"Vladimir N. Oleynik"4fc92202006-02-02 14:48:54 +0000883 argv [optind] : NULL )) {
Eric Andersen908e3622003-06-20 09:56:37 +0000884 bb_error_msg ("failed to remove module %s",
Eric Andersen3b1a7442003-12-24 20:30:45 +0000885 argv [optind] );
Eric Andersen908e3622003-06-20 09:56:37 +0000886 rc = EXIT_FAILURE;
887 }
Robert Griebl52e8d062002-05-14 23:42:08 +0000888 } while ( ++optind < argc );
Rob Landleye9190962005-12-12 19:38:44 +0000889 } else {
890 if (optind >= argc)
891 bb_error_msg_and_die ( "No module or pattern provided\n" );
Eric Andersen3b1a7442003-12-24 20:30:45 +0000892
"Vladimir N. Oleynik"4fc92202006-02-02 14:48:54 +0000893 if ( mod_insert ( argv [optind], argc - optind - 1, argv + optind + 1 ))
Rob Landleye9190962005-12-12 19:38:44 +0000894 bb_error_msg_and_die ( "failed to load module %s", argv [optind] );
Eric Andersen0139ca92001-07-22 23:01:03 +0000895 }
896
Rob Landleye9190962005-12-12 19:38:44 +0000897 /* Here would be a good place to free up memory allocated during the dependencies build. */
Eric Andersen3b1a7442003-12-24 20:30:45 +0000898
Rob Landleye9190962005-12-12 19:38:44 +0000899 return rc;
Eric Andersen0139ca92001-07-22 23:01:03 +0000900}