rmmod.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  8. */
  9. #include "libbb.h"
  10. #include <sys/syscall.h>
  11. #ifdef CONFIG_FEATURE_2_6_MODULES
  12. static inline void filename2modname(char *modname, const char *afterslash)
  13. {
  14. unsigned int i;
  15. int kr_chk = 1;
  16. if (ENABLE_FEATURE_2_4_MODULES
  17. && get_linux_version_code() <= KERNEL_VERSION(2,6,0))
  18. kr_chk = 0;
  19. /* Convert to underscores, stop at first . */
  20. for (i = 0; afterslash[i] && afterslash[i] != '.'; i++) {
  21. if (kr_chk && (afterslash[i] == '-'))
  22. modname[i] = '_';
  23. else
  24. modname[i] = afterslash[i];
  25. }
  26. modname[i] = '\0';
  27. }
  28. #else
  29. void filename2modname(char *modname, const char *afterslash);
  30. #endif
  31. // There really should be a header file for this...
  32. int query_module(const char *name, int which, void *buf,
  33. size_t bufsize, size_t *ret);
  34. int rmmod_main(int argc, char **argv);
  35. int rmmod_main(int argc, char **argv)
  36. {
  37. int n, ret = EXIT_SUCCESS;
  38. unsigned int flags = O_NONBLOCK|O_EXCL;
  39. /* Parse command line. */
  40. n = getopt32(argc, argv, "wfa");
  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. /* until the number of modules does not change */
  48. size_t nmod = 0; /* number of modules */
  49. size_t pnmod = -1; /* previous number of modules */
  50. while (nmod != pnmod) {
  51. if (syscall(__NR_delete_module, NULL, flags) != 0) {
  52. if (errno == EFAULT)
  53. return ret;
  54. bb_perror_msg_and_die("rmmod");
  55. }
  56. pnmod = nmod;
  57. // the 1 here is QM_MODULES.
  58. if (ENABLE_FEATURE_QUERY_MODULE_INTERFACE && query_module(NULL,
  59. 1, bb_common_bufsiz1, sizeof(bb_common_bufsiz1),
  60. &nmod))
  61. {
  62. bb_perror_msg_and_die("QM_MODULES");
  63. }
  64. }
  65. return EXIT_SUCCESS;
  66. }
  67. if (optind == argc)
  68. bb_show_usage();
  69. for (n = optind; n < argc; n++) {
  70. if (ENABLE_FEATURE_2_6_MODULES) {
  71. const char *afterslash;
  72. afterslash = strrchr(argv[n], '/');
  73. if (!afterslash) afterslash = argv[n];
  74. else afterslash++;
  75. filename2modname(bb_common_bufsiz1, afterslash);
  76. }
  77. if (syscall(__NR_delete_module, ENABLE_FEATURE_2_6_MODULES ? bb_common_bufsiz1 : argv[n], flags)) {
  78. bb_perror_msg("%s", argv[n]);
  79. ret = EXIT_FAILURE;
  80. }
  81. }
  82. return ret;
  83. }