2000-02-08 19:58:47 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
1999-12-16 23:04:20 +00:00
|
|
|
/*
|
|
|
|
* Mini rmmod implementation for busybox
|
|
|
|
*
|
2004-03-15 08:29:22 +00:00
|
|
|
* Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
|
1999-12-16 23:04:20 +00:00
|
|
|
*
|
2006-06-03 19:35:15 +00:00
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
1999-12-16 23:04:20 +00:00
|
|
|
*/
|
|
|
|
|
2007-05-26 19:00:18 +00:00
|
|
|
#include "libbb.h"
|
2003-12-11 01:42:13 +00:00
|
|
|
#include <sys/syscall.h>
|
1999-12-16 23:04:20 +00:00
|
|
|
|
2004-07-20 10:05:13 +00:00
|
|
|
#ifdef CONFIG_FEATURE_2_6_MODULES
|
2005-11-28 15:54:22 +00:00
|
|
|
static inline void filename2modname(char *modname, const char *afterslash)
|
2004-07-20 10:05:13 +00:00
|
|
|
{
|
|
|
|
unsigned int i;
|
2005-11-28 15:54:22 +00:00
|
|
|
int kr_chk = 1;
|
2006-08-28 19:40:08 +00:00
|
|
|
|
|
|
|
if (ENABLE_FEATURE_2_4_MODULES
|
|
|
|
&& get_linux_version_code() <= KERNEL_VERSION(2,6,0))
|
|
|
|
kr_chk = 0;
|
2004-07-20 10:05:13 +00:00
|
|
|
|
|
|
|
/* Convert to underscores, stop at first . */
|
|
|
|
for (i = 0; afterslash[i] && afterslash[i] != '.'; i++) {
|
2005-11-27 19:01:53 +00:00
|
|
|
if (kr_chk && (afterslash[i] == '-'))
|
2004-07-20 10:05:13 +00:00
|
|
|
modname[i] = '_';
|
|
|
|
else
|
|
|
|
modname[i] = afterslash[i];
|
|
|
|
}
|
|
|
|
modname[i] = '\0';
|
|
|
|
}
|
2006-08-28 19:40:08 +00:00
|
|
|
#else
|
|
|
|
void filename2modname(char *modname, const char *afterslash);
|
2004-07-20 10:05:13 +00:00
|
|
|
#endif
|
|
|
|
|
2006-08-28 19:40:08 +00:00
|
|
|
// There really should be a header file for this...
|
|
|
|
|
|
|
|
int query_module(const char *name, int which, void *buf,
|
2006-09-17 16:28:10 +00:00
|
|
|
size_t bufsize, size_t *ret);
|
2006-08-28 19:40:08 +00:00
|
|
|
|
2007-02-03 17:28:39 +00:00
|
|
|
int rmmod_main(int argc, char **argv);
|
2006-03-06 20:47:33 +00:00
|
|
|
int rmmod_main(int argc, char **argv)
|
1999-12-16 23:04:20 +00:00
|
|
|
{
|
2001-03-09 21:49:12 +00:00
|
|
|
int n, ret = EXIT_SUCCESS;
|
2004-07-13 00:09:34 +00:00
|
|
|
unsigned int flags = O_NONBLOCK|O_EXCL;
|
1999-12-16 23:04:20 +00:00
|
|
|
|
2007-06-04 10:16:52 +00:00
|
|
|
#define misc_buf bb_common_bufsiz1
|
|
|
|
|
2001-03-09 21:49:12 +00:00
|
|
|
/* Parse command line. */
|
2006-10-03 21:00:06 +00:00
|
|
|
n = getopt32(argc, argv, "wfa");
|
2007-04-12 00:32:05 +00:00
|
|
|
if (n & 1) // --wait
|
2005-11-28 15:54:22 +00:00
|
|
|
flags &= ~O_NONBLOCK;
|
2007-04-12 00:32:05 +00:00
|
|
|
if (n & 2) // --force
|
2005-11-28 15:54:22 +00:00
|
|
|
flags |= O_TRUNC;
|
2007-04-12 00:32:05 +00:00
|
|
|
if (n & 4) {
|
2005-11-28 15:54:22 +00:00
|
|
|
/* Unload _all_ unused modules via NULL delete_module() call */
|
|
|
|
/* until the number of modules does not change */
|
|
|
|
size_t nmod = 0; /* number of modules */
|
|
|
|
size_t pnmod = -1; /* previous number of modules */
|
2006-01-25 00:08:53 +00:00
|
|
|
|
2005-11-28 15:54:22 +00:00
|
|
|
while (nmod != pnmod) {
|
2005-12-11 19:46:50 +00:00
|
|
|
if (syscall(__NR_delete_module, NULL, flags) != 0) {
|
2006-11-27 16:49:31 +00:00
|
|
|
if (errno == EFAULT)
|
|
|
|
return ret;
|
2005-11-28 15:54:22 +00:00
|
|
|
bb_perror_msg_and_die("rmmod");
|
|
|
|
}
|
|
|
|
pnmod = nmod;
|
2006-08-28 19:40:08 +00:00
|
|
|
// the 1 here is QM_MODULES.
|
|
|
|
if (ENABLE_FEATURE_QUERY_MODULE_INTERFACE && query_module(NULL,
|
2007-06-04 10:16:52 +00:00
|
|
|
1, misc_buf, sizeof(misc_buf),
|
2006-08-28 19:40:08 +00:00
|
|
|
&nmod))
|
|
|
|
{
|
2005-11-28 15:54:22 +00:00
|
|
|
bb_perror_msg_and_die("QM_MODULES");
|
|
|
|
}
|
1999-12-16 23:04:20 +00:00
|
|
|
}
|
2005-11-28 15:54:22 +00:00
|
|
|
return EXIT_SUCCESS;
|
1999-12-16 23:04:20 +00:00
|
|
|
}
|
|
|
|
|
2001-03-09 21:49:12 +00:00
|
|
|
if (optind == argc)
|
2003-12-24 20:30:45 +00:00
|
|
|
bb_show_usage();
|
2001-03-09 21:49:12 +00:00
|
|
|
|
2005-11-28 15:54:22 +00:00
|
|
|
for (n = optind; n < argc; n++) {
|
2006-08-28 19:40:08 +00:00
|
|
|
if (ENABLE_FEATURE_2_6_MODULES) {
|
|
|
|
const char *afterslash;
|
2006-01-25 00:08:53 +00:00
|
|
|
|
2006-08-28 19:40:08 +00:00
|
|
|
afterslash = strrchr(argv[n], '/');
|
|
|
|
if (!afterslash) afterslash = argv[n];
|
|
|
|
else afterslash++;
|
2007-06-04 10:16:52 +00:00
|
|
|
filename2modname(misc_buf, afterslash);
|
2006-08-28 19:40:08 +00:00
|
|
|
}
|
|
|
|
|
2007-06-04 10:16:52 +00:00
|
|
|
if (syscall(__NR_delete_module, ENABLE_FEATURE_2_6_MODULES ? misc_buf : argv[n], flags)) {
|
2005-11-28 15:54:22 +00:00
|
|
|
bb_perror_msg("%s", argv[n]);
|
|
|
|
ret = EXIT_FAILURE;
|
2000-02-08 19:58:47 +00:00
|
|
|
}
|
1999-12-16 23:04:20 +00:00
|
|
|
}
|
2001-03-09 21:49:12 +00:00
|
|
|
|
2006-11-27 16:49:31 +00:00
|
|
|
return ret;
|
1999-12-16 23:04:20 +00:00
|
|
|
}
|