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 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
11 #include <sys/syscall.h>
13 #ifdef CONFIG_FEATURE_2_6_MODULES
14 static inline void filename2modname(char *modname, const char *afterslash)
19 if (ENABLE_FEATURE_2_4_MODULES
20 && get_linux_version_code() <= KERNEL_VERSION(2,6,0))
23 /* Convert to underscores, stop at first . */
24 for (i = 0; afterslash[i] && afterslash[i] != '.'; i++) {
25 if (kr_chk && (afterslash[i] == '-'))
28 modname[i] = afterslash[i];
33 void filename2modname(char *modname, const char *afterslash);
36 // There really should be a header file for this...
38 int query_module(const char *name, int which, void *buf,
39 size_t bufsize, size_t *ret);
41 int rmmod_main(int argc, char **argv);
42 int rmmod_main(int argc, char **argv)
44 int n, ret = EXIT_SUCCESS;
45 unsigned int flags = O_NONBLOCK|O_EXCL;
47 /* Parse command line. */
48 n = getopt32(argc, argv, "wfa");
51 if((n & 2)) // --force
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 */
59 while (nmod != pnmod) {
60 if (syscall(__NR_delete_module, NULL, flags) != 0) {
63 bb_perror_msg_and_die("rmmod");
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),
71 bb_perror_msg_and_die("QM_MODULES");
80 for (n = optind; n < argc; n++) {
81 if (ENABLE_FEATURE_2_6_MODULES) {
82 const char *afterslash;
84 afterslash = strrchr(argv[n], '/');
85 if (!afterslash) afterslash = argv[n];
87 filename2modname(bb_common_bufsiz1, afterslash);
90 if (syscall(__NR_delete_module, ENABLE_FEATURE_2_6_MODULES ? bb_common_bufsiz1 : argv[n], flags)) {
91 bb_perror_msg("%s", argv[n]);