blob: bcdea4c572627ef58bb1a2e9333233a10f1068d2 [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 Marchie6b0e492013-01-16 11:27:21 -02004 * Copyright (C) 2011-2013 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
Lucas De Marchic2e42862014-10-03 01:41:42 -030020#include <errno.h>
21#include <getopt.h>
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -020022#include <stdio.h>
23#include <stdlib.h>
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -020024#include <string.h>
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -020025#include <unistd.h>
Lucas De Marchic2e42862014-10-03 01:41:42 -030026#include <sys/stat.h>
27#include <sys/types.h>
28
Lucas De Marchi576dd432014-10-02 22:03:19 -030029#include <shared/macro.h>
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -020030
Lucas De Marchif3578662015-01-02 12:38:27 -020031#include <libkmod/libkmod.h>
Lucas De Marchic2e42862014-10-03 01:41:42 -030032
Lucas De Marchi4a2e20d2012-11-06 16:54:17 -020033#include "kmod.h"
34
Lucas De Marchie6996c52012-11-05 18:32:05 -020035#define DEFAULT_VERBOSE LOG_ERR
36static int verbose = DEFAULT_VERBOSE;
37static int use_syslog;
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -020038
39static const char cmdopts_s[] = "fsvVwh";
40static 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 Barbieri72c51a92011-12-10 22:19:41 -020045 {"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{
Lucas De Marchi34e06bf2012-11-06 17:32:41 -020051 printf("Usage:\n"
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -020052 "\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 Barbieri72c51a92011-12-10 22:19:41 -020060 "\t-h, --help show this help\n",
Lucas De Marchi7c04aee2012-11-06 19:20:09 -020061 program_invocation_short_name);
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -020062}
63
Dave Reisner6da9cdf2012-01-10 19:28:30 -050064static int check_module_inuse(struct kmod_module *mod) {
65 struct kmod_list *holders;
Josh Boyerd53abca2013-03-15 13:43:40 -040066 int state;
Dave Reisner6da9cdf2012-01-10 19:28:30 -050067
Josh Boyerd53abca2013-03-15 13:43:40 -040068 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 Marchi04c26d22012-11-05 17:51:32 -020074 ERR("Module %s is not currently loaded\n",
Dave Reisner6da9cdf2012-01-10 19:28:30 -050075 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 Marchi04c26d22012-11-05 17:51:32 -020083 ERR("Module %s is in use by:", kmod_module_get_name(mod));
Dave Reisner6da9cdf2012-01-10 19:28:30 -050084
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 Marchi04c26d22012-11-05 17:51:32 -020097 ERR("Module %s is in use\n", kmod_module_get_name(mod));
Dave Reisner6da9cdf2012-01-10 19:28:30 -050098 return -EBUSY;
99 }
100
101 return 0;
102}
103
Lucas De Marchif712ebc2011-12-22 03:39:11 -0200104static int do_rmmod(int argc, char *argv[])
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200105{
106 struct kmod_ctx *ctx;
Lucas De Marchi2411c072011-12-12 15:41:02 -0200107 const char *null_config = NULL;
Lucas De Marchi017893f2013-09-20 01:40:58 -0500108 int flags = 0;
Dave Reisner6da9cdf2012-01-10 19:28:30 -0500109 int i, err, r = 0;
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200110
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 Barbieri72c51a92011-12-10 22:19:41 -0200126 case 'h':
Lucas De Marchi4a2e20d2012-11-06 16:54:17 -0200127 help();
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200128 return EXIT_SUCCESS;
129 case 'V':
130 puts(PACKAGE " version " VERSION);
Lucas De Marchi655de272015-06-07 02:46:22 -0300131 puts(KMOD_FEATURES);
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200132 return EXIT_SUCCESS;
133 case '?':
134 return EXIT_FAILURE;
135 default:
Lucas De Marchi04c26d22012-11-05 17:51:32 -0200136 ERR("unexpected getopt_long() value '%c'.\n", c);
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200137 return EXIT_FAILURE;
138 }
139 }
140
Lucas De Marchi92aad742012-11-06 18:04:09 -0200141 log_open(use_syslog);
Lucas De Marchie6996c52012-11-05 18:32:05 -0200142
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200143 if (optind >= argc) {
Lucas De Marchi04c26d22012-11-05 17:51:32 -0200144 ERR("missing module name.\n");
Lucas De Marchie6996c52012-11-05 18:32:05 -0200145 r = EXIT_FAILURE;
146 goto done;
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200147 }
148
Lucas De Marchi2411c072011-12-12 15:41:02 -0200149 ctx = kmod_new(NULL, &null_config);
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200150 if (!ctx) {
Lucas De Marchi04c26d22012-11-05 17:51:32 -0200151 ERR("kmod_new() failed!\n");
Lucas De Marchie6996c52012-11-05 18:32:05 -0200152 r = EXIT_FAILURE;
153 goto done;
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200154 }
155
Lucas De Marchi52a50fe2012-11-06 18:26:34 -0200156 log_setup_kmod_log(ctx, verbose);
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200157
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 Marchi04c26d22012-11-05 17:51:32 -0200168 ERR("could not use module %s: %s\n", arg,
169 strerror(-err));
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200170 break;
171 }
172
Lucas De Marchi017893f2013-09-20 01:40:58 -0500173 if (!(flags & KMOD_REMOVE_FORCE) && check_module_inuse(mod) < 0) {
174 r++;
175 goto next;
176 }
Dave Reisner6da9cdf2012-01-10 19:28:30 -0500177
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200178 err = kmod_module_remove_module(mod, flags);
179 if (err < 0) {
Lucas De Marchi04c26d22012-11-05 17:51:32 -0200180 ERR("could not remove module %s: %s\n", arg,
181 strerror(-err));
Dave Reisner6da9cdf2012-01-10 19:28:30 -0500182 r++;
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200183 }
Dave Reisner6da9cdf2012-01-10 19:28:30 -0500184next:
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200185 kmod_module_unref(mod);
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200186 }
187
188 kmod_unref(ctx);
189
Lucas De Marchie6996c52012-11-05 18:32:05 -0200190done:
Lucas De Marchi92aad742012-11-06 18:04:09 -0200191 log_close();
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200192
Dave Reisner6da9cdf2012-01-10 19:28:30 -0500193 return r == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200194}
Lucas De Marchif712ebc2011-12-22 03:39:11 -0200195
Lucas De Marchif712ebc2011-12-22 03:39:11 -0200196const struct kmod_cmd kmod_cmd_compat_rmmod = {
197 .name = "rmmod",
198 .cmd = do_rmmod,
199 .help = "compat rmmod command",
200};