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