rmmod.c 2.1 KB

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