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