Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Erik Andersen | 3d7e341 | 1999-12-16 23:04:20 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Mini rmmod implementation for busybox |
| 4 | * |
Eric Andersen | cb81e64 | 2003-07-14 21:21:08 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2003 by Erik Andersen <andersen@codepoet.org> |
Erik Andersen | 3d7e341 | 1999-12-16 23:04:20 +0000 | [diff] [blame] | 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 | |
Erik Andersen | 3d7e341 | 1999-12-16 23:04:20 +0000 | [diff] [blame] | 23 | #include <stdio.h> |
| 24 | #include <errno.h> |
| 25 | #include <unistd.h> |
Eric Andersen | ed3ef50 | 2001-01-27 08:24:39 +0000 | [diff] [blame] | 26 | #include <stdlib.h> |
Mark Whitley | f90c28d | 2001-03-09 21:49:12 +0000 | [diff] [blame] | 27 | #include <getopt.h> |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 28 | #include "busybox.h" |
Erik Andersen | 3d7e341 | 1999-12-16 23:04:20 +0000 | [diff] [blame] | 29 | |
Eric Andersen | e76c3b0 | 2001-04-05 03:14:39 +0000 | [diff] [blame] | 30 | extern int delete_module(const char * name); |
Erik Andersen | 3d7e341 | 1999-12-16 23:04:20 +0000 | [diff] [blame] | 31 | |
| 32 | |
Erik Andersen | 3d7e341 | 1999-12-16 23:04:20 +0000 | [diff] [blame] | 33 | extern int rmmod_main(int argc, char **argv) |
| 34 | { |
Mark Whitley | f90c28d | 2001-03-09 21:49:12 +0000 | [diff] [blame] | 35 | int n, ret = EXIT_SUCCESS; |
Tim Riker | cf93274 | 2002-12-14 01:58:59 +0000 | [diff] [blame] | 36 | 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 Andersen | 3d7e341 | 1999-12-16 23:04:20 +0000 | [diff] [blame] | 40 | |
Mark Whitley | f90c28d | 2001-03-09 21:49:12 +0000 | [diff] [blame] | 41 | /* Parse command line. */ |
| 42 | while ((n = getopt(argc, argv, "a")) != EOF) { |
| 43 | switch (n) { |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 44 | case 'a': |
| 45 | /* Unload _all_ unused modules via NULL delete_module() call */ |
Tim Riker | cf93274 | 2002-12-14 01:58:59 +0000 | [diff] [blame] | 46 | /* 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 | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 50 | bb_perror_msg_and_die("rmmod"); |
Tim Riker | cf93274 | 2002-12-14 01:58:59 +0000 | [diff] [blame] | 51 | pnmod = nmod; |
| 52 | /* 1 == QM_MODULES */ |
| 53 | if (my_query_module(NULL, 1, &buf, &bufsize, &nmod)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 54 | bb_perror_msg_and_die("QM_MODULES"); |
Tim Riker | cf93274 | 2002-12-14 01:58:59 +0000 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | #ifdef CONFIG_FEATURE_CLEAN_UP |
| 58 | free(buf); |
| 59 | #endif |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 60 | return EXIT_SUCCESS; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 61 | default: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 62 | bb_show_usage(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 63 | } |
Erik Andersen | 3d7e341 | 1999-12-16 23:04:20 +0000 | [diff] [blame] | 64 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 65 | |
Mark Whitley | f90c28d | 2001-03-09 21:49:12 +0000 | [diff] [blame] | 66 | if (optind == argc) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 67 | bb_show_usage(); |
Mark Whitley | f90c28d | 2001-03-09 21:49:12 +0000 | [diff] [blame] | 68 | |
| 69 | for (n = optind; n < argc; n++) { |
| 70 | if (delete_module(argv[n]) < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 71 | bb_perror_msg("%s", argv[n]); |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 72 | ret = EXIT_FAILURE; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 73 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 74 | } |
Mark Whitley | f90c28d | 2001-03-09 21:49:12 +0000 | [diff] [blame] | 75 | |
Eric Andersen | a5716d3 | 2000-07-28 15:16:37 +0000 | [diff] [blame] | 76 | return(ret); |
Erik Andersen | 3d7e341 | 1999-12-16 23:04:20 +0000 | [diff] [blame] | 77 | } |