rmmod.c 1.8 KB

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