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 |
| 6 | * Copyright (c) 2008 Bernhard Fischer (initial depmod code) |
| 7 | * |
| 8 | * Licensed under GPLv2, see file LICENSE in this tarball for details. |
| 9 | */ |
| 10 | |
| 11 | #include "libbb.h" |
| 12 | #include "unarchive.h" |
| 13 | |
| 14 | #include <sys/utsname.h> /* uname() */ |
| 15 | #include <fnmatch.h> |
| 16 | |
Denis Vlasenko | 24a131e | 2008-07-09 15:30:57 +0000 | [diff] [blame^] | 17 | extern int init_module(void *module, unsigned long len, const char *options); |
| 18 | extern int delete_module(const char *module, unsigned flags); |
| 19 | extern int query_module(const char *name, int which, void *buf, size_t bufsize, size_t *ret); |
| 20 | |
| 21 | |
Denis Vlasenko | cee0dfc | 2008-07-06 11:11:35 +0000 | [diff] [blame] | 22 | #define dbg1_error_msg(...) ((void)0) |
| 23 | #define dbg2_error_msg(...) ((void)0) |
| 24 | //#define dbg1_error_msg(...) bb_error_msg(__VA_ARGS__) |
| 25 | //#define dbg2_error_msg(...) bb_error_msg(__VA_ARGS__) |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 26 | |
Denis Vlasenko | 24a131e | 2008-07-09 15:30:57 +0000 | [diff] [blame^] | 27 | #define DEPFILE_BB CONFIG_DEFAULT_DEPMOD_FILE".bb" |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 28 | |
| 29 | enum { |
| 30 | OPT_q = (1 << 0), /* be quiet */ |
| 31 | OPT_r = (1 << 1), /* module removal instead of loading */ |
| 32 | }; |
| 33 | |
| 34 | typedef struct module_info { |
| 35 | char *pathname; |
| 36 | char *desc; |
| 37 | } module_info; |
| 38 | |
| 39 | /* |
| 40 | * GLOBALS |
| 41 | */ |
| 42 | struct globals { |
| 43 | module_info *modinfo; |
| 44 | char *module_load_options; |
| 45 | int module_count; |
| 46 | int module_found_idx; |
| 47 | int stringbuf_idx; |
| 48 | char stringbuf[32 * 1024]; /* some modules have lots of stuff */ |
| 49 | /* for example, drivers/media/video/saa7134/saa7134.ko */ |
| 50 | }; |
| 51 | #define G (*ptr_to_globals) |
| 52 | #define modinfo (G.modinfo ) |
| 53 | #define module_count (G.module_count ) |
| 54 | #define module_found_idx (G.module_found_idx ) |
| 55 | #define module_load_options (G.module_load_options) |
| 56 | #define stringbuf_idx (G.stringbuf_idx ) |
| 57 | #define stringbuf (G.stringbuf ) |
| 58 | #define INIT_G() do { \ |
| 59 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
| 60 | } while (0) |
| 61 | |
| 62 | |
| 63 | static void appendc(char c) |
| 64 | { |
| 65 | if (stringbuf_idx < sizeof(stringbuf)) |
| 66 | stringbuf[stringbuf_idx++] = c; |
| 67 | } |
| 68 | |
Denis Vlasenko | 24a131e | 2008-07-09 15:30:57 +0000 | [diff] [blame^] | 69 | static void bksp(void) |
| 70 | { |
| 71 | if (stringbuf_idx) |
| 72 | stringbuf_idx--; |
| 73 | } |
| 74 | |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 75 | static void append(const char *s) |
| 76 | { |
| 77 | size_t len = strlen(s); |
| 78 | if (stringbuf_idx + len < sizeof(stringbuf)) { |
| 79 | memcpy(stringbuf + stringbuf_idx, s, len); |
| 80 | stringbuf_idx += len; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | static void reset_stringbuf(void) |
| 85 | { |
| 86 | stringbuf_idx = 0; |
| 87 | } |
| 88 | |
| 89 | static char* copy_stringbuf(void) |
| 90 | { |
| 91 | char *copy = xmalloc(stringbuf_idx); |
| 92 | return memcpy(copy, stringbuf, stringbuf_idx); |
| 93 | } |
| 94 | |
| 95 | static char* find_keyword(char *ptr, size_t len, const char *word) |
| 96 | { |
| 97 | int wlen; |
| 98 | |
| 99 | if (!ptr) /* happens if read_module cannot read it */ |
| 100 | return NULL; |
| 101 | |
| 102 | wlen = strlen(word); |
| 103 | len -= wlen - 1; |
| 104 | while ((ssize_t)len > 0) { |
| 105 | char *old = ptr; |
| 106 | /* search for the first char in word */ |
| 107 | ptr = memchr(ptr, *word, len); |
| 108 | if (ptr == NULL) /* no occurance left, done */ |
| 109 | break; |
| 110 | if (strncmp(ptr, word, wlen) == 0) |
| 111 | return ptr + wlen; /* found, return ptr past it */ |
| 112 | ++ptr; |
| 113 | len -= (ptr - old); |
| 114 | } |
| 115 | return NULL; |
| 116 | } |
| 117 | |
| 118 | static void replace(char *s, char what, char with) |
| 119 | { |
| 120 | while (*s) { |
| 121 | if (what == *s) |
| 122 | *s = with; |
| 123 | ++s; |
| 124 | } |
| 125 | } |
| 126 | |
Denis Vlasenko | 24a131e | 2008-07-09 15:30:57 +0000 | [diff] [blame^] | 127 | /* Take "word word", return malloced "word",NUL,"word",NUL,NUL */ |
| 128 | static char* str_2_list(const char *str) |
| 129 | { |
| 130 | int len = strlen(str) + 1; |
| 131 | char *dst = xmalloc(len + 1); |
| 132 | |
| 133 | dst[len] = '\0'; |
| 134 | memcpy(dst, str, len); |
| 135 | //TODO: protect against 2+ spaces: "word word" |
| 136 | replace(dst, ' ', '\0'); |
| 137 | return dst; |
| 138 | } |
| 139 | |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 140 | #if ENABLE_FEATURE_MODPROBE_SMALL_ZIPPED |
| 141 | static char *xmalloc_open_zipped_read_close(const char *fname, size_t *sizep) |
| 142 | { |
| 143 | size_t len; |
| 144 | char *image; |
| 145 | char *suffix; |
| 146 | |
| 147 | int fd = open_or_warn(fname, O_RDONLY); |
| 148 | if (fd < 0) |
| 149 | return NULL; |
| 150 | |
| 151 | suffix = strrchr(fname, '.'); |
| 152 | if (suffix) { |
| 153 | if (strcmp(suffix, ".gz") == 0) |
| 154 | fd = open_transformer(fd, unpack_gz_stream, "gunzip"); |
| 155 | else if (strcmp(suffix, ".bz2") == 0) |
| 156 | fd = open_transformer(fd, unpack_bz2_stream, "bunzip2"); |
| 157 | } |
| 158 | |
| 159 | len = (sizep) ? *sizep : 64 * 1024 * 1024; |
| 160 | image = xmalloc_read(fd, &len); |
| 161 | if (!image) |
| 162 | bb_perror_msg("read error from '%s'", fname); |
| 163 | close(fd); |
| 164 | |
| 165 | if (sizep) |
| 166 | *sizep = len; |
| 167 | return image; |
| 168 | } |
| 169 | # define read_module xmalloc_open_zipped_read_close |
| 170 | #else |
| 171 | # define read_module xmalloc_open_read_close |
| 172 | #endif |
| 173 | |
| 174 | /* We use error numbers in a loose translation... */ |
| 175 | static const char *moderror(int err) |
| 176 | { |
| 177 | switch (err) { |
| 178 | case ENOEXEC: |
| 179 | return "invalid module format"; |
| 180 | case ENOENT: |
| 181 | return "unknown symbol in module or invalid parameter"; |
| 182 | case ESRCH: |
| 183 | return "module has wrong symbol version"; |
| 184 | case EINVAL: /* "invalid parameter" */ |
| 185 | return "unknown symbol in module or invalid parameter" |
| 186 | + sizeof("unknown symbol in module or"); |
| 187 | default: |
| 188 | return strerror(err); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | static int load_module(const char *fname, const char *options) |
| 193 | { |
| 194 | #if 1 |
| 195 | int r; |
| 196 | size_t len = MAXINT(ssize_t); |
| 197 | char *module_image; |
| 198 | dbg1_error_msg("load_module('%s','%s')", fname, options); |
| 199 | |
| 200 | module_image = read_module(fname, &len); |
| 201 | r = (!module_image || init_module(module_image, len, options ? options : "") != 0); |
| 202 | free(module_image); |
| 203 | dbg1_error_msg("load_module:%d", r); |
| 204 | return r; /* 0 = success */ |
| 205 | #else |
| 206 | /* For testing */ |
| 207 | dbg1_error_msg("load_module('%s','%s')", fname, options); |
| 208 | return 1; |
| 209 | #endif |
| 210 | } |
| 211 | |
Denis Vlasenko | 24a131e | 2008-07-09 15:30:57 +0000 | [diff] [blame^] | 212 | static char* parse_module(const char *pathname) |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 213 | { |
| 214 | char *module_image; |
| 215 | char *ptr; |
| 216 | size_t len; |
| 217 | size_t pos; |
Denis Vlasenko | 24a131e | 2008-07-09 15:30:57 +0000 | [diff] [blame^] | 218 | dbg1_error_msg("parse_module('%s')", pathname); |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 219 | |
| 220 | /* Read (possibly compressed) module */ |
| 221 | len = 64 * 1024 * 1024; /* 64 Mb at most */ |
| 222 | module_image = read_module(pathname, &len); |
| 223 | |
| 224 | reset_stringbuf(); |
| 225 | |
| 226 | /* First desc line's format is |
Denis Vlasenko | 24a131e | 2008-07-09 15:30:57 +0000 | [diff] [blame^] | 227 | * "alias1 symbol:sym1 alias2 symbol:sym2" */ |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 228 | pos = 0; |
| 229 | while (1) { |
| 230 | ptr = find_keyword(module_image + pos, len - pos, "alias="); |
| 231 | if (!ptr) { |
| 232 | ptr = find_keyword(module_image + pos, len - pos, "__ksymtab_"); |
| 233 | if (!ptr) |
| 234 | break; |
| 235 | /* DOCME: __ksymtab_gpl and __ksymtab_strings occur |
| 236 | * in many modules. What do they mean? */ |
| 237 | if (strcmp(ptr, "gpl") != 0 && strcmp(ptr, "strings") != 0) { |
Denis Vlasenko | 24a131e | 2008-07-09 15:30:57 +0000 | [diff] [blame^] | 238 | dbg2_error_msg("alias:'symbol:%s'", ptr); |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 239 | append("symbol:"); |
| 240 | } |
| 241 | } else { |
Denis Vlasenko | 24a131e | 2008-07-09 15:30:57 +0000 | [diff] [blame^] | 242 | dbg2_error_msg("alias:'%s'", ptr); |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 243 | } |
| 244 | append(ptr); |
| 245 | appendc(' '); |
| 246 | pos = (ptr - module_image); |
| 247 | } |
Denis Vlasenko | 24a131e | 2008-07-09 15:30:57 +0000 | [diff] [blame^] | 248 | bksp(); /* remove last ' ' */ |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 249 | appendc('\0'); |
| 250 | |
Denis Vlasenko | 24a131e | 2008-07-09 15:30:57 +0000 | [diff] [blame^] | 251 | /* Second line: "dependency1 depandency2" */ |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 252 | ptr = find_keyword(module_image, len, "depends="); |
| 253 | if (ptr && *ptr) { |
| 254 | replace(ptr, ',', ' '); |
| 255 | replace(ptr, '-', '_'); |
| 256 | dbg2_error_msg("dep:'%s'", ptr); |
| 257 | append(ptr); |
| 258 | } |
Denis Vlasenko | 24a131e | 2008-07-09 15:30:57 +0000 | [diff] [blame^] | 259 | appendc('\0'); |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 260 | |
| 261 | free(module_image); |
| 262 | return copy_stringbuf(); |
| 263 | } |
| 264 | |
Denis Vlasenko | 24a131e | 2008-07-09 15:30:57 +0000 | [diff] [blame^] | 265 | static int pathname_matches_modname(const char *pathname, const char *modname) |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 266 | { |
| 267 | const char *fname = bb_get_last_path_component_nostrip(pathname); |
| 268 | const char *suffix = strrstr(fname, ".ko"); |
Denis Vlasenko | 24a131e | 2008-07-09 15:30:57 +0000 | [diff] [blame^] | 269 | //TODO: can do without malloc? |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 270 | char *name = xstrndup(fname, suffix - fname); |
Denis Vlasenko | 24a131e | 2008-07-09 15:30:57 +0000 | [diff] [blame^] | 271 | int r; |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 272 | replace(name, '-', '_'); |
Denis Vlasenko | 24a131e | 2008-07-09 15:30:57 +0000 | [diff] [blame^] | 273 | r = (strcmp(name, modname) == 0); |
| 274 | free(name); |
| 275 | return r; |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 276 | } |
| 277 | |
| 278 | static FAST_FUNC int fileAction(const char *pathname, |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 279 | struct stat *sb UNUSED_PARAM, |
Denis Vlasenko | 24a131e | 2008-07-09 15:30:57 +0000 | [diff] [blame^] | 280 | void *modname_to_match, |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 281 | int depth UNUSED_PARAM) |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 282 | { |
| 283 | int cur; |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 284 | const char *fname; |
| 285 | |
| 286 | pathname += 2; /* skip "./" */ |
| 287 | fname = bb_get_last_path_component_nostrip(pathname); |
| 288 | if (!strrstr(fname, ".ko")) { |
| 289 | dbg1_error_msg("'%s' is not a module", pathname); |
| 290 | return TRUE; /* not a module, continue search */ |
| 291 | } |
| 292 | |
| 293 | cur = module_count++; |
Denis Vlasenko | deeed59 | 2008-07-08 05:14:36 +0000 | [diff] [blame] | 294 | modinfo = xrealloc_vector(modinfo, 12, cur); |
Denis Vlasenko | 24a131e | 2008-07-09 15:30:57 +0000 | [diff] [blame^] | 295 | //TODO: use zeroing version of xrealloc_vector? |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 296 | modinfo[cur].pathname = xstrdup(pathname); |
| 297 | modinfo[cur].desc = NULL; |
| 298 | modinfo[cur+1].pathname = NULL; |
| 299 | modinfo[cur+1].desc = NULL; |
| 300 | |
Denis Vlasenko | 24a131e | 2008-07-09 15:30:57 +0000 | [diff] [blame^] | 301 | if (!pathname_matches_modname(fname, modname_to_match)) { |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 302 | dbg1_error_msg("'%s' module name doesn't match", pathname); |
| 303 | return TRUE; /* module name doesn't match, continue search */ |
| 304 | } |
| 305 | |
| 306 | dbg1_error_msg("'%s' module name matches", pathname); |
| 307 | module_found_idx = cur; |
Denis Vlasenko | 24a131e | 2008-07-09 15:30:57 +0000 | [diff] [blame^] | 308 | modinfo[cur].desc = parse_module(pathname); |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 309 | |
| 310 | if (!(option_mask32 & OPT_r)) { |
| 311 | if (load_module(pathname, module_load_options) == 0) { |
| 312 | /* Load was successful, there is nothing else to do. |
| 313 | * This can happen ONLY for "top-level" module load, |
| 314 | * not a dep, because deps dont do dirscan. */ |
| 315 | exit(EXIT_SUCCESS); |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 316 | } |
| 317 | } |
| 318 | |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 319 | return TRUE; |
| 320 | } |
| 321 | |
| 322 | static module_info* find_alias(const char *alias) |
| 323 | { |
| 324 | int i; |
| 325 | dbg1_error_msg("find_alias('%s')", alias); |
| 326 | |
| 327 | /* First try to find by name (cheaper) */ |
| 328 | i = 0; |
| 329 | while (modinfo[i].pathname) { |
Denis Vlasenko | 24a131e | 2008-07-09 15:30:57 +0000 | [diff] [blame^] | 330 | if (pathname_matches_modname(modinfo[i].pathname, alias)) { |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 331 | dbg1_error_msg("found '%s' in module '%s'", |
| 332 | alias, modinfo[i].pathname); |
| 333 | if (!modinfo[i].desc) |
Denis Vlasenko | 24a131e | 2008-07-09 15:30:57 +0000 | [diff] [blame^] | 334 | modinfo[i].desc = parse_module(modinfo[i].pathname); |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 335 | return &modinfo[i]; |
| 336 | } |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 337 | i++; |
| 338 | } |
| 339 | |
| 340 | /* Scan all module bodies, extract modinfo (it contains aliases) */ |
| 341 | i = 0; |
| 342 | while (modinfo[i].pathname) { |
| 343 | char *desc, *s; |
| 344 | if (!modinfo[i].desc) { |
Denis Vlasenko | 24a131e | 2008-07-09 15:30:57 +0000 | [diff] [blame^] | 345 | modinfo[i].desc = parse_module(modinfo[i].pathname); |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 346 | } |
Denis Vlasenko | 24a131e | 2008-07-09 15:30:57 +0000 | [diff] [blame^] | 347 | /* "alias1 symbol:sym1 alias2 symbol:sym2" */ |
| 348 | desc = str_2_list(modinfo[i].desc); |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 349 | /* Does matching substring exist? */ |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 350 | for (s = desc; *s; s += strlen(s) + 1) { |
Denis Vlasenko | 24a131e | 2008-07-09 15:30:57 +0000 | [diff] [blame^] | 351 | /* Aliases in module bodies can be defined with |
Denis Vlasenko | 58f59a2 | 2008-07-06 11:52:23 +0000 | [diff] [blame] | 352 | * shell patterns. Example: |
| 353 | * "pci:v000010DEd000000D9sv*sd*bc*sc*i*". |
| 354 | * Plain strcmp() won't catch that */ |
| 355 | if (fnmatch(s, alias, 0) == 0) { |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 356 | free(desc); |
| 357 | dbg1_error_msg("found alias '%s' in module '%s'", |
| 358 | alias, modinfo[i].pathname); |
| 359 | return &modinfo[i]; |
| 360 | } |
| 361 | } |
| 362 | free(desc); |
| 363 | i++; |
| 364 | } |
| 365 | dbg1_error_msg("find_alias '%s' returns NULL", alias); |
| 366 | return NULL; |
| 367 | } |
| 368 | |
| 369 | #if ENABLE_FEATURE_MODPROBE_SMALL_CHECK_ALREADY_LOADED |
| 370 | static int already_loaded(const char *name) |
| 371 | { |
| 372 | int ret = 0; |
| 373 | int len = strlen(name); |
| 374 | char *line; |
| 375 | FILE* modules; |
| 376 | |
| 377 | modules = xfopen("/proc/modules", "r"); |
| 378 | while ((line = xmalloc_fgets(modules)) != NULL) { |
| 379 | if (strncmp(line, name, len) == 0 && line[len] == ' ') { |
| 380 | free(line); |
| 381 | ret = 1; |
| 382 | break; |
| 383 | } |
| 384 | free(line); |
| 385 | } |
| 386 | fclose(modules); |
| 387 | return ret; |
| 388 | } |
| 389 | #else |
| 390 | #define already_loaded(name) is_rmmod |
| 391 | #endif |
| 392 | |
| 393 | /* |
| 394 | Given modules definition and module name (or alias, or symbol) |
| 395 | load/remove the module respecting dependencies |
| 396 | */ |
| 397 | #if !ENABLE_FEATURE_MODPROBE_SMALL_OPTIONS_ON_CMDLINE |
| 398 | #define process_module(a,b) process_module(a) |
| 399 | #define cmdline_options "" |
| 400 | #endif |
| 401 | static void process_module(char *name, const char *cmdline_options) |
| 402 | { |
| 403 | char *s, *deps, *options; |
| 404 | module_info *info; |
| 405 | int is_rmmod = (option_mask32 & OPT_r) != 0; |
| 406 | dbg1_error_msg("process_module('%s','%s')", name, cmdline_options); |
| 407 | |
| 408 | replace(name, '-', '_'); |
| 409 | |
| 410 | dbg1_error_msg("already_loaded:%d is_rmmod:%d", already_loaded(name), is_rmmod); |
| 411 | if (already_loaded(name) != is_rmmod) { |
| 412 | dbg1_error_msg("nothing to do for '%s'", name); |
| 413 | return; |
| 414 | } |
| 415 | |
| 416 | options = NULL; |
| 417 | if (!is_rmmod) { |
| 418 | char *opt_filename = xasprintf("/etc/modules/%s", name); |
| 419 | options = xmalloc_open_read_close(opt_filename, NULL); |
| 420 | if (options) |
| 421 | replace(options, '\n', ' '); |
| 422 | #if ENABLE_FEATURE_MODPROBE_SMALL_OPTIONS_ON_CMDLINE |
| 423 | if (cmdline_options) { |
| 424 | /* NB: cmdline_options always have one leading ' ' |
| 425 | * (see main()), we remove it here */ |
| 426 | char *op = xasprintf(options ? "%s %s" : "%s %s" + 3, |
| 427 | cmdline_options + 1, options); |
| 428 | free(options); |
| 429 | options = op; |
| 430 | } |
| 431 | #endif |
| 432 | free(opt_filename); |
| 433 | module_load_options = options; |
| 434 | dbg1_error_msg("process_module('%s'): options:'%s'", name, options); |
| 435 | } |
| 436 | |
| 437 | if (!module_count) { |
| 438 | /* Scan module directory. This is done only once. |
| 439 | * It will attempt module load, and will exit(EXIT_SUCCESS) |
| 440 | * on success. */ |
| 441 | module_found_idx = -1; |
| 442 | recursive_action(".", |
| 443 | ACTION_RECURSE, /* flags */ |
| 444 | fileAction, /* file action */ |
| 445 | NULL, /* dir action */ |
| 446 | name, /* user data */ |
| 447 | 0); /* depth */ |
| 448 | dbg1_error_msg("dirscan complete"); |
| 449 | /* Module was not found, or load failed, or is_rmmod */ |
| 450 | if (module_found_idx >= 0) { /* module was found */ |
| 451 | info = &modinfo[module_found_idx]; |
| 452 | } else { /* search for alias, not a plain module name */ |
| 453 | info = find_alias(name); |
| 454 | } |
| 455 | } else { |
| 456 | info = find_alias(name); |
| 457 | } |
| 458 | |
| 459 | /* rmmod? unload it by name */ |
| 460 | if (is_rmmod) { |
| 461 | if (delete_module(name, O_NONBLOCK|O_EXCL) != 0 |
| 462 | && !(option_mask32 & OPT_q) |
| 463 | ) { |
| 464 | bb_perror_msg("remove '%s'", name); |
| 465 | goto ret; |
| 466 | } |
| 467 | /* N.B. we do not stop here - |
| 468 | * continue to unload modules on which the module depends: |
| 469 | * "-r --remove: option causes modprobe to remove a module. |
| 470 | * If the modules it depends on are also unused, modprobe |
| 471 | * will try to remove them, too." */ |
| 472 | } |
| 473 | |
| 474 | if (!info) { /* both dirscan and find_alias found nothing */ |
| 475 | goto ret; |
| 476 | } |
| 477 | |
| 478 | /* Second line of desc contains dependencies */ |
Denis Vlasenko | 24a131e | 2008-07-09 15:30:57 +0000 | [diff] [blame^] | 479 | deps = str_2_list(info->desc + strlen(info->desc) + 1); |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 480 | |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 481 | /* Iterate thru dependencies, trying to (un)load them */ |
| 482 | for (s = deps; *s; s += strlen(s) + 1) { |
| 483 | //if (strcmp(name, s) != 0) // N.B. do loops exist? |
| 484 | dbg1_error_msg("recurse on dep '%s'", s); |
| 485 | process_module(s, NULL); |
| 486 | dbg1_error_msg("recurse on dep '%s' done", s); |
| 487 | } |
| 488 | free(deps); |
| 489 | |
| 490 | /* insmod -> load it */ |
| 491 | if (!is_rmmod) { |
| 492 | errno = 0; |
| 493 | if (load_module(info->pathname, options) != 0) { |
| 494 | if (EEXIST != errno) { |
Denis Vlasenko | 24a131e | 2008-07-09 15:30:57 +0000 | [diff] [blame^] | 495 | bb_error_msg("'%s': %s", |
| 496 | info->pathname, |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 497 | moderror(errno)); |
| 498 | } else { |
Denis Vlasenko | 24a131e | 2008-07-09 15:30:57 +0000 | [diff] [blame^] | 499 | dbg1_error_msg("'%s': %s", |
| 500 | info->pathname, |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 501 | moderror(errno)); |
| 502 | } |
| 503 | } |
| 504 | } |
| 505 | ret: |
| 506 | free(options); |
| 507 | //TODO: return load attempt result from process_module. |
| 508 | //If dep didn't load ok, continuing makes little sense. |
| 509 | } |
| 510 | #undef cmdline_options |
| 511 | |
| 512 | |
| 513 | /* For reference, module-init-tools-0.9.15-pre2 options: |
| 514 | |
| 515 | # insmod |
| 516 | Usage: insmod filename [args] |
| 517 | |
| 518 | # rmmod --help |
| 519 | Usage: rmmod [-fhswvV] modulename ... |
| 520 | -f (or --force) forces a module unload, and may crash your machine. |
| 521 | -s (or --syslog) says use syslog, not stderr |
| 522 | -v (or --verbose) enables more messages |
| 523 | -w (or --wait) begins a module removal even if it is used |
| 524 | and will stop new users from accessing the module (so it |
| 525 | should eventually fall to zero). |
| 526 | |
| 527 | # modprobe |
| 528 | Usage: modprobe [--verbose|--version|--config|--remove] filename [options] |
| 529 | |
| 530 | # depmod --help |
| 531 | depmod 0.9.15-pre2 -- part of module-init-tools |
| 532 | depmod -[aA] [-n -e -v -q -V -r -u] [-b basedirectory] [forced_version] |
| 533 | depmod [-n -e -v -q -r -u] [-F kernelsyms] module1.o module2.o ... |
| 534 | If no arguments (except options) are given, "depmod -a" is assumed |
| 535 | |
| 536 | depmod will output a dependancy list suitable for the modprobe utility. |
| 537 | |
| 538 | Options: |
| 539 | -a, --all Probe all modules |
| 540 | -n, --show Write the dependency file on stdout only |
| 541 | -b basedirectory |
| 542 | --basedir basedirectory Use an image of a module tree. |
| 543 | -F kernelsyms |
| 544 | --filesyms kernelsyms Use the file instead of the |
| 545 | current kernel symbols. |
| 546 | */ |
| 547 | |
| 548 | int modprobe_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 549 | int modprobe_main(int argc UNUSED_PARAM, char **argv) |
Denis Vlasenko | 671691c | 2008-07-04 10:25:44 +0000 | [diff] [blame] | 550 | { |
| 551 | struct utsname uts; |
| 552 | char applet0 = applet_name[0]; |
| 553 | USE_FEATURE_MODPROBE_SMALL_OPTIONS_ON_CMDLINE(char *options;) |
| 554 | |
| 555 | /* depmod is a stub */ |
| 556 | if ('d' == applet0) |
| 557 | return EXIT_SUCCESS; |
| 558 | |
| 559 | /* are we lsmod? -> just dump /proc/modules */ |
| 560 | if ('l' == applet0) { |
| 561 | xprint_and_close_file(xfopen("/proc/modules", "r")); |
| 562 | return EXIT_SUCCESS; |
| 563 | } |
| 564 | |
| 565 | INIT_G(); |
| 566 | |
| 567 | /* insmod, modprobe, rmmod require at least one argument */ |
| 568 | opt_complementary = "-1"; |
| 569 | /* only -q (quiet) and -r (rmmod), |
| 570 | * the rest are accepted and ignored (compat) */ |
| 571 | getopt32(argv, "qrfsvw"); |
| 572 | argv += optind; |
| 573 | |
| 574 | /* are we rmmod? -> simulate modprobe -r */ |
| 575 | if ('r' == applet0) { |
| 576 | option_mask32 |= OPT_r; |
| 577 | } |
| 578 | |
| 579 | /* goto modules directory */ |
| 580 | xchdir(CONFIG_DEFAULT_MODULES_DIR); |
| 581 | uname(&uts); /* never fails */ |
| 582 | xchdir(uts.release); |
| 583 | |
| 584 | #if ENABLE_FEATURE_MODPROBE_SMALL_OPTIONS_ON_CMDLINE |
| 585 | /* If not rmmod, parse possible module options given on command line. |
| 586 | * insmod/modprobe takes one module name, the rest are parameters. */ |
| 587 | options = NULL; |
| 588 | if ('r' != applet0) { |
| 589 | char **arg = argv; |
| 590 | while (*++arg) { |
| 591 | /* Enclose options in quotes */ |
| 592 | char *s = options; |
| 593 | options = xasprintf("%s \"%s\"", s ? s : "", *arg); |
| 594 | free(s); |
| 595 | *arg = NULL; |
| 596 | } |
| 597 | } |
| 598 | #else |
| 599 | if ('r' != applet0) |
| 600 | argv[1] = NULL; |
| 601 | #endif |
| 602 | |
| 603 | /* Load/remove modules. |
| 604 | * Only rmmod loops here, insmod/modprobe has only argv[0] */ |
| 605 | do { |
| 606 | process_module(*argv++, options); |
| 607 | } while (*argv); |
| 608 | |
| 609 | if (ENABLE_FEATURE_CLEAN_UP) { |
| 610 | USE_FEATURE_MODPROBE_SMALL_OPTIONS_ON_CMDLINE(free(options);) |
| 611 | } |
| 612 | return EXIT_SUCCESS; |
| 613 | } |