Deal with the fact that 2.6.x kernels replace any '-'s in the
[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  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  *
21  */
22
23 #include <stdio.h>
24 #include <errno.h>
25 #include <unistd.h>
26 #include <stdlib.h>
27 #include <getopt.h>
28 #include <fcntl.h>
29 #include <string.h>
30 #include <sys/syscall.h>
31 #include "busybox.h"
32
33 #ifdef CONFIG_FEATURE_2_6_MODULES
34 static inline void filename2modname(char *modname, const char *filename)
35 {
36         const char *afterslash;
37         unsigned int i;
38
39         afterslash = strrchr(filename, '/');
40         if (!afterslash)
41                 afterslash = filename;
42         else
43                 afterslash++;
44
45         /* Convert to underscores, stop at first . */
46         for (i = 0; afterslash[i] && afterslash[i] != '.'; i++) {
47                 if (afterslash[i] == '-')
48                         modname[i] = '_';
49                 else
50                         modname[i] = afterslash[i];
51         }
52         modname[i] = '\0';
53 }
54 #endif
55
56 extern int rmmod_main(int argc, char **argv)
57 {
58         int n, ret = EXIT_SUCCESS;
59         size_t nmod = 0; /* number of modules */
60         size_t pnmod = -1; /* previous number of modules */
61         unsigned int flags = O_NONBLOCK|O_EXCL;
62 #ifdef CONFIG_FEATURE_QUERY_MODULE_INTERFACE
63         void *buf; /* hold the module names which we ignore but must get */
64         size_t bufsize = 0;
65 #endif
66
67         /* Parse command line. */
68         while ((n = getopt(argc, argv, "a")) != EOF) {
69                 switch (n) {
70                         case 'w':       // --wait
71                                 flags &= ~O_NONBLOCK;
72                                 break;
73                         case 'f':       // --force
74                                 flags |= O_TRUNC;
75                                 break;
76                         case 'a':
77                                 /* Unload _all_ unused modules via NULL delete_module() call */
78                                 /* until the number of modules does not change */
79 #ifdef CONFIG_FEATURE_QUERY_MODULE_INTERFACE
80                                 buf = xmalloc(bufsize = 256);
81 #endif
82                                 while (nmod != pnmod) {
83                                         if (syscall(__NR_delete_module, NULL, flags) < 0) {
84                                                 if (errno==EFAULT)
85                                                         return(ret);
86                                                 bb_perror_msg_and_die("rmmod");
87                                         }
88                                         pnmod = nmod;
89 #ifdef CONFIG_FEATURE_QUERY_MODULE_INTERFACE
90                                         /* 1 == QM_MODULES */
91                                         if (my_query_module(NULL, 1, &buf, &bufsize, &nmod)) {
92                                                 bb_perror_msg_and_die("QM_MODULES");
93                                         }
94 #endif
95                                 }
96 #if defined CONFIG_FEATURE_CLEAN_UP && CONFIG_FEATURE_QUERY_MODULE_INTERFACE
97                                 free(buf);
98 #endif
99                                 return EXIT_SUCCESS;
100                         default:
101                                 bb_show_usage();
102                 }
103         }
104
105         if (optind == argc)
106                 bb_show_usage();
107
108         {
109 #ifdef CONFIG_FEATURE_2_6_MODULES
110                 char module_name[strlen(argv[n]) + 1];
111                 filename2modname(module_name, argv[n]);
112 #else
113 #define module_name             argv[n]
114 #endif
115                 for (n = optind; n < argc; n++) {
116                         if (syscall(__NR_delete_module, module_name, flags) < 0) {
117                                 bb_perror_msg("%s", argv[n]);
118                                 ret = EXIT_FAILURE;
119                         }
120                 }
121         }
122
123         return(ret);
124 }