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 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 8 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 9 | */ |
Denys Vlasenko | e32b64c | 2016-11-23 07:54:52 +0100 | [diff] [blame] | 10 | //config:config MODPROBE |
| 11 | //config: bool "modprobe" |
Denys Vlasenko | a1cd0d9 | 2016-12-23 15:12:27 +0100 | [diff] [blame] | 12 | //config: default y |
Denys Vlasenko | e32b64c | 2016-11-23 07:54:52 +0100 | [diff] [blame] | 13 | //config: select PLATFORM_LINUX |
| 14 | //config: help |
| 15 | //config: Handle the loading of modules, and their dependencies on a high |
| 16 | //config: level. |
| 17 | //config: |
| 18 | //config:config FEATURE_MODPROBE_BLACKLIST |
| 19 | //config: bool "Blacklist support" |
Denys Vlasenko | a1cd0d9 | 2016-12-23 15:12:27 +0100 | [diff] [blame] | 20 | //config: default y |
| 21 | //config: depends on MODPROBE && !MODPROBE_SMALL |
Denys Vlasenko | e32b64c | 2016-11-23 07:54:52 +0100 | [diff] [blame] | 22 | //config: select PLATFORM_LINUX |
| 23 | //config: help |
| 24 | //config: Say 'y' here to enable support for the 'blacklist' command in |
| 25 | //config: modprobe.conf. This prevents the alias resolver to resolve |
| 26 | //config: blacklisted modules. This is useful if you want to prevent your |
| 27 | //config: hardware autodetection scripts to load modules like evdev, frame |
| 28 | //config: buffer drivers etc. |
Eric Andersen | 0139ca9 | 2001-07-22 23:01:03 +0000 | [diff] [blame] | 29 | |
Denys Vlasenko | a1cd0d9 | 2016-12-23 15:12:27 +0100 | [diff] [blame] | 30 | //applet:IF_MODPROBE(IF_NOT_MODPROBE_SMALL(APPLET(modprobe, BB_DIR_SBIN, BB_SUID_DROP))) |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 31 | |
Denys Vlasenko | a1cd0d9 | 2016-12-23 15:12:27 +0100 | [diff] [blame] | 32 | //kbuild:ifneq ($(CONFIG_MODPROBE_SMALL),y) |
Denys Vlasenko | e32b64c | 2016-11-23 07:54:52 +0100 | [diff] [blame] | 33 | //kbuild:lib-$(CONFIG_MODPROBE) += modprobe.o modutils.o |
Denys Vlasenko | a1cd0d9 | 2016-12-23 15:12:27 +0100 | [diff] [blame] | 34 | //kbuild:endif |
Denys Vlasenko | e32b64c | 2016-11-23 07:54:52 +0100 | [diff] [blame] | 35 | |
Serj Kalichev | 8578196 | 2010-12-28 04:18:18 +0100 | [diff] [blame] | 36 | #include "libbb.h" |
| 37 | #include "modutils.h" |
| 38 | #include <sys/utsname.h> |
| 39 | #include <fnmatch.h> |
| 40 | |
Denys Vlasenko | e998b08 | 2015-01-15 02:48:36 +0100 | [diff] [blame] | 41 | #if 1 |
Serj Kalichev | 8578196 | 2010-12-28 04:18:18 +0100 | [diff] [blame] | 42 | #define DBG(...) ((void)0) |
Denys Vlasenko | e998b08 | 2015-01-15 02:48:36 +0100 | [diff] [blame] | 43 | #else |
| 44 | #define DBG(fmt, ...) bb_error_msg("%s: " fmt, __func__, ## __VA_ARGS__) |
| 45 | #endif |
Serj Kalichev | 8578196 | 2010-12-28 04:18:18 +0100 | [diff] [blame] | 46 | |
| 47 | /* Note that unlike older versions of modules.dep/depmod (busybox and m-i-t), |
| 48 | * we expect the full dependency list to be specified in modules.dep. |
| 49 | * Older versions would only export the direct dependency list. |
| 50 | */ |
| 51 | |
| 52 | |
Pascal Bellard | b82b34e | 2010-06-07 01:16:45 +0200 | [diff] [blame] | 53 | //usage:#if !ENABLE_MODPROBE_SMALL |
| 54 | //usage:#define modprobe_notes_usage |
| 55 | //usage: "modprobe can (un)load a stack of modules, passing each module options (when\n" |
| 56 | //usage: "loading). modprobe uses a configuration file to determine what option(s) to\n" |
| 57 | //usage: "pass each module it loads.\n" |
| 58 | //usage: "\n" |
| 59 | //usage: "The configuration file is searched (in this order):\n" |
| 60 | //usage: "\n" |
| 61 | //usage: " /etc/modprobe.conf (2.6 only)\n" |
| 62 | //usage: " /etc/modules.conf\n" |
| 63 | //usage: " /etc/conf.modules (deprecated)\n" |
| 64 | //usage: "\n" |
| 65 | //usage: "They all have the same syntax (see below). If none is present, it is\n" |
| 66 | //usage: "_not_ an error; each loaded module is then expected to load without\n" |
| 67 | //usage: "options. Once a file is found, the others are tested for.\n" |
| 68 | //usage: "\n" |
| 69 | //usage: "/etc/modules.conf entry format:\n" |
| 70 | //usage: "\n" |
| 71 | //usage: " alias <alias_name> <mod_name>\n" |
| 72 | //usage: " Makes it possible to modprobe alias_name, when there is no such module.\n" |
| 73 | //usage: " It makes sense if your mod_name is long, or you want a more representative\n" |
| 74 | //usage: " name for that module (eg. 'scsi' in place of 'aha7xxx').\n" |
| 75 | //usage: " This makes it also possible to use a different set of options (below) for\n" |
| 76 | //usage: " the module and the alias.\n" |
| 77 | //usage: " A module can be aliased more than once.\n" |
| 78 | //usage: "\n" |
| 79 | //usage: " options <mod_name|alias_name> <symbol=value...>\n" |
| 80 | //usage: " When loading module mod_name (or the module aliased by alias_name), pass\n" |
| 81 | //usage: " the \"symbol=value\" pairs as option to that module.\n" |
| 82 | //usage: "\n" |
| 83 | //usage: "Sample /etc/modules.conf file:\n" |
| 84 | //usage: "\n" |
| 85 | //usage: " options tulip irq=3\n" |
| 86 | //usage: " alias tulip tulip2\n" |
| 87 | //usage: " options tulip2 irq=4 io=0x308\n" |
| 88 | //usage: "\n" |
| 89 | //usage: "Other functionality offered by 'classic' modprobe is not available in\n" |
| 90 | //usage: "this implementation.\n" |
| 91 | //usage: "\n" |
| 92 | //usage: "If module options are present both in the config file, and on the command line,\n" |
| 93 | //usage: "then the options from the command line will be passed to the module _after_\n" |
| 94 | //usage: "the options from the config file. That way, you can have defaults in the config\n" |
| 95 | //usage: "file, and override them for a specific usage from the command line.\n" |
| 96 | //usage:#define modprobe_example_usage |
| 97 | //usage: "(with the above /etc/modules.conf):\n\n" |
| 98 | //usage: "$ modprobe tulip\n" |
| 99 | //usage: " will load the module 'tulip' with default option 'irq=3'\n\n" |
| 100 | //usage: "$ modprobe tulip irq=5\n" |
| 101 | //usage: " will load the module 'tulip' with option 'irq=5', thus overriding the default\n\n" |
| 102 | //usage: "$ modprobe tulip2\n" |
| 103 | //usage: " will load the module 'tulip' with default options 'irq=4 io=0x308',\n" |
| 104 | //usage: " which are the default for alias 'tulip2'\n\n" |
| 105 | //usage: "$ modprobe tulip2 irq=8\n" |
| 106 | //usage: " will load the module 'tulip' with default options 'irq=4 io=0x308 irq=8',\n" |
| 107 | //usage: " which are the default for alias 'tulip2' overridden by the option 'irq=8'\n\n" |
| 108 | //usage: " from the command line\n\n" |
| 109 | //usage: "$ modprobe tulip2 irq=2 io=0x210\n" |
| 110 | //usage: " will load the module 'tulip' with default options 'irq=4 io=0x308 irq=4 io=0x210',\n" |
| 111 | //usage: " which are the default for alias 'tulip2' overridden by the options 'irq=2 io=0x210'\n\n" |
| 112 | //usage: " from the command line\n" |
| 113 | //usage: |
| 114 | //usage:#define modprobe_trivial_usage |
Serj Kalichev | 8578196 | 2010-12-28 04:18:18 +0100 | [diff] [blame] | 115 | //usage: "[-alrqvsD" IF_FEATURE_MODPROBE_BLACKLIST("b") "]" |
Denys Vlasenko | 5fd3ddf | 2014-04-19 15:04:39 +0200 | [diff] [blame] | 116 | //usage: " MODULE [SYMBOL=VALUE]..." |
Pascal Bellard | b82b34e | 2010-06-07 01:16:45 +0200 | [diff] [blame] | 117 | //usage:#define modprobe_full_usage "\n\n" |
Denys Vlasenko | 6642676 | 2011-06-05 03:58:28 +0200 | [diff] [blame] | 118 | //usage: " -a Load multiple MODULEs" |
Serj Kalichev | 8578196 | 2010-12-28 04:18:18 +0100 | [diff] [blame] | 119 | //usage: "\n -l List (MODULE is a pattern)" |
| 120 | //usage: "\n -r Remove MODULE (stacks) or do autoclean" |
| 121 | //usage: "\n -q Quiet" |
| 122 | //usage: "\n -v Verbose" |
| 123 | //usage: "\n -s Log to syslog" |
| 124 | //usage: "\n -D Show dependencies" |
Pascal Bellard | b82b34e | 2010-06-07 01:16:45 +0200 | [diff] [blame] | 125 | //usage: IF_FEATURE_MODPROBE_BLACKLIST( |
Serj Kalichev | 8578196 | 2010-12-28 04:18:18 +0100 | [diff] [blame] | 126 | //usage: "\n -b Apply blacklist to module names too" |
Pascal Bellard | b82b34e | 2010-06-07 01:16:45 +0200 | [diff] [blame] | 127 | //usage: ) |
| 128 | //usage:#endif /* !ENABLE_MODPROBE_SMALL */ |
| 129 | |
Felipe Contreras | 428bd2d | 2012-01-31 14:55:15 +0100 | [diff] [blame] | 130 | /* Note: usage text doesn't document various 2.4 options |
| 131 | * we pull in through INSMOD_OPTS define |
| 132 | * Note2: -b is always accepted, but if !FEATURE_MODPROBE_BLACKLIST, |
| 133 | * it is a no-op. |
| 134 | */ |
| 135 | #define MODPROBE_OPTS "alrDb" |
Serj Kalichev | 8578196 | 2010-12-28 04:18:18 +0100 | [diff] [blame] | 136 | /* -a and -D _are_ in fact compatible */ |
| 137 | #define MODPROBE_COMPLEMENTARY ("q-v:v-q:l--arD:r--alD:a--lr:D--rl") |
Felipe Contreras | 428bd2d | 2012-01-31 14:55:15 +0100 | [diff] [blame] | 138 | //#define MODPROBE_OPTS "acd:lnrt:C:b" |
Serj Kalichev | e4e911e | 2010-12-26 01:56:19 +0100 | [diff] [blame] | 139 | //#define MODPROBE_COMPLEMENTARY "q-v:v-q:l--acr:a--lr:r--al" |
Pascal Bellard | b82b34e | 2010-06-07 01:16:45 +0200 | [diff] [blame] | 140 | enum { |
Serj Kalichev | e4e911e | 2010-12-26 01:56:19 +0100 | [diff] [blame] | 141 | OPT_INSERT_ALL = (INSMOD_OPT_UNUSED << 0), /* a */ |
| 142 | //OPT_DUMP_ONLY = (INSMOD_OPT_UNUSED << x), /* c */ |
| 143 | //OPT_DIRNAME = (INSMOD_OPT_UNUSED << x), /* d */ |
| 144 | OPT_LIST_ONLY = (INSMOD_OPT_UNUSED << 1), /* l */ |
| 145 | //OPT_SHOW_ONLY = (INSMOD_OPT_UNUSED << x), /* n */ |
| 146 | OPT_REMOVE = (INSMOD_OPT_UNUSED << 2), /* r */ |
| 147 | //OPT_RESTRICT = (INSMOD_OPT_UNUSED << x), /* t */ |
| 148 | //OPT_VERONLY = (INSMOD_OPT_UNUSED << x), /* V */ |
Serj Kalichev | 8578196 | 2010-12-28 04:18:18 +0100 | [diff] [blame] | 149 | //OPT_CONFIGFILE = (INSMOD_OPT_UNUSED << x), /* C */ |
| 150 | OPT_SHOW_DEPS = (INSMOD_OPT_UNUSED << 3), /* D */ |
| 151 | OPT_BLACKLIST = (INSMOD_OPT_UNUSED << 4) * ENABLE_FEATURE_MODPROBE_BLACKLIST, |
Pascal Bellard | b82b34e | 2010-06-07 01:16:45 +0200 | [diff] [blame] | 152 | }; |
Serj Kalichev | 8578196 | 2010-12-28 04:18:18 +0100 | [diff] [blame] | 153 | #if ENABLE_LONG_OPTS |
| 154 | static const char modprobe_longopts[] ALIGN1 = |
| 155 | /* nobody asked for long opts (yet) */ |
| 156 | // "all\0" No_argument "a" |
| 157 | // "list\0" No_argument "l" |
| 158 | // "remove\0" No_argument "r" |
| 159 | // "quiet\0" No_argument "q" |
| 160 | // "verbose\0" No_argument "v" |
| 161 | // "syslog\0" No_argument "s" |
| 162 | /* module-init-tools 3.11.1 has only long opt --show-depends |
| 163 | * but no short -D, we provide long opt for scripts which |
| 164 | * were written for 3.11.1: */ |
Felipe Contreras | 428bd2d | 2012-01-31 14:55:15 +0100 | [diff] [blame] | 165 | "show-depends\0" No_argument "D" |
Serj Kalichev | 8578196 | 2010-12-28 04:18:18 +0100 | [diff] [blame] | 166 | // "use-blacklist\0" No_argument "b" |
Serj Kalichev | 8578196 | 2010-12-28 04:18:18 +0100 | [diff] [blame] | 167 | ; |
| 168 | #endif |
Pascal Bellard | b82b34e | 2010-06-07 01:16:45 +0200 | [diff] [blame] | 169 | |
Denys Vlasenko | ee47f6e | 2009-06-17 18:46:06 +0200 | [diff] [blame] | 170 | #define MODULE_FLAG_LOADED 0x0001 |
| 171 | #define MODULE_FLAG_NEED_DEPS 0x0002 |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 172 | /* "was seen in modules.dep": */ |
Denys Vlasenko | ee47f6e | 2009-06-17 18:46:06 +0200 | [diff] [blame] | 173 | #define MODULE_FLAG_FOUND_IN_MODDEP 0x0004 |
| 174 | #define MODULE_FLAG_BLACKLISTED 0x0008 |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 175 | |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 176 | struct globals { |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 177 | llist_t *probes; /* MEs of module(s) requested on cmdline */ |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 178 | char *cmdline_mopts; /* module options from cmdline */ |
| 179 | int num_unresolved_deps; |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 180 | /* bool. "Did we have 'symbol:FOO' requested on cmdline?" */ |
| 181 | smallint need_symbols; |
Serj Kalichev | e4e911e | 2010-12-26 01:56:19 +0100 | [diff] [blame] | 182 | struct utsname uts; |
Timo Teräs | 48dc80b | 2015-11-05 18:54:55 +0100 | [diff] [blame] | 183 | module_db db; |
Denys Vlasenko | 98a4c7c | 2010-02-04 15:00:15 +0100 | [diff] [blame] | 184 | } FIX_ALIASING; |
Timo Teras | e12e0ac | 2011-06-20 09:38:13 +0200 | [diff] [blame] | 185 | #define G (*ptr_to_globals) |
| 186 | #define INIT_G() do { \ |
Denys Vlasenko | 982e87f | 2013-07-30 11:52:58 +0200 | [diff] [blame] | 187 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
Timo Teras | e12e0ac | 2011-06-20 09:38:13 +0200 | [diff] [blame] | 188 | } while (0) |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 189 | |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 190 | |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 191 | static int read_config(const char *path); |
| 192 | |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 193 | static char *gather_options_str(char *opts, const char *append) |
| 194 | { |
| 195 | /* Speed-optimized. We call gather_options_str many times. */ |
Denys Vlasenko | da879ec | 2010-01-26 08:17:45 +0100 | [diff] [blame] | 196 | if (append) { |
| 197 | if (opts == NULL) { |
| 198 | opts = xstrdup(append); |
| 199 | } else { |
| 200 | int optlen = strlen(opts); |
| 201 | opts = xrealloc(opts, optlen + strlen(append) + 2); |
| 202 | sprintf(opts + optlen, " %s", append); |
| 203 | } |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 204 | } |
| 205 | return opts; |
| 206 | } |
| 207 | |
Timo Teräs | 48dc80b | 2015-11-05 18:54:55 +0100 | [diff] [blame] | 208 | static struct module_entry *get_or_add_modentry(const char *module) |
Denis Vlasenko | f45c4f4 | 2008-06-16 04:09:25 +0000 | [diff] [blame] | 209 | { |
Timo Teräs | 48dc80b | 2015-11-05 18:54:55 +0100 | [diff] [blame] | 210 | return moddb_get_or_create(&G.db, module); |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | static void add_probe(const char *name) |
| 214 | { |
| 215 | struct module_entry *m; |
| 216 | |
| 217 | m = get_or_add_modentry(name); |
Serj Kalichev | e4e911e | 2010-12-26 01:56:19 +0100 | [diff] [blame] | 218 | if (!(option_mask32 & (OPT_REMOVE | OPT_SHOW_DEPS)) |
Denys Vlasenko | 140def8 | 2009-05-26 12:48:34 +0200 | [diff] [blame] | 219 | && (m->flags & MODULE_FLAG_LOADED) |
| 220 | ) { |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 221 | DBG("skipping %s, it is already loaded", name); |
| 222 | return; |
| 223 | } |
| 224 | |
Mike Frysinger | 00ffaea | 2009-05-05 20:13:45 -0400 | [diff] [blame] | 225 | DBG("queuing %s", name); |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 226 | m->probed_name = name; |
| 227 | m->flags |= MODULE_FLAG_NEED_DEPS; |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 228 | llist_add_to_end(&G.probes, m); |
| 229 | G.num_unresolved_deps++; |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 230 | if (ENABLE_FEATURE_MODUTILS_SYMBOLS |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 231 | && is_prefixed_with(m->modname, "symbol:") |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 232 | ) { |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 233 | G.need_symbols = 1; |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 234 | } |
Denis Vlasenko | 9ddc8d5 | 2008-05-18 14:39:43 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 237 | static int FAST_FUNC config_file_action(const char *filename, |
| 238 | struct stat *statbuf UNUSED_PARAM, |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 239 | void *userdata UNUSED_PARAM, |
Denys Vlasenko | 20dd499 | 2016-03-29 19:23:55 +0200 | [diff] [blame] | 240 | int depth) |
Rob Landley | 7261575 | 2006-04-10 16:09:52 +0000 | [diff] [blame] | 241 | { |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 242 | char *tokens[3]; |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 243 | parser_t *p; |
Denys Vlasenko | ee47f6e | 2009-06-17 18:46:06 +0200 | [diff] [blame] | 244 | struct module_entry *m; |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 245 | int rc = TRUE; |
Mike Frysinger | 3a5cc98 | 2016-02-12 23:26:51 -0500 | [diff] [blame] | 246 | const char *base, *ext; |
Denis Vlasenko | e1ee48e | 2008-07-29 00:19:44 +0000 | [diff] [blame] | 247 | |
Denys Vlasenko | 20dd499 | 2016-03-29 19:23:55 +0200 | [diff] [blame] | 248 | /* Skip files that begin with a "." */ |
Mike Frysinger | 3a5cc98 | 2016-02-12 23:26:51 -0500 | [diff] [blame] | 249 | base = bb_basename(filename); |
| 250 | if (base[0] == '.') |
| 251 | goto error; |
| 252 | |
Denys Vlasenko | 20dd499 | 2016-03-29 19:23:55 +0200 | [diff] [blame] | 253 | /* In dir recursion, skip files that do not end with a ".conf" |
| 254 | * depth==0: read_config("modules.{symbols,alias}") must work, |
| 255 | * "include FILE_NOT_ENDING_IN_CONF" must work too. |
| 256 | */ |
| 257 | if (depth != 0) { |
| 258 | ext = strrchr(base, '.'); |
| 259 | if (ext == NULL || strcmp(ext + 1, "conf")) |
| 260 | goto error; |
| 261 | } |
Denis Vlasenko | e1ee48e | 2008-07-29 00:19:44 +0000 | [diff] [blame] | 262 | |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 263 | p = config_open2(filename, fopen_for_read); |
| 264 | if (p == NULL) { |
| 265 | rc = FALSE; |
| 266 | goto error; |
| 267 | } |
Rob Landley | 7261575 | 2006-04-10 16:09:52 +0000 | [diff] [blame] | 268 | |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 269 | while (config_read(p, tokens, 3, 2, "# \t", PARSE_NORMAL)) { |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 270 | //Use index_in_strings? |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 271 | if (strcmp(tokens[0], "alias") == 0) { |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 272 | /* alias <wildcard> <modulename> */ |
| 273 | llist_t *l; |
| 274 | char wildcard[MODULE_NAME_LEN]; |
| 275 | char *rmod; |
| 276 | |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 277 | if (tokens[2] == NULL) |
| 278 | continue; |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 279 | filename2modname(tokens[1], wildcard); |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 280 | |
Timo Teras | e12e0ac | 2011-06-20 09:38:13 +0200 | [diff] [blame] | 281 | for (l = G.probes; l; l = l->link) { |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 282 | m = (struct module_entry *) l->data; |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 283 | if (fnmatch(wildcard, m->modname, 0) != 0) |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 284 | continue; |
| 285 | rmod = filename2modname(tokens[2], NULL); |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 286 | llist_add_to(&m->realnames, rmod); |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 287 | |
| 288 | if (m->flags & MODULE_FLAG_NEED_DEPS) { |
| 289 | m->flags &= ~MODULE_FLAG_NEED_DEPS; |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 290 | G.num_unresolved_deps--; |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | m = get_or_add_modentry(rmod); |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 294 | if (!(m->flags & MODULE_FLAG_NEED_DEPS)) { |
| 295 | m->flags |= MODULE_FLAG_NEED_DEPS; |
| 296 | G.num_unresolved_deps++; |
| 297 | } |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 298 | } |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 299 | } else if (strcmp(tokens[0], "options") == 0) { |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 300 | /* options <modulename> <option...> */ |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 301 | if (tokens[2] == NULL) |
| 302 | continue; |
| 303 | m = get_or_add_modentry(tokens[1]); |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 304 | m->options = gather_options_str(m->options, tokens[2]); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 305 | } else if (strcmp(tokens[0], "include") == 0) { |
Denys Vlasenko | 20dd499 | 2016-03-29 19:23:55 +0200 | [diff] [blame] | 306 | /* include <filename>/<dirname> (yes, directories also must work) */ |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 307 | read_config(tokens[1]); |
| 308 | } else if (ENABLE_FEATURE_MODPROBE_BLACKLIST |
| 309 | && strcmp(tokens[0], "blacklist") == 0 |
| 310 | ) { |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 311 | /* blacklist <modulename> */ |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 312 | get_or_add_modentry(tokens[1])->flags |= MODULE_FLAG_BLACKLISTED; |
Rob Landley | 7261575 | 2006-04-10 16:09:52 +0000 | [diff] [blame] | 313 | } |
Rob Landley | e919096 | 2005-12-12 19:38:44 +0000 | [diff] [blame] | 314 | } |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 315 | config_close(p); |
Denys Vlasenko | ee47f6e | 2009-06-17 18:46:06 +0200 | [diff] [blame] | 316 | error: |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 317 | return rc; |
| 318 | } |
| 319 | |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 320 | static int read_config(const char *path) |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 321 | { |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 322 | return recursive_action(path, ACTION_RECURSE | ACTION_QUIET, |
Denys Vlasenko | 20dd499 | 2016-03-29 19:23:55 +0200 | [diff] [blame] | 323 | config_file_action, NULL, NULL, |
| 324 | /*depth:*/ 0); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 325 | } |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 326 | |
Denys Vlasenko | 7eaf58d | 2009-06-17 20:33:50 +0200 | [diff] [blame] | 327 | static const char *humanly_readable_name(struct module_entry *m) |
| 328 | { |
| 329 | /* probed_name may be NULL. modname always exists. */ |
| 330 | return m->probed_name ? m->probed_name : m->modname; |
| 331 | } |
| 332 | |
Peter Korsgaard | 8752973 | 2015-09-09 14:55:07 +0200 | [diff] [blame] | 333 | /* Like strsep(&stringp, "\n\t ") but quoted text goes to single token |
| 334 | * even if it contains whitespace. |
| 335 | */ |
| 336 | static char *strsep_quotes(char **stringp) |
| 337 | { |
| 338 | char *s, *start = *stringp; |
| 339 | |
| 340 | if (!start) |
| 341 | return NULL; |
| 342 | |
| 343 | for (s = start; ; s++) { |
| 344 | switch (*s) { |
| 345 | case '"': |
| 346 | s = strchrnul(s + 1, '"'); /* find trailing quote */ |
| 347 | if (*s != '\0') |
| 348 | s++; /* skip trailing quote */ |
| 349 | /* fall through */ |
| 350 | case '\0': |
| 351 | case '\n': |
| 352 | case '\t': |
| 353 | case ' ': |
| 354 | if (*s != '\0') { |
| 355 | *s = '\0'; |
| 356 | *stringp = s + 1; |
| 357 | } else { |
| 358 | *stringp = NULL; |
| 359 | } |
| 360 | return start; |
| 361 | } |
| 362 | } |
| 363 | } |
| 364 | |
Denys Vlasenko | 3e26d4f | 2010-02-27 23:15:22 +0100 | [diff] [blame] | 365 | static char *parse_and_add_kcmdline_module_options(char *options, const char *modulename) |
| 366 | { |
Denys Vlasenko | 197202d | 2010-03-03 04:20:33 +0100 | [diff] [blame] | 367 | char *kcmdline_buf; |
Denys Vlasenko | 3e26d4f | 2010-02-27 23:15:22 +0100 | [diff] [blame] | 368 | char *kcmdline; |
| 369 | char *kptr; |
Denys Vlasenko | 3e26d4f | 2010-02-27 23:15:22 +0100 | [diff] [blame] | 370 | |
Denys Vlasenko | 197202d | 2010-03-03 04:20:33 +0100 | [diff] [blame] | 371 | kcmdline_buf = xmalloc_open_read_close("/proc/cmdline", NULL); |
| 372 | if (!kcmdline_buf) |
Denys Vlasenko | 3e26d4f | 2010-02-27 23:15:22 +0100 | [diff] [blame] | 373 | return options; |
Denys Vlasenko | 3e26d4f | 2010-02-27 23:15:22 +0100 | [diff] [blame] | 374 | |
Denys Vlasenko | 3e26d4f | 2010-02-27 23:15:22 +0100 | [diff] [blame] | 375 | kcmdline = kcmdline_buf; |
Peter Korsgaard | 8752973 | 2015-09-09 14:55:07 +0200 | [diff] [blame] | 376 | while ((kptr = strsep_quotes(&kcmdline)) != NULL) { |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 377 | char *after_modulename = is_prefixed_with(kptr, modulename); |
| 378 | if (!after_modulename || *after_modulename != '.') |
Denys Vlasenko | 3e26d4f | 2010-02-27 23:15:22 +0100 | [diff] [blame] | 379 | continue; |
| 380 | /* It is "modulename.xxxx" */ |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 381 | kptr = after_modulename + 1; |
Denys Vlasenko | 3e26d4f | 2010-02-27 23:15:22 +0100 | [diff] [blame] | 382 | if (strchr(kptr, '=') != NULL) { |
| 383 | /* It is "modulename.opt=[val]" */ |
| 384 | options = gather_options_str(options, kptr); |
| 385 | } |
| 386 | } |
Denys Vlasenko | 197202d | 2010-03-03 04:20:33 +0100 | [diff] [blame] | 387 | free(kcmdline_buf); |
Denys Vlasenko | 3e26d4f | 2010-02-27 23:15:22 +0100 | [diff] [blame] | 388 | |
| 389 | return options; |
| 390 | } |
| 391 | |
Denys Vlasenko | ee47f6e | 2009-06-17 18:46:06 +0200 | [diff] [blame] | 392 | /* Return: similar to bb_init_module: |
| 393 | * 0 on success, |
| 394 | * -errno on open/read error, |
| 395 | * errno on init_module() error |
| 396 | */ |
Pascal Bellard | b82b34e | 2010-06-07 01:16:45 +0200 | [diff] [blame] | 397 | /* NB: INSMOD_OPT_SILENT bit suppresses ONLY non-existent modules, |
| 398 | * not deleted ones (those are still listed in modules.dep). |
| 399 | * module-init-tools version 3.4: |
| 400 | * # modprobe bogus |
| 401 | * FATAL: Module bogus not found. [exitcode 1] |
| 402 | * # modprobe -q bogus [silent, exitcode still 1] |
| 403 | * but: |
| 404 | * # rm kernel/drivers/net/dummy.ko |
| 405 | * # modprobe -q dummy |
| 406 | * FATAL: Could not open '/lib/modules/xxx/kernel/drivers/net/dummy.ko': No such file or directory |
| 407 | * [exitcode 1] |
| 408 | */ |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 409 | static int do_modprobe(struct module_entry *m) |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 410 | { |
Denys Vlasenko | f3cbfc0 | 2009-05-28 03:54:29 +0200 | [diff] [blame] | 411 | int rc, first; |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 412 | |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 413 | if (!(m->flags & MODULE_FLAG_FOUND_IN_MODDEP)) { |
Denys Vlasenko | ee47f6e | 2009-06-17 18:46:06 +0200 | [diff] [blame] | 414 | if (!(option_mask32 & INSMOD_OPT_SILENT)) |
Denys Vlasenko | 7eaf58d | 2009-06-17 20:33:50 +0200 | [diff] [blame] | 415 | bb_error_msg("module %s not found in modules.dep", |
| 416 | humanly_readable_name(m)); |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 417 | return -ENOENT; |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 418 | } |
| 419 | DBG("do_modprob'ing %s", m->modname); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 420 | |
Serj Kalichev | e4e911e | 2010-12-26 01:56:19 +0100 | [diff] [blame] | 421 | if (!(option_mask32 & OPT_REMOVE)) |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 422 | m->deps = llist_rev(m->deps); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 423 | |
Timo Teras | e12e0ac | 2011-06-20 09:38:13 +0200 | [diff] [blame] | 424 | if (0) { |
| 425 | llist_t *l; |
| 426 | for (l = m->deps; l; l = l->link) |
| 427 | DBG("dep: %s", l->data); |
| 428 | } |
Mike Frysinger | 00ffaea | 2009-05-05 20:13:45 -0400 | [diff] [blame] | 429 | |
Denys Vlasenko | f3cbfc0 | 2009-05-28 03:54:29 +0200 | [diff] [blame] | 430 | first = 1; |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 431 | rc = 0; |
Denys Vlasenko | ee47f6e | 2009-06-17 18:46:06 +0200 | [diff] [blame] | 432 | while (m->deps) { |
Serj Kalichev | 8578196 | 2010-12-28 04:18:18 +0100 | [diff] [blame] | 433 | struct module_entry *m2; |
| 434 | char *fn, *options; |
| 435 | |
Denys Vlasenko | ee47f6e | 2009-06-17 18:46:06 +0200 | [diff] [blame] | 436 | rc = 0; |
| 437 | fn = llist_pop(&m->deps); /* we leak it */ |
Natanael Copa | 9de69c0 | 2015-01-16 13:53:05 +0100 | [diff] [blame] | 438 | m2 = get_or_add_modentry(bb_get_last_path_component_nostrip(fn)); |
Denys Vlasenko | ee47f6e | 2009-06-17 18:46:06 +0200 | [diff] [blame] | 439 | |
Serj Kalichev | e4e911e | 2010-12-26 01:56:19 +0100 | [diff] [blame] | 440 | if (option_mask32 & OPT_REMOVE) { |
Denys Vlasenko | ee47f6e | 2009-06-17 18:46:06 +0200 | [diff] [blame] | 441 | /* modprobe -r */ |
Denys Vlasenko | f3cbfc0 | 2009-05-28 03:54:29 +0200 | [diff] [blame] | 442 | if (m2->flags & MODULE_FLAG_LOADED) { |
Denys Vlasenko | ee47f6e | 2009-06-17 18:46:06 +0200 | [diff] [blame] | 443 | rc = bb_delete_module(m2->modname, O_EXCL); |
| 444 | if (rc) { |
| 445 | if (first) { |
Denys Vlasenko | 941e7a4 | 2015-10-24 04:19:56 +0200 | [diff] [blame] | 446 | bb_perror_msg("can't unload module '%s'", |
Denys Vlasenko | cd13974 | 2015-10-24 04:17:04 +0200 | [diff] [blame] | 447 | humanly_readable_name(m2)); |
Denys Vlasenko | ee47f6e | 2009-06-17 18:46:06 +0200 | [diff] [blame] | 448 | break; |
| 449 | } |
Denys Vlasenko | f3cbfc0 | 2009-05-28 03:54:29 +0200 | [diff] [blame] | 450 | } else { |
| 451 | m2->flags &= ~MODULE_FLAG_LOADED; |
| 452 | } |
| 453 | } |
| 454 | /* do not error out if *deps* fail to unload */ |
| 455 | first = 0; |
Denys Vlasenko | ee47f6e | 2009-06-17 18:46:06 +0200 | [diff] [blame] | 456 | continue; |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 457 | } |
| 458 | |
Denys Vlasenko | ee47f6e | 2009-06-17 18:46:06 +0200 | [diff] [blame] | 459 | options = m2->options; |
| 460 | m2->options = NULL; |
Denys Vlasenko | 3e26d4f | 2010-02-27 23:15:22 +0100 | [diff] [blame] | 461 | options = parse_and_add_kcmdline_module_options(options, m2->modname); |
Denys Vlasenko | ee47f6e | 2009-06-17 18:46:06 +0200 | [diff] [blame] | 462 | if (m == m2) |
| 463 | options = gather_options_str(options, G.cmdline_mopts); |
Serj Kalichev | 8578196 | 2010-12-28 04:18:18 +0100 | [diff] [blame] | 464 | |
| 465 | if (option_mask32 & OPT_SHOW_DEPS) { |
| 466 | printf(options ? "insmod %s/%s/%s %s\n" |
| 467 | : "insmod %s/%s/%s\n", |
| 468 | CONFIG_DEFAULT_MODULES_DIR, G.uts.release, fn, |
| 469 | options); |
| 470 | free(options); |
| 471 | continue; |
| 472 | } |
| 473 | |
| 474 | if (m2->flags & MODULE_FLAG_LOADED) { |
| 475 | DBG("%s is already loaded, skipping", fn); |
| 476 | free(options); |
| 477 | continue; |
| 478 | } |
| 479 | |
Denys Vlasenko | ee47f6e | 2009-06-17 18:46:06 +0200 | [diff] [blame] | 480 | rc = bb_init_module(fn, options); |
| 481 | DBG("loaded %s '%s', rc:%d", fn, options, rc); |
Denys Vlasenko | 725b5a3 | 2010-01-10 04:52:45 +0100 | [diff] [blame] | 482 | if (rc == EEXIST) |
| 483 | rc = 0; |
Denys Vlasenko | ee47f6e | 2009-06-17 18:46:06 +0200 | [diff] [blame] | 484 | free(options); |
| 485 | if (rc) { |
Denys Vlasenko | 651a269 | 2010-03-23 16:25:17 +0100 | [diff] [blame] | 486 | bb_error_msg("can't load module %s (%s): %s", |
Denys Vlasenko | 7eaf58d | 2009-06-17 20:33:50 +0200 | [diff] [blame] | 487 | humanly_readable_name(m2), |
Denys Vlasenko | ee47f6e | 2009-06-17 18:46:06 +0200 | [diff] [blame] | 488 | fn, |
| 489 | moderror(rc) |
| 490 | ); |
| 491 | break; |
| 492 | } |
| 493 | m2->flags |= MODULE_FLAG_LOADED; |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 494 | } |
Denis Vlasenko | 9604e1b | 2009-03-03 18:47:56 +0000 | [diff] [blame] | 495 | |
Eric Andersen | 908e362 | 2003-06-20 09:56:37 +0000 | [diff] [blame] | 496 | return rc; |
Robert Griebl | 52e8d06 | 2002-05-14 23:42:08 +0000 | [diff] [blame] | 497 | } |
| 498 | |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 499 | static void load_modules_dep(void) |
| 500 | { |
| 501 | struct module_entry *m; |
| 502 | char *colon, *tokens[2]; |
| 503 | parser_t *p; |
| 504 | |
Denys Vlasenko | ee47f6e | 2009-06-17 18:46:06 +0200 | [diff] [blame] | 505 | /* Modprobe does not work at all without modules.dep, |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 506 | * even if the full module name is given. Returning error here |
| 507 | * was making us later confuse user with this message: |
| 508 | * "module /full/path/to/existing/file/module.ko not found". |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 509 | * It's better to die immediately, with good message. |
| 510 | * xfopen_for_read provides that. */ |
| 511 | p = config_open2(CONFIG_DEFAULT_DEPMOD_FILE, xfopen_for_read); |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 512 | |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 513 | while (G.num_unresolved_deps |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 514 | && config_read(p, tokens, 2, 1, "# \t", PARSE_NORMAL) |
| 515 | ) { |
| 516 | colon = last_char_is(tokens[0], ':'); |
| 517 | if (colon == NULL) |
| 518 | continue; |
Denys Vlasenko | 76b2262 | 2015-01-15 03:04:23 +0100 | [diff] [blame] | 519 | *colon = '\0'; |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 520 | |
Timo Teräs | 48dc80b | 2015-11-05 18:54:55 +0100 | [diff] [blame] | 521 | m = moddb_get(&G.db, bb_get_last_path_component_nostrip(tokens[0])); |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 522 | if (m == NULL) |
| 523 | continue; |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 524 | |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 525 | /* Optimization... */ |
| 526 | if ((m->flags & MODULE_FLAG_LOADED) |
Serj Kalichev | e4e911e | 2010-12-26 01:56:19 +0100 | [diff] [blame] | 527 | && !(option_mask32 & (OPT_REMOVE | OPT_SHOW_DEPS)) |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 528 | ) { |
| 529 | DBG("skip deps of %s, it's already loaded", tokens[0]); |
| 530 | continue; |
| 531 | } |
| 532 | |
| 533 | m->flags |= MODULE_FLAG_FOUND_IN_MODDEP; |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 534 | if ((m->flags & MODULE_FLAG_NEED_DEPS) && (m->deps == NULL)) { |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 535 | G.num_unresolved_deps--; |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 536 | llist_add_to(&m->deps, xstrdup(tokens[0])); |
| 537 | if (tokens[1]) |
Denys Vlasenko | 5da42fc | 2009-08-14 02:10:54 +0200 | [diff] [blame] | 538 | string_to_llist(tokens[1], &m->deps, " \t"); |
Mike Frysinger | 00ffaea | 2009-05-05 20:13:45 -0400 | [diff] [blame] | 539 | } else |
| 540 | DBG("skipping dep line"); |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 541 | } |
| 542 | config_close(p); |
| 543 | } |
| 544 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 545 | int modprobe_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | 9ddc004 | 2008-08-06 00:51:43 +0000 | [diff] [blame] | 546 | int modprobe_main(int argc UNUSED_PARAM, char **argv) |
Eric Andersen | 0139ca9 | 2001-07-22 23:01:03 +0000 | [diff] [blame] | 547 | { |
Denis Vlasenko | bb26db4 | 2008-10-31 02:04:28 +0000 | [diff] [blame] | 548 | int rc; |
| 549 | unsigned opt; |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 550 | struct module_entry *me; |
Eric Andersen | 0139ca9 | 2001-07-22 23:01:03 +0000 | [diff] [blame] | 551 | |
Serj Kalichev | e4e911e | 2010-12-26 01:56:19 +0100 | [diff] [blame] | 552 | INIT_G(); |
| 553 | |
| 554 | IF_LONG_OPTS(applet_long_options = modprobe_longopts;) |
Pascal Bellard | b82b34e | 2010-06-07 01:16:45 +0200 | [diff] [blame] | 555 | opt_complementary = MODPROBE_COMPLEMENTARY; |
| 556 | opt = getopt32(argv, INSMOD_OPTS MODPROBE_OPTS INSMOD_ARGS); |
Denis Vlasenko | 9ddc004 | 2008-08-06 00:51:43 +0000 | [diff] [blame] | 557 | argv += optind; |
Denis Vlasenko | b68979a | 2007-11-02 23:31:10 +0000 | [diff] [blame] | 558 | |
Lauri Kasanen | a48a29f | 2010-07-08 11:22:30 +0300 | [diff] [blame] | 559 | /* Goto modules location */ |
| 560 | xchdir(CONFIG_DEFAULT_MODULES_DIR); |
Serj Kalichev | e4e911e | 2010-12-26 01:56:19 +0100 | [diff] [blame] | 561 | uname(&G.uts); |
| 562 | xchdir(G.uts.release); |
Lauri Kasanen | a48a29f | 2010-07-08 11:22:30 +0300 | [diff] [blame] | 563 | |
Serj Kalichev | e4e911e | 2010-12-26 01:56:19 +0100 | [diff] [blame] | 564 | if (opt & OPT_LIST_ONLY) { |
Denys Vlasenko | 5dad4ae | 2011-02-15 02:17:31 +0100 | [diff] [blame] | 565 | int i; |
Pascal Bellard | b82b34e | 2010-06-07 01:16:45 +0200 | [diff] [blame] | 566 | char *colon, *tokens[2]; |
| 567 | parser_t *p = config_open2(CONFIG_DEFAULT_DEPMOD_FILE, xfopen_for_read); |
| 568 | |
Denys Vlasenko | 5dad4ae | 2011-02-15 02:17:31 +0100 | [diff] [blame] | 569 | for (i = 0; argv[i]; i++) |
| 570 | replace(argv[i], '-', '_'); |
| 571 | |
Pascal Bellard | b82b34e | 2010-06-07 01:16:45 +0200 | [diff] [blame] | 572 | while (config_read(p, tokens, 2, 1, "# \t", PARSE_NORMAL)) { |
| 573 | colon = last_char_is(tokens[0], ':'); |
| 574 | if (!colon) |
| 575 | continue; |
| 576 | *colon = '\0'; |
Pascal Bellard | b82b34e | 2010-06-07 01:16:45 +0200 | [diff] [blame] | 577 | if (!argv[0]) |
| 578 | puts(tokens[0]); |
| 579 | else { |
Denys Vlasenko | e998b08 | 2015-01-15 02:48:36 +0100 | [diff] [blame] | 580 | char name[MODULE_NAME_LEN]; |
| 581 | filename2modname( |
| 582 | bb_get_last_path_component_nostrip(tokens[0]), |
| 583 | name |
| 584 | ); |
Pascal Bellard | b82b34e | 2010-06-07 01:16:45 +0200 | [diff] [blame] | 585 | for (i = 0; argv[i]; i++) { |
| 586 | if (fnmatch(argv[i], name, 0) == 0) { |
| 587 | puts(tokens[0]); |
| 588 | } |
| 589 | } |
| 590 | } |
| 591 | } |
| 592 | return EXIT_SUCCESS; |
| 593 | } |
| 594 | |
| 595 | /* Yes, for some reason -l ignores -s... */ |
| 596 | if (opt & INSMOD_OPT_SYSLOG) |
| 597 | logmode = LOGMODE_SYSLOG; |
Robert Griebl | 236abbf | 2002-05-22 23:34:35 +0000 | [diff] [blame] | 598 | |
Denis Vlasenko | bb26db4 | 2008-10-31 02:04:28 +0000 | [diff] [blame] | 599 | if (!argv[0]) { |
Serj Kalichev | e4e911e | 2010-12-26 01:56:19 +0100 | [diff] [blame] | 600 | if (opt & OPT_REMOVE) { |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 601 | /* "modprobe -r" (w/o params). |
| 602 | * "If name is NULL, all unused modules marked |
| 603 | * autoclean will be removed". |
| 604 | */ |
Denys Vlasenko | ab19ede | 2009-11-11 21:05:42 +0100 | [diff] [blame] | 605 | if (bb_delete_module(NULL, O_NONBLOCK | O_EXCL) != 0) |
Denys Vlasenko | cd13974 | 2015-10-24 04:17:04 +0200 | [diff] [blame] | 606 | bb_perror_nomsg_and_die(); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 607 | } |
Denis Vlasenko | bb26db4 | 2008-10-31 02:04:28 +0000 | [diff] [blame] | 608 | return EXIT_SUCCESS; |
| 609 | } |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 610 | |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 611 | /* Retrieve module names of already loaded modules */ |
Denis Vlasenko | bb26db4 | 2008-10-31 02:04:28 +0000 | [diff] [blame] | 612 | { |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 613 | char *s; |
Denis Vlasenko | bb26db4 | 2008-10-31 02:04:28 +0000 | [diff] [blame] | 614 | parser_t *parser = config_open2("/proc/modules", fopen_for_read); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 615 | while (config_read(parser, &s, 1, 1, "# \t", PARSE_NORMAL & ~PARSE_GREEDY)) |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 616 | get_or_add_modentry(s)->flags |= MODULE_FLAG_LOADED; |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 617 | config_close(parser); |
| 618 | } |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 619 | |
Serj Kalichev | e4e911e | 2010-12-26 01:56:19 +0100 | [diff] [blame] | 620 | if (opt & (OPT_INSERT_ALL | OPT_REMOVE)) { |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 621 | /* Each argument is a module name */ |
| 622 | do { |
Mike Frysinger | 00ffaea | 2009-05-05 20:13:45 -0400 | [diff] [blame] | 623 | DBG("adding module %s", *argv); |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 624 | add_probe(*argv++); |
| 625 | } while (*argv); |
| 626 | } else { |
| 627 | /* First argument is module name, rest are parameters */ |
Mike Frysinger | 00ffaea | 2009-05-05 20:13:45 -0400 | [diff] [blame] | 628 | DBG("probing just module %s", *argv); |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 629 | add_probe(argv[0]); |
Denys Vlasenko | c5830bd | 2011-02-02 00:00:36 +0100 | [diff] [blame] | 630 | G.cmdline_mopts = parse_cmdline_module_options(argv, /*quote_spaces:*/ 1); |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 631 | } |
| 632 | |
| 633 | /* Happens if all requested modules are already loaded */ |
| 634 | if (G.probes == NULL) |
| 635 | return EXIT_SUCCESS; |
| 636 | |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 637 | read_config("/etc/modprobe.conf"); |
| 638 | read_config("/etc/modprobe.d"); |
| 639 | if (ENABLE_FEATURE_MODUTILS_SYMBOLS && G.need_symbols) |
| 640 | read_config("modules.symbols"); |
| 641 | load_modules_dep(); |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 642 | if (ENABLE_FEATURE_MODUTILS_ALIAS && G.num_unresolved_deps) { |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 643 | read_config("modules.alias"); |
| 644 | load_modules_dep(); |
| 645 | } |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 646 | |
Denys Vlasenko | ee47f6e | 2009-06-17 18:46:06 +0200 | [diff] [blame] | 647 | rc = 0; |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 648 | while ((me = llist_pop(&G.probes)) != NULL) { |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 649 | if (me->realnames == NULL) { |
Denys Vlasenko | ee47f6e | 2009-06-17 18:46:06 +0200 | [diff] [blame] | 650 | DBG("probing by module name"); |
Denis Vlasenko | a6b6f05 | 2009-03-06 22:48:11 +0000 | [diff] [blame] | 651 | /* This is not an alias. Literal names are blacklisted |
| 652 | * only if '-b' is given. |
| 653 | */ |
Serj Kalichev | e4e911e | 2010-12-26 01:56:19 +0100 | [diff] [blame] | 654 | if (!(opt & OPT_BLACKLIST) |
Denis Vlasenko | 0e2f362 | 2009-03-05 16:32:27 +0000 | [diff] [blame] | 655 | || !(me->flags & MODULE_FLAG_BLACKLISTED) |
| 656 | ) { |
Denys Vlasenko | ee47f6e | 2009-06-17 18:46:06 +0200 | [diff] [blame] | 657 | rc |= do_modprobe(me); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 658 | } |
Denys Vlasenko | ee47f6e | 2009-06-17 18:46:06 +0200 | [diff] [blame] | 659 | continue; |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 660 | } |
Denys Vlasenko | ee47f6e | 2009-06-17 18:46:06 +0200 | [diff] [blame] | 661 | |
| 662 | /* Probe all real names for the alias */ |
| 663 | do { |
| 664 | char *realname = llist_pop(&me->realnames); |
| 665 | struct module_entry *m2; |
| 666 | |
| 667 | DBG("probing alias %s by realname %s", me->modname, realname); |
| 668 | m2 = get_or_add_modentry(realname); |
| 669 | if (!(m2->flags & MODULE_FLAG_BLACKLISTED) |
| 670 | && (!(m2->flags & MODULE_FLAG_LOADED) |
Serj Kalichev | e4e911e | 2010-12-26 01:56:19 +0100 | [diff] [blame] | 671 | || (opt & (OPT_REMOVE | OPT_SHOW_DEPS))) |
Denys Vlasenko | ee47f6e | 2009-06-17 18:46:06 +0200 | [diff] [blame] | 672 | ) { |
| 673 | //TODO: we can pass "me" as 2nd param to do_modprobe, |
| 674 | //and make do_modprobe emit more meaningful error messages |
| 675 | //with alias name included, not just module name alias resolves to. |
| 676 | rc |= do_modprobe(m2); |
| 677 | } |
| 678 | free(realname); |
| 679 | } while (me->realnames != NULL); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 680 | } |
| 681 | |
Timo Teräs | 48dc80b | 2015-11-05 18:54:55 +0100 | [diff] [blame] | 682 | if (ENABLE_FEATURE_CLEAN_UP) |
| 683 | moddb_free(&G.db); |
| 684 | |
Denys Vlasenko | ee47f6e | 2009-06-17 18:46:06 +0200 | [diff] [blame] | 685 | return (rc != 0); |
Eric Andersen | 0139ca9 | 2001-07-22 23:01:03 +0000 | [diff] [blame] | 686 | } |