rmmod.c 901 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. /* rmmod.c - Remove a module from the Linux kernel.
  2. *
  3. * Copyright 2012 Elie De Brauwer <eliedebrauwer@gmail.com>
  4. USE_RMMOD(NEWTOY(rmmod, "<1wf", TOYFLAG_SBIN|TOYFLAG_NEEDROOT))
  5. config RMMOD
  6. bool "rmmod"
  7. default y
  8. help
  9. usage: rmmod [-wf] MODULE...
  10. Unload the given kernel modules.
  11. -f Force unload of a module
  12. -w Wait until the module is no longer used
  13. */
  14. #define FOR_rmmod
  15. #include "toys.h"
  16. #define delete_module(mod, flags) syscall(__NR_delete_module, mod, flags)
  17. void rmmod_main(void)
  18. {
  19. char **args, *module, *s;
  20. unsigned flags;
  21. for (args = toys.optargs; *args; args++) {
  22. module = basename(*args);
  23. // Remove .ko if present
  24. if ((s = strend(module, ".ko"))) *s = 0;
  25. flags = O_NONBLOCK;
  26. if (FLAG(f)) flags |= O_TRUNC;
  27. if (FLAG(w)) flags &= ~O_NONBLOCK;
  28. if (delete_module(module, flags)) perror_msg("failed to unload %s", module);
  29. }
  30. }