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