rmmod.c 1.8 KB

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