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