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_dependencies = 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 {"resolve-alias", no_argument, 0, 'R'},
66 {"first-time", no_argument, 0, 3},
67 {"ignore-install", no_argument, 0, 'i'},
68 {"ignore-remove", no_argument, 0, 'i'},
69 {"use-blacklist", no_argument, 0, 'b'},
70 {"force", no_argument, 0, 'f'},
71 {"force-modversion", no_argument, 0, 2},
72 {"force-vermagic", no_argument, 0, 1},
74 {"show-depends", no_argument, 0, 'D'},
75 {"showconfig", no_argument, 0, 'c'},
76 {"show-config", no_argument, 0, 'c'},
77 {"show-modversions", no_argument, 0, 4},
78 {"dump-modversions", no_argument, 0, 4},
79 {"show-exports", no_argument, 0, 6},
81 {"dry-run", no_argument, 0, 'n'},
82 {"show", no_argument, 0, 'n'},
84 {"config", required_argument, 0, 'C'},
85 {"dirname", required_argument, 0, 'd'},
86 {"set-version", required_argument, 0, 'S'},
88 {"syslog", no_argument, 0, 's'},
89 {"quiet", no_argument, 0, 'q'},
90 {"verbose", no_argument, 0, 'v'},
91 {"version", no_argument, 0, 'V'},
92 {"help", no_argument, 0, 'h'},
96 static void help(void)
99 "\t%s [options] [-i] [-b] modulename\n"
100 "\t%s [options] -a [-i] [-b] modulename [modulename...]\n"
101 "\t%s [options] -r [-i] modulename\n"
102 "\t%s [options] -r -a [-i] modulename [modulename...]\n"
103 "\t%s [options] -c\n"
104 "\t%s [options] --dump-modversions filename\n"
105 "Management Options:\n"
106 "\t-a, --all Consider every non-argument to\n"
107 "\t be a module name to be inserted\n"
108 "\t or removed (-r)\n"
109 "\t-r, --remove Remove modules instead of inserting\n"
110 "\t --remove-dependencies Also remove modules depending on it\n"
111 "\t-R, --resolve-alias Only lookup and print alias and exit\n"
112 "\t --first-time Fail if module already inserted or removed\n"
113 "\t-i, --ignore-install Ignore install commands\n"
114 "\t-i, --ignore-remove Ignore remove commands\n"
115 "\t-b, --use-blacklist Apply blacklist to resolved alias.\n"
116 "\t-f, --force Force module insertion or removal.\n"
117 "\t implies --force-modversions and\n"
118 "\t --force-vermagic\n"
119 "\t --force-modversion Ignore module's version\n"
120 "\t --force-vermagic Ignore module's version magic\n"
123 "\t-D, --show-depends Only print module dependencies and exit\n"
124 "\t-c, --showconfig Print out known configuration and exit\n"
125 "\t-c, --show-config Same as --showconfig\n"
126 "\t --show-modversions Dump module symbol version and exit\n"
127 "\t --dump-modversions Same as --show-modversions\n"
128 "\t --show-exports Only print module exported symbol versions and exit\n"
131 "\t-n, --dry-run Do not execute operations, just print out\n"
132 "\t-n, --show Same as --dry-run\n"
134 "\t-C, --config=FILE Use FILE instead of default search paths\n"
135 "\t-d, --dirname=DIR Use DIR as filesystem root for /lib/modules\n"
136 "\t-S, --set-version=VERSION Use VERSION instead of `uname -r`\n"
138 "\t-s, --syslog print to syslog, not stderr\n"
139 "\t-q, --quiet disable messages\n"
140 "\t-v, --verbose enables more messages\n"
141 "\t-V, --version show version\n"
142 "\t-h, --help show this help\n",
143 program_invocation_short_name, program_invocation_short_name,
144 program_invocation_short_name, program_invocation_short_name,
145 program_invocation_short_name, program_invocation_short_name);
148 _printf_format_(1, 2)
149 static inline void _show(const char *fmt, ...)
153 if (!do_show && verbose <= DEFAULT_VERBOSE)
157 vfprintf(stdout, fmt, args);
161 #define SHOW(...) _show(__VA_ARGS__)
163 static int show_config(struct kmod_ctx *ctx)
165 struct config_iterators {
167 struct kmod_config_iter *(*get_iter)(const struct kmod_ctx *ctx);
169 { "blacklist", kmod_config_get_blacklists },
170 { "install", kmod_config_get_install_commands },
171 { "remove", kmod_config_get_remove_commands },
172 { "alias", kmod_config_get_aliases },
173 { "options", kmod_config_get_options },
174 { "softdep", kmod_config_get_softdeps },
178 for (i = 0; i < ARRAY_SIZE(ci); i++) {
179 struct kmod_config_iter *iter = ci[i].get_iter(ctx);
184 while (kmod_config_iter_next(iter)) {
187 printf("%s %s", ci[i].name,
188 kmod_config_iter_get_key(iter));
189 val = kmod_config_iter_get_value(iter);
197 kmod_config_iter_free_iter(iter);
200 puts("\n# End of configuration files. Dumping indexes now:\n");
203 kmod_dump_index(ctx, KMOD_INDEX_MODULES_ALIAS, STDOUT_FILENO);
204 kmod_dump_index(ctx, KMOD_INDEX_MODULES_SYMBOL, STDOUT_FILENO);
209 static int show_modversions(struct kmod_ctx *ctx, const char *filename)
211 struct kmod_list *l, *list = NULL;
212 struct kmod_module *mod;
213 int err = kmod_module_new_from_path(ctx, filename, &mod);
215 LOG("Module %s not found.\n", filename);
219 err = kmod_module_get_versions(mod, &list);
221 LOG("could not get modversions of %s: %s\n",
222 filename, strerror(-err));
223 kmod_module_unref(mod);
227 kmod_list_foreach(l, list) {
228 const char *symbol = kmod_module_version_get_symbol(l);
229 uint64_t crc = kmod_module_version_get_crc(l);
230 printf("0x%08"PRIx64"\t%s\n", crc, symbol);
232 kmod_module_versions_free_list(list);
233 kmod_module_unref(mod);
237 static int show_exports(struct kmod_ctx *ctx, const char *filename)
239 struct kmod_list *l, *list = NULL;
240 struct kmod_module *mod;
241 int err = kmod_module_new_from_path(ctx, filename, &mod);
243 LOG("Module %s not found.\n", filename);
247 err = kmod_module_get_symbols(mod, &list);
249 LOG("could not get symbols of %s: %s\n",
250 filename, strerror(-err));
251 kmod_module_unref(mod);
255 kmod_list_foreach(l, list) {
256 const char *symbol = kmod_module_symbol_get_symbol(l);
257 uint64_t crc = kmod_module_symbol_get_crc(l);
258 printf("0x%08"PRIx64"\t%s\n", crc, symbol);
260 kmod_module_symbols_free_list(list);
261 kmod_module_unref(mod);
265 static int command_do(struct kmod_module *module, const char *type,
266 const char *command, const char *cmdline_opts)
268 const char *modname = kmod_module_get_name(module);
269 char *p, *cmd = NULL;
270 size_t cmdlen, cmdline_opts_len, varlen;
273 if (cmdline_opts == NULL)
275 cmdline_opts_len = strlen(cmdline_opts);
277 cmd = strdup(command);
280 cmdlen = strlen(cmd);
281 varlen = sizeof("$CMDLINE_OPTS") - 1;
282 while ((p = strstr(cmd, "$CMDLINE_OPTS")) != NULL) {
283 size_t prefixlen = p - cmd;
284 size_t suffixlen = cmdlen - prefixlen - varlen;
285 size_t slen = cmdlen - varlen + cmdline_opts_len;
286 char *suffix = p + varlen;
287 char *s = malloc(slen + 1);
292 memcpy(s, cmd, p - cmd);
293 memcpy(s + prefixlen, cmdline_opts, cmdline_opts_len);
294 memcpy(s + prefixlen + cmdline_opts_len, suffix, suffixlen);
302 SHOW("%s %s\n", type, cmd);
306 setenv("MODPROBE_MODULE", modname, 1);
308 unsetenv("MODPROBE_MODULE");
309 if (ret == -1 || WEXITSTATUS(ret)) {
310 LOG("Error running %s command for %s\n", type, modname);
312 ret = -WEXITSTATUS(ret);
320 static int rmmod_do_remove_module(struct kmod_module *mod)
322 const char *modname = kmod_module_get_name(mod);
323 struct kmod_list *deps, *itr;
326 SHOW("rmmod %s\n", kmod_module_get_name(mod));
332 flags |= KMOD_REMOVE_FORCE;
334 err = kmod_module_remove_module(mod, flags);
335 if (err == -EEXIST) {
339 LOG("Module %s is not in kernel.\n", modname);
342 deps = kmod_module_get_dependencies(mod);
344 kmod_list_foreach(itr, deps) {
345 struct kmod_module *dep = kmod_module_get_module(itr);
346 if (kmod_module_get_refcnt(dep) == 0)
347 rmmod_do_remove_module(dep);
348 kmod_module_unref(dep);
350 kmod_module_unref_list(deps);
356 #define RMMOD_FLAG_DO_DEPENDENCIES 0x1
357 #define RMMOD_FLAG_IGNORE_BUILTIN 0x2
358 static int rmmod_do_module(struct kmod_module *mod, int flags);
360 static int rmmod_do_deps_list(struct kmod_list *list, bool stop_on_errors)
364 kmod_list_foreach_reverse(l, list) {
365 struct kmod_module *m = kmod_module_get_module(l);
366 int r = rmmod_do_module(m, RMMOD_FLAG_IGNORE_BUILTIN);
367 kmod_module_unref(m);
369 if (r < 0 && stop_on_errors)
376 static int rmmod_do_module(struct kmod_module *mod, int flags)
378 const char *modname = kmod_module_get_name(mod);
379 struct kmod_list *pre = NULL, *post = NULL;
380 const char *cmd = NULL;
383 if (!ignore_commands) {
384 err = kmod_module_get_softdeps(mod, &pre, &post);
386 WRN("could not get softdeps of '%s': %s\n",
387 modname, strerror(-err));
391 cmd = kmod_module_get_remove_commands(mod);
394 if (cmd == NULL && !ignore_loaded) {
395 int state = kmod_module_get_initstate(mod);
399 LOG("Module %s is not in kernel.\n", modname);
405 } else if (state == KMOD_MODULE_BUILTIN) {
406 if (flags & RMMOD_FLAG_IGNORE_BUILTIN) {
409 LOG("Module %s is builtin.\n", modname);
416 rmmod_do_deps_list(post, false);
418 if ((flags & RMMOD_FLAG_DO_DEPENDENCIES) && remove_dependencies) {
419 struct kmod_list *deps = kmod_module_get_dependencies(mod);
421 err = rmmod_do_deps_list(deps, true);
426 if (!ignore_loaded && !cmd) {
427 int usage = kmod_module_get_refcnt(mod);
431 LOG("Module %s is in use.\n", modname);
439 err = rmmod_do_remove_module(mod);
441 err = command_do(mod, "remove", cmd, NULL);
446 rmmod_do_deps_list(pre, false);
449 kmod_module_unref_list(pre);
450 kmod_module_unref_list(post);
455 static int rmmod(struct kmod_ctx *ctx, const char *alias)
457 struct kmod_list *l, *list = NULL;
460 err = kmod_module_new_from_lookup(ctx, alias, &list);
465 LOG("Module %s not found.\n", alias);
469 kmod_list_foreach(l, list) {
470 struct kmod_module *mod = kmod_module_get_module(l);
471 err = rmmod_do_module(mod, RMMOD_FLAG_DO_DEPENDENCIES);
472 kmod_module_unref(mod);
477 kmod_module_unref_list(list);
481 static int rmmod_all(struct kmod_ctx *ctx, char **args, int nargs)
485 for (i = 0; i < nargs; i++) {
486 int r = rmmod(ctx, args[i]);
494 static void print_action(struct kmod_module *m, bool install,
500 printf("install %s %s\n", kmod_module_get_install_commands(m),
505 path = kmod_module_get_path(m);
509 * Either a builtin module, or an alias, print only for
512 if (kmod_module_get_initstate(m) == KMOD_MODULE_BUILTIN)
513 printf("builtin %s\n", kmod_module_get_name(m));
515 printf("insmod %s %s\n", kmod_module_get_path(m), options);
518 static int insmod(struct kmod_ctx *ctx, const char *alias,
519 const char *extra_options)
521 struct kmod_list *l, *list = NULL;
524 void (*show)(struct kmod_module *m, bool install,
525 const char *options) = NULL;
527 err = kmod_module_new_from_lookup(ctx, alias, &list);
529 if (list == NULL || err < 0) {
530 LOG("Module %s not found in directory %s\n", alias,
531 ctx ? kmod_get_dirname(ctx) : "(missing)");
535 if (strip_modversion || force)
536 flags |= KMOD_PROBE_FORCE_MODVERSION;
537 if (strip_vermagic || force)
538 flags |= KMOD_PROBE_FORCE_VERMAGIC;
540 flags |= KMOD_PROBE_IGNORE_COMMAND;
542 flags |= KMOD_PROBE_IGNORE_LOADED;
544 flags |= KMOD_PROBE_DRY_RUN;
545 if (do_show || verbose > DEFAULT_VERBOSE)
546 show = &print_action;
548 flags |= KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY;
551 flags |= KMOD_PROBE_APPLY_BLACKLIST;
553 flags |= KMOD_PROBE_FAIL_ON_LOADED;
555 kmod_list_foreach(l, list) {
556 struct kmod_module *mod = kmod_module_get_module(l);
559 printf("%s\n", kmod_module_get_name(mod));
561 err = kmod_module_probe_insert_module(mod, flags,
562 extra_options, NULL, NULL, show);
566 /* ignore flag return values such as a mod being blacklisted */
571 ERR("could not insert '%s': Module already in kernel\n",
572 kmod_module_get_name(mod));
575 ERR("could not insert '%s': Unknown symbol in module, "
576 "or unknown parameter (see dmesg)\n",
577 kmod_module_get_name(mod));
580 ERR("could not insert '%s': %s\n",
581 kmod_module_get_name(mod),
587 kmod_module_unref(mod);
590 kmod_module_unref_list(list);
594 static int insmod_all(struct kmod_ctx *ctx, char **args, int nargs)
598 for (i = 0; i < nargs; i++) {
599 int r = insmod(ctx, args[i], NULL);
607 static void env_modprobe_options_append(const char *value)
609 const char *old = getenv("MODPROBE_OPTIONS");
613 setenv("MODPROBE_OPTIONS", value, 1);
617 if (asprintf(&env, "%s %s", old, value) < 0) {
618 ERR("could not append value to $MODPROBE_OPTIONS\n");
622 if (setenv("MODPROBE_OPTIONS", env, 1) < 0)
623 ERR("could not setenv(MODPROBE_OPTIONS, \"%s\")\n", env);
627 static int options_from_array(char **args, int nargs, char **output)
633 for (i = 1; i < nargs; i++) {
634 size_t len = strlen(args[i]);
639 value = strchr(args[i], '=');
642 if (*value != '"' && *value != '\'') {
643 if (strchr(value, ' '))
648 tmp = realloc(opts, optslen + len + qlen + 2);
653 ERR("could not gather module options: out-of-memory\n");
662 memcpy(opts + optslen, args[i], len + 1);
665 size_t keylen = value - args[i];
666 size_t valuelen = len - keylen;
667 memcpy(opts + optslen, args[i], keylen);
671 memcpy(opts + optslen, value, valuelen);
675 opts[optslen] = '\0';
683 static char **prepend_options_from_env(int *p_argc, char **orig_argv)
685 const char *p, *env = getenv("MODPROBE_OPTIONS");
686 char **new_argv, *str_start, *str_end, *str, *s, *quote;
687 int i, argc = *p_argc;
688 size_t envlen, space_count = 0;
693 for (p = env; *p != '\0'; p++) {
699 new_argv = malloc(sizeof(char *) * (argc + space_count + 3 + envlen));
700 if (new_argv == NULL)
703 new_argv[0] = orig_argv[0];
704 str_start = str = (char *) (new_argv + argc + space_count + 3);
705 memcpy(str, env, envlen + 1);
707 str_end = str_start + envlen;
710 for (i = 1, s = str; *s != '\0'; s++) {
717 } else if (*s == '"' || *s == '\'')
722 new_argv[i] = str + 1;
728 for (it = quote; it < s - 1; it++)
730 for (it = s - 1; it < str_end - 2; it++)
745 memcpy(new_argv + i, orig_argv + 1, sizeof(char *) * (argc - 1));
746 new_argv[i + argc] = NULL;
747 *p_argc = i + argc - 1;
752 static int do_modprobe(int argc, char **orig_argv)
754 struct kmod_ctx *ctx;
755 char **args = NULL, **argv;
756 const char **config_paths = NULL;
757 int nargs = 0, n_config_paths = 0;
758 char dirname_buf[PATH_MAX];
759 const char *dirname = NULL;
760 const char *root = NULL;
761 const char *kversion = NULL;
764 int do_show_config = 0;
765 int do_show_modversions = 0;
766 int do_show_exports = 0;
769 argv = prepend_options_from_env(&argc, orig_argv);
771 ERR("Could not prepend options from command line\n");
777 c = getopt_long(argc, argv, cmdopts_s, cmdopts, &idx);
782 log_priority = LOG_WARNING;
789 remove_dependencies = 1;
807 strip_modversion = 1;
821 do_show_modversions = 1;
830 size_t bytes = sizeof(char *) * (n_config_paths + 2);
831 void *tmp = realloc(config_paths, bytes);
833 ERR("out-of-memory\n");
838 config_paths[n_config_paths] = optarg;
840 config_paths[n_config_paths] = NULL;
842 env_modprobe_options_append("-C");
843 env_modprobe_options_append(optarg);
853 env_modprobe_options_append("-s");
857 env_modprobe_options_append("-q");
861 env_modprobe_options_append("-v");
865 puts(PACKAGE " version " VERSION);
877 ERR("unexpected getopt_long() value '%c'.\n", c);
883 args = argv + optind;
884 nargs = argc - optind;
886 log_open(use_syslog);
888 if (!do_show_config) {
890 ERR("missing parameters. See -h.\n");
896 if (root != NULL || kversion != NULL) {
900 if (kversion == NULL) {
902 ERR("uname() failed: %m\n");
906 kversion = u.release;
908 snprintf(dirname_buf, sizeof(dirname_buf),
909 "%s/lib/modules/%s", root,
911 dirname = dirname_buf;
914 ctx = kmod_new(dirname, config_paths);
916 ERR("kmod_new() failed!\n");
921 log_setup_kmod_log(ctx, verbose);
923 kmod_load_resources(ctx);
926 err = show_config(ctx);
927 else if (do_show_modversions)
928 err = show_modversions(ctx, args[0]);
929 else if (do_show_exports)
930 err = show_exports(ctx, args[0]);
932 err = rmmod_all(ctx, args, nargs);
934 err = insmod_all(ctx, args, nargs);
937 err = options_from_array(args, nargs, &opts);
939 err = insmod(ctx, args[0], opts);
949 if (argv != orig_argv)
954 return err >= 0 ? EXIT_SUCCESS : EXIT_FAILURE;
957 const struct kmod_cmd kmod_cmd_compat_modprobe = {
960 .help = "compat modprobe command",