rmmod.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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/syscall.h>
  30. #include "busybox.h"
  31. #ifdef CONFIG_FEATURE_2_6_MODULES
  32. static inline void filename2modname(char *modname, const char *filename)
  33. {
  34. const char *afterslash;
  35. unsigned int i;
  36. afterslash = strrchr(filename, '/');
  37. if (!afterslash)
  38. afterslash = filename;
  39. else
  40. afterslash++;
  41. /* Convert to underscores, stop at first . */
  42. for (i = 0; afterslash[i] && afterslash[i] != '.'; i++) {
  43. if (afterslash[i] == '-')
  44. modname[i] = '_';
  45. else
  46. modname[i] = afterslash[i];
  47. }
  48. modname[i] = '\0';
  49. }
  50. #endif
  51. extern int rmmod_main(int argc, char **argv)
  52. {
  53. int n, ret = EXIT_SUCCESS;
  54. size_t nmod = 0; /* number of modules */
  55. size_t pnmod = -1; /* previous number of modules */
  56. unsigned int flags = O_NONBLOCK|O_EXCL;
  57. #ifdef CONFIG_FEATURE_QUERY_MODULE_INTERFACE
  58. void *buf; /* hold the module names which we ignore but must get */
  59. size_t bufsize = 0;
  60. #endif
  61. /* Parse command line. */
  62. while ((n = getopt(argc, argv, "a")) != EOF) {
  63. switch (n) {
  64. case 'w': // --wait
  65. flags &= ~O_NONBLOCK;
  66. break;
  67. case 'f': // --force
  68. flags |= O_TRUNC;
  69. break;
  70. case 'a':
  71. /* Unload _all_ unused modules via NULL delete_module() call */
  72. /* until the number of modules does not change */
  73. #ifdef CONFIG_FEATURE_QUERY_MODULE_INTERFACE
  74. buf = xmalloc(bufsize = 256);
  75. #endif
  76. while (nmod != pnmod) {
  77. if (syscall(__NR_delete_module, NULL, flags) < 0) {
  78. if (errno==EFAULT)
  79. return(ret);
  80. bb_perror_msg_and_die("rmmod");
  81. }
  82. pnmod = nmod;
  83. #ifdef CONFIG_FEATURE_QUERY_MODULE_INTERFACE
  84. /* 1 == QM_MODULES */
  85. if (my_query_module(NULL, 1, &buf, &bufsize, &nmod)) {
  86. bb_perror_msg_and_die("QM_MODULES");
  87. }
  88. #endif
  89. }
  90. #if defined CONFIG_FEATURE_CLEAN_UP && CONFIG_FEATURE_QUERY_MODULE_INTERFACE
  91. free(buf);
  92. #endif
  93. return EXIT_SUCCESS;
  94. default:
  95. bb_show_usage();
  96. }
  97. }
  98. if (optind == argc)
  99. bb_show_usage();
  100. {
  101. for (n = optind; n < argc; n++) {
  102. #ifdef CONFIG_FEATURE_2_6_MODULES
  103. char module_name[strlen(argv[n]) + 1];
  104. filename2modname(module_name, argv[n]);
  105. #else
  106. #define module_name argv[n]
  107. #endif
  108. if (syscall(__NR_delete_module, module_name, flags) < 0) {
  109. bb_perror_msg("%s", argv[n]);
  110. ret = EXIT_FAILURE;
  111. }
  112. }
  113. }
  114. return(ret);
  115. }