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" |
| 30 | |
| 31 | |
| 32 | static const char cmdopts_s[] = "fsvVwh"; |
| 33 | static const struct option cmdopts[] = { |
| 34 | {"force", no_argument, 0, 'f'}, |
| 35 | {"syslog", no_argument, 0, 's'}, |
| 36 | {"verbose", no_argument, 0, 'v'}, |
| 37 | {"version", no_argument, 0, 'V'}, |
| 38 | {"wait", no_argument, 0, 'w'}, |
| 39 | {"help", no_argument, 0, 'h'}, |
| 40 | {NULL, 0, 0, 0} |
| 41 | }; |
| 42 | |
| 43 | static void help(const char *progname) |
| 44 | { |
| 45 | fprintf(stderr, |
| 46 | "Usage:\n" |
| 47 | "\t%s [options] modulename ...\n" |
| 48 | "Options:\n" |
| 49 | "\t-f, --force forces a module unload and may crash your\n" |
| 50 | "\t machine. This requires Forced Module Removal\n" |
| 51 | "\t option in your kernel. DANGEROUS\n" |
| 52 | "\t-s, --syslog print to syslog, not stderr\n" |
| 53 | "\t-v, --verbose enables more messages\n" |
| 54 | "\t-V, --version show version\n" |
| 55 | "\t-w, --wait begins module removal even if it is used and\n" |
| 56 | "\t will stop new users from accessing it.\n" |
| 57 | "\t-h, --help show this help\n", |
| 58 | progname); |
| 59 | } |
| 60 | |
| 61 | static void log_syslog(void *data, int priority, const char *file, int line, |
| 62 | const char *fn, const char *format, |
| 63 | va_list args) |
| 64 | { |
| 65 | char *str, buf[32]; |
| 66 | const char *prioname; |
| 67 | |
| 68 | switch (priority) { |
| 69 | case LOG_CRIT: |
| 70 | prioname = "FATAL"; |
| 71 | break; |
| 72 | case LOG_ERR: |
| 73 | prioname = "ERROR"; |
| 74 | break; |
| 75 | case LOG_WARNING: |
| 76 | prioname = "WARNING"; |
| 77 | break; |
| 78 | case LOG_NOTICE: |
| 79 | prioname = "NOTICE"; |
| 80 | break; |
| 81 | case LOG_INFO: |
| 82 | prioname = "INFO"; |
| 83 | break; |
| 84 | case LOG_DEBUG: |
| 85 | prioname = "DEBUG"; |
| 86 | break; |
| 87 | default: |
| 88 | snprintf(buf, sizeof(buf), "LOG-%03d", priority); |
| 89 | prioname = buf; |
| 90 | } |
| 91 | |
| 92 | if (vasprintf(&str, format, args) < 0) |
| 93 | return; |
| 94 | #ifdef ENABLE_DEBUG |
| 95 | syslog(LOG_NOTICE, "%s: %s:%d %s() %s", prioname, file, line, fn, str); |
| 96 | #else |
| 97 | syslog(LOG_NOTICE, "%s: %s", prioname, str); |
| 98 | #endif |
| 99 | free(str); |
| 100 | (void)data; |
| 101 | } |
| 102 | |
Lucas De Marchi | f712ebc | 2011-12-22 03:39:11 -0200 | [diff] [blame] | 103 | static int do_rmmod(int argc, char *argv[]) |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 104 | { |
| 105 | struct kmod_ctx *ctx; |
Lucas De Marchi | 2411c07 | 2011-12-12 15:41:02 -0200 | [diff] [blame] | 106 | const char *null_config = NULL; |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 107 | int flags = KMOD_REMOVE_NOWAIT; |
| 108 | int use_syslog = 0; |
| 109 | int verbose = 0; |
Lucas De Marchi | 45f2778 | 2011-12-12 17:23:04 -0200 | [diff] [blame] | 110 | int i, err = 0; |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 111 | |
| 112 | for (;;) { |
| 113 | int c, idx = 0; |
| 114 | c = getopt_long(argc, argv, cmdopts_s, cmdopts, &idx); |
| 115 | if (c == -1) |
| 116 | break; |
| 117 | switch (c) { |
| 118 | case 'f': |
| 119 | flags |= KMOD_REMOVE_FORCE; |
| 120 | break; |
| 121 | case 's': |
| 122 | use_syslog = 1; |
| 123 | break; |
| 124 | case 'v': |
| 125 | verbose++; |
| 126 | break; |
| 127 | case 'w': |
| 128 | flags &= ~KMOD_REMOVE_NOWAIT; |
| 129 | break; |
| 130 | case 'h': |
Lucas De Marchi | aa156c9 | 2012-01-05 08:15:51 -0200 | [diff] [blame] | 131 | help(basename(argv[0])); |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 132 | return EXIT_SUCCESS; |
| 133 | case 'V': |
| 134 | puts(PACKAGE " version " VERSION); |
| 135 | return EXIT_SUCCESS; |
| 136 | case '?': |
| 137 | return EXIT_FAILURE; |
| 138 | default: |
| 139 | fprintf(stderr, |
| 140 | "Error: unexpected getopt_long() value '%c'.\n", |
| 141 | c); |
| 142 | return EXIT_FAILURE; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | if (optind >= argc) { |
| 147 | fprintf(stderr, "Error: missing module name.\n"); |
| 148 | return EXIT_FAILURE; |
| 149 | } |
| 150 | |
Lucas De Marchi | 2411c07 | 2011-12-12 15:41:02 -0200 | [diff] [blame] | 151 | ctx = kmod_new(NULL, &null_config); |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 152 | if (!ctx) { |
| 153 | fputs("Error: kmod_new() failed!\n", stderr); |
| 154 | return EXIT_FAILURE; |
| 155 | } |
| 156 | |
| 157 | kmod_set_log_priority(ctx, kmod_get_log_priority(ctx) + verbose); |
| 158 | if (use_syslog) { |
| 159 | openlog("rmmod", LOG_CONS, LOG_DAEMON); |
| 160 | kmod_set_log_fn(ctx, log_syslog, NULL); |
| 161 | } |
| 162 | |
| 163 | for (i = optind; i < argc; i++) { |
| 164 | struct kmod_module *mod; |
| 165 | const char *arg = argv[i]; |
| 166 | struct stat st; |
| 167 | if (stat(arg, &st) == 0) |
| 168 | err = kmod_module_new_from_path(ctx, arg, &mod); |
| 169 | else |
| 170 | err = kmod_module_new_from_name(ctx, arg, &mod); |
| 171 | |
| 172 | if (err < 0) { |
| 173 | fprintf(stderr, "Error: could not use module %s: %s\n", |
| 174 | arg, strerror(-err)); |
| 175 | break; |
| 176 | } |
| 177 | |
| 178 | err = kmod_module_remove_module(mod, flags); |
| 179 | if (err < 0) { |
| 180 | fprintf(stderr, |
| 181 | "Error: could not remove module %s: %s\n", |
| 182 | arg, strerror(-err)); |
| 183 | } |
| 184 | kmod_module_unref(mod); |
| 185 | if (err < 0) |
| 186 | break; |
| 187 | } |
| 188 | |
| 189 | kmod_unref(ctx); |
| 190 | |
| 191 | if (use_syslog) |
| 192 | closelog(); |
| 193 | |
| 194 | return err >= 0 ? EXIT_SUCCESS : EXIT_FAILURE; |
| 195 | } |
Lucas De Marchi | f712ebc | 2011-12-22 03:39:11 -0200 | [diff] [blame] | 196 | |
| 197 | #ifndef KMOD_BUNDLE_TOOL |
| 198 | int main(int argc, char *argv[]) |
| 199 | { |
| 200 | return do_rmmod(argc, argv); |
| 201 | } |
| 202 | |
| 203 | #else |
| 204 | #include "kmod.h" |
| 205 | |
| 206 | const struct kmod_cmd kmod_cmd_compat_rmmod = { |
| 207 | .name = "rmmod", |
| 208 | .cmd = do_rmmod, |
| 209 | .help = "compat rmmod command", |
| 210 | }; |
| 211 | |
| 212 | #endif |