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 | |
Bernhard Reutner-Fischer | deda6a5 | 2006-06-03 19:35:15 +0000 | [diff] [blame] | 10 | #include "busybox.h" |
Erik Andersen | 3d7e341 | 1999-12-16 23:04:20 +0000 | [diff] [blame] | 11 | #include <stdio.h> |
| 12 | #include <errno.h> |
| 13 | #include <unistd.h> |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 14 | #include <stdlib.h> |
Mark Whitley | f90c28d | 2001-03-09 21:49:12 +0000 | [diff] [blame] | 15 | #include <getopt.h> |
Eric Andersen | e704788 | 2003-12-11 01:42:13 +0000 | [diff] [blame] | 16 | #include <fcntl.h> |
Eric Andersen | c0693ed | 2004-07-20 10:05:13 +0000 | [diff] [blame] | 17 | #include <string.h> |
Rob Landley | 5221987 | 2005-11-27 19:01:53 +0000 | [diff] [blame] | 18 | #include <sys/utsname.h> |
Eric Andersen | e704788 | 2003-12-11 01:42:13 +0000 | [diff] [blame] | 19 | #include <sys/syscall.h> |
Erik Andersen | 3d7e341 | 1999-12-16 23:04:20 +0000 | [diff] [blame] | 20 | |
Eric Andersen | c0693ed | 2004-07-20 10:05:13 +0000 | [diff] [blame] | 21 | #ifdef CONFIG_FEATURE_2_6_MODULES |
"Vladimir N. Oleynik" | 8c44f01 | 2005-11-28 15:54:22 +0000 | [diff] [blame] | 22 | static inline void filename2modname(char *modname, const char *afterslash) |
Eric Andersen | c0693ed | 2004-07-20 10:05:13 +0000 | [diff] [blame] | 23 | { |
Eric Andersen | c0693ed | 2004-07-20 10:05:13 +0000 | [diff] [blame] | 24 | unsigned int i; |
| 25 | |
"Vladimir N. Oleynik" | 8c44f01 | 2005-11-28 15:54:22 +0000 | [diff] [blame] | 26 | #if ENABLE_FEATURE_2_4_MODULES |
| 27 | int kr_chk = 1; |
Bernhard Reutner-Fischer | e2e56c7 | 2006-05-19 11:54:02 +0000 | [diff] [blame] | 28 | if (get_linux_version_code() <= KERNEL_VERSION(2,6,0)) |
"Vladimir N. Oleynik" | 8c44f01 | 2005-11-28 15:54:22 +0000 | [diff] [blame] | 29 | kr_chk = 0; |
| 30 | #else |
| 31 | #define kr_chk 1 |
| 32 | #endif |
Eric Andersen | c0693ed | 2004-07-20 10:05:13 +0000 | [diff] [blame] | 33 | |
| 34 | /* Convert to underscores, stop at first . */ |
| 35 | for (i = 0; afterslash[i] && afterslash[i] != '.'; i++) { |
Rob Landley | 5221987 | 2005-11-27 19:01:53 +0000 | [diff] [blame] | 36 | if (kr_chk && (afterslash[i] == '-')) |
Eric Andersen | c0693ed | 2004-07-20 10:05:13 +0000 | [diff] [blame] | 37 | modname[i] = '_'; |
| 38 | else |
| 39 | modname[i] = afterslash[i]; |
| 40 | } |
| 41 | modname[i] = '\0'; |
| 42 | } |
| 43 | #endif |
| 44 | |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 45 | int rmmod_main(int argc, char **argv) |
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 | int n, ret = EXIT_SUCCESS; |
Eric Andersen | e8521f1 | 2004-07-13 00:09:34 +0000 | [diff] [blame] | 48 | unsigned int flags = O_NONBLOCK|O_EXCL; |
| 49 | #ifdef CONFIG_FEATURE_QUERY_MODULE_INTERFACE |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 50 | /* bb_common_bufsiz1 hold the module names which we ignore |
"Vladimir N. Oleynik" | 8c44f01 | 2005-11-28 15:54:22 +0000 | [diff] [blame] | 51 | but must get */ |
| 52 | size_t bufsize = sizeof(bb_common_bufsiz1); |
Eric Andersen | e8521f1 | 2004-07-13 00:09:34 +0000 | [diff] [blame] | 53 | #endif |
Erik Andersen | 3d7e341 | 1999-12-16 23:04:20 +0000 | [diff] [blame] | 54 | |
Mark Whitley | f90c28d | 2001-03-09 21:49:12 +0000 | [diff] [blame] | 55 | /* Parse command line. */ |
"Vladimir N. Oleynik" | 8c44f01 | 2005-11-28 15:54:22 +0000 | [diff] [blame] | 56 | n = bb_getopt_ulflags(argc, argv, "wfa"); |
| 57 | if((n & 1)) // --wait |
| 58 | flags &= ~O_NONBLOCK; |
| 59 | if((n & 2)) // --force |
| 60 | flags |= O_TRUNC; |
| 61 | if((n & 4)) { |
| 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) { |
Rob Landley | 82327f4 | 2005-12-11 19:46:50 +0000 | [diff] [blame] | 68 | if (syscall(__NR_delete_module, NULL, flags) != 0) { |
"Vladimir N. Oleynik" | 8c44f01 | 2005-11-28 15:54:22 +0000 | [diff] [blame] | 69 | if (errno==EFAULT) |
| 70 | return(ret); |
| 71 | bb_perror_msg_and_die("rmmod"); |
| 72 | } |
| 73 | pnmod = nmod; |
Eric Andersen | e8521f1 | 2004-07-13 00:09:34 +0000 | [diff] [blame] | 74 | #ifdef CONFIG_FEATURE_QUERY_MODULE_INTERFACE |
"Vladimir N. Oleynik" | 8c44f01 | 2005-11-28 15:54:22 +0000 | [diff] [blame] | 75 | /* 1 == QM_MODULES */ |
| 76 | if (my_query_module(NULL, 1, &bb_common_bufsiz1, &bufsize, &nmod)) { |
| 77 | bb_perror_msg_and_die("QM_MODULES"); |
| 78 | } |
Eric Andersen | e8521f1 | 2004-07-13 00:09:34 +0000 | [diff] [blame] | 79 | #endif |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 80 | } |
"Vladimir N. Oleynik" | 8c44f01 | 2005-11-28 15:54:22 +0000 | [diff] [blame] | 81 | return EXIT_SUCCESS; |
Erik Andersen | 3d7e341 | 1999-12-16 23:04:20 +0000 | [diff] [blame] | 82 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 83 | |
Mark Whitley | f90c28d | 2001-03-09 21:49:12 +0000 | [diff] [blame] | 84 | if (optind == argc) |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 85 | bb_show_usage(); |
Mark Whitley | f90c28d | 2001-03-09 21:49:12 +0000 | [diff] [blame] | 86 | |
"Vladimir N. Oleynik" | 8c44f01 | 2005-11-28 15:54:22 +0000 | [diff] [blame] | 87 | for (n = optind; n < argc; n++) { |
Eric Andersen | c0693ed | 2004-07-20 10:05:13 +0000 | [diff] [blame] | 88 | #ifdef CONFIG_FEATURE_2_6_MODULES |
"Vladimir N. Oleynik" | 8c44f01 | 2005-11-28 15:54:22 +0000 | [diff] [blame] | 89 | const char *afterslash; |
| 90 | char *module_name; |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 91 | |
"Vladimir N. Oleynik" | 8c44f01 | 2005-11-28 15:54:22 +0000 | [diff] [blame] | 92 | afterslash = strrchr(argv[n], '/'); |
| 93 | if (!afterslash) |
| 94 | afterslash = argv[n]; |
| 95 | else |
| 96 | afterslash++; |
| 97 | module_name = alloca(strlen(afterslash) + 1); |
| 98 | filename2modname(module_name, afterslash); |
Eric Andersen | c0693ed | 2004-07-20 10:05:13 +0000 | [diff] [blame] | 99 | #else |
| 100 | #define module_name argv[n] |
| 101 | #endif |
Rob Landley | 82327f4 | 2005-12-11 19:46:50 +0000 | [diff] [blame] | 102 | if (syscall(__NR_delete_module, module_name, flags) != 0) { |
"Vladimir N. Oleynik" | 8c44f01 | 2005-11-28 15:54:22 +0000 | [diff] [blame] | 103 | bb_perror_msg("%s", argv[n]); |
| 104 | ret = EXIT_FAILURE; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 105 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 106 | } |
Mark Whitley | f90c28d | 2001-03-09 21:49:12 +0000 | [diff] [blame] | 107 | |
Eric Andersen | a5716d3 | 2000-07-28 15:16:37 +0000 | [diff] [blame] | 108 | return(ret); |
Erik Andersen | 3d7e341 | 1999-12-16 23:04:20 +0000 | [diff] [blame] | 109 | } |