Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * simplified modprobe |
| 4 | * |
| 5 | * Copyright (c) 2008 Vladimir Dronnikov |
Bernhard Reutner-Fischer | 6c4dade | 2008-09-25 12:13:34 +0000 | [diff] [blame] | 6 | * Copyright (c) 2008 Bernhard Reutner-Fischer (initial depmod code) |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 7 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 8 | * Licensed under GPLv2, see file LICENSE in this source tree. |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 9 | */ |
Denys Vlasenko | 326edc3 | 2016-12-22 14:36:49 +0100 | [diff] [blame] | 10 | |
| 11 | /* config MODPROBE_SMALL is defined in Config.src to ensure better "make config" order */ |
| 12 | |
Denys Vlasenko | e32b64c | 2016-11-23 07:54:52 +0100 | [diff] [blame] | 13 | //config:config FEATURE_MODPROBE_SMALL_OPTIONS_ON_CMDLINE |
| 14 | //config: bool "Accept module options on modprobe command line" |
| 15 | //config: default y |
Explorer09 | 80172e4 | 2017-01-09 15:04:43 +0800 | [diff] [blame] | 16 | //config: depends on MODPROBE_SMALL && (INSMOD || MODPROBE) |
Denys Vlasenko | e32b64c | 2016-11-23 07:54:52 +0100 | [diff] [blame] | 17 | //config: help |
| 18 | //config: Allow insmod and modprobe take module options from command line. |
| 19 | //config: |
| 20 | //config:config FEATURE_MODPROBE_SMALL_CHECK_ALREADY_LOADED |
| 21 | //config: bool "Skip loading of already loaded modules" |
| 22 | //config: default y |
Explorer09 | 80172e4 | 2017-01-09 15:04:43 +0800 | [diff] [blame] | 23 | //config: depends on MODPROBE_SMALL && (DEPMOD || INSMOD || MODPROBE) |
Denys Vlasenko | e32b64c | 2016-11-23 07:54:52 +0100 | [diff] [blame] | 24 | //config: help |
| 25 | //config: Check if the module is already loaded. |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 26 | |
Denys Vlasenko | a1cd0d9 | 2016-12-23 15:12:27 +0100 | [diff] [blame] | 27 | //applet:IF_MODPROBE(IF_MODPROBE_SMALL(APPLET(modprobe, BB_DIR_SBIN, BB_SUID_DROP))) |
| 28 | //applet:IF_DEPMOD(IF_MODPROBE_SMALL(APPLET_ODDNAME(depmod, modprobe, BB_DIR_SBIN, BB_SUID_DROP, depmod))) |
| 29 | //applet:IF_INSMOD(IF_MODPROBE_SMALL(APPLET_ODDNAME(insmod, modprobe, BB_DIR_SBIN, BB_SUID_DROP, insmod))) |
| 30 | //applet:IF_LSMOD(IF_MODPROBE_SMALL(APPLET_ODDNAME(lsmod, modprobe, BB_DIR_SBIN, BB_SUID_DROP, lsmod))) |
| 31 | //applet:IF_RMMOD(IF_MODPROBE_SMALL(APPLET_ODDNAME(rmmod, modprobe, BB_DIR_SBIN, BB_SUID_DROP, rmmod))) |
Denys Vlasenko | c15613c | 2010-10-15 11:29:02 +0200 | [diff] [blame] | 32 | |
Denys Vlasenko | e32b64c | 2016-11-23 07:54:52 +0100 | [diff] [blame] | 33 | //kbuild:lib-$(CONFIG_MODPROBE_SMALL) += modprobe-small.o |
| 34 | |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 35 | #include "libbb.h" |
Denys Vlasenko | 043b1e5 | 2009-09-06 12:47:55 +0200 | [diff] [blame] | 36 | /* After libbb.h, since it needs sys/types.h on some systems */ |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 37 | #include <sys/utsname.h> /* uname() */ |
| 38 | #include <fnmatch.h> |
Mike Frysinger | 3a45b87 | 2016-09-15 12:16:33 +0200 | [diff] [blame] | 39 | #include <sys/syscall.h> |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 40 | |
Waldemar Brodkorb | 9c083f5 | 2016-12-26 20:07:59 +0100 | [diff] [blame] | 41 | #define init_module(mod, len, opts) syscall(__NR_init_module, mod, len, opts) |
| 42 | #define delete_module(mod, flags) syscall(__NR_delete_module, mod, flags) |
Mike Frysinger | 3a45b87 | 2016-09-15 12:16:33 +0200 | [diff] [blame] | 43 | #ifdef __NR_finit_module |
| 44 | # define finit_module(fd, uargs, flags) syscall(__NR_finit_module, fd, uargs, flags) |
| 45 | #endif |
Denys Vlasenko | 07e5555 | 2014-04-21 16:59:36 +0200 | [diff] [blame] | 46 | /* linux/include/linux/module.h has limit of 64 chars on module names */ |
| 47 | #undef MODULE_NAME_LEN |
| 48 | #define MODULE_NAME_LEN 64 |
Denis Vlasenko | 24a131e | 2008-07-09 15:30:57 +0000 | [diff] [blame] | 49 | |
| 50 | |
Denys Vlasenko | 5b3151c | 2010-09-25 14:37:06 +0200 | [diff] [blame] | 51 | #if 1 |
| 52 | # define dbg1_error_msg(...) ((void)0) |
| 53 | # define dbg2_error_msg(...) ((void)0) |
| 54 | #else |
| 55 | # define dbg1_error_msg(...) bb_error_msg(__VA_ARGS__) |
| 56 | # define dbg2_error_msg(...) bb_error_msg(__VA_ARGS__) |
| 57 | #endif |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 58 | |
Denis Vlasenko | 24a131e | 2008-07-09 15:30:57 +0000 | [diff] [blame] | 59 | #define DEPFILE_BB CONFIG_DEFAULT_DEPMOD_FILE".bb" |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 60 | |
Denys Vlasenko | 6bf52b6 | 2017-01-09 09:48:58 +0100 | [diff] [blame] | 61 | #define MOD_APPLET_CNT (ENABLE_MODPROBE + ENABLE_DEPMOD + ENABLE_INSMOD + ENABLE_LSMOD + ENABLE_RMMOD) |
Denys Vlasenko | e7b54d0 | 2017-01-09 16:31:21 +0100 | [diff] [blame] | 62 | |
| 63 | /* Do not bother if MODPROBE_SMALL=y but no applets selected. */ |
| 64 | /* The rest of the file is in this if block. */ |
| 65 | #if MOD_APPLET_CNT > 0 |
| 66 | |
| 67 | #define ONLY_APPLET (MOD_APPLET_CNT == 1) |
Explorer09 | 80172e4 | 2017-01-09 15:04:43 +0800 | [diff] [blame] | 68 | #define is_modprobe (ENABLE_MODPROBE && (ONLY_APPLET || applet_name[0] == 'm')) |
| 69 | #define is_depmod (ENABLE_DEPMOD && (ONLY_APPLET || applet_name[0] == 'd')) |
| 70 | #define is_insmod (ENABLE_INSMOD && (ONLY_APPLET || applet_name[0] == 'i')) |
| 71 | #define is_lsmod (ENABLE_LSMOD && (ONLY_APPLET || applet_name[0] == 'l')) |
| 72 | #define is_rmmod (ENABLE_RMMOD && (ONLY_APPLET || applet_name[0] == 'r')) |
| 73 | |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 74 | enum { |
| 75 | OPT_q = (1 << 0), /* be quiet */ |
| 76 | OPT_r = (1 << 1), /* module removal instead of loading */ |
| 77 | }; |
| 78 | |
| 79 | typedef struct module_info { |
| 80 | char *pathname; |
Denis Vlasenko | 7843699 | 2008-07-10 14:14:20 +0000 | [diff] [blame] | 81 | char *aliases; |
| 82 | char *deps; |
Denys Vlasenko | 5c3e060 | 2016-06-20 10:59:06 +0200 | [diff] [blame] | 83 | smallint open_read_failed; |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 84 | } module_info; |
| 85 | |
| 86 | /* |
| 87 | * GLOBALS |
| 88 | */ |
| 89 | struct globals { |
| 90 | module_info *modinfo; |
| 91 | char *module_load_options; |
Denis Vlasenko | 7f950a9 | 2008-07-10 14:14:45 +0000 | [diff] [blame] | 92 | smallint dep_bb_seen; |
Denis Vlasenko | 0e2c93f | 2008-07-10 14:16:11 +0000 | [diff] [blame] | 93 | smallint wrote_dep_bb_ok; |
Denys Vlasenko | 0c6914e | 2009-09-07 02:38:26 +0200 | [diff] [blame] | 94 | unsigned module_count; |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 95 | int module_found_idx; |
Denys Vlasenko | 0c6914e | 2009-09-07 02:38:26 +0200 | [diff] [blame] | 96 | unsigned stringbuf_idx; |
| 97 | unsigned stringbuf_size; |
| 98 | char *stringbuf; /* some modules have lots of stuff */ |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 99 | /* for example, drivers/media/video/saa7134/saa7134.ko */ |
Denys Vlasenko | 0c6914e | 2009-09-07 02:38:26 +0200 | [diff] [blame] | 100 | /* therefore having a fixed biggish buffer is not wise */ |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 101 | }; |
| 102 | #define G (*ptr_to_globals) |
| 103 | #define modinfo (G.modinfo ) |
Denis Vlasenko | 7f950a9 | 2008-07-10 14:14:45 +0000 | [diff] [blame] | 104 | #define dep_bb_seen (G.dep_bb_seen ) |
Denis Vlasenko | 0e2c93f | 2008-07-10 14:16:11 +0000 | [diff] [blame] | 105 | #define wrote_dep_bb_ok (G.wrote_dep_bb_ok ) |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 106 | #define module_count (G.module_count ) |
| 107 | #define module_found_idx (G.module_found_idx ) |
| 108 | #define module_load_options (G.module_load_options) |
| 109 | #define stringbuf_idx (G.stringbuf_idx ) |
Denys Vlasenko | 0c6914e | 2009-09-07 02:38:26 +0200 | [diff] [blame] | 110 | #define stringbuf_size (G.stringbuf_size ) |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 111 | #define stringbuf (G.stringbuf ) |
| 112 | #define INIT_G() do { \ |
| 113 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
| 114 | } while (0) |
| 115 | |
Denys Vlasenko | 0c6914e | 2009-09-07 02:38:26 +0200 | [diff] [blame] | 116 | static void append(const char *s) |
| 117 | { |
| 118 | unsigned len = strlen(s); |
| 119 | if (stringbuf_idx + len + 15 > stringbuf_size) { |
| 120 | stringbuf_size = stringbuf_idx + len + 127; |
| 121 | dbg2_error_msg("grow stringbuf to %u", stringbuf_size); |
| 122 | stringbuf = xrealloc(stringbuf, stringbuf_size); |
| 123 | } |
| 124 | memcpy(stringbuf + stringbuf_idx, s, len); |
| 125 | stringbuf_idx += len; |
| 126 | } |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 127 | |
| 128 | static void appendc(char c) |
| 129 | { |
Denys Vlasenko | 0c6914e | 2009-09-07 02:38:26 +0200 | [diff] [blame] | 130 | /* We appendc() only after append(), + 15 trick in append() |
| 131 | * makes it unnecessary to check for overflow here */ |
| 132 | stringbuf[stringbuf_idx++] = c; |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 133 | } |
| 134 | |
Denis Vlasenko | 24a131e | 2008-07-09 15:30:57 +0000 | [diff] [blame] | 135 | static void bksp(void) |
| 136 | { |
| 137 | if (stringbuf_idx) |
| 138 | stringbuf_idx--; |
| 139 | } |
| 140 | |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 141 | static void reset_stringbuf(void) |
| 142 | { |
| 143 | stringbuf_idx = 0; |
| 144 | } |
| 145 | |
| 146 | static char* copy_stringbuf(void) |
| 147 | { |
Denys Vlasenko | 0c6914e | 2009-09-07 02:38:26 +0200 | [diff] [blame] | 148 | char *copy = xzalloc(stringbuf_idx + 1); /* terminating NUL */ |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 149 | return memcpy(copy, stringbuf, stringbuf_idx); |
| 150 | } |
| 151 | |
| 152 | static char* find_keyword(char *ptr, size_t len, const char *word) |
| 153 | { |
Denis Vlasenko | e9ad84d | 2008-08-05 13:10:34 +0000 | [diff] [blame] | 154 | if (!ptr) /* happens if xmalloc_open_zipped_read_close cannot read it */ |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 155 | return NULL; |
| 156 | |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 157 | len -= strlen(word) - 1; |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 158 | while ((ssize_t)len > 0) { |
| 159 | char *old = ptr; |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 160 | char *after_word; |
| 161 | |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 162 | /* search for the first char in word */ |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 163 | ptr = memchr(ptr, word[0], len); |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 164 | if (ptr == NULL) /* no occurance left, done */ |
| 165 | break; |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 166 | after_word = is_prefixed_with(ptr, word); |
| 167 | if (after_word) |
| 168 | return after_word; /* found, return ptr past it */ |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 169 | ++ptr; |
| 170 | len -= (ptr - old); |
| 171 | } |
| 172 | return NULL; |
| 173 | } |
| 174 | |
| 175 | static void replace(char *s, char what, char with) |
| 176 | { |
| 177 | while (*s) { |
| 178 | if (what == *s) |
| 179 | *s = with; |
| 180 | ++s; |
| 181 | } |
| 182 | } |
| 183 | |
Denys Vlasenko | 07e5555 | 2014-04-21 16:59:36 +0200 | [diff] [blame] | 184 | static char *filename2modname(const char *filename, char *modname) |
| 185 | { |
| 186 | int i; |
Denys Vlasenko | 7885452 | 2015-01-01 19:02:40 +0100 | [diff] [blame] | 187 | const char *from; |
Denys Vlasenko | 07e5555 | 2014-04-21 16:59:36 +0200 | [diff] [blame] | 188 | |
Denys Vlasenko | 7885452 | 2015-01-01 19:02:40 +0100 | [diff] [blame] | 189 | // Disabled since otherwise "modprobe dir/name" would work |
| 190 | // as if it is "modprobe name". It is unclear why |
| 191 | // 'basenamization' was here in the first place. |
| 192 | //from = bb_get_last_path_component_nostrip(filename); |
| 193 | from = filename; |
Denys Vlasenko | 07e5555 | 2014-04-21 16:59:36 +0200 | [diff] [blame] | 194 | for (i = 0; i < (MODULE_NAME_LEN-1) && from[i] != '\0' && from[i] != '.'; i++) |
| 195 | modname[i] = (from[i] == '-') ? '_' : from[i]; |
| 196 | modname[i] = '\0'; |
| 197 | |
| 198 | return modname; |
| 199 | } |
| 200 | |
Denys Vlasenko | 1b67153 | 2015-01-11 17:46:56 +0100 | [diff] [blame] | 201 | static int pathname_matches_modname(const char *pathname, const char *modname) |
| 202 | { |
| 203 | int r; |
| 204 | char name[MODULE_NAME_LEN]; |
| 205 | filename2modname(bb_get_last_path_component_nostrip(pathname), name); |
| 206 | r = (strcmp(name, modname) == 0); |
| 207 | return r; |
| 208 | } |
| 209 | |
Denis Vlasenko | 24a131e | 2008-07-09 15:30:57 +0000 | [diff] [blame] | 210 | /* Take "word word", return malloced "word",NUL,"word",NUL,NUL */ |
| 211 | static char* str_2_list(const char *str) |
| 212 | { |
| 213 | int len = strlen(str) + 1; |
| 214 | char *dst = xmalloc(len + 1); |
| 215 | |
| 216 | dst[len] = '\0'; |
| 217 | memcpy(dst, str, len); |
| 218 | //TODO: protect against 2+ spaces: "word word" |
| 219 | replace(dst, ' ', '\0'); |
| 220 | return dst; |
| 221 | } |
| 222 | |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 223 | /* We use error numbers in a loose translation... */ |
| 224 | static const char *moderror(int err) |
| 225 | { |
| 226 | switch (err) { |
| 227 | case ENOEXEC: |
| 228 | return "invalid module format"; |
| 229 | case ENOENT: |
| 230 | return "unknown symbol in module or invalid parameter"; |
| 231 | case ESRCH: |
| 232 | return "module has wrong symbol version"; |
| 233 | case EINVAL: /* "invalid parameter" */ |
| 234 | return "unknown symbol in module or invalid parameter" |
| 235 | + sizeof("unknown symbol in module or"); |
| 236 | default: |
| 237 | return strerror(err); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | static int load_module(const char *fname, const char *options) |
| 242 | { |
| 243 | #if 1 |
| 244 | int r; |
| 245 | size_t len = MAXINT(ssize_t); |
| 246 | char *module_image; |
Mike Frysinger | 3a45b87 | 2016-09-15 12:16:33 +0200 | [diff] [blame] | 247 | |
| 248 | if (!options) |
| 249 | options = ""; |
| 250 | |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 251 | dbg1_error_msg("load_module('%s','%s')", fname, options); |
| 252 | |
Mike Frysinger | 3a45b87 | 2016-09-15 12:16:33 +0200 | [diff] [blame] | 253 | /* |
| 254 | * First we try finit_module if available. Some kernels are configured |
| 255 | * to only allow loading of modules off of secure storage (like a read- |
| 256 | * only rootfs) which needs the finit_module call. If it fails, we fall |
| 257 | * back to normal module loading to support compressed modules. |
| 258 | */ |
| 259 | r = 1; |
| 260 | # ifdef __NR_finit_module |
| 261 | { |
| 262 | int fd = open(fname, O_RDONLY | O_CLOEXEC); |
| 263 | if (fd >= 0) { |
| 264 | r = finit_module(fd, options, 0) != 0; |
| 265 | close(fd); |
| 266 | } |
| 267 | } |
| 268 | # endif |
| 269 | if (r != 0) { |
| 270 | module_image = xmalloc_open_zipped_read_close(fname, &len); |
| 271 | r = (!module_image || init_module(module_image, len, options) != 0); |
| 272 | free(module_image); |
| 273 | } |
| 274 | |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 275 | dbg1_error_msg("load_module:%d", r); |
| 276 | return r; /* 0 = success */ |
| 277 | #else |
| 278 | /* For testing */ |
| 279 | dbg1_error_msg("load_module('%s','%s')", fname, options); |
| 280 | return 1; |
| 281 | #endif |
| 282 | } |
| 283 | |
Denys Vlasenko | 5c3e060 | 2016-06-20 10:59:06 +0200 | [diff] [blame] | 284 | /* Returns !0 if open/read was unsuccessful */ |
| 285 | static int parse_module(module_info *info, const char *pathname) |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 286 | { |
| 287 | char *module_image; |
| 288 | char *ptr; |
| 289 | size_t len; |
| 290 | size_t pos; |
Denis Vlasenko | 24a131e | 2008-07-09 15:30:57 +0000 | [diff] [blame] | 291 | dbg1_error_msg("parse_module('%s')", pathname); |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 292 | |
| 293 | /* Read (possibly compressed) module */ |
Denys Vlasenko | 5c3e060 | 2016-06-20 10:59:06 +0200 | [diff] [blame] | 294 | errno = 0; |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 295 | len = 64 * 1024 * 1024; /* 64 Mb at most */ |
Denis Vlasenko | e9ad84d | 2008-08-05 13:10:34 +0000 | [diff] [blame] | 296 | module_image = xmalloc_open_zipped_read_close(pathname, &len); |
Denys Vlasenko | 094cc51 | 2011-01-17 14:58:27 +0100 | [diff] [blame] | 297 | /* module_image == NULL is ok here, find_keyword handles it */ |
Denis Vlasenko | 7843699 | 2008-07-10 14:14:20 +0000 | [diff] [blame] | 298 | //TODO: optimize redundant module body reads |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 299 | |
Denis Vlasenko | 7843699 | 2008-07-10 14:14:20 +0000 | [diff] [blame] | 300 | /* "alias1 symbol:sym1 alias2 symbol:sym2" */ |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 301 | reset_stringbuf(); |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 302 | pos = 0; |
| 303 | while (1) { |
Denys Vlasenko | 6116cb2 | 2014-04-19 16:17:27 +0200 | [diff] [blame] | 304 | unsigned start = stringbuf_idx; |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 305 | ptr = find_keyword(module_image + pos, len - pos, "alias="); |
| 306 | if (!ptr) { |
| 307 | ptr = find_keyword(module_image + pos, len - pos, "__ksymtab_"); |
| 308 | if (!ptr) |
| 309 | break; |
| 310 | /* DOCME: __ksymtab_gpl and __ksymtab_strings occur |
| 311 | * in many modules. What do they mean? */ |
Denis Vlasenko | 0e2c93f | 2008-07-10 14:16:11 +0000 | [diff] [blame] | 312 | if (strcmp(ptr, "gpl") == 0 || strcmp(ptr, "strings") == 0) |
| 313 | goto skip; |
| 314 | dbg2_error_msg("alias:'symbol:%s'", ptr); |
| 315 | append("symbol:"); |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 316 | } else { |
Denis Vlasenko | 24a131e | 2008-07-09 15:30:57 +0000 | [diff] [blame] | 317 | dbg2_error_msg("alias:'%s'", ptr); |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 318 | } |
| 319 | append(ptr); |
| 320 | appendc(' '); |
Denys Vlasenko | 6116cb2 | 2014-04-19 16:17:27 +0200 | [diff] [blame] | 321 | /* |
| 322 | * Don't add redundant aliases, such as: |
| 323 | * libcrc32c.ko symbol:crc32c symbol:crc32c |
| 324 | */ |
| 325 | if (start) { /* "if we aren't the first alias" */ |
| 326 | char *found, *last; |
| 327 | stringbuf[stringbuf_idx] = '\0'; |
| 328 | last = stringbuf + start; |
| 329 | /* |
| 330 | * String at last-1 is " symbol:crc32c " |
| 331 | * (with both leading and trailing spaces). |
| 332 | */ |
| 333 | if (strncmp(stringbuf, last, stringbuf_idx - start) == 0) |
| 334 | /* First alias matches us */ |
| 335 | found = stringbuf; |
| 336 | else |
| 337 | /* Does any other alias match? */ |
| 338 | found = strstr(stringbuf, last-1); |
| 339 | if (found < last-1) { |
| 340 | /* There is absolutely the same string before us */ |
| 341 | dbg2_error_msg("redundant:'%s'", last); |
| 342 | stringbuf_idx = start; |
| 343 | goto skip; |
| 344 | } |
| 345 | } |
Denis Vlasenko | 0e2c93f | 2008-07-10 14:16:11 +0000 | [diff] [blame] | 346 | skip: |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 347 | pos = (ptr - module_image); |
| 348 | } |
Denis Vlasenko | 24a131e | 2008-07-09 15:30:57 +0000 | [diff] [blame] | 349 | bksp(); /* remove last ' ' */ |
Denis Vlasenko | 7843699 | 2008-07-10 14:14:20 +0000 | [diff] [blame] | 350 | info->aliases = copy_stringbuf(); |
Denys Vlasenko | f9c814b | 2009-09-07 02:37:19 +0200 | [diff] [blame] | 351 | replace(info->aliases, '-', '_'); |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 352 | |
Denis Vlasenko | 7843699 | 2008-07-10 14:14:20 +0000 | [diff] [blame] | 353 | /* "dependency1 depandency2" */ |
| 354 | reset_stringbuf(); |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 355 | ptr = find_keyword(module_image, len, "depends="); |
| 356 | if (ptr && *ptr) { |
| 357 | replace(ptr, ',', ' '); |
| 358 | replace(ptr, '-', '_'); |
| 359 | dbg2_error_msg("dep:'%s'", ptr); |
| 360 | append(ptr); |
| 361 | } |
Denys Vlasenko | 5c3e060 | 2016-06-20 10:59:06 +0200 | [diff] [blame] | 362 | free(module_image); |
Denis Vlasenko | 7843699 | 2008-07-10 14:14:20 +0000 | [diff] [blame] | 363 | info->deps = copy_stringbuf(); |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 364 | |
Denys Vlasenko | 5c3e060 | 2016-06-20 10:59:06 +0200 | [diff] [blame] | 365 | info->open_read_failed = (module_image == NULL); |
| 366 | return info->open_read_failed; |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 367 | } |
| 368 | |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 369 | static FAST_FUNC int fileAction(const char *pathname, |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 370 | struct stat *sb UNUSED_PARAM, |
Denis Vlasenko | 24a131e | 2008-07-09 15:30:57 +0000 | [diff] [blame] | 371 | void *modname_to_match, |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 372 | int depth UNUSED_PARAM) |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 373 | { |
| 374 | int cur; |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 375 | const char *fname; |
Explorer09 | 80172e4 | 2017-01-09 15:04:43 +0800 | [diff] [blame] | 376 | bool is_remove = (ENABLE_RMMOD && ONLY_APPLET) |
| 377 | || ((ENABLE_RMMOD || ENABLE_MODPROBE) && (option_mask32 & OPT_r)); |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 378 | |
| 379 | pathname += 2; /* skip "./" */ |
| 380 | fname = bb_get_last_path_component_nostrip(pathname); |
| 381 | if (!strrstr(fname, ".ko")) { |
| 382 | dbg1_error_msg("'%s' is not a module", pathname); |
| 383 | return TRUE; /* not a module, continue search */ |
| 384 | } |
| 385 | |
| 386 | cur = module_count++; |
Denis Vlasenko | deeed59 | 2008-07-08 05:14:36 +0000 | [diff] [blame] | 387 | modinfo = xrealloc_vector(modinfo, 12, cur); |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 388 | modinfo[cur].pathname = xstrdup(pathname); |
Denis Vlasenko | 2784228 | 2008-08-04 13:20:36 +0000 | [diff] [blame] | 389 | /*modinfo[cur].aliases = NULL; - xrealloc_vector did it */ |
| 390 | /*modinfo[cur+1].pathname = NULL;*/ |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 391 | |
Denis Vlasenko | 24a131e | 2008-07-09 15:30:57 +0000 | [diff] [blame] | 392 | if (!pathname_matches_modname(fname, modname_to_match)) { |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 393 | dbg1_error_msg("'%s' module name doesn't match", pathname); |
| 394 | return TRUE; /* module name doesn't match, continue search */ |
| 395 | } |
| 396 | |
| 397 | dbg1_error_msg("'%s' module name matches", pathname); |
| 398 | module_found_idx = cur; |
Denys Vlasenko | 5c3e060 | 2016-06-20 10:59:06 +0200 | [diff] [blame] | 399 | if (parse_module(&modinfo[cur], pathname) != 0) |
| 400 | return TRUE; /* failed to open/read it, no point in trying loading */ |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 401 | |
Explorer09 | 80172e4 | 2017-01-09 15:04:43 +0800 | [diff] [blame] | 402 | if (!is_remove) { |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 403 | if (load_module(pathname, module_load_options) == 0) { |
| 404 | /* Load was successful, there is nothing else to do. |
| 405 | * This can happen ONLY for "top-level" module load, |
| 406 | * not a dep, because deps dont do dirscan. */ |
| 407 | exit(EXIT_SUCCESS); |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 408 | } |
| 409 | } |
| 410 | |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 411 | return TRUE; |
| 412 | } |
| 413 | |
Denis Vlasenko | 7f950a9 | 2008-07-10 14:14:45 +0000 | [diff] [blame] | 414 | static int load_dep_bb(void) |
Denis Vlasenko | 7843699 | 2008-07-10 14:14:20 +0000 | [diff] [blame] | 415 | { |
| 416 | char *line; |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 417 | FILE *fp = fopen_for_read(DEPFILE_BB); |
Denis Vlasenko | 7843699 | 2008-07-10 14:14:20 +0000 | [diff] [blame] | 418 | |
| 419 | if (!fp) |
Denis Vlasenko | 7f950a9 | 2008-07-10 14:14:45 +0000 | [diff] [blame] | 420 | return 0; |
| 421 | |
| 422 | dep_bb_seen = 1; |
| 423 | dbg1_error_msg("loading "DEPFILE_BB); |
| 424 | |
| 425 | /* Why? There is a rare scenario: we did not find modprobe.dep.bb, |
| 426 | * we scanned the dir and found no module by name, then we search |
| 427 | * for alias (full scan), and we decided to generate modprobe.dep.bb. |
| 428 | * But we see modprobe.dep.bb.new! Other modprobe is at work! |
| 429 | * We wait and other modprobe renames it to modprobe.dep.bb. |
| 430 | * Now we can use it. |
| 431 | * But we already have modinfo[] filled, and "module_count = 0" |
| 432 | * makes us start anew. Yes, we leak modinfo[].xxx pointers - |
| 433 | * there is not much of data there anyway. */ |
| 434 | module_count = 0; |
| 435 | memset(&modinfo[0], 0, sizeof(modinfo[0])); |
Denis Vlasenko | 7843699 | 2008-07-10 14:14:20 +0000 | [diff] [blame] | 436 | |
| 437 | while ((line = xmalloc_fgetline(fp)) != NULL) { |
| 438 | char* space; |
Denys Vlasenko | 90a9904 | 2009-09-06 02:36:23 +0200 | [diff] [blame] | 439 | char* linebuf; |
Denis Vlasenko | 7843699 | 2008-07-10 14:14:20 +0000 | [diff] [blame] | 440 | int cur; |
| 441 | |
| 442 | if (!line[0]) { |
| 443 | free(line); |
| 444 | continue; |
| 445 | } |
| 446 | space = strchrnul(line, ' '); |
| 447 | cur = module_count++; |
| 448 | modinfo = xrealloc_vector(modinfo, 12, cur); |
Denis Vlasenko | 2784228 | 2008-08-04 13:20:36 +0000 | [diff] [blame] | 449 | /*modinfo[cur+1].pathname = NULL; - xrealloc_vector did it */ |
Denis Vlasenko | 7843699 | 2008-07-10 14:14:20 +0000 | [diff] [blame] | 450 | modinfo[cur].pathname = line; /* we take ownership of malloced block here */ |
| 451 | if (*space) |
| 452 | *space++ = '\0'; |
| 453 | modinfo[cur].aliases = space; |
Denys Vlasenko | 90a9904 | 2009-09-06 02:36:23 +0200 | [diff] [blame] | 454 | linebuf = xmalloc_fgetline(fp); |
| 455 | modinfo[cur].deps = linebuf ? linebuf : xzalloc(1); |
Denis Vlasenko | 7843699 | 2008-07-10 14:14:20 +0000 | [diff] [blame] | 456 | if (modinfo[cur].deps[0]) { |
| 457 | /* deps are not "", so next line must be empty */ |
| 458 | line = xmalloc_fgetline(fp); |
| 459 | /* Refuse to work with damaged config file */ |
| 460 | if (line && line[0]) |
| 461 | bb_error_msg_and_die("error in %s at '%s'", DEPFILE_BB, line); |
| 462 | free(line); |
| 463 | } |
| 464 | } |
Denis Vlasenko | 7f950a9 | 2008-07-10 14:14:45 +0000 | [diff] [blame] | 465 | return 1; |
| 466 | } |
| 467 | |
| 468 | static int start_dep_bb_writeout(void) |
| 469 | { |
| 470 | int fd; |
| 471 | |
Denis Vlasenko | 0e2c93f | 2008-07-10 14:16:11 +0000 | [diff] [blame] | 472 | /* depmod -n: write result to stdout */ |
| 473 | if (applet_name[0] == 'd' && (option_mask32 & 1)) |
| 474 | return STDOUT_FILENO; |
| 475 | |
Denis Vlasenko | 7f950a9 | 2008-07-10 14:14:45 +0000 | [diff] [blame] | 476 | fd = open(DEPFILE_BB".new", O_WRONLY | O_CREAT | O_TRUNC | O_EXCL, 0644); |
| 477 | if (fd < 0) { |
| 478 | if (errno == EEXIST) { |
| 479 | int count = 5 * 20; |
| 480 | dbg1_error_msg(DEPFILE_BB".new exists, waiting for "DEPFILE_BB); |
| 481 | while (1) { |
| 482 | usleep(1000*1000 / 20); |
| 483 | if (load_dep_bb()) { |
| 484 | dbg1_error_msg(DEPFILE_BB" appeared"); |
| 485 | return -2; /* magic number */ |
| 486 | } |
| 487 | if (!--count) |
| 488 | break; |
| 489 | } |
| 490 | bb_error_msg("deleting stale %s", DEPFILE_BB".new"); |
| 491 | fd = open_or_warn(DEPFILE_BB".new", O_WRONLY | O_CREAT | O_TRUNC); |
| 492 | } |
| 493 | } |
| 494 | dbg1_error_msg("opened "DEPFILE_BB".new:%d", fd); |
| 495 | return fd; |
| 496 | } |
| 497 | |
| 498 | static void write_out_dep_bb(int fd) |
| 499 | { |
| 500 | int i; |
| 501 | FILE *fp; |
| 502 | |
| 503 | /* We want good error reporting. fdprintf is not good enough. */ |
Denys Vlasenko | a7ccdee | 2009-11-15 23:28:11 +0100 | [diff] [blame] | 504 | fp = xfdopen_for_write(fd); |
Denis Vlasenko | 7f950a9 | 2008-07-10 14:14:45 +0000 | [diff] [blame] | 505 | i = 0; |
| 506 | while (modinfo[i].pathname) { |
| 507 | fprintf(fp, "%s%s%s\n" "%s%s\n", |
| 508 | modinfo[i].pathname, modinfo[i].aliases[0] ? " " : "", modinfo[i].aliases, |
| 509 | modinfo[i].deps, modinfo[i].deps[0] ? "\n" : ""); |
| 510 | i++; |
| 511 | } |
| 512 | /* Badly formatted depfile is a no-no. Be paranoid. */ |
| 513 | errno = 0; |
Denis Vlasenko | 0e2c93f | 2008-07-10 14:16:11 +0000 | [diff] [blame] | 514 | if (ferror(fp) | fclose(fp)) /* | instead of || is intended */ |
Denis Vlasenko | 7f950a9 | 2008-07-10 14:14:45 +0000 | [diff] [blame] | 515 | goto err; |
Denis Vlasenko | 0e2c93f | 2008-07-10 14:16:11 +0000 | [diff] [blame] | 516 | |
| 517 | if (fd == STDOUT_FILENO) /* it was depmod -n */ |
| 518 | goto ok; |
| 519 | |
Denis Vlasenko | 7f950a9 | 2008-07-10 14:14:45 +0000 | [diff] [blame] | 520 | if (rename(DEPFILE_BB".new", DEPFILE_BB) != 0) { |
| 521 | err: |
Denys Vlasenko | 651a269 | 2010-03-23 16:25:17 +0100 | [diff] [blame] | 522 | bb_perror_msg("can't create '%s'", DEPFILE_BB); |
Denis Vlasenko | 7f950a9 | 2008-07-10 14:14:45 +0000 | [diff] [blame] | 523 | unlink(DEPFILE_BB".new"); |
| 524 | } else { |
Denis Vlasenko | 0e2c93f | 2008-07-10 14:16:11 +0000 | [diff] [blame] | 525 | ok: |
| 526 | wrote_dep_bb_ok = 1; |
Denis Vlasenko | 7f950a9 | 2008-07-10 14:14:45 +0000 | [diff] [blame] | 527 | dbg1_error_msg("created "DEPFILE_BB); |
| 528 | } |
Denis Vlasenko | 7843699 | 2008-07-10 14:14:20 +0000 | [diff] [blame] | 529 | } |
| 530 | |
Denys Vlasenko | 07e5555 | 2014-04-21 16:59:36 +0200 | [diff] [blame] | 531 | static module_info** find_alias(const char *alias) |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 532 | { |
| 533 | int i; |
Denis Vlasenko | 7f950a9 | 2008-07-10 14:14:45 +0000 | [diff] [blame] | 534 | int dep_bb_fd; |
Denys Vlasenko | 07e5555 | 2014-04-21 16:59:36 +0200 | [diff] [blame] | 535 | int infoidx; |
| 536 | module_info **infovec; |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 537 | dbg1_error_msg("find_alias('%s')", alias); |
| 538 | |
Denis Vlasenko | 7f950a9 | 2008-07-10 14:14:45 +0000 | [diff] [blame] | 539 | try_again: |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 540 | /* First try to find by name (cheaper) */ |
| 541 | i = 0; |
| 542 | while (modinfo[i].pathname) { |
Denis Vlasenko | 24a131e | 2008-07-09 15:30:57 +0000 | [diff] [blame] | 543 | if (pathname_matches_modname(modinfo[i].pathname, alias)) { |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 544 | dbg1_error_msg("found '%s' in module '%s'", |
| 545 | alias, modinfo[i].pathname); |
Denis Vlasenko | 7843699 | 2008-07-10 14:14:20 +0000 | [diff] [blame] | 546 | if (!modinfo[i].aliases) { |
| 547 | parse_module(&modinfo[i], modinfo[i].pathname); |
| 548 | } |
Denys Vlasenko | 07e5555 | 2014-04-21 16:59:36 +0200 | [diff] [blame] | 549 | infovec = xzalloc(2 * sizeof(infovec[0])); |
| 550 | infovec[0] = &modinfo[i]; |
| 551 | return infovec; |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 552 | } |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 553 | i++; |
| 554 | } |
| 555 | |
Denis Vlasenko | 7f950a9 | 2008-07-10 14:14:45 +0000 | [diff] [blame] | 556 | /* Ok, we definitely have to scan module bodies. This is a good |
| 557 | * moment to generate modprobe.dep.bb, if it does not exist yet */ |
| 558 | dep_bb_fd = dep_bb_seen ? -1 : start_dep_bb_writeout(); |
| 559 | if (dep_bb_fd == -2) /* modprobe.dep.bb appeared? */ |
| 560 | goto try_again; |
| 561 | |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 562 | /* Scan all module bodies, extract modinfo (it contains aliases) */ |
| 563 | i = 0; |
Denys Vlasenko | 07e5555 | 2014-04-21 16:59:36 +0200 | [diff] [blame] | 564 | infoidx = 0; |
| 565 | infovec = NULL; |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 566 | while (modinfo[i].pathname) { |
| 567 | char *desc, *s; |
Denis Vlasenko | 7843699 | 2008-07-10 14:14:20 +0000 | [diff] [blame] | 568 | if (!modinfo[i].aliases) { |
| 569 | parse_module(&modinfo[i], modinfo[i].pathname); |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 570 | } |
Denis Vlasenko | 24a131e | 2008-07-09 15:30:57 +0000 | [diff] [blame] | 571 | /* "alias1 symbol:sym1 alias2 symbol:sym2" */ |
Denis Vlasenko | 7843699 | 2008-07-10 14:14:20 +0000 | [diff] [blame] | 572 | desc = str_2_list(modinfo[i].aliases); |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 573 | /* Does matching substring exist? */ |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 574 | for (s = desc; *s; s += strlen(s) + 1) { |
Denis Vlasenko | 24a131e | 2008-07-09 15:30:57 +0000 | [diff] [blame] | 575 | /* Aliases in module bodies can be defined with |
Denis Vlasenko | 58f59a2 | 2008-07-06 11:52:23 +0000 | [diff] [blame] | 576 | * shell patterns. Example: |
| 577 | * "pci:v000010DEd000000D9sv*sd*bc*sc*i*". |
| 578 | * Plain strcmp() won't catch that */ |
| 579 | if (fnmatch(s, alias, 0) == 0) { |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 580 | dbg1_error_msg("found alias '%s' in module '%s'", |
| 581 | alias, modinfo[i].pathname); |
Denys Vlasenko | 07e5555 | 2014-04-21 16:59:36 +0200 | [diff] [blame] | 582 | infovec = xrealloc_vector(infovec, 1, infoidx); |
| 583 | infovec[infoidx++] = &modinfo[i]; |
Denis Vlasenko | 7f950a9 | 2008-07-10 14:14:45 +0000 | [diff] [blame] | 584 | break; |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 585 | } |
| 586 | } |
| 587 | free(desc); |
| 588 | i++; |
| 589 | } |
Denis Vlasenko | 7f950a9 | 2008-07-10 14:14:45 +0000 | [diff] [blame] | 590 | |
| 591 | /* Create module.dep.bb if needed */ |
| 592 | if (dep_bb_fd >= 0) { |
| 593 | write_out_dep_bb(dep_bb_fd); |
| 594 | } |
| 595 | |
Denys Vlasenko | 07e5555 | 2014-04-21 16:59:36 +0200 | [diff] [blame] | 596 | dbg1_error_msg("find_alias '%s' returns %d results", alias, infoidx); |
| 597 | return infovec; |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | #if ENABLE_FEATURE_MODPROBE_SMALL_CHECK_ALREADY_LOADED |
Denis Vlasenko | 0f99d49 | 2008-07-24 23:38:04 +0000 | [diff] [blame] | 601 | // TODO: open only once, invent config_rewind() |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 602 | static int already_loaded(const char *name) |
| 603 | { |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 604 | int ret; |
Denys Vlasenko | 1fd8442 | 2015-02-12 16:18:39 +0100 | [diff] [blame] | 605 | char *line; |
| 606 | FILE *fp; |
| 607 | |
| 608 | ret = 5 * 2; |
| 609 | again: |
| 610 | fp = fopen_for_read("/proc/modules"); |
| 611 | if (!fp) |
| 612 | return 0; |
Denys Vlasenko | 1fd8442 | 2015-02-12 16:18:39 +0100 | [diff] [blame] | 613 | while ((line = xmalloc_fgetline(fp)) != NULL) { |
| 614 | char *live; |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 615 | char *after_name; |
Denys Vlasenko | 1fd8442 | 2015-02-12 16:18:39 +0100 | [diff] [blame] | 616 | |
| 617 | // Examples from kernel 3.14.6: |
| 618 | //pcspkr 12718 0 - Live 0xffffffffa017e000 |
| 619 | //snd_timer 28690 2 snd_seq,snd_pcm, Live 0xffffffffa025e000 |
| 620 | //i915 801405 2 - Live 0xffffffffa0096000 |
Denys Vlasenko | 8dff01d | 2015-03-12 17:48:34 +0100 | [diff] [blame] | 621 | after_name = is_prefixed_with(line, name); |
| 622 | if (!after_name || *after_name != ' ') { |
Denys Vlasenko | 1fd8442 | 2015-02-12 16:18:39 +0100 | [diff] [blame] | 623 | free(line); |
| 624 | continue; |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 625 | } |
Denys Vlasenko | 1fd8442 | 2015-02-12 16:18:39 +0100 | [diff] [blame] | 626 | live = strstr(line, " Live"); |
| 627 | free(line); |
| 628 | if (!live) { |
| 629 | /* State can be Unloading, Loading, or Live. |
| 630 | * modprobe must not return prematurely if we see "Loading": |
| 631 | * it can cause further programs to assume load completed, |
| 632 | * but it did not (yet)! |
| 633 | * Wait up to 5*20 ms for it to resolve. |
| 634 | */ |
| 635 | ret -= 2; |
| 636 | if (ret == 0) |
| 637 | break; /* huh? report as "not loaded" */ |
| 638 | fclose(fp); |
| 639 | usleep(20*1000); |
| 640 | goto again; |
| 641 | } |
| 642 | ret = 1; |
| 643 | break; |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 644 | } |
Denys Vlasenko | 1fd8442 | 2015-02-12 16:18:39 +0100 | [diff] [blame] | 645 | fclose(fp); |
| 646 | |
Denys Vlasenko | 402afe1 | 2015-02-13 11:53:33 +0100 | [diff] [blame] | 647 | return ret & 1; |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 648 | } |
| 649 | #else |
Denys Vlasenko | 68c048f | 2015-02-07 20:44:46 +0100 | [diff] [blame] | 650 | #define already_loaded(name) 0 |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 651 | #endif |
| 652 | |
Denys Vlasenko | 68c048f | 2015-02-07 20:44:46 +0100 | [diff] [blame] | 653 | static int rmmod(const char *filename) |
| 654 | { |
| 655 | int r; |
| 656 | char modname[MODULE_NAME_LEN]; |
| 657 | |
| 658 | filename2modname(filename, modname); |
| 659 | r = delete_module(modname, O_NONBLOCK | O_EXCL); |
| 660 | dbg1_error_msg("delete_module('%s', O_NONBLOCK | O_EXCL):%d", modname, r); |
| 661 | if (r != 0 && !(option_mask32 & OPT_q)) { |
| 662 | bb_perror_msg("remove '%s'", modname); |
| 663 | } |
| 664 | return r; |
| 665 | } |
| 666 | |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 667 | /* |
Denis Vlasenko | 0e2c93f | 2008-07-10 14:16:11 +0000 | [diff] [blame] | 668 | * Given modules definition and module name (or alias, or symbol) |
| 669 | * load/remove the module respecting dependencies. |
| 670 | * NB: also called by depmod with bogus name "/", |
| 671 | * just in order to force modprobe.dep.bb creation. |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 672 | */ |
| 673 | #if !ENABLE_FEATURE_MODPROBE_SMALL_OPTIONS_ON_CMDLINE |
| 674 | #define process_module(a,b) process_module(a) |
| 675 | #define cmdline_options "" |
| 676 | #endif |
Denys Vlasenko | 5c3e060 | 2016-06-20 10:59:06 +0200 | [diff] [blame] | 677 | static int process_module(char *name, const char *cmdline_options) |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 678 | { |
| 679 | char *s, *deps, *options; |
Denys Vlasenko | 07e5555 | 2014-04-21 16:59:36 +0200 | [diff] [blame] | 680 | module_info **infovec; |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 681 | module_info *info; |
Denys Vlasenko | 07e5555 | 2014-04-21 16:59:36 +0200 | [diff] [blame] | 682 | int infoidx; |
Explorer09 | 80172e4 | 2017-01-09 15:04:43 +0800 | [diff] [blame] | 683 | bool is_remove = (ENABLE_RMMOD && ONLY_APPLET) |
| 684 | || ((ENABLE_RMMOD || ENABLE_MODPROBE) && (option_mask32 & OPT_r)); |
Denys Vlasenko | 5c3e060 | 2016-06-20 10:59:06 +0200 | [diff] [blame] | 685 | int exitcode = EXIT_SUCCESS; |
Denys Vlasenko | 07e5555 | 2014-04-21 16:59:36 +0200 | [diff] [blame] | 686 | |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 687 | dbg1_error_msg("process_module('%s','%s')", name, cmdline_options); |
| 688 | |
| 689 | replace(name, '-', '_'); |
| 690 | |
Denys Vlasenko | 68c048f | 2015-02-07 20:44:46 +0100 | [diff] [blame] | 691 | dbg1_error_msg("already_loaded:%d is_remove:%d", already_loaded(name), is_remove); |
| 692 | |
Explorer09 | 80172e4 | 2017-01-09 15:04:43 +0800 | [diff] [blame] | 693 | if (is_rmmod) { |
| 694 | /* Does not remove dependencies, no need to scan, just remove. |
Denys Vlasenko | 68c048f | 2015-02-07 20:44:46 +0100 | [diff] [blame] | 695 | * (compat note: this allows and strips .ko suffix) |
| 696 | */ |
| 697 | rmmod(name); |
Denys Vlasenko | 5c3e060 | 2016-06-20 10:59:06 +0200 | [diff] [blame] | 698 | return EXIT_SUCCESS; |
Denys Vlasenko | 68c048f | 2015-02-07 20:44:46 +0100 | [diff] [blame] | 699 | } |
| 700 | |
Denys Vlasenko | 07e5555 | 2014-04-21 16:59:36 +0200 | [diff] [blame] | 701 | /* |
Denys Vlasenko | 68c048f | 2015-02-07 20:44:46 +0100 | [diff] [blame] | 702 | * We used to have "is_remove != already_loaded(name)" check here, but |
Denys Vlasenko | 07e5555 | 2014-04-21 16:59:36 +0200 | [diff] [blame] | 703 | * modprobe -r pci:v00008086d00007010sv00000000sd00000000bc01sc01i80 |
| 704 | * won't unload modules (there are more than one) |
| 705 | * which have this alias. |
| 706 | */ |
Denys Vlasenko | 68c048f | 2015-02-07 20:44:46 +0100 | [diff] [blame] | 707 | if (!is_remove && already_loaded(name)) { |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 708 | dbg1_error_msg("nothing to do for '%s'", name); |
Denys Vlasenko | 5c3e060 | 2016-06-20 10:59:06 +0200 | [diff] [blame] | 709 | return EXIT_SUCCESS; |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | options = NULL; |
Denys Vlasenko | 68c048f | 2015-02-07 20:44:46 +0100 | [diff] [blame] | 713 | if (!is_remove) { |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 714 | char *opt_filename = xasprintf("/etc/modules/%s", name); |
| 715 | options = xmalloc_open_read_close(opt_filename, NULL); |
| 716 | if (options) |
| 717 | replace(options, '\n', ' '); |
| 718 | #if ENABLE_FEATURE_MODPROBE_SMALL_OPTIONS_ON_CMDLINE |
| 719 | if (cmdline_options) { |
| 720 | /* NB: cmdline_options always have one leading ' ' |
| 721 | * (see main()), we remove it here */ |
| 722 | char *op = xasprintf(options ? "%s %s" : "%s %s" + 3, |
| 723 | cmdline_options + 1, options); |
| 724 | free(options); |
| 725 | options = op; |
| 726 | } |
| 727 | #endif |
| 728 | free(opt_filename); |
| 729 | module_load_options = options; |
| 730 | dbg1_error_msg("process_module('%s'): options:'%s'", name, options); |
| 731 | } |
| 732 | |
| 733 | if (!module_count) { |
| 734 | /* Scan module directory. This is done only once. |
| 735 | * It will attempt module load, and will exit(EXIT_SUCCESS) |
Denys Vlasenko | 07e5555 | 2014-04-21 16:59:36 +0200 | [diff] [blame] | 736 | * on success. |
| 737 | */ |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 738 | module_found_idx = -1; |
| 739 | recursive_action(".", |
| 740 | ACTION_RECURSE, /* flags */ |
| 741 | fileAction, /* file action */ |
| 742 | NULL, /* dir action */ |
| 743 | name, /* user data */ |
Denys Vlasenko | 07e5555 | 2014-04-21 16:59:36 +0200 | [diff] [blame] | 744 | 0 /* depth */ |
| 745 | ); |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 746 | dbg1_error_msg("dirscan complete"); |
Denys Vlasenko | 68c048f | 2015-02-07 20:44:46 +0100 | [diff] [blame] | 747 | /* Module was not found, or load failed, or is_remove */ |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 748 | if (module_found_idx >= 0) { /* module was found */ |
Denys Vlasenko | 07e5555 | 2014-04-21 16:59:36 +0200 | [diff] [blame] | 749 | infovec = xzalloc(2 * sizeof(infovec[0])); |
| 750 | infovec[0] = &modinfo[module_found_idx]; |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 751 | } else { /* search for alias, not a plain module name */ |
Denys Vlasenko | 07e5555 | 2014-04-21 16:59:36 +0200 | [diff] [blame] | 752 | infovec = find_alias(name); |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 753 | } |
| 754 | } else { |
Denys Vlasenko | 07e5555 | 2014-04-21 16:59:36 +0200 | [diff] [blame] | 755 | infovec = find_alias(name); |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 756 | } |
| 757 | |
Denys Vlasenko | 3c75b1c | 2015-01-11 17:40:30 +0100 | [diff] [blame] | 758 | if (!infovec) { |
| 759 | /* both dirscan and find_alias found nothing */ |
Explorer09 | 80172e4 | 2017-01-09 15:04:43 +0800 | [diff] [blame] | 760 | if (!is_remove && !is_depmod) /* it wasn't rmmod or depmod */ |
Denys Vlasenko | 3c75b1c | 2015-01-11 17:40:30 +0100 | [diff] [blame] | 761 | bb_error_msg("module '%s' not found", name); |
| 762 | //TODO: _and_die()? or should we continue (un)loading modules listed on cmdline? |
| 763 | goto ret; |
| 764 | } |
| 765 | |
Denys Vlasenko | 07e5555 | 2014-04-21 16:59:36 +0200 | [diff] [blame] | 766 | /* There can be more than one module for the given alias. For example, |
| 767 | * "pci:v00008086d00007010sv00000000sd00000000bc01sc01i80" matches |
| 768 | * ata_piix because it has alias "pci:v00008086d00007010sv*sd*bc*sc*i*" |
| 769 | * and ata_generic, it has alias "pci:v*d*sv*sd*bc01sc01i*" |
| 770 | * Standard modprobe loads them both. We achieve it by returning |
| 771 | * a *list* of modinfo pointers from find_alias(). |
| 772 | */ |
Denys Vlasenko | 6332151 | 2009-10-08 22:54:41 +0200 | [diff] [blame] | 773 | |
Denys Vlasenko | 68c048f | 2015-02-07 20:44:46 +0100 | [diff] [blame] | 774 | /* modprobe -r? unload module(s) */ |
| 775 | if (is_remove) { |
Denys Vlasenko | 07e5555 | 2014-04-21 16:59:36 +0200 | [diff] [blame] | 776 | infoidx = 0; |
| 777 | while ((info = infovec[infoidx++]) != NULL) { |
Denys Vlasenko | 68c048f | 2015-02-07 20:44:46 +0100 | [diff] [blame] | 778 | int r = rmmod(bb_get_last_path_component_nostrip(info->pathname)); |
Denys Vlasenko | 07e5555 | 2014-04-21 16:59:36 +0200 | [diff] [blame] | 779 | if (r != 0) { |
Denys Vlasenko | 68c048f | 2015-02-07 20:44:46 +0100 | [diff] [blame] | 780 | goto ret; /* error */ |
Denys Vlasenko | 07e5555 | 2014-04-21 16:59:36 +0200 | [diff] [blame] | 781 | } |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 782 | } |
Denys Vlasenko | 06a98e3 | 2012-09-25 20:37:38 +0200 | [diff] [blame] | 783 | /* modprobe -r: we do not stop here - |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 784 | * continue to unload modules on which the module depends: |
| 785 | * "-r --remove: option causes modprobe to remove a module. |
| 786 | * If the modules it depends on are also unused, modprobe |
Denys Vlasenko | 06a98e3 | 2012-09-25 20:37:38 +0200 | [diff] [blame] | 787 | * will try to remove them, too." |
| 788 | */ |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 789 | } |
| 790 | |
Denys Vlasenko | 07e5555 | 2014-04-21 16:59:36 +0200 | [diff] [blame] | 791 | infoidx = 0; |
| 792 | while ((info = infovec[infoidx++]) != NULL) { |
| 793 | /* Iterate thru dependencies, trying to (un)load them */ |
| 794 | deps = str_2_list(info->deps); |
| 795 | for (s = deps; *s; s += strlen(s) + 1) { |
| 796 | //if (strcmp(name, s) != 0) // N.B. do loops exist? |
| 797 | dbg1_error_msg("recurse on dep '%s'", s); |
| 798 | process_module(s, NULL); |
| 799 | dbg1_error_msg("recurse on dep '%s' done", s); |
| 800 | } |
| 801 | free(deps); |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 802 | |
Denys Vlasenko | 68c048f | 2015-02-07 20:44:46 +0100 | [diff] [blame] | 803 | if (is_remove) |
Denys Vlasenko | 07e5555 | 2014-04-21 16:59:36 +0200 | [diff] [blame] | 804 | continue; |
| 805 | |
| 806 | /* We are modprobe: load it */ |
| 807 | if (options && strstr(options, "blacklist")) { |
Denis Vlasenko | 1ad4db1 | 2008-11-12 00:09:58 +0000 | [diff] [blame] | 808 | dbg1_error_msg("'%s': blacklisted", info->pathname); |
Denys Vlasenko | 07e5555 | 2014-04-21 16:59:36 +0200 | [diff] [blame] | 809 | continue; |
| 810 | } |
Denys Vlasenko | 5c3e060 | 2016-06-20 10:59:06 +0200 | [diff] [blame] | 811 | if (info->open_read_failed) { |
| 812 | /* We already tried it, didn't work. Don't try load again */ |
| 813 | exitcode = EXIT_FAILURE; |
| 814 | continue; |
| 815 | } |
Denys Vlasenko | 07e5555 | 2014-04-21 16:59:36 +0200 | [diff] [blame] | 816 | errno = 0; |
| 817 | if (load_module(info->pathname, options) != 0) { |
| 818 | if (EEXIST != errno) { |
| 819 | bb_error_msg("'%s': %s", |
| 820 | info->pathname, |
| 821 | moderror(errno)); |
| 822 | } else { |
| 823 | dbg1_error_msg("'%s': %s", |
| 824 | info->pathname, |
| 825 | moderror(errno)); |
| 826 | } |
Denys Vlasenko | 5c3e060 | 2016-06-20 10:59:06 +0200 | [diff] [blame] | 827 | exitcode = EXIT_FAILURE; |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 828 | } |
| 829 | } |
| 830 | ret: |
Denys Vlasenko | 07e5555 | 2014-04-21 16:59:36 +0200 | [diff] [blame] | 831 | free(infovec); |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 832 | free(options); |
Denys Vlasenko | 5c3e060 | 2016-06-20 10:59:06 +0200 | [diff] [blame] | 833 | |
| 834 | return exitcode; |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 835 | } |
| 836 | #undef cmdline_options |
| 837 | |
| 838 | |
Denis Vlasenko | 0e2c93f | 2008-07-10 14:16:11 +0000 | [diff] [blame] | 839 | /* For reference, module-init-tools v3.4 options: |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 840 | |
| 841 | # insmod |
| 842 | Usage: insmod filename [args] |
| 843 | |
| 844 | # rmmod --help |
| 845 | Usage: rmmod [-fhswvV] modulename ... |
Denis Vlasenko | 0e2c93f | 2008-07-10 14:16:11 +0000 | [diff] [blame] | 846 | -f (or --force) forces a module unload, and may crash your |
| 847 | machine. This requires the Forced Module Removal option |
| 848 | when the kernel was compiled. |
| 849 | -h (or --help) prints this help text |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 850 | -s (or --syslog) says use syslog, not stderr |
| 851 | -v (or --verbose) enables more messages |
Denis Vlasenko | 0e2c93f | 2008-07-10 14:16:11 +0000 | [diff] [blame] | 852 | -V (or --version) prints the version code |
Denis Vlasenko | 35d8c47 | 2008-08-05 07:59:25 +0000 | [diff] [blame] | 853 | -w (or --wait) begins module removal even if it is used |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 854 | and will stop new users from accessing the module (so it |
| 855 | should eventually fall to zero). |
| 856 | |
| 857 | # modprobe |
Denis Vlasenko | 0e2c93f | 2008-07-10 14:16:11 +0000 | [diff] [blame] | 858 | Usage: modprobe [-v] [-V] [-C config-file] [-n] [-i] [-q] [-b] |
| 859 | [-o <modname>] [ --dump-modversions ] <modname> [parameters...] |
| 860 | modprobe -r [-n] [-i] [-v] <modulename> ... |
| 861 | modprobe -l -t <dirname> [ -a <modulename> ...] |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 862 | |
| 863 | # depmod --help |
Denis Vlasenko | 0e2c93f | 2008-07-10 14:16:11 +0000 | [diff] [blame] | 864 | depmod 3.4 -- part of module-init-tools |
| 865 | depmod -[aA] [-n -e -v -q -V -r -u] |
| 866 | [-b basedirectory] [forced_version] |
| 867 | depmod [-n -e -v -q -r -u] [-F kernelsyms] module1.ko module2.ko ... |
| 868 | If no arguments (except options) are given, "depmod -a" is assumed. |
Denys Vlasenko | bf2af9a | 2009-05-25 04:15:37 +0200 | [diff] [blame] | 869 | depmod will output a dependency list suitable for the modprobe utility. |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 870 | Options: |
Denis Vlasenko | 35d8c47 | 2008-08-05 07:59:25 +0000 | [diff] [blame] | 871 | -a, --all Probe all modules |
| 872 | -A, --quick Only does the work if there's a new module |
| 873 | -n, --show Write the dependency file on stdout only |
| 874 | -e, --errsyms Report not supplied symbols |
| 875 | -V, --version Print the release version |
| 876 | -v, --verbose Enable verbose mode |
| 877 | -h, --help Print this usage message |
Denis Vlasenko | 0e2c93f | 2008-07-10 14:16:11 +0000 | [diff] [blame] | 878 | The following options are useful for people managing distributions: |
| 879 | -b basedirectory |
Denis Vlasenko | 35d8c47 | 2008-08-05 07:59:25 +0000 | [diff] [blame] | 880 | --basedir basedirectory |
| 881 | Use an image of a module tree |
Denis Vlasenko | 0e2c93f | 2008-07-10 14:16:11 +0000 | [diff] [blame] | 882 | -F kernelsyms |
Denis Vlasenko | 35d8c47 | 2008-08-05 07:59:25 +0000 | [diff] [blame] | 883 | --filesyms kernelsyms |
| 884 | Use the file instead of the current kernel symbols |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 885 | */ |
| 886 | |
Pascal Bellard | b82b34e | 2010-06-07 01:16:45 +0200 | [diff] [blame] | 887 | //usage:#if ENABLE_MODPROBE_SMALL |
Denys Vlasenko | 1a5e11c | 2010-10-16 01:56:41 +0200 | [diff] [blame] | 888 | |
| 889 | //usage:#define depmod_trivial_usage NOUSAGE_STR |
| 890 | //usage:#define depmod_full_usage "" |
| 891 | |
| 892 | //usage:#define lsmod_trivial_usage |
| 893 | //usage: "" |
| 894 | //usage:#define lsmod_full_usage "\n\n" |
| 895 | //usage: "List the currently loaded kernel modules" |
| 896 | |
| 897 | //usage:#define insmod_trivial_usage |
| 898 | //usage: IF_FEATURE_2_4_MODULES("[OPTIONS] MODULE ") |
| 899 | //usage: IF_NOT_FEATURE_2_4_MODULES("FILE ") |
| 900 | //usage: "[SYMBOL=VALUE]..." |
| 901 | //usage:#define insmod_full_usage "\n\n" |
Denys Vlasenko | 5fd3ddf | 2014-04-19 15:04:39 +0200 | [diff] [blame] | 902 | //usage: "Load kernel module" |
Denys Vlasenko | 1a5e11c | 2010-10-16 01:56:41 +0200 | [diff] [blame] | 903 | //usage: IF_FEATURE_2_4_MODULES( "\n" |
Denys Vlasenko | 1a5e11c | 2010-10-16 01:56:41 +0200 | [diff] [blame] | 904 | //usage: "\n -f Force module to load into the wrong kernel version" |
| 905 | //usage: "\n -k Make module autoclean-able" |
| 906 | //usage: "\n -v Verbose" |
| 907 | //usage: "\n -q Quiet" |
| 908 | //usage: "\n -L Lock: prevent simultaneous loads" |
| 909 | //usage: IF_FEATURE_INSMOD_LOAD_MAP( |
| 910 | //usage: "\n -m Output load map to stdout" |
| 911 | //usage: ) |
| 912 | //usage: "\n -x Don't export externs" |
| 913 | //usage: ) |
| 914 | |
| 915 | //usage:#define rmmod_trivial_usage |
| 916 | //usage: "[-wfa] [MODULE]..." |
| 917 | //usage:#define rmmod_full_usage "\n\n" |
| 918 | //usage: "Unload kernel modules\n" |
Denys Vlasenko | 1a5e11c | 2010-10-16 01:56:41 +0200 | [diff] [blame] | 919 | //usage: "\n -w Wait until the module is no longer used" |
| 920 | //usage: "\n -f Force unload" |
| 921 | //usage: "\n -a Remove all unused modules (recursively)" |
| 922 | //usage: |
| 923 | //usage:#define rmmod_example_usage |
| 924 | //usage: "$ rmmod tulip\n" |
| 925 | |
Pascal Bellard | b82b34e | 2010-06-07 01:16:45 +0200 | [diff] [blame] | 926 | //usage:#define modprobe_trivial_usage |
Denys Vlasenko | 5fd3ddf | 2014-04-19 15:04:39 +0200 | [diff] [blame] | 927 | //usage: "[-qfwrsv] MODULE [SYMBOL=VALUE]..." |
Pascal Bellard | b82b34e | 2010-06-07 01:16:45 +0200 | [diff] [blame] | 928 | //usage:#define modprobe_full_usage "\n\n" |
Denys Vlasenko | 6642676 | 2011-06-05 03:58:28 +0200 | [diff] [blame] | 929 | //usage: " -r Remove MODULE (stacks) or do autoclean" |
Pascal Bellard | b82b34e | 2010-06-07 01:16:45 +0200 | [diff] [blame] | 930 | //usage: "\n -q Quiet" |
| 931 | //usage: "\n -v Verbose" |
| 932 | //usage: "\n -f Force" |
| 933 | //usage: "\n -w Wait for unload" |
| 934 | //usage: "\n -s Report via syslog instead of stderr" |
Denys Vlasenko | 1a5e11c | 2010-10-16 01:56:41 +0200 | [diff] [blame] | 935 | |
| 936 | //usage:#endif |
Pascal Bellard | b82b34e | 2010-06-07 01:16:45 +0200 | [diff] [blame] | 937 | |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 938 | int modprobe_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 939 | int modprobe_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 940 | { |
Denys Vlasenko | 6bf52b6 | 2017-01-09 09:48:58 +0100 | [diff] [blame] | 941 | #if ENABLE_MODPROBE || ENABLE_INSMOD || ENABLE_RMMOD |
Denys Vlasenko | 5c3e060 | 2016-06-20 10:59:06 +0200 | [diff] [blame] | 942 | int exitcode; |
Denys Vlasenko | 6bf52b6 | 2017-01-09 09:48:58 +0100 | [diff] [blame] | 943 | #endif |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 944 | struct utsname uts; |
Explorer09 | 80172e4 | 2017-01-09 15:04:43 +0800 | [diff] [blame] | 945 | IF_FEATURE_MODPROBE_SMALL_OPTIONS_ON_CMDLINE(char *options = NULL;) |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 946 | |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 947 | /* are we lsmod? -> just dump /proc/modules */ |
Explorer09 | 80172e4 | 2017-01-09 15:04:43 +0800 | [diff] [blame] | 948 | if (is_lsmod) { |
Denis Vlasenko | 5415c85 | 2008-07-21 23:05:26 +0000 | [diff] [blame] | 949 | xprint_and_close_file(xfopen_for_read("/proc/modules")); |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 950 | return EXIT_SUCCESS; |
| 951 | } |
| 952 | |
| 953 | INIT_G(); |
| 954 | |
Denis Vlasenko | 0e2c93f | 2008-07-10 14:16:11 +0000 | [diff] [blame] | 955 | /* Prevent ugly corner cases with no modules at all */ |
| 956 | modinfo = xzalloc(sizeof(modinfo[0])); |
| 957 | |
Explorer09 | 80172e4 | 2017-01-09 15:04:43 +0800 | [diff] [blame] | 958 | if (!is_insmod) { |
Denis Vlasenko | 1c781cc | 2008-09-06 14:14:01 +0000 | [diff] [blame] | 959 | /* Goto modules directory */ |
| 960 | xchdir(CONFIG_DEFAULT_MODULES_DIR); |
| 961 | } |
Denis Vlasenko | 0e2c93f | 2008-07-10 14:16:11 +0000 | [diff] [blame] | 962 | uname(&uts); /* never fails */ |
| 963 | |
Denys Vlasenko | 6bf52b6 | 2017-01-09 09:48:58 +0100 | [diff] [blame] | 964 | /* depmod? */ |
| 965 | if ((MOD_APPLET_CNT == 2 && ENABLE_DEPMOD && ENABLE_LSMOD) |
| 966 | /* ^^^^only depmod and lsmod is configured^^^^^^^^^^^^^^^ */ |
| 967 | /* note: we already know here it is not lsmod (handled before) */ |
| 968 | || is_depmod |
| 969 | ) { |
Denis Vlasenko | 0e2c93f | 2008-07-10 14:16:11 +0000 | [diff] [blame] | 970 | /* Supported: |
| 971 | * -n: print result to stdout |
| 972 | * -a: process all modules (default) |
| 973 | * optional VERSION parameter |
| 974 | * Ignored: |
| 975 | * -A: do work only if a module is newer than depfile |
| 976 | * -e: report any symbols which a module needs |
| 977 | * which are not supplied by other modules or the kernel |
| 978 | * -F FILE: System.map (symbols for -e) |
| 979 | * -q, -r, -u: noop? |
| 980 | * Not supported: |
| 981 | * -b BASEDIR: (TODO!) modules are in |
| 982 | * $BASEDIR/lib/modules/$VERSION |
| 983 | * -v: human readable deps to stdout |
| 984 | * -V: version (don't want to support it - people may depend |
| 985 | * on it as an indicator of "standard" depmod) |
| 986 | * -h: help (well duh) |
| 987 | * module1.o module2.o parameters (just ignored for now) |
| 988 | */ |
| 989 | getopt32(argv, "na" "AeF:qru" /* "b:vV", NULL */, NULL); |
| 990 | argv += optind; |
| 991 | /* if (argv[0] && argv[1]) bb_show_usage(); */ |
| 992 | /* Goto $VERSION directory */ |
| 993 | xchdir(argv[0] ? argv[0] : uts.release); |
| 994 | /* Force full module scan by asking to find a bogus module. |
| 995 | * This will generate modules.dep.bb as a side effect. */ |
| 996 | process_module((char*)"/", NULL); |
| 997 | return !wrote_dep_bb_ok; |
| 998 | } |
| 999 | |
Explorer09 | 80172e4 | 2017-01-09 15:04:43 +0800 | [diff] [blame] | 1000 | #if ENABLE_MODPROBE || ENABLE_INSMOD || ENABLE_RMMOD |
| 1001 | /* modprobe, insmod, rmmod require at least one argument */ |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 1002 | opt_complementary = "-1"; |
| 1003 | /* only -q (quiet) and -r (rmmod), |
| 1004 | * the rest are accepted and ignored (compat) */ |
Felipe Contreras | 428bd2d | 2012-01-31 14:55:15 +0100 | [diff] [blame] | 1005 | getopt32(argv, "qrfsvwb"); |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 1006 | argv += optind; |
| 1007 | |
Explorer09 | 80172e4 | 2017-01-09 15:04:43 +0800 | [diff] [blame] | 1008 | if (!is_insmod) { |
Denis Vlasenko | 1c781cc | 2008-09-06 14:14:01 +0000 | [diff] [blame] | 1009 | /* Goto $VERSION directory */ |
| 1010 | xchdir(uts.release); |
| 1011 | } |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 1012 | |
Explorer09 | 80172e4 | 2017-01-09 15:04:43 +0800 | [diff] [blame] | 1013 | /* are we rmmod? -> simulate modprobe -r, but don't bother the flag if |
| 1014 | * there're no other applets here */ |
| 1015 | if (is_rmmod) { |
| 1016 | if (!ONLY_APPLET) |
| 1017 | option_mask32 |= OPT_r; |
| 1018 | } else if (!ENABLE_MODPROBE || !(option_mask32 & OPT_r)) { |
| 1019 | # if ENABLE_FEATURE_MODPROBE_SMALL_OPTIONS_ON_CMDLINE |
Denys Vlasenko | 68c048f | 2015-02-07 20:44:46 +0100 | [diff] [blame] | 1020 | /* If not rmmod/-r, parse possible module options given on command line. |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 1021 | * insmod/modprobe takes one module name, the rest are parameters. */ |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 1022 | char **arg = argv; |
| 1023 | while (*++arg) { |
| 1024 | /* Enclose options in quotes */ |
| 1025 | char *s = options; |
| 1026 | options = xasprintf("%s \"%s\"", s ? s : "", *arg); |
| 1027 | free(s); |
| 1028 | *arg = NULL; |
| 1029 | } |
Explorer09 | 80172e4 | 2017-01-09 15:04:43 +0800 | [diff] [blame] | 1030 | # else |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 1031 | argv[1] = NULL; |
Explorer09 | 80172e4 | 2017-01-09 15:04:43 +0800 | [diff] [blame] | 1032 | # endif |
| 1033 | } |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 1034 | |
Explorer09 | 80172e4 | 2017-01-09 15:04:43 +0800 | [diff] [blame] | 1035 | if (is_insmod) { |
Denis Vlasenko | 1c781cc | 2008-09-06 14:14:01 +0000 | [diff] [blame] | 1036 | size_t len; |
| 1037 | void *map; |
| 1038 | |
| 1039 | len = MAXINT(ssize_t); |
Denys Vlasenko | e9d12b5 | 2011-01-09 20:57:52 +0100 | [diff] [blame] | 1040 | map = xmalloc_open_zipped_read_close(*argv, &len); |
Denys Vlasenko | 094cc51 | 2011-01-17 14:58:27 +0100 | [diff] [blame] | 1041 | if (!map) |
| 1042 | bb_perror_msg_and_die("can't read '%s'", *argv); |
Denis Vlasenko | 1c781cc | 2008-09-06 14:14:01 +0000 | [diff] [blame] | 1043 | if (init_module(map, len, |
Explorer09 | 80172e4 | 2017-01-09 15:04:43 +0800 | [diff] [blame] | 1044 | (IF_FEATURE_MODPROBE_SMALL_OPTIONS_ON_CMDLINE(options ? options : ) "") |
Denys Vlasenko | e9d12b5 | 2011-01-09 20:57:52 +0100 | [diff] [blame] | 1045 | ) != 0 |
| 1046 | ) { |
Denis Vlasenko | 1f63229 | 2009-04-13 02:25:40 +0000 | [diff] [blame] | 1047 | bb_error_msg_and_die("can't insert '%s': %s", |
Denis Vlasenko | 1c781cc | 2008-09-06 14:14:01 +0000 | [diff] [blame] | 1048 | *argv, moderror(errno)); |
Denys Vlasenko | e9d12b5 | 2011-01-09 20:57:52 +0100 | [diff] [blame] | 1049 | } |
Denys Vlasenko | 5c3e060 | 2016-06-20 10:59:06 +0200 | [diff] [blame] | 1050 | return EXIT_SUCCESS; |
Denis Vlasenko | 1c781cc | 2008-09-06 14:14:01 +0000 | [diff] [blame] | 1051 | } |
| 1052 | |
Denis Vlasenko | 7f950a9 | 2008-07-10 14:14:45 +0000 | [diff] [blame] | 1053 | /* Try to load modprobe.dep.bb */ |
Explorer09 | 80172e4 | 2017-01-09 15:04:43 +0800 | [diff] [blame] | 1054 | if (!is_rmmod) { |
Denys Vlasenko | 68c048f | 2015-02-07 20:44:46 +0100 | [diff] [blame] | 1055 | load_dep_bb(); |
Denys Vlasenko | 5c3e060 | 2016-06-20 10:59:06 +0200 | [diff] [blame] | 1056 | } |
Denis Vlasenko | 7843699 | 2008-07-10 14:14:20 +0000 | [diff] [blame] | 1057 | |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 1058 | /* Load/remove modules. |
Denys Vlasenko | 68c048f | 2015-02-07 20:44:46 +0100 | [diff] [blame] | 1059 | * Only rmmod/modprobe -r loops here, insmod/modprobe has only argv[0] */ |
Denys Vlasenko | 5c3e060 | 2016-06-20 10:59:06 +0200 | [diff] [blame] | 1060 | exitcode = EXIT_SUCCESS; |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 1061 | do { |
Denys Vlasenko | 5c3e060 | 2016-06-20 10:59:06 +0200 | [diff] [blame] | 1062 | exitcode |= process_module(*argv, options); |
Denys Vlasenko | 5b3151c | 2010-09-25 14:37:06 +0200 | [diff] [blame] | 1063 | } while (*++argv); |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 1064 | |
| 1065 | if (ENABLE_FEATURE_CLEAN_UP) { |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 1066 | IF_FEATURE_MODPROBE_SMALL_OPTIONS_ON_CMDLINE(free(options);) |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 1067 | } |
Denys Vlasenko | 5c3e060 | 2016-06-20 10:59:06 +0200 | [diff] [blame] | 1068 | return exitcode; |
Explorer09 | 80172e4 | 2017-01-09 15:04:43 +0800 | [diff] [blame] | 1069 | #endif /* MODPROBE || INSMOD || RMMOD */ |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 1070 | } |
Denys Vlasenko | e7b54d0 | 2017-01-09 16:31:21 +0100 | [diff] [blame] | 1071 | |
| 1072 | #endif /* MOD_APPLET_CNT > 0 */ |