Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Erik Andersen | 3d7e341 | 1999-12-16 23:04:20 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Mini rmmod implementation for busybox |
| 4 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Erik Andersen | 3d7e341 | 1999-12-16 23:04:20 +0000 | [diff] [blame] | 6 | * |
Bernhard Reutner-Fischer | deda6a5 | 2006-06-03 19:35:15 +0000 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Erik Andersen | 3d7e341 | 1999-12-16 23:04:20 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 10 | #include "libbb.h" |
Bernhard Reutner-Fischer | 4fec346 | 2008-05-27 10:13:54 +0000 | [diff] [blame] | 11 | |
| 12 | #ifdef __UCLIBC__ |
| 13 | extern int delete_module(const char *module, unsigned int flags); |
| 14 | #else |
| 15 | # include <sys/syscall.h> |
| 16 | # define delete_module(mod, flags) syscall(__NR_delete_module, mod, flags) |
| 17 | #endif |
Erik Andersen | 3d7e341 | 1999-12-16 23:04:20 +0000 | [diff] [blame] | 18 | |
Denis Vlasenko | e324184 | 2007-08-13 10:36:25 +0000 | [diff] [blame] | 19 | #if ENABLE_FEATURE_2_6_MODULES |
"Vladimir N. Oleynik" | 8c44f01 | 2005-11-28 15:54:22 +0000 | [diff] [blame] | 20 | static inline void filename2modname(char *modname, const char *afterslash) |
Eric Andersen | c0693ed | 2004-07-20 10:05:13 +0000 | [diff] [blame] | 21 | { |
Eric Andersen | c0693ed | 2004-07-20 10:05:13 +0000 | [diff] [blame] | 22 | unsigned int i; |
"Vladimir N. Oleynik" | 8c44f01 | 2005-11-28 15:54:22 +0000 | [diff] [blame] | 23 | int kr_chk = 1; |
Rob Landley | abfe107 | 2006-08-28 19:40:08 +0000 | [diff] [blame] | 24 | |
| 25 | if (ENABLE_FEATURE_2_4_MODULES |
| 26 | && get_linux_version_code() <= KERNEL_VERSION(2,6,0)) |
| 27 | kr_chk = 0; |
Eric Andersen | c0693ed | 2004-07-20 10:05:13 +0000 | [diff] [blame] | 28 | |
| 29 | /* Convert to underscores, stop at first . */ |
| 30 | for (i = 0; afterslash[i] && afterslash[i] != '.'; i++) { |
Rob Landley | 5221987 | 2005-11-27 19:01:53 +0000 | [diff] [blame] | 31 | if (kr_chk && (afterslash[i] == '-')) |
Eric Andersen | c0693ed | 2004-07-20 10:05:13 +0000 | [diff] [blame] | 32 | modname[i] = '_'; |
| 33 | else |
| 34 | modname[i] = afterslash[i]; |
| 35 | } |
| 36 | modname[i] = '\0'; |
| 37 | } |
Rob Landley | abfe107 | 2006-08-28 19:40:08 +0000 | [diff] [blame] | 38 | #else |
| 39 | void filename2modname(char *modname, const char *afterslash); |
Eric Andersen | c0693ed | 2004-07-20 10:05:13 +0000 | [diff] [blame] | 40 | #endif |
| 41 | |
Rob Landley | abfe107 | 2006-08-28 19:40:08 +0000 | [diff] [blame] | 42 | // There really should be a header file for this... |
| 43 | |
| 44 | int query_module(const char *name, int which, void *buf, |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 45 | size_t bufsize, size_t *ret); |
Rob Landley | abfe107 | 2006-08-28 19:40:08 +0000 | [diff] [blame] | 46 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 47 | int rmmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 48 | int rmmod_main(int argc, char **argv) |
Erik Andersen | 3d7e341 | 1999-12-16 23:04:20 +0000 | [diff] [blame] | 49 | { |
Mark Whitley | f90c28d | 2001-03-09 21:49:12 +0000 | [diff] [blame] | 50 | int n, ret = EXIT_SUCCESS; |
Eric Andersen | e8521f1 | 2004-07-13 00:09:34 +0000 | [diff] [blame] | 51 | unsigned int flags = O_NONBLOCK|O_EXCL; |
Erik Andersen | 3d7e341 | 1999-12-16 23:04:20 +0000 | [diff] [blame] | 52 | |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 53 | #define misc_buf bb_common_bufsiz1 |
| 54 | |
Mark Whitley | f90c28d | 2001-03-09 21:49:12 +0000 | [diff] [blame] | 55 | /* Parse command line. */ |
Denis Vlasenko | fe7cd64 | 2007-08-18 15:32:12 +0000 | [diff] [blame] | 56 | n = getopt32(argv, "wfa"); |
Denis Vlasenko | 51742f4 | 2007-04-12 00:32:05 +0000 | [diff] [blame] | 57 | if (n & 1) // --wait |
"Vladimir N. Oleynik" | 8c44f01 | 2005-11-28 15:54:22 +0000 | [diff] [blame] | 58 | flags &= ~O_NONBLOCK; |
Denis Vlasenko | 51742f4 | 2007-04-12 00:32:05 +0000 | [diff] [blame] | 59 | if (n & 2) // --force |
"Vladimir N. Oleynik" | 8c44f01 | 2005-11-28 15:54:22 +0000 | [diff] [blame] | 60 | flags |= O_TRUNC; |
Denis Vlasenko | 51742f4 | 2007-04-12 00:32:05 +0000 | [diff] [blame] | 61 | if (n & 4) { |
"Vladimir N. Oleynik" | 8c44f01 | 2005-11-28 15:54:22 +0000 | [diff] [blame] | 62 | /* Unload _all_ unused modules via NULL delete_module() call */ |
| 63 | /* until the number of modules does not change */ |
| 64 | size_t nmod = 0; /* number of modules */ |
| 65 | size_t pnmod = -1; /* previous number of modules */ |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 66 | |
"Vladimir N. Oleynik" | 8c44f01 | 2005-11-28 15:54:22 +0000 | [diff] [blame] | 67 | while (nmod != pnmod) { |
Bernhard Reutner-Fischer | 4fec346 | 2008-05-27 10:13:54 +0000 | [diff] [blame] | 68 | if (delete_module(NULL, flags) != 0) { |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 69 | if (errno == EFAULT) |
| 70 | return ret; |
"Vladimir N. Oleynik" | 8c44f01 | 2005-11-28 15:54:22 +0000 | [diff] [blame] | 71 | bb_perror_msg_and_die("rmmod"); |
| 72 | } |
| 73 | pnmod = nmod; |
Rob Landley | abfe107 | 2006-08-28 19:40:08 +0000 | [diff] [blame] | 74 | // the 1 here is QM_MODULES. |
| 75 | if (ENABLE_FEATURE_QUERY_MODULE_INTERFACE && query_module(NULL, |
Denis Vlasenko | 74324c8 | 2007-06-04 10:16:52 +0000 | [diff] [blame] | 76 | 1, misc_buf, sizeof(misc_buf), |
Rob Landley | abfe107 | 2006-08-28 19:40:08 +0000 | [diff] [blame] | 77 | &nmod)) |
| 78 | { |
"Vladimir N. Oleynik" | 8c44f01 | 2005-11-28 15:54:22 +0000 | [diff] [blame] | 79 | bb_perror_msg_and_die("QM_MODULES"); |
| 80 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 81 | } |
"Vladimir N. Oleynik" | 8c44f01 | 2005-11-28 15:54:22 +0000 | [diff] [blame] | 82 | return EXIT_SUCCESS; |
Erik Andersen | 3d7e341 | 1999-12-16 23:04:20 +0000 | [diff] [blame] | 83 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 84 | |
Mark Whitley | f90c28d | 2001-03-09 21:49:12 +0000 | [diff] [blame] | 85 | if (optind == argc) |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 86 | bb_show_usage(); |
Mark Whitley | f90c28d | 2001-03-09 21:49:12 +0000 | [diff] [blame] | 87 | |
"Vladimir N. Oleynik" | 8c44f01 | 2005-11-28 15:54:22 +0000 | [diff] [blame] | 88 | for (n = optind; n < argc; n++) { |
Rob Landley | abfe107 | 2006-08-28 19:40:08 +0000 | [diff] [blame] | 89 | if (ENABLE_FEATURE_2_6_MODULES) { |
Denis Vlasenko | dc757aa | 2007-06-30 08:04:05 +0000 | [diff] [blame] | 90 | filename2modname(misc_buf, bb_basename(argv[n])); |
Rob Landley | abfe107 | 2006-08-28 19:40:08 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Bernhard Reutner-Fischer | 4fec346 | 2008-05-27 10:13:54 +0000 | [diff] [blame] | 93 | if (delete_module(ENABLE_FEATURE_2_6_MODULES ? misc_buf : argv[n], flags)) { |
Denis Vlasenko | 0c97c9d | 2007-10-01 11:58:38 +0000 | [diff] [blame] | 94 | bb_simple_perror_msg(argv[n]); |
"Vladimir N. Oleynik" | 8c44f01 | 2005-11-28 15:54:22 +0000 | [diff] [blame] | 95 | ret = EXIT_FAILURE; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 96 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 97 | } |
Mark Whitley | f90c28d | 2001-03-09 21:49:12 +0000 | [diff] [blame] | 98 | |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 99 | return ret; |
Erik Andersen | 3d7e341 | 1999-12-16 23:04:20 +0000 | [diff] [blame] | 100 | } |