blob: f7201ae76405e6ac2287a3455016e86a303feb97 [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{
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 Marchi4a2e20d2012-11-06 16:54:17 -020061 binname);
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -020062}
63
Lucas De Marchie6996c52012-11-05 18:32:05 -020064static void _log(int prio, const char *fmt, ...)
65{
66 const char *prioname;
67 char *msg;
68 va_list args;
69
70 if (prio > verbose)
71 return;
72
73 va_start(args, fmt);
74 if (vasprintf(&msg, fmt, args) < 0)
75 msg = NULL;
76 va_end(args);
77 if (msg == NULL)
78 return;
79
80 prioname = prio_to_str(prio);
81
Lucas De Marchi92aad742012-11-06 18:04:09 -020082 if (use_syslog)
Lucas De Marchie6996c52012-11-05 18:32:05 -020083 syslog(prio, "%s: %s", prioname, msg);
84 else
85 fprintf(stderr, "rmmod: %s: %s", prioname, msg);
86 free(msg);
87
88 if (prio <= LOG_CRIT)
89 exit(EXIT_FAILURE);
90}
91#define ERR(...) _log(LOG_ERR, __VA_ARGS__)
92
Dave Reisner6da9cdf2012-01-10 19:28:30 -050093static int check_module_inuse(struct kmod_module *mod) {
94 struct kmod_list *holders;
95
96 if (kmod_module_get_initstate(mod) == -ENOENT) {
Lucas De Marchi04c26d22012-11-05 17:51:32 -020097 ERR("Module %s is not currently loaded\n",
Dave Reisner6da9cdf2012-01-10 19:28:30 -050098 kmod_module_get_name(mod));
99 return -ENOENT;
100 }
101
102 holders = kmod_module_get_holders(mod);
103 if (holders != NULL) {
104 struct kmod_list *itr;
105
Lucas De Marchi04c26d22012-11-05 17:51:32 -0200106 ERR("Module %s is in use by:", kmod_module_get_name(mod));
Dave Reisner6da9cdf2012-01-10 19:28:30 -0500107
108 kmod_list_foreach(itr, holders) {
109 struct kmod_module *hm = kmod_module_get_module(itr);
110 fprintf(stderr, " %s", kmod_module_get_name(hm));
111 kmod_module_unref(hm);
112 }
113 fputc('\n', stderr);
114
115 kmod_module_unref_list(holders);
116 return -EBUSY;
117 }
118
119 if (kmod_module_get_refcnt(mod) != 0) {
Lucas De Marchi04c26d22012-11-05 17:51:32 -0200120 ERR("Module %s is in use\n", kmod_module_get_name(mod));
Dave Reisner6da9cdf2012-01-10 19:28:30 -0500121 return -EBUSY;
122 }
123
124 return 0;
125}
126
Lucas De Marchif712ebc2011-12-22 03:39:11 -0200127static int do_rmmod(int argc, char *argv[])
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200128{
129 struct kmod_ctx *ctx;
Lucas De Marchi2411c072011-12-12 15:41:02 -0200130 const char *null_config = NULL;
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200131 int flags = KMOD_REMOVE_NOWAIT;
Dave Reisner6da9cdf2012-01-10 19:28:30 -0500132 int i, err, r = 0;
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200133
134 for (;;) {
135 int c, idx = 0;
136 c = getopt_long(argc, argv, cmdopts_s, cmdopts, &idx);
137 if (c == -1)
138 break;
139 switch (c) {
140 case 'f':
141 flags |= KMOD_REMOVE_FORCE;
142 break;
143 case 's':
144 use_syslog = 1;
145 break;
146 case 'v':
147 verbose++;
148 break;
149 case 'w':
Lucas De Marchi04c26d22012-11-05 17:51:32 -0200150 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 -0300151 sleep(10);
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200152 flags &= ~KMOD_REMOVE_NOWAIT;
153 break;
154 case 'h':
Lucas De Marchi4a2e20d2012-11-06 16:54:17 -0200155 help();
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200156 return EXIT_SUCCESS;
157 case 'V':
158 puts(PACKAGE " version " VERSION);
159 return EXIT_SUCCESS;
160 case '?':
161 return EXIT_FAILURE;
162 default:
Lucas De Marchi04c26d22012-11-05 17:51:32 -0200163 ERR("unexpected getopt_long() value '%c'.\n", c);
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200164 return EXIT_FAILURE;
165 }
166 }
167
Lucas De Marchi92aad742012-11-06 18:04:09 -0200168 log_open(use_syslog);
Lucas De Marchie6996c52012-11-05 18:32:05 -0200169
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200170 if (optind >= argc) {
Lucas De Marchi04c26d22012-11-05 17:51:32 -0200171 ERR("missing module name.\n");
Lucas De Marchie6996c52012-11-05 18:32:05 -0200172 r = EXIT_FAILURE;
173 goto done;
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200174 }
175
Lucas De Marchi2411c072011-12-12 15:41:02 -0200176 ctx = kmod_new(NULL, &null_config);
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200177 if (!ctx) {
Lucas De Marchi04c26d22012-11-05 17:51:32 -0200178 ERR("kmod_new() failed!\n");
Lucas De Marchie6996c52012-11-05 18:32:05 -0200179 r = EXIT_FAILURE;
180 goto done;
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200181 }
182
Lucas De Marchie6996c52012-11-05 18:32:05 -0200183 kmod_set_log_priority(ctx, kmod_get_log_priority(ctx));
Lucas De Marchi92aad742012-11-06 18:04:09 -0200184 kmod_set_log_fn(ctx, log_kmod, NULL);
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200185
186 for (i = optind; i < argc; i++) {
187 struct kmod_module *mod;
188 const char *arg = argv[i];
189 struct stat st;
190 if (stat(arg, &st) == 0)
191 err = kmod_module_new_from_path(ctx, arg, &mod);
192 else
193 err = kmod_module_new_from_name(ctx, arg, &mod);
194
195 if (err < 0) {
Lucas De Marchi04c26d22012-11-05 17:51:32 -0200196 ERR("could not use module %s: %s\n", arg,
197 strerror(-err));
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200198 break;
199 }
200
Dave Reisner6da9cdf2012-01-10 19:28:30 -0500201 if (!(flags & KMOD_REMOVE_FORCE) && (flags & KMOD_REMOVE_NOWAIT))
202 if (check_module_inuse(mod) < 0) {
203 r++;
204 goto next;
205 }
206
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200207 err = kmod_module_remove_module(mod, flags);
208 if (err < 0) {
Lucas De Marchi04c26d22012-11-05 17:51:32 -0200209 ERR("could not remove module %s: %s\n", arg,
210 strerror(-err));
Dave Reisner6da9cdf2012-01-10 19:28:30 -0500211 r++;
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200212 }
Dave Reisner6da9cdf2012-01-10 19:28:30 -0500213next:
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200214 kmod_module_unref(mod);
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200215 }
216
217 kmod_unref(ctx);
218
Lucas De Marchie6996c52012-11-05 18:32:05 -0200219done:
Lucas De Marchi92aad742012-11-06 18:04:09 -0200220 log_close();
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200221
Dave Reisner6da9cdf2012-01-10 19:28:30 -0500222 return r == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
Gustavo Sverzut Barbieri72c51a92011-12-10 22:19:41 -0200223}
Lucas De Marchif712ebc2011-12-22 03:39:11 -0200224
Lucas De Marchif712ebc2011-12-22 03:39:11 -0200225const struct kmod_cmd kmod_cmd_compat_rmmod = {
226 .name = "rmmod",
227 .cmd = do_rmmod,
228 .help = "compat rmmod command",
229};