Eric Andersen | 0139ca9 | 2001-07-22 23:01:03 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 3 | * Modprobe written from scratch for BusyBox |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 4 | * |
Robert Griebl | 6859d76 | 2002-08-05 02:57:12 +0000 | [diff] [blame] | 5 | * Copyright (c) 2002 by Robert Griebl, griebl@gmx.de |
Eric Andersen | 908e362 | 2003-06-20 09:56:37 +0000 | [diff] [blame] | 6 | * Copyright (c) 2003 by Andrew Dennison, andrew.dennison@motec.com.au |
Paul Fox | 8eeb655 | 2005-08-04 18:33:36 +0000 | [diff] [blame] | 7 | * Copyright (c) 2005 by Jim Bauer, jfbauer@nfr.com |
Bernhard Reutner-Fischer | 17d355c | 2005-12-14 08:32:44 +0000 | [diff] [blame] | 8 | * |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 9 | * Portions Copyright (c) 2005 by Yann E. MORIN, yann.morin.1998@anciens.enib.fr |
Robert Griebl | 6859d76 | 2002-08-05 02:57:12 +0000 | [diff] [blame] | 10 | * |
Rob Landley | 79e1cab | 2005-11-15 00:08:29 +0000 | [diff] [blame] | 11 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Robert Griebl | 6859d76 | 2002-08-05 02:57:12 +0000 | [diff] [blame] | 12 | */ |
Eric Andersen | 0139ca9 | 2001-07-22 23:01:03 +0000 | [diff] [blame] | 13 | |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 14 | #include <sys/utsname.h> |
Paul Fox | 8eeb655 | 2005-08-04 18:33:36 +0000 | [diff] [blame] | 15 | #include <sys/types.h> |
| 16 | #include <sys/wait.h> |
Eric Andersen | 0139ca9 | 2001-07-22 23:01:03 +0000 | [diff] [blame] | 17 | #include <getopt.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <unistd.h> |
| 20 | #include <syslog.h> |
| 21 | #include <string.h> |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 22 | #include <ctype.h> |
Eric Andersen | c06391b | 2002-06-04 13:28:43 +0000 | [diff] [blame] | 23 | #include <fcntl.h> |
Eric Andersen | 0139ca9 | 2001-07-22 23:01:03 +0000 | [diff] [blame] | 24 | #include "busybox.h" |
| 25 | |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 26 | struct 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 Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 29 | }; |
| 30 | |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 31 | struct 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-Fischer | 17d355c | 2005-12-14 08:32:44 +0000 | [diff] [blame] | 36 | |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 37 | 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 | |
| 46 | struct mod_list_t { /* two-way list of modules to process */ |
| 47 | /* a module description */ |
Eric Andersen | 807bd84 | 2004-08-19 18:30:31 +0000 | [diff] [blame] | 48 | char * m_name; |
| 49 | char * m_path; |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 50 | struct mod_opt_t * m_options; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 51 | |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 52 | struct mod_list_t * m_prev; |
| 53 | struct mod_list_t * m_next; |
| 54 | }; |
| 55 | |
| 56 | |
Robert Griebl | 236abbf | 2002-05-22 23:34:35 +0000 | [diff] [blame] | 57 | static struct dep_t *depend; |
| 58 | static int autoclean, show_only, quiet, do_syslog, verbose; |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 59 | |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 60 | static int parse_tag_value ( char *buffer, char **ptag, char **pvalue ) |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 61 | { |
| 62 | char *tag, *value; |
| 63 | |
| 64 | while ( isspace ( *buffer )) |
Eric Andersen | 716ccb2 | 2004-01-10 11:29:31 +0000 | [diff] [blame] | 65 | buffer++; |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 66 | tag = value = buffer; |
| 67 | while ( !isspace ( *value )) |
Eric Andersen | 7e496a7 | 2004-04-06 12:06:03 +0000 | [diff] [blame] | 68 | if (!*value) return 0; |
| 69 | else value++; |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 70 | *value++ = 0; |
| 71 | while ( isspace ( *value )) |
| 72 | value++; |
Eric Andersen | 7e496a7 | 2004-04-06 12:06:03 +0000 | [diff] [blame] | 73 | if (!*value) return 0; |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 74 | |
| 75 | *ptag = tag; |
| 76 | *pvalue = value; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 77 | |
Eric Andersen | 7e496a7 | 2004-04-06 12:06:03 +0000 | [diff] [blame] | 78 | return 1; |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 79 | } |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 80 | |
Robert Griebl | bc28f7a | 2002-06-04 19:33:58 +0000 | [diff] [blame] | 81 | /* Jump through hoops to simulate how fgets() grabs just one line at a |
| 82 | * time... Don't use any stdio since modprobe gets called from a kernel |
Eric Andersen | 716ccb2 | 2004-01-10 11:29:31 +0000 | [diff] [blame] | 83 | * thread and stdio junk can overflow the limited stack... |
Robert Griebl | bc28f7a | 2002-06-04 19:33:58 +0000 | [diff] [blame] | 84 | */ |
| 85 | static char *reads ( int fd, char *buffer, size_t len ) |
| 86 | { |
| 87 | int n = read ( fd, buffer, len ); |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 88 | |
Robert Griebl | bc28f7a | 2002-06-04 19:33:58 +0000 | [diff] [blame] | 89 | if ( n > 0 ) { |
| 90 | char *p; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 91 | |
Robert Griebl | bc28f7a | 2002-06-04 19:33:58 +0000 | [diff] [blame] | 92 | buffer [len-1] = 0; |
| 93 | p = strchr ( buffer, '\n' ); |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 94 | |
Robert Griebl | bc28f7a | 2002-06-04 19:33:58 +0000 | [diff] [blame] | 95 | if ( p ) { |
| 96 | off_t offset; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 97 | |
Eric Andersen | 716ccb2 | 2004-01-10 11:29:31 +0000 | [diff] [blame] | 98 | offset = lseek ( fd, 0L, SEEK_CUR ); // Get the current file descriptor offset |
Robert Griebl | bc28f7a | 2002-06-04 19:33:58 +0000 | [diff] [blame] | 99 | lseek ( fd, offset-n + (p-buffer) + 1, SEEK_SET ); // Set the file descriptor offset to right after the \n |
| 100 | |
| 101 | p[1] = 0; |
| 102 | } |
| 103 | return buffer; |
| 104 | } |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 105 | |
Robert Griebl | bc28f7a | 2002-06-04 19:33:58 +0000 | [diff] [blame] | 106 | else |
| 107 | return 0; |
| 108 | } |
| 109 | |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 110 | /* |
| 111 | * This function appends an option to a list |
| 112 | */ |
| 113 | struct mod_opt_t *append_option( struct mod_opt_t *opt_list, char *opt ) |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 114 | { |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 115 | struct mod_opt_t *ol = opt_list; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 116 | |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 117 | if( ol ) { |
| 118 | while( ol-> m_next ) { |
| 119 | ol = ol-> m_next; |
| 120 | } |
| 121 | ol-> m_next = xmalloc( sizeof( struct mod_opt_t ) ); |
| 122 | ol = ol-> m_next; |
| 123 | } else { |
| 124 | ol = opt_list = xmalloc( sizeof( struct mod_opt_t ) ); |
Eric Andersen | 03d8091 | 2003-12-19 21:04:19 +0000 | [diff] [blame] | 125 | } |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 126 | |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 127 | ol-> m_opt_val = bb_xstrdup( opt ); |
| 128 | ol-> m_next = NULL; |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 129 | |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 130 | return opt_list; |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Bernhard Reutner-Fischer | 17d355c | 2005-12-14 08:32:44 +0000 | [diff] [blame] | 133 | #if ENABLE_FEATURE_MODPROBE_MULTIPLE_OPTIONS |
Rob Landley | 79e1cab | 2005-11-15 00:08:29 +0000 | [diff] [blame] | 134 | /* static char* parse_command_string( char* src, char **dst ); |
| 135 | * src: pointer to string containing argument |
| 136 | * dst: pointer to where to store the parsed argument |
| 137 | * return value: the pointer to the first char after the parsed argument, |
| 138 | * NULL if there was no argument parsed (only trailing spaces). |
| 139 | * Note that memory is allocated with bb_xstrdup when a new argument was |
| 140 | * parsed. Don't forget to free it! |
| 141 | */ |
| 142 | #define ARG_EMPTY 0x00 |
| 143 | #define ARG_IN_DQUOTES 0x01 |
| 144 | #define ARG_IN_SQUOTES 0x02 |
| 145 | static char *parse_command_string( char *src, char **dst ) |
| 146 | { |
| 147 | int opt_status = ARG_EMPTY; |
| 148 | char* tmp_str; |
| 149 | |
| 150 | /* Dumb you, I have nothing to do... */ |
| 151 | if( src == NULL ) return src; |
| 152 | |
| 153 | /* Skip leading spaces */ |
| 154 | while( *src == ' ' ) { |
| 155 | src++; |
| 156 | } |
| 157 | /* Is the end of string reached? */ |
| 158 | if( *src == '\0' ) { |
| 159 | return NULL; |
| 160 | } |
| 161 | /* Reached the start of an argument |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 162 | * By the way, we duplicate a little too much |
| 163 | * here but what is too much is freed later. */ |
Rob Landley | 79e1cab | 2005-11-15 00:08:29 +0000 | [diff] [blame] | 164 | *dst = tmp_str = bb_xstrdup( src ); |
| 165 | /* Get to the end of that argument */ |
| 166 | while( ( *tmp_str != '\0' ) |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 167 | && ( ( *tmp_str != ' ' ) |
| 168 | || ( opt_status & ( ARG_IN_DQUOTES | ARG_IN_SQUOTES ) ) ) ) { |
Rob Landley | 79e1cab | 2005-11-15 00:08:29 +0000 | [diff] [blame] | 169 | switch( *tmp_str ) { |
| 170 | case '\'': |
| 171 | if( opt_status & ARG_IN_DQUOTES ) { |
| 172 | /* Already in double quotes, keep current char as is */ |
| 173 | } else { |
| 174 | /* shift left 1 char, until end of string: get rid of the opening/closing quotes */ |
| 175 | memmove( tmp_str, tmp_str + 1, strlen( tmp_str ) ); |
| 176 | /* mark me: we enter or leave single quotes */ |
| 177 | opt_status ^= ARG_IN_SQUOTES; |
| 178 | /* Back one char, as we need to re-scan the new char there. */ |
| 179 | tmp_str--; |
| 180 | } |
| 181 | break; |
| 182 | case '"': |
| 183 | if( opt_status & ARG_IN_SQUOTES ) { |
| 184 | /* Already in single quotes, keep current char as is */ |
| 185 | } else { |
| 186 | /* shift left 1 char, until end of string: get rid of the opening/closing quotes */ |
| 187 | memmove( tmp_str, tmp_str + 1, strlen( tmp_str ) ); |
| 188 | /* mark me: we enter or leave double quotes */ |
| 189 | opt_status ^= ARG_IN_DQUOTES; |
| 190 | /* Back one char, as we need to re-scan the new char there. */ |
| 191 | tmp_str--; |
| 192 | } |
| 193 | break; |
| 194 | case '\\': |
| 195 | if( opt_status & ARG_IN_SQUOTES ) { |
| 196 | /* Between single quotes: keep as is. */ |
| 197 | } else { |
| 198 | switch( *(tmp_str+1) ) { |
| 199 | case 'a': |
| 200 | case 'b': |
| 201 | case 't': |
| 202 | case 'n': |
| 203 | case 'v': |
| 204 | case 'f': |
| 205 | case 'r': |
| 206 | case '0': |
| 207 | /* We escaped a special character. For now, keep |
| 208 | * both the back-slash and the following char. */ |
| 209 | tmp_str++; src++; |
| 210 | break; |
| 211 | default: |
| 212 | /* We escaped a space or a single or double quote, |
| 213 | * or a back-slash, or a non-escapable char. Remove |
| 214 | * the '\' and keep the new current char as is. */ |
| 215 | memmove( tmp_str, tmp_str + 1, strlen( tmp_str ) ); |
| 216 | break; |
| 217 | } |
| 218 | } |
| 219 | break; |
| 220 | /* Any other char that is special shall appear here. |
| 221 | * Example: $ starts a variable |
| 222 | case '$': |
| 223 | do_variable_expansion(); |
| 224 | break; |
| 225 | * */ |
| 226 | default: |
| 227 | /* any other char is kept as is. */ |
| 228 | break; |
| 229 | } |
| 230 | tmp_str++; /* Go to next char */ |
| 231 | src++; /* Go to next char to find the end of the argument. */ |
| 232 | } |
| 233 | /* End of string, but still no ending quote */ |
| 234 | if( opt_status & ( ARG_IN_DQUOTES | ARG_IN_SQUOTES ) ) { |
| 235 | bb_error_msg_and_die( "unterminated (single or double) quote in options list: %s", src ); |
| 236 | } |
| 237 | *tmp_str = '\0'; |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 238 | *dst = xrealloc( *dst, strlen( *dst ) ); |
Rob Landley | 79e1cab | 2005-11-15 00:08:29 +0000 | [diff] [blame] | 239 | return src; |
| 240 | } |
Rob Landley | ae50c6d | 2005-12-15 07:42:13 +0000 | [diff] [blame] | 241 | #else |
| 242 | #define parse_command_string(src, dst) (0) |
Bernhard Reutner-Fischer | 17d355c | 2005-12-14 08:32:44 +0000 | [diff] [blame] | 243 | #endif /* ENABLE_FEATURE_MODPROBE_MULTIPLE_OPTIONS */ |
Rob Landley | 79e1cab | 2005-11-15 00:08:29 +0000 | [diff] [blame] | 244 | |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 245 | /* |
| 246 | * This function builds a list of dependency rules from /lib/modules/`uname -r\modules.dep. |
| 247 | * It then fills every modules and aliases with their default options, found by parsing |
| 248 | * modprobe.conf (or modules.conf, or conf.modules). |
| 249 | */ |
| 250 | static struct dep_t *build_dep ( void ) |
| 251 | { |
| 252 | int fd; |
| 253 | struct utsname un; |
| 254 | struct dep_t *first = 0; |
| 255 | struct dep_t *current = 0; |
| 256 | char buffer[2048]; |
| 257 | char *filename = buffer; |
| 258 | int continuation_line = 0; |
| 259 | int k_version; |
| 260 | |
| 261 | k_version = 0; |
| 262 | if ( uname ( &un )) |
| 263 | bb_error_msg_and_die("can't determine kernel version"); |
| 264 | |
| 265 | // check for buffer overflow in following code |
| 266 | if ( bb_strlen ( un.release ) > ( sizeof( buffer ) - 64 )) { |
| 267 | return 0; |
| 268 | } |
| 269 | if (un.release[0] == '2') { |
| 270 | k_version = un.release[2] - '0'; |
| 271 | } |
| 272 | |
| 273 | strcpy ( filename, "/lib/modules/" ); |
| 274 | strcat ( filename, un.release ); |
| 275 | strcat ( filename, "/modules.dep" ); |
| 276 | |
| 277 | if (( fd = open ( filename, O_RDONLY )) < 0 ) { |
| 278 | |
| 279 | /* Ok, that didn't work. Fall back to looking in /lib/modules */ |
| 280 | if (( fd = open ( "/lib/modules/modules.dep", O_RDONLY )) < 0 ) { |
| 281 | return 0; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | while ( reads ( fd, buffer, sizeof( buffer ))) { |
| 286 | int l = bb_strlen ( buffer ); |
| 287 | char *p = 0; |
| 288 | |
| 289 | while ( isspace ( buffer [l-1] )) { |
| 290 | buffer [l-1] = 0; |
| 291 | l--; |
| 292 | } |
| 293 | |
| 294 | if ( l == 0 ) { |
| 295 | continuation_line = 0; |
| 296 | continue; |
| 297 | } |
| 298 | |
| 299 | /* Is this a new module dep description? */ |
| 300 | if ( !continuation_line ) { |
Bernhard Reutner-Fischer | 17d355c | 2005-12-14 08:32:44 +0000 | [diff] [blame] | 301 | /* find the dep beginning */ |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 302 | char *col = strchr ( buffer, ':' ); |
| 303 | char *dot = col; |
| 304 | |
| 305 | if ( col ) { |
| 306 | /* This line is a dep description */ |
| 307 | char *mods; |
| 308 | char *modpath; |
| 309 | char *mod; |
| 310 | |
| 311 | /* Find the beginning of the module file name */ |
| 312 | *col = 0; |
| 313 | mods = strrchr ( buffer, '/' ); |
| 314 | |
| 315 | if ( !mods ) |
| 316 | mods = buffer; /* no path for this module */ |
| 317 | else |
| 318 | mods++; /* there was a path for this module... */ |
| 319 | |
| 320 | /* find the path of the module */ |
| 321 | modpath = strchr ( buffer, '/' ); /* ... and this is the path */ |
| 322 | if ( !modpath ) |
| 323 | modpath = buffer; /* module with no path */ |
| 324 | /* find the end of the module name in the file name */ |
| 325 | if ( ENABLE_FEATURE_2_6_MODULES && |
| 326 | (k_version > 4) && ( *(col-3) == '.' ) && |
| 327 | ( *(col-2) == 'k' ) && ( *(col-1) == 'o' ) ) |
| 328 | dot = col - 3; |
| 329 | else |
| 330 | if (( *(col-2) == '.' ) && ( *(col-1) == 'o' )) |
| 331 | dot = col - 2; |
| 332 | |
| 333 | mod = bb_xstrndup ( mods, dot - mods ); |
| 334 | |
| 335 | /* enqueue new module */ |
| 336 | if ( !current ) { |
| 337 | first = current = (struct dep_t *) xmalloc ( sizeof ( struct dep_t )); |
| 338 | } |
| 339 | else { |
| 340 | current-> m_next = (struct dep_t *) xmalloc ( sizeof ( struct dep_t )); |
| 341 | current = current-> m_next; |
| 342 | } |
| 343 | current-> m_name = mod; |
| 344 | current-> m_path = bb_xstrdup(modpath); |
| 345 | current-> m_options = NULL; |
| 346 | current-> m_isalias = 0; |
| 347 | current-> m_depcnt = 0; |
| 348 | current-> m_deparr = 0; |
| 349 | current-> m_next = 0; |
| 350 | |
| 351 | p = col + 1; |
| 352 | } |
| 353 | else |
| 354 | /* this line is not a dep description */ |
| 355 | p = 0; |
| 356 | } |
| 357 | else |
| 358 | /* It's a dep description continuation */ |
| 359 | p = buffer; |
| 360 | |
| 361 | while ( p && *p && isblank(*p)) |
| 362 | p++; |
| 363 | |
| 364 | /* p points to the first dependable module; if NULL, no dependable module */ |
| 365 | if ( p && *p ) { |
| 366 | char *end = &buffer [l-1]; |
| 367 | char *deps; |
| 368 | char *dep; |
| 369 | char *next; |
| 370 | int ext = 0; |
| 371 | |
| 372 | while ( isblank ( *end ) || ( *end == '\\' )) |
| 373 | end--; |
| 374 | |
| 375 | do |
| 376 | { |
| 377 | /* search the end of the dependency */ |
| 378 | next = strchr (p, ' ' ); |
| 379 | if (next) |
| 380 | { |
| 381 | *next = 0; |
| 382 | next--; |
| 383 | } |
| 384 | else |
| 385 | next = end; |
| 386 | |
Bernhard Reutner-Fischer | 17d355c | 2005-12-14 08:32:44 +0000 | [diff] [blame] | 387 | /* find the beginning of the module file name */ |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 388 | deps = strrchr ( p, '/' ); |
| 389 | |
| 390 | if ( !deps || ( deps < p )) { |
| 391 | deps = p; |
| 392 | |
| 393 | while ( isblank ( *deps )) |
| 394 | deps++; |
| 395 | } |
| 396 | else |
| 397 | deps++; |
| 398 | |
| 399 | /* find the end of the module name in the file name */ |
| 400 | if ( ENABLE_FEATURE_2_6_MODULES && |
| 401 | (k_version > 4) && ( *(next-2) == '.' ) && |
| 402 | ( *(next-1) == 'k' ) && ( *next == 'o' ) ) |
| 403 | ext = 3; |
| 404 | else |
| 405 | if (( *(next-1) == '.' ) && ( *next == 'o' )) |
| 406 | ext = 2; |
| 407 | |
| 408 | /* Cope with blank lines */ |
| 409 | if ((next-deps-ext+1) <= 0) |
| 410 | continue; |
| 411 | dep = bb_xstrndup ( deps, next - deps - ext + 1 ); |
| 412 | |
| 413 | /* Add the new dependable module name */ |
| 414 | current-> m_depcnt++; |
| 415 | current-> m_deparr = (char **) xrealloc ( current-> m_deparr, |
| 416 | sizeof ( char *) * current-> m_depcnt ); |
| 417 | current-> m_deparr [current-> m_depcnt - 1] = dep; |
| 418 | |
| 419 | p = next + 2; |
| 420 | } while (next < end); |
| 421 | } |
| 422 | |
| 423 | /* is there other dependable module(s) ? */ |
| 424 | if ( buffer [l-1] == '\\' ) |
| 425 | continuation_line = 1; |
| 426 | else |
| 427 | continuation_line = 0; |
| 428 | } |
| 429 | close ( fd ); |
| 430 | |
| 431 | // alias parsing is not 100% correct (no correct handling of continuation lines within an alias) ! |
| 432 | |
Rob Landley | ae50c6d | 2005-12-15 07:42:13 +0000 | [diff] [blame] | 433 | if (!ENABLE_FEATURE_2_6_MODULES |
| 434 | || ( fd = open ( "/etc/modprobe.conf", O_RDONLY )) < 0 ) |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 435 | if (( fd = open ( "/etc/modules.conf", O_RDONLY )) < 0 ) |
| 436 | if (( fd = open ( "/etc/conf.modules", O_RDONLY )) < 0 ) |
| 437 | return first; |
| 438 | |
| 439 | continuation_line = 0; |
| 440 | while ( reads ( fd, buffer, sizeof( buffer ))) { |
| 441 | int l; |
| 442 | char *p; |
| 443 | |
| 444 | p = strchr ( buffer, '#' ); |
| 445 | if ( p ) |
| 446 | *p = 0; |
| 447 | |
| 448 | l = bb_strlen ( buffer ); |
| 449 | |
| 450 | while ( l && isspace ( buffer [l-1] )) { |
| 451 | buffer [l-1] = 0; |
| 452 | l--; |
| 453 | } |
| 454 | |
| 455 | if ( l == 0 ) { |
| 456 | continuation_line = 0; |
| 457 | continue; |
| 458 | } |
| 459 | |
| 460 | if ( !continuation_line ) { |
| 461 | if (( strncmp ( buffer, "alias", 5 ) == 0 ) && isspace ( buffer [5] )) { |
| 462 | char *alias, *mod; |
| 463 | |
| 464 | if ( parse_tag_value ( buffer + 6, &alias, &mod )) { |
| 465 | /* handle alias as a module dependent on the aliased module */ |
| 466 | if ( !current ) { |
| 467 | first = current = (struct dep_t *) xcalloc ( 1, sizeof ( struct dep_t )); |
| 468 | } |
| 469 | else { |
| 470 | current-> m_next = (struct dep_t *) xcalloc ( 1, sizeof ( struct dep_t )); |
| 471 | current = current-> m_next; |
| 472 | } |
| 473 | current-> m_name = bb_xstrdup ( alias ); |
| 474 | current-> m_isalias = 1; |
| 475 | |
| 476 | if (( strcmp ( mod, "off" ) == 0 ) || ( strcmp ( mod, "null" ) == 0 )) { |
| 477 | current-> m_depcnt = 0; |
| 478 | current-> m_deparr = 0; |
| 479 | } |
| 480 | else { |
| 481 | current-> m_depcnt = 1; |
| 482 | current-> m_deparr = xmalloc ( 1 * sizeof( char * )); |
| 483 | current-> m_deparr[0] = bb_xstrdup ( mod ); |
| 484 | } |
| 485 | current-> m_next = 0; |
| 486 | } |
| 487 | } |
| 488 | else if (( strncmp ( buffer, "options", 7 ) == 0 ) && isspace ( buffer [7] )) { |
| 489 | char *mod, *opt; |
| 490 | |
| 491 | /* split the line in the module/alias name, and options */ |
| 492 | if ( parse_tag_value ( buffer + 8, &mod, &opt )) { |
| 493 | struct dep_t *dt; |
| 494 | |
| 495 | /* find the corresponding module */ |
| 496 | for ( dt = first; dt; dt = dt-> m_next ) { |
| 497 | if ( strcmp ( dt-> m_name, mod ) == 0 ) |
| 498 | break; |
| 499 | } |
Rob Landley | 199501f | 2005-12-16 06:18:06 +0000 | [diff] [blame] | 500 | if ( dt ) { |
| 501 | if ( ENABLE_FEATURE_MODPROBE_MULTIPLE_OPTIONS ) { |
| 502 | char* new_opt = NULL; |
| 503 | while( ( opt = parse_command_string( opt, &new_opt ) ) ) { |
| 504 | dt-> m_options = append_option( dt-> m_options, new_opt ); |
| 505 | } |
| 506 | } else { |
| 507 | dt-> m_options = append_option( dt-> m_options, opt ); |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 508 | } |
| 509 | } |
| 510 | } |
| 511 | } |
| 512 | } |
| 513 | } |
| 514 | close ( fd ); |
| 515 | |
| 516 | return first; |
| 517 | } |
| 518 | |
| 519 | /* return 1 = loaded, 0 = not loaded, -1 = can't tell */ |
| 520 | static int already_loaded (const char *name) |
| 521 | { |
| 522 | int fd; |
| 523 | char buffer[4096]; |
| 524 | |
| 525 | fd = open ("/proc/modules", O_RDONLY); |
| 526 | if (fd < 0) |
| 527 | return -1; |
| 528 | |
| 529 | while ( reads ( fd, buffer, sizeof( buffer ))) { |
| 530 | char *p; |
| 531 | |
| 532 | p = strchr (buffer, ' '); |
| 533 | if (p) { |
| 534 | *p = 0; |
| 535 | for( p = buffer; ENABLE_FEATURE_2_6_MODULES && *p; p++ ) { |
| 536 | *p = ((*p)=='-')?'_':*p; |
| 537 | } |
| 538 | if (strcmp (name, buffer) == 0) { |
| 539 | close (fd); |
| 540 | return 1; |
| 541 | } |
| 542 | } |
| 543 | } |
| 544 | |
| 545 | close (fd); |
| 546 | return 0; |
| 547 | } |
| 548 | |
Robert Griebl | 236abbf | 2002-05-22 23:34:35 +0000 | [diff] [blame] | 549 | static int mod_process ( struct mod_list_t *list, int do_insert ) |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 550 | { |
Eric Andersen | 44b5758 | 2004-08-03 08:23:33 +0000 | [diff] [blame] | 551 | int rc = 0; |
Rob Landley | 79e1cab | 2005-11-15 00:08:29 +0000 | [diff] [blame] | 552 | char **argv = NULL; |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 553 | struct mod_opt_t *opts; |
| 554 | int argc_malloc; /* never used when CONFIG_FEATURE_CLEAN_UP not defined */ |
Paul Fox | 8eeb655 | 2005-08-04 18:33:36 +0000 | [diff] [blame] | 555 | int argc; |
Eric Andersen | 864b797 | 2002-05-03 15:48:26 +0000 | [diff] [blame] | 556 | |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 557 | while ( list ) { |
Paul Fox | 8eeb655 | 2005-08-04 18:33:36 +0000 | [diff] [blame] | 558 | argc = 0; |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 559 | if( ENABLE_FEATURE_CLEAN_UP ) |
| 560 | argc_malloc = 0; |
| 561 | /* If CONFIG_FEATURE_CLEAN_UP is not defined, then we leak memory |
| 562 | * each time we allocate memory for argv. |
| 563 | * But it is (quite) small amounts of memory that leak each |
| 564 | * time a module is loaded, and it is reclaimed when modprobe |
| 565 | * exits anyway (even when standalone shell?). |
| 566 | * This could become a problem when loading a module with LOTS of |
| 567 | * dependencies, with LOTS of options for each dependencies, with |
| 568 | * very little memory on the target... But in that case, the module |
| 569 | * would not load because there is no more memory, so there's no |
| 570 | * problem. */ |
| 571 | /* enough for minimal insmod (5 args + NULL) or rmmod (3 args + NULL) */ |
| 572 | argv = (char**) malloc( 6 * sizeof( char* ) ); |
Glenn L McGrath | 350733a | 2003-09-08 00:32:49 +0000 | [diff] [blame] | 573 | if ( do_insert ) { |
Rob Landley | 79e1cab | 2005-11-15 00:08:29 +0000 | [diff] [blame] | 574 | if (already_loaded (list->m_name) != 1) { |
Paul Fox | 8eeb655 | 2005-08-04 18:33:36 +0000 | [diff] [blame] | 575 | argv[argc++] = "insmod"; |
| 576 | if (do_syslog) |
| 577 | argv[argc++] = "-s"; |
| 578 | if (autoclean) |
| 579 | argv[argc++] = "-k"; |
| 580 | if (quiet) |
| 581 | argv[argc++] = "-q"; |
Rob Landley | 79e1cab | 2005-11-15 00:08:29 +0000 | [diff] [blame] | 582 | else if(verbose) /* verbose and quiet are mutually exclusive */ |
| 583 | argv[argc++] = "-v"; |
Paul Fox | 8eeb655 | 2005-08-04 18:33:36 +0000 | [diff] [blame] | 584 | argv[argc++] = list-> m_path; |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 585 | if( ENABLE_FEATURE_CLEAN_UP ) |
| 586 | argc_malloc = argc; |
Rob Landley | 79e1cab | 2005-11-15 00:08:29 +0000 | [diff] [blame] | 587 | opts = list-> m_options; |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 588 | while( opts ) { |
| 589 | /* Add one more option */ |
Rob Landley | 79e1cab | 2005-11-15 00:08:29 +0000 | [diff] [blame] | 590 | argc++; |
| 591 | argv = (char**) xrealloc( argv, ( argc + 1 ) * sizeof( char* ) ); |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 592 | argv[argc-1] = opts-> m_opt_val; |
| 593 | opts = opts-> m_next; |
Rob Landley | 79e1cab | 2005-11-15 00:08:29 +0000 | [diff] [blame] | 594 | } |
Paul Fox | 8eeb655 | 2005-08-04 18:33:36 +0000 | [diff] [blame] | 595 | } |
Glenn L McGrath | 350733a | 2003-09-08 00:32:49 +0000 | [diff] [blame] | 596 | } else { |
Eric Andersen | 807bd84 | 2004-08-19 18:30:31 +0000 | [diff] [blame] | 597 | /* modutils uses short name for removal */ |
Rob Landley | 79e1cab | 2005-11-15 00:08:29 +0000 | [diff] [blame] | 598 | if (already_loaded (list->m_name) != 0) { |
Paul Fox | 8eeb655 | 2005-08-04 18:33:36 +0000 | [diff] [blame] | 599 | argv[argc++] = "rmmod"; |
| 600 | if (do_syslog) |
| 601 | argv[argc++] = "-s"; |
| 602 | argv[argc++] = list->m_name; |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 603 | if( ENABLE_FEATURE_CLEAN_UP ) |
| 604 | argc_malloc = argc; |
Paul Fox | 8eeb655 | 2005-08-04 18:33:36 +0000 | [diff] [blame] | 605 | } |
Glenn L McGrath | 350733a | 2003-09-08 00:32:49 +0000 | [diff] [blame] | 606 | } |
Paul Fox | 8eeb655 | 2005-08-04 18:33:36 +0000 | [diff] [blame] | 607 | argv[argc] = NULL; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 608 | |
Paul Fox | 8eeb655 | 2005-08-04 18:33:36 +0000 | [diff] [blame] | 609 | if (argc) { |
Glenn L McGrath | fcf4732 | 2004-08-11 05:56:30 +0000 | [diff] [blame] | 610 | if (verbose) { |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 611 | printf("%s module %s\n", do_insert?"Loading":"Unloading", list-> m_name ); |
Glenn L McGrath | fcf4732 | 2004-08-11 05:56:30 +0000 | [diff] [blame] | 612 | } |
| 613 | if (!show_only) { |
Paul Fox | 8eeb655 | 2005-08-04 18:33:36 +0000 | [diff] [blame] | 614 | int rc2 = 0; |
| 615 | int status; |
| 616 | switch (fork()) { |
| 617 | case -1: |
| 618 | rc2 = 1; |
| 619 | break; |
| 620 | case 0: //child |
| 621 | execvp(argv[0], argv); |
| 622 | bb_perror_msg_and_die("exec of %s", argv[0]); |
| 623 | /* NOTREACHED */ |
| 624 | default: |
| 625 | if (wait(&status) == -1) { |
| 626 | rc2 = 1; |
| 627 | break; |
| 628 | } |
| 629 | if (WIFEXITED(status)) |
| 630 | rc2 = WEXITSTATUS(status); |
| 631 | if (WIFSIGNALED(status)) |
| 632 | rc2 = WTERMSIG(status); |
| 633 | break; |
| 634 | } |
Glenn L McGrath | fcf4732 | 2004-08-11 05:56:30 +0000 | [diff] [blame] | 635 | if (do_insert) { |
| 636 | rc = rc2; /* only last module matters */ |
| 637 | } |
| 638 | else if (!rc2) { |
| 639 | rc = 0; /* success if remove any mod */ |
| 640 | } |
| 641 | } |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 642 | if( ENABLE_FEATURE_CLEAN_UP ) |
| 643 | /* the last value in the array has index == argc, but |
| 644 | * it is the terminating NULL, so we must not free it. */ |
| 645 | while( argc_malloc < argc ) { |
| 646 | free( argv[argc_malloc++] ); |
Rob Landley | 79e1cab | 2005-11-15 00:08:29 +0000 | [diff] [blame] | 647 | } |
Eric Andersen | 908e362 | 2003-06-20 09:56:37 +0000 | [diff] [blame] | 648 | } |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 649 | if( ENABLE_FEATURE_CLEAN_UP ) { |
| 650 | free( argv ); |
| 651 | argv = NULL; |
Rob Landley | 79e1cab | 2005-11-15 00:08:29 +0000 | [diff] [blame] | 652 | } |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 653 | list = do_insert ? list-> m_prev : list-> m_next; |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 654 | } |
Eric Andersen | 908e362 | 2003-06-20 09:56:37 +0000 | [diff] [blame] | 655 | return (show_only) ? 0 : rc; |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 656 | } |
| 657 | |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 658 | /* |
| 659 | * Builds the dependency list (aka stack) of a module. |
| 660 | * head: the highest module in the stack (last to insmod, first to rmmod) |
| 661 | * tail: the lowest module in the stack (first to insmod, last to rmmod) |
| 662 | */ |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 663 | static void check_dep ( char *mod, struct mod_list_t **head, struct mod_list_t **tail ) |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 664 | { |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 665 | struct mod_list_t *find; |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 666 | struct dep_t *dt; |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 667 | struct mod_opt_t *opt = 0; |
Eric Andersen | 807bd84 | 2004-08-19 18:30:31 +0000 | [diff] [blame] | 668 | char *path = 0; |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 669 | |
Robert Griebl | 1d4ef2a | 2002-05-28 21:32:10 +0000 | [diff] [blame] | 670 | // check dependencies |
| 671 | for ( dt = depend; dt; dt = dt-> m_next ) { |
Eric Andersen | 807bd84 | 2004-08-19 18:30:31 +0000 | [diff] [blame] | 672 | if ( strcmp ( dt-> m_name, mod ) == 0) { |
Robert Griebl | 1d4ef2a | 2002-05-28 21:32:10 +0000 | [diff] [blame] | 673 | break; |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 674 | } |
Robert Griebl | 1d4ef2a | 2002-05-28 21:32:10 +0000 | [diff] [blame] | 675 | } |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 676 | |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 677 | if( !dt ) { |
| 678 | bb_error_msg ("module %s not found.", mod); |
| 679 | return; |
| 680 | } |
| 681 | |
Robert Griebl | 1d4ef2a | 2002-05-28 21:32:10 +0000 | [diff] [blame] | 682 | // resolve alias names |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 683 | while ( dt-> m_isalias ) { |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 684 | if ( dt-> m_depcnt == 1 ) { |
| 685 | struct dep_t *adt; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 686 | |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 687 | for ( adt = depend; adt; adt = adt-> m_next ) { |
Eric Andersen | 807bd84 | 2004-08-19 18:30:31 +0000 | [diff] [blame] | 688 | if ( strcmp ( adt-> m_name, dt-> m_deparr [0] ) == 0 ) |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 689 | break; |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 690 | } |
Robert Griebl | 70112da | 2002-07-29 20:28:38 +0000 | [diff] [blame] | 691 | if ( adt ) { |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 692 | /* This is the module we are aliased to */ |
| 693 | struct mod_opt_t *opts = dt-> m_options; |
| 694 | /* Option of the alias are appended to the options of the module */ |
| 695 | while( opts ) { |
| 696 | adt-> m_options = append_option( adt-> m_options, opts-> m_opt_val ); |
| 697 | opts = opts-> m_next; |
| 698 | } |
Eric Andersen | 716ccb2 | 2004-01-10 11:29:31 +0000 | [diff] [blame] | 699 | dt = adt; |
Robert Griebl | 70112da | 2002-07-29 20:28:38 +0000 | [diff] [blame] | 700 | } |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 701 | else { |
| 702 | bb_error_msg ("module %s not found.", mod); |
Robert Griebl | 70112da | 2002-07-29 20:28:38 +0000 | [diff] [blame] | 703 | return; |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 704 | } |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 705 | } |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 706 | else { |
| 707 | bb_error_msg ("Bad alias %s", dt-> m_name); |
Eric Andersen | 716ccb2 | 2004-01-10 11:29:31 +0000 | [diff] [blame] | 708 | return; |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 709 | } |
Robert Griebl | 1d4ef2a | 2002-05-28 21:32:10 +0000 | [diff] [blame] | 710 | } |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 711 | |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 712 | mod = dt-> m_name; |
| 713 | path = dt-> m_path; |
| 714 | opt = dt-> m_options; |
Bernhard Reutner-Fischer | 17d355c | 2005-12-14 08:32:44 +0000 | [diff] [blame] | 715 | |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 716 | // search for duplicates |
| 717 | for ( find = *head; find; find = find-> m_next ) { |
Eric Andersen | 807bd84 | 2004-08-19 18:30:31 +0000 | [diff] [blame] | 718 | if ( !strcmp ( mod, find-> m_name )) { |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 719 | // found -> dequeue it |
| 720 | |
| 721 | if ( find-> m_prev ) |
| 722 | find-> m_prev-> m_next = find-> m_next; |
| 723 | else |
| 724 | *head = find-> m_next; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 725 | |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 726 | if ( find-> m_next ) |
| 727 | find-> m_next-> m_prev = find-> m_prev; |
| 728 | else |
| 729 | *tail = find-> m_prev; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 730 | |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 731 | break; // there can be only one duplicate |
Eric Andersen | 716ccb2 | 2004-01-10 11:29:31 +0000 | [diff] [blame] | 732 | } |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 733 | } |
| 734 | |
| 735 | if ( !find ) { // did not find a duplicate |
Eric Andersen | 716ccb2 | 2004-01-10 11:29:31 +0000 | [diff] [blame] | 736 | find = (struct mod_list_t *) xmalloc ( sizeof(struct mod_list_t)); |
Eric Andersen | 807bd84 | 2004-08-19 18:30:31 +0000 | [diff] [blame] | 737 | find-> m_name = mod; |
| 738 | find-> m_path = path; |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 739 | find-> m_options = opt; |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 740 | } |
| 741 | |
Eric Andersen | 716ccb2 | 2004-01-10 11:29:31 +0000 | [diff] [blame] | 742 | // enqueue at tail |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 743 | if ( *tail ) |
| 744 | (*tail)-> m_next = find; |
| 745 | find-> m_prev = *tail; |
| 746 | find-> m_next = 0; |
Robert Griebl | 1d4ef2a | 2002-05-28 21:32:10 +0000 | [diff] [blame] | 747 | |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 748 | if ( !*head ) |
| 749 | *head = find; |
| 750 | *tail = find; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 751 | |
Eric Andersen | 716ccb2 | 2004-01-10 11:29:31 +0000 | [diff] [blame] | 752 | if ( dt ) { |
Robert Griebl | 1d4ef2a | 2002-05-28 21:32:10 +0000 | [diff] [blame] | 753 | int i; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 754 | |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 755 | /* Add all dependable module for that new module */ |
Robert Griebl | 1d4ef2a | 2002-05-28 21:32:10 +0000 | [diff] [blame] | 756 | for ( i = 0; i < dt-> m_depcnt; i++ ) |
| 757 | check_dep ( dt-> m_deparr [i], head, tail ); |
| 758 | } |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 759 | } |
| 760 | |
Robert Griebl | 236abbf | 2002-05-22 23:34:35 +0000 | [diff] [blame] | 761 | static int mod_insert ( char *mod, int argc, char **argv ) |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 762 | { |
| 763 | struct mod_list_t *tail = 0; |
Eric Andersen | 716ccb2 | 2004-01-10 11:29:31 +0000 | [diff] [blame] | 764 | struct mod_list_t *head = 0; |
Eric Andersen | 908e362 | 2003-06-20 09:56:37 +0000 | [diff] [blame] | 765 | int rc; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 766 | |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 767 | // get dep list for module mod |
| 768 | check_dep ( mod, &head, &tail ); |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 769 | |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 770 | if ( head && tail ) { |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 771 | if( argc ) { |
Eric Andersen | 716ccb2 | 2004-01-10 11:29:31 +0000 | [diff] [blame] | 772 | int i; |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 773 | // append module args |
Eric Andersen | 716ccb2 | 2004-01-10 11:29:31 +0000 | [diff] [blame] | 774 | for ( i = 0; i < argc; i++ ) |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 775 | head->m_options = append_option( head->m_options, argv[i] ); |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 776 | } |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 777 | |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 778 | // process tail ---> head |
Eric Andersen | 908e362 | 2003-06-20 09:56:37 +0000 | [diff] [blame] | 779 | rc = mod_process ( tail, 1 ); |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 780 | } |
| 781 | else |
| 782 | rc = 1; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 783 | |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 784 | return rc; |
| 785 | } |
| 786 | |
Eric Andersen | 908e362 | 2003-06-20 09:56:37 +0000 | [diff] [blame] | 787 | static int mod_remove ( char *mod ) |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 788 | { |
Eric Andersen | 908e362 | 2003-06-20 09:56:37 +0000 | [diff] [blame] | 789 | int rc; |
Eric Andersen | 716ccb2 | 2004-01-10 11:29:31 +0000 | [diff] [blame] | 790 | static struct mod_list_t rm_a_dummy = { "-a", 0, 0 }; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 791 | |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 792 | struct mod_list_t *head = 0; |
| 793 | struct mod_list_t *tail = 0; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 794 | |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 795 | if ( mod ) |
| 796 | check_dep ( mod, &head, &tail ); |
| 797 | else // autoclean |
| 798 | head = tail = &rm_a_dummy; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 799 | |
Robert Griebl | 236abbf | 2002-05-22 23:34:35 +0000 | [diff] [blame] | 800 | if ( head && tail ) |
Eric Andersen | 908e362 | 2003-06-20 09:56:37 +0000 | [diff] [blame] | 801 | rc = mod_process ( head, 0 ); // process head ---> tail |
| 802 | else |
| 803 | rc = 1; |
| 804 | return rc; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 805 | |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 806 | } |
| 807 | |
Eric Andersen | 0139ca9 | 2001-07-22 23:01:03 +0000 | [diff] [blame] | 808 | extern int modprobe_main(int argc, char** argv) |
| 809 | { |
Robert Griebl | 236abbf | 2002-05-22 23:34:35 +0000 | [diff] [blame] | 810 | int opt; |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 811 | int rc = EXIT_SUCCESS; |
Robert Griebl | 236abbf | 2002-05-22 23:34:35 +0000 | [diff] [blame] | 812 | int remove_opt = 0; |
Eric Andersen | 0139ca9 | 2001-07-22 23:01:03 +0000 | [diff] [blame] | 813 | |
Robert Griebl | 236abbf | 2002-05-22 23:34:35 +0000 | [diff] [blame] | 814 | autoclean = show_only = quiet = do_syslog = verbose = 0; |
| 815 | |
| 816 | while ((opt = getopt(argc, argv, "acdklnqrst:vVC:")) != -1) { |
| 817 | switch(opt) { |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 818 | case 'c': // no config used |
| 819 | case 'l': // no pattern matching |
| 820 | return EXIT_SUCCESS; |
| 821 | break; |
| 822 | case 'C': // no config used |
| 823 | case 't': // no pattern matching |
| 824 | bb_error_msg_and_die("-t and -C not supported"); |
Robert Griebl | 236abbf | 2002-05-22 23:34:35 +0000 | [diff] [blame] | 825 | |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 826 | case 'a': // ignore |
| 827 | case 'd': // ignore |
| 828 | break; |
| 829 | case 'k': |
| 830 | autoclean++; |
| 831 | break; |
| 832 | case 'n': |
| 833 | show_only++; |
| 834 | break; |
| 835 | case 'q': |
Rob Landley | 79e1cab | 2005-11-15 00:08:29 +0000 | [diff] [blame] | 836 | quiet++; verbose=0; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 837 | break; |
| 838 | case 'r': |
| 839 | remove_opt++; |
| 840 | break; |
| 841 | case 's': |
| 842 | do_syslog++; |
| 843 | break; |
| 844 | case 'v': |
Rob Landley | 79e1cab | 2005-11-15 00:08:29 +0000 | [diff] [blame] | 845 | verbose++; quiet=0; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 846 | break; |
| 847 | case 'V': |
| 848 | default: |
| 849 | bb_show_usage(); |
| 850 | break; |
Eric Andersen | 0139ca9 | 2001-07-22 23:01:03 +0000 | [diff] [blame] | 851 | } |
Eric Andersen | 0139ca9 | 2001-07-22 23:01:03 +0000 | [diff] [blame] | 852 | } |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 853 | |
Eric Andersen | 716ccb2 | 2004-01-10 11:29:31 +0000 | [diff] [blame] | 854 | depend = build_dep ( ); |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 855 | |
Eric Andersen | 716ccb2 | 2004-01-10 11:29:31 +0000 | [diff] [blame] | 856 | if ( !depend ) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 857 | bb_error_msg_and_die ( "could not parse modules.dep\n" ); |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 858 | |
Eric Andersen | 1b06419 | 2001-07-25 07:23:38 +0000 | [diff] [blame] | 859 | if (remove_opt) { |
Eric Andersen | 0139ca9 | 2001-07-22 23:01:03 +0000 | [diff] [blame] | 860 | do { |
Eric Andersen | 908e362 | 2003-06-20 09:56:37 +0000 | [diff] [blame] | 861 | if (mod_remove ( optind < argc ? |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 862 | bb_xstrdup (argv [optind]) : NULL )) { |
Eric Andersen | 908e362 | 2003-06-20 09:56:37 +0000 | [diff] [blame] | 863 | bb_error_msg ("failed to remove module %s", |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 864 | argv [optind] ); |
Eric Andersen | 908e362 | 2003-06-20 09:56:37 +0000 | [diff] [blame] | 865 | rc = EXIT_FAILURE; |
| 866 | } |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 867 | } while ( ++optind < argc ); |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 868 | } else { |
| 869 | if (optind >= argc) |
| 870 | bb_error_msg_and_die ( "No module or pattern provided\n" ); |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 871 | |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 872 | if ( mod_insert ( bb_xstrdup ( argv [optind] ), argc - optind - 1, argv + optind + 1 )) |
| 873 | bb_error_msg_and_die ( "failed to load module %s", argv [optind] ); |
Eric Andersen | 0139ca9 | 2001-07-22 23:01:03 +0000 | [diff] [blame] | 874 | } |
| 875 | |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 876 | /* Here would be a good place to free up memory allocated during the dependencies build. */ |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 877 | |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 878 | return rc; |
Eric Andersen | 0139ca9 | 2001-07-22 23:01:03 +0000 | [diff] [blame] | 879 | } |