rmmod.c (1118B)
1 2 #if FEATURE_LINUX_SYSCALL 3 #ifndef _GNU_SOURCE 4 #define _GNU_SOURCE 5 #endif 6 #endif 7 #include <sys/syscall.h> 8 9 #include <fcntl.h> 10 #include <libgen.h> 11 #include <stdio.h> 12 #include <stdlib.h> 13 #include <string.h> 14 #include <unistd.h> 15 16 #include "util.h" 17 18 static void 19 usage(void) 20 { 21 eprintf("usage: %s [-fw] module...\n", argv0); 22 } 23 24 // ?man rmmod: remove a module from the Linux kernel 25 // ?man arguments: module... 26 // ?man rmmod removes a kernel module from the running kernel 27 // ?man -f: force removal of a module even if it is busy or in use 28 // ?man -w: wait for the module to become unused before removing 29 int 30 main(int argc, char *argv[]) 31 { 32 char *mod, *p; 33 int i; 34 int flags = O_NONBLOCK; 35 36 ARGBEGIN 37 { 38 case 'f': 39 flags |= O_TRUNC; 40 break; 41 case 'w': 42 flags &= ~O_NONBLOCK; 43 break; 44 default: 45 usage(); 46 } 47 ARGEND; 48 49 if (argc < 1) 50 usage(); 51 52 for (i = 0; i < argc; i++) { 53 mod = argv[i]; 54 p = strrchr(mod, '.'); 55 if (p && !strcmp(p, ".ko")) 56 *p = '\0'; 57 if (syscall(__NR_delete_module, mod, flags) < 0) 58 eprintf("delete_module:"); 59 } 60 61 return 0; 62 }