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>
|
2008-09-13 14:59:38 +00:00
|
|
|
* Copyright (C) 2008 Timo Teras <timo.teras@iki.fi>
|
1999-12-16 23:04:20 +00:00
|
|
|
*
|
2010-08-16 18:14:46 +00:00
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
1999-12-16 23:04:20 +00:00
|
|
|
*/
|
|
|
|
|
2011-01-18 12:58:01 +00:00
|
|
|
//applet:IF_RMMOD(APPLET(rmmod, BB_DIR_SBIN, BB_SUID_DROP))
|
2010-10-15 09:29:02 +00:00
|
|
|
|
2010-10-15 23:56:41 +00:00
|
|
|
//usage:#if !ENABLE_MODPROBE_SMALL
|
|
|
|
//usage:#define rmmod_trivial_usage
|
|
|
|
//usage: "[-wfa] [MODULE]..."
|
|
|
|
//usage:#define rmmod_full_usage "\n\n"
|
|
|
|
//usage: "Unload kernel modules\n"
|
|
|
|
//usage: "\n -w Wait until the module is no longer used"
|
|
|
|
//usage: "\n -f Force unload"
|
|
|
|
//usage: "\n -a Remove all unused modules (recursively)"
|
|
|
|
//usage:#define rmmod_example_usage
|
|
|
|
//usage: "$ rmmod tulip\n"
|
|
|
|
//usage:#endif
|
|
|
|
|
2007-05-26 19:00:18 +00:00
|
|
|
#include "libbb.h"
|
2008-09-13 14:59:38 +00:00
|
|
|
#include "modutils.h"
|
2006-08-28 19:40:08 +00:00
|
|
|
|
2007-10-11 10:05:36 +00:00
|
|
|
int rmmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2008-09-13 14:59:38 +00:00
|
|
|
int rmmod_main(int argc UNUSED_PARAM, char **argv)
|
1999-12-16 23:04:20 +00:00
|
|
|
{
|
2008-09-13 14:59:38 +00:00
|
|
|
int n;
|
2009-04-13 02:25:40 +00:00
|
|
|
unsigned flags = O_NONBLOCK | O_EXCL;
|
1999-12-16 23:04:20 +00:00
|
|
|
|
2001-03-09 21:49:12 +00:00
|
|
|
/* Parse command line. */
|
2008-09-13 14:59:38 +00:00
|
|
|
n = getopt32(argv, "wfas"); // -s ignored
|
|
|
|
argv += optind;
|
2010-10-29 09:46:52 +00:00
|
|
|
if (n & 1) // --wait
|
2005-11-28 15:54:22 +00:00
|
|
|
flags &= ~O_NONBLOCK;
|
2010-10-29 09:46:52 +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 */
|
2008-09-13 14:59:38 +00:00
|
|
|
if (bb_delete_module(NULL, flags) != 0 && errno != EFAULT)
|
|
|
|
bb_perror_msg_and_die("rmmod");
|
2005-11-28 15:54:22 +00:00
|
|
|
return EXIT_SUCCESS;
|
1999-12-16 23:04:20 +00:00
|
|
|
}
|
|
|
|
|
2008-09-13 14:59:38 +00:00
|
|
|
if (!*argv)
|
2003-12-24 20:30:45 +00:00
|
|
|
bb_show_usage();
|
2001-03-09 21:49:12 +00:00
|
|
|
|
2009-04-13 02:25:40 +00:00
|
|
|
n = ENABLE_FEATURE_2_4_MODULES && get_linux_version_code() < KERNEL_VERSION(2,6,0);
|
2008-09-13 14:59:38 +00:00
|
|
|
while (*argv) {
|
|
|
|
char modname[MODULE_NAME_LEN];
|
2009-04-13 02:25:40 +00:00
|
|
|
const char *bname;
|
|
|
|
|
|
|
|
bname = bb_basename(*argv++);
|
|
|
|
if (n)
|
|
|
|
safe_strncpy(modname, bname, MODULE_NAME_LEN);
|
|
|
|
else
|
|
|
|
filename2modname(bname, modname);
|
2008-09-13 14:59:38 +00:00
|
|
|
if (bb_delete_module(modname, flags))
|
2009-04-13 02:25:40 +00:00
|
|
|
bb_error_msg_and_die("can't unload '%s': %s",
|
2013-01-14 14:57:44 +00:00
|
|
|
modname, moderror(errno));
|
1999-12-16 23:04:20 +00:00
|
|
|
}
|
2001-03-09 21:49:12 +00:00
|
|
|
|
2008-09-13 14:59:38 +00:00
|
|
|
return EXIT_SUCCESS;
|
1999-12-16 23:04:20 +00:00
|
|
|
}
|