1 /* rmmod.c - Remove a module from the Linux kernel.
3 * Copyright 2012 Elie De Brauwer <eliedebrauwer@gmail.com>
5 USE_RMMOD(NEWTOY(rmmod, "<1wf", TOYFLAG_SBIN|TOYFLAG_NEEDROOT))
11 usage: rmmod [-wf] [MODULE]
13 Unload the module named MODULE from the Linux kernel.
14 -f Force unload of a module
15 -w Wait until the module is no longer used
22 #include <sys/syscall.h>
23 #define delete_module(mod, flags) syscall(__NR_delete_module, mod, flags)
27 unsigned int flags = O_NONBLOCK|O_EXCL;
32 mod_name = basename(*toys.optargs);
34 // Remove .ko if present
35 len = strlen(mod_name);
36 if (len > 3 && !strcmp(&mod_name[len-3], ".ko" )) mod_name[len-3] = 0;
38 if (toys.optflags & FLAG_f) flags |= O_TRUNC;
39 if (toys.optflags & FLAG_w) flags &= ~O_NONBLOCK;
41 if (delete_module(mod_name, flags))
42 perror_exit("failed to unload %s", mod_name);