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