Prevent "`bootp_down' was declared implicitly `extern' and later `static'" warning
[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  * 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 <sys/syscall.h>
30 #include "busybox.h"
31
32 extern int rmmod_main(int argc, char **argv)
33 {
34         int n, ret = EXIT_SUCCESS;
35         size_t nmod = 0; /* number of modules */
36         size_t pnmod = -1; /* previous number of modules */
37         unsigned int flags = O_NONBLOCK|O_EXCL;
38 #ifdef CONFIG_FEATURE_QUERY_MODULE_INTERFACE
39         void *buf; /* hold the module names which we ignore but must get */
40         size_t bufsize = 0;
41 #endif
42
43         /* Parse command line. */
44         while ((n = getopt(argc, argv, "a")) != EOF) {
45                 switch (n) {
46                         case 'w':       // --wait
47                                 flags &= ~O_NONBLOCK;
48                                 break;
49                         case 'f':       // --force
50                                 flags |= O_TRUNC;
51                                 break;
52                         case 'a':
53                                 /* Unload _all_ unused modules via NULL delete_module() call */
54                                 /* until the number of modules does not change */
55 #ifdef CONFIG_FEATURE_QUERY_MODULE_INTERFACE
56                                 buf = xmalloc(bufsize = 256);
57 #endif
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 #ifdef CONFIG_FEATURE_QUERY_MODULE_INTERFACE
66                                         /* 1 == QM_MODULES */
67                                         if (my_query_module(NULL, 1, &buf, &bufsize, &nmod)) {
68                                                 bb_perror_msg_and_die("QM_MODULES");
69                                         }
70 #endif
71                                 }
72 #if defined CONFIG_FEATURE_CLEAN_UP && CONFIG_FEATURE_QUERY_MODULE_INTERFACE
73                                 free(buf);
74 #endif
75                                 return EXIT_SUCCESS;
76                         default:
77                                 bb_show_usage();
78                 }
79         }
80
81         if (optind == argc)
82                 bb_show_usage();
83
84         for (n = optind; n < argc; n++) {
85                 if (syscall(__NR_delete_module, argv[n], flags) < 0) {
86                         bb_perror_msg("%s", argv[n]);
87                         ret = EXIT_FAILURE;
88                 }
89         }
90
91         return(ret);
92 }