getopt_ulflags -> getopt32.
[platform/upstream/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 "busybox.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 {
43         int n, ret = EXIT_SUCCESS;
44         unsigned int flags = O_NONBLOCK|O_EXCL;
45
46         /* Parse command line. */
47         n = getopt32(argc, argv, "wfa");
48         if((n & 1))     // --wait
49                 flags &= ~O_NONBLOCK;
50         if((n & 2))     // --force
51                 flags |= O_TRUNC;
52         if((n & 4)) {
53                 /* Unload _all_ unused modules via NULL delete_module() call */
54                 /* until the number of modules does not change */
55                 size_t nmod = 0; /* number of modules */
56                 size_t pnmod = -1; /* previous number of modules */
57
58                 while (nmod != pnmod) {
59                         if (syscall(__NR_delete_module, NULL, flags) != 0) {
60                                 if (errno==EFAULT)
61                                         return(ret);
62                                 bb_perror_msg_and_die("rmmod");
63                         }
64                         pnmod = nmod;
65                         // the 1 here is QM_MODULES.
66                         if (ENABLE_FEATURE_QUERY_MODULE_INTERFACE && query_module(NULL,
67                                         1, bb_common_bufsiz1, sizeof(bb_common_bufsiz1),
68                                         &nmod))
69                         {
70                                 bb_perror_msg_and_die("QM_MODULES");
71                         }
72                 }
73                 return EXIT_SUCCESS;
74         }
75
76         if (optind == argc)
77                 bb_show_usage();
78
79         for (n = optind; n < argc; n++) {
80                 if (ENABLE_FEATURE_2_6_MODULES) {
81                         const char *afterslash;
82
83                         afterslash = strrchr(argv[n], '/');
84                         if (!afterslash) afterslash = argv[n];
85                         else afterslash++;
86                         filename2modname(bb_common_bufsiz1, afterslash);
87                 }
88
89                 if (syscall(__NR_delete_module, ENABLE_FEATURE_2_6_MODULES ? bb_common_bufsiz1 : argv[n], flags)) {
90                         bb_perror_msg("%s", argv[n]);
91                         ret = EXIT_FAILURE;
92                 }
93         }
94
95         return(ret);
96 }