2011-12-11 07:19:41 +07:00
|
|
|
/*
|
|
|
|
* kmod-rmmod - remove modules from linux kernel using libkmod.
|
|
|
|
*
|
2013-01-16 20:27:21 +07:00
|
|
|
* Copyright (C) 2011-2013 ProFUSION embedded systems
|
2011-12-11 07:19:41 +07:00
|
|
|
*
|
2011-12-13 03:24:35 +07:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
2011-12-11 07:19:41 +07:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2011-12-13 03:24:35 +07:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2011-12-11 07:19:41 +07:00
|
|
|
*
|
2011-12-13 03:24:35 +07:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2011-12-11 07:19:41 +07:00
|
|
|
*/
|
2011-12-13 03:24:35 +07:00
|
|
|
|
2014-10-03 11:41:42 +07:00
|
|
|
#include <errno.h>
|
|
|
|
#include <getopt.h>
|
2011-12-11 07:19:41 +07:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
2014-10-03 11:41:42 +07:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
2014-10-03 08:03:19 +07:00
|
|
|
#include <shared/macro.h>
|
2011-12-11 07:19:41 +07:00
|
|
|
|
2015-01-02 21:38:27 +07:00
|
|
|
#include <libkmod/libkmod.h>
|
2014-10-03 11:41:42 +07:00
|
|
|
|
2012-11-07 01:54:17 +07:00
|
|
|
#include "kmod.h"
|
|
|
|
|
2012-11-06 03:32:05 +07:00
|
|
|
#define DEFAULT_VERBOSE LOG_ERR
|
|
|
|
static int verbose = DEFAULT_VERBOSE;
|
|
|
|
static int use_syslog;
|
2011-12-11 07:19:41 +07:00
|
|
|
|
|
|
|
static const char cmdopts_s[] = "fsvVwh";
|
|
|
|
static const struct option cmdopts[] = {
|
|
|
|
{"force", no_argument, 0, 'f'},
|
|
|
|
{"syslog", no_argument, 0, 's'},
|
|
|
|
{"verbose", no_argument, 0, 'v'},
|
|
|
|
{"version", no_argument, 0, 'V'},
|
|
|
|
{"help", no_argument, 0, 'h'},
|
|
|
|
{NULL, 0, 0, 0}
|
|
|
|
};
|
|
|
|
|
2012-11-07 01:54:17 +07:00
|
|
|
static void help(void)
|
2011-12-11 07:19:41 +07:00
|
|
|
{
|
2012-11-07 02:32:41 +07:00
|
|
|
printf("Usage:\n"
|
2011-12-11 07:19:41 +07:00
|
|
|
"\t%s [options] modulename ...\n"
|
|
|
|
"Options:\n"
|
|
|
|
"\t-f, --force forces a module unload and may crash your\n"
|
|
|
|
"\t machine. This requires Forced Module Removal\n"
|
|
|
|
"\t option in your kernel. DANGEROUS\n"
|
|
|
|
"\t-s, --syslog print to syslog, not stderr\n"
|
|
|
|
"\t-v, --verbose enables more messages\n"
|
|
|
|
"\t-V, --version show version\n"
|
|
|
|
"\t-h, --help show this help\n",
|
2012-11-07 04:20:09 +07:00
|
|
|
program_invocation_short_name);
|
2011-12-11 07:19:41 +07:00
|
|
|
}
|
|
|
|
|
2012-01-11 07:28:30 +07:00
|
|
|
static int check_module_inuse(struct kmod_module *mod) {
|
|
|
|
struct kmod_list *holders;
|
2013-03-16 00:43:40 +07:00
|
|
|
int state;
|
2012-01-11 07:28:30 +07:00
|
|
|
|
2013-03-16 00:43:40 +07:00
|
|
|
state = kmod_module_get_initstate(mod);
|
|
|
|
|
|
|
|
if (state == KMOD_MODULE_BUILTIN) {
|
|
|
|
ERR("Module %s is builtin.\n", kmod_module_get_name(mod));
|
|
|
|
return -ENOENT;
|
|
|
|
} else if (state < 0) {
|
2012-11-06 02:51:32 +07:00
|
|
|
ERR("Module %s is not currently loaded\n",
|
2012-01-11 07:28:30 +07:00
|
|
|
kmod_module_get_name(mod));
|
|
|
|
return -ENOENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
holders = kmod_module_get_holders(mod);
|
|
|
|
if (holders != NULL) {
|
|
|
|
struct kmod_list *itr;
|
|
|
|
|
2012-11-06 02:51:32 +07:00
|
|
|
ERR("Module %s is in use by:", kmod_module_get_name(mod));
|
2012-01-11 07:28:30 +07:00
|
|
|
|
|
|
|
kmod_list_foreach(itr, holders) {
|
|
|
|
struct kmod_module *hm = kmod_module_get_module(itr);
|
|
|
|
fprintf(stderr, " %s", kmod_module_get_name(hm));
|
|
|
|
kmod_module_unref(hm);
|
|
|
|
}
|
|
|
|
fputc('\n', stderr);
|
|
|
|
|
|
|
|
kmod_module_unref_list(holders);
|
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (kmod_module_get_refcnt(mod) != 0) {
|
2012-11-06 02:51:32 +07:00
|
|
|
ERR("Module %s is in use\n", kmod_module_get_name(mod));
|
2012-01-11 07:28:30 +07:00
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2011-12-22 12:39:11 +07:00
|
|
|
static int do_rmmod(int argc, char *argv[])
|
2011-12-11 07:19:41 +07:00
|
|
|
{
|
|
|
|
struct kmod_ctx *ctx;
|
2011-12-13 00:41:02 +07:00
|
|
|
const char *null_config = NULL;
|
2013-09-20 13:40:58 +07:00
|
|
|
int flags = 0;
|
2012-01-11 07:28:30 +07:00
|
|
|
int i, err, r = 0;
|
2011-12-11 07:19:41 +07:00
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
int c, idx = 0;
|
|
|
|
c = getopt_long(argc, argv, cmdopts_s, cmdopts, &idx);
|
|
|
|
if (c == -1)
|
|
|
|
break;
|
|
|
|
switch (c) {
|
|
|
|
case 'f':
|
|
|
|
flags |= KMOD_REMOVE_FORCE;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
use_syslog = 1;
|
|
|
|
break;
|
|
|
|
case 'v':
|
|
|
|
verbose++;
|
|
|
|
break;
|
|
|
|
case 'h':
|
2012-11-07 01:54:17 +07:00
|
|
|
help();
|
2011-12-11 07:19:41 +07:00
|
|
|
return EXIT_SUCCESS;
|
|
|
|
case 'V':
|
|
|
|
puts(PACKAGE " version " VERSION);
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
case '?':
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
default:
|
2012-11-06 02:51:32 +07:00
|
|
|
ERR("unexpected getopt_long() value '%c'.\n", c);
|
2011-12-11 07:19:41 +07:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-07 03:04:09 +07:00
|
|
|
log_open(use_syslog);
|
2012-11-06 03:32:05 +07:00
|
|
|
|
2011-12-11 07:19:41 +07:00
|
|
|
if (optind >= argc) {
|
2012-11-06 02:51:32 +07:00
|
|
|
ERR("missing module name.\n");
|
2012-11-06 03:32:05 +07:00
|
|
|
r = EXIT_FAILURE;
|
|
|
|
goto done;
|
2011-12-11 07:19:41 +07:00
|
|
|
}
|
|
|
|
|
2011-12-13 00:41:02 +07:00
|
|
|
ctx = kmod_new(NULL, &null_config);
|
2011-12-11 07:19:41 +07:00
|
|
|
if (!ctx) {
|
2012-11-06 02:51:32 +07:00
|
|
|
ERR("kmod_new() failed!\n");
|
2012-11-06 03:32:05 +07:00
|
|
|
r = EXIT_FAILURE;
|
|
|
|
goto done;
|
2011-12-11 07:19:41 +07:00
|
|
|
}
|
|
|
|
|
2012-11-07 03:26:34 +07:00
|
|
|
log_setup_kmod_log(ctx, verbose);
|
2011-12-11 07:19:41 +07:00
|
|
|
|
|
|
|
for (i = optind; i < argc; i++) {
|
|
|
|
struct kmod_module *mod;
|
|
|
|
const char *arg = argv[i];
|
|
|
|
struct stat st;
|
|
|
|
if (stat(arg, &st) == 0)
|
|
|
|
err = kmod_module_new_from_path(ctx, arg, &mod);
|
|
|
|
else
|
|
|
|
err = kmod_module_new_from_name(ctx, arg, &mod);
|
|
|
|
|
|
|
|
if (err < 0) {
|
2012-11-06 02:51:32 +07:00
|
|
|
ERR("could not use module %s: %s\n", arg,
|
|
|
|
strerror(-err));
|
2011-12-11 07:19:41 +07:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-09-20 13:40:58 +07:00
|
|
|
if (!(flags & KMOD_REMOVE_FORCE) && check_module_inuse(mod) < 0) {
|
|
|
|
r++;
|
|
|
|
goto next;
|
|
|
|
}
|
2012-01-11 07:28:30 +07:00
|
|
|
|
2011-12-11 07:19:41 +07:00
|
|
|
err = kmod_module_remove_module(mod, flags);
|
|
|
|
if (err < 0) {
|
2012-11-06 02:51:32 +07:00
|
|
|
ERR("could not remove module %s: %s\n", arg,
|
|
|
|
strerror(-err));
|
2012-01-11 07:28:30 +07:00
|
|
|
r++;
|
2011-12-11 07:19:41 +07:00
|
|
|
}
|
2012-01-11 07:28:30 +07:00
|
|
|
next:
|
2011-12-11 07:19:41 +07:00
|
|
|
kmod_module_unref(mod);
|
|
|
|
}
|
|
|
|
|
|
|
|
kmod_unref(ctx);
|
|
|
|
|
2012-11-06 03:32:05 +07:00
|
|
|
done:
|
2012-11-07 03:04:09 +07:00
|
|
|
log_close();
|
2011-12-11 07:19:41 +07:00
|
|
|
|
2012-01-11 07:28:30 +07:00
|
|
|
return r == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
|
2011-12-11 07:19:41 +07:00
|
|
|
}
|
2011-12-22 12:39:11 +07:00
|
|
|
|
|
|
|
const struct kmod_cmd kmod_cmd_compat_rmmod = {
|
|
|
|
.name = "rmmod",
|
|
|
|
.cmd = do_rmmod,
|
|
|
|
.help = "compat rmmod command",
|
|
|
|
};
|