1 /* rmmod.c - Remove a module from the Linux kernel.
2  *
3  * Copyright 2012 Elie De Brauwer <eliedebrauwer@gmail.com>
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 module named MODULE from the Linux kernel.
14     -f	Force unload of a module
15     -w	Wait until the module is no longer used.
16 
17 */
18 
19 #define FOR_rmmod
20 #include "toys.h"
21 
22 #include <sys/syscall.h>
23 #define delete_module(mod, flags) syscall(__NR_delete_module, mod, flags)
24 
rmmod_main(void)25 void rmmod_main(void)
26 {
27   unsigned int flags = O_NONBLOCK|O_EXCL;
28   char * mod_name;
29   int len;
30 
31   // Basename
32   mod_name = basename(*toys.optargs);
33 
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;
37 
38   if (toys.optflags & FLAG_f) flags |= O_TRUNC;
39   if (toys.optflags & FLAG_w) flags &= ~O_NONBLOCK;
40 
41   if (delete_module(mod_name, flags))
42     perror_exit("failed to unload %s", mod_name);
43 }
44