blob: f4e65d0ce2f965d56721f50ff7cb59681d442c3d [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>
Eric Andersene7047882003-12-11 01:42:13 +000030#include <sys/syscall.h>
Eric Andersencbe31da2001-02-20 06:14:08 +000031#include "busybox.h"
Erik Andersen3d7e3411999-12-16 23:04:20 +000032
Eric Andersenc0693ed2004-07-20 10:05:13 +000033#ifdef CONFIG_FEATURE_2_6_MODULES
34static 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
Erik Andersen3d7e3411999-12-16 23:04:20 +000056extern int rmmod_main(int argc, char **argv)
57{
Mark Whitleyf90c28d2001-03-09 21:49:12 +000058 int n, ret = EXIT_SUCCESS;
Tim Rikercf932742002-12-14 01:58:59 +000059 size_t nmod = 0; /* number of modules */
60 size_t pnmod = -1; /* previous number of modules */
Eric Andersene8521f12004-07-13 00:09:34 +000061 unsigned int flags = O_NONBLOCK|O_EXCL;
62#ifdef CONFIG_FEATURE_QUERY_MODULE_INTERFACE
Tim Rikercf932742002-12-14 01:58:59 +000063 void *buf; /* hold the module names which we ignore but must get */
64 size_t bufsize = 0;
Eric Andersene8521f12004-07-13 00:09:34 +000065#endif
Erik Andersen3d7e3411999-12-16 23:04:20 +000066
Mark Whitleyf90c28d2001-03-09 21:49:12 +000067 /* Parse command line. */
68 while ((n = getopt(argc, argv, "a")) != EOF) {
69 switch (n) {
Eric Andersene7047882003-12-11 01:42:13 +000070 case 'w': // --wait
71 flags &= ~O_NONBLOCK;
Eric Andersenc7bda1c2004-03-15 08:29:22 +000072 break;
Eric Andersene7047882003-12-11 01:42:13 +000073 case 'f': // --force
74 flags |= O_TRUNC;
Eric Andersenc7bda1c2004-03-15 08:29:22 +000075 break;
Erik Andersene49d5ec2000-02-08 19:58:47 +000076 case 'a':
77 /* Unload _all_ unused modules via NULL delete_module() call */
Tim Rikercf932742002-12-14 01:58:59 +000078 /* until the number of modules does not change */
Eric Andersene8521f12004-07-13 00:09:34 +000079#ifdef CONFIG_FEATURE_QUERY_MODULE_INTERFACE
Tim Rikercf932742002-12-14 01:58:59 +000080 buf = xmalloc(bufsize = 256);
Eric Andersene8521f12004-07-13 00:09:34 +000081#endif
Tim Rikercf932742002-12-14 01:58:59 +000082 while (nmod != pnmod) {
Eric Andersene8521f12004-07-13 00:09:34 +000083 if (syscall(__NR_delete_module, NULL, flags) < 0) {
84 if (errno==EFAULT)
85 return(ret);
Manuel Novoa III cad53642003-03-19 09:13:01 +000086 bb_perror_msg_and_die("rmmod");
Eric Andersene8521f12004-07-13 00:09:34 +000087 }
Tim Rikercf932742002-12-14 01:58:59 +000088 pnmod = nmod;
Eric Andersene8521f12004-07-13 00:09:34 +000089#ifdef CONFIG_FEATURE_QUERY_MODULE_INTERFACE
Tim Rikercf932742002-12-14 01:58:59 +000090 /* 1 == QM_MODULES */
91 if (my_query_module(NULL, 1, &buf, &bufsize, &nmod)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000092 bb_perror_msg_and_die("QM_MODULES");
Tim Rikercf932742002-12-14 01:58:59 +000093 }
Eric Andersene8521f12004-07-13 00:09:34 +000094#endif
Tim Rikercf932742002-12-14 01:58:59 +000095 }
Eric Andersene8521f12004-07-13 00:09:34 +000096#if defined CONFIG_FEATURE_CLEAN_UP && CONFIG_FEATURE_QUERY_MODULE_INTERFACE
Tim Rikercf932742002-12-14 01:58:59 +000097 free(buf);
98#endif
Matt Kraai3e856ce2000-12-01 02:55:13 +000099 return EXIT_SUCCESS;
Erik Andersene49d5ec2000-02-08 19:58:47 +0000100 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +0000101 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +0000102 }
Erik Andersen3d7e3411999-12-16 23:04:20 +0000103 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000104
Mark Whitleyf90c28d2001-03-09 21:49:12 +0000105 if (optind == argc)
Eric Andersen3b1a7442003-12-24 20:30:45 +0000106 bb_show_usage();
Mark Whitleyf90c28d2001-03-09 21:49:12 +0000107
Eric Andersenc0693ed2004-07-20 10:05:13 +0000108 {
Robert Grieblbd8dd1e2004-07-20 18:36:51 +0000109 for (n = optind; n < argc; n++) {
Eric Andersenc0693ed2004-07-20 10:05:13 +0000110#ifdef CONFIG_FEATURE_2_6_MODULES
Robert Grieblbd8dd1e2004-07-20 18:36:51 +0000111 char module_name[strlen(argv[n]) + 1];
112 filename2modname(module_name, argv[n]);
Eric Andersenc0693ed2004-07-20 10:05:13 +0000113#else
114#define module_name argv[n]
115#endif
Eric Andersenc0693ed2004-07-20 10:05:13 +0000116 if (syscall(__NR_delete_module, module_name, flags) < 0) {
117 bb_perror_msg("%s", argv[n]);
118 ret = EXIT_FAILURE;
119 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000120 }
Erik Andersene49d5ec2000-02-08 19:58:47 +0000121 }
Mark Whitleyf90c28d2001-03-09 21:49:12 +0000122
Eric Andersena5716d32000-07-28 15:16:37 +0000123 return(ret);
Erik Andersen3d7e3411999-12-16 23:04:20 +0000124}