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 | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2004 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 | e704788 | 2003-12-11 01:42:13 +0000 | [diff] [blame] | 28 | #include <fcntl.h> |
Eric Andersen | c0693ed | 2004-07-20 10:05:13 +0000 | [diff] [blame] | 29 | #include <string.h> |
Rob Landley | 5221987 | 2005-11-27 19:01:53 +0000 | [diff] [blame^] | 30 | #include <sys/utsname.h> |
Eric Andersen | e704788 | 2003-12-11 01:42:13 +0000 | [diff] [blame] | 31 | #include <sys/syscall.h> |
Eric Andersen | cbe31da | 2001-02-20 06:14:08 +0000 | [diff] [blame] | 32 | #include "busybox.h" |
Erik Andersen | 3d7e341 | 1999-12-16 23:04:20 +0000 | [diff] [blame] | 33 | |
Eric Andersen | c0693ed | 2004-07-20 10:05:13 +0000 | [diff] [blame] | 34 | #ifdef CONFIG_FEATURE_2_6_MODULES |
| 35 | static inline void filename2modname(char *modname, const char *filename) |
| 36 | { |
| 37 | const char *afterslash; |
| 38 | unsigned int i; |
| 39 | |
| 40 | afterslash = strrchr(filename, '/'); |
| 41 | if (!afterslash) |
| 42 | afterslash = filename; |
| 43 | else |
| 44 | afterslash++; |
| 45 | |
| 46 | /* Convert to underscores, stop at first . */ |
| 47 | for (i = 0; afterslash[i] && afterslash[i] != '.'; i++) { |
Rob Landley | 5221987 | 2005-11-27 19:01:53 +0000 | [diff] [blame^] | 48 | int kr_chk = 1; |
| 49 | |
| 50 | if (ENABLE_FEATURE_2_4_MODULES) { |
| 51 | struct utsname uname_info; |
| 52 | if (uname(&uname_info) == -1) |
| 53 | bb_error_msg_and_die("cannot get uname data"); |
| 54 | if (strcmp(uname_info.release, "2.6") < 0) |
| 55 | kr_chk = 0; |
| 56 | } |
| 57 | if (kr_chk && (afterslash[i] == '-')) |
Eric Andersen | c0693ed | 2004-07-20 10:05:13 +0000 | [diff] [blame] | 58 | modname[i] = '_'; |
| 59 | else |
| 60 | modname[i] = afterslash[i]; |
| 61 | } |
| 62 | modname[i] = '\0'; |
| 63 | } |
| 64 | #endif |
| 65 | |
Erik Andersen | 3d7e341 | 1999-12-16 23:04:20 +0000 | [diff] [blame] | 66 | extern int rmmod_main(int argc, char **argv) |
| 67 | { |
Mark Whitley | f90c28d | 2001-03-09 21:49:12 +0000 | [diff] [blame] | 68 | int n, ret = EXIT_SUCCESS; |
Tim Riker | cf93274 | 2002-12-14 01:58:59 +0000 | [diff] [blame] | 69 | size_t nmod = 0; /* number of modules */ |
| 70 | size_t pnmod = -1; /* previous number of modules */ |
Eric Andersen | e8521f1 | 2004-07-13 00:09:34 +0000 | [diff] [blame] | 71 | unsigned int flags = O_NONBLOCK|O_EXCL; |
| 72 | #ifdef CONFIG_FEATURE_QUERY_MODULE_INTERFACE |
Tim Riker | cf93274 | 2002-12-14 01:58:59 +0000 | [diff] [blame] | 73 | void *buf; /* hold the module names which we ignore but must get */ |
| 74 | size_t bufsize = 0; |
Eric Andersen | e8521f1 | 2004-07-13 00:09:34 +0000 | [diff] [blame] | 75 | #endif |
Erik Andersen | 3d7e341 | 1999-12-16 23:04:20 +0000 | [diff] [blame] | 76 | |
Mark Whitley | f90c28d | 2001-03-09 21:49:12 +0000 | [diff] [blame] | 77 | /* Parse command line. */ |
| 78 | while ((n = getopt(argc, argv, "a")) != EOF) { |
| 79 | switch (n) { |
Eric Andersen | e704788 | 2003-12-11 01:42:13 +0000 | [diff] [blame] | 80 | case 'w': // --wait |
| 81 | flags &= ~O_NONBLOCK; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 82 | break; |
Eric Andersen | e704788 | 2003-12-11 01:42:13 +0000 | [diff] [blame] | 83 | case 'f': // --force |
| 84 | flags |= O_TRUNC; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 85 | break; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 86 | case 'a': |
| 87 | /* Unload _all_ unused modules via NULL delete_module() call */ |
Tim Riker | cf93274 | 2002-12-14 01:58:59 +0000 | [diff] [blame] | 88 | /* until the number of modules does not change */ |
Eric Andersen | e8521f1 | 2004-07-13 00:09:34 +0000 | [diff] [blame] | 89 | #ifdef CONFIG_FEATURE_QUERY_MODULE_INTERFACE |
Tim Riker | cf93274 | 2002-12-14 01:58:59 +0000 | [diff] [blame] | 90 | buf = xmalloc(bufsize = 256); |
Eric Andersen | e8521f1 | 2004-07-13 00:09:34 +0000 | [diff] [blame] | 91 | #endif |
Tim Riker | cf93274 | 2002-12-14 01:58:59 +0000 | [diff] [blame] | 92 | while (nmod != pnmod) { |
Eric Andersen | e8521f1 | 2004-07-13 00:09:34 +0000 | [diff] [blame] | 93 | if (syscall(__NR_delete_module, NULL, flags) < 0) { |
| 94 | if (errno==EFAULT) |
| 95 | return(ret); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 96 | bb_perror_msg_and_die("rmmod"); |
Eric Andersen | e8521f1 | 2004-07-13 00:09:34 +0000 | [diff] [blame] | 97 | } |
Tim Riker | cf93274 | 2002-12-14 01:58:59 +0000 | [diff] [blame] | 98 | pnmod = nmod; |
Eric Andersen | e8521f1 | 2004-07-13 00:09:34 +0000 | [diff] [blame] | 99 | #ifdef CONFIG_FEATURE_QUERY_MODULE_INTERFACE |
Tim Riker | cf93274 | 2002-12-14 01:58:59 +0000 | [diff] [blame] | 100 | /* 1 == QM_MODULES */ |
| 101 | if (my_query_module(NULL, 1, &buf, &bufsize, &nmod)) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 102 | bb_perror_msg_and_die("QM_MODULES"); |
Tim Riker | cf93274 | 2002-12-14 01:58:59 +0000 | [diff] [blame] | 103 | } |
Eric Andersen | e8521f1 | 2004-07-13 00:09:34 +0000 | [diff] [blame] | 104 | #endif |
Tim Riker | cf93274 | 2002-12-14 01:58:59 +0000 | [diff] [blame] | 105 | } |
Eric Andersen | e8521f1 | 2004-07-13 00:09:34 +0000 | [diff] [blame] | 106 | #if defined CONFIG_FEATURE_CLEAN_UP && CONFIG_FEATURE_QUERY_MODULE_INTERFACE |
Tim Riker | cf93274 | 2002-12-14 01:58:59 +0000 | [diff] [blame] | 107 | free(buf); |
| 108 | #endif |
Matt Kraai | 3e856ce | 2000-12-01 02:55:13 +0000 | [diff] [blame] | 109 | return EXIT_SUCCESS; |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 110 | default: |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 111 | bb_show_usage(); |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 112 | } |
Erik Andersen | 3d7e341 | 1999-12-16 23:04:20 +0000 | [diff] [blame] | 113 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 114 | |
Mark Whitley | f90c28d | 2001-03-09 21:49:12 +0000 | [diff] [blame] | 115 | if (optind == argc) |
Eric Andersen | 3b1a744 | 2003-12-24 20:30:45 +0000 | [diff] [blame] | 116 | bb_show_usage(); |
Mark Whitley | f90c28d | 2001-03-09 21:49:12 +0000 | [diff] [blame] | 117 | |
Eric Andersen | c0693ed | 2004-07-20 10:05:13 +0000 | [diff] [blame] | 118 | { |
Robert Griebl | bd8dd1e | 2004-07-20 18:36:51 +0000 | [diff] [blame] | 119 | for (n = optind; n < argc; n++) { |
Eric Andersen | c0693ed | 2004-07-20 10:05:13 +0000 | [diff] [blame] | 120 | #ifdef CONFIG_FEATURE_2_6_MODULES |
Robert Griebl | bd8dd1e | 2004-07-20 18:36:51 +0000 | [diff] [blame] | 121 | char module_name[strlen(argv[n]) + 1]; |
| 122 | filename2modname(module_name, argv[n]); |
Eric Andersen | c0693ed | 2004-07-20 10:05:13 +0000 | [diff] [blame] | 123 | #else |
| 124 | #define module_name argv[n] |
| 125 | #endif |
Eric Andersen | c0693ed | 2004-07-20 10:05:13 +0000 | [diff] [blame] | 126 | if (syscall(__NR_delete_module, module_name, flags) < 0) { |
| 127 | bb_perror_msg("%s", argv[n]); |
| 128 | ret = EXIT_FAILURE; |
| 129 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 130 | } |
Erik Andersen | e49d5ec | 2000-02-08 19:58:47 +0000 | [diff] [blame] | 131 | } |
Mark Whitley | f90c28d | 2001-03-09 21:49:12 +0000 | [diff] [blame] | 132 | |
Eric Andersen | a5716d3 | 2000-07-28 15:16:37 +0000 | [diff] [blame] | 133 | return(ret); |
Erik Andersen | 3d7e341 | 1999-12-16 23:04:20 +0000 | [diff] [blame] | 134 | } |