rmmod.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. */
  22. #include <stdio.h>
  23. #include <errno.h>
  24. #include <unistd.h>
  25. #include <stdlib.h>
  26. #include <getopt.h>
  27. #include <fcntl.h>
  28. #include <string.h>
  29. #include <sys/utsname.h>
  30. #include <sys/syscall.h>
  31. #include "busybox.h"
  32. #ifdef CONFIG_FEATURE_2_6_MODULES
  33. static inline void filename2modname(char *modname, const char *afterslash)
  34. {
  35. unsigned int i;
  36. #if ENABLE_FEATURE_2_4_MODULES
  37. int kr_chk = 1;
  38. if (get_kernel_revision() <= 2*65536+6*256)
  39. kr_chk = 0;
  40. #else
  41. #define kr_chk 1
  42. #endif
  43. /* Convert to underscores, stop at first . */
  44. for (i = 0; afterslash[i] && afterslash[i] != '.'; i++) {
  45. if (kr_chk && (afterslash[i] == '-'))
  46. modname[i] = '_';
  47. else
  48. modname[i] = afterslash[i];
  49. }
  50. modname[i] = '\0';
  51. }
  52. #endif
  53. int rmmod_main(int argc, char **argv)
  54. {
  55. int n, ret = EXIT_SUCCESS;
  56. unsigned int flags = O_NONBLOCK|O_EXCL;
  57. #ifdef CONFIG_FEATURE_QUERY_MODULE_INTERFACE
  58. /* bb_common_bufsiz1 hold the module names which we ignore
  59. but must get */
  60. size_t bufsize = sizeof(bb_common_bufsiz1);
  61. #endif
  62. /* Parse command line. */
  63. n = bb_getopt_ulflags(argc, argv, "wfa");
  64. if((n & 1)) // --wait
  65. flags &= ~O_NONBLOCK;
  66. if((n & 2)) // --force
  67. flags |= O_TRUNC;
  68. if((n & 4)) {
  69. /* Unload _all_ unused modules via NULL delete_module() call */
  70. /* until the number of modules does not change */
  71. size_t nmod = 0; /* number of modules */
  72. size_t pnmod = -1; /* previous number of modules */
  73. while (nmod != pnmod) {
  74. if (syscall(__NR_delete_module, NULL, flags) != 0) {
  75. if (errno==EFAULT)
  76. return(ret);
  77. bb_perror_msg_and_die("rmmod");
  78. }
  79. pnmod = nmod;
  80. #ifdef CONFIG_FEATURE_QUERY_MODULE_INTERFACE
  81. /* 1 == QM_MODULES */
  82. if (my_query_module(NULL, 1, &bb_common_bufsiz1, &bufsize, &nmod)) {
  83. bb_perror_msg_and_die("QM_MODULES");
  84. }
  85. #endif
  86. }
  87. return EXIT_SUCCESS;
  88. }
  89. if (optind == argc)
  90. bb_show_usage();
  91. for (n = optind; n < argc; n++) {
  92. #ifdef CONFIG_FEATURE_2_6_MODULES
  93. const char *afterslash;
  94. char *module_name;
  95. afterslash = strrchr(argv[n], '/');
  96. if (!afterslash)
  97. afterslash = argv[n];
  98. else
  99. afterslash++;
  100. module_name = alloca(strlen(afterslash) + 1);
  101. filename2modname(module_name, afterslash);
  102. #else
  103. #define module_name argv[n]
  104. #endif
  105. if (syscall(__NR_delete_module, module_name, flags) != 0) {
  106. bb_perror_msg("%s", argv[n]);
  107. ret = EXIT_FAILURE;
  108. }
  109. }
  110. return(ret);
  111. }