rmmod.c 2.1 KB

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