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> |
Bernhard Reutner-Fischer | 2c351a8 | 2006-06-03 19:08:49 +0000 | [diff] [blame^] | 24 | #include <fnmatch.h> |
Eric Andersen | 0139ca9 | 2001-07-22 23:01:03 +0000 | [diff] [blame] | 25 | #include "busybox.h" |
| 26 | |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 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; |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 30 | }; |
| 31 | |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 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 */ |
Bernhard Reutner-Fischer | 17d355c | 2005-12-14 08:32:44 +0000 | [diff] [blame] | 37 | |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 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 */ |
Eric Andersen | 807bd84 | 2004-08-19 18:30:31 +0000 | [diff] [blame] | 49 | char * m_name; |
| 50 | char * m_path; |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 51 | struct mod_opt_t * m_options; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 52 | |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 53 | struct mod_list_t * m_prev; |
| 54 | struct mod_list_t * m_next; |
| 55 | }; |
| 56 | |
| 57 | |
Robert Griebl | 236abbf | 2002-05-22 23:34:35 +0000 | [diff] [blame] | 58 | static struct dep_t *depend; |
"Vladimir N. Oleynik" | 4fc9220 | 2006-02-02 14:48:54 +0000 | [diff] [blame] | 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; |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 83 | |
Eric Andersen | 14f5c8d | 2005-04-16 19:39:00 +0000 | [diff] [blame] | 84 | static int parse_tag_value ( char *buffer, char **ptag, char **pvalue ) |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 85 | { |
| 86 | char *tag, *value; |
| 87 | |
| 88 | while ( isspace ( *buffer )) |
Eric Andersen | 716ccb2 | 2004-01-10 11:29:31 +0000 | [diff] [blame] | 89 | buffer++; |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 90 | tag = value = buffer; |
| 91 | while ( !isspace ( *value )) |
Eric Andersen | 7e496a7 | 2004-04-06 12:06:03 +0000 | [diff] [blame] | 92 | if (!*value) return 0; |
| 93 | else value++; |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 94 | *value++ = 0; |
| 95 | while ( isspace ( *value )) |
| 96 | value++; |
Eric Andersen | 7e496a7 | 2004-04-06 12:06:03 +0000 | [diff] [blame] | 97 | if (!*value) return 0; |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 98 | |
| 99 | *ptag = tag; |
| 100 | *pvalue = value; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 101 | |
Eric Andersen | 7e496a7 | 2004-04-06 12:06:03 +0000 | [diff] [blame] | 102 | return 1; |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 103 | } |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 104 | |
Robert Griebl | bc28f7a | 2002-06-04 19:33:58 +0000 | [diff] [blame] | 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 |
Eric Andersen | 716ccb2 | 2004-01-10 11:29:31 +0000 | [diff] [blame] | 107 | * thread and stdio junk can overflow the limited stack... |
Robert Griebl | bc28f7a | 2002-06-04 19:33:58 +0000 | [diff] [blame] | 108 | */ |
| 109 | static char *reads ( int fd, char *buffer, size_t len ) |
| 110 | { |
| 111 | int n = read ( fd, buffer, len ); |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 112 | |
Robert Griebl | bc28f7a | 2002-06-04 19:33:58 +0000 | [diff] [blame] | 113 | if ( n > 0 ) { |
| 114 | char *p; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 115 | |
Robert Griebl | bc28f7a | 2002-06-04 19:33:58 +0000 | [diff] [blame] | 116 | buffer [len-1] = 0; |
| 117 | p = strchr ( buffer, '\n' ); |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 118 | |
Robert Griebl | bc28f7a | 2002-06-04 19:33:58 +0000 | [diff] [blame] | 119 | if ( p ) { |
| 120 | off_t offset; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 121 | |
Eric Andersen | 716ccb2 | 2004-01-10 11:29:31 +0000 | [diff] [blame] | 122 | offset = lseek ( fd, 0L, SEEK_CUR ); // Get the current file descriptor offset |
Robert Griebl | bc28f7a | 2002-06-04 19:33:58 +0000 | [diff] [blame] | 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 | } |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 129 | |
Robert Griebl | bc28f7a | 2002-06-04 19:33:58 +0000 | [diff] [blame] | 130 | else |
| 131 | return 0; |
| 132 | } |
| 133 | |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 134 | /* |
| 135 | * This function appends an option to a list |
| 136 | */ |
Bernhard Reutner-Fischer | 101a470 | 2006-04-03 15:46:14 +0000 | [diff] [blame] | 137 | static 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] | 138 | { |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 139 | struct mod_opt_t *ol = opt_list; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 140 | |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 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 ) ); |
Eric Andersen | 03d8091 | 2003-12-19 21:04:19 +0000 | [diff] [blame] | 149 | } |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 150 | |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 151 | ol-> m_opt_val = bb_xstrdup( opt ); |
| 152 | ol-> m_next = NULL; |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 153 | |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 154 | return opt_list; |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Bernhard Reutner-Fischer | 17d355c | 2005-12-14 08:32:44 +0000 | [diff] [blame] | 157 | #if ENABLE_FEATURE_MODPROBE_MULTIPLE_OPTIONS |
Rob Landley | 79e1cab | 2005-11-15 00:08:29 +0000 | [diff] [blame] | 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 |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 186 | * By the way, we duplicate a little too much |
| 187 | * here but what is too much is freed later. */ |
Rob Landley | 79e1cab | 2005-11-15 00:08:29 +0000 | [diff] [blame] | 188 | *dst = tmp_str = bb_xstrdup( src ); |
| 189 | /* Get to the end of that argument */ |
| 190 | while( ( *tmp_str != '\0' ) |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 191 | && ( ( *tmp_str != ' ' ) |
| 192 | || ( opt_status & ( ARG_IN_DQUOTES | ARG_IN_SQUOTES ) ) ) ) { |
Rob Landley | 79e1cab | 2005-11-15 00:08:29 +0000 | [diff] [blame] | 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 | } |
"Vladimir N. Oleynik" | 4fc9220 | 2006-02-02 14:48:54 +0000 | [diff] [blame] | 261 | *tmp_str++ = '\0'; |
| 262 | *dst = xrealloc( *dst, (tmp_str - *dst ) ); |
Rob Landley | 79e1cab | 2005-11-15 00:08:29 +0000 | [diff] [blame] | 263 | return src; |
| 264 | } |
Rob Landley | ae50c6d | 2005-12-15 07:42:13 +0000 | [diff] [blame] | 265 | #else |
| 266 | #define parse_command_string(src, dst) (0) |
Bernhard Reutner-Fischer | 17d355c | 2005-12-14 08:32:44 +0000 | [diff] [blame] | 267 | #endif /* ENABLE_FEATURE_MODPROBE_MULTIPLE_OPTIONS */ |
Rob Landley | 79e1cab | 2005-11-15 00:08:29 +0000 | [diff] [blame] | 268 | |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 269 | /* |
Rob Landley | 7261575 | 2006-04-10 16:09:52 +0000 | [diff] [blame] | 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 | |
Rob Landley | a389651 | 2006-05-07 20:20:34 +0000 | [diff] [blame] | 287 | l = strlen ( buffer ); |
Rob Landley | 7261575 | 2006-04-10 16:09:52 +0000 | [diff] [blame] | 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 | /* |
Bernhard Reutner-Fischer | e3c150b | 2006-05-19 11:24:28 +0000 | [diff] [blame] | 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 |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 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]; |
"Vladimir N. Oleynik" | 4fc9220 | 2006-02-02 14:48:54 +0000 | [diff] [blame] | 379 | char *filename; |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 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 | |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 387 | if (un.release[0] == '2') { |
| 388 | k_version = un.release[2] - '0'; |
| 389 | } |
| 390 | |
"Vladimir N. Oleynik" | 4fc9220 | 2006-02-02 14:48:54 +0000 | [diff] [blame] | 391 | filename = bb_xasprintf("/lib/modules/%s/modules.dep", un.release ); |
Rob Landley | 3afb070 | 2006-05-18 20:41:43 +0000 | [diff] [blame] | 392 | fd = open ( filename, O_RDONLY ); |
Bernhard Reutner-Fischer | e3c150b | 2006-05-19 11:24:28 +0000 | [diff] [blame] | 393 | if (ENABLE_FEATURE_CLEAN_UP) |
| 394 | free(filename); |
Rob Landley | 3afb070 | 2006-05-18 20:41:43 +0000 | [diff] [blame] | 395 | if (fd < 0) { |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 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 ))) { |
Rob Landley | a389651 | 2006-05-07 20:20:34 +0000 | [diff] [blame] | 403 | int l = strlen ( buffer ); |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 404 | char *p = 0; |
| 405 | |
"Vladimir N. Oleynik" | 4fc9220 | 2006-02-02 14:48:54 +0000 | [diff] [blame] | 406 | while ( l > 0 && isspace ( buffer [l-1] )) { |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 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 ) { |
Bernhard Reutner-Fischer | 17d355c | 2005-12-14 08:32:44 +0000 | [diff] [blame] | 418 | /* find the dep beginning */ |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 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 | |
Bernhard Reutner-Fischer | 17d355c | 2005-12-14 08:32:44 +0000 | [diff] [blame] | 504 | /* find the beginning of the module file name */ |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 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 | |
Rob Landley | ae50c6d | 2005-12-15 07:42:13 +0000 | [diff] [blame] | 548 | if (!ENABLE_FEATURE_2_6_MODULES |
| 549 | || ( fd = open ( "/etc/modprobe.conf", O_RDONLY )) < 0 ) |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 550 | if (( fd = open ( "/etc/modules.conf", O_RDONLY )) < 0 ) |
| 551 | if (( fd = open ( "/etc/conf.modules", O_RDONLY )) < 0 ) |
| 552 | return first; |
| 553 | |
Rob Landley | 7261575 | 2006-04-10 16:09:52 +0000 | [diff] [blame] | 554 | include_conf (&first, ¤t, buffer, sizeof(buffer), fd); |
| 555 | close(fd); |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 556 | |
Bernhard Reutner-Fischer | e3c150b | 2006-05-19 11:24:28 +0000 | [diff] [blame] | 557 | filename = bb_xasprintf("/lib/modules/%s/modules.alias", un.release); |
| 558 | fd = open ( filename, O_RDONLY ); |
| 559 | if (ENABLE_FEATURE_CLEAN_UP) |
| 560 | free(filename); |
| 561 | if (fd < 0) { |
Rob Landley | 7261575 | 2006-04-10 16:09:52 +0000 | [diff] [blame] | 562 | /* Ok, that didn't work. Fall back to looking in /lib/modules */ |
| 563 | if (( fd = open ( "/lib/modules/modules.alias", O_RDONLY )) < 0 ) { |
| 564 | return first; |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 565 | } |
| 566 | } |
Rob Landley | 7261575 | 2006-04-10 16:09:52 +0000 | [diff] [blame] | 567 | |
| 568 | include_conf (&first, ¤t, buffer, sizeof(buffer), fd); |
| 569 | close(fd); |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 570 | |
| 571 | return first; |
| 572 | } |
| 573 | |
| 574 | /* return 1 = loaded, 0 = not loaded, -1 = can't tell */ |
| 575 | static int already_loaded (const char *name) |
| 576 | { |
| 577 | int fd; |
| 578 | char buffer[4096]; |
| 579 | |
| 580 | fd = open ("/proc/modules", O_RDONLY); |
| 581 | if (fd < 0) |
| 582 | return -1; |
| 583 | |
| 584 | while ( reads ( fd, buffer, sizeof( buffer ))) { |
| 585 | char *p; |
| 586 | |
| 587 | p = strchr (buffer, ' '); |
| 588 | if (p) { |
| 589 | *p = 0; |
| 590 | for( p = buffer; ENABLE_FEATURE_2_6_MODULES && *p; p++ ) { |
| 591 | *p = ((*p)=='-')?'_':*p; |
| 592 | } |
| 593 | if (strcmp (name, buffer) == 0) { |
| 594 | close (fd); |
| 595 | return 1; |
| 596 | } |
| 597 | } |
| 598 | } |
| 599 | |
| 600 | close (fd); |
| 601 | return 0; |
| 602 | } |
| 603 | |
Robert Griebl | 236abbf | 2002-05-22 23:34:35 +0000 | [diff] [blame] | 604 | static int mod_process ( struct mod_list_t *list, int do_insert ) |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 605 | { |
Eric Andersen | 44b5758 | 2004-08-03 08:23:33 +0000 | [diff] [blame] | 606 | int rc = 0; |
Rob Landley | 79e1cab | 2005-11-15 00:08:29 +0000 | [diff] [blame] | 607 | char **argv = NULL; |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 608 | struct mod_opt_t *opts; |
| 609 | int argc_malloc; /* never used when CONFIG_FEATURE_CLEAN_UP not defined */ |
Paul Fox | 8eeb655 | 2005-08-04 18:33:36 +0000 | [diff] [blame] | 610 | int argc; |
Eric Andersen | 864b797 | 2002-05-03 15:48:26 +0000 | [diff] [blame] | 611 | |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 612 | while ( list ) { |
Paul Fox | 8eeb655 | 2005-08-04 18:33:36 +0000 | [diff] [blame] | 613 | argc = 0; |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 614 | if( ENABLE_FEATURE_CLEAN_UP ) |
| 615 | argc_malloc = 0; |
| 616 | /* If CONFIG_FEATURE_CLEAN_UP is not defined, then we leak memory |
| 617 | * each time we allocate memory for argv. |
| 618 | * But it is (quite) small amounts of memory that leak each |
| 619 | * time a module is loaded, and it is reclaimed when modprobe |
| 620 | * exits anyway (even when standalone shell?). |
| 621 | * This could become a problem when loading a module with LOTS of |
| 622 | * dependencies, with LOTS of options for each dependencies, with |
| 623 | * very little memory on the target... But in that case, the module |
| 624 | * would not load because there is no more memory, so there's no |
| 625 | * problem. */ |
| 626 | /* enough for minimal insmod (5 args + NULL) or rmmod (3 args + NULL) */ |
| 627 | argv = (char**) malloc( 6 * sizeof( char* ) ); |
Glenn L McGrath | 350733a | 2003-09-08 00:32:49 +0000 | [diff] [blame] | 628 | if ( do_insert ) { |
Rob Landley | 79e1cab | 2005-11-15 00:08:29 +0000 | [diff] [blame] | 629 | if (already_loaded (list->m_name) != 1) { |
Paul Fox | 8eeb655 | 2005-08-04 18:33:36 +0000 | [diff] [blame] | 630 | argv[argc++] = "insmod"; |
| 631 | if (do_syslog) |
| 632 | argv[argc++] = "-s"; |
| 633 | if (autoclean) |
| 634 | argv[argc++] = "-k"; |
| 635 | if (quiet) |
| 636 | argv[argc++] = "-q"; |
Rob Landley | 79e1cab | 2005-11-15 00:08:29 +0000 | [diff] [blame] | 637 | else if(verbose) /* verbose and quiet are mutually exclusive */ |
| 638 | argv[argc++] = "-v"; |
Paul Fox | 8eeb655 | 2005-08-04 18:33:36 +0000 | [diff] [blame] | 639 | argv[argc++] = list-> m_path; |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 640 | if( ENABLE_FEATURE_CLEAN_UP ) |
| 641 | argc_malloc = argc; |
Rob Landley | 79e1cab | 2005-11-15 00:08:29 +0000 | [diff] [blame] | 642 | opts = list-> m_options; |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 643 | while( opts ) { |
| 644 | /* Add one more option */ |
Rob Landley | 79e1cab | 2005-11-15 00:08:29 +0000 | [diff] [blame] | 645 | argc++; |
| 646 | argv = (char**) xrealloc( argv, ( argc + 1 ) * sizeof( char* ) ); |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 647 | argv[argc-1] = opts-> m_opt_val; |
| 648 | opts = opts-> m_next; |
Rob Landley | 79e1cab | 2005-11-15 00:08:29 +0000 | [diff] [blame] | 649 | } |
Paul Fox | 8eeb655 | 2005-08-04 18:33:36 +0000 | [diff] [blame] | 650 | } |
Glenn L McGrath | 350733a | 2003-09-08 00:32:49 +0000 | [diff] [blame] | 651 | } else { |
Eric Andersen | 807bd84 | 2004-08-19 18:30:31 +0000 | [diff] [blame] | 652 | /* modutils uses short name for removal */ |
Rob Landley | 79e1cab | 2005-11-15 00:08:29 +0000 | [diff] [blame] | 653 | if (already_loaded (list->m_name) != 0) { |
Paul Fox | 8eeb655 | 2005-08-04 18:33:36 +0000 | [diff] [blame] | 654 | argv[argc++] = "rmmod"; |
| 655 | if (do_syslog) |
| 656 | argv[argc++] = "-s"; |
| 657 | argv[argc++] = list->m_name; |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 658 | if( ENABLE_FEATURE_CLEAN_UP ) |
| 659 | argc_malloc = argc; |
Paul Fox | 8eeb655 | 2005-08-04 18:33:36 +0000 | [diff] [blame] | 660 | } |
Glenn L McGrath | 350733a | 2003-09-08 00:32:49 +0000 | [diff] [blame] | 661 | } |
Paul Fox | 8eeb655 | 2005-08-04 18:33:36 +0000 | [diff] [blame] | 662 | argv[argc] = NULL; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 663 | |
Paul Fox | 8eeb655 | 2005-08-04 18:33:36 +0000 | [diff] [blame] | 664 | if (argc) { |
Glenn L McGrath | fcf4732 | 2004-08-11 05:56:30 +0000 | [diff] [blame] | 665 | if (verbose) { |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 666 | 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] | 667 | } |
| 668 | if (!show_only) { |
Paul Fox | 8eeb655 | 2005-08-04 18:33:36 +0000 | [diff] [blame] | 669 | int rc2 = 0; |
| 670 | int status; |
| 671 | switch (fork()) { |
| 672 | case -1: |
| 673 | rc2 = 1; |
| 674 | break; |
| 675 | case 0: //child |
| 676 | execvp(argv[0], argv); |
| 677 | bb_perror_msg_and_die("exec of %s", argv[0]); |
| 678 | /* NOTREACHED */ |
| 679 | default: |
| 680 | if (wait(&status) == -1) { |
| 681 | rc2 = 1; |
| 682 | break; |
| 683 | } |
| 684 | if (WIFEXITED(status)) |
| 685 | rc2 = WEXITSTATUS(status); |
| 686 | if (WIFSIGNALED(status)) |
| 687 | rc2 = WTERMSIG(status); |
| 688 | break; |
| 689 | } |
Glenn L McGrath | fcf4732 | 2004-08-11 05:56:30 +0000 | [diff] [blame] | 690 | if (do_insert) { |
| 691 | rc = rc2; /* only last module matters */ |
| 692 | } |
| 693 | else if (!rc2) { |
| 694 | rc = 0; /* success if remove any mod */ |
| 695 | } |
| 696 | } |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 697 | if( ENABLE_FEATURE_CLEAN_UP ) |
| 698 | /* the last value in the array has index == argc, but |
| 699 | * it is the terminating NULL, so we must not free it. */ |
| 700 | while( argc_malloc < argc ) { |
| 701 | free( argv[argc_malloc++] ); |
Rob Landley | 79e1cab | 2005-11-15 00:08:29 +0000 | [diff] [blame] | 702 | } |
Eric Andersen | 908e362 | 2003-06-20 09:56:37 +0000 | [diff] [blame] | 703 | } |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 704 | if( ENABLE_FEATURE_CLEAN_UP ) { |
| 705 | free( argv ); |
| 706 | argv = NULL; |
Rob Landley | 79e1cab | 2005-11-15 00:08:29 +0000 | [diff] [blame] | 707 | } |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 708 | list = do_insert ? list-> m_prev : list-> m_next; |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 709 | } |
Eric Andersen | 908e362 | 2003-06-20 09:56:37 +0000 | [diff] [blame] | 710 | return (show_only) ? 0 : rc; |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 711 | } |
| 712 | |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 713 | /* |
| 714 | * Builds the dependency list (aka stack) of a module. |
| 715 | * head: the highest module in the stack (last to insmod, first to rmmod) |
| 716 | * tail: the lowest module in the stack (first to insmod, last to rmmod) |
| 717 | */ |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 718 | 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] | 719 | { |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 720 | struct mod_list_t *find; |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 721 | struct dep_t *dt; |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 722 | struct mod_opt_t *opt = 0; |
Eric Andersen | 807bd84 | 2004-08-19 18:30:31 +0000 | [diff] [blame] | 723 | char *path = 0; |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 724 | |
Bernhard Reutner-Fischer | 2c351a8 | 2006-06-03 19:08:49 +0000 | [diff] [blame^] | 725 | /* Search for the given module name amongst all dependency rules. |
| 726 | * The module name in a dependency rule can be a shell pattern, |
| 727 | * so try to match the given module name against such a pattern. |
| 728 | * Of course if the name in the dependency rule is a plain string, |
| 729 | * then we consider it a pattern, and matching will still work. */ |
Robert Griebl | 1d4ef2a | 2002-05-28 21:32:10 +0000 | [diff] [blame] | 730 | for ( dt = depend; dt; dt = dt-> m_next ) { |
Bernhard Reutner-Fischer | 2c351a8 | 2006-06-03 19:08:49 +0000 | [diff] [blame^] | 731 | if ( fnmatch ( dt-> m_name, mod, 0 ) == 0) { |
Robert Griebl | 1d4ef2a | 2002-05-28 21:32:10 +0000 | [diff] [blame] | 732 | break; |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 733 | } |
Robert Griebl | 1d4ef2a | 2002-05-28 21:32:10 +0000 | [diff] [blame] | 734 | } |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 735 | |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 736 | if( !dt ) { |
| 737 | bb_error_msg ("module %s not found.", mod); |
| 738 | return; |
| 739 | } |
| 740 | |
Robert Griebl | 1d4ef2a | 2002-05-28 21:32:10 +0000 | [diff] [blame] | 741 | // resolve alias names |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 742 | while ( dt-> m_isalias ) { |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 743 | if ( dt-> m_depcnt == 1 ) { |
| 744 | struct dep_t *adt; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 745 | |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 746 | for ( adt = depend; adt; adt = adt-> m_next ) { |
Eric Andersen | 807bd84 | 2004-08-19 18:30:31 +0000 | [diff] [blame] | 747 | if ( strcmp ( adt-> m_name, dt-> m_deparr [0] ) == 0 ) |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 748 | break; |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 749 | } |
Robert Griebl | 70112da | 2002-07-29 20:28:38 +0000 | [diff] [blame] | 750 | if ( adt ) { |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 751 | /* This is the module we are aliased to */ |
| 752 | struct mod_opt_t *opts = dt-> m_options; |
| 753 | /* Option of the alias are appended to the options of the module */ |
| 754 | while( opts ) { |
| 755 | adt-> m_options = append_option( adt-> m_options, opts-> m_opt_val ); |
| 756 | opts = opts-> m_next; |
| 757 | } |
Eric Andersen | 716ccb2 | 2004-01-10 11:29:31 +0000 | [diff] [blame] | 758 | dt = adt; |
Robert Griebl | 70112da | 2002-07-29 20:28:38 +0000 | [diff] [blame] | 759 | } |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 760 | else { |
| 761 | bb_error_msg ("module %s not found.", mod); |
Robert Griebl | 70112da | 2002-07-29 20:28:38 +0000 | [diff] [blame] | 762 | return; |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 763 | } |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 764 | } |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 765 | else { |
| 766 | bb_error_msg ("Bad alias %s", dt-> m_name); |
Eric Andersen | 716ccb2 | 2004-01-10 11:29:31 +0000 | [diff] [blame] | 767 | return; |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 768 | } |
Robert Griebl | 1d4ef2a | 2002-05-28 21:32:10 +0000 | [diff] [blame] | 769 | } |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 770 | |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 771 | mod = dt-> m_name; |
| 772 | path = dt-> m_path; |
| 773 | opt = dt-> m_options; |
Bernhard Reutner-Fischer | 17d355c | 2005-12-14 08:32:44 +0000 | [diff] [blame] | 774 | |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 775 | // search for duplicates |
| 776 | for ( find = *head; find; find = find-> m_next ) { |
Eric Andersen | 807bd84 | 2004-08-19 18:30:31 +0000 | [diff] [blame] | 777 | if ( !strcmp ( mod, find-> m_name )) { |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 778 | // found -> dequeue it |
| 779 | |
| 780 | if ( find-> m_prev ) |
| 781 | find-> m_prev-> m_next = find-> m_next; |
| 782 | else |
| 783 | *head = find-> m_next; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 784 | |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 785 | if ( find-> m_next ) |
| 786 | find-> m_next-> m_prev = find-> m_prev; |
| 787 | else |
| 788 | *tail = find-> m_prev; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 789 | |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 790 | break; // there can be only one duplicate |
Eric Andersen | 716ccb2 | 2004-01-10 11:29:31 +0000 | [diff] [blame] | 791 | } |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | if ( !find ) { // did not find a duplicate |
Eric Andersen | 716ccb2 | 2004-01-10 11:29:31 +0000 | [diff] [blame] | 795 | find = (struct mod_list_t *) xmalloc ( sizeof(struct mod_list_t)); |
Eric Andersen | 807bd84 | 2004-08-19 18:30:31 +0000 | [diff] [blame] | 796 | find-> m_name = mod; |
| 797 | find-> m_path = path; |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 798 | find-> m_options = opt; |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 799 | } |
| 800 | |
Eric Andersen | 716ccb2 | 2004-01-10 11:29:31 +0000 | [diff] [blame] | 801 | // enqueue at tail |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 802 | if ( *tail ) |
| 803 | (*tail)-> m_next = find; |
| 804 | find-> m_prev = *tail; |
| 805 | find-> m_next = 0; |
Robert Griebl | 1d4ef2a | 2002-05-28 21:32:10 +0000 | [diff] [blame] | 806 | |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 807 | if ( !*head ) |
| 808 | *head = find; |
| 809 | *tail = find; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 810 | |
Eric Andersen | 716ccb2 | 2004-01-10 11:29:31 +0000 | [diff] [blame] | 811 | if ( dt ) { |
Robert Griebl | 1d4ef2a | 2002-05-28 21:32:10 +0000 | [diff] [blame] | 812 | int i; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 813 | |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 814 | /* Add all dependable module for that new module */ |
Robert Griebl | 1d4ef2a | 2002-05-28 21:32:10 +0000 | [diff] [blame] | 815 | for ( i = 0; i < dt-> m_depcnt; i++ ) |
| 816 | check_dep ( dt-> m_deparr [i], head, tail ); |
| 817 | } |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 818 | } |
| 819 | |
Robert Griebl | 236abbf | 2002-05-22 23:34:35 +0000 | [diff] [blame] | 820 | static int mod_insert ( char *mod, int argc, char **argv ) |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 821 | { |
| 822 | struct mod_list_t *tail = 0; |
Eric Andersen | 716ccb2 | 2004-01-10 11:29:31 +0000 | [diff] [blame] | 823 | struct mod_list_t *head = 0; |
Eric Andersen | 908e362 | 2003-06-20 09:56:37 +0000 | [diff] [blame] | 824 | int rc; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 825 | |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 826 | // get dep list for module mod |
| 827 | check_dep ( mod, &head, &tail ); |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 828 | |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 829 | if ( head && tail ) { |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 830 | if( argc ) { |
Eric Andersen | 716ccb2 | 2004-01-10 11:29:31 +0000 | [diff] [blame] | 831 | int i; |
Robert Griebl | aead70b | 2002-07-26 15:54:20 +0000 | [diff] [blame] | 832 | // append module args |
Eric Andersen | 716ccb2 | 2004-01-10 11:29:31 +0000 | [diff] [blame] | 833 | for ( i = 0; i < argc; i++ ) |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 834 | head->m_options = append_option( head->m_options, argv[i] ); |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 835 | } |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 836 | |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 837 | // process tail ---> head |
Eric Andersen | 908e362 | 2003-06-20 09:56:37 +0000 | [diff] [blame] | 838 | rc = mod_process ( tail, 1 ); |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 839 | } |
| 840 | else |
| 841 | rc = 1; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 842 | |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 843 | return rc; |
| 844 | } |
| 845 | |
Eric Andersen | 908e362 | 2003-06-20 09:56:37 +0000 | [diff] [blame] | 846 | static int mod_remove ( char *mod ) |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 847 | { |
Eric Andersen | 908e362 | 2003-06-20 09:56:37 +0000 | [diff] [blame] | 848 | int rc; |
"Vladimir N. Oleynik" | 4fc9220 | 2006-02-02 14:48:54 +0000 | [diff] [blame] | 849 | static struct mod_list_t rm_a_dummy = { "-a", NULL, NULL, NULL, NULL }; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 850 | |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 851 | struct mod_list_t *head = 0; |
| 852 | struct mod_list_t *tail = 0; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 853 | |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 854 | if ( mod ) |
| 855 | check_dep ( mod, &head, &tail ); |
| 856 | else // autoclean |
| 857 | head = tail = &rm_a_dummy; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 858 | |
Robert Griebl | 236abbf | 2002-05-22 23:34:35 +0000 | [diff] [blame] | 859 | if ( head && tail ) |
Eric Andersen | 908e362 | 2003-06-20 09:56:37 +0000 | [diff] [blame] | 860 | rc = mod_process ( head, 0 ); // process head ---> tail |
| 861 | else |
| 862 | rc = 1; |
| 863 | return rc; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 864 | |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 865 | } |
| 866 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 867 | int modprobe_main(int argc, char** argv) |
Eric Andersen | 0139ca9 | 2001-07-22 23:01:03 +0000 | [diff] [blame] | 868 | { |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 869 | int rc = EXIT_SUCCESS; |
"Vladimir N. Oleynik" | 4fc9220 | 2006-02-02 14:48:54 +0000 | [diff] [blame] | 870 | char *unused; |
Eric Andersen | 0139ca9 | 2001-07-22 23:01:03 +0000 | [diff] [blame] | 871 | |
"Vladimir N. Oleynik" | 4fc9220 | 2006-02-02 14:48:54 +0000 | [diff] [blame] | 872 | bb_opt_complementally = "?V-:q-v:v-q"; |
| 873 | main_opts = bb_getopt_ulflags(argc, argv, "acdklnqrst:vVC:", |
| 874 | &unused, &unused); |
| 875 | if((main_opts & (DUMP_CONF_EXIT | LIST_ALL))) |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 876 | return EXIT_SUCCESS; |
"Vladimir N. Oleynik" | 4fc9220 | 2006-02-02 14:48:54 +0000 | [diff] [blame] | 877 | if((main_opts & (RESTRICT_DIR | CONFIG_FILE))) |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 878 | bb_error_msg_and_die("-t and -C not supported"); |
Robert Griebl | 236abbf | 2002-05-22 23:34:35 +0000 | [diff] [blame] | 879 | |
Eric Andersen | 716ccb2 | 2004-01-10 11:29:31 +0000 | [diff] [blame] | 880 | depend = build_dep ( ); |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 881 | |
Eric Andersen | 716ccb2 | 2004-01-10 11:29:31 +0000 | [diff] [blame] | 882 | if ( !depend ) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 883 | bb_error_msg_and_die ( "could not parse modules.dep\n" ); |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 884 | |
Eric Andersen | 1b06419 | 2001-07-25 07:23:38 +0000 | [diff] [blame] | 885 | if (remove_opt) { |
Eric Andersen | 0139ca9 | 2001-07-22 23:01:03 +0000 | [diff] [blame] | 886 | do { |
Eric Andersen | 908e362 | 2003-06-20 09:56:37 +0000 | [diff] [blame] | 887 | if (mod_remove ( optind < argc ? |
"Vladimir N. Oleynik" | 4fc9220 | 2006-02-02 14:48:54 +0000 | [diff] [blame] | 888 | argv [optind] : NULL )) { |
Eric Andersen | 908e362 | 2003-06-20 09:56:37 +0000 | [diff] [blame] | 889 | bb_error_msg ("failed to remove module %s", |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 890 | argv [optind] ); |
Eric Andersen | 908e362 | 2003-06-20 09:56:37 +0000 | [diff] [blame] | 891 | rc = EXIT_FAILURE; |
| 892 | } |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 893 | } while ( ++optind < argc ); |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 894 | } else { |
| 895 | if (optind >= argc) |
| 896 | bb_error_msg_and_die ( "No module or pattern provided\n" ); |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 897 | |
"Vladimir N. Oleynik" | 4fc9220 | 2006-02-02 14:48:54 +0000 | [diff] [blame] | 898 | if ( mod_insert ( argv [optind], argc - optind - 1, argv + optind + 1 )) |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 899 | bb_error_msg_and_die ( "failed to load module %s", argv [optind] ); |
Eric Andersen | 0139ca9 | 2001-07-22 23:01:03 +0000 | [diff] [blame] | 900 | } |
| 901 | |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 902 | /* 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] | 903 | |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 904 | return rc; |
Eric Andersen | 0139ca9 | 2001-07-22 23:01:03 +0000 | [diff] [blame] | 905 | } |