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