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 | a66a6a9 | 2012-01-09 00:40:50 -0200 | [diff] [blame] | 4 | * Copyright (C) 2011-2012 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 | |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <getopt.h> |
| 23 | #include <errno.h> |
| 24 | #include <string.h> |
| 25 | #include <sys/types.h> |
| 26 | #include <sys/stat.h> |
| 27 | #include <unistd.h> |
| 28 | #include <syslog.h> |
| 29 | #include "libkmod.h" |
Lucas De Marchi | e6996c5 | 2012-11-05 18:32:05 -0200 | [diff] [blame] | 30 | #include "macro.h" |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 31 | |
Lucas De Marchi | 4a2e20d | 2012-11-06 16:54:17 -0200 | [diff] [blame] | 32 | #include "kmod.h" |
| 33 | |
Lucas De Marchi | e6996c5 | 2012-11-05 18:32:05 -0200 | [diff] [blame] | 34 | #define DEFAULT_VERBOSE LOG_ERR |
| 35 | static int verbose = DEFAULT_VERBOSE; |
| 36 | static int use_syslog; |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 37 | |
| 38 | static const char cmdopts_s[] = "fsvVwh"; |
| 39 | static const struct option cmdopts[] = { |
| 40 | {"force", no_argument, 0, 'f'}, |
| 41 | {"syslog", no_argument, 0, 's'}, |
| 42 | {"verbose", no_argument, 0, 'v'}, |
| 43 | {"version", no_argument, 0, 'V'}, |
| 44 | {"wait", no_argument, 0, 'w'}, |
| 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 | 4a2e20d | 2012-11-06 16:54:17 -0200 | [diff] [blame] | 61 | binname); |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 62 | } |
| 63 | |
Lucas De Marchi | e6996c5 | 2012-11-05 18:32:05 -0200 | [diff] [blame] | 64 | static void log_rmmod(void *data, int priority, const char *file, int line, |
| 65 | const char *fn, const char *format, va_list args) |
| 66 | { |
| 67 | const char *prioname = prio_to_str(priority); |
| 68 | char *str; |
| 69 | |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 70 | if (vasprintf(&str, format, args) < 0) |
| 71 | return; |
Lucas De Marchi | e6996c5 | 2012-11-05 18:32:05 -0200 | [diff] [blame] | 72 | |
| 73 | if (use_syslog > 1) { |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 74 | #ifdef ENABLE_DEBUG |
Lucas De Marchi | e6996c5 | 2012-11-05 18:32:05 -0200 | [diff] [blame] | 75 | syslog(priority, "%s: %s:%d %s() %s", prioname, file, line, |
| 76 | fn, str); |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 77 | #else |
Lucas De Marchi | e6996c5 | 2012-11-05 18:32:05 -0200 | [diff] [blame] | 78 | syslog(priority, "%s: %s", prioname, str); |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 79 | #endif |
Lucas De Marchi | e6996c5 | 2012-11-05 18:32:05 -0200 | [diff] [blame] | 80 | } else { |
| 81 | #ifdef ENABLE_DEBUG |
| 82 | fprintf(stderr, "rmmod: %s: %s:%d %s() %s", prioname, file, |
| 83 | line, fn, str); |
| 84 | #else |
| 85 | fprintf(stderr, "rmmod: %s: %s", prioname, str); |
| 86 | #endif |
| 87 | } |
| 88 | |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 89 | free(str); |
| 90 | (void)data; |
| 91 | } |
| 92 | |
Lucas De Marchi | e6996c5 | 2012-11-05 18:32:05 -0200 | [diff] [blame] | 93 | static void _log(int prio, const char *fmt, ...) |
| 94 | { |
| 95 | const char *prioname; |
| 96 | char *msg; |
| 97 | va_list args; |
| 98 | |
| 99 | if (prio > verbose) |
| 100 | return; |
| 101 | |
| 102 | va_start(args, fmt); |
| 103 | if (vasprintf(&msg, fmt, args) < 0) |
| 104 | msg = NULL; |
| 105 | va_end(args); |
| 106 | if (msg == NULL) |
| 107 | return; |
| 108 | |
| 109 | prioname = prio_to_str(prio); |
| 110 | |
| 111 | if (use_syslog > 1) |
| 112 | syslog(prio, "%s: %s", prioname, msg); |
| 113 | else |
| 114 | fprintf(stderr, "rmmod: %s: %s", prioname, msg); |
| 115 | free(msg); |
| 116 | |
| 117 | if (prio <= LOG_CRIT) |
| 118 | exit(EXIT_FAILURE); |
| 119 | } |
| 120 | #define ERR(...) _log(LOG_ERR, __VA_ARGS__) |
| 121 | |
Dave Reisner | 6da9cdf | 2012-01-10 19:28:30 -0500 | [diff] [blame] | 122 | static int check_module_inuse(struct kmod_module *mod) { |
| 123 | struct kmod_list *holders; |
| 124 | |
| 125 | if (kmod_module_get_initstate(mod) == -ENOENT) { |
Lucas De Marchi | 04c26d2 | 2012-11-05 17:51:32 -0200 | [diff] [blame] | 126 | ERR("Module %s is not currently loaded\n", |
Dave Reisner | 6da9cdf | 2012-01-10 19:28:30 -0500 | [diff] [blame] | 127 | kmod_module_get_name(mod)); |
| 128 | return -ENOENT; |
| 129 | } |
| 130 | |
| 131 | holders = kmod_module_get_holders(mod); |
| 132 | if (holders != NULL) { |
| 133 | struct kmod_list *itr; |
| 134 | |
Lucas De Marchi | 04c26d2 | 2012-11-05 17:51:32 -0200 | [diff] [blame] | 135 | ERR("Module %s is in use by:", kmod_module_get_name(mod)); |
Dave Reisner | 6da9cdf | 2012-01-10 19:28:30 -0500 | [diff] [blame] | 136 | |
| 137 | kmod_list_foreach(itr, holders) { |
| 138 | struct kmod_module *hm = kmod_module_get_module(itr); |
| 139 | fprintf(stderr, " %s", kmod_module_get_name(hm)); |
| 140 | kmod_module_unref(hm); |
| 141 | } |
| 142 | fputc('\n', stderr); |
| 143 | |
| 144 | kmod_module_unref_list(holders); |
| 145 | return -EBUSY; |
| 146 | } |
| 147 | |
| 148 | if (kmod_module_get_refcnt(mod) != 0) { |
Lucas De Marchi | 04c26d2 | 2012-11-05 17:51:32 -0200 | [diff] [blame] | 149 | ERR("Module %s is in use\n", kmod_module_get_name(mod)); |
Dave Reisner | 6da9cdf | 2012-01-10 19:28:30 -0500 | [diff] [blame] | 150 | return -EBUSY; |
| 151 | } |
| 152 | |
| 153 | return 0; |
| 154 | } |
| 155 | |
Lucas De Marchi | f712ebc | 2011-12-22 03:39:11 -0200 | [diff] [blame] | 156 | static int do_rmmod(int argc, char *argv[]) |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 157 | { |
| 158 | struct kmod_ctx *ctx; |
Lucas De Marchi | 2411c07 | 2011-12-12 15:41:02 -0200 | [diff] [blame] | 159 | const char *null_config = NULL; |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 160 | int flags = KMOD_REMOVE_NOWAIT; |
Dave Reisner | 6da9cdf | 2012-01-10 19:28:30 -0500 | [diff] [blame] | 161 | int i, err, r = 0; |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 162 | |
| 163 | for (;;) { |
| 164 | int c, idx = 0; |
| 165 | c = getopt_long(argc, argv, cmdopts_s, cmdopts, &idx); |
| 166 | if (c == -1) |
| 167 | break; |
| 168 | switch (c) { |
| 169 | case 'f': |
| 170 | flags |= KMOD_REMOVE_FORCE; |
| 171 | break; |
| 172 | case 's': |
| 173 | use_syslog = 1; |
| 174 | break; |
| 175 | case 'v': |
| 176 | verbose++; |
| 177 | break; |
| 178 | case 'w': |
Lucas De Marchi | 04c26d2 | 2012-11-05 17:51:32 -0200 | [diff] [blame] | 179 | ERR("'Wait' behavior is targeted for removal from kernel.\nWe will now sleep for 10s, and then continue.\n"); |
Lucas De Marchi | cc83364 | 2012-10-17 18:32:56 -0300 | [diff] [blame] | 180 | sleep(10); |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 181 | flags &= ~KMOD_REMOVE_NOWAIT; |
| 182 | break; |
| 183 | case 'h': |
Lucas De Marchi | 4a2e20d | 2012-11-06 16:54:17 -0200 | [diff] [blame] | 184 | help(); |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 185 | return EXIT_SUCCESS; |
| 186 | case 'V': |
| 187 | puts(PACKAGE " version " VERSION); |
| 188 | return EXIT_SUCCESS; |
| 189 | case '?': |
| 190 | return EXIT_FAILURE; |
| 191 | default: |
Lucas De Marchi | 04c26d2 | 2012-11-05 17:51:32 -0200 | [diff] [blame] | 192 | ERR("unexpected getopt_long() value '%c'.\n", c); |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 193 | return EXIT_FAILURE; |
| 194 | } |
| 195 | } |
| 196 | |
Lucas De Marchi | e6996c5 | 2012-11-05 18:32:05 -0200 | [diff] [blame] | 197 | if (use_syslog) { |
| 198 | openlog("rmmod", LOG_CONS, LOG_DAEMON); |
| 199 | use_syslog++; |
| 200 | } |
| 201 | |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 202 | if (optind >= argc) { |
Lucas De Marchi | 04c26d2 | 2012-11-05 17:51:32 -0200 | [diff] [blame] | 203 | ERR("missing module name.\n"); |
Lucas De Marchi | e6996c5 | 2012-11-05 18:32:05 -0200 | [diff] [blame] | 204 | r = EXIT_FAILURE; |
| 205 | goto done; |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 206 | } |
| 207 | |
Lucas De Marchi | 2411c07 | 2011-12-12 15:41:02 -0200 | [diff] [blame] | 208 | ctx = kmod_new(NULL, &null_config); |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 209 | if (!ctx) { |
Lucas De Marchi | 04c26d2 | 2012-11-05 17:51:32 -0200 | [diff] [blame] | 210 | ERR("kmod_new() failed!\n"); |
Lucas De Marchi | e6996c5 | 2012-11-05 18:32:05 -0200 | [diff] [blame] | 211 | r = EXIT_FAILURE; |
| 212 | goto done; |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 213 | } |
| 214 | |
Lucas De Marchi | e6996c5 | 2012-11-05 18:32:05 -0200 | [diff] [blame] | 215 | kmod_set_log_priority(ctx, kmod_get_log_priority(ctx)); |
| 216 | kmod_set_log_fn(ctx, log_rmmod, NULL); |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 217 | |
| 218 | for (i = optind; i < argc; i++) { |
| 219 | struct kmod_module *mod; |
| 220 | const char *arg = argv[i]; |
| 221 | struct stat st; |
| 222 | if (stat(arg, &st) == 0) |
| 223 | err = kmod_module_new_from_path(ctx, arg, &mod); |
| 224 | else |
| 225 | err = kmod_module_new_from_name(ctx, arg, &mod); |
| 226 | |
| 227 | if (err < 0) { |
Lucas De Marchi | 04c26d2 | 2012-11-05 17:51:32 -0200 | [diff] [blame] | 228 | ERR("could not use module %s: %s\n", arg, |
| 229 | strerror(-err)); |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 230 | break; |
| 231 | } |
| 232 | |
Dave Reisner | 6da9cdf | 2012-01-10 19:28:30 -0500 | [diff] [blame] | 233 | if (!(flags & KMOD_REMOVE_FORCE) && (flags & KMOD_REMOVE_NOWAIT)) |
| 234 | if (check_module_inuse(mod) < 0) { |
| 235 | r++; |
| 236 | goto next; |
| 237 | } |
| 238 | |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 239 | err = kmod_module_remove_module(mod, flags); |
| 240 | if (err < 0) { |
Lucas De Marchi | 04c26d2 | 2012-11-05 17:51:32 -0200 | [diff] [blame] | 241 | ERR("could not remove module %s: %s\n", arg, |
| 242 | strerror(-err)); |
Dave Reisner | 6da9cdf | 2012-01-10 19:28:30 -0500 | [diff] [blame] | 243 | r++; |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 244 | } |
Dave Reisner | 6da9cdf | 2012-01-10 19:28:30 -0500 | [diff] [blame] | 245 | next: |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 246 | kmod_module_unref(mod); |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | kmod_unref(ctx); |
| 250 | |
Lucas De Marchi | e6996c5 | 2012-11-05 18:32:05 -0200 | [diff] [blame] | 251 | done: |
| 252 | if (use_syslog > 1) |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 253 | closelog(); |
| 254 | |
Dave Reisner | 6da9cdf | 2012-01-10 19:28:30 -0500 | [diff] [blame] | 255 | return r == 0 ? EXIT_SUCCESS : EXIT_FAILURE; |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 256 | } |
Lucas De Marchi | f712ebc | 2011-12-22 03:39:11 -0200 | [diff] [blame] | 257 | |
Lucas De Marchi | f712ebc | 2011-12-22 03:39:11 -0200 | [diff] [blame] | 258 | const struct kmod_cmd kmod_cmd_compat_rmmod = { |
| 259 | .name = "rmmod", |
| 260 | .cmd = do_rmmod, |
| 261 | .help = "compat rmmod command", |
| 262 | }; |