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 | |
Mike Frysinger | 1e12ae9 | 2009-05-06 05:24:41 -0400 | [diff] [blame] | 11 | /* Note that unlike older versions of modules.dep/depmod (busybox and m-i-t), |
| 12 | * we expect the full dependency list to be specified in modules.dep. Older |
| 13 | * versions would only export the direct dependency list. |
| 14 | */ |
| 15 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 16 | #include "libbb.h" |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 17 | #include "modutils.h" |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 18 | #include <sys/utsname.h> |
Bernhard Reutner-Fischer | 2c351a8 | 2006-06-03 19:08:49 +0000 | [diff] [blame] | 19 | #include <fnmatch.h> |
Eric Andersen | 0139ca9 | 2001-07-22 23:01:03 +0000 | [diff] [blame] | 20 | |
Mike Frysinger | 00ffaea | 2009-05-05 20:13:45 -0400 | [diff] [blame] | 21 | //#define DBG(fmt, ...) bb_error_msg("%s: " fmt, __func__, ## __VA_ARGS__) |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 22 | #define DBG(...) ((void)0) |
| 23 | |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 24 | #define MODULE_FLAG_LOADED 0x0001 |
| 25 | #define MODULE_FLAG_NEED_DEPS 0x0002 |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 26 | /* "was seen in modules.dep": */ |
| 27 | #define MODULE_FLAG_FOUND_IN_MODDEP 0x0004 |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 28 | #define MODULE_FLAG_BLACKLISTED 0x0008 |
| 29 | |
| 30 | struct module_entry { /* I'll call it ME. */ |
| 31 | unsigned flags; |
| 32 | char *modname; /* stripped of /path/, .ext and s/-/_/g */ |
| 33 | const char *probed_name; /* verbatim as seen on cmdline */ |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 34 | char *options; /* options from config files */ |
| 35 | llist_t *realnames; /* strings. if this module is an alias, */ |
| 36 | /* real module name is one of these. */ |
| 37 | //Can there really be more than one? Example from real kernel? |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 38 | llist_t *deps; /* strings. modules we depend on */ |
Eric Andersen | 60e56f5 | 2002-04-26 06:04:01 +0000 | [diff] [blame] | 39 | }; |
| 40 | |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 41 | #define MODPROBE_OPTS "acdlnrt:VC:" IF_FEATURE_MODPROBE_BLACKLIST("b") |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 42 | enum { |
| 43 | MODPROBE_OPT_INSERT_ALL = (INSMOD_OPT_UNUSED << 0), /* a */ |
| 44 | MODPROBE_OPT_DUMP_ONLY = (INSMOD_OPT_UNUSED << 1), /* c */ |
| 45 | MODPROBE_OPT_D = (INSMOD_OPT_UNUSED << 2), /* d */ |
| 46 | MODPROBE_OPT_LIST_ONLY = (INSMOD_OPT_UNUSED << 3), /* l */ |
| 47 | MODPROBE_OPT_SHOW_ONLY = (INSMOD_OPT_UNUSED << 4), /* n */ |
| 48 | MODPROBE_OPT_REMOVE = (INSMOD_OPT_UNUSED << 5), /* r */ |
| 49 | MODPROBE_OPT_RESTRICT = (INSMOD_OPT_UNUSED << 6), /* t */ |
| 50 | MODPROBE_OPT_VERONLY = (INSMOD_OPT_UNUSED << 7), /* V */ |
| 51 | MODPROBE_OPT_CONFIGFILE = (INSMOD_OPT_UNUSED << 8), /* C */ |
| 52 | MODPROBE_OPT_BLACKLIST = (INSMOD_OPT_UNUSED << 9) * ENABLE_FEATURE_MODPROBE_BLACKLIST, |
| 53 | }; |
| 54 | |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 55 | struct globals { |
| 56 | llist_t *db; /* MEs of all modules ever seen (caching for speed) */ |
| 57 | llist_t *probes; /* MEs of module(s) requested on cmdline */ |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 58 | char *cmdline_mopts; /* module options from cmdline */ |
| 59 | int num_unresolved_deps; |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 60 | /* bool. "Did we have 'symbol:FOO' requested on cmdline?" */ |
| 61 | smallint need_symbols; |
| 62 | }; |
| 63 | #define G (*(struct globals*)&bb_common_bufsiz1) |
| 64 | #define INIT_G() do { } while (0) |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 65 | |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 66 | |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 67 | static int read_config(const char *path); |
| 68 | |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 69 | static char *gather_options_str(char *opts, const char *append) |
| 70 | { |
| 71 | /* Speed-optimized. We call gather_options_str many times. */ |
| 72 | if (opts == NULL) { |
| 73 | opts = xstrdup(append); |
| 74 | } else { |
| 75 | int optlen = strlen(opts); |
| 76 | opts = xrealloc(opts, optlen + strlen(append) + 2); |
| 77 | sprintf(opts + optlen, " %s", append); |
| 78 | } |
| 79 | return opts; |
| 80 | } |
| 81 | |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 82 | static struct module_entry *helper_get_module(const char *module, int create) |
Denis Vlasenko | f45c4f4 | 2008-06-16 04:09:25 +0000 | [diff] [blame] | 83 | { |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 84 | char modname[MODULE_NAME_LEN]; |
| 85 | struct module_entry *e; |
| 86 | llist_t *l; |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 87 | |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 88 | filename2modname(module, modname); |
| 89 | for (l = G.db; l != NULL; l = l->link) { |
| 90 | e = (struct module_entry *) l->data; |
| 91 | if (strcmp(e->modname, modname) == 0) |
| 92 | return e; |
| 93 | } |
| 94 | if (!create) |
| 95 | return NULL; |
| 96 | |
| 97 | e = xzalloc(sizeof(*e)); |
| 98 | e->modname = xstrdup(modname); |
| 99 | llist_add_to(&G.db, e); |
| 100 | |
| 101 | return e; |
| 102 | } |
| 103 | static struct module_entry *get_or_add_modentry(const char *module) |
| 104 | { |
| 105 | return helper_get_module(module, 1); |
| 106 | } |
| 107 | static struct module_entry *get_modentry(const char *module) |
| 108 | { |
| 109 | return helper_get_module(module, 0); |
| 110 | } |
| 111 | |
| 112 | static void add_probe(const char *name) |
| 113 | { |
| 114 | struct module_entry *m; |
| 115 | |
| 116 | m = get_or_add_modentry(name); |
Denys Vlasenko | 140def8 | 2009-05-26 12:48:34 +0200 | [diff] [blame] | 117 | if (!(option_mask32 & MODPROBE_OPT_REMOVE) |
| 118 | && (m->flags & MODULE_FLAG_LOADED) |
| 119 | ) { |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 120 | DBG("skipping %s, it is already loaded", name); |
| 121 | return; |
| 122 | } |
| 123 | |
Mike Frysinger | 00ffaea | 2009-05-05 20:13:45 -0400 | [diff] [blame] | 124 | DBG("queuing %s", name); |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 125 | m->probed_name = name; |
| 126 | m->flags |= MODULE_FLAG_NEED_DEPS; |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 127 | llist_add_to_end(&G.probes, m); |
| 128 | G.num_unresolved_deps++; |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 129 | if (ENABLE_FEATURE_MODUTILS_SYMBOLS |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 130 | && strncmp(m->modname, "symbol:", 7) == 0 |
| 131 | ) { |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 132 | G.need_symbols = 1; |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 133 | } |
Denis Vlasenko | 9ddc8d5 | 2008-05-18 14:39:43 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 136 | static int FAST_FUNC config_file_action(const char *filename, |
| 137 | struct stat *statbuf UNUSED_PARAM, |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 138 | void *userdata UNUSED_PARAM, |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 139 | int depth UNUSED_PARAM) |
Rob Landley | 7261575 | 2006-04-10 16:09:52 +0000 | [diff] [blame] | 140 | { |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 141 | char *tokens[3]; |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 142 | parser_t *p; |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 143 | struct module_entry *m; |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 144 | int rc = TRUE; |
Denis Vlasenko | e1ee48e | 2008-07-29 00:19:44 +0000 | [diff] [blame] | 145 | |
| 146 | if (bb_basename(filename)[0] == '.') |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 147 | goto error; |
Denis Vlasenko | e1ee48e | 2008-07-29 00:19:44 +0000 | [diff] [blame] | 148 | |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 149 | p = config_open2(filename, fopen_for_read); |
| 150 | if (p == NULL) { |
| 151 | rc = FALSE; |
| 152 | goto error; |
| 153 | } |
Rob Landley | 7261575 | 2006-04-10 16:09:52 +0000 | [diff] [blame] | 154 | |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 155 | while (config_read(p, tokens, 3, 2, "# \t", PARSE_NORMAL)) { |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 156 | //Use index_in_strings? |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 157 | if (strcmp(tokens[0], "alias") == 0) { |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 158 | /* alias <wildcard> <modulename> */ |
| 159 | llist_t *l; |
| 160 | char wildcard[MODULE_NAME_LEN]; |
| 161 | char *rmod; |
| 162 | |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 163 | if (tokens[2] == NULL) |
| 164 | continue; |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 165 | filename2modname(tokens[1], wildcard); |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 166 | |
| 167 | for (l = G.probes; l != NULL; l = l->link) { |
| 168 | m = (struct module_entry *) l->data; |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 169 | if (fnmatch(wildcard, m->modname, 0) != 0) |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 170 | continue; |
| 171 | rmod = filename2modname(tokens[2], NULL); |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 172 | llist_add_to(&m->realnames, rmod); |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 173 | |
| 174 | if (m->flags & MODULE_FLAG_NEED_DEPS) { |
| 175 | m->flags &= ~MODULE_FLAG_NEED_DEPS; |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 176 | G.num_unresolved_deps--; |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | m = get_or_add_modentry(rmod); |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 180 | if (!(m->flags & MODULE_FLAG_NEED_DEPS)) { |
| 181 | m->flags |= MODULE_FLAG_NEED_DEPS; |
| 182 | G.num_unresolved_deps++; |
| 183 | } |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 184 | } |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 185 | } else if (strcmp(tokens[0], "options") == 0) { |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 186 | /* options <modulename> <option...> */ |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 187 | if (tokens[2] == NULL) |
| 188 | continue; |
| 189 | m = get_or_add_modentry(tokens[1]); |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 190 | m->options = gather_options_str(m->options, tokens[2]); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 191 | } else if (strcmp(tokens[0], "include") == 0) { |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 192 | /* include <filename> */ |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 193 | read_config(tokens[1]); |
| 194 | } else if (ENABLE_FEATURE_MODPROBE_BLACKLIST |
| 195 | && strcmp(tokens[0], "blacklist") == 0 |
| 196 | ) { |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 197 | /* blacklist <modulename> */ |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 198 | get_or_add_modentry(tokens[1])->flags |= MODULE_FLAG_BLACKLISTED; |
Rob Landley | 7261575 | 2006-04-10 16:09:52 +0000 | [diff] [blame] | 199 | } |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 200 | } |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 201 | config_close(p); |
| 202 | error: |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 203 | return rc; |
| 204 | } |
| 205 | |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 206 | static int read_config(const char *path) |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 207 | { |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 208 | return recursive_action(path, ACTION_RECURSE | ACTION_QUIET, |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 209 | config_file_action, NULL, NULL, 1); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 210 | } |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 211 | |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 212 | static int do_modprobe(struct module_entry *m) |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 213 | { |
Denys Vlasenko | f3cbfc0 | 2009-05-28 03:54:29 +0200 | [diff] [blame] | 214 | struct module_entry *m2 = m2; /* for compiler */ |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 215 | char *fn, *options; |
Denys Vlasenko | f3cbfc0 | 2009-05-28 03:54:29 +0200 | [diff] [blame] | 216 | int rc, first; |
Mike Frysinger | 00ffaea | 2009-05-05 20:13:45 -0400 | [diff] [blame] | 217 | llist_t *l; |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 218 | |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 219 | if (!(m->flags & MODULE_FLAG_FOUND_IN_MODDEP)) { |
| 220 | DBG("skipping %s, not found in modules.dep", m->modname); |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 221 | return -ENOENT; |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 222 | } |
| 223 | DBG("do_modprob'ing %s", m->modname); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 224 | |
| 225 | if (!(option_mask32 & MODPROBE_OPT_REMOVE)) |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 226 | m->deps = llist_rev(m->deps); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 227 | |
Mike Frysinger | 00ffaea | 2009-05-05 20:13:45 -0400 | [diff] [blame] | 228 | for (l = m->deps; l != NULL; l = l->link) |
| 229 | DBG("dep: %s", l->data); |
| 230 | |
Denys Vlasenko | f3cbfc0 | 2009-05-28 03:54:29 +0200 | [diff] [blame] | 231 | first = 1; |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 232 | rc = 0; |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 233 | while (m->deps && rc == 0) { |
| 234 | fn = llist_pop(&m->deps); |
| 235 | m2 = get_or_add_modentry(fn); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 236 | if (option_mask32 & MODPROBE_OPT_REMOVE) { |
Denys Vlasenko | f3cbfc0 | 2009-05-28 03:54:29 +0200 | [diff] [blame] | 237 | if (m2->flags & MODULE_FLAG_LOADED) { |
| 238 | if (bb_delete_module(m2->modname, O_EXCL) != 0) { |
| 239 | if (first) |
| 240 | rc = errno; |
| 241 | } else { |
| 242 | m2->flags &= ~MODULE_FLAG_LOADED; |
| 243 | } |
| 244 | } |
| 245 | /* do not error out if *deps* fail to unload */ |
| 246 | first = 0; |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 247 | } else if (!(m2->flags & MODULE_FLAG_LOADED)) { |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 248 | options = m2->options; |
| 249 | m2->options = NULL; |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 250 | if (m == m2) |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 251 | options = gather_options_str(options, G.cmdline_mopts); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 252 | rc = bb_init_module(fn, options); |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 253 | DBG("loaded %s '%s', rc:%d", fn, options, rc); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 254 | if (rc == 0) |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 255 | m2->flags |= MODULE_FLAG_LOADED; |
Denis Vlasenko | 9604e1b | 2009-03-03 18:47:56 +0000 | [diff] [blame] | 256 | free(options); |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 257 | } else { |
| 258 | DBG("%s is already loaded, skipping", fn); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 259 | } |
| 260 | |
Denis Vlasenko | 9604e1b | 2009-03-03 18:47:56 +0000 | [diff] [blame] | 261 | free(fn); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Denys Vlasenko | f3cbfc0 | 2009-05-28 03:54:29 +0200 | [diff] [blame] | 264 | if (rc && !(option_mask32 & INSMOD_OPT_SILENT)) { |
Denis Vlasenko | 54d1005 | 2008-12-24 03:11:43 +0000 | [diff] [blame] | 265 | bb_error_msg("failed to %sload module %s: %s", |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 266 | (option_mask32 & MODPROBE_OPT_REMOVE) ? "un" : "", |
Denys Vlasenko | f3cbfc0 | 2009-05-28 03:54:29 +0200 | [diff] [blame] | 267 | m2->probed_name ? m2->probed_name : m2->modname, |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 268 | moderror(rc) |
| 269 | ); |
| 270 | } |
Denis Vlasenko | 9604e1b | 2009-03-03 18:47:56 +0000 | [diff] [blame] | 271 | |
Eric Andersen | 908e362 | 2003-06-20 09:56:37 +0000 | [diff] [blame] | 272 | return rc; |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 273 | } |
| 274 | |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 275 | static void load_modules_dep(void) |
| 276 | { |
| 277 | struct module_entry *m; |
| 278 | char *colon, *tokens[2]; |
| 279 | parser_t *p; |
| 280 | |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 281 | /* Modprobe does not work at all without modprobe.dep, |
| 282 | * even if the full module name is given. Returning error here |
| 283 | * was making us later confuse user with this message: |
| 284 | * "module /full/path/to/existing/file/module.ko not found". |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 285 | * It's better to die immediately, with good message. |
| 286 | * xfopen_for_read provides that. */ |
| 287 | p = config_open2(CONFIG_DEFAULT_DEPMOD_FILE, xfopen_for_read); |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 288 | |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 289 | while (G.num_unresolved_deps |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 290 | && config_read(p, tokens, 2, 1, "# \t", PARSE_NORMAL) |
| 291 | ) { |
| 292 | colon = last_char_is(tokens[0], ':'); |
| 293 | if (colon == NULL) |
| 294 | continue; |
| 295 | *colon = 0; |
| 296 | |
| 297 | m = get_modentry(tokens[0]); |
| 298 | if (m == NULL) |
| 299 | continue; |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 300 | |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 301 | /* Optimization... */ |
| 302 | if ((m->flags & MODULE_FLAG_LOADED) |
| 303 | && !(option_mask32 & MODPROBE_OPT_REMOVE) |
| 304 | ) { |
| 305 | DBG("skip deps of %s, it's already loaded", tokens[0]); |
| 306 | continue; |
| 307 | } |
| 308 | |
| 309 | m->flags |= MODULE_FLAG_FOUND_IN_MODDEP; |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 310 | if ((m->flags & MODULE_FLAG_NEED_DEPS) && (m->deps == NULL)) { |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 311 | G.num_unresolved_deps--; |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 312 | llist_add_to(&m->deps, xstrdup(tokens[0])); |
| 313 | if (tokens[1]) |
| 314 | string_to_llist(tokens[1], &m->deps, " "); |
Mike Frysinger | 00ffaea | 2009-05-05 20:13:45 -0400 | [diff] [blame] | 315 | } else |
| 316 | DBG("skipping dep line"); |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 317 | } |
| 318 | config_close(p); |
| 319 | } |
| 320 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 321 | int modprobe_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | 9ddc004 | 2008-08-06 00:51:43 +0000 | [diff] [blame] | 322 | int modprobe_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | 0139ca9 | 2001-07-22 23:01:03 +0000 | [diff] [blame] | 323 | { |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 324 | struct utsname uts; |
Denis Vlasenko | bb26db4 | 2008-10-31 02:04:28 +0000 | [diff] [blame] | 325 | int rc; |
| 326 | unsigned opt; |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 327 | struct module_entry *me; |
Eric Andersen | 0139ca9 | 2001-07-22 23:01:03 +0000 | [diff] [blame] | 328 | |
Denis Vlasenko | e7fca51 | 2007-11-10 01:32:18 +0000 | [diff] [blame] | 329 | opt_complementary = "q-v:v-q"; |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 330 | opt = getopt32(argv, INSMOD_OPTS MODPROBE_OPTS INSMOD_ARGS, NULL, NULL); |
Denis Vlasenko | 9ddc004 | 2008-08-06 00:51:43 +0000 | [diff] [blame] | 331 | argv += optind; |
Denis Vlasenko | b68979a | 2007-11-02 23:31:10 +0000 | [diff] [blame] | 332 | |
Denis Vlasenko | bb26db4 | 2008-10-31 02:04:28 +0000 | [diff] [blame] | 333 | if (opt & (MODPROBE_OPT_DUMP_ONLY | MODPROBE_OPT_LIST_ONLY | |
| 334 | MODPROBE_OPT_SHOW_ONLY)) |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 335 | bb_error_msg_and_die("not supported"); |
Robert Griebl | 236abbf | 2002-05-22 23:34:35 +0000 | [diff] [blame] | 336 | |
Denis Vlasenko | bb26db4 | 2008-10-31 02:04:28 +0000 | [diff] [blame] | 337 | if (!argv[0]) { |
| 338 | if (opt & MODPROBE_OPT_REMOVE) { |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 339 | /* "modprobe -r" (w/o params). |
| 340 | * "If name is NULL, all unused modules marked |
| 341 | * autoclean will be removed". |
| 342 | */ |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 343 | if (bb_delete_module(NULL, O_NONBLOCK|O_EXCL) != 0) |
| 344 | bb_perror_msg_and_die("rmmod"); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 345 | } |
Denis Vlasenko | bb26db4 | 2008-10-31 02:04:28 +0000 | [diff] [blame] | 346 | return EXIT_SUCCESS; |
| 347 | } |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 348 | |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 349 | /* Goto modules location */ |
| 350 | xchdir(CONFIG_DEFAULT_MODULES_DIR); |
| 351 | uname(&uts); |
| 352 | xchdir(uts.release); |
Eric Andersen | 0139ca9 | 2001-07-22 23:01:03 +0000 | [diff] [blame] | 353 | |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 354 | /* Retrieve module names of already loaded modules */ |
Denis Vlasenko | bb26db4 | 2008-10-31 02:04:28 +0000 | [diff] [blame] | 355 | { |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 356 | char *s; |
Denis Vlasenko | bb26db4 | 2008-10-31 02:04:28 +0000 | [diff] [blame] | 357 | parser_t *parser = config_open2("/proc/modules", fopen_for_read); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 358 | while (config_read(parser, &s, 1, 1, "# \t", PARSE_NORMAL & ~PARSE_GREEDY)) |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 359 | get_or_add_modentry(s)->flags |= MODULE_FLAG_LOADED; |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 360 | config_close(parser); |
| 361 | } |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 362 | |
Denys Vlasenko | 140def8 | 2009-05-26 12:48:34 +0200 | [diff] [blame] | 363 | if (opt & (MODPROBE_OPT_INSERT_ALL | MODPROBE_OPT_REMOVE)) { |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 364 | /* Each argument is a module name */ |
| 365 | do { |
Mike Frysinger | 00ffaea | 2009-05-05 20:13:45 -0400 | [diff] [blame] | 366 | DBG("adding module %s", *argv); |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 367 | add_probe(*argv++); |
| 368 | } while (*argv); |
| 369 | } else { |
| 370 | /* First argument is module name, rest are parameters */ |
Mike Frysinger | 00ffaea | 2009-05-05 20:13:45 -0400 | [diff] [blame] | 371 | DBG("probing just module %s", *argv); |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 372 | add_probe(argv[0]); |
| 373 | G.cmdline_mopts = parse_cmdline_module_options(argv); |
| 374 | } |
| 375 | |
| 376 | /* Happens if all requested modules are already loaded */ |
| 377 | if (G.probes == NULL) |
| 378 | return EXIT_SUCCESS; |
| 379 | |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 380 | read_config("/etc/modprobe.conf"); |
| 381 | read_config("/etc/modprobe.d"); |
| 382 | if (ENABLE_FEATURE_MODUTILS_SYMBOLS && G.need_symbols) |
| 383 | read_config("modules.symbols"); |
| 384 | load_modules_dep(); |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 385 | if (ENABLE_FEATURE_MODUTILS_ALIAS && G.num_unresolved_deps) { |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 386 | read_config("modules.alias"); |
| 387 | load_modules_dep(); |
| 388 | } |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 389 | |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 390 | while ((me = llist_pop(&G.probes)) != NULL) { |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 391 | if (me->realnames == NULL) { |
| 392 | /* This is not an alias. Literal names are blacklisted |
| 393 | * only if '-b' is given. |
| 394 | */ |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 395 | if (!(opt & MODPROBE_OPT_BLACKLIST) |
| 396 | || !(me->flags & MODULE_FLAG_BLACKLISTED) |
| 397 | ) { |
| 398 | rc = do_modprobe(me); |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 399 | //FIXME: what if rc > 0? |
Denis Vlasenko | bb26db4 | 2008-10-31 02:04:28 +0000 | [diff] [blame] | 400 | if (rc < 0 && !(opt & INSMOD_OPT_SILENT)) |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 401 | bb_error_msg("module %s not found", |
| 402 | me->probed_name); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 403 | } |
| 404 | } else { |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 405 | /* Probe all realnames */ |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 406 | do { |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 407 | char *realname = llist_pop(&me->realnames); |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 408 | struct module_entry *m2; |
| 409 | |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 410 | DBG("probing %s by realname %s", me->modname, realname); |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 411 | m2 = get_or_add_modentry(realname); |
| 412 | if (!(m2->flags & MODULE_FLAG_BLACKLISTED)) |
| 413 | do_modprobe(m2); |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 414 | //FIXME: error check? |
Denis Vlasenko | 9604e1b | 2009-03-03 18:47:56 +0000 | [diff] [blame] | 415 | free(realname); |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 416 | } while (me->realnames != NULL); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 417 | } |
| 418 | } |
| 419 | |
| 420 | return EXIT_SUCCESS; |
Eric Andersen | 0139ca9 | 2001-07-22 23:01:03 +0000 | [diff] [blame] | 421 | } |