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