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