[821] | 1 | /* vi: set sw=4 ts=4: */
|
---|
| 2 | /*
|
---|
| 3 | * Modprobe written from scratch for BusyBox
|
---|
| 4 | *
|
---|
| 5 | * Copyright (c) 2002 by Robert Griebl, griebl@gmx.de
|
---|
| 6 | * Copyright (c) 2003 by Andrew Dennison, andrew.dennison@motec.com.au
|
---|
| 7 | * Copyright (c) 2005 by Jim Bauer, jfbauer@nfr.com
|
---|
| 8 | *
|
---|
| 9 | * Portions Copyright (c) 2005 by Yann E. MORIN, yann.morin.1998@anciens.enib.fr
|
---|
| 10 | *
|
---|
| 11 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
---|
| 12 | */
|
---|
| 13 |
|
---|
| 14 | #include "busybox.h"
|
---|
| 15 | #include <sys/utsname.h>
|
---|
| 16 | #include <sys/types.h>
|
---|
| 17 | #include <sys/wait.h>
|
---|
| 18 | #include <getopt.h>
|
---|
| 19 | #include <stdlib.h>
|
---|
| 20 | #include <unistd.h>
|
---|
| 21 | #include <syslog.h>
|
---|
| 22 | #include <string.h>
|
---|
| 23 | #include <ctype.h>
|
---|
| 24 | #include <fcntl.h>
|
---|
| 25 | #include <fnmatch.h>
|
---|
| 26 |
|
---|
| 27 | struct 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;
|
---|
| 30 | };
|
---|
| 31 |
|
---|
| 32 | struct 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 */
|
---|
| 37 |
|
---|
| 38 | 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 |
|
---|
| 47 | struct mod_list_t { /* two-way list of modules to process */
|
---|
| 48 | /* a module description */
|
---|
| 49 | char * m_name;
|
---|
| 50 | char * m_path;
|
---|
| 51 | struct mod_opt_t * m_options;
|
---|
| 52 |
|
---|
| 53 | struct mod_list_t * m_prev;
|
---|
| 54 | struct mod_list_t * m_next;
|
---|
| 55 | };
|
---|
| 56 |
|
---|
| 57 |
|
---|
| 58 | static struct dep_t *depend;
|
---|
| 59 |
|
---|
| 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 |
|
---|
| 82 | static int main_opts;
|
---|
| 83 |
|
---|
| 84 | static int parse_tag_value ( char *buffer, char **ptag, char **pvalue )
|
---|
| 85 | {
|
---|
| 86 | char *tag, *value;
|
---|
| 87 |
|
---|
| 88 | while ( isspace ( *buffer ))
|
---|
| 89 | buffer++;
|
---|
| 90 | tag = value = buffer;
|
---|
| 91 | while ( !isspace ( *value ))
|
---|
| 92 | if (!*value) return 0;
|
---|
| 93 | else value++;
|
---|
| 94 | *value++ = 0;
|
---|
| 95 | while ( isspace ( *value ))
|
---|
| 96 | value++;
|
---|
| 97 | if (!*value) return 0;
|
---|
| 98 |
|
---|
| 99 | *ptag = tag;
|
---|
| 100 | *pvalue = value;
|
---|
| 101 |
|
---|
| 102 | return 1;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | /* 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
|
---|
| 107 | * thread and stdio junk can overflow the limited stack...
|
---|
| 108 | */
|
---|
| 109 | static char *reads ( int fd, char *buffer, size_t len )
|
---|
| 110 | {
|
---|
| 111 | int n = read ( fd, buffer, len );
|
---|
| 112 |
|
---|
| 113 | if ( n > 0 ) {
|
---|
| 114 | char *p;
|
---|
| 115 |
|
---|
| 116 | buffer [len-1] = 0;
|
---|
| 117 | p = strchr ( buffer, '\n' );
|
---|
| 118 |
|
---|
| 119 | if ( p ) {
|
---|
| 120 | off_t offset;
|
---|
| 121 |
|
---|
| 122 | offset = lseek ( fd, 0L, SEEK_CUR ); // Get the current file descriptor offset
|
---|
| 123 | 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 | }
|
---|
| 129 |
|
---|
| 130 | else
|
---|
| 131 | return 0;
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | /*
|
---|
| 135 | * This function appends an option to a list
|
---|
| 136 | */
|
---|
| 137 | static struct mod_opt_t *append_option( struct mod_opt_t *opt_list, char *opt )
|
---|
| 138 | {
|
---|
| 139 | struct mod_opt_t *ol = opt_list;
|
---|
| 140 |
|
---|
| 141 | 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 ) );
|
---|
| 149 | }
|
---|
| 150 |
|
---|
| 151 | ol-> m_opt_val = bb_xstrdup( opt );
|
---|
| 152 | ol-> m_next = NULL;
|
---|
| 153 |
|
---|
| 154 | return opt_list;
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | #if ENABLE_FEATURE_MODPROBE_MULTIPLE_OPTIONS
|
---|
| 158 | /* 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
|
---|
| 169 | static 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
|
---|
| 186 | * By the way, we duplicate a little too much
|
---|
| 187 | * here but what is too much is freed later. */
|
---|
| 188 | *dst = tmp_str = bb_xstrdup( src );
|
---|
| 189 | /* Get to the end of that argument */
|
---|
| 190 | while( ( *tmp_str != '\0' )
|
---|
| 191 | && ( ( *tmp_str != ' ' )
|
---|
| 192 | || ( opt_status & ( ARG_IN_DQUOTES | ARG_IN_SQUOTES ) ) ) ) {
|
---|
| 193 | 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 | }
|
---|
| 261 | *tmp_str++ = '\0';
|
---|
| 262 | *dst = xrealloc( *dst, (tmp_str - *dst ) );
|
---|
| 263 | return src;
|
---|
| 264 | }
|
---|
| 265 | #else
|
---|
| 266 | #define parse_command_string(src, dst) (0)
|
---|
| 267 | #endif /* ENABLE_FEATURE_MODPROBE_MULTIPLE_OPTIONS */
|
---|
| 268 |
|
---|
| 269 | /*
|
---|
| 270 | * 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 | */
|
---|
| 273 | static 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 |
|
---|
| 287 | l = strlen ( buffer );
|
---|
| 288 |
|
---|
| 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 | /*
|
---|
| 368 | * 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
|
---|
| 370 | * modprobe.conf (or modules.conf, or conf.modules).
|
---|
| 371 | */
|
---|
| 372 | static 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];
|
---|
| 379 | char *filename;
|
---|
| 380 | 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 |
|
---|
| 387 | if (un.release[0] == '2') {
|
---|
| 388 | k_version = un.release[2] - '0';
|
---|
| 389 | }
|
---|
| 390 |
|
---|
| 391 | filename = bb_xasprintf("/lib/modules/%s/modules.dep", un.release );
|
---|
| 392 | fd = open ( filename, O_RDONLY );
|
---|
| 393 | if (ENABLE_FEATURE_CLEAN_UP)
|
---|
| 394 | free(filename);
|
---|
| 395 | if (fd < 0) {
|
---|
| 396 | /* 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 ))) {
|
---|
| 403 | int l = strlen ( buffer );
|
---|
| 404 | char *p = 0;
|
---|
| 405 |
|
---|
| 406 | while ( l > 0 && isspace ( buffer [l-1] )) {
|
---|
| 407 | 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 ) {
|
---|
| 418 | /* find the dep beginning */
|
---|
| 419 | 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 |
|
---|
| 504 | /* find the beginning of the module file name */
|
---|
| 505 | 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 |
|
---|
| 548 | /*
|
---|
| 549 | * First parse system-specific options and aliases
|
---|
| 550 | * as they take precedence over the kernel ones.
|
---|
| 551 | */
|
---|
| 552 | if (!ENABLE_FEATURE_2_6_MODULES
|
---|
| 553 | || ( fd = open ( "/etc/modprobe.conf", O_RDONLY )) < 0 )
|
---|
| 554 | if (( fd = open ( "/etc/modules.conf", O_RDONLY )) < 0 )
|
---|
| 555 | fd = open ( "/etc/conf.modules", O_RDONLY );
|
---|
| 556 |
|
---|
| 557 | if (fd >= 0) {
|
---|
| 558 | include_conf (&first, ¤t, buffer, sizeof(buffer), fd);
|
---|
| 559 | close(fd);
|
---|
| 560 | }
|
---|
| 561 |
|
---|
| 562 | /* Only 2.6 has a modules.alias file */
|
---|
| 563 | if (ENABLE_FEATURE_2_6_MODULES) {
|
---|
| 564 | /* Parse kernel-declared aliases */
|
---|
| 565 | filename = bb_xasprintf("/lib/modules/%s/modules.alias", un.release);
|
---|
| 566 | if ((fd = open ( filename, O_RDONLY )) < 0) {
|
---|
| 567 | /* Ok, that didn't work. Fall back to looking in /lib/modules */
|
---|
| 568 | fd = open ( "/lib/modules/modules.alias", O_RDONLY );
|
---|
| 569 | }
|
---|
| 570 | if (ENABLE_FEATURE_CLEAN_UP)
|
---|
| 571 | free(filename);
|
---|
| 572 |
|
---|
| 573 | if (fd >= 0) {
|
---|
| 574 | include_conf (&first, ¤t, buffer, sizeof(buffer), fd);
|
---|
| 575 | close(fd);
|
---|
| 576 | }
|
---|
| 577 | }
|
---|
| 578 |
|
---|
| 579 | return first;
|
---|
| 580 | }
|
---|
| 581 |
|
---|
| 582 | /* return 1 = loaded, 0 = not loaded, -1 = can't tell */
|
---|
| 583 | static int already_loaded (const char *name)
|
---|
| 584 | {
|
---|
| 585 | int fd, ret = 0;
|
---|
| 586 | char buffer[4096];
|
---|
| 587 |
|
---|
| 588 | fd = open ("/proc/modules", O_RDONLY);
|
---|
| 589 | if (fd < 0)
|
---|
| 590 | return -1;
|
---|
| 591 |
|
---|
| 592 | while ( reads ( fd, buffer, sizeof( buffer ))) {
|
---|
| 593 | char *p;
|
---|
| 594 |
|
---|
| 595 | p = strchr (buffer, ' ');
|
---|
| 596 | if (p) {
|
---|
| 597 | const char *n;
|
---|
| 598 |
|
---|
| 599 | // Truncate buffer at first space and check for matches, with
|
---|
| 600 | // the idiosyncrasy that _ and - are interchangeable because the
|
---|
| 601 | // 2.6 kernel does weird things.
|
---|
| 602 |
|
---|
| 603 | *p = 0;
|
---|
| 604 | for (p = buffer, n = name; ; p++, n++) {
|
---|
| 605 | if (*p != *n) {
|
---|
| 606 | if ((*p == '_' || *p == '-') && (*n == '_' || *n == '-'))
|
---|
| 607 | continue;
|
---|
| 608 | break;
|
---|
| 609 | }
|
---|
| 610 | // If we made it to the end, that's a match.
|
---|
| 611 | if (!*p) {
|
---|
| 612 | ret = 1;
|
---|
| 613 | goto done;
|
---|
| 614 | }
|
---|
| 615 | }
|
---|
| 616 | }
|
---|
| 617 | }
|
---|
| 618 | done:
|
---|
| 619 | close (fd);
|
---|
| 620 | return ret;
|
---|
| 621 | }
|
---|
| 622 |
|
---|
| 623 | static int mod_process ( struct mod_list_t *list, int do_insert )
|
---|
| 624 | {
|
---|
| 625 | int rc = 0;
|
---|
| 626 | char **argv = NULL;
|
---|
| 627 | struct mod_opt_t *opts;
|
---|
| 628 | int argc_malloc; /* never used when CONFIG_FEATURE_CLEAN_UP not defined */
|
---|
| 629 | int argc;
|
---|
| 630 |
|
---|
| 631 | while ( list ) {
|
---|
| 632 | argc = 0;
|
---|
| 633 | if( ENABLE_FEATURE_CLEAN_UP )
|
---|
| 634 | argc_malloc = 0;
|
---|
| 635 | /* If CONFIG_FEATURE_CLEAN_UP is not defined, then we leak memory
|
---|
| 636 | * each time we allocate memory for argv.
|
---|
| 637 | * But it is (quite) small amounts of memory that leak each
|
---|
| 638 | * time a module is loaded, and it is reclaimed when modprobe
|
---|
| 639 | * exits anyway (even when standalone shell?).
|
---|
| 640 | * This could become a problem when loading a module with LOTS of
|
---|
| 641 | * dependencies, with LOTS of options for each dependencies, with
|
---|
| 642 | * very little memory on the target... But in that case, the module
|
---|
| 643 | * would not load because there is no more memory, so there's no
|
---|
| 644 | * problem. */
|
---|
| 645 | /* enough for minimal insmod (5 args + NULL) or rmmod (3 args + NULL) */
|
---|
| 646 | argv = (char**) malloc( 6 * sizeof( char* ) );
|
---|
| 647 | if ( do_insert ) {
|
---|
| 648 | if (already_loaded (list->m_name) != 1) {
|
---|
| 649 | argv[argc++] = "insmod";
|
---|
| 650 | if (ENABLE_FEATURE_2_4_MODULES) {
|
---|
| 651 | if (do_syslog)
|
---|
| 652 | argv[argc++] = "-s";
|
---|
| 653 | if (autoclean)
|
---|
| 654 | argv[argc++] = "-k";
|
---|
| 655 | if (quiet)
|
---|
| 656 | argv[argc++] = "-q";
|
---|
| 657 | else if(verbose) /* verbose and quiet are mutually exclusive */
|
---|
| 658 | argv[argc++] = "-v";
|
---|
| 659 | }
|
---|
| 660 | argv[argc++] = list-> m_path;
|
---|
| 661 | if( ENABLE_FEATURE_CLEAN_UP )
|
---|
| 662 | argc_malloc = argc;
|
---|
| 663 | opts = list-> m_options;
|
---|
| 664 | while( opts ) {
|
---|
| 665 | /* Add one more option */
|
---|
| 666 | argc++;
|
---|
| 667 | argv = (char**) xrealloc( argv, ( argc + 1 ) * sizeof( char* ) );
|
---|
| 668 | argv[argc-1] = opts-> m_opt_val;
|
---|
| 669 | opts = opts-> m_next;
|
---|
| 670 | }
|
---|
| 671 | }
|
---|
| 672 | } else {
|
---|
| 673 | /* modutils uses short name for removal */
|
---|
| 674 | if (already_loaded (list->m_name) != 0) {
|
---|
| 675 | argv[argc++] = "rmmod";
|
---|
| 676 | if (do_syslog)
|
---|
| 677 | argv[argc++] = "-s";
|
---|
| 678 | argv[argc++] = list->m_name;
|
---|
| 679 | if( ENABLE_FEATURE_CLEAN_UP )
|
---|
| 680 | argc_malloc = argc;
|
---|
| 681 | }
|
---|
| 682 | }
|
---|
| 683 | argv[argc] = NULL;
|
---|
| 684 |
|
---|
| 685 | if (argc) {
|
---|
| 686 | if (verbose) {
|
---|
| 687 | printf("%s module %s\n", do_insert?"Loading":"Unloading", list-> m_name );
|
---|
| 688 | }
|
---|
| 689 | if (!show_only) {
|
---|
| 690 | int rc2 = wait4pid(bb_spawn(argv));
|
---|
[904] | 691 |
|
---|
[821] | 692 | if (do_insert) {
|
---|
| 693 | rc = rc2; /* only last module matters */
|
---|
| 694 | }
|
---|
| 695 | else if (!rc2) {
|
---|
| 696 | rc = 0; /* success if remove any mod */
|
---|
| 697 | }
|
---|
| 698 | }
|
---|
| 699 | if( ENABLE_FEATURE_CLEAN_UP )
|
---|
| 700 | /* the last value in the array has index == argc, but
|
---|
| 701 | * it is the terminating NULL, so we must not free it. */
|
---|
| 702 | while( argc_malloc < argc ) {
|
---|
| 703 | free( argv[argc_malloc++] );
|
---|
| 704 | }
|
---|
| 705 | }
|
---|
| 706 | if( ENABLE_FEATURE_CLEAN_UP ) {
|
---|
| 707 | free( argv );
|
---|
| 708 | argv = NULL;
|
---|
| 709 | }
|
---|
| 710 | list = do_insert ? list-> m_prev : list-> m_next;
|
---|
| 711 | }
|
---|
| 712 | return (show_only) ? 0 : rc;
|
---|
| 713 | }
|
---|
| 714 |
|
---|
| 715 | /*
|
---|
| 716 | * Builds the dependency list (aka stack) of a module.
|
---|
| 717 | * head: the highest module in the stack (last to insmod, first to rmmod)
|
---|
| 718 | * tail: the lowest module in the stack (first to insmod, last to rmmod)
|
---|
| 719 | */
|
---|
| 720 | static void check_dep ( char *mod, struct mod_list_t **head, struct mod_list_t **tail )
|
---|
| 721 | {
|
---|
| 722 | struct mod_list_t *find;
|
---|
| 723 | struct dep_t *dt;
|
---|
| 724 | struct mod_opt_t *opt = 0;
|
---|
| 725 | char *path = 0;
|
---|
| 726 |
|
---|
| 727 | /* Search for the given module name amongst all dependency rules.
|
---|
| 728 | * The module name in a dependency rule can be a shell pattern,
|
---|
| 729 | * so try to match the given module name against such a pattern.
|
---|
| 730 | * Of course if the name in the dependency rule is a plain string,
|
---|
| 731 | * then we consider it a pattern, and matching will still work. */
|
---|
| 732 | for ( dt = depend; dt; dt = dt-> m_next ) {
|
---|
| 733 | if ( fnmatch ( dt-> m_name, mod, 0 ) == 0) {
|
---|
| 734 | break;
|
---|
| 735 | }
|
---|
| 736 | }
|
---|
| 737 |
|
---|
| 738 | if( !dt ) {
|
---|
| 739 | bb_error_msg ("module %s not found.", mod);
|
---|
| 740 | return;
|
---|
| 741 | }
|
---|
| 742 |
|
---|
| 743 | // resolve alias names
|
---|
| 744 | while ( dt-> m_isalias ) {
|
---|
| 745 | if ( dt-> m_depcnt == 1 ) {
|
---|
| 746 | struct dep_t *adt;
|
---|
| 747 |
|
---|
| 748 | for ( adt = depend; adt; adt = adt-> m_next ) {
|
---|
| 749 | if ( strcmp ( adt-> m_name, dt-> m_deparr [0] ) == 0 )
|
---|
| 750 | break;
|
---|
| 751 | }
|
---|
| 752 | if ( adt ) {
|
---|
| 753 | /* This is the module we are aliased to */
|
---|
| 754 | struct mod_opt_t *opts = dt-> m_options;
|
---|
| 755 | /* Option of the alias are appended to the options of the module */
|
---|
| 756 | while( opts ) {
|
---|
| 757 | adt-> m_options = append_option( adt-> m_options, opts-> m_opt_val );
|
---|
| 758 | opts = opts-> m_next;
|
---|
| 759 | }
|
---|
| 760 | dt = adt;
|
---|
| 761 | }
|
---|
| 762 | else {
|
---|
| 763 | bb_error_msg ("module %s not found.", mod);
|
---|
| 764 | return;
|
---|
| 765 | }
|
---|
| 766 | }
|
---|
| 767 | else {
|
---|
| 768 | bb_error_msg ("Bad alias %s", dt-> m_name);
|
---|
| 769 | return;
|
---|
| 770 | }
|
---|
| 771 | }
|
---|
| 772 |
|
---|
| 773 | mod = dt-> m_name;
|
---|
| 774 | path = dt-> m_path;
|
---|
| 775 | opt = dt-> m_options;
|
---|
| 776 |
|
---|
| 777 | // search for duplicates
|
---|
| 778 | for ( find = *head; find; find = find-> m_next ) {
|
---|
| 779 | if ( !strcmp ( mod, find-> m_name )) {
|
---|
| 780 | // found -> dequeue it
|
---|
| 781 |
|
---|
| 782 | if ( find-> m_prev )
|
---|
| 783 | find-> m_prev-> m_next = find-> m_next;
|
---|
| 784 | else
|
---|
| 785 | *head = find-> m_next;
|
---|
| 786 |
|
---|
| 787 | if ( find-> m_next )
|
---|
| 788 | find-> m_next-> m_prev = find-> m_prev;
|
---|
| 789 | else
|
---|
| 790 | *tail = find-> m_prev;
|
---|
| 791 |
|
---|
| 792 | break; // there can be only one duplicate
|
---|
| 793 | }
|
---|
| 794 | }
|
---|
| 795 |
|
---|
| 796 | if ( !find ) { // did not find a duplicate
|
---|
| 797 | find = (struct mod_list_t *) xmalloc ( sizeof(struct mod_list_t));
|
---|
| 798 | find-> m_name = mod;
|
---|
| 799 | find-> m_path = path;
|
---|
| 800 | find-> m_options = opt;
|
---|
| 801 | }
|
---|
| 802 |
|
---|
| 803 | // enqueue at tail
|
---|
| 804 | if ( *tail )
|
---|
| 805 | (*tail)-> m_next = find;
|
---|
| 806 | find-> m_prev = *tail;
|
---|
| 807 | find-> m_next = 0;
|
---|
| 808 |
|
---|
| 809 | if ( !*head )
|
---|
| 810 | *head = find;
|
---|
| 811 | *tail = find;
|
---|
| 812 |
|
---|
| 813 | if ( dt ) {
|
---|
| 814 | int i;
|
---|
| 815 |
|
---|
| 816 | /* Add all dependable module for that new module */
|
---|
| 817 | for ( i = 0; i < dt-> m_depcnt; i++ )
|
---|
| 818 | check_dep ( dt-> m_deparr [i], head, tail );
|
---|
| 819 | }
|
---|
| 820 | }
|
---|
| 821 |
|
---|
| 822 | static int mod_insert ( char *mod, int argc, char **argv )
|
---|
| 823 | {
|
---|
| 824 | struct mod_list_t *tail = 0;
|
---|
| 825 | struct mod_list_t *head = 0;
|
---|
| 826 | int rc;
|
---|
| 827 |
|
---|
| 828 | // get dep list for module mod
|
---|
| 829 | check_dep ( mod, &head, &tail );
|
---|
| 830 |
|
---|
| 831 | if ( head && tail ) {
|
---|
| 832 | if( argc ) {
|
---|
| 833 | int i;
|
---|
| 834 | // append module args
|
---|
| 835 | for ( i = 0; i < argc; i++ )
|
---|
| 836 | head->m_options = append_option( head->m_options, argv[i] );
|
---|
| 837 | }
|
---|
| 838 |
|
---|
| 839 | // process tail ---> head
|
---|
[904] | 840 | if ((rc = mod_process ( tail, 1 )) != 0) {
|
---|
| 841 | /*
|
---|
| 842 | * In case of using udev, multiple instances of modprobe can be
|
---|
| 843 | * spawned to load the same module (think of two same usb devices,
|
---|
| 844 | * for example; or cold-plugging at boot time). Thus we shouldn't
|
---|
| 845 | * fail if the module was loaded, and not by us.
|
---|
| 846 | */
|
---|
| 847 | if (already_loaded (mod) )
|
---|
| 848 | rc = 0;
|
---|
| 849 | }
|
---|
[821] | 850 | }
|
---|
| 851 | else
|
---|
| 852 | rc = 1;
|
---|
| 853 |
|
---|
| 854 | return rc;
|
---|
| 855 | }
|
---|
| 856 |
|
---|
| 857 | static int mod_remove ( char *mod )
|
---|
| 858 | {
|
---|
| 859 | int rc;
|
---|
| 860 | static struct mod_list_t rm_a_dummy = { "-a", NULL, NULL, NULL, NULL };
|
---|
| 861 |
|
---|
| 862 | struct mod_list_t *head = 0;
|
---|
| 863 | struct mod_list_t *tail = 0;
|
---|
| 864 |
|
---|
| 865 | if ( mod )
|
---|
| 866 | check_dep ( mod, &head, &tail );
|
---|
| 867 | else // autoclean
|
---|
| 868 | head = tail = &rm_a_dummy;
|
---|
| 869 |
|
---|
| 870 | if ( head && tail )
|
---|
| 871 | rc = mod_process ( head, 0 ); // process head ---> tail
|
---|
| 872 | else
|
---|
| 873 | rc = 1;
|
---|
| 874 | return rc;
|
---|
| 875 |
|
---|
| 876 | }
|
---|
| 877 |
|
---|
| 878 | int modprobe_main(int argc, char** argv)
|
---|
| 879 | {
|
---|
| 880 | int rc = EXIT_SUCCESS;
|
---|
| 881 | char *unused;
|
---|
| 882 |
|
---|
| 883 | bb_opt_complementally = "?V-:q-v:v-q";
|
---|
| 884 | main_opts = bb_getopt_ulflags(argc, argv, "acdklnqrst:vVC:",
|
---|
| 885 | &unused, &unused);
|
---|
| 886 | if((main_opts & (DUMP_CONF_EXIT | LIST_ALL)))
|
---|
| 887 | return EXIT_SUCCESS;
|
---|
| 888 | if((main_opts & (RESTRICT_DIR | CONFIG_FILE)))
|
---|
| 889 | bb_error_msg_and_die("-t and -C not supported");
|
---|
| 890 |
|
---|
| 891 | depend = build_dep ( );
|
---|
| 892 |
|
---|
| 893 | if ( !depend )
|
---|
| 894 | bb_error_msg_and_die ( "could not parse modules.dep" );
|
---|
| 895 |
|
---|
| 896 | if (remove_opt) {
|
---|
| 897 | do {
|
---|
| 898 | if (mod_remove ( optind < argc ?
|
---|
| 899 | argv [optind] : NULL )) {
|
---|
| 900 | bb_error_msg ("failed to remove module %s",
|
---|
| 901 | argv [optind] );
|
---|
| 902 | rc = EXIT_FAILURE;
|
---|
| 903 | }
|
---|
| 904 | } while ( ++optind < argc );
|
---|
| 905 | } else {
|
---|
| 906 | if (optind >= argc)
|
---|
| 907 | bb_error_msg_and_die ( "No module or pattern provided" );
|
---|
| 908 |
|
---|
| 909 | if ( mod_insert ( argv [optind], argc - optind - 1, argv + optind + 1 ))
|
---|
| 910 | bb_error_msg_and_die ( "failed to load module %s", argv [optind] );
|
---|
| 911 | }
|
---|
| 912 |
|
---|
| 913 | /* Here would be a good place to free up memory allocated during the dependencies build. */
|
---|
| 914 |
|
---|
| 915 | return rc;
|
---|
| 916 | }
|
---|