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