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>
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},
80 {"dry-run", no_argument
, 0, 'n'},
81 {"show", no_argument
, 0, 'n'},
83 {"config", required_argument
, 0, 'C'},
84 {"dirname", required_argument
, 0, 'd'},
85 {"set-version", required_argument
, 0, 'S'},
87 {"syslog", no_argument
, 0, 's'},
88 {"quiet", no_argument
, 0, 'q'},
89 {"verbose", no_argument
, 0, 'v'},
90 {"version", no_argument
, 0, 'V'},
91 {"help", no_argument
, 0, 'h'},
95 static void help(void)
98 "\t%s [options] [-i] [-b] modulename\n"
99 "\t%s [options] -a [-i] [-b] modulename [modulename...]\n"
100 "\t%s [options] -r [-i] modulename\n"
101 "\t%s [options] -r -a [-i] modulename [modulename...]\n"
102 "\t%s [options] -c\n"
103 "\t%s [options] --dump-modversions filename\n"
104 "Management Options:\n"
105 "\t-a, --all Consider every non-argument to\n"
106 "\t be a module name to be inserted\n"
107 "\t or removed (-r)\n"
108 "\t-r, --remove Remove modules instead of inserting\n"
109 "\t --remove-dependencies Also remove modules depending on it\n"
110 "\t-R, --resolve-alias Only lookup and print alias and exit\n"
111 "\t --first-time Fail if module already inserted or removed\n"
112 "\t-i, --ignore-install Ignore install commands\n"
113 "\t-i, --ignore-remove Ignore remove commands\n"
114 "\t-b, --use-blacklist Apply blacklist to resolved alias.\n"
115 "\t-f, --force Force module insertion or removal.\n"
116 "\t implies --force-modversions and\n"
117 "\t --force-vermagic\n"
118 "\t --force-modversion Ignore module's version\n"
119 "\t --force-vermagic Ignore module's version magic\n"
122 "\t-D, --show-depends Only print module dependencies and exit\n"
123 "\t-c, --showconfig Print out known configuration and exit\n"
124 "\t-c, --show-config Same as --showconfig\n"
125 "\t --show-modversions Dump module symbol version and exit\n"
126 "\t --dump-modversions Same as --show-modversions\n"
129 "\t-n, --dry-run Do not execute operations, just print out\n"
130 "\t-n, --show Same as --dry-run\n"
132 "\t-C, --config=FILE Use FILE instead of default search paths\n"
133 "\t-d, --dirname=DIR Use DIR as filesystem root for /lib/modules\n"
134 "\t-S, --set-version=VERSION Use VERSION instead of `uname -r`\n"
136 "\t-s, --syslog print to syslog, not stderr\n"
137 "\t-q, --quiet disable messages\n"
138 "\t-v, --verbose enables more messages\n"
139 "\t-V, --version show version\n"
140 "\t-h, --help show this help\n",
141 program_invocation_short_name
, program_invocation_short_name
,
142 program_invocation_short_name
, program_invocation_short_name
,
143 program_invocation_short_name
, program_invocation_short_name
);
146 _printf_format_(1, 2)
147 static inline void _show(const char *fmt
, ...)
151 if (!do_show
&& verbose
<= DEFAULT_VERBOSE
)
155 vfprintf(stdout
, fmt
, args
);
159 #define SHOW(...) _show(__VA_ARGS__)
161 static int show_config(struct kmod_ctx
*ctx
)
163 struct config_iterators
{
165 struct kmod_config_iter
*(*get_iter
)(const struct kmod_ctx
*ctx
);
167 { "blacklist", kmod_config_get_blacklists
},
168 { "install", kmod_config_get_install_commands
},
169 { "remove", kmod_config_get_remove_commands
},
170 { "alias", kmod_config_get_aliases
},
171 { "options", kmod_config_get_options
},
172 { "softdep", kmod_config_get_softdeps
},
176 for (i
= 0; i
< ARRAY_SIZE(ci
); i
++) {
177 struct kmod_config_iter
*iter
= ci
[i
].get_iter(ctx
);
182 while (kmod_config_iter_next(iter
)) {
185 printf("%s %s", ci
[i
].name
,
186 kmod_config_iter_get_key(iter
));
187 val
= kmod_config_iter_get_value(iter
);
195 kmod_config_iter_free_iter(iter
);
198 puts("\n# End of configuration files. Dumping indexes now:\n");
201 kmod_dump_index(ctx
, KMOD_INDEX_MODULES_ALIAS
, STDOUT_FILENO
);
202 kmod_dump_index(ctx
, KMOD_INDEX_MODULES_SYMBOL
, STDOUT_FILENO
);
207 static int show_modversions(struct kmod_ctx
*ctx
, const char *filename
)
209 struct kmod_list
*l
, *list
= NULL
;
210 struct kmod_module
*mod
;
211 int err
= kmod_module_new_from_path(ctx
, filename
, &mod
);
213 LOG("Module %s not found.\n", filename
);
217 err
= kmod_module_get_versions(mod
, &list
);
219 LOG("could not get modversions of %s: %s\n",
220 filename
, strerror(-err
));
221 kmod_module_unref(mod
);
225 kmod_list_foreach(l
, list
) {
226 const char *symbol
= kmod_module_version_get_symbol(l
);
227 uint64_t crc
= kmod_module_version_get_crc(l
);
228 printf("0x%08"PRIx64
"\t%s\n", crc
, symbol
);
230 kmod_module_versions_free_list(list
);
231 kmod_module_unref(mod
);
235 static int command_do(struct kmod_module
*module
, const char *type
,
236 const char *command
, const char *cmdline_opts
)
238 const char *modname
= kmod_module_get_name(module
);
239 char *p
, *cmd
= NULL
;
240 size_t cmdlen
, cmdline_opts_len
, varlen
;
243 if (cmdline_opts
== NULL
)
245 cmdline_opts_len
= strlen(cmdline_opts
);
247 cmd
= strdup(command
);
250 cmdlen
= strlen(cmd
);
251 varlen
= sizeof("$CMDLINE_OPTS") - 1;
252 while ((p
= strstr(cmd
, "$CMDLINE_OPTS")) != NULL
) {
253 size_t prefixlen
= p
- cmd
;
254 size_t suffixlen
= cmdlen
- prefixlen
- varlen
;
255 size_t slen
= cmdlen
- varlen
+ cmdline_opts_len
;
256 char *suffix
= p
+ varlen
;
257 char *s
= malloc(slen
+ 1);
262 memcpy(s
, cmd
, p
- cmd
);
263 memcpy(s
+ prefixlen
, cmdline_opts
, cmdline_opts_len
);
264 memcpy(s
+ prefixlen
+ cmdline_opts_len
, suffix
, suffixlen
);
272 SHOW("%s %s\n", type
, cmd
);
276 setenv("MODPROBE_MODULE", modname
, 1);
278 unsetenv("MODPROBE_MODULE");
279 if (ret
== -1 || WEXITSTATUS(ret
)) {
280 LOG("Error running %s command for %s\n", type
, modname
);
282 ret
= -WEXITSTATUS(ret
);
290 static int rmmod_do_remove_module(struct kmod_module
*mod
)
292 const char *modname
= kmod_module_get_name(mod
);
293 struct kmod_list
*deps
, *itr
;
296 SHOW("rmmod %s\n", kmod_module_get_name(mod
));
302 flags
|= KMOD_REMOVE_FORCE
;
304 err
= kmod_module_remove_module(mod
, flags
);
305 if (err
== -EEXIST
) {
309 LOG("Module %s is not in kernel.\n", modname
);
312 deps
= kmod_module_get_dependencies(mod
);
314 kmod_list_foreach(itr
, deps
) {
315 struct kmod_module
*dep
= kmod_module_get_module(itr
);
316 if (kmod_module_get_refcnt(dep
) == 0)
317 rmmod_do_remove_module(dep
);
318 kmod_module_unref(dep
);
320 kmod_module_unref_list(deps
);
326 static int rmmod_do_module(struct kmod_module
*mod
, bool do_dependencies
);
328 static int rmmod_do_deps_list(struct kmod_list
*list
, bool stop_on_errors
)
332 kmod_list_foreach_reverse(l
, list
) {
333 struct kmod_module
*m
= kmod_module_get_module(l
);
334 int r
= rmmod_do_module(m
, false);
335 kmod_module_unref(m
);
337 if (r
< 0 && stop_on_errors
)
344 static int rmmod_do_module(struct kmod_module
*mod
, bool do_dependencies
)
346 const char *modname
= kmod_module_get_name(mod
);
347 struct kmod_list
*pre
= NULL
, *post
= NULL
;
348 const char *cmd
= NULL
;
351 if (!ignore_commands
) {
352 err
= kmod_module_get_softdeps(mod
, &pre
, &post
);
354 WRN("could not get softdeps of '%s': %s\n",
355 modname
, strerror(-err
));
359 cmd
= kmod_module_get_remove_commands(mod
);
362 if (cmd
== NULL
&& !ignore_loaded
) {
363 int state
= kmod_module_get_initstate(mod
);
367 LOG("Module %s is not in kernel.\n", modname
);
373 } else if (state
== KMOD_MODULE_BUILTIN
) {
374 LOG("Module %s is builtin.\n", modname
);
380 rmmod_do_deps_list(post
, false);
382 if (do_dependencies
&& remove_dependencies
) {
383 struct kmod_list
*deps
= kmod_module_get_dependencies(mod
);
385 err
= rmmod_do_deps_list(deps
, true);
390 if (!ignore_loaded
&& !cmd
) {
391 int usage
= kmod_module_get_refcnt(mod
);
395 LOG("Module %s is in use.\n", modname
);
403 err
= rmmod_do_remove_module(mod
);
405 err
= command_do(mod
, "remove", cmd
, NULL
);
410 rmmod_do_deps_list(pre
, false);
413 kmod_module_unref_list(pre
);
414 kmod_module_unref_list(post
);
419 static int rmmod(struct kmod_ctx
*ctx
, const char *alias
)
421 struct kmod_list
*l
, *list
= NULL
;
424 err
= kmod_module_new_from_lookup(ctx
, alias
, &list
);
429 LOG("Module %s not found.\n", alias
);
433 kmod_list_foreach(l
, list
) {
434 struct kmod_module
*mod
= kmod_module_get_module(l
);
435 err
= rmmod_do_module(mod
, true);
436 kmod_module_unref(mod
);
441 kmod_module_unref_list(list
);
445 static int rmmod_all(struct kmod_ctx
*ctx
, char **args
, int nargs
)
449 for (i
= 0; i
< nargs
; i
++) {
450 int r
= rmmod(ctx
, args
[i
]);
458 static int handle_failed_lookup(struct kmod_ctx
*ctx
, const char *alias
)
460 struct kmod_module
*mod
;
463 DBG("lookup failed - trying to check if it's builtin\n");
465 err
= kmod_module_new_from_name(ctx
, alias
, &mod
);
469 state
= kmod_module_get_initstate(mod
);
470 kmod_module_unref(mod
);
472 if (state
!= KMOD_MODULE_BUILTIN
) {
473 LOG("Module %s not found.\n", alias
);
478 LOG("Module %s already in kernel (builtin).\n", alias
);
482 SHOW("builtin %s\n", alias
);
486 static void print_action(struct kmod_module
*m
, bool install
,
492 printf("install %s %s\n", kmod_module_get_install_commands(m
),
497 path
= kmod_module_get_path(m
);
501 * Either a builtin module, or an alias, print only for
504 if (kmod_module_get_initstate(m
) == KMOD_MODULE_BUILTIN
)
505 printf("builtin %s\n", kmod_module_get_name(m
));
507 printf("insmod %s %s\n", kmod_module_get_path(m
), options
);
510 static int insmod(struct kmod_ctx
*ctx
, const char *alias
,
511 const char *extra_options
)
513 struct kmod_list
*l
, *list
= NULL
;
516 void (*show
)(struct kmod_module
*m
, bool install
,
517 const char *options
) = NULL
;
519 err
= kmod_module_new_from_lookup(ctx
, alias
, &list
);
524 return handle_failed_lookup(ctx
, alias
);
526 if (strip_modversion
|| force
)
527 flags
|= KMOD_PROBE_FORCE_MODVERSION
;
528 if (strip_vermagic
|| force
)
529 flags
|= KMOD_PROBE_FORCE_VERMAGIC
;
531 flags
|= KMOD_PROBE_IGNORE_COMMAND
;
533 flags
|= KMOD_PROBE_IGNORE_LOADED
;
535 flags
|= KMOD_PROBE_DRY_RUN
;
536 if (do_show
|| verbose
> DEFAULT_VERBOSE
)
537 show
= &print_action
;
539 flags
|= KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY
;
542 flags
|= KMOD_PROBE_APPLY_BLACKLIST
;
544 flags
|= KMOD_PROBE_FAIL_ON_LOADED
;
546 kmod_list_foreach(l
, list
) {
547 struct kmod_module
*mod
= kmod_module_get_module(l
);
550 printf("%s\n", kmod_module_get_name(mod
));
552 err
= kmod_module_probe_insert_module(mod
, flags
,
553 extra_options
, NULL
, NULL
, show
);
557 /* ignore flag return values such as a mod being blacklisted */
562 ERR("could not insert '%s': Module already in kernel\n",
563 kmod_module_get_name(mod
));
566 ERR("could not insert '%s': Unknown symbol in module, "
567 "or unknown parameter (see dmesg)\n",
568 kmod_module_get_name(mod
));
571 ERR("could not insert '%s': %s\n",
572 kmod_module_get_name(mod
),
578 kmod_module_unref(mod
);
581 kmod_module_unref_list(list
);
585 static int insmod_all(struct kmod_ctx
*ctx
, char **args
, int nargs
)
589 for (i
= 0; i
< nargs
; i
++) {
590 int r
= insmod(ctx
, args
[i
], NULL
);
598 static void env_modprobe_options_append(const char *value
)
600 const char *old
= getenv("MODPROBE_OPTIONS");
604 setenv("MODPROBE_OPTIONS", value
, 1);
608 if (asprintf(&env
, "%s %s", old
, value
) < 0) {
609 ERR("could not append value to $MODPROBE_OPTIONS\n");
613 if (setenv("MODPROBE_OPTIONS", env
, 1) < 0)
614 ERR("could not setenv(MODPROBE_OPTIONS, \"%s\")\n", env
);
618 static int options_from_array(char **args
, int nargs
, char **output
)
624 for (i
= 1; i
< nargs
; i
++) {
625 size_t len
= strlen(args
[i
]);
630 value
= strchr(args
[i
], '=');
633 if (*value
!= '"' && *value
!= '\'') {
634 if (strchr(value
, ' '))
639 tmp
= realloc(opts
, optslen
+ len
+ qlen
+ 2);
644 ERR("could not gather module options: out-of-memory\n");
653 memcpy(opts
+ optslen
, args
[i
], len
+ 1);
656 size_t keylen
= value
- args
[i
];
657 size_t valuelen
= len
- keylen
;
658 memcpy(opts
+ optslen
, args
[i
], keylen
);
662 memcpy(opts
+ optslen
, value
, valuelen
);
666 opts
[optslen
] = '\0';
674 static char **prepend_options_from_env(int *p_argc
, char **orig_argv
)
676 const char *p
, *env
= getenv("MODPROBE_OPTIONS");
677 char **new_argv
, *str_start
, *str_end
, *str
, *s
, *quote
;
678 int i
, argc
= *p_argc
;
679 size_t envlen
, space_count
= 0;
684 for (p
= env
; *p
!= '\0'; p
++) {
690 new_argv
= malloc(sizeof(char *) * (argc
+ space_count
+ 3 + envlen
));
691 if (new_argv
== NULL
)
694 new_argv
[0] = orig_argv
[0];
695 str_start
= str
= (char *) (new_argv
+ argc
+ space_count
+ 3);
696 memcpy(str
, env
, envlen
+ 1);
698 str_end
= str_start
+ envlen
;
701 for (i
= 1, s
= str
; *s
!= '\0'; s
++) {
708 } else if (*s
== '"' || *s
== '\'')
713 new_argv
[i
] = str
+ 1;
719 for (it
= quote
; it
< s
- 1; it
++)
721 for (it
= s
- 1; it
< str_end
- 2; it
++)
736 memcpy(new_argv
+ i
, orig_argv
+ 1, sizeof(char *) * (argc
- 1));
737 new_argv
[i
+ argc
] = NULL
;
738 *p_argc
= i
+ argc
- 1;
743 static int do_modprobe(int argc
, char **orig_argv
)
745 struct kmod_ctx
*ctx
;
746 char **args
= NULL
, **argv
;
747 const char **config_paths
= NULL
;
748 int nargs
= 0, n_config_paths
= 0;
749 char dirname_buf
[PATH_MAX
];
750 const char *dirname
= NULL
;
751 const char *root
= NULL
;
752 const char *kversion
= NULL
;
755 int do_show_config
= 0;
756 int do_show_modversions
= 0;
759 argv
= prepend_options_from_env(&argc
, orig_argv
);
761 ERR("Could not prepend options from command line\n");
767 c
= getopt_long(argc
, argv
, cmdopts_s
, cmdopts
, &idx
);
772 log_priority
= LOG_WARNING
;
779 remove_dependencies
= 1;
797 strip_modversion
= 1;
811 do_show_modversions
= 1;
817 size_t bytes
= sizeof(char *) * (n_config_paths
+ 2);
818 void *tmp
= realloc(config_paths
, bytes
);
820 ERR("out-of-memory\n");
825 config_paths
[n_config_paths
] = optarg
;
827 config_paths
[n_config_paths
] = NULL
;
829 env_modprobe_options_append("-C");
830 env_modprobe_options_append(optarg
);
840 env_modprobe_options_append("-s");
844 env_modprobe_options_append("-q");
848 env_modprobe_options_append("-v");
852 puts(PACKAGE
" version " VERSION
);
863 ERR("unexpected getopt_long() value '%c'.\n", c
);
869 args
= argv
+ optind
;
870 nargs
= argc
- optind
;
872 log_open(use_syslog
);
874 if (!do_show_config
) {
876 ERR("missing parameters. See -h.\n");
882 if (root
!= NULL
|| kversion
!= NULL
) {
886 if (kversion
== NULL
) {
888 ERR("uname() failed: %m\n");
892 kversion
= u
.release
;
894 snprintf(dirname_buf
, sizeof(dirname_buf
),
895 "%s/lib/modules/%s", root
,
897 dirname
= dirname_buf
;
900 ctx
= kmod_new(dirname
, config_paths
);
902 ERR("kmod_new() failed!\n");
907 log_setup_kmod_log(ctx
, verbose
);
909 kmod_load_resources(ctx
);
912 err
= show_config(ctx
);
913 else if (do_show_modversions
)
914 err
= show_modversions(ctx
, args
[0]);
916 err
= rmmod_all(ctx
, args
, nargs
);
918 err
= insmod_all(ctx
, args
, nargs
);
921 err
= options_from_array(args
, nargs
, &opts
);
923 err
= insmod(ctx
, args
[0], opts
);
933 if (argv
!= orig_argv
)
938 return err
>= 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;
941 const struct kmod_cmd kmod_cmd_compat_modprobe
= {
944 .help
= "compat modprobe command",