annotate toys/rmmod.c @ 562:4d802d438983

Match uint64_t with PRIu64 to avoid warnings on 64 bit builds.
author Rob Landley <rob@landley.net>
date Sat, 14 Apr 2012 21:27:00 -0500
parents d473dff476e2
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
489
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
1 /* vi: set sw=4 ts=4:
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
2 *
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
3 * rmmod.c - Remove a module from the Linux kernel.
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
4 *
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
5 * Copyright 2012 Elie De Brauwer <eliedebrauwer@gmail.com>
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
6 *
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
7 * Not in SUSv4.
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
8
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
9 USE_RMMOD(NEWTOY(rmmod, "<1wf", TOYFLAG_BIN|TOYFLAG_NEEDROOT))
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
10
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
11 config RMMOD
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
12 bool "rmmod"
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
13 default y
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
14 help
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
15 usage: rmmod [-wf] [MODULE]
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
16
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
17 Unload the module named MODULE from the Linux kernel.
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
18 -f Force unload of a module
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
19 -w Wait until the module is no longer used.
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
20
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
21 */
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
22
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
23 #include "toys.h"
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
24
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
25 #include <sys/syscall.h>
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
26 #define delete_module(mod, flags) syscall(__NR_delete_module, mod, flags)
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
27
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
28 void rmmod_main(void)
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
29 {
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
30 unsigned int flags = O_NONBLOCK|O_EXCL;
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
31 char * mod_name;
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
32 int len;
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
33
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
34 // Basename
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
35 mod_name = strrchr(toys.optargs[0],'/');
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
36 if (mod_name)
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
37 mod_name++;
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
38 else
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
39 mod_name = toys.optargs[0];
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
40
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
41 // Remove .ko if present
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
42 len = strlen(mod_name);
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
43 if (len > 3 && !strcmp(&mod_name[len-3], ".ko" ))
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
44 mod_name[len-3] = 0;
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
45
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
46 if (toys.optflags & 1) flags |= O_TRUNC;
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
47 if (toys.optflags & 2) flags &= ~O_NONBLOCK;
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
48
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
49 if (delete_module(mod_name, flags))
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
50 perror_exit("failed to unload %s", mod_name);
d473dff476e2 Adding insmod and rmmod
Elie De Brauwer <eliedebrauwer@gmail.com>
parents:
diff changeset
51 }