blob: df2f0416ed0f1c0ad4081d885f0a722f3a490a37 [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
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>
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -020028#include "libkmod.h"
Lucas De Marchie6996c52012-11-05 18:32:05 -020029#include "macro.h"
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -020030
Lucas De Marchi4a2e20d2012-11-06 16:54:17 -020031#include "kmod.h"
32
Lucas De Marchie6996c52012-11-05 18:32:05 -020033#define DEFAULT_VERBOSE LOG_ERR
34static int verbose = DEFAULT_VERBOSE;
35static int use_syslog;
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -020036
37static const char cmdopts_s[] = "fsvVwh";
38static const struct option cmdopts[] = {
39 {"force", no_argument, 0, 'f'},
40 {"syslog", no_argument, 0, 's'},
41 {"verbose", no_argument, 0, 'v'},
42 {"version", no_argument, 0, 'V'},
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -020043 {"help", no_argument, 0, 'h'},
44 {NULL, 0, 0, 0}
45};
46
Lucas De Marchi4a2e20d2012-11-06 16:54:17 -020047static void help(void)
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -020048{
Lucas De Marchi34e06bf2012-11-06 17:32:41 -020049 printf("Usage:\n"
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -020050 "\t%s [options] modulename ...\n"
51 "Options:\n"
52 "\t-f, --force forces a module unload and may crash your\n"
53 "\t machine. This requires Forced Module Removal\n"
54 "\t option in your kernel. DANGEROUS\n"
55 "\t-s, --syslog print to syslog, not stderr\n"
56 "\t-v, --verbose enables more messages\n"
57 "\t-V, --version show version\n"
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -020058 "\t-h, --help show this help\n",
Lucas De Marchi7c04aee2012-11-06 19:20:09 -020059 program_invocation_short_name);
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -020060}
61
Dave Reisner6da9cdf2012-01-10 19:28:30 -050062static int check_module_inuse(struct kmod_module *mod) {
63 struct kmod_list *holders;
Josh Boyerd53abca2013-03-15 13:43:40 -040064 int state;
Dave Reisner6da9cdf2012-01-10 19:28:30 -050065
Josh Boyerd53abca2013-03-15 13:43:40 -040066 state = kmod_module_get_initstate(mod);
67
68 if (state == KMOD_MODULE_BUILTIN) {
69 ERR("Module %s is builtin.\n", kmod_module_get_name(mod));
70 return -ENOENT;
71 } else if (state < 0) {
Lucas De Marchi04c26d22012-11-05 17:51:32 -020072 ERR("Module %s is not currently loaded\n",
Dave Reisner6da9cdf2012-01-10 19:28:30 -050073 kmod_module_get_name(mod));
74 return -ENOENT;
75 }
76
77 holders = kmod_module_get_holders(mod);
78 if (holders != NULL) {
79 struct kmod_list *itr;
80
Lucas De Marchi04c26d22012-11-05 17:51:32 -020081 ERR("Module %s is in use by:", kmod_module_get_name(mod));
Dave Reisner6da9cdf2012-01-10 19:28:30 -050082
83 kmod_list_foreach(itr, holders) {
84 struct kmod_module *hm = kmod_module_get_module(itr);
85 fprintf(stderr, " %s", kmod_module_get_name(hm));
86 kmod_module_unref(hm);
87 }
88 fputc('\n', stderr);
89
90 kmod_module_unref_list(holders);
91 return -EBUSY;
92 }
93
94 if (kmod_module_get_refcnt(mod) != 0) {
Lucas De Marchi04c26d22012-11-05 17:51:32 -020095 ERR("Module %s is in use\n", kmod_module_get_name(mod));
Dave Reisner6da9cdf2012-01-10 19:28:30 -050096 return -EBUSY;
97 }
98
99 return 0;
100}
101
Lucas De Marchif712ebc2011-12-22 03:39:11 -0200102static int do_rmmod(int argc, char *argv[])
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200103{
104 struct kmod_ctx *ctx;
Lucas De Marchi2411c072011-12-12 15:41:02 -0200105 const char *null_config = NULL;
Lucas De Marchi017893f2013-09-20 01:40:58 -0500106 int flags = 0;
Dave Reisner6da9cdf2012-01-10 19:28:30 -0500107 int i, err, r = 0;
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200108
109 for (;;) {
110 int c, idx = 0;
111 c = getopt_long(argc, argv, cmdopts_s, cmdopts, &idx);
112 if (c == -1)
113 break;
114 switch (c) {
115 case 'f':
116 flags |= KMOD_REMOVE_FORCE;
117 break;
118 case 's':
119 use_syslog = 1;
120 break;
121 case 'v':
122 verbose++;
123 break;
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200124 case 'h':
Lucas De Marchi4a2e20d2012-11-06 16:54:17 -0200125 help();
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200126 return EXIT_SUCCESS;
127 case 'V':
128 puts(PACKAGE " version " VERSION);
129 return EXIT_SUCCESS;
130 case '?':
131 return EXIT_FAILURE;
132 default:
Lucas De Marchi04c26d22012-11-05 17:51:32 -0200133 ERR("unexpected getopt_long() value '%c'.\n", c);
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200134 return EXIT_FAILURE;
135 }
136 }
137
Lucas De Marchi92aad742012-11-06 18:04:09 -0200138 log_open(use_syslog);
Lucas De Marchie6996c52012-11-05 18:32:05 -0200139
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200140 if (optind >= argc) {
Lucas De Marchi04c26d22012-11-05 17:51:32 -0200141 ERR("missing module name.\n");
Lucas De Marchie6996c52012-11-05 18:32:05 -0200142 r = EXIT_FAILURE;
143 goto done;
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200144 }
145
Lucas De Marchi2411c072011-12-12 15:41:02 -0200146 ctx = kmod_new(NULL, &null_config);
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200147 if (!ctx) {
Lucas De Marchi04c26d22012-11-05 17:51:32 -0200148 ERR("kmod_new() failed!\n");
Lucas De Marchie6996c52012-11-05 18:32:05 -0200149 r = EXIT_FAILURE;
150 goto done;
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200151 }
152
Lucas De Marchi52a50fe2012-11-06 18:26:34 -0200153 log_setup_kmod_log(ctx, verbose);
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200154
155 for (i = optind; i < argc; i++) {
156 struct kmod_module *mod;
157 const char *arg = argv[i];
158 struct stat st;
159 if (stat(arg, &st) == 0)
160 err = kmod_module_new_from_path(ctx, arg, &mod);
161 else
162 err = kmod_module_new_from_name(ctx, arg, &mod);
163
164 if (err < 0) {
Lucas De Marchi04c26d22012-11-05 17:51:32 -0200165 ERR("could not use module %s: %s\n", arg,
166 strerror(-err));
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200167 break;
168 }
169
Lucas De Marchi017893f2013-09-20 01:40:58 -0500170 if (!(flags & KMOD_REMOVE_FORCE) && check_module_inuse(mod) < 0) {
171 r++;
172 goto next;
173 }
Dave Reisner6da9cdf2012-01-10 19:28:30 -0500174
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200175 err = kmod_module_remove_module(mod, flags);
176 if (err < 0) {
Lucas De Marchi04c26d22012-11-05 17:51:32 -0200177 ERR("could not remove module %s: %s\n", arg,
178 strerror(-err));
Dave Reisner6da9cdf2012-01-10 19:28:30 -0500179 r++;
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200180 }
Dave Reisner6da9cdf2012-01-10 19:28:30 -0500181next:
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200182 kmod_module_unref(mod);
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200183 }
184
185 kmod_unref(ctx);
186
Lucas De Marchie6996c52012-11-05 18:32:05 -0200187done:
Lucas De Marchi92aad742012-11-06 18:04:09 -0200188 log_close();
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200189
Dave Reisner6da9cdf2012-01-10 19:28:30 -0500190 return r == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200191}
Lucas De Marchif712ebc2011-12-22 03:39:11 -0200192
Lucas De Marchif712ebc2011-12-22 03:39:11 -0200193const struct kmod_cmd kmod_cmd_compat_rmmod = {
194 .name = "rmmod",
195 .cmd = do_rmmod,
196 .help = "compat rmmod command",
197};