blob: df50e58afe4be1f6c4639ecedecc36c443131cd3 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Erik Andersen3d7e3411999-12-16 23:04:20 +00002/*
3 * Mini rmmod implementation for busybox
4 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Denis Vlasenkoba1315d2008-09-13 14:59:38 +00006 * Copyright (C) 2008 Timo Teras <timo.teras@iki.fi>
Erik Andersen3d7e3411999-12-16 23:04:20 +00007 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02008 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Erik Andersen3d7e3411999-12-16 23:04:20 +00009 */
Denys Vlasenkoe32b64c2016-11-23 07:54:52 +010010//config:config RMMOD
Denys Vlasenko4eed2c62017-07-18 22:01:24 +020011//config: bool "rmmod (3.6 kb)"
Denys Vlasenko04c1a2a2016-12-23 15:57:26 +010012//config: default y
Denys Vlasenkoe32b64c2016-11-23 07:54:52 +010013//config: select PLATFORM_LINUX
14//config: help
Denys Vlasenko72089cf2017-07-21 09:50:55 +020015//config: rmmod is used to unload specified modules from the kernel.
Erik Andersen3d7e3411999-12-16 23:04:20 +000016
Denys Vlasenko3346b4a2017-08-04 02:56:39 +020017//applet:IF_RMMOD(IF_NOT_MODPROBE_SMALL(APPLET_NOEXEC(rmmod, rmmod, BB_DIR_SBIN, BB_SUID_DROP, rmmod)))
Denys Vlasenkoc15613c2010-10-15 11:29:02 +020018
Denys Vlasenkoa1cd0d92016-12-23 15:12:27 +010019//kbuild:ifneq ($(CONFIG_MODPROBE_SMALL),y)
Denys Vlasenkoe32b64c2016-11-23 07:54:52 +010020//kbuild:lib-$(CONFIG_RMMOD) += rmmod.o modutils.o
Denys Vlasenkoa1cd0d92016-12-23 15:12:27 +010021//kbuild:endif
Denys Vlasenkoe32b64c2016-11-23 07:54:52 +010022
Denys Vlasenko1a5e11c2010-10-16 01:56:41 +020023//usage:#if !ENABLE_MODPROBE_SMALL
24//usage:#define rmmod_trivial_usage
25//usage: "[-wfa] [MODULE]..."
26//usage:#define rmmod_full_usage "\n\n"
27//usage: "Unload kernel modules\n"
Denys Vlasenko1a5e11c2010-10-16 01:56:41 +020028//usage: "\n -w Wait until the module is no longer used"
29//usage: "\n -f Force unload"
30//usage: "\n -a Remove all unused modules (recursively)"
31//usage:#define rmmod_example_usage
32//usage: "$ rmmod tulip\n"
33//usage:#endif
34
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000035#include "libbb.h"
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000036#include "modutils.h"
Rob Landleyabfe1072006-08-28 19:40:08 +000037
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000038int rmmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000039int rmmod_main(int argc UNUSED_PARAM, char **argv)
Erik Andersen3d7e3411999-12-16 23:04:20 +000040{
Denys Vlasenkocd139742015-10-24 04:17:04 +020041 int n, err;
Denis Vlasenko1f632292009-04-13 02:25:40 +000042 unsigned flags = O_NONBLOCK | O_EXCL;
Erik Andersen3d7e3411999-12-16 23:04:20 +000043
Mark Whitleyf90c28d2001-03-09 21:49:12 +000044 /* Parse command line. */
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000045 n = getopt32(argv, "wfas"); // -s ignored
46 argv += optind;
Denys Vlasenkofb132e42010-10-29 11:46:52 +020047 if (n & 1) // --wait
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000048 flags &= ~O_NONBLOCK;
Denys Vlasenkofb132e42010-10-29 11:46:52 +020049 if (n & 2) // --force
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000050 flags |= O_TRUNC;
Denis Vlasenko51742f42007-04-12 00:32:05 +000051 if (n & 4) {
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000052 /* Unload _all_ unused modules via NULL delete_module() call */
Denys Vlasenkocd139742015-10-24 04:17:04 +020053 err = bb_delete_module(NULL, flags);
54 if (err && err != EFAULT)
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000055 bb_perror_msg_and_die("rmmod");
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000056 return EXIT_SUCCESS;
Erik Andersen3d7e3411999-12-16 23:04:20 +000057 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000058
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000059 if (!*argv)
Eric Andersen3b1a7442003-12-24 20:30:45 +000060 bb_show_usage();
Mark Whitleyf90c28d2001-03-09 21:49:12 +000061
Denis Vlasenko1f632292009-04-13 02:25:40 +000062 n = ENABLE_FEATURE_2_4_MODULES && get_linux_version_code() < KERNEL_VERSION(2,6,0);
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000063 while (*argv) {
64 char modname[MODULE_NAME_LEN];
Denis Vlasenko1f632292009-04-13 02:25:40 +000065 const char *bname;
66
67 bname = bb_basename(*argv++);
68 if (n)
69 safe_strncpy(modname, bname, MODULE_NAME_LEN);
70 else
71 filename2modname(bname, modname);
Denys Vlasenkocd139742015-10-24 04:17:04 +020072 err = bb_delete_module(modname, flags);
73 if (err)
74 bb_perror_msg_and_die("can't unload module '%s'",
75 modname);
Erik Andersene49d5ec2000-02-08 19:58:47 +000076 }
Mark Whitleyf90c28d2001-03-09 21:49:12 +000077
Denis Vlasenkoba1315d2008-09-13 14:59:38 +000078 return EXIT_SUCCESS;
Erik Andersen3d7e3411999-12-16 23:04:20 +000079}