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 | * |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 5 | * Copyright (c) 2008 Timo Teras <timo.teras@iki.fi> |
| 6 | * Copyright (c) 2008 Vladimir Dronnikov |
Robert Griebl | 6859d76 | 2002-08-05 02:57:12 +0000 | [diff] [blame] | 7 | * |
Rob Landley | 79e1cab | 2005-11-15 00:08:29 +0000 | [diff] [blame] | 8 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 9 | */ |
Eric Andersen | 0139ca9 | 2001-07-22 23:01:03 +0000 | [diff] [blame] | 10 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 11 | #include "libbb.h" |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 12 | #include "modutils.h" |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 13 | #include <sys/utsname.h> |
Bernhard Reutner-Fischer | 2c351a8 | 2006-06-03 19:08:49 +0000 | [diff] [blame] | 14 | #include <fnmatch.h> |
Eric Andersen | 0139ca9 | 2001-07-22 23:01:03 +0000 | [diff] [blame] | 15 | |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame^] | 16 | #define MODULE_FLAG_LOADED 0x0001 |
| 17 | #define MODULE_FLAG_NEED_DEPS 0x0002 |
| 18 | //Misnomer? Seems to mean "was seen in modules.dep": |
| 19 | #define MODULE_FLAG_EXISTS 0x0004 |
| 20 | #define MODULE_FLAG_BLACKLISTED 0x0008 |
| 21 | |
| 22 | struct module_entry { /* I'll call it ME. */ |
| 23 | unsigned flags; |
| 24 | char *modname; /* stripped of /path/, .ext and s/-/_/g */ |
| 25 | const char *probed_name; /* verbatim as seen on cmdline */ |
| 26 | llist_t *aliases; /* strings. aliases from config files */ |
| 27 | llist_t *options; /* strings. options from config files */ |
| 28 | llist_t *deps; /* strings. modules we depend on */ |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 29 | }; |
| 30 | |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame^] | 31 | #define MODPROBE_OPTS "acdlnrt:VC:" USE_FEATURE_MODPROBE_BLACKLIST("b") |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 32 | enum { |
| 33 | MODPROBE_OPT_INSERT_ALL = (INSMOD_OPT_UNUSED << 0), /* a */ |
| 34 | MODPROBE_OPT_DUMP_ONLY = (INSMOD_OPT_UNUSED << 1), /* c */ |
| 35 | MODPROBE_OPT_D = (INSMOD_OPT_UNUSED << 2), /* d */ |
| 36 | MODPROBE_OPT_LIST_ONLY = (INSMOD_OPT_UNUSED << 3), /* l */ |
| 37 | MODPROBE_OPT_SHOW_ONLY = (INSMOD_OPT_UNUSED << 4), /* n */ |
| 38 | MODPROBE_OPT_REMOVE = (INSMOD_OPT_UNUSED << 5), /* r */ |
| 39 | MODPROBE_OPT_RESTRICT = (INSMOD_OPT_UNUSED << 6), /* t */ |
| 40 | MODPROBE_OPT_VERONLY = (INSMOD_OPT_UNUSED << 7), /* V */ |
| 41 | MODPROBE_OPT_CONFIGFILE = (INSMOD_OPT_UNUSED << 8), /* C */ |
| 42 | MODPROBE_OPT_BLACKLIST = (INSMOD_OPT_UNUSED << 9) * ENABLE_FEATURE_MODPROBE_BLACKLIST, |
| 43 | }; |
| 44 | |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame^] | 45 | struct globals { |
| 46 | llist_t *db; /* MEs of all modules ever seen (caching for speed) */ |
| 47 | llist_t *probes; /* MEs of module(s) requested on cmdline */ |
| 48 | llist_t *cmdline_mopts; /* strings. module options (from cmdline) */ |
| 49 | int num_deps; /* what is this? */ |
| 50 | /* bool. "Did we have 'symbol:FOO' requested on cmdline?" */ |
| 51 | smallint need_symbols; |
| 52 | }; |
| 53 | #define G (*(struct globals*)&bb_common_bufsiz1) |
| 54 | #define INIT_G() do { } while (0) |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 55 | |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 56 | |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame^] | 57 | static int read_config(const char *path); |
| 58 | |
| 59 | static struct module_entry *helper_get_module(const char *module, int create) |
Denis Vlasenko | f45c4f4 | 2008-06-16 04:09:25 +0000 | [diff] [blame] | 60 | { |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame^] | 61 | char modname[MODULE_NAME_LEN]; |
| 62 | struct module_entry *e; |
| 63 | llist_t *l; |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 64 | |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame^] | 65 | filename2modname(module, modname); |
| 66 | for (l = G.db; l != NULL; l = l->link) { |
| 67 | e = (struct module_entry *) l->data; |
| 68 | if (strcmp(e->modname, modname) == 0) |
| 69 | return e; |
| 70 | } |
| 71 | if (!create) |
| 72 | return NULL; |
| 73 | |
| 74 | e = xzalloc(sizeof(*e)); |
| 75 | e->modname = xstrdup(modname); |
| 76 | llist_add_to(&G.db, e); |
| 77 | |
| 78 | return e; |
| 79 | } |
| 80 | static struct module_entry *get_or_add_modentry(const char *module) |
| 81 | { |
| 82 | return helper_get_module(module, 1); |
| 83 | } |
| 84 | static struct module_entry *get_modentry(const char *module) |
| 85 | { |
| 86 | return helper_get_module(module, 0); |
| 87 | } |
| 88 | |
| 89 | static void add_probe(const char *name) |
| 90 | { |
| 91 | struct module_entry *m; |
| 92 | |
| 93 | m = get_or_add_modentry(name); |
| 94 | m->probed_name = name; |
| 95 | m->flags |= MODULE_FLAG_NEED_DEPS; |
| 96 | llist_add_to(&G.probes, m); |
| 97 | |
| 98 | G.num_deps++; |
| 99 | if (ENABLE_FEATURE_MODUTILS_SYMBOLS |
| 100 | && strncmp(m->modname, "symbol:", 7) == 0) |
| 101 | G.need_symbols = 1; |
Denis Vlasenko | 9ddc8d5 | 2008-05-18 14:39:43 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 104 | static int FAST_FUNC config_file_action(const char *filename, |
| 105 | struct stat *statbuf UNUSED_PARAM, |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame^] | 106 | void *userdata UNUSED_PARAM, |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 107 | int depth UNUSED_PARAM) |
Rob Landley | 7261575 | 2006-04-10 16:09:52 +0000 | [diff] [blame] | 108 | { |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 109 | RESERVE_CONFIG_BUFFER(modname, MODULE_NAME_LEN); |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame^] | 110 | char *tokens[3], *rmod; |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 111 | parser_t *p; |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame^] | 112 | llist_t *l; |
| 113 | struct module_entry *m; |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 114 | int rc = TRUE; |
Denis Vlasenko | e1ee48e | 2008-07-29 00:19:44 +0000 | [diff] [blame] | 115 | |
| 116 | if (bb_basename(filename)[0] == '.') |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 117 | goto error; |
Denis Vlasenko | e1ee48e | 2008-07-29 00:19:44 +0000 | [diff] [blame] | 118 | |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 119 | p = config_open2(filename, fopen_for_read); |
| 120 | if (p == NULL) { |
| 121 | rc = FALSE; |
| 122 | goto error; |
| 123 | } |
Rob Landley | 7261575 | 2006-04-10 16:09:52 +0000 | [diff] [blame] | 124 | |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 125 | while (config_read(p, tokens, 3, 2, "# \t", PARSE_NORMAL)) { |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame^] | 126 | //Use index_in_strings? |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 127 | if (strcmp(tokens[0], "alias") == 0) { |
| 128 | filename2modname(tokens[1], modname); |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame^] | 129 | if (tokens[2] == NULL) |
| 130 | continue; |
| 131 | |
| 132 | for (l = G.probes; l != NULL; l = l->link) { |
| 133 | m = (struct module_entry *) l->data; |
| 134 | if (fnmatch(modname, m->modname, 0) != 0) |
| 135 | continue; |
| 136 | rmod = filename2modname(tokens[2], NULL); |
| 137 | llist_add_to(&m->aliases, rmod); |
| 138 | |
| 139 | if (m->flags & MODULE_FLAG_NEED_DEPS) { |
| 140 | m->flags &= ~MODULE_FLAG_NEED_DEPS; |
| 141 | G.num_deps--; |
| 142 | } |
| 143 | |
| 144 | m = get_or_add_modentry(rmod); |
| 145 | m->flags |= MODULE_FLAG_NEED_DEPS; |
| 146 | G.num_deps++; |
| 147 | } |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 148 | } else if (strcmp(tokens[0], "options") == 0) { |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame^] | 149 | if (tokens[2] == NULL) |
| 150 | continue; |
| 151 | m = get_or_add_modentry(tokens[1]); |
| 152 | llist_add_to(&m->options, xstrdup(tokens[2])); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 153 | } else if (strcmp(tokens[0], "include") == 0) { |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame^] | 154 | read_config(tokens[1]); |
| 155 | } else if (ENABLE_FEATURE_MODPROBE_BLACKLIST |
| 156 | && strcmp(tokens[0], "blacklist") == 0 |
| 157 | ) { |
| 158 | get_or_add_modentry(tokens[1])->flags |= MODULE_FLAG_BLACKLISTED; |
Rob Landley | 7261575 | 2006-04-10 16:09:52 +0000 | [diff] [blame] | 159 | } |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 160 | } |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 161 | config_close(p); |
| 162 | error: |
Denis Vlasenko | 9604e1b | 2009-03-03 18:47:56 +0000 | [diff] [blame] | 163 | RELEASE_CONFIG_BUFFER(modname); |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 164 | return rc; |
| 165 | } |
| 166 | |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame^] | 167 | static int read_config(const char *path) |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 168 | { |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 169 | return recursive_action(path, ACTION_RECURSE | ACTION_QUIET, |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame^] | 170 | config_file_action, NULL, NULL, 1); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 171 | } |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 172 | |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame^] | 173 | static char *gather_options(char *opts, llist_t *o) |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 174 | { |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame^] | 175 | int optlen; |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 176 | |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame^] | 177 | if (opts == NULL) |
| 178 | opts = xstrdup(""); |
| 179 | optlen = strlen(opts); |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 180 | |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame^] | 181 | for (; o != NULL; o = o->link) { |
| 182 | opts = xrealloc(opts, optlen + strlen(o->data) + 2); |
| 183 | optlen += sprintf(opts + optlen, "%s ", o->data); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 184 | } |
| 185 | return opts; |
| 186 | } |
| 187 | |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame^] | 188 | static int do_modprobe(struct module_entry *m) |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 189 | { |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame^] | 190 | struct module_entry *m2; |
| 191 | char *fn, *options; |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 192 | int rc = -1; |
| 193 | |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame^] | 194 | if (!(m->flags & MODULE_FLAG_EXISTS)) |
| 195 | return -ENOENT; |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 196 | |
| 197 | if (!(option_mask32 & MODPROBE_OPT_REMOVE)) |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame^] | 198 | m->deps = llist_rev(m->deps); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 199 | |
| 200 | rc = 0; |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame^] | 201 | while (m->deps && rc == 0) { |
| 202 | fn = llist_pop(&m->deps); |
| 203 | m2 = get_or_add_modentry(fn); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 204 | if (option_mask32 & MODPROBE_OPT_REMOVE) { |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame^] | 205 | if (bb_delete_module(m->modname, O_EXCL) != 0) |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 206 | rc = errno; |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame^] | 207 | } else if (!(m2->flags & MODULE_FLAG_LOADED)) { |
| 208 | options = gather_options(NULL, m2->options); |
| 209 | if (m == m2) |
| 210 | options = gather_options(options, G.cmdline_mopts); |
| 211 | //TODO: looks like G.cmdline_mopts can contain either NULL or just one string, not more? |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 212 | rc = bb_init_module(fn, options); |
| 213 | if (rc == 0) |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame^] | 214 | m2->flags |= MODULE_FLAG_LOADED; |
Denis Vlasenko | 9604e1b | 2009-03-03 18:47:56 +0000 | [diff] [blame] | 215 | free(options); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Denis Vlasenko | 9604e1b | 2009-03-03 18:47:56 +0000 | [diff] [blame] | 218 | free(fn); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 219 | } |
| 220 | |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame^] | 221 | if (rc > 0 && !(option_mask32 & INSMOD_OPT_SILENT)) { |
Denis Vlasenko | 54d1005 | 2008-12-24 03:11:43 +0000 | [diff] [blame] | 222 | bb_error_msg("failed to %sload module %s: %s", |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame^] | 223 | (option_mask32 & MODPROBE_OPT_REMOVE) ? "un" : "", |
| 224 | m->probed_name ? m->probed_name : m->modname, |
| 225 | moderror(rc) |
| 226 | ); |
| 227 | } |
Denis Vlasenko | 9604e1b | 2009-03-03 18:47:56 +0000 | [diff] [blame] | 228 | |
Eric Andersen | 908e362 | 2003-06-20 09:56:37 +0000 | [diff] [blame] | 229 | return rc; |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame^] | 232 | static void load_modules_dep(void) |
| 233 | { |
| 234 | struct module_entry *m; |
| 235 | char *colon, *tokens[2]; |
| 236 | parser_t *p; |
| 237 | |
| 238 | p = config_open2(CONFIG_DEFAULT_DEPMOD_FILE, xfopen_for_read); |
| 239 | //Still true? |
| 240 | /* Modprobe does not work at all without modprobe.dep, |
| 241 | * even if the full module name is given. Returning error here |
| 242 | * was making us later confuse user with this message: |
| 243 | * "module /full/path/to/existing/file/module.ko not found". |
| 244 | * It's better to die immediately, with good message: */ |
| 245 | // if (p == NULL) |
| 246 | // bb_perror_msg_and_die("can't open '%s'", CONFIG_DEFAULT_DEPMOD_FILE); |
| 247 | |
| 248 | while (G.num_deps |
| 249 | && config_read(p, tokens, 2, 1, "# \t", PARSE_NORMAL) |
| 250 | ) { |
| 251 | colon = last_char_is(tokens[0], ':'); |
| 252 | if (colon == NULL) |
| 253 | continue; |
| 254 | *colon = 0; |
| 255 | |
| 256 | m = get_modentry(tokens[0]); |
| 257 | if (m == NULL) |
| 258 | continue; |
| 259 | //Can we skip it too if it is MODULE_FLAG_LOADED? |
| 260 | |
| 261 | m->flags |= MODULE_FLAG_EXISTS; |
| 262 | if ((m->flags & MODULE_FLAG_NEED_DEPS) && (m->deps == NULL)) { |
| 263 | G.num_deps--; |
| 264 | llist_add_to(&m->deps, xstrdup(tokens[0])); |
| 265 | if (tokens[1]) |
| 266 | string_to_llist(tokens[1], &m->deps, " "); |
| 267 | } |
| 268 | } |
| 269 | config_close(p); |
| 270 | } |
| 271 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 272 | int modprobe_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | 9ddc004 | 2008-08-06 00:51:43 +0000 | [diff] [blame] | 273 | int modprobe_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | 0139ca9 | 2001-07-22 23:01:03 +0000 | [diff] [blame] | 274 | { |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 275 | struct utsname uts; |
Denis Vlasenko | bb26db4 | 2008-10-31 02:04:28 +0000 | [diff] [blame] | 276 | int rc; |
| 277 | unsigned opt; |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame^] | 278 | struct module_entry *me; |
Eric Andersen | 0139ca9 | 2001-07-22 23:01:03 +0000 | [diff] [blame] | 279 | |
Denis Vlasenko | e7fca51 | 2007-11-10 01:32:18 +0000 | [diff] [blame] | 280 | opt_complementary = "q-v:v-q"; |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame^] | 281 | opt = getopt32(argv, INSMOD_OPTS MODPROBE_OPTS INSMOD_ARGS, NULL, NULL); |
Denis Vlasenko | 9ddc004 | 2008-08-06 00:51:43 +0000 | [diff] [blame] | 282 | argv += optind; |
Denis Vlasenko | b68979a | 2007-11-02 23:31:10 +0000 | [diff] [blame] | 283 | |
Denis Vlasenko | bb26db4 | 2008-10-31 02:04:28 +0000 | [diff] [blame] | 284 | if (opt & (MODPROBE_OPT_DUMP_ONLY | MODPROBE_OPT_LIST_ONLY | |
| 285 | MODPROBE_OPT_SHOW_ONLY)) |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 286 | bb_error_msg_and_die("not supported"); |
Robert Griebl | 236abbf | 2002-05-22 23:34:35 +0000 | [diff] [blame] | 287 | |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame^] | 288 | /* Goto modules location */ |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 289 | xchdir(CONFIG_DEFAULT_MODULES_DIR); |
| 290 | uname(&uts); |
| 291 | xchdir(uts.release); |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 292 | |
Denis Vlasenko | bb26db4 | 2008-10-31 02:04:28 +0000 | [diff] [blame] | 293 | if (!argv[0]) { |
| 294 | if (opt & MODPROBE_OPT_REMOVE) { |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame^] | 295 | /* TODO: comment here */ |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 296 | if (bb_delete_module(NULL, O_NONBLOCK|O_EXCL) != 0) |
| 297 | bb_perror_msg_and_die("rmmod"); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 298 | } |
Denis Vlasenko | bb26db4 | 2008-10-31 02:04:28 +0000 | [diff] [blame] | 299 | return EXIT_SUCCESS; |
| 300 | } |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame^] | 301 | |
| 302 | if (opt & MODPROBE_OPT_INSERT_ALL) { |
| 303 | /* Each argument is a module name */ |
| 304 | do { |
| 305 | add_probe(*argv++); |
| 306 | } while (*argv); |
| 307 | G.probes = llist_rev(G.probes); |
| 308 | } else { |
| 309 | /* First argument is module name, rest are parameters */ |
| 310 | add_probe(argv[0]); |
| 311 | //TODO: looks like G.cmdline_mopts can contain either NULL or just one string, not more? |
| 312 | //optimize it them |
| 313 | llist_add_to(&G.cmdline_mopts, parse_cmdline_module_options(argv)); |
Eric Andersen | 0139ca9 | 2001-07-22 23:01:03 +0000 | [diff] [blame] | 314 | } |
| 315 | |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame^] | 316 | /* Retrieve module names of already loaded modules */ |
Denis Vlasenko | bb26db4 | 2008-10-31 02:04:28 +0000 | [diff] [blame] | 317 | { |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 318 | char *s; |
Denis Vlasenko | bb26db4 | 2008-10-31 02:04:28 +0000 | [diff] [blame] | 319 | parser_t *parser = config_open2("/proc/modules", fopen_for_read); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 320 | while (config_read(parser, &s, 1, 1, "# \t", PARSE_NORMAL & ~PARSE_GREEDY)) |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame^] | 321 | get_or_add_modentry(s)->flags |= MODULE_FLAG_LOADED; |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 322 | config_close(parser); |
| 323 | } |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 324 | |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame^] | 325 | read_config("/etc/modprobe.conf"); |
| 326 | read_config("/etc/modprobe.d"); |
| 327 | if (ENABLE_FEATURE_MODUTILS_SYMBOLS && G.need_symbols) |
| 328 | read_config("modules.symbols"); |
| 329 | load_modules_dep(); |
| 330 | if (ENABLE_FEATURE_MODUTILS_ALIAS && G.num_deps) { |
| 331 | read_config("modules.alias"); |
| 332 | load_modules_dep(); |
| 333 | } |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 334 | |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame^] | 335 | while ((me = llist_pop(&G.probes)) != NULL) { |
| 336 | if (me->aliases == NULL) { |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 337 | /* Try if module by literal name is found; literal |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame^] | 338 | * names are blacklisted only if '-b' is given. */ |
| 339 | if (!(opt & MODPROBE_OPT_BLACKLIST) |
| 340 | || !(me->flags & MODULE_FLAG_BLACKLISTED) |
| 341 | ) { |
| 342 | rc = do_modprobe(me); |
Denis Vlasenko | bb26db4 | 2008-10-31 02:04:28 +0000 | [diff] [blame] | 343 | if (rc < 0 && !(opt & INSMOD_OPT_SILENT)) |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame^] | 344 | bb_error_msg("module %s not found", |
| 345 | me->probed_name); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 346 | } |
| 347 | } else { |
| 348 | /* Probe all aliases */ |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame^] | 349 | do { |
| 350 | char *realname = llist_pop(&me->aliases); |
| 351 | struct module_entry *m2; |
| 352 | |
| 353 | m2 = get_or_add_modentry(realname); |
| 354 | if (!(m2->flags & MODULE_FLAG_BLACKLISTED)) |
| 355 | do_modprobe(m2); |
Denis Vlasenko | 9604e1b | 2009-03-03 18:47:56 +0000 | [diff] [blame] | 356 | free(realname); |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame^] | 357 | } while (me->aliases != NULL); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 358 | } |
| 359 | } |
| 360 | |
| 361 | return EXIT_SUCCESS; |
Eric Andersen | 0139ca9 | 2001-07-22 23:01:03 +0000 | [diff] [blame] | 362 | } |