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