lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 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 | |
| 11 | //applet:IF_RMMOD(APPLET(rmmod, BB_DIR_SBIN, BB_SUID_DROP)) |
| 12 | |
| 13 | //usage:#if !ENABLE_MODPROBE_SMALL |
| 14 | //usage:#define rmmod_trivial_usage |
| 15 | //usage: "[-wfa] [MODULE]..." |
| 16 | //usage:#define rmmod_full_usage "\n\n" |
| 17 | //usage: "Unload kernel modules\n" |
| 18 | //usage: "\n -w Wait until the module is no longer used" |
| 19 | //usage: "\n -f Force unload" |
| 20 | //usage: "\n -a Remove all unused modules (recursively)" |
| 21 | //usage:#define rmmod_example_usage |
| 22 | //usage: "$ rmmod tulip\n" |
| 23 | //usage:#endif |
| 24 | |
| 25 | #include "libbb.h" |
| 26 | #include "modutils.h" |
| 27 | |
| 28 | int rmmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
| 29 | int rmmod_main(int argc UNUSED_PARAM, char **argv) |
| 30 | { |
| 31 | int n; |
| 32 | unsigned flags = O_NONBLOCK | O_EXCL; |
| 33 | |
| 34 | /* Parse command line. */ |
| 35 | n = getopt32(argv, "wfas"); // -s ignored |
| 36 | argv += optind; |
| 37 | if (n & 1) // --wait |
| 38 | flags &= ~O_NONBLOCK; |
| 39 | if (n & 2) // --force |
| 40 | flags |= O_TRUNC; |
| 41 | if (n & 4) { |
| 42 | /* Unload _all_ unused modules via NULL delete_module() call */ |
| 43 | if (bb_delete_module(NULL, flags) != 0 && errno != EFAULT) |
| 44 | bb_perror_msg_and_die("rmmod"); |
| 45 | return EXIT_SUCCESS; |
| 46 | } |
| 47 | |
| 48 | if (!*argv) |
| 49 | bb_show_usage(); |
| 50 | |
| 51 | n = ENABLE_FEATURE_2_4_MODULES && get_linux_version_code() < KERNEL_VERSION(2,6,0); |
| 52 | while (*argv) { |
| 53 | char modname[MODULE_NAME_LEN]; |
| 54 | const char *bname; |
| 55 | |
| 56 | bname = bb_basename(*argv++); |
| 57 | if (n) |
| 58 | safe_strncpy(modname, bname, MODULE_NAME_LEN); |
| 59 | else |
| 60 | filename2modname(bname, modname); |
| 61 | if (bb_delete_module(modname, flags)) |
| 62 | bb_error_msg_and_die("can't unload '%s': %s", |
| 63 | modname, moderror(errno)); |
| 64 | } |
| 65 | |
| 66 | return EXIT_SUCCESS; |
| 67 | } |