blob: 1983f70fdfcd04a000c2107696d743dd21188828 [file] [log] [blame]
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -02001/*
2 * kmod-rmmod - remove modules from linux kernel using libkmod.
3 *
Lucas De Marchia66a6a92012-01-09 00:40:50 -02004 * Copyright (C) 2011-2012 ProFUSION embedded systems
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -02005 *
Lucas De Marchicb451f32011-12-12 18:24:35 -02006 * 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 Barbieri72c51a92011-12-10 22:19:41 -020010 *
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 Marchicb451f32011-12-12 18:24:35 -020013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -020015 *
Lucas De Marchicb451f32011-12-12 18:24:35 -020016 * 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 Barbieri72c51a92011-12-10 22:19:41 -020018 */
Lucas De Marchicb451f32011-12-12 18:24:35 -020019
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -020020#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 Marchie6996c52012-11-05 18:32:05 -020030#include "macro.h"
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -020031
Lucas De Marchi4a2e20d2012-11-06 16:54:17 -020032#include "kmod.h"
33
Lucas De Marchie6996c52012-11-05 18:32:05 -020034#define DEFAULT_VERBOSE LOG_ERR
35static int verbose = DEFAULT_VERBOSE;
36static int use_syslog;
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -020037
38static const char cmdopts_s[] = "fsvVwh";
39static 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 Marchi4a2e20d2012-11-06 16:54:17 -020049static void help(void)
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -020050{
51 fprintf(stderr,
52 "Usage:\n"
53 "\t%s [options] modulename ...\n"
54 "Options:\n"
55 "\t-f, --force forces a module unload and may crash your\n"
56 "\t machine. This requires Forced Module Removal\n"
57 "\t option in your kernel. DANGEROUS\n"
58 "\t-s, --syslog print to syslog, not stderr\n"
59 "\t-v, --verbose enables more messages\n"
60 "\t-V, --version show version\n"
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -020061 "\t-h, --help show this help\n",
Lucas De Marchi4a2e20d2012-11-06 16:54:17 -020062 binname);
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -020063}
64
Lucas De Marchie6996c52012-11-05 18:32:05 -020065static _always_inline_ const char *prio_to_str(int prio)
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -020066{
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -020067 const char *prioname;
Lucas De Marchie6996c52012-11-05 18:32:05 -020068 char buf[32];
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -020069
Lucas De Marchie6996c52012-11-05 18:32:05 -020070 switch (prio) {
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -020071 case LOG_CRIT:
72 prioname = "FATAL";
73 break;
74 case LOG_ERR:
75 prioname = "ERROR";
76 break;
77 case LOG_WARNING:
78 prioname = "WARNING";
79 break;
80 case LOG_NOTICE:
81 prioname = "NOTICE";
82 break;
83 case LOG_INFO:
84 prioname = "INFO";
85 break;
86 case LOG_DEBUG:
87 prioname = "DEBUG";
88 break;
89 default:
Lucas De Marchie6996c52012-11-05 18:32:05 -020090 snprintf(buf, sizeof(buf), "LOG-%03d", prio);
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -020091 prioname = buf;
92 }
93
Lucas De Marchie6996c52012-11-05 18:32:05 -020094 return prioname;
95}
96
97static void log_rmmod(void *data, int priority, const char *file, int line,
98 const char *fn, const char *format, va_list args)
99{
100 const char *prioname = prio_to_str(priority);
101 char *str;
102
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200103 if (vasprintf(&str, format, args) < 0)
104 return;
Lucas De Marchie6996c52012-11-05 18:32:05 -0200105
106 if (use_syslog > 1) {
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200107#ifdef ENABLE_DEBUG
Lucas De Marchie6996c52012-11-05 18:32:05 -0200108 syslog(priority, "%s: %s:%d %s() %s", prioname, file, line,
109 fn, str);
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200110#else
Lucas De Marchie6996c52012-11-05 18:32:05 -0200111 syslog(priority, "%s: %s", prioname, str);
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200112#endif
Lucas De Marchie6996c52012-11-05 18:32:05 -0200113 } else {
114#ifdef ENABLE_DEBUG
115 fprintf(stderr, "rmmod: %s: %s:%d %s() %s", prioname, file,
116 line, fn, str);
117#else
118 fprintf(stderr, "rmmod: %s: %s", prioname, str);
119#endif
120 }
121
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200122 free(str);
123 (void)data;
124}
125
Lucas De Marchie6996c52012-11-05 18:32:05 -0200126static void _log(int prio, const char *fmt, ...)
127{
128 const char *prioname;
129 char *msg;
130 va_list args;
131
132 if (prio > verbose)
133 return;
134
135 va_start(args, fmt);
136 if (vasprintf(&msg, fmt, args) < 0)
137 msg = NULL;
138 va_end(args);
139 if (msg == NULL)
140 return;
141
142 prioname = prio_to_str(prio);
143
144 if (use_syslog > 1)
145 syslog(prio, "%s: %s", prioname, msg);
146 else
147 fprintf(stderr, "rmmod: %s: %s", prioname, msg);
148 free(msg);
149
150 if (prio <= LOG_CRIT)
151 exit(EXIT_FAILURE);
152}
153#define ERR(...) _log(LOG_ERR, __VA_ARGS__)
154
Dave Reisner6da9cdf2012-01-10 19:28:30 -0500155static int check_module_inuse(struct kmod_module *mod) {
156 struct kmod_list *holders;
157
158 if (kmod_module_get_initstate(mod) == -ENOENT) {
Lucas De Marchi04c26d22012-11-05 17:51:32 -0200159 ERR("Module %s is not currently loaded\n",
Dave Reisner6da9cdf2012-01-10 19:28:30 -0500160 kmod_module_get_name(mod));
161 return -ENOENT;
162 }
163
164 holders = kmod_module_get_holders(mod);
165 if (holders != NULL) {
166 struct kmod_list *itr;
167
Lucas De Marchi04c26d22012-11-05 17:51:32 -0200168 ERR("Module %s is in use by:", kmod_module_get_name(mod));
Dave Reisner6da9cdf2012-01-10 19:28:30 -0500169
170 kmod_list_foreach(itr, holders) {
171 struct kmod_module *hm = kmod_module_get_module(itr);
172 fprintf(stderr, " %s", kmod_module_get_name(hm));
173 kmod_module_unref(hm);
174 }
175 fputc('\n', stderr);
176
177 kmod_module_unref_list(holders);
178 return -EBUSY;
179 }
180
181 if (kmod_module_get_refcnt(mod) != 0) {
Lucas De Marchi04c26d22012-11-05 17:51:32 -0200182 ERR("Module %s is in use\n", kmod_module_get_name(mod));
Dave Reisner6da9cdf2012-01-10 19:28:30 -0500183 return -EBUSY;
184 }
185
186 return 0;
187}
188
Lucas De Marchif712ebc2011-12-22 03:39:11 -0200189static int do_rmmod(int argc, char *argv[])
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200190{
191 struct kmod_ctx *ctx;
Lucas De Marchi2411c072011-12-12 15:41:02 -0200192 const char *null_config = NULL;
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200193 int flags = KMOD_REMOVE_NOWAIT;
Dave Reisner6da9cdf2012-01-10 19:28:30 -0500194 int i, err, r = 0;
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200195
196 for (;;) {
197 int c, idx = 0;
198 c = getopt_long(argc, argv, cmdopts_s, cmdopts, &idx);
199 if (c == -1)
200 break;
201 switch (c) {
202 case 'f':
203 flags |= KMOD_REMOVE_FORCE;
204 break;
205 case 's':
206 use_syslog = 1;
207 break;
208 case 'v':
209 verbose++;
210 break;
211 case 'w':
Lucas De Marchi04c26d22012-11-05 17:51:32 -0200212 ERR("'Wait' behavior is targeted for removal from kernel.\nWe will now sleep for 10s, and then continue.\n");
Lucas De Marchicc833642012-10-17 18:32:56 -0300213 sleep(10);
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200214 flags &= ~KMOD_REMOVE_NOWAIT;
215 break;
216 case 'h':
Lucas De Marchi4a2e20d2012-11-06 16:54:17 -0200217 help();
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200218 return EXIT_SUCCESS;
219 case 'V':
220 puts(PACKAGE " version " VERSION);
221 return EXIT_SUCCESS;
222 case '?':
223 return EXIT_FAILURE;
224 default:
Lucas De Marchi04c26d22012-11-05 17:51:32 -0200225 ERR("unexpected getopt_long() value '%c'.\n", c);
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200226 return EXIT_FAILURE;
227 }
228 }
229
Lucas De Marchie6996c52012-11-05 18:32:05 -0200230 if (use_syslog) {
231 openlog("rmmod", LOG_CONS, LOG_DAEMON);
232 use_syslog++;
233 }
234
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200235 if (optind >= argc) {
Lucas De Marchi04c26d22012-11-05 17:51:32 -0200236 ERR("missing module name.\n");
Lucas De Marchie6996c52012-11-05 18:32:05 -0200237 r = EXIT_FAILURE;
238 goto done;
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200239 }
240
Lucas De Marchi2411c072011-12-12 15:41:02 -0200241 ctx = kmod_new(NULL, &null_config);
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200242 if (!ctx) {
Lucas De Marchi04c26d22012-11-05 17:51:32 -0200243 ERR("kmod_new() failed!\n");
Lucas De Marchie6996c52012-11-05 18:32:05 -0200244 r = EXIT_FAILURE;
245 goto done;
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200246 }
247
Lucas De Marchie6996c52012-11-05 18:32:05 -0200248 kmod_set_log_priority(ctx, kmod_get_log_priority(ctx));
249 kmod_set_log_fn(ctx, log_rmmod, NULL);
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200250
251 for (i = optind; i < argc; i++) {
252 struct kmod_module *mod;
253 const char *arg = argv[i];
254 struct stat st;
255 if (stat(arg, &st) == 0)
256 err = kmod_module_new_from_path(ctx, arg, &mod);
257 else
258 err = kmod_module_new_from_name(ctx, arg, &mod);
259
260 if (err < 0) {
Lucas De Marchi04c26d22012-11-05 17:51:32 -0200261 ERR("could not use module %s: %s\n", arg,
262 strerror(-err));
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200263 break;
264 }
265
Dave Reisner6da9cdf2012-01-10 19:28:30 -0500266 if (!(flags & KMOD_REMOVE_FORCE) && (flags & KMOD_REMOVE_NOWAIT))
267 if (check_module_inuse(mod) < 0) {
268 r++;
269 goto next;
270 }
271
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200272 err = kmod_module_remove_module(mod, flags);
273 if (err < 0) {
Lucas De Marchi04c26d22012-11-05 17:51:32 -0200274 ERR("could not remove module %s: %s\n", arg,
275 strerror(-err));
Dave Reisner6da9cdf2012-01-10 19:28:30 -0500276 r++;
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200277 }
Dave Reisner6da9cdf2012-01-10 19:28:30 -0500278next:
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200279 kmod_module_unref(mod);
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200280 }
281
282 kmod_unref(ctx);
283
Lucas De Marchie6996c52012-11-05 18:32:05 -0200284done:
285 if (use_syslog > 1)
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200286 closelog();
287
Dave Reisner6da9cdf2012-01-10 19:28:30 -0500288 return r == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200289}
Lucas De Marchif712ebc2011-12-22 03:39:11 -0200290
Lucas De Marchif712ebc2011-12-22 03:39:11 -0200291const struct kmod_cmd kmod_cmd_compat_rmmod = {
292 .name = "rmmod",
293 .cmd = do_rmmod,
294 .help = "compat rmmod command",
295};