2 * kmod-rmmod - remove modules from linux kernel using libkmod.
4 * Copyright (C) 2011-2013 ProFUSION embedded systems
6 * 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.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * 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/>.
27 #include <sys/types.h>
29 #include <shared/macro.h>
31 #include <libkmod/libkmod.h>
35 #define DEFAULT_VERBOSE LOG_ERR
36 static int verbose
= DEFAULT_VERBOSE
;
37 static int use_syslog
;
39 static const char cmdopts_s
[] = "fsvVwh";
40 static 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'},
45 {"help", no_argument
, 0, 'h'},
49 static void help(void)
52 "\t%s [options] modulename ...\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"
60 "\t-h, --help show this help\n",
61 program_invocation_short_name
);
64 static int check_module_inuse(struct kmod_module
*mod
) {
65 struct kmod_list
*holders
;
68 state
= kmod_module_get_initstate(mod
);
70 if (state
== KMOD_MODULE_BUILTIN
) {
71 ERR("Module %s is builtin.\n", kmod_module_get_name(mod
));
73 } else if (state
< 0) {
74 ERR("Module %s is not currently loaded\n",
75 kmod_module_get_name(mod
));
79 holders
= kmod_module_get_holders(mod
);
80 if (holders
!= NULL
) {
81 struct kmod_list
*itr
;
83 ERR("Module %s is in use by:", kmod_module_get_name(mod
));
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
);
92 kmod_module_unref_list(holders
);
96 ret
= kmod_module_get_refcnt(mod
);
98 ERR("Module %s is in use\n", kmod_module_get_name(mod
));
100 } else if (ret
== -ENOENT
) {
101 ERR("Module unloading is not supported\n");
107 static int do_rmmod(int argc
, char *argv
[])
109 struct kmod_ctx
*ctx
;
110 const char *null_config
= NULL
;
116 c
= getopt_long(argc
, argv
, cmdopts_s
, cmdopts
, &idx
);
121 flags
|= KMOD_REMOVE_FORCE
;
133 puts(PACKAGE
" version " VERSION
);
139 ERR("unexpected getopt_long() value '%c'.\n", c
);
144 log_open(use_syslog
);
146 if (optind
>= argc
) {
147 ERR("missing module name.\n");
152 ctx
= kmod_new(NULL
, &null_config
);
154 ERR("kmod_new() failed!\n");
159 log_setup_kmod_log(ctx
, verbose
);
161 for (i
= optind
; i
< argc
; i
++) {
162 struct kmod_module
*mod
;
163 const char *arg
= argv
[i
];
165 if (stat(arg
, &st
) == 0)
166 err
= kmod_module_new_from_path(ctx
, arg
, &mod
);
168 err
= kmod_module_new_from_name(ctx
, arg
, &mod
);
171 ERR("could not use module %s: %s\n", arg
,
176 if (!(flags
& KMOD_REMOVE_FORCE
) && check_module_inuse(mod
) < 0) {
181 err
= kmod_module_remove_module(mod
, flags
);
183 ERR("could not remove module %s: %s\n", arg
,
188 kmod_module_unref(mod
);
196 return r
== 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;
199 const struct kmod_cmd kmod_cmd_compat_rmmod
= {
202 .help
= "compat rmmod command",