rmmod.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 tarball for details.
  9. */
  10. #include "libbb.h"
  11. #include "modutils.h"
  12. int rmmod_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  13. int rmmod_main(int argc UNUSED_PARAM, char **argv)
  14. {
  15. int n;
  16. unsigned flags = O_NONBLOCK | O_EXCL;
  17. /* Parse command line. */
  18. n = getopt32(argv, "wfas"); // -s ignored
  19. argv += optind;
  20. if (n & 1) // --wait
  21. flags &= ~O_NONBLOCK;
  22. if (n & 2) // --force
  23. flags |= O_TRUNC;
  24. if (n & 4) {
  25. /* Unload _all_ unused modules via NULL delete_module() call */
  26. if (bb_delete_module(NULL, flags) != 0 && errno != EFAULT)
  27. bb_perror_msg_and_die("rmmod");
  28. return EXIT_SUCCESS;
  29. }
  30. if (!*argv)
  31. bb_show_usage();
  32. n = ENABLE_FEATURE_2_4_MODULES && get_linux_version_code() < KERNEL_VERSION(2,6,0);
  33. while (*argv) {
  34. char modname[MODULE_NAME_LEN];
  35. const char *bname;
  36. bname = bb_basename(*argv++);
  37. if (n)
  38. safe_strncpy(modname, bname, MODULE_NAME_LEN);
  39. else
  40. filename2modname(bname, modname);
  41. if (bb_delete_module(modname, flags))
  42. bb_error_msg_and_die("can't unload '%s': %s",
  43. modname, moderror(errno));
  44. }
  45. return EXIT_SUCCESS;
  46. }