1 /* vi: set sw=4 ts=4: */
3 * Mini rmmod implementation for busybox
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include <sys/utsname.h>
31 #include <sys/syscall.h>
34 #ifdef CONFIG_FEATURE_2_6_MODULES
35 static inline void filename2modname(char *modname, const char *afterslash)
39 #if ENABLE_FEATURE_2_4_MODULES
41 if (get_kernel_revision() <= 2*65536+6*256)
47 /* Convert to underscores, stop at first . */
48 for (i = 0; afterslash[i] && afterslash[i] != '.'; i++) {
49 if (kr_chk && (afterslash[i] == '-'))
52 modname[i] = afterslash[i];
58 int rmmod_main(int argc, char **argv)
60 int n, ret = EXIT_SUCCESS;
61 unsigned int flags = O_NONBLOCK|O_EXCL;
62 #ifdef CONFIG_FEATURE_QUERY_MODULE_INTERFACE
63 /* bb_common_bufsiz1 hold the module names which we ignore
65 size_t bufsize = sizeof(bb_common_bufsiz1);
68 /* Parse command line. */
69 n = bb_getopt_ulflags(argc, argv, "wfa");
72 if((n & 2)) // --force
75 /* Unload _all_ unused modules via NULL delete_module() call */
76 /* until the number of modules does not change */
77 size_t nmod = 0; /* number of modules */
78 size_t pnmod = -1; /* previous number of modules */
80 while (nmod != pnmod) {
81 if (syscall(__NR_delete_module, NULL, flags) != 0) {
84 bb_perror_msg_and_die("rmmod");
87 #ifdef CONFIG_FEATURE_QUERY_MODULE_INTERFACE
89 if (my_query_module(NULL, 1, &bb_common_bufsiz1, &bufsize, &nmod)) {
90 bb_perror_msg_and_die("QM_MODULES");
100 for (n = optind; n < argc; n++) {
101 #ifdef CONFIG_FEATURE_2_6_MODULES
102 const char *afterslash;
105 afterslash = strrchr(argv[n], '/');
107 afterslash = argv[n];
110 module_name = alloca(strlen(afterslash) + 1);
111 filename2modname(module_name, afterslash);
113 #define module_name argv[n]
115 if (syscall(__NR_delete_module, module_name, flags) != 0) {
116 bb_perror_msg("%s", argv[n]);