1 /*
2 * kmod-rmmod - remove modules from linux kernel using libkmod.
3 *
4 * Copyright (C) 2011-2013 ProFUSION embedded systems
5 *
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.
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
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
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/>.
18 */
19
20 #include <errno.h>
21 #include <getopt.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <unistd.h>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28
29 #include <shared/macro.h>
30
31 #include <libkmod/libkmod.h>
32
33 #include "kmod.h"
34
35 #define DEFAULT_VERBOSE LOG_ERR
36 static int verbose = DEFAULT_VERBOSE;
37 static int use_syslog;
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'},
45 {"help", no_argument, 0, 'h'},
46 {NULL, 0, 0, 0}
47 };
48
help(void)49 static void help(void)
50 {
51 printf("Usage:\n"
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"
60 "\t-h, --help show this help\n",
61 program_invocation_short_name);
62 }
63
check_module_inuse(struct kmod_module * mod)64 static int check_module_inuse(struct kmod_module *mod) {
65 struct kmod_list *holders;
66 int state, ret;
67
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) {
74 ERR("Module %s is not currently loaded\n",
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
83 ERR("Module %s is in use by:", kmod_module_get_name(mod));
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
96 ret = kmod_module_get_refcnt(mod);
97 if (ret > 0) {
98 ERR("Module %s is in use\n", kmod_module_get_name(mod));
99 return -EBUSY;
100 } else if (ret == -ENOENT) {
101 ERR("Module unloading is not supported\n");
102 }
103
104 return ret;
105 }
106
do_rmmod(int argc,char * argv[])107 static int do_rmmod(int argc, char *argv[])
108 {
109 struct kmod_ctx *ctx;
110 const char *null_config = NULL;
111 int flags = 0;
112 int i, err, r = 0;
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;
129 case 'h':
130 help();
131 return EXIT_SUCCESS;
132 case 'V':
133 puts(PACKAGE " version " VERSION);
134 puts(KMOD_FEATURES);
135 return EXIT_SUCCESS;
136 case '?':
137 return EXIT_FAILURE;
138 default:
139 ERR("unexpected getopt_long() value '%c'.\n", c);
140 return EXIT_FAILURE;
141 }
142 }
143
144 log_open(use_syslog);
145
146 if (optind >= argc) {
147 ERR("missing module name.\n");
148 r = EXIT_FAILURE;
149 goto done;
150 }
151
152 ctx = kmod_new(NULL, &null_config);
153 if (!ctx) {
154 ERR("kmod_new() failed!\n");
155 r = EXIT_FAILURE;
156 goto done;
157 }
158
159 log_setup_kmod_log(ctx, verbose);
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) {
171 ERR("could not use module %s: %s\n", arg,
172 strerror(-err));
173 break;
174 }
175
176 if (!(flags & KMOD_REMOVE_FORCE) && check_module_inuse(mod) < 0) {
177 r++;
178 goto next;
179 }
180
181 err = kmod_module_remove_module(mod, flags);
182 if (err < 0) {
183 ERR("could not remove module %s: %s\n", arg,
184 strerror(-err));
185 r++;
186 }
187 next:
188 kmod_module_unref(mod);
189 }
190
191 kmod_unref(ctx);
192
193 done:
194 log_close();
195
196 return r == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
197 }
198
199 const struct kmod_cmd kmod_cmd_compat_rmmod = {
200 .name = "rmmod",
201 .cmd = do_rmmod,
202 .help = "compat rmmod command",
203 };
204