blob: a4ea704104ed37a01a0bae45eeb11d72b2a5230e [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 Andersencb81e642003-07-14 21:21:08 +00005 * Copyright (C) 1999-2003 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 Andersencbe31da2001-02-20 06:14:08 +000028#include "busybox.h"
Erik Andersen3d7e3411999-12-16 23:04:20 +000029
Eric Andersene76c3b02001-04-05 03:14:39 +000030extern int delete_module(const char * name);
Erik Andersen3d7e3411999-12-16 23:04:20 +000031
32
Erik Andersen3d7e3411999-12-16 23:04:20 +000033extern int rmmod_main(int argc, char **argv)
34{
Mark Whitleyf90c28d2001-03-09 21:49:12 +000035 int n, ret = EXIT_SUCCESS;
Tim Rikercf932742002-12-14 01:58:59 +000036 size_t nmod = 0; /* number of modules */
37 size_t pnmod = -1; /* previous number of modules */
38 void *buf; /* hold the module names which we ignore but must get */
39 size_t bufsize = 0;
Erik Andersen3d7e3411999-12-16 23:04:20 +000040
Mark Whitleyf90c28d2001-03-09 21:49:12 +000041 /* Parse command line. */
42 while ((n = getopt(argc, argv, "a")) != EOF) {
43 switch (n) {
Erik Andersene49d5ec2000-02-08 19:58:47 +000044 case 'a':
45 /* Unload _all_ unused modules via NULL delete_module() call */
Tim Rikercf932742002-12-14 01:58:59 +000046 /* until the number of modules does not change */
47 buf = xmalloc(bufsize = 256);
48 while (nmod != pnmod) {
49 if (delete_module(NULL))
Manuel Novoa III cad53642003-03-19 09:13:01 +000050 bb_perror_msg_and_die("rmmod");
Tim Rikercf932742002-12-14 01:58:59 +000051 pnmod = nmod;
52 /* 1 == QM_MODULES */
53 if (my_query_module(NULL, 1, &buf, &bufsize, &nmod)) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000054 bb_perror_msg_and_die("QM_MODULES");
Tim Rikercf932742002-12-14 01:58:59 +000055 }
56 }
57#ifdef CONFIG_FEATURE_CLEAN_UP
58 free(buf);
59#endif
Matt Kraai3e856ce2000-12-01 02:55:13 +000060 return EXIT_SUCCESS;
Erik Andersene49d5ec2000-02-08 19:58:47 +000061 default:
Manuel Novoa III cad53642003-03-19 09:13:01 +000062 bb_show_usage();
Erik Andersene49d5ec2000-02-08 19:58:47 +000063 }
Erik Andersen3d7e3411999-12-16 23:04:20 +000064 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000065
Mark Whitleyf90c28d2001-03-09 21:49:12 +000066 if (optind == argc)
Manuel Novoa III cad53642003-03-19 09:13:01 +000067 bb_show_usage();
Mark Whitleyf90c28d2001-03-09 21:49:12 +000068
69 for (n = optind; n < argc; n++) {
70 if (delete_module(argv[n]) < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +000071 bb_perror_msg("%s", argv[n]);
Matt Kraai3e856ce2000-12-01 02:55:13 +000072 ret = EXIT_FAILURE;
Erik Andersene49d5ec2000-02-08 19:58:47 +000073 }
Erik Andersene49d5ec2000-02-08 19:58:47 +000074 }
Mark Whitleyf90c28d2001-03-09 21:49:12 +000075
Eric Andersena5716d32000-07-28 15:16:37 +000076 return(ret);
Erik Andersen3d7e3411999-12-16 23:04:20 +000077}