blob: 4c4d50885a11643f42398111d2554581ce1c0062
1 | /* vi: set sw=4 ts=4: */ |
2 | /* |
3 | * Mini rmmod implementation for busybox |
4 | * |
5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
6 | * Copyright (C) 2008 Timo Teras <timo.teras@iki.fi> |
7 | * |
8 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
9 | */ |
10 | //config:config RMMOD |
11 | //config: bool "rmmod" |
12 | //config: default n |
13 | //config: select PLATFORM_LINUX |
14 | //config: help |
15 | //config: rmmod is used to unload specified modules from the kernel. |
16 | |
17 | //applet:IF_RMMOD(IF_NOT_MODPROBE_SMALL(APPLET(rmmod, BB_DIR_SBIN, BB_SUID_DROP))) |
18 | |
19 | //kbuild:ifneq ($(CONFIG_MODPROBE_SMALL),y) |
20 | //kbuild:lib-$(CONFIG_RMMOD) += rmmod.o modutils.o |
21 | //kbuild:endif |
22 | |
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" |
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 | |
35 | #include "libbb.h" |
36 | #include "modutils.h" |
37 | |
38 | int rmmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
39 | int rmmod_main(int argc UNUSED_PARAM, char **argv) |
40 | { |
41 | int n, err; |
42 | unsigned flags = O_NONBLOCK | O_EXCL; |
43 | |
44 | /* Parse command line. */ |
45 | n = getopt32(argv, "wfas"); // -s ignored |
46 | argv += optind; |
47 | if (n & 1) // --wait |
48 | flags &= ~O_NONBLOCK; |
49 | if (n & 2) // --force |
50 | flags |= O_TRUNC; |
51 | if (n & 4) { |
52 | /* Unload _all_ unused modules via NULL delete_module() call */ |
53 | err = bb_delete_module(NULL, flags); |
54 | if (err && err != EFAULT) |
55 | bb_perror_msg_and_die("rmmod"); |
56 | return EXIT_SUCCESS; |
57 | } |
58 | |
59 | if (!*argv) |
60 | bb_show_usage(); |
61 | |
62 | n = ENABLE_FEATURE_2_4_MODULES && get_linux_version_code() < KERNEL_VERSION(2,6,0); |
63 | while (*argv) { |
64 | char modname[MODULE_NAME_LEN]; |
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); |
72 | err = bb_delete_module(modname, flags); |
73 | if (err) |
74 | bb_perror_msg_and_die("can't unload module '%s'", |
75 | modname); |
76 | } |
77 | |
78 | return EXIT_SUCCESS; |
79 | } |
80 |