2 * kmod-remove - remove modules from the kernel.
4 * Copyright (C) 2015 Intel Corporation. All rights reserved.
5 * Copyright (C) 2011-2013 ProFUSION embedded systems
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 #include <libkmod/libkmod.h>
30 static const char cmdopts_s
[] = "h";
31 static const struct option cmdopts
[] = {
32 {"help", no_argument
, 0, 'h'},
36 static void help(void)
39 "\t%s remove [options] module\n"
41 "\t-h, --help show this help\n",
42 program_invocation_short_name
);
45 static int check_module_inuse(struct kmod_module
*mod
) {
46 struct kmod_list
*holders
;
49 state
= kmod_module_get_initstate(mod
);
51 if (state
== KMOD_MODULE_BUILTIN
) {
52 ERR("Module %s is builtin.\n", kmod_module_get_name(mod
));
54 } else if (state
< 0) {
55 ERR("Module %s is not currently loaded\n",
56 kmod_module_get_name(mod
));
60 holders
= kmod_module_get_holders(mod
);
61 if (holders
!= NULL
) {
62 struct kmod_list
*itr
;
64 ERR("Module %s is in use by:", kmod_module_get_name(mod
));
66 kmod_list_foreach(itr
, holders
) {
67 struct kmod_module
*hm
= kmod_module_get_module(itr
);
68 fprintf(stderr
, " %s", kmod_module_get_name(hm
));
69 kmod_module_unref(hm
);
73 kmod_module_unref_list(holders
);
77 if (kmod_module_get_refcnt(mod
) != 0) {
78 ERR("Module %s is in use\n", kmod_module_get_name(mod
));
85 static int do_remove(int argc
, char *argv
[])
88 struct kmod_module
*mod
;
90 int err
, r
= EXIT_SUCCESS
;
94 c
= getopt_long(argc
, argv
, cmdopts_s
, cmdopts
, &idx
);
103 ERR("Unexpected getopt_long() value '%c'.\n", c
);
108 if (optind
>= argc
) {
109 ERR("Missing module name\n");
113 ctx
= kmod_new(NULL
, NULL
);
115 ERR("kmod_new() failed!\n");
120 err
= kmod_module_new_from_name(ctx
, name
, &mod
);
122 ERR("Could not remove module %s: %s\n", name
, strerror(-err
));
126 err
= check_module_inuse(mod
);
130 err
= kmod_module_remove_module(mod
, 0);
135 kmod_module_unref(mod
);
141 ERR("Could not remove module %s: %s\n", name
, strerror(-err
));
146 const struct kmod_cmd kmod_cmd_remove
= {
149 .help
= "remove module from kernel",