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  {
33    // ?man -f: specify f option
34    case 'f':
35      flags |= O_TRUNC;
36      break;
37    // ?man -w: specify w option
38    case 'w':
39      flags &= ~O_NONBLOCK;
40      break;
41    default:
42      usage();
43  }
44  ARGEND;
45
46  if (argc < 1)
47    usage();
48
49  for (i = 0; i < argc; i++) {
50    mod = argv[i];
51    p   = strrchr(mod, '.');
52    if (p && !strcmp(p, ".ko"))
53      *p = '\0';
54    if (syscall(__NR_delete_module, mod, flags) < 0)
55      eprintf("delete_module:");
56  }
57
58  return 0;
59}