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