]>
Commit | Line | Data |
---|---|---|
c3d0a5f2 | 1 | /* |
8b013767 | 2 | * kmod-modprobe - manage linux kernel modules using libkmod. |
c3d0a5f2 | 3 | * |
e6b0e49b | 4 | * Copyright (C) 2011-2013 ProFUSION embedded systems |
c3d0a5f2 | 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. | |
c3d0a5f2 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. | |
c3d0a5f2 | 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/>. | |
c3d0a5f2 | 18 | */ |
cb451f35 | 19 | |
4744ebce | 20 | #include <assert.h> |
c2e4286b LDM |
21 | #include <errno.h> |
22 | #include <getopt.h> | |
23 | #include <limits.h> | |
24 | #include <stdbool.h> | |
c3d0a5f2 GSB |
25 | #include <stdio.h> |
26 | #include <stdlib.h> | |
c3d0a5f2 | 27 | #include <string.h> |
c2e4286b | 28 | #include <unistd.h> |
c3d0a5f2 | 29 | #include <sys/stat.h> |
c2e4286b | 30 | #include <sys/types.h> |
c3d0a5f2 | 31 | #include <sys/utsname.h> |
eff917c0 | 32 | #include <sys/wait.h> |
74d1df66 LDM |
33 | |
34 | #include <shared/array.h> | |
2b98ed88 | 35 | #include <shared/util.h> |
576dd439 | 36 | #include <shared/macro.h> |
c3d0a5f2 | 37 | |
f357866d | 38 | #include <libkmod/libkmod.h> |
c2e4286b | 39 | |
4a2e20df LDM |
40 | #include "kmod.h" |
41 | ||
c3d0a5f2 GSB |
42 | static int log_priority = LOG_CRIT; |
43 | static int use_syslog = 0; | |
fcb0ce94 | 44 | #define LOG(...) log_printf(log_priority, __VA_ARGS__) |
c3d0a5f2 GSB |
45 | |
46 | #define DEFAULT_VERBOSE LOG_WARNING | |
47 | static int verbose = DEFAULT_VERBOSE; | |
525fa07b | 48 | static int do_show = 0; |
c3d0a5f2 GSB |
49 | static int dry_run = 0; |
50 | static int ignore_loaded = 0; | |
8122985d | 51 | static int lookup_only = 0; |
c3d0a5f2 GSB |
52 | static int first_time = 0; |
53 | static int ignore_commands = 0; | |
54 | static int use_blacklist = 0; | |
55 | static int force = 0; | |
56 | static int strip_modversion = 0; | |
57 | static int strip_vermagic = 0; | |
42b32d30 | 58 | static int remove_holders = 0; |
2b98ed88 | 59 | static unsigned long long wait_msec = 0; |
d8a6c0cc | 60 | static int quiet_inuse = 0; |
c3d0a5f2 | 61 | |
2b98ed88 | 62 | static const char cmdopts_s[] = "arw:RibfDcnC:d:S:sqvVh"; |
c3d0a5f2 GSB |
63 | static const struct option cmdopts[] = { |
64 | {"all", no_argument, 0, 'a'}, | |
2b98ed88 | 65 | |
c3d0a5f2 GSB |
66 | {"remove", no_argument, 0, 'r'}, |
67 | {"remove-dependencies", no_argument, 0, 5}, | |
42b32d30 | 68 | {"remove-holders", no_argument, 0, 5}, |
2b98ed88 LDM |
69 | {"wait", required_argument, 0, 'w'}, |
70 | ||
c3d0a5f2 GSB |
71 | {"resolve-alias", no_argument, 0, 'R'}, |
72 | {"first-time", no_argument, 0, 3}, | |
73 | {"ignore-install", no_argument, 0, 'i'}, | |
74 | {"ignore-remove", no_argument, 0, 'i'}, | |
75 | {"use-blacklist", no_argument, 0, 'b'}, | |
76 | {"force", no_argument, 0, 'f'}, | |
77 | {"force-modversion", no_argument, 0, 2}, | |
78 | {"force-vermagic", no_argument, 0, 1}, | |
79 | ||
c3d0a5f2 GSB |
80 | {"show-depends", no_argument, 0, 'D'}, |
81 | {"showconfig", no_argument, 0, 'c'}, | |
82 | {"show-config", no_argument, 0, 'c'}, | |
83 | {"show-modversions", no_argument, 0, 4}, | |
84 | {"dump-modversions", no_argument, 0, 4}, | |
3ada8df8 | 85 | {"show-exports", no_argument, 0, 6}, |
c3d0a5f2 GSB |
86 | |
87 | {"dry-run", no_argument, 0, 'n'}, | |
88 | {"show", no_argument, 0, 'n'}, | |
89 | ||
90 | {"config", required_argument, 0, 'C'}, | |
91 | {"dirname", required_argument, 0, 'd'}, | |
92 | {"set-version", required_argument, 0, 'S'}, | |
93 | ||
94 | {"syslog", no_argument, 0, 's'}, | |
95 | {"quiet", no_argument, 0, 'q'}, | |
96 | {"verbose", no_argument, 0, 'v'}, | |
97 | {"version", no_argument, 0, 'V'}, | |
98 | {"help", no_argument, 0, 'h'}, | |
99 | {NULL, 0, 0, 0} | |
100 | }; | |
101 | ||
4a2e20df | 102 | static void help(void) |
c3d0a5f2 | 103 | { |
34e06bfb | 104 | printf("Usage:\n" |
c3d0a5f2 GSB |
105 | "\t%s [options] [-i] [-b] modulename\n" |
106 | "\t%s [options] -a [-i] [-b] modulename [modulename...]\n" | |
107 | "\t%s [options] -r [-i] modulename\n" | |
108 | "\t%s [options] -r -a [-i] modulename [modulename...]\n" | |
c3d0a5f2 GSB |
109 | "\t%s [options] -c\n" |
110 | "\t%s [options] --dump-modversions filename\n" | |
111 | "Management Options:\n" | |
ab70dce1 GSB |
112 | "\t-a, --all Consider every non-argument to\n" |
113 | "\t be a module name to be inserted\n" | |
114 | "\t or removed (-r)\n" | |
115 | "\t-r, --remove Remove modules instead of inserting\n" | |
42b32d30 LDM |
116 | "\t --remove-dependencies Deprecated: use --remove-holders\n" |
117 | "\t --remove-holders Also remove module holders (use together with -r)\n" | |
2b98ed88 LDM |
118 | "\t-w, --wait <MSEC> When removing a module, wait up to MSEC for\n" |
119 | "\t module's refcount to become 0 so it can be\n" | |
120 | "\t removed (use together with -r)\n" | |
ab70dce1 GSB |
121 | "\t --first-time Fail if module already inserted or removed\n" |
122 | "\t-i, --ignore-install Ignore install commands\n" | |
123 | "\t-i, --ignore-remove Ignore remove commands\n" | |
124 | "\t-b, --use-blacklist Apply blacklist to resolved alias.\n" | |
125 | "\t-f, --force Force module insertion or removal.\n" | |
126 | "\t implies --force-modversions and\n" | |
127 | "\t --force-vermagic\n" | |
128 | "\t --force-modversion Ignore module's version\n" | |
129 | "\t --force-vermagic Ignore module's version magic\n" | |
c3d0a5f2 GSB |
130 | "\n" |
131 | "Query Options:\n" | |
081fff2c | 132 | "\t-R, --resolve-alias Only lookup and print alias and exit\n" |
ab70dce1 GSB |
133 | "\t-D, --show-depends Only print module dependencies and exit\n" |
134 | "\t-c, --showconfig Print out known configuration and exit\n" | |
135 | "\t-c, --show-config Same as --showconfig\n" | |
136 | "\t --show-modversions Dump module symbol version and exit\n" | |
137 | "\t --dump-modversions Same as --show-modversions\n" | |
3ada8df8 | 138 | "\t --show-exports Only print module exported symbol versions and exit\n" |
c3d0a5f2 GSB |
139 | "\n" |
140 | "General Options:\n" | |
ab70dce1 GSB |
141 | "\t-n, --dry-run Do not execute operations, just print out\n" |
142 | "\t-n, --show Same as --dry-run\n" | |
c3d0a5f2 | 143 | |
2c96693e | 144 | "\t-C, --config=FILE Use FILE instead of default search paths\n" |
c5b37dba | 145 | "\t-d, --dirname=DIR Use DIR as filesystem root for /lib/modules\n" |
ab70dce1 | 146 | "\t-S, --set-version=VERSION Use VERSION instead of `uname -r`\n" |
c3d0a5f2 GSB |
147 | |
148 | "\t-s, --syslog print to syslog, not stderr\n" | |
149 | "\t-q, --quiet disable messages\n" | |
150 | "\t-v, --verbose enables more messages\n" | |
151 | "\t-V, --version show version\n" | |
152 | "\t-h, --help show this help\n", | |
7c04aeee LDM |
153 | program_invocation_short_name, program_invocation_short_name, |
154 | program_invocation_short_name, program_invocation_short_name, | |
155 | program_invocation_short_name, program_invocation_short_name); | |
c3d0a5f2 GSB |
156 | } |
157 | ||
1958af88 | 158 | _printf_format_(1, 2) |
c3d0a5f2 GSB |
159 | static inline void _show(const char *fmt, ...) |
160 | { | |
161 | va_list args; | |
162 | ||
525fa07b | 163 | if (!do_show && verbose <= DEFAULT_VERBOSE) |
c3d0a5f2 GSB |
164 | return; |
165 | ||
166 | va_start(args, fmt); | |
167 | vfprintf(stdout, fmt, args); | |
168 | fflush(stdout); | |
169 | va_end(args); | |
170 | } | |
c3d0a5f2 GSB |
171 | #define SHOW(...) _show(__VA_ARGS__) |
172 | ||
c3d0a5f2 GSB |
173 | static int show_config(struct kmod_ctx *ctx) |
174 | { | |
bc43496a LDM |
175 | struct config_iterators { |
176 | const char *name; | |
177 | struct kmod_config_iter *(*get_iter)(const struct kmod_ctx *ctx); | |
178 | } ci[] = { | |
179 | { "blacklist", kmod_config_get_blacklists }, | |
180 | { "install", kmod_config_get_install_commands }, | |
181 | { "remove", kmod_config_get_remove_commands }, | |
182 | { "alias", kmod_config_get_aliases }, | |
02629fa0 | 183 | { "options", kmod_config_get_options }, |
bc43496a LDM |
184 | { "softdep", kmod_config_get_softdeps }, |
185 | }; | |
186 | size_t i; | |
187 | ||
188 | for (i = 0; i < ARRAY_SIZE(ci); i++) { | |
189 | struct kmod_config_iter *iter = ci[i].get_iter(ctx); | |
190 | ||
191 | if (iter == NULL) | |
192 | continue; | |
193 | ||
194 | while (kmod_config_iter_next(iter)) { | |
195 | const char *val; | |
196 | ||
197 | printf("%s %s", ci[i].name, | |
198 | kmod_config_iter_get_key(iter)); | |
199 | val = kmod_config_iter_get_value(iter); | |
200 | if (val != NULL) { | |
201 | putchar(' '); | |
202 | puts(val); | |
203 | } else | |
204 | putchar('\n'); | |
205 | } | |
206 | ||
207 | kmod_config_iter_free_iter(iter); | |
208 | } | |
209 | ||
28f32c65 | 210 | puts("\n# End of configuration files. Dumping indexes now:\n"); |
09e9ae58 LDM |
211 | fflush(stdout); |
212 | ||
49a16375 LDM |
213 | kmod_dump_index(ctx, KMOD_INDEX_MODULES_ALIAS, STDOUT_FILENO); |
214 | kmod_dump_index(ctx, KMOD_INDEX_MODULES_SYMBOL, STDOUT_FILENO); | |
215 | ||
bc43496a | 216 | return 0; |
c3d0a5f2 GSB |
217 | } |
218 | ||
219 | static int show_modversions(struct kmod_ctx *ctx, const char *filename) | |
220 | { | |
0e3e2f43 GSB |
221 | struct kmod_list *l, *list = NULL; |
222 | struct kmod_module *mod; | |
223 | int err = kmod_module_new_from_path(ctx, filename, &mod); | |
224 | if (err < 0) { | |
225 | LOG("Module %s not found.\n", filename); | |
226 | return err; | |
227 | } | |
228 | ||
229 | err = kmod_module_get_versions(mod, &list); | |
230 | if (err < 0) { | |
63698377 | 231 | LOG("could not get modversions of %s: %s\n", |
0e3e2f43 GSB |
232 | filename, strerror(-err)); |
233 | kmod_module_unref(mod); | |
234 | return err; | |
235 | } | |
236 | ||
237 | kmod_list_foreach(l, list) { | |
238 | const char *symbol = kmod_module_version_get_symbol(l); | |
239 | uint64_t crc = kmod_module_version_get_crc(l); | |
240 | printf("0x%08"PRIx64"\t%s\n", crc, symbol); | |
241 | } | |
242 | kmod_module_versions_free_list(list); | |
243 | kmod_module_unref(mod); | |
244 | return 0; | |
c3d0a5f2 GSB |
245 | } |
246 | ||
3ada8df8 YK |
247 | static int show_exports(struct kmod_ctx *ctx, const char *filename) |
248 | { | |
249 | struct kmod_list *l, *list = NULL; | |
250 | struct kmod_module *mod; | |
251 | int err = kmod_module_new_from_path(ctx, filename, &mod); | |
252 | if (err < 0) { | |
253 | LOG("Module %s not found.\n", filename); | |
254 | return err; | |
255 | } | |
256 | ||
257 | err = kmod_module_get_symbols(mod, &list); | |
258 | if (err < 0) { | |
259 | LOG("could not get symbols of %s: %s\n", | |
260 | filename, strerror(-err)); | |
261 | kmod_module_unref(mod); | |
262 | return err; | |
263 | } | |
264 | ||
265 | kmod_list_foreach(l, list) { | |
266 | const char *symbol = kmod_module_symbol_get_symbol(l); | |
267 | uint64_t crc = kmod_module_symbol_get_crc(l); | |
268 | printf("0x%08"PRIx64"\t%s\n", crc, symbol); | |
269 | } | |
270 | kmod_module_symbols_free_list(list); | |
271 | kmod_module_unref(mod); | |
272 | return 0; | |
273 | } | |
274 | ||
8f192210 LDM |
275 | static int command_do(struct kmod_module *module, const char *type, |
276 | const char *command, const char *cmdline_opts) | |
c3d0a5f2 GSB |
277 | { |
278 | const char *modname = kmod_module_get_name(module); | |
279 | char *p, *cmd = NULL; | |
280 | size_t cmdlen, cmdline_opts_len, varlen; | |
281 | int ret = 0; | |
282 | ||
283 | if (cmdline_opts == NULL) | |
284 | cmdline_opts = ""; | |
285 | cmdline_opts_len = strlen(cmdline_opts); | |
286 | ||
287 | cmd = strdup(command); | |
288 | if (cmd == NULL) | |
289 | return -ENOMEM; | |
290 | cmdlen = strlen(cmd); | |
291 | varlen = sizeof("$CMDLINE_OPTS") - 1; | |
292 | while ((p = strstr(cmd, "$CMDLINE_OPTS")) != NULL) { | |
293 | size_t prefixlen = p - cmd; | |
294 | size_t suffixlen = cmdlen - prefixlen - varlen; | |
295 | size_t slen = cmdlen - varlen + cmdline_opts_len; | |
296 | char *suffix = p + varlen; | |
297 | char *s = malloc(slen + 1); | |
298 | if (s == NULL) { | |
299 | free(cmd); | |
300 | return -ENOMEM; | |
301 | } | |
302 | memcpy(s, cmd, p - cmd); | |
303 | memcpy(s + prefixlen, cmdline_opts, cmdline_opts_len); | |
304 | memcpy(s + prefixlen + cmdline_opts_len, suffix, suffixlen); | |
305 | s[slen] = '\0'; | |
306 | ||
307 | free(cmd); | |
308 | cmd = s; | |
309 | cmdlen = slen; | |
310 | } | |
311 | ||
312 | SHOW("%s %s\n", type, cmd); | |
313 | if (dry_run) | |
314 | goto end; | |
315 | ||
316 | setenv("MODPROBE_MODULE", modname, 1); | |
317 | ret = system(cmd); | |
318 | unsetenv("MODPROBE_MODULE"); | |
319 | if (ret == -1 || WEXITSTATUS(ret)) { | |
320 | LOG("Error running %s command for %s\n", type, modname); | |
321 | if (ret != -1) | |
322 | ret = -WEXITSTATUS(ret); | |
323 | } | |
324 | ||
325 | end: | |
326 | free(cmd); | |
327 | return ret; | |
328 | } | |
329 | ||
a872bba2 | 330 | static int rmmod_do_remove_module(struct kmod_module *mod) |
c3d0a5f2 | 331 | { |
a872bba2 | 332 | const char *modname = kmod_module_get_name(mod); |
2b98ed88 LDM |
333 | unsigned long long interval_msec = 0, t0_msec = 0, |
334 | tend_msec = 0; | |
a872bba2 | 335 | int flags = 0, err; |
e793f1ea | 336 | |
6f7ab216 | 337 | SHOW("rmmod %s\n", modname); |
c3d0a5f2 | 338 | |
a872bba2 LDM |
339 | if (dry_run) |
340 | return 0; | |
c3d0a5f2 | 341 | |
a872bba2 LDM |
342 | if (force) |
343 | flags |= KMOD_REMOVE_FORCE; | |
c3d0a5f2 | 344 | |
2b98ed88 LDM |
345 | if (wait_msec) |
346 | flags |= KMOD_REMOVE_NOLOG; | |
347 | ||
348 | do { | |
349 | err = kmod_module_remove_module(mod, flags); | |
350 | if (err == -EEXIST) { | |
351 | if (!first_time) | |
352 | err = 0; | |
353 | else | |
354 | LOG("Module %s is not in kernel.\n", modname); | |
355 | break; | |
356 | } else if (err == -EAGAIN && wait_msec) { | |
357 | unsigned long long until_msec; | |
358 | ||
359 | if (!t0_msec) { | |
360 | t0_msec = now_msec(); | |
361 | tend_msec = t0_msec + wait_msec; | |
362 | interval_msec = 1; | |
363 | } | |
364 | ||
365 | until_msec = get_backoff_delta_msec(t0_msec, tend_msec, | |
366 | &interval_msec); | |
367 | err = sleep_until_msec(until_msec); | |
368 | ||
369 | if (!t0_msec) | |
370 | err = -ENOTSUP; | |
371 | ||
372 | if (err < 0) { | |
373 | ERR("Failed to sleep: %s\n", strerror(-err)); | |
374 | err = -EAGAIN; | |
375 | break; | |
376 | } | |
377 | } else { | |
378 | break; | |
379 | } | |
380 | } while (interval_msec); | |
381 | ||
382 | if (err < 0 && wait_msec) | |
383 | ERR("could not remove '%s': %s\n", modname, strerror(-err)); | |
e793f1ea GSB |
384 | |
385 | return err; | |
386 | } | |
387 | ||
42b32d30 | 388 | #define RMMOD_FLAG_REMOVE_HOLDERS 0x1 |
a6b540de LDM |
389 | #define RMMOD_FLAG_IGNORE_BUILTIN 0x2 |
390 | static int rmmod_do_module(struct kmod_module *mod, int flags); | |
e793f1ea | 391 | |
7089386e LDM |
392 | /* Remove modules in reverse order */ |
393 | static int rmmod_do_modlist(struct kmod_list *list, bool stop_on_errors) | |
e793f1ea | 394 | { |
a872bba2 LDM |
395 | struct kmod_list *l; |
396 | ||
397 | kmod_list_foreach_reverse(l, list) { | |
398 | struct kmod_module *m = kmod_module_get_module(l); | |
a6b540de | 399 | int r = rmmod_do_module(m, RMMOD_FLAG_IGNORE_BUILTIN); |
a872bba2 LDM |
400 | kmod_module_unref(m); |
401 | ||
402 | if (r < 0 && stop_on_errors) | |
403 | return r; | |
404 | } | |
405 | ||
406 | return 0; | |
c3d0a5f2 GSB |
407 | } |
408 | ||
a6b540de | 409 | static int rmmod_do_module(struct kmod_module *mod, int flags) |
c3d0a5f2 GSB |
410 | { |
411 | const char *modname = kmod_module_get_name(mod); | |
a872bba2 LDM |
412 | struct kmod_list *pre = NULL, *post = NULL; |
413 | const char *cmd = NULL; | |
c3d0a5f2 GSB |
414 | int err; |
415 | ||
c3d0a5f2 | 416 | if (!ignore_commands) { |
e793f1ea GSB |
417 | err = kmod_module_get_softdeps(mod, &pre, &post); |
418 | if (err < 0) { | |
419 | WRN("could not get softdeps of '%s': %s\n", | |
8f192210 | 420 | modname, strerror(-err)); |
c3d0a5f2 | 421 | return err; |
e793f1ea GSB |
422 | } |
423 | ||
c3d0a5f2 | 424 | cmd = kmod_module_get_remove_commands(mod); |
c3d0a5f2 GSB |
425 | } |
426 | ||
ea3c8adf LDM |
427 | /* Quick check if module is loaded, otherwise there's nothing to do */ |
428 | if (!cmd && !ignore_loaded) { | |
9bf60d21 LDM |
429 | int state = kmod_module_get_initstate(mod); |
430 | ||
e5e2a683 | 431 | if (state < 0) { |
9bf60d21 LDM |
432 | if (first_time) { |
433 | LOG("Module %s is not in kernel.\n", modname); | |
e4e1e64a | 434 | err = -ENOENT; |
e4e1e64a LDM |
435 | } else { |
436 | err = 0; | |
e4e1e64a | 437 | } |
f758caf5 DR |
438 | goto error; |
439 | } else if (state == KMOD_MODULE_BUILTIN) { | |
a6b540de | 440 | if (flags & RMMOD_FLAG_IGNORE_BUILTIN) { |
52a0ba82 YK |
441 | err = 0; |
442 | } else { | |
443 | LOG("Module %s is builtin.\n", modname); | |
444 | err = -ENOENT; | |
445 | } | |
f758caf5 | 446 | goto error; |
9bf60d21 LDM |
447 | } |
448 | } | |
449 | ||
ea3c8adf | 450 | /* 1. @mod's post-softdeps in reverse order */ |
7089386e | 451 | rmmod_do_modlist(post, false); |
a872bba2 | 452 | |
ea3c8adf | 453 | /* 2. Other modules holding @mod */ |
d29ed6ef | 454 | if (flags & RMMOD_FLAG_REMOVE_HOLDERS) { |
42b32d30 | 455 | struct kmod_list *holders = kmod_module_get_holders(mod); |
a872bba2 | 456 | |
42b32d30 | 457 | err = rmmod_do_modlist(holders, true); |
116e7c7f | 458 | kmod_module_unref_list(holders); |
9bf60d21 | 459 | if (err < 0) |
a872bba2 | 460 | goto error; |
9bf60d21 LDM |
461 | } |
462 | ||
ea3c8adf | 463 | /* 3. @mod itself, but check for refcnt first */ |
2b98ed88 | 464 | if (!cmd && !ignore_loaded && !wait_msec) { |
c3d0a5f2 | 465 | int usage = kmod_module_get_refcnt(mod); |
d8a6c0cc | 466 | |
c3d0a5f2 | 467 | if (usage > 0) { |
d8a6c0cc LDM |
468 | if (!quiet_inuse) |
469 | LOG("Module %s is in use.\n", modname); | |
470 | ||
e793f1ea | 471 | err = -EBUSY; |
e793f1ea GSB |
472 | goto error; |
473 | } | |
474 | } | |
475 | ||
ea3c8adf | 476 | if (!cmd) |
a872bba2 LDM |
477 | err = rmmod_do_remove_module(mod); |
478 | else | |
479 | err = command_do(mod, "remove", cmd, NULL); | |
d8a6c0cc | 480 | |
a872bba2 LDM |
481 | if (err < 0) |
482 | goto error; | |
c9a14448 | 483 | |
ea3c8adf LDM |
484 | /* 4. Other modules that became unused: errors are non-fatal */ |
485 | if (!cmd) { | |
486 | struct kmod_list *deps, *itr; | |
487 | ||
488 | deps = kmod_module_get_dependencies(mod); | |
489 | kmod_list_foreach(itr, deps) { | |
490 | struct kmod_module *dep = kmod_module_get_module(itr); | |
491 | if (kmod_module_get_refcnt(dep) == 0) | |
492 | rmmod_do_remove_module(dep); | |
493 | kmod_module_unref(dep); | |
494 | } | |
495 | kmod_module_unref_list(deps); | |
496 | } | |
497 | ||
498 | /* 5. @mod's pre-softdeps in reverse order: errors are non-fatal */ | |
7089386e | 499 | rmmod_do_modlist(pre, false); |
0e9bd2d1 | 500 | |
e793f1ea GSB |
501 | error: |
502 | kmod_module_unref_list(pre); | |
503 | kmod_module_unref_list(post); | |
a872bba2 | 504 | |
c3d0a5f2 GSB |
505 | return err; |
506 | } | |
507 | ||
569f1609 | 508 | static int rmmod(struct kmod_ctx *ctx, const char *alias) |
c3d0a5f2 GSB |
509 | { |
510 | struct kmod_list *l, *list = NULL; | |
511 | int err; | |
512 | ||
513 | err = kmod_module_new_from_lookup(ctx, alias, &list); | |
e5e2a683 | 514 | if (err < 0) |
c3d0a5f2 | 515 | return err; |
e5e2a683 | 516 | |
f758caf5 | 517 | if (list == NULL) { |
e5e2a683 | 518 | LOG("Module %s not found.\n", alias); |
f758caf5 DR |
519 | err = -ENOENT; |
520 | } | |
c3d0a5f2 GSB |
521 | |
522 | kmod_list_foreach(l, list) { | |
523 | struct kmod_module *mod = kmod_module_get_module(l); | |
d29ed6ef LDM |
524 | int flags = remove_holders ? RMMOD_FLAG_REMOVE_HOLDERS : 0; |
525 | ||
526 | err = rmmod_do_module(mod, flags); | |
c3d0a5f2 GSB |
527 | kmod_module_unref(mod); |
528 | if (err < 0) | |
529 | break; | |
530 | } | |
531 | ||
532 | kmod_module_unref_list(list); | |
533 | return err; | |
534 | } | |
535 | ||
c3d0a5f2 GSB |
536 | static int rmmod_all(struct kmod_ctx *ctx, char **args, int nargs) |
537 | { | |
538 | int i, err = 0; | |
539 | ||
540 | for (i = 0; i < nargs; i++) { | |
541 | int r = rmmod(ctx, args[i]); | |
542 | if (r < 0) | |
543 | err = r; | |
544 | } | |
545 | ||
546 | return err; | |
547 | } | |
548 | ||
2e9dcd74 LDM |
549 | static void print_action(struct kmod_module *m, bool install, |
550 | const char *options) | |
551 | { | |
4744ebce LDM |
552 | const char *path; |
553 | ||
554 | if (install) { | |
2e9dcd74 LDM |
555 | printf("install %s %s\n", kmod_module_get_install_commands(m), |
556 | options); | |
4744ebce LDM |
557 | return; |
558 | } | |
559 | ||
560 | path = kmod_module_get_path(m); | |
561 | ||
562 | if (path == NULL) { | |
ace742fa LDM |
563 | /* |
564 | * Either a builtin module, or an alias, print only for | |
565 | * builtin | |
566 | */ | |
567 | if (kmod_module_get_initstate(m) == KMOD_MODULE_BUILTIN) | |
568 | printf("builtin %s\n", kmod_module_get_name(m)); | |
4744ebce LDM |
569 | } else |
570 | printf("insmod %s %s\n", kmod_module_get_path(m), options); | |
2e9dcd74 LDM |
571 | } |
572 | ||
f3db15e9 GS |
573 | static int insmod_insert(struct kmod_module *mod, int flags, |
574 | const char *extra_options) | |
575 | { | |
576 | int err = 0; | |
577 | void (*show)(struct kmod_module *m, bool install, | |
578 | const char *options) = NULL; | |
579 | ||
580 | if (do_show || verbose > DEFAULT_VERBOSE) | |
581 | show = &print_action; | |
582 | ||
583 | if (lookup_only) | |
584 | printf("%s\n", kmod_module_get_name(mod)); | |
585 | else | |
586 | err = kmod_module_probe_insert_module(mod, flags, | |
587 | extra_options, NULL, NULL, show); | |
588 | ||
589 | if (err >= 0) | |
590 | /* ignore flag return values such as a mod being blacklisted */ | |
591 | err = 0; | |
592 | else { | |
593 | switch (err) { | |
594 | case -EEXIST: | |
595 | ERR("could not insert '%s': Module already in kernel\n", | |
596 | kmod_module_get_name(mod)); | |
597 | break; | |
598 | case -ENOENT: | |
599 | ERR("could not insert '%s': Unknown symbol in module, " | |
600 | "or unknown parameter (see dmesg)\n", | |
601 | kmod_module_get_name(mod)); | |
602 | break; | |
603 | default: | |
604 | ERR("could not insert '%s': %s\n", | |
605 | kmod_module_get_name(mod), | |
606 | strerror(-err)); | |
607 | break; | |
608 | } | |
609 | } | |
610 | ||
611 | return err; | |
612 | } | |
613 | ||
569f1609 | 614 | static int insmod(struct kmod_ctx *ctx, const char *alias, |
8f192210 | 615 | const char *extra_options) |
c3d0a5f2 GSB |
616 | { |
617 | struct kmod_list *l, *list = NULL; | |
883d931d | 618 | struct kmod_module *mod = NULL; |
2e9dcd74 LDM |
619 | int err, flags = 0; |
620 | ||
883d931d GS |
621 | if (strncmp(alias, "/", 1) == 0 || strncmp(alias, "./", 2) == 0) { |
622 | err = kmod_module_new_from_path(ctx, alias, &mod); | |
623 | if (err < 0) { | |
624 | LOG("Failed to get module from path %s: %s\n", alias, | |
625 | strerror(-err)); | |
626 | return -ENOENT; | |
627 | } | |
628 | } else { | |
629 | err = kmod_module_new_from_lookup(ctx, alias, &list); | |
630 | if (list == NULL || err < 0) { | |
631 | LOG("Module %s not found in directory %s\n", alias, | |
632 | ctx ? kmod_get_dirname(ctx) : "(missing)"); | |
633 | return -ENOENT; | |
634 | } | |
e2719b32 | 635 | } |
c3d0a5f2 | 636 | |
2e9dcd74 LDM |
637 | if (strip_modversion || force) |
638 | flags |= KMOD_PROBE_FORCE_MODVERSION; | |
639 | if (strip_vermagic || force) | |
640 | flags |= KMOD_PROBE_FORCE_VERMAGIC; | |
641 | if (ignore_commands) | |
642 | flags |= KMOD_PROBE_IGNORE_COMMAND; | |
643 | if (ignore_loaded) | |
644 | flags |= KMOD_PROBE_IGNORE_LOADED; | |
645 | if (dry_run) | |
646 | flags |= KMOD_PROBE_DRY_RUN; | |
2e9dcd74 | 647 | |
36ddee65 | 648 | flags |= KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY; |
2e9dcd74 LDM |
649 | |
650 | if (use_blacklist) | |
651 | flags |= KMOD_PROBE_APPLY_BLACKLIST; | |
652 | if (first_time) | |
814a57ba | 653 | flags |= KMOD_PROBE_FAIL_ON_LOADED; |
c3d0a5f2 | 654 | |
883d931d GS |
655 | /* If module is loaded from path */ |
656 | if (mod != NULL) { | |
f3db15e9 | 657 | err = insmod_insert(mod, flags, extra_options); |
c3d0a5f2 | 658 | kmod_module_unref(mod); |
883d931d GS |
659 | } else { |
660 | kmod_list_foreach(l, list) { | |
661 | mod = kmod_module_get_module(l); | |
662 | err = insmod_insert(mod, flags, extra_options); | |
663 | kmod_module_unref(mod); | |
664 | } | |
665 | kmod_module_unref_list(list); | |
c3d0a5f2 | 666 | } |
c3d0a5f2 GSB |
667 | return err; |
668 | } | |
669 | ||
c3d0a5f2 GSB |
670 | static int insmod_all(struct kmod_ctx *ctx, char **args, int nargs) |
671 | { | |
672 | int i, err = 0; | |
673 | ||
674 | for (i = 0; i < nargs; i++) { | |
675 | int r = insmod(ctx, args[i], NULL); | |
676 | if (r < 0) | |
677 | err = r; | |
678 | } | |
679 | ||
680 | return err; | |
681 | } | |
682 | ||
683 | static void env_modprobe_options_append(const char *value) | |
684 | { | |
685 | const char *old = getenv("MODPROBE_OPTIONS"); | |
686 | char *env; | |
687 | ||
688 | if (old == NULL) { | |
689 | setenv("MODPROBE_OPTIONS", value, 1); | |
690 | return; | |
691 | } | |
692 | ||
693 | if (asprintf(&env, "%s %s", old, value) < 0) { | |
694 | ERR("could not append value to $MODPROBE_OPTIONS\n"); | |
695 | return; | |
696 | } | |
697 | ||
698 | if (setenv("MODPROBE_OPTIONS", env, 1) < 0) | |
699 | ERR("could not setenv(MODPROBE_OPTIONS, \"%s\")\n", env); | |
700 | free(env); | |
701 | } | |
702 | ||
703 | static int options_from_array(char **args, int nargs, char **output) | |
704 | { | |
705 | char *opts = NULL; | |
706 | size_t optslen = 0; | |
707 | int i, err = 0; | |
708 | ||
709 | for (i = 1; i < nargs; i++) { | |
710 | size_t len = strlen(args[i]); | |
711 | size_t qlen = 0; | |
712 | const char *value; | |
713 | void *tmp; | |
714 | ||
715 | value = strchr(args[i], '='); | |
716 | if (value) { | |
717 | value++; | |
718 | if (*value != '"' && *value != '\'') { | |
719 | if (strchr(value, ' ')) | |
720 | qlen = 2; | |
721 | } | |
722 | } | |
723 | ||
724 | tmp = realloc(opts, optslen + len + qlen + 2); | |
725 | if (!tmp) { | |
726 | err = -errno; | |
727 | free(opts); | |
728 | opts = NULL; | |
729 | ERR("could not gather module options: out-of-memory\n"); | |
730 | break; | |
731 | } | |
732 | opts = tmp; | |
733 | if (optslen > 0) { | |
734 | opts[optslen] = ' '; | |
735 | optslen++; | |
736 | } | |
737 | if (qlen == 0) { | |
738 | memcpy(opts + optslen, args[i], len + 1); | |
739 | optslen += len; | |
740 | } else { | |
741 | size_t keylen = value - args[i]; | |
742 | size_t valuelen = len - keylen; | |
743 | memcpy(opts + optslen, args[i], keylen); | |
744 | optslen += keylen; | |
745 | opts[optslen] = '"'; | |
746 | optslen++; | |
747 | memcpy(opts + optslen, value, valuelen); | |
748 | optslen += valuelen; | |
749 | opts[optslen] = '"'; | |
750 | optslen++; | |
751 | opts[optslen] = '\0'; | |
752 | } | |
753 | } | |
754 | ||
755 | *output = opts; | |
756 | return err; | |
757 | } | |
758 | ||
759 | static char **prepend_options_from_env(int *p_argc, char **orig_argv) | |
760 | { | |
761 | const char *p, *env = getenv("MODPROBE_OPTIONS"); | |
d890179b | 762 | char **new_argv, *str_end, *str, *s, *quote; |
c3d0a5f2 GSB |
763 | int i, argc = *p_argc; |
764 | size_t envlen, space_count = 0; | |
765 | ||
766 | if (env == NULL) | |
767 | return orig_argv; | |
768 | ||
769 | for (p = env; *p != '\0'; p++) { | |
770 | if (*p == ' ') | |
771 | space_count++; | |
772 | } | |
773 | ||
774 | envlen = p - env; | |
775 | new_argv = malloc(sizeof(char *) * (argc + space_count + 3 + envlen)); | |
776 | if (new_argv == NULL) | |
777 | return NULL; | |
778 | ||
779 | new_argv[0] = orig_argv[0]; | |
d890179b | 780 | str = (char *) (new_argv + argc + space_count + 3); |
c3d0a5f2 GSB |
781 | memcpy(str, env, envlen + 1); |
782 | ||
d890179b | 783 | str_end = str + envlen; |
c3d0a5f2 GSB |
784 | |
785 | quote = NULL; | |
786 | for (i = 1, s = str; *s != '\0'; s++) { | |
787 | if (quote == NULL) { | |
788 | if (*s == ' ') { | |
789 | new_argv[i] = str; | |
790 | i++; | |
791 | *s = '\0'; | |
792 | str = s + 1; | |
793 | } else if (*s == '"' || *s == '\'') | |
794 | quote = s; | |
795 | } else { | |
796 | if (*s == *quote) { | |
797 | if (quote == str) { | |
798 | new_argv[i] = str + 1; | |
799 | i++; | |
800 | *s = '\0'; | |
801 | str = s + 1; | |
802 | } else { | |
803 | char *it; | |
804 | for (it = quote; it < s - 1; it++) | |
805 | it[0] = it[1]; | |
806 | for (it = s - 1; it < str_end - 2; it++) | |
807 | it[0] = it[2]; | |
808 | str_end -= 2; | |
809 | *str_end = '\0'; | |
810 | s -= 2; | |
811 | } | |
812 | quote = NULL; | |
813 | } | |
814 | } | |
815 | } | |
816 | if (str < s) { | |
817 | new_argv[i] = str; | |
818 | i++; | |
819 | } | |
820 | ||
821 | memcpy(new_argv + i, orig_argv + 1, sizeof(char *) * (argc - 1)); | |
757b3599 | 822 | new_argv[i + argc - 1] = NULL; |
c3d0a5f2 GSB |
823 | *p_argc = i + argc - 1; |
824 | ||
825 | return new_argv; | |
826 | } | |
827 | ||
fa29c0ee | 828 | static int do_modprobe(int argc, char **orig_argv) |
c3d0a5f2 GSB |
829 | { |
830 | struct kmod_ctx *ctx; | |
831 | char **args = NULL, **argv; | |
cb8d4d3e GSB |
832 | const char **config_paths = NULL; |
833 | int nargs = 0, n_config_paths = 0; | |
c3d0a5f2 GSB |
834 | char dirname_buf[PATH_MAX]; |
835 | const char *dirname = NULL; | |
836 | const char *root = NULL; | |
c3d0a5f2 | 837 | const char *kversion = NULL; |
c3d0a5f2 GSB |
838 | int use_all = 0; |
839 | int do_remove = 0; | |
840 | int do_show_config = 0; | |
841 | int do_show_modversions = 0; | |
3ada8df8 | 842 | int do_show_exports = 0; |
c3d0a5f2 | 843 | int err; |
c1fb98a3 | 844 | struct stat stat_buf; |
c3d0a5f2 GSB |
845 | |
846 | argv = prepend_options_from_env(&argc, orig_argv); | |
847 | if (argv == NULL) { | |
d9a2e155 | 848 | ERR("Could not prepend options from command line\n"); |
c3d0a5f2 GSB |
849 | return EXIT_FAILURE; |
850 | } | |
851 | ||
852 | for (;;) { | |
853 | int c, idx = 0; | |
854 | c = getopt_long(argc, argv, cmdopts_s, cmdopts, &idx); | |
855 | if (c == -1) | |
856 | break; | |
857 | switch (c) { | |
858 | case 'a': | |
859 | log_priority = LOG_WARNING; | |
860 | use_all = 1; | |
861 | break; | |
862 | case 'r': | |
863 | do_remove = 1; | |
864 | break; | |
865 | case 5: | |
42b32d30 | 866 | remove_holders = 1; |
c3d0a5f2 | 867 | break; |
2b98ed88 LDM |
868 | case 'w': { |
869 | char *endptr = NULL; | |
870 | wait_msec = strtoul(optarg, &endptr, 0); | |
871 | if (!*optarg || *endptr) { | |
872 | ERR("unexpected wait value '%s'.\n", optarg); | |
873 | err = -1; | |
874 | goto done; | |
875 | } | |
876 | break; | |
877 | } | |
c3d0a5f2 GSB |
878 | case 3: |
879 | first_time = 1; | |
880 | break; | |
881 | case 'i': | |
882 | ignore_commands = 1; | |
883 | break; | |
884 | case 'b': | |
885 | use_blacklist = 1; | |
886 | break; | |
887 | case 'f': | |
888 | force = 1; | |
889 | break; | |
890 | case 2: | |
891 | strip_modversion = 1; | |
892 | break; | |
893 | case 1: | |
894 | strip_vermagic = 1; | |
895 | break; | |
c3d0a5f2 GSB |
896 | case 'D': |
897 | ignore_loaded = 1; | |
898 | dry_run = 1; | |
525fa07b | 899 | do_show = 1; |
c3d0a5f2 | 900 | break; |
081fff2c LDM |
901 | case 'R': |
902 | lookup_only = 1; | |
903 | break; | |
c3d0a5f2 GSB |
904 | case 'c': |
905 | do_show_config = 1; | |
906 | break; | |
907 | case 4: | |
908 | do_show_modversions = 1; | |
909 | break; | |
3ada8df8 YK |
910 | case 6: |
911 | do_show_exports = 1; | |
912 | break; | |
c3d0a5f2 GSB |
913 | case 'n': |
914 | dry_run = 1; | |
915 | break; | |
cb8d4d3e GSB |
916 | case 'C': { |
917 | size_t bytes = sizeof(char *) * (n_config_paths + 2); | |
918 | void *tmp = realloc(config_paths, bytes); | |
919 | if (!tmp) { | |
d9a2e155 LDM |
920 | ERR("out-of-memory\n"); |
921 | err = -1; | |
922 | goto done; | |
cb8d4d3e GSB |
923 | } |
924 | config_paths = tmp; | |
925 | config_paths[n_config_paths] = optarg; | |
926 | n_config_paths++; | |
927 | config_paths[n_config_paths] = NULL; | |
928 | ||
c3d0a5f2 GSB |
929 | env_modprobe_options_append("-C"); |
930 | env_modprobe_options_append(optarg); | |
c3d0a5f2 | 931 | break; |
cb8d4d3e | 932 | } |
c3d0a5f2 GSB |
933 | case 'd': |
934 | root = optarg; | |
935 | break; | |
936 | case 'S': | |
937 | kversion = optarg; | |
938 | break; | |
939 | case 's': | |
940 | env_modprobe_options_append("-s"); | |
941 | use_syslog = 1; | |
942 | break; | |
943 | case 'q': | |
944 | env_modprobe_options_append("-q"); | |
ae7ebe87 | 945 | verbose = LOG_EMERG; |
c3d0a5f2 GSB |
946 | break; |
947 | case 'v': | |
948 | env_modprobe_options_append("-v"); | |
949 | verbose++; | |
950 | break; | |
951 | case 'V': | |
952 | puts(PACKAGE " version " VERSION); | |
655de275 | 953 | puts(KMOD_FEATURES); |
d9a2e155 | 954 | err = 0; |
4434d8ba | 955 | goto done; |
c3d0a5f2 | 956 | case 'h': |
4a2e20df | 957 | help(); |
d9a2e155 | 958 | err = 0; |
4434d8ba | 959 | goto done; |
c3d0a5f2 | 960 | case '?': |
d9a2e155 LDM |
961 | err = -1; |
962 | goto done; | |
c3d0a5f2 | 963 | default: |
d9a2e155 LDM |
964 | ERR("unexpected getopt_long() value '%c'.\n", c); |
965 | err = -1; | |
966 | goto done; | |
c3d0a5f2 GSB |
967 | } |
968 | } | |
969 | ||
970 | args = argv + optind; | |
971 | nargs = argc - optind; | |
972 | ||
c1fb98a3 QA |
973 | if (!use_syslog && |
974 | (!stderr || | |
975 | fileno(stderr) == -1 || | |
976 | fstat(fileno(stderr), &stat_buf))) | |
977 | use_syslog = 1; | |
978 | ||
92aad749 | 979 | log_open(use_syslog); |
d9a2e155 | 980 | |
b09668cf | 981 | if (!do_show_config) { |
c3d0a5f2 | 982 | if (nargs == 0) { |
d9a2e155 LDM |
983 | ERR("missing parameters. See -h.\n"); |
984 | err = -1; | |
985 | goto done; | |
c3d0a5f2 GSB |
986 | } |
987 | } | |
988 | ||
c3d0a5f2 GSB |
989 | if (root != NULL || kversion != NULL) { |
990 | struct utsname u; | |
991 | if (root == NULL) | |
992 | root = ""; | |
993 | if (kversion == NULL) { | |
994 | if (uname(&u) < 0) { | |
d9a2e155 LDM |
995 | ERR("uname() failed: %m\n"); |
996 | err = -1; | |
997 | goto done; | |
c3d0a5f2 GSB |
998 | } |
999 | kversion = u.release; | |
1000 | } | |
8f192210 | 1001 | snprintf(dirname_buf, sizeof(dirname_buf), |
c5b37dba | 1002 | "%s/lib/modules/%s", root, |
8f192210 | 1003 | kversion); |
c3d0a5f2 GSB |
1004 | dirname = dirname_buf; |
1005 | } | |
1006 | ||
cb8d4d3e | 1007 | ctx = kmod_new(dirname, config_paths); |
c3d0a5f2 | 1008 | if (!ctx) { |
d9a2e155 LDM |
1009 | ERR("kmod_new() failed!\n"); |
1010 | err = -1; | |
1011 | goto done; | |
c3d0a5f2 | 1012 | } |
c3d0a5f2 | 1013 | |
52a50fe2 | 1014 | log_setup_kmod_log(ctx, verbose); |
d9a2e155 LDM |
1015 | |
1016 | kmod_load_resources(ctx); | |
c3d0a5f2 | 1017 | |
b09668cf | 1018 | if (do_show_config) |
c3d0a5f2 GSB |
1019 | err = show_config(ctx); |
1020 | else if (do_show_modversions) | |
1021 | err = show_modversions(ctx, args[0]); | |
3ada8df8 YK |
1022 | else if (do_show_exports) |
1023 | err = show_exports(ctx, args[0]); | |
c3d0a5f2 | 1024 | else if (do_remove) |
c1b84540 | 1025 | err = rmmod_all(ctx, args, nargs); |
c3d0a5f2 GSB |
1026 | else if (use_all) |
1027 | err = insmod_all(ctx, args, nargs); | |
1028 | else { | |
1029 | char *opts; | |
1030 | err = options_from_array(args, nargs, &opts); | |
1031 | if (err == 0) { | |
1032 | err = insmod(ctx, args[0], opts); | |
1033 | free(opts); | |
1034 | } | |
1035 | } | |
1036 | ||
1037 | kmod_unref(ctx); | |
1038 | ||
d9a2e155 | 1039 | done: |
92aad749 | 1040 | log_close(); |
c3d0a5f2 GSB |
1041 | |
1042 | if (argv != orig_argv) | |
1043 | free(argv); | |
8f192210 | 1044 | |
cb8d4d3e | 1045 | free(config_paths); |
c3d0a5f2 | 1046 | |
d9a2e155 | 1047 | return err >= 0 ? EXIT_SUCCESS : EXIT_FAILURE; |
c3d0a5f2 | 1048 | } |
fa29c0ee | 1049 | |
fa29c0ee LDM |
1050 | const struct kmod_cmd kmod_cmd_compat_modprobe = { |
1051 | .name = "modprobe", | |
1052 | .cmd = do_modprobe, | |
1053 | .help = "compat modprobe command", | |
1054 | }; |