usage.c: remove reference to busybox.h
[oweals/busybox.git] / modutils / rmmod.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini rmmod implementation for busybox
4  *
5  * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6  *
7  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
8  */
9
10 #include "libbb.h"
11 #include <sys/syscall.h>
12
13 #ifdef CONFIG_FEATURE_2_6_MODULES
14 static inline void filename2modname(char *modname, const char *afterslash)
15 {
16         unsigned int i;
17         int kr_chk = 1;
18
19         if (ENABLE_FEATURE_2_4_MODULES
20                         && get_linux_version_code() <= KERNEL_VERSION(2,6,0))
21                                 kr_chk = 0;
22
23         /* Convert to underscores, stop at first . */
24         for (i = 0; afterslash[i] && afterslash[i] != '.'; i++) {
25                 if (kr_chk && (afterslash[i] == '-'))
26                         modname[i] = '_';
27                 else
28                         modname[i] = afterslash[i];
29         }
30         modname[i] = '\0';
31 }
32 #else
33 void filename2modname(char *modname, const char *afterslash);
34 #endif
35
36 // There really should be a header file for this...
37
38 int query_module(const char *name, int which, void *buf,
39                         size_t bufsize, size_t *ret);
40
41 int rmmod_main(int argc, char **argv);
42 int rmmod_main(int argc, char **argv)
43 {
44         int n, ret = EXIT_SUCCESS;
45         unsigned int flags = O_NONBLOCK|O_EXCL;
46
47         /* Parse command line. */
48         n = getopt32(argc, argv, "wfa");
49         if (n & 1)      // --wait
50                 flags &= ~O_NONBLOCK;
51         if (n & 2)      // --force
52                 flags |= O_TRUNC;
53         if (n & 4) {
54                 /* Unload _all_ unused modules via NULL delete_module() call */
55                 /* until the number of modules does not change */
56                 size_t nmod = 0; /* number of modules */
57                 size_t pnmod = -1; /* previous number of modules */
58
59                 while (nmod != pnmod) {
60                         if (syscall(__NR_delete_module, NULL, flags) != 0) {
61                                 if (errno == EFAULT)
62                                         return ret;
63                                 bb_perror_msg_and_die("rmmod");
64                         }
65                         pnmod = nmod;
66                         // the 1 here is QM_MODULES.
67                         if (ENABLE_FEATURE_QUERY_MODULE_INTERFACE && query_module(NULL,
68                                         1, bb_common_bufsiz1, sizeof(bb_common_bufsiz1),
69                                         &nmod))
70                         {
71                                 bb_perror_msg_and_die("QM_MODULES");
72                         }
73                 }
74                 return EXIT_SUCCESS;
75         }
76
77         if (optind == argc)
78                 bb_show_usage();
79
80         for (n = optind; n < argc; n++) {
81                 if (ENABLE_FEATURE_2_6_MODULES) {
82                         const char *afterslash;
83
84                         afterslash = strrchr(argv[n], '/');
85                         if (!afterslash) afterslash = argv[n];
86                         else afterslash++;
87                         filename2modname(bb_common_bufsiz1, afterslash);
88                 }
89
90                 if (syscall(__NR_delete_module, ENABLE_FEATURE_2_6_MODULES ? bb_common_bufsiz1 : argv[n], flags)) {
91                         bb_perror_msg("%s", argv[n]);
92                         ret = EXIT_FAILURE;
93                 }
94         }
95
96         return ret;
97 }