blob: 12580c5cf98b5f63e49506ad1a56e00107d288dc [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Erik Andersen3d7e3411999-12-16 23:04:20 +00002/*
3 * Mini rmmod implementation for busybox
4 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Erik Andersen3d7e3411999-12-16 23:04:20 +00006 *
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
Erik Andersen3d7e3411999-12-16 23:04:20 +000023#include <stdio.h>
24#include <errno.h>
25#include <unistd.h>
Eric Andersened3ef502001-01-27 08:24:39 +000026#include <stdlib.h>
Mark Whitleyf90c28d2001-03-09 21:49:12 +000027#include <getopt.h>
Eric Andersene7047882003-12-11 01:42:13 +000028#include <fcntl.h>
Eric Andersenc0693ed2004-07-20 10:05:13 +000029#include <string.h>
Rob Landley52219872005-11-27 19:01:53 +000030#include <sys/utsname.h>
Eric Andersene7047882003-12-11 01:42:13 +000031#include <sys/syscall.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000032#include "busybox.h"
Erik Andersen3d7e3411999-12-16 23:04:20 +000033
Eric Andersenc0693ed2004-07-20 10:05:13 +000034#ifdef CONFIG_FEATURE_2_6_MODULES
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000035static inline void filename2modname(char *modname, const char *afterslash)
Eric Andersenc0693ed2004-07-20 10:05:13 +000036{
Eric Andersenc0693ed2004-07-20 10:05:13 +000037 unsigned int i;
38
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000039#if ENABLE_FEATURE_2_4_MODULES
40 int kr_chk = 1;
41 if (get_kernel_revision() <= 2*65536+6*256)
42 kr_chk = 0;
43#else
44#define kr_chk 1
45#endif
Eric Andersenc0693ed2004-07-20 10:05:13 +000046
47 /* Convert to underscores, stop at first . */
48 for (i = 0; afterslash[i] && afterslash[i] != '.'; i++) {
Rob Landley52219872005-11-27 19:01:53 +000049 if (kr_chk && (afterslash[i] == '-'))
Eric Andersenc0693ed2004-07-20 10:05:13 +000050 modname[i] = '_';
51 else
52 modname[i] = afterslash[i];
53 }
54 modname[i] = '\0';
55}
56#endif
57
Erik Andersen3d7e3411999-12-16 23:04:20 +000058extern int rmmod_main(int argc, char **argv)
59{
Mark Whitleyf90c28d2001-03-09 21:49:12 +000060 int n, ret = EXIT_SUCCESS;
Eric Andersene8521f12004-07-13 00:09:34 +000061 unsigned int flags = O_NONBLOCK|O_EXCL;
62#ifdef CONFIG_FEATURE_QUERY_MODULE_INTERFACE
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000063 /* bb_common_bufsiz1 hold the module names which we ignore
64 but must get */
65 size_t bufsize = sizeof(bb_common_bufsiz1);
Eric Andersene8521f12004-07-13 00:09:34 +000066#endif
Erik Andersen3d7e3411999-12-16 23:04:20 +000067
Mark Whitleyf90c28d2001-03-09 21:49:12 +000068 /* Parse command line. */
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000069 n = bb_getopt_ulflags(argc, argv, "wfa");
70 if((n & 1)) // --wait
71 flags &= ~O_NONBLOCK;
72 if((n & 2)) // --force
73 flags |= O_TRUNC;
74 if((n & 4)) {
75 /* Unload _all_ unused modules via NULL delete_module() call */
76 /* until the number of modules does not change */
77 size_t nmod = 0; /* number of modules */
78 size_t pnmod = -1; /* previous number of modules */
79
80 while (nmod != pnmod) {
81 if (syscall(__NR_delete_module, NULL, flags) < 0) {
82 if (errno==EFAULT)
83 return(ret);
84 bb_perror_msg_and_die("rmmod");
85 }
86 pnmod = nmod;
Eric Andersene8521f12004-07-13 00:09:34 +000087#ifdef CONFIG_FEATURE_QUERY_MODULE_INTERFACE
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000088 /* 1 == QM_MODULES */
89 if (my_query_module(NULL, 1, &bb_common_bufsiz1, &bufsize, &nmod)) {
90 bb_perror_msg_and_die("QM_MODULES");
91 }
Eric Andersene8521f12004-07-13 00:09:34 +000092#endif
Erik Andersene49d5ec2000-02-08 19:58:47 +000093 }
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +000094 return EXIT_SUCCESS;
Erik Andersen3d7e3411999-12-16 23:04:20 +000095 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000096
Mark Whitleyf90c28d2001-03-09 21:49:12 +000097 if (optind == argc)
Eric Andersen3b1a7442003-12-24 20:30:45 +000098 bb_show_usage();
Mark Whitleyf90c28d2001-03-09 21:49:12 +000099
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +0000100 for (n = optind; n < argc; n++) {
Eric Andersenc0693ed2004-07-20 10:05:13 +0000101#ifdef CONFIG_FEATURE_2_6_MODULES
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +0000102 const char *afterslash;
103 char *module_name;
104
105 afterslash = strrchr(argv[n], '/');
106 if (!afterslash)
107 afterslash = argv[n];
108 else
109 afterslash++;
110 module_name = alloca(strlen(afterslash) + 1);
111 filename2modname(module_name, afterslash);
Eric Andersenc0693ed2004-07-20 10:05:13 +0000112#else
113#define module_name argv[n]
114#endif
"Vladimir N. Oleynik"8c44f012005-11-28 15:54:22 +0000115 if (syscall(__NR_delete_module, module_name, flags) < 0) {
116 bb_perror_msg("%s", argv[n]);
117 ret = EXIT_FAILURE;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000118 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000119 }
Mark Whitleyf90c28d2001-03-09 21:49:12 +0000120
Eric Andersena5716d32000-07-28 15:16:37 +0000121 return(ret);
Erik Andersen3d7e3411999-12-16 23:04:20 +0000122}