3
0

rmmod.c 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. #if ENABLE_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. #define misc_buf bb_common_bufsiz1
  40. /* Parse command line. */
  41. n = getopt32(argv, "wfa");
  42. if (n & 1) // --wait
  43. flags &= ~O_NONBLOCK;
  44. if (n & 2) // --force
  45. flags |= O_TRUNC;
  46. if (n & 4) {
  47. /* Unload _all_ unused modules via NULL delete_module() call */
  48. /* until the number of modules does not change */
  49. size_t nmod = 0; /* number of modules */
  50. size_t pnmod = -1; /* previous number of modules */
  51. while (nmod != pnmod) {
  52. if (syscall(__NR_delete_module, NULL, flags) != 0) {
  53. if (errno == EFAULT)
  54. return ret;
  55. bb_perror_msg_and_die("rmmod");
  56. }
  57. pnmod = nmod;
  58. // the 1 here is QM_MODULES.
  59. if (ENABLE_FEATURE_QUERY_MODULE_INTERFACE && query_module(NULL,
  60. 1, misc_buf, sizeof(misc_buf),
  61. &nmod))
  62. {
  63. bb_perror_msg_and_die("QM_MODULES");
  64. }
  65. }
  66. return EXIT_SUCCESS;
  67. }
  68. if (optind == argc)
  69. bb_show_usage();
  70. for (n = optind; n < argc; n++) {
  71. if (ENABLE_FEATURE_2_6_MODULES) {
  72. filename2modname(misc_buf, bb_basename(argv[n]));
  73. }
  74. if (syscall(__NR_delete_module, ENABLE_FEATURE_2_6_MODULES ? misc_buf : argv[n], flags)) {
  75. bb_perror_msg("%s", argv[n]);
  76. ret = EXIT_FAILURE;
  77. }
  78. }
  79. return ret;
  80. }