2 * kmod-modprobe - manage linux kernel modules 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/>.
30 #include <sys/types.h>
31 #include <sys/utsname.h>
34 #include <shared/array.h>
35 #include <shared/macro.h>
37 #include <libkmod/libkmod.h>
41 static int log_priority = LOG_CRIT;
42 static int use_syslog = 0;
43 #define LOG(...) log_printf(log_priority, __VA_ARGS__)
45 #define DEFAULT_VERBOSE LOG_WARNING
46 static int verbose = DEFAULT_VERBOSE;
47 static int do_show = 0;
48 static int dry_run = 0;
49 static int ignore_loaded = 0;
50 static int lookup_only = 0;
51 static int first_time = 0;
52 static int ignore_commands = 0;
53 static int use_blacklist = 0;
55 static int strip_modversion = 0;
56 static int strip_vermagic = 0;
57 static int remove_holders = 0;
58 static int quiet_inuse = 0;
60 static const char cmdopts_s[] = "arRibfDcnC:d:S:sqvVh";
61 static const struct option cmdopts[] = {
62 {"all", no_argument, 0, 'a'},
63 {"remove", no_argument, 0, 'r'},
64 {"remove-dependencies", no_argument, 0, 5},
65 {"remove-holders", no_argument, 0, 5},
66 {"resolve-alias", no_argument, 0, 'R'},
67 {"first-time", no_argument, 0, 3},
68 {"ignore-install", no_argument, 0, 'i'},
69 {"ignore-remove", no_argument, 0, 'i'},
70 {"use-blacklist", no_argument, 0, 'b'},
71 {"force", no_argument, 0, 'f'},
72 {"force-modversion", no_argument, 0, 2},
73 {"force-vermagic", no_argument, 0, 1},
75 {"show-depends", no_argument, 0, 'D'},
76 {"showconfig", no_argument, 0, 'c'},
77 {"show-config", no_argument, 0, 'c'},
78 {"show-modversions", no_argument, 0, 4},
79 {"dump-modversions", no_argument, 0, 4},
80 {"show-exports", no_argument, 0, 6},
82 {"dry-run", no_argument, 0, 'n'},
83 {"show", no_argument, 0, 'n'},
85 {"config", required_argument, 0, 'C'},
86 {"dirname", required_argument, 0, 'd'},
87 {"set-version", required_argument, 0, 'S'},
89 {"syslog", no_argument, 0, 's'},
90 {"quiet", no_argument, 0, 'q'},
91 {"verbose", no_argument, 0, 'v'},
92 {"version", no_argument, 0, 'V'},
93 {"help", no_argument, 0, 'h'},
97 static void help(void)
100 "\t%s [options] [-i] [-b] modulename\n"
101 "\t%s [options] -a [-i] [-b] modulename [modulename...]\n"
102 "\t%s [options] -r [-i] modulename\n"
103 "\t%s [options] -r -a [-i] modulename [modulename...]\n"
104 "\t%s [options] -c\n"
105 "\t%s [options] --dump-modversions filename\n"
106 "Management Options:\n"
107 "\t-a, --all Consider every non-argument to\n"
108 "\t be a module name to be inserted\n"
109 "\t or removed (-r)\n"
110 "\t-r, --remove Remove modules instead of inserting\n"
111 "\t --remove-dependencies Deprecated: use --remove-holders\n"
112 "\t --remove-holders Also remove module holders (use together with -r)\n"
113 "\t-R, --resolve-alias Only lookup and print alias and exit\n"
114 "\t --first-time Fail if module already inserted or removed\n"
115 "\t-i, --ignore-install Ignore install commands\n"
116 "\t-i, --ignore-remove Ignore remove commands\n"
117 "\t-b, --use-blacklist Apply blacklist to resolved alias.\n"
118 "\t-f, --force Force module insertion or removal.\n"
119 "\t implies --force-modversions and\n"
120 "\t --force-vermagic\n"
121 "\t --force-modversion Ignore module's version\n"
122 "\t --force-vermagic Ignore module's version magic\n"
125 "\t-D, --show-depends Only print module dependencies and exit\n"
126 "\t-c, --showconfig Print out known configuration and exit\n"
127 "\t-c, --show-config Same as --showconfig\n"
128 "\t --show-modversions Dump module symbol version and exit\n"
129 "\t --dump-modversions Same as --show-modversions\n"
130 "\t --show-exports Only print module exported symbol versions and exit\n"
133 "\t-n, --dry-run Do not execute operations, just print out\n"
134 "\t-n, --show Same as --dry-run\n"
136 "\t-C, --config=FILE Use FILE instead of default search paths\n"
137 "\t-d, --dirname=DIR Use DIR as filesystem root for /lib/modules\n"
138 "\t-S, --set-version=VERSION Use VERSION instead of `uname -r`\n"
140 "\t-s, --syslog print to syslog, not stderr\n"
141 "\t-q, --quiet disable messages\n"
142 "\t-v, --verbose enables more messages\n"
143 "\t-V, --version show version\n"
144 "\t-h, --help show this help\n",
145 program_invocation_short_name, program_invocation_short_name,
146 program_invocation_short_name, program_invocation_short_name,
147 program_invocation_short_name, program_invocation_short_name);
150 _printf_format_(1, 2)
151 static inline void _show(const char *fmt, ...)
155 if (!do_show && verbose <= DEFAULT_VERBOSE)
159 vfprintf(stdout, fmt, args);
163 #define SHOW(...) _show(__VA_ARGS__)
165 static int show_config(struct kmod_ctx *ctx)
167 struct config_iterators {
169 struct kmod_config_iter *(*get_iter)(const struct kmod_ctx *ctx);
171 { "blacklist", kmod_config_get_blacklists },
172 { "install", kmod_config_get_install_commands },
173 { "remove", kmod_config_get_remove_commands },
174 { "alias", kmod_config_get_aliases },
175 { "options", kmod_config_get_options },
176 { "softdep", kmod_config_get_softdeps },
180 for (i = 0; i < ARRAY_SIZE(ci); i++) {
181 struct kmod_config_iter *iter = ci[i].get_iter(ctx);
186 while (kmod_config_iter_next(iter)) {
189 printf("%s %s", ci[i].name,
190 kmod_config_iter_get_key(iter));
191 val = kmod_config_iter_get_value(iter);
199 kmod_config_iter_free_iter(iter);
202 puts("\n# End of configuration files. Dumping indexes now:\n");
205 kmod_dump_index(ctx, KMOD_INDEX_MODULES_ALIAS, STDOUT_FILENO);
206 kmod_dump_index(ctx, KMOD_INDEX_MODULES_SYMBOL, STDOUT_FILENO);
211 static int show_modversions(struct kmod_ctx *ctx, const char *filename)
213 struct kmod_list *l, *list = NULL;
214 struct kmod_module *mod;
215 int err = kmod_module_new_from_path(ctx, filename, &mod);
217 LOG("Module %s not found.\n", filename);
221 err = kmod_module_get_versions(mod, &list);
223 LOG("could not get modversions of %s: %s\n",
224 filename, strerror(-err));
225 kmod_module_unref(mod);
229 kmod_list_foreach(l, list) {
230 const char *symbol = kmod_module_version_get_symbol(l);
231 uint64_t crc = kmod_module_version_get_crc(l);
232 printf("0x%08"PRIx64"\t%s\n", crc, symbol);
234 kmod_module_versions_free_list(list);
235 kmod_module_unref(mod);
239 static int show_exports(struct kmod_ctx *ctx, const char *filename)
241 struct kmod_list *l, *list = NULL;
242 struct kmod_module *mod;
243 int err = kmod_module_new_from_path(ctx, filename, &mod);
245 LOG("Module %s not found.\n", filename);
249 err = kmod_module_get_symbols(mod, &list);
251 LOG("could not get symbols of %s: %s\n",
252 filename, strerror(-err));
253 kmod_module_unref(mod);
257 kmod_list_foreach(l, list) {
258 const char *symbol = kmod_module_symbol_get_symbol(l);
259 uint64_t crc = kmod_module_symbol_get_crc(l);
260 printf("0x%08"PRIx64"\t%s\n", crc, symbol);
262 kmod_module_symbols_free_list(list);
263 kmod_module_unref(mod);
267 static int command_do(struct kmod_module *module, const char *type,
268 const char *command, const char *cmdline_opts)
270 const char *modname = kmod_module_get_name(module);
271 char *p, *cmd = NULL;
272 size_t cmdlen, cmdline_opts_len, varlen;
275 if (cmdline_opts == NULL)
277 cmdline_opts_len = strlen(cmdline_opts);
279 cmd = strdup(command);
282 cmdlen = strlen(cmd);
283 varlen = sizeof("$CMDLINE_OPTS") - 1;
284 while ((p = strstr(cmd, "$CMDLINE_OPTS")) != NULL) {
285 size_t prefixlen = p - cmd;
286 size_t suffixlen = cmdlen - prefixlen - varlen;
287 size_t slen = cmdlen - varlen + cmdline_opts_len;
288 char *suffix = p + varlen;
289 char *s = malloc(slen + 1);
294 memcpy(s, cmd, p - cmd);
295 memcpy(s + prefixlen, cmdline_opts, cmdline_opts_len);
296 memcpy(s + prefixlen + cmdline_opts_len, suffix, suffixlen);
304 SHOW("%s %s\n", type, cmd);
308 setenv("MODPROBE_MODULE", modname, 1);
310 unsetenv("MODPROBE_MODULE");
311 if (ret == -1 || WEXITSTATUS(ret)) {
312 LOG("Error running %s command for %s\n", type, modname);
314 ret = -WEXITSTATUS(ret);
322 static int rmmod_do_remove_module(struct kmod_module *mod)
324 const char *modname = kmod_module_get_name(mod);
325 struct kmod_list *deps, *itr;
328 SHOW("rmmod %s\n", kmod_module_get_name(mod));
334 flags |= KMOD_REMOVE_FORCE;
336 err = kmod_module_remove_module(mod, flags);
337 if (err == -EEXIST) {
341 LOG("Module %s is not in kernel.\n", modname);
344 deps = kmod_module_get_dependencies(mod);
346 kmod_list_foreach(itr, deps) {
347 struct kmod_module *dep = kmod_module_get_module(itr);
348 if (kmod_module_get_refcnt(dep) == 0)
349 rmmod_do_remove_module(dep);
350 kmod_module_unref(dep);
352 kmod_module_unref_list(deps);
358 #define RMMOD_FLAG_REMOVE_HOLDERS 0x1
359 #define RMMOD_FLAG_IGNORE_BUILTIN 0x2
360 static int rmmod_do_module(struct kmod_module *mod, int flags);
362 /* Remove modules in reverse order */
363 static int rmmod_do_modlist(struct kmod_list *list, bool stop_on_errors)
367 kmod_list_foreach_reverse(l, list) {
368 struct kmod_module *m = kmod_module_get_module(l);
369 int r = rmmod_do_module(m, RMMOD_FLAG_IGNORE_BUILTIN);
370 kmod_module_unref(m);
372 if (r < 0 && stop_on_errors)
379 static int rmmod_do_module(struct kmod_module *mod, int flags)
381 const char *modname = kmod_module_get_name(mod);
382 struct kmod_list *pre = NULL, *post = NULL;
383 const char *cmd = NULL;
386 if (!ignore_commands) {
387 err = kmod_module_get_softdeps(mod, &pre, &post);
389 WRN("could not get softdeps of '%s': %s\n",
390 modname, strerror(-err));
394 cmd = kmod_module_get_remove_commands(mod);
397 if (cmd == NULL && !ignore_loaded) {
398 int state = kmod_module_get_initstate(mod);
402 LOG("Module %s is not in kernel.\n", modname);
408 } else if (state == KMOD_MODULE_BUILTIN) {
409 if (flags & RMMOD_FLAG_IGNORE_BUILTIN) {
412 LOG("Module %s is builtin.\n", modname);
419 rmmod_do_modlist(post, false);
421 if ((flags & RMMOD_FLAG_REMOVE_HOLDERS) && remove_holders) {
422 struct kmod_list *holders = kmod_module_get_holders(mod);
424 err = rmmod_do_modlist(holders, true);
429 if (!ignore_loaded && !cmd) {
430 int usage = kmod_module_get_refcnt(mod);
434 LOG("Module %s is in use.\n", modname);
442 err = rmmod_do_remove_module(mod);
444 err = command_do(mod, "remove", cmd, NULL);
449 rmmod_do_modlist(pre, false);
452 kmod_module_unref_list(pre);
453 kmod_module_unref_list(post);
458 static int rmmod(struct kmod_ctx *ctx, const char *alias)
460 struct kmod_list *l, *list = NULL;
463 err = kmod_module_new_from_lookup(ctx, alias, &list);
468 LOG("Module %s not found.\n", alias);
472 kmod_list_foreach(l, list) {
473 struct kmod_module *mod = kmod_module_get_module(l);
474 err = rmmod_do_module(mod, RMMOD_FLAG_REMOVE_HOLDERS);
475 kmod_module_unref(mod);
480 kmod_module_unref_list(list);
484 static int rmmod_all(struct kmod_ctx *ctx, char **args, int nargs)
488 for (i = 0; i < nargs; i++) {
489 int r = rmmod(ctx, args[i]);
497 static void print_action(struct kmod_module *m, bool install,
503 printf("install %s %s\n", kmod_module_get_install_commands(m),
508 path = kmod_module_get_path(m);
512 * Either a builtin module, or an alias, print only for
515 if (kmod_module_get_initstate(m) == KMOD_MODULE_BUILTIN)
516 printf("builtin %s\n", kmod_module_get_name(m));
518 printf("insmod %s %s\n", kmod_module_get_path(m), options);
521 static int insmod(struct kmod_ctx *ctx, const char *alias,
522 const char *extra_options)
524 struct kmod_list *l, *list = NULL;
527 void (*show)(struct kmod_module *m, bool install,
528 const char *options) = NULL;
530 err = kmod_module_new_from_lookup(ctx, alias, &list);
532 if (list == NULL || err < 0) {
533 LOG("Module %s not found in directory %s\n", alias,
534 ctx ? kmod_get_dirname(ctx) : "(missing)");
538 if (strip_modversion || force)
539 flags |= KMOD_PROBE_FORCE_MODVERSION;
540 if (strip_vermagic || force)
541 flags |= KMOD_PROBE_FORCE_VERMAGIC;
543 flags |= KMOD_PROBE_IGNORE_COMMAND;
545 flags |= KMOD_PROBE_IGNORE_LOADED;
547 flags |= KMOD_PROBE_DRY_RUN;
548 if (do_show || verbose > DEFAULT_VERBOSE)
549 show = &print_action;
551 flags |= KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY;
554 flags |= KMOD_PROBE_APPLY_BLACKLIST;
556 flags |= KMOD_PROBE_FAIL_ON_LOADED;
558 kmod_list_foreach(l, list) {
559 struct kmod_module *mod = kmod_module_get_module(l);
562 printf("%s\n", kmod_module_get_name(mod));
564 err = kmod_module_probe_insert_module(mod, flags,
565 extra_options, NULL, NULL, show);
569 /* ignore flag return values such as a mod being blacklisted */
574 ERR("could not insert '%s': Module already in kernel\n",
575 kmod_module_get_name(mod));
578 ERR("could not insert '%s': Unknown symbol in module, "
579 "or unknown parameter (see dmesg)\n",
580 kmod_module_get_name(mod));
583 ERR("could not insert '%s': %s\n",
584 kmod_module_get_name(mod),
590 kmod_module_unref(mod);
593 kmod_module_unref_list(list);
597 static int insmod_all(struct kmod_ctx *ctx, char **args, int nargs)
601 for (i = 0; i < nargs; i++) {
602 int r = insmod(ctx, args[i], NULL);
610 static void env_modprobe_options_append(const char *value)
612 const char *old = getenv("MODPROBE_OPTIONS");
616 setenv("MODPROBE_OPTIONS", value, 1);
620 if (asprintf(&env, "%s %s", old, value) < 0) {
621 ERR("could not append value to $MODPROBE_OPTIONS\n");
625 if (setenv("MODPROBE_OPTIONS", env, 1) < 0)
626 ERR("could not setenv(MODPROBE_OPTIONS, \"%s\")\n", env);
630 static int options_from_array(char **args, int nargs, char **output)
636 for (i = 1; i < nargs; i++) {
637 size_t len = strlen(args[i]);
642 value = strchr(args[i], '=');
645 if (*value != '"' && *value != '\'') {
646 if (strchr(value, ' '))
651 tmp = realloc(opts, optslen + len + qlen + 2);
656 ERR("could not gather module options: out-of-memory\n");
665 memcpy(opts + optslen, args[i], len + 1);
668 size_t keylen = value - args[i];
669 size_t valuelen = len - keylen;
670 memcpy(opts + optslen, args[i], keylen);
674 memcpy(opts + optslen, value, valuelen);
678 opts[optslen] = '\0';
686 static char **prepend_options_from_env(int *p_argc, char **orig_argv)
688 const char *p, *env = getenv("MODPROBE_OPTIONS");
689 char **new_argv, *str_end, *str, *s, *quote;
690 int i, argc = *p_argc;
691 size_t envlen, space_count = 0;
696 for (p = env; *p != '\0'; p++) {
702 new_argv = malloc(sizeof(char *) * (argc + space_count + 3 + envlen));
703 if (new_argv == NULL)
706 new_argv[0] = orig_argv[0];
707 str = (char *) (new_argv + argc + space_count + 3);
708 memcpy(str, env, envlen + 1);
710 str_end = str + envlen;
713 for (i = 1, s = str; *s != '\0'; s++) {
720 } else if (*s == '"' || *s == '\'')
725 new_argv[i] = str + 1;
731 for (it = quote; it < s - 1; it++)
733 for (it = s - 1; it < str_end - 2; it++)
748 memcpy(new_argv + i, orig_argv + 1, sizeof(char *) * (argc - 1));
749 new_argv[i + argc - 1] = NULL;
750 *p_argc = i + argc - 1;
755 static int do_modprobe(int argc, char **orig_argv)
757 struct kmod_ctx *ctx;
758 char **args = NULL, **argv;
759 const char **config_paths = NULL;
760 int nargs = 0, n_config_paths = 0;
761 char dirname_buf[PATH_MAX];
762 const char *dirname = NULL;
763 const char *root = NULL;
764 const char *kversion = NULL;
767 int do_show_config = 0;
768 int do_show_modversions = 0;
769 int do_show_exports = 0;
772 argv = prepend_options_from_env(&argc, orig_argv);
774 ERR("Could not prepend options from command line\n");
780 c = getopt_long(argc, argv, cmdopts_s, cmdopts, &idx);
785 log_priority = LOG_WARNING;
810 strip_modversion = 1;
824 do_show_modversions = 1;
833 size_t bytes = sizeof(char *) * (n_config_paths + 2);
834 void *tmp = realloc(config_paths, bytes);
836 ERR("out-of-memory\n");
841 config_paths[n_config_paths] = optarg;
843 config_paths[n_config_paths] = NULL;
845 env_modprobe_options_append("-C");
846 env_modprobe_options_append(optarg);
856 env_modprobe_options_append("-s");
860 env_modprobe_options_append("-q");
864 env_modprobe_options_append("-v");
868 puts(PACKAGE " version " VERSION);
880 ERR("unexpected getopt_long() value '%c'.\n", c);
886 args = argv + optind;
887 nargs = argc - optind;
889 log_open(use_syslog);
891 if (!do_show_config) {
893 ERR("missing parameters. See -h.\n");
899 if (root != NULL || kversion != NULL) {
903 if (kversion == NULL) {
905 ERR("uname() failed: %m\n");
909 kversion = u.release;
911 snprintf(dirname_buf, sizeof(dirname_buf),
912 "%s/lib/modules/%s", root,
914 dirname = dirname_buf;
917 ctx = kmod_new(dirname, config_paths);
919 ERR("kmod_new() failed!\n");
924 log_setup_kmod_log(ctx, verbose);
926 kmod_load_resources(ctx);
929 err = show_config(ctx);
930 else if (do_show_modversions)
931 err = show_modversions(ctx, args[0]);
932 else if (do_show_exports)
933 err = show_exports(ctx, args[0]);
935 err = rmmod_all(ctx, args, nargs);
937 err = insmod_all(ctx, args, nargs);
940 err = options_from_array(args, nargs, &opts);
942 err = insmod(ctx, args[0], opts);
952 if (argv != orig_argv)
957 return err >= 0 ? EXIT_SUCCESS : EXIT_FAILURE;
960 const struct kmod_cmd kmod_cmd_compat_modprobe = {
963 .help = "compat modprobe command",