]>
Commit | Line | Data |
---|---|---|
72c51a9e GSB |
1 | /* |
2 | * kmod-rmmod - remove modules from linux kernel using libkmod. | |
3 | * | |
e6b0e49b | 4 | * Copyright (C) 2011-2013 ProFUSION embedded systems |
72c51a9e | 5 | * |
cb451f35 LDM |
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. | |
72c51a9e GSB |
10 | * |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
cb451f35 LDM |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | * GNU General Public License for more details. | |
72c51a9e | 15 | * |
cb451f35 LDM |
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/>. | |
72c51a9e | 18 | */ |
cb451f35 | 19 | |
c2e4286b LDM |
20 | #include <errno.h> |
21 | #include <getopt.h> | |
72c51a9e GSB |
22 | #include <stdio.h> |
23 | #include <stdlib.h> | |
72c51a9e | 24 | #include <string.h> |
72c51a9e | 25 | #include <unistd.h> |
c2e4286b LDM |
26 | #include <sys/stat.h> |
27 | #include <sys/types.h> | |
28 | ||
576dd439 | 29 | #include <shared/macro.h> |
72c51a9e | 30 | |
f357866d | 31 | #include <libkmod/libkmod.h> |
c2e4286b | 32 | |
4a2e20df LDM |
33 | #include "kmod.h" |
34 | ||
e6996c5c LDM |
35 | #define DEFAULT_VERBOSE LOG_ERR |
36 | static int verbose = DEFAULT_VERBOSE; | |
37 | static int use_syslog; | |
72c51a9e GSB |
38 | |
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'}, | |
72c51a9e GSB |
45 | {"help", no_argument, 0, 'h'}, |
46 | {NULL, 0, 0, 0} | |
47 | }; | |
48 | ||
4a2e20df | 49 | static void help(void) |
72c51a9e | 50 | { |
34e06bfb | 51 | printf("Usage:\n" |
72c51a9e GSB |
52 | "\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" | |
72c51a9e | 60 | "\t-h, --help show this help\n", |
7c04aeee | 61 | program_invocation_short_name); |
72c51a9e GSB |
62 | } |
63 | ||
6da9cdf1 DR |
64 | static int check_module_inuse(struct kmod_module *mod) { |
65 | struct kmod_list *holders; | |
ea3e508f | 66 | int state, ret; |
6da9cdf1 | 67 | |
d53abca3 JB |
68 | 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) { | |
04c26d28 | 74 | ERR("Module %s is not currently loaded\n", |
6da9cdf1 DR |
75 | 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 | ||
04c26d28 | 83 | ERR("Module %s is in use by:", kmod_module_get_name(mod)); |
6da9cdf1 DR |
84 | |
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 | ||
ea3e508f EG |
96 | ret = kmod_module_get_refcnt(mod); |
97 | if (ret > 0) { | |
04c26d28 | 98 | ERR("Module %s is in use\n", kmod_module_get_name(mod)); |
6da9cdf1 | 99 | return -EBUSY; |
ea3e508f EG |
100 | } else if (ret == -ENOENT) { |
101 | ERR("Module unloading is not supported\n"); | |
6da9cdf1 DR |
102 | } |
103 | ||
ea3e508f | 104 | return ret; |
6da9cdf1 DR |
105 | } |
106 | ||
f712ebc4 | 107 | static int do_rmmod(int argc, char *argv[]) |
72c51a9e GSB |
108 | { |
109 | struct kmod_ctx *ctx; | |
2411c077 | 110 | const char *null_config = NULL; |
017893f2 | 111 | int flags = 0; |
6da9cdf1 | 112 | int i, err, r = 0; |
72c51a9e GSB |
113 | |
114 | for (;;) { | |
115 | int c, idx = 0; | |
116 | c = getopt_long(argc, argv, cmdopts_s, cmdopts, &idx); | |
117 | if (c == -1) | |
118 | break; | |
119 | switch (c) { | |
120 | case 'f': | |
121 | flags |= KMOD_REMOVE_FORCE; | |
122 | break; | |
123 | case 's': | |
124 | use_syslog = 1; | |
125 | break; | |
126 | case 'v': | |
127 | verbose++; | |
128 | break; | |
72c51a9e | 129 | case 'h': |
4a2e20df | 130 | help(); |
72c51a9e GSB |
131 | return EXIT_SUCCESS; |
132 | case 'V': | |
133 | puts(PACKAGE " version " VERSION); | |
655de275 | 134 | puts(KMOD_FEATURES); |
72c51a9e GSB |
135 | return EXIT_SUCCESS; |
136 | case '?': | |
137 | return EXIT_FAILURE; | |
138 | default: | |
04c26d28 | 139 | ERR("unexpected getopt_long() value '%c'.\n", c); |
72c51a9e GSB |
140 | return EXIT_FAILURE; |
141 | } | |
142 | } | |
143 | ||
92aad749 | 144 | log_open(use_syslog); |
e6996c5c | 145 | |
72c51a9e | 146 | if (optind >= argc) { |
04c26d28 | 147 | ERR("missing module name.\n"); |
e6996c5c LDM |
148 | r = EXIT_FAILURE; |
149 | goto done; | |
72c51a9e GSB |
150 | } |
151 | ||
2411c077 | 152 | ctx = kmod_new(NULL, &null_config); |
72c51a9e | 153 | if (!ctx) { |
04c26d28 | 154 | ERR("kmod_new() failed!\n"); |
e6996c5c LDM |
155 | r = EXIT_FAILURE; |
156 | goto done; | |
72c51a9e GSB |
157 | } |
158 | ||
52a50fe2 | 159 | log_setup_kmod_log(ctx, verbose); |
72c51a9e GSB |
160 | |
161 | for (i = optind; i < argc; i++) { | |
162 | struct kmod_module *mod; | |
163 | const char *arg = argv[i]; | |
164 | struct stat st; | |
165 | if (stat(arg, &st) == 0) | |
166 | err = kmod_module_new_from_path(ctx, arg, &mod); | |
167 | else | |
168 | err = kmod_module_new_from_name(ctx, arg, &mod); | |
169 | ||
170 | if (err < 0) { | |
04c26d28 LDM |
171 | ERR("could not use module %s: %s\n", arg, |
172 | strerror(-err)); | |
72c51a9e GSB |
173 | break; |
174 | } | |
175 | ||
017893f2 LDM |
176 | if (!(flags & KMOD_REMOVE_FORCE) && check_module_inuse(mod) < 0) { |
177 | r++; | |
178 | goto next; | |
179 | } | |
6da9cdf1 | 180 | |
72c51a9e GSB |
181 | err = kmod_module_remove_module(mod, flags); |
182 | if (err < 0) { | |
04c26d28 LDM |
183 | ERR("could not remove module %s: %s\n", arg, |
184 | strerror(-err)); | |
6da9cdf1 | 185 | r++; |
72c51a9e | 186 | } |
6da9cdf1 | 187 | next: |
72c51a9e | 188 | kmod_module_unref(mod); |
72c51a9e GSB |
189 | } |
190 | ||
191 | kmod_unref(ctx); | |
192 | ||
e6996c5c | 193 | done: |
92aad749 | 194 | log_close(); |
72c51a9e | 195 | |
6da9cdf1 | 196 | return r == 0 ? EXIT_SUCCESS : EXIT_FAILURE; |
72c51a9e | 197 | } |
f712ebc4 | 198 | |
f712ebc4 LDM |
199 | const struct kmod_cmd kmod_cmd_compat_rmmod = { |
200 | .name = "rmmod", | |
201 | .cmd = do_rmmod, | |
202 | .help = "compat rmmod command", | |
203 | }; |