Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Erik Andersen | 3d7e341 | 1999-12-16 23:04:20 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Mini rmmod implementation for busybox |
| 4 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 6 | * Copyright (C) 2008 Timo Teras <timo.teras@iki.fi> |
Erik Andersen | 3d7e341 | 1999-12-16 23:04:20 +0000 | [diff] [blame] | 7 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 8 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Erik Andersen | 3d7e341 | 1999-12-16 23:04:20 +0000 | [diff] [blame] | 9 | */ |
Denys Vlasenko | e32b64c | 2016-11-23 07:54:52 +0100 | [diff] [blame] | 10 | //config:config RMMOD |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 11 | //config: bool "rmmod (3.6 kb)" |
Denys Vlasenko | 04c1a2a | 2016-12-23 15:57:26 +0100 | [diff] [blame] | 12 | //config: default y |
Denys Vlasenko | e32b64c | 2016-11-23 07:54:52 +0100 | [diff] [blame] | 13 | //config: select PLATFORM_LINUX |
| 14 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame^] | 15 | //config: rmmod is used to unload specified modules from the kernel. |
Erik Andersen | 3d7e341 | 1999-12-16 23:04:20 +0000 | [diff] [blame] | 16 | |
Denys Vlasenko | a1cd0d9 | 2016-12-23 15:12:27 +0100 | [diff] [blame] | 17 | //applet:IF_RMMOD(IF_NOT_MODPROBE_SMALL(APPLET(rmmod, BB_DIR_SBIN, BB_SUID_DROP))) |
Denys Vlasenko | c15613c | 2010-10-15 11:29:02 +0200 | [diff] [blame] | 18 | |
Denys Vlasenko | a1cd0d9 | 2016-12-23 15:12:27 +0100 | [diff] [blame] | 19 | //kbuild:ifneq ($(CONFIG_MODPROBE_SMALL),y) |
Denys Vlasenko | e32b64c | 2016-11-23 07:54:52 +0100 | [diff] [blame] | 20 | //kbuild:lib-$(CONFIG_RMMOD) += rmmod.o modutils.o |
Denys Vlasenko | a1cd0d9 | 2016-12-23 15:12:27 +0100 | [diff] [blame] | 21 | //kbuild:endif |
Denys Vlasenko | e32b64c | 2016-11-23 07:54:52 +0100 | [diff] [blame] | 22 | |
Denys Vlasenko | 1a5e11c | 2010-10-16 01:56:41 +0200 | [diff] [blame] | 23 | //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 Vlasenko | 1a5e11c | 2010-10-16 01:56:41 +0200 | [diff] [blame] | 28 | //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 Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 35 | #include "libbb.h" |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 36 | #include "modutils.h" |
Rob Landley | abfe107 | 2006-08-28 19:40:08 +0000 | [diff] [blame] | 37 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 38 | int rmmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 39 | int rmmod_main(int argc UNUSED_PARAM, char **argv) |
Erik Andersen | 3d7e341 | 1999-12-16 23:04:20 +0000 | [diff] [blame] | 40 | { |
Denys Vlasenko | cd13974 | 2015-10-24 04:17:04 +0200 | [diff] [blame] | 41 | int n, err; |
Denis Vlasenko | 1f63229 | 2009-04-13 02:25:40 +0000 | [diff] [blame] | 42 | unsigned flags = O_NONBLOCK | O_EXCL; |
Erik Andersen | 3d7e341 | 1999-12-16 23:04:20 +0000 | [diff] [blame] | 43 | |
Mark Whitley | f90c28d | 2001-03-09 21:49:12 +0000 | [diff] [blame] | 44 | /* Parse command line. */ |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 45 | n = getopt32(argv, "wfas"); // -s ignored |
| 46 | argv += optind; |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 47 | if (n & 1) // --wait |
"Vladimir N. Oleynik" | 8c44f01 | 2005-11-28 15:54:22 +0000 | [diff] [blame] | 48 | flags &= ~O_NONBLOCK; |
Denys Vlasenko | fb132e4 | 2010-10-29 11:46:52 +0200 | [diff] [blame] | 49 | if (n & 2) // --force |
"Vladimir N. Oleynik" | 8c44f01 | 2005-11-28 15:54:22 +0000 | [diff] [blame] | 50 | flags |= O_TRUNC; |
Denis Vlasenko | 51742f4 | 2007-04-12 00:32:05 +0000 | [diff] [blame] | 51 | if (n & 4) { |
"Vladimir N. Oleynik" | 8c44f01 | 2005-11-28 15:54:22 +0000 | [diff] [blame] | 52 | /* Unload _all_ unused modules via NULL delete_module() call */ |
Denys Vlasenko | cd13974 | 2015-10-24 04:17:04 +0200 | [diff] [blame] | 53 | err = bb_delete_module(NULL, flags); |
| 54 | if (err && err != EFAULT) |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 55 | bb_perror_msg_and_die("rmmod"); |
"Vladimir N. Oleynik" | 8c44f01 | 2005-11-28 15:54:22 +0000 | [diff] [blame] | 56 | return EXIT_SUCCESS; |
Erik Andersen | 3d7e341 | 1999-12-16 23:04:20 +0000 | [diff] [blame] | 57 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 58 | |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 59 | if (!*argv) |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 60 | bb_show_usage(); |
Mark Whitley | f90c28d | 2001-03-09 21:49:12 +0000 | [diff] [blame] | 61 | |
Denis Vlasenko | 1f63229 | 2009-04-13 02:25:40 +0000 | [diff] [blame] | 62 | n = ENABLE_FEATURE_2_4_MODULES && get_linux_version_code() < KERNEL_VERSION(2,6,0); |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 63 | while (*argv) { |
| 64 | char modname[MODULE_NAME_LEN]; |
Denis Vlasenko | 1f63229 | 2009-04-13 02:25:40 +0000 | [diff] [blame] | 65 | 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 Vlasenko | cd13974 | 2015-10-24 04:17:04 +0200 | [diff] [blame] | 72 | err = bb_delete_module(modname, flags); |
| 73 | if (err) |
| 74 | bb_perror_msg_and_die("can't unload module '%s'", |
| 75 | modname); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 76 | } |
Mark Whitley | f90c28d | 2001-03-09 21:49:12 +0000 | [diff] [blame] | 77 | |
Denis Vlasenko | ba1315d | 2008-09-13 14:59:38 +0000 | [diff] [blame] | 78 | return EXIT_SUCCESS; |
Erik Andersen | 3d7e341 | 1999-12-16 23:04:20 +0000 | [diff] [blame] | 79 | } |