blob: c7e0d6ac718b4272424c42c3271ac5ba41309354 [file] [log] [blame]
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -08001#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
9extern int delete_module(const char *, unsigned int);
10
11int rmmod_main(int argc, char **argv)
12{
Vishal Bhojfc26c0b2012-05-14 16:04:12 +000013 int ret, i;
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080014 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 Pruvotec4db512011-09-01 13:16:18 +020028 else modname++;
29
The Android Open Source Projectdd7bc332009-03-03 19:32:55 -080030 dot = strchr(argv[1], '.');
31 if (dot)
32 *dot = '\0';
33
Vishal Bhojfc26c0b2012-05-14 16:04:12 +000034 /* 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 Projectdd7bc332009-03-03 19:32:55 -080043 /* 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