The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <string.h> |
| 3 | #include <fcntl.h> |
| 4 | #include <unistd.h> |
| 5 | #include <malloc.h> |
| 6 | #include <errno.h> |
| 7 | #include <asm/unistd.h> |
| 8 | |
| 9 | extern int delete_module(const char *, unsigned int); |
| 10 | |
| 11 | int rmmod_main(int argc, char **argv) |
| 12 | { |
Vishal Bhoj | fc26c0b | 2012-05-14 16:04:12 +0000 | [diff] [blame] | 13 | int ret, i; |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 14 | char *modname, *dot; |
| 15 | |
| 16 | /* make sure we've got an argument */ |
| 17 | if (argc < 2) { |
| 18 | fprintf(stderr, "usage: rmmod <module>\n"); |
| 19 | return -1; |
| 20 | } |
| 21 | |
| 22 | /* if given /foo/bar/blah.ko, make a weak attempt |
| 23 | * to convert to "blah", just for convenience |
| 24 | */ |
| 25 | modname = strrchr(argv[1], '/'); |
| 26 | if (!modname) |
| 27 | modname = argv[1]; |
Tanguy Pruvot | ec4db51 | 2011-09-01 13:16:18 +0200 | [diff] [blame] | 28 | else modname++; |
| 29 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 30 | dot = strchr(argv[1], '.'); |
| 31 | if (dot) |
| 32 | *dot = '\0'; |
| 33 | |
Vishal Bhoj | fc26c0b | 2012-05-14 16:04:12 +0000 | [diff] [blame] | 34 | /* Replace "-" with "_". This would keep rmmod |
| 35 | * compatible with module-init-tools version of |
| 36 | * rmmod |
| 37 | */ |
| 38 | for (i = 0; modname[i] != '\0'; i++) { |
| 39 | if (modname[i] == '-') |
| 40 | modname[i] = '_'; |
| 41 | } |
| 42 | |
The Android Open Source Project | dd7bc33 | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 43 | /* pass it to the kernel */ |
| 44 | ret = delete_module(modname, O_NONBLOCK | O_EXCL); |
| 45 | if (ret != 0) { |
| 46 | fprintf(stderr, "rmmod: delete_module '%s' failed (errno %d)\n", |
| 47 | modname, errno); |
| 48 | return -1; |
| 49 | } |
| 50 | |
| 51 | return 0; |
| 52 | } |
| 53 | |