Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 1 | /* |
| 2 | * kmod-rmmod - remove modules from linux kernel using libkmod. |
| 3 | * |
Lucas De Marchi | e6b0e49 | 2013-01-16 11:27:21 -0200 | [diff] [blame] | 4 | * Copyright (C) 2011-2013 ProFUSION embedded systems |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 5 | * |
Lucas De Marchi | cb451f3 | 2011-12-12 18:24:35 -0200 | [diff] [blame] | 6 | * This program is free software: you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation, either version 2 of the License, or |
| 9 | * (at your option) any later version. |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
Lucas De Marchi | cb451f3 | 2011-12-12 18:24:35 -0200 | [diff] [blame] | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 15 | * |
Lucas De Marchi | cb451f3 | 2011-12-12 18:24:35 -0200 | [diff] [blame] | 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 18 | */ |
Lucas De Marchi | cb451f3 | 2011-12-12 18:24:35 -0200 | [diff] [blame] | 19 | |
Lucas De Marchi | c2e4286 | 2014-10-03 01:41:42 -0300 | [diff] [blame] | 20 | #include <errno.h> |
| 21 | #include <getopt.h> |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 24 | #include <string.h> |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 25 | #include <unistd.h> |
Lucas De Marchi | c2e4286 | 2014-10-03 01:41:42 -0300 | [diff] [blame] | 26 | #include <sys/stat.h> |
| 27 | #include <sys/types.h> |
| 28 | |
Lucas De Marchi | 576dd43 | 2014-10-02 22:03:19 -0300 | [diff] [blame] | 29 | #include <shared/macro.h> |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 30 | |
Lucas De Marchi | f357866 | 2015-01-02 12:38:27 -0200 | [diff] [blame] | 31 | #include <libkmod/libkmod.h> |
Lucas De Marchi | c2e4286 | 2014-10-03 01:41:42 -0300 | [diff] [blame] | 32 | |
Lucas De Marchi | 4a2e20d | 2012-11-06 16:54:17 -0200 | [diff] [blame] | 33 | #include "kmod.h" |
| 34 | |
Lucas De Marchi | e6996c5 | 2012-11-05 18:32:05 -0200 | [diff] [blame] | 35 | #define DEFAULT_VERBOSE LOG_ERR |
| 36 | static int verbose = DEFAULT_VERBOSE; |
| 37 | static int use_syslog; |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 38 | |
| 39 | static const char cmdopts_s[] = "fsvVwh"; |
| 40 | static const struct option cmdopts[] = { |
| 41 | {"force", no_argument, 0, 'f'}, |
| 42 | {"syslog", no_argument, 0, 's'}, |
| 43 | {"verbose", no_argument, 0, 'v'}, |
| 44 | {"version", no_argument, 0, 'V'}, |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 45 | {"help", no_argument, 0, 'h'}, |
| 46 | {NULL, 0, 0, 0} |
| 47 | }; |
| 48 | |
Lucas De Marchi | 4a2e20d | 2012-11-06 16:54:17 -0200 | [diff] [blame] | 49 | static void help(void) |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 50 | { |
Lucas De Marchi | 34e06bf | 2012-11-06 17:32:41 -0200 | [diff] [blame] | 51 | printf("Usage:\n" |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 52 | "\t%s [options] modulename ...\n" |
| 53 | "Options:\n" |
| 54 | "\t-f, --force forces a module unload and may crash your\n" |
| 55 | "\t machine. This requires Forced Module Removal\n" |
| 56 | "\t option in your kernel. DANGEROUS\n" |
| 57 | "\t-s, --syslog print to syslog, not stderr\n" |
| 58 | "\t-v, --verbose enables more messages\n" |
| 59 | "\t-V, --version show version\n" |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 60 | "\t-h, --help show this help\n", |
Lucas De Marchi | 7c04aee | 2012-11-06 19:20:09 -0200 | [diff] [blame] | 61 | program_invocation_short_name); |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 62 | } |
| 63 | |
Dave Reisner | 6da9cdf | 2012-01-10 19:28:30 -0500 | [diff] [blame] | 64 | static int check_module_inuse(struct kmod_module *mod) { |
| 65 | struct kmod_list *holders; |
Josh Boyer | d53abca | 2013-03-15 13:43:40 -0400 | [diff] [blame] | 66 | int state; |
Dave Reisner | 6da9cdf | 2012-01-10 19:28:30 -0500 | [diff] [blame] | 67 | |
Josh Boyer | d53abca | 2013-03-15 13:43:40 -0400 | [diff] [blame] | 68 | state = kmod_module_get_initstate(mod); |
| 69 | |
| 70 | if (state == KMOD_MODULE_BUILTIN) { |
| 71 | ERR("Module %s is builtin.\n", kmod_module_get_name(mod)); |
| 72 | return -ENOENT; |
| 73 | } else if (state < 0) { |
Lucas De Marchi | 04c26d2 | 2012-11-05 17:51:32 -0200 | [diff] [blame] | 74 | ERR("Module %s is not currently loaded\n", |
Dave Reisner | 6da9cdf | 2012-01-10 19:28:30 -0500 | [diff] [blame] | 75 | kmod_module_get_name(mod)); |
| 76 | return -ENOENT; |
| 77 | } |
| 78 | |
| 79 | holders = kmod_module_get_holders(mod); |
| 80 | if (holders != NULL) { |
| 81 | struct kmod_list *itr; |
| 82 | |
Lucas De Marchi | 04c26d2 | 2012-11-05 17:51:32 -0200 | [diff] [blame] | 83 | ERR("Module %s is in use by:", kmod_module_get_name(mod)); |
Dave Reisner | 6da9cdf | 2012-01-10 19:28:30 -0500 | [diff] [blame] | 84 | |
| 85 | kmod_list_foreach(itr, holders) { |
| 86 | struct kmod_module *hm = kmod_module_get_module(itr); |
| 87 | fprintf(stderr, " %s", kmod_module_get_name(hm)); |
| 88 | kmod_module_unref(hm); |
| 89 | } |
| 90 | fputc('\n', stderr); |
| 91 | |
| 92 | kmod_module_unref_list(holders); |
| 93 | return -EBUSY; |
| 94 | } |
| 95 | |
| 96 | if (kmod_module_get_refcnt(mod) != 0) { |
Lucas De Marchi | 04c26d2 | 2012-11-05 17:51:32 -0200 | [diff] [blame] | 97 | ERR("Module %s is in use\n", kmod_module_get_name(mod)); |
Dave Reisner | 6da9cdf | 2012-01-10 19:28:30 -0500 | [diff] [blame] | 98 | return -EBUSY; |
| 99 | } |
| 100 | |
| 101 | return 0; |
| 102 | } |
| 103 | |
Lucas De Marchi | f712ebc | 2011-12-22 03:39:11 -0200 | [diff] [blame] | 104 | static int do_rmmod(int argc, char *argv[]) |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 105 | { |
| 106 | struct kmod_ctx *ctx; |
Lucas De Marchi | 2411c07 | 2011-12-12 15:41:02 -0200 | [diff] [blame] | 107 | const char *null_config = NULL; |
Lucas De Marchi | 017893f | 2013-09-20 01:40:58 -0500 | [diff] [blame] | 108 | int flags = 0; |
Dave Reisner | 6da9cdf | 2012-01-10 19:28:30 -0500 | [diff] [blame] | 109 | int i, err, r = 0; |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 110 | |
| 111 | for (;;) { |
| 112 | int c, idx = 0; |
| 113 | c = getopt_long(argc, argv, cmdopts_s, cmdopts, &idx); |
| 114 | if (c == -1) |
| 115 | break; |
| 116 | switch (c) { |
| 117 | case 'f': |
| 118 | flags |= KMOD_REMOVE_FORCE; |
| 119 | break; |
| 120 | case 's': |
| 121 | use_syslog = 1; |
| 122 | break; |
| 123 | case 'v': |
| 124 | verbose++; |
| 125 | break; |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 126 | case 'h': |
Lucas De Marchi | 4a2e20d | 2012-11-06 16:54:17 -0200 | [diff] [blame] | 127 | help(); |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 128 | return EXIT_SUCCESS; |
| 129 | case 'V': |
| 130 | puts(PACKAGE " version " VERSION); |
Lucas De Marchi | 655de27 | 2015-06-07 02:46:22 -0300 | [diff] [blame^] | 131 | puts(KMOD_FEATURES); |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 132 | return EXIT_SUCCESS; |
| 133 | case '?': |
| 134 | return EXIT_FAILURE; |
| 135 | default: |
Lucas De Marchi | 04c26d2 | 2012-11-05 17:51:32 -0200 | [diff] [blame] | 136 | ERR("unexpected getopt_long() value '%c'.\n", c); |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 137 | return EXIT_FAILURE; |
| 138 | } |
| 139 | } |
| 140 | |
Lucas De Marchi | 92aad74 | 2012-11-06 18:04:09 -0200 | [diff] [blame] | 141 | log_open(use_syslog); |
Lucas De Marchi | e6996c5 | 2012-11-05 18:32:05 -0200 | [diff] [blame] | 142 | |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 143 | if (optind >= argc) { |
Lucas De Marchi | 04c26d2 | 2012-11-05 17:51:32 -0200 | [diff] [blame] | 144 | ERR("missing module name.\n"); |
Lucas De Marchi | e6996c5 | 2012-11-05 18:32:05 -0200 | [diff] [blame] | 145 | r = EXIT_FAILURE; |
| 146 | goto done; |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 147 | } |
| 148 | |
Lucas De Marchi | 2411c07 | 2011-12-12 15:41:02 -0200 | [diff] [blame] | 149 | ctx = kmod_new(NULL, &null_config); |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 150 | if (!ctx) { |
Lucas De Marchi | 04c26d2 | 2012-11-05 17:51:32 -0200 | [diff] [blame] | 151 | ERR("kmod_new() failed!\n"); |
Lucas De Marchi | e6996c5 | 2012-11-05 18:32:05 -0200 | [diff] [blame] | 152 | r = EXIT_FAILURE; |
| 153 | goto done; |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 154 | } |
| 155 | |
Lucas De Marchi | 52a50fe | 2012-11-06 18:26:34 -0200 | [diff] [blame] | 156 | log_setup_kmod_log(ctx, verbose); |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 157 | |
| 158 | for (i = optind; i < argc; i++) { |
| 159 | struct kmod_module *mod; |
| 160 | const char *arg = argv[i]; |
| 161 | struct stat st; |
| 162 | if (stat(arg, &st) == 0) |
| 163 | err = kmod_module_new_from_path(ctx, arg, &mod); |
| 164 | else |
| 165 | err = kmod_module_new_from_name(ctx, arg, &mod); |
| 166 | |
| 167 | if (err < 0) { |
Lucas De Marchi | 04c26d2 | 2012-11-05 17:51:32 -0200 | [diff] [blame] | 168 | ERR("could not use module %s: %s\n", arg, |
| 169 | strerror(-err)); |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 170 | break; |
| 171 | } |
| 172 | |
Lucas De Marchi | 017893f | 2013-09-20 01:40:58 -0500 | [diff] [blame] | 173 | if (!(flags & KMOD_REMOVE_FORCE) && check_module_inuse(mod) < 0) { |
| 174 | r++; |
| 175 | goto next; |
| 176 | } |
Dave Reisner | 6da9cdf | 2012-01-10 19:28:30 -0500 | [diff] [blame] | 177 | |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 178 | err = kmod_module_remove_module(mod, flags); |
| 179 | if (err < 0) { |
Lucas De Marchi | 04c26d2 | 2012-11-05 17:51:32 -0200 | [diff] [blame] | 180 | ERR("could not remove module %s: %s\n", arg, |
| 181 | strerror(-err)); |
Dave Reisner | 6da9cdf | 2012-01-10 19:28:30 -0500 | [diff] [blame] | 182 | r++; |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 183 | } |
Dave Reisner | 6da9cdf | 2012-01-10 19:28:30 -0500 | [diff] [blame] | 184 | next: |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 185 | kmod_module_unref(mod); |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | kmod_unref(ctx); |
| 189 | |
Lucas De Marchi | e6996c5 | 2012-11-05 18:32:05 -0200 | [diff] [blame] | 190 | done: |
Lucas De Marchi | 92aad74 | 2012-11-06 18:04:09 -0200 | [diff] [blame] | 191 | log_close(); |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 192 | |
Dave Reisner | 6da9cdf | 2012-01-10 19:28:30 -0500 | [diff] [blame] | 193 | return r == 0 ? EXIT_SUCCESS : EXIT_FAILURE; |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 194 | } |
Lucas De Marchi | f712ebc | 2011-12-22 03:39:11 -0200 | [diff] [blame] | 195 | |
Lucas De Marchi | f712ebc | 2011-12-22 03:39:11 -0200 | [diff] [blame] | 196 | const struct kmod_cmd kmod_cmd_compat_rmmod = { |
| 197 | .name = "rmmod", |
| 198 | .cmd = do_rmmod, |
| 199 | .help = "compat rmmod command", |
| 200 | }; |