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