2 * kmod-modprobe - manage linux kernel modules using libkmod.
4 * Copyright (C) 2011-2012 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/>.
27 #include <sys/types.h>
29 #include <sys/utsname.h>
36 #include "libkmod-array.h"
39 static int log_priority
= LOG_CRIT
;
40 static int use_syslog
= 0;
42 #define DEFAULT_VERBOSE LOG_WARNING
43 static int verbose
= DEFAULT_VERBOSE
;
44 static int do_show
= 0;
45 static int dry_run
= 0;
46 static int ignore_loaded
= 0;
47 static int lookup_only
= 0;
48 static int first_time
= 0;
49 static int ignore_commands
= 0;
50 static int use_blacklist
= 0;
52 static int strip_modversion
= 0;
53 static int strip_vermagic
= 0;
54 static int remove_dependencies
= 0;
55 static int quiet_inuse
= 0;
57 static const char cmdopts_s
[] = "arRibfDcnC:d:S:sqvVh";
58 static const struct option cmdopts
[] = {
59 {"all", no_argument
, 0, 'a'},
60 {"remove", no_argument
, 0, 'r'},
61 {"remove-dependencies", no_argument
, 0, 5},
62 {"resolve-alias", no_argument
, 0, 'R'},
63 {"first-time", no_argument
, 0, 3},
64 {"ignore-install", no_argument
, 0, 'i'},
65 {"ignore-remove", no_argument
, 0, 'i'},
66 {"use-blacklist", no_argument
, 0, 'b'},
67 {"force", no_argument
, 0, 'f'},
68 {"force-modversion", no_argument
, 0, 2},
69 {"force-vermagic", no_argument
, 0, 1},
71 {"show-depends", no_argument
, 0, 'D'},
72 {"showconfig", no_argument
, 0, 'c'},
73 {"show-config", no_argument
, 0, 'c'},
74 {"show-modversions", no_argument
, 0, 4},
75 {"dump-modversions", no_argument
, 0, 4},
77 {"dry-run", no_argument
, 0, 'n'},
78 {"show", no_argument
, 0, 'n'},
80 {"config", required_argument
, 0, 'C'},
81 {"dirname", required_argument
, 0, 'd'},
82 {"set-version", required_argument
, 0, 'S'},
84 {"syslog", no_argument
, 0, 's'},
85 {"quiet", no_argument
, 0, 'q'},
86 {"verbose", no_argument
, 0, 'v'},
87 {"version", no_argument
, 0, 'V'},
88 {"help", no_argument
, 0, 'h'},
92 static void help(const char *progname
)
96 "\t%s [options] [-i] [-b] modulename\n"
97 "\t%s [options] -a [-i] [-b] modulename [modulename...]\n"
98 "\t%s [options] -r [-i] modulename\n"
99 "\t%s [options] -r -a [-i] modulename [modulename...]\n"
100 "\t%s [options] -c\n"
101 "\t%s [options] --dump-modversions filename\n"
102 "Management Options:\n"
103 "\t-a, --all Consider every non-argument to\n"
104 "\t be a module name to be inserted\n"
105 "\t or removed (-r)\n"
106 "\t-r, --remove Remove modules instead of inserting\n"
107 "\t --remove-dependencies Also remove modules depending on it\n"
108 "\t-R, --resolve-alias Only lookup and print alias and exit\n"
109 "\t --first-time Fail if module already inserted or removed\n"
110 "\t-i, --ignore-install Ignore install commands\n"
111 "\t-i, --ignore-remove Ignore remove commands\n"
112 "\t-b, --use-blacklist Apply blacklist to resolved alias.\n"
113 "\t-f, --force Force module insertion or removal.\n"
114 "\t implies --force-modversions and\n"
115 "\t --force-vermagic\n"
116 "\t --force-modversion Ignore module's version\n"
117 "\t --force-vermagic Ignore module's version magic\n"
120 "\t-D, --show-depends Only print module dependencies and exit\n"
121 "\t-c, --showconfig Print out known configuration and exit\n"
122 "\t-c, --show-config Same as --showconfig\n"
123 "\t --show-modversions Dump module symbol version and exit\n"
124 "\t --dump-modversions Same as --show-modversions\n"
127 "\t-n, --dry-run Do not execute operations, just print out\n"
128 "\t-n, --show Same as --dry-run\n"
130 "\t-C, --config=FILE Use FILE instead of default search paths\n"
131 "\t-d, --dirname=DIR Use DIR as filesystem root for /lib/modules\n"
132 "\t-S, --set-version=VERSION Use VERSION instead of `uname -r`\n"
134 "\t-s, --syslog print to syslog, not stderr\n"
135 "\t-q, --quiet disable messages\n"
136 "\t-v, --verbose enables more messages\n"
137 "\t-V, --version show version\n"
138 "\t-h, --help show this help\n",
139 progname
, progname
, progname
, progname
, progname
, progname
);
142 static inline void _show(const char *fmt
, ...)
146 if (!do_show
&& verbose
<= DEFAULT_VERBOSE
)
150 vfprintf(stdout
, fmt
, args
);
155 static inline void _log(int prio
, const char *fmt
, ...)
157 const char *prioname
;
165 if (vasprintf(&msg
, fmt
, args
) < 0)
179 prioname
= "WARNING";
191 snprintf(buf
, sizeof(buf
), "LOG-%03d", prio
);
196 syslog(LOG_NOTICE
, "%s: %s", prioname
, msg
);
198 fprintf(stderr
, "%s: %s", prioname
, msg
);
201 if (prio
<= LOG_CRIT
)
204 #define ERR(...) _log(LOG_ERR, __VA_ARGS__)
205 #define WRN(...) _log(LOG_WARNING, __VA_ARGS__)
206 #define INF(...) _log(LOG_INFO, __VA_ARGS__)
207 #define DBG(...) _log(LOG_DEBUG, __VA_ARGS__)
208 #define LOG(...) _log(log_priority, __VA_ARGS__)
209 #define SHOW(...) _show(__VA_ARGS__)
211 static int show_config(struct kmod_ctx
*ctx
)
213 struct config_iterators
{
215 struct kmod_config_iter
*(*get_iter
)(const struct kmod_ctx
*ctx
);
217 { "blacklist", kmod_config_get_blacklists
},
218 { "install", kmod_config_get_install_commands
},
219 { "remove", kmod_config_get_remove_commands
},
220 { "alias", kmod_config_get_aliases
},
221 { "options", kmod_config_get_options
},
222 { "softdep", kmod_config_get_softdeps
},
226 for (i
= 0; i
< ARRAY_SIZE(ci
); i
++) {
227 struct kmod_config_iter
*iter
= ci
[i
].get_iter(ctx
);
232 while (kmod_config_iter_next(iter
)) {
235 printf("%s %s", ci
[i
].name
,
236 kmod_config_iter_get_key(iter
));
237 val
= kmod_config_iter_get_value(iter
);
245 kmod_config_iter_free_iter(iter
);
248 puts("\n# End of configuration files. Dumping indexes now:\n");
251 kmod_dump_index(ctx
, KMOD_INDEX_MODULES_ALIAS
, STDOUT_FILENO
);
252 kmod_dump_index(ctx
, KMOD_INDEX_MODULES_SYMBOL
, STDOUT_FILENO
);
257 static int show_modversions(struct kmod_ctx
*ctx
, const char *filename
)
259 struct kmod_list
*l
, *list
= NULL
;
260 struct kmod_module
*mod
;
261 int err
= kmod_module_new_from_path(ctx
, filename
, &mod
);
263 LOG("Module %s not found.\n", filename
);
267 err
= kmod_module_get_versions(mod
, &list
);
269 LOG("could not get modversions of %s: %s\n",
270 filename
, strerror(-err
));
271 kmod_module_unref(mod
);
275 kmod_list_foreach(l
, list
) {
276 const char *symbol
= kmod_module_version_get_symbol(l
);
277 uint64_t crc
= kmod_module_version_get_crc(l
);
278 printf("0x%08"PRIx64
"\t%s\n", crc
, symbol
);
280 kmod_module_versions_free_list(list
);
281 kmod_module_unref(mod
);
285 static int command_do(struct kmod_module
*module
, const char *type
,
286 const char *command
, const char *cmdline_opts
)
288 const char *modname
= kmod_module_get_name(module
);
289 char *p
, *cmd
= NULL
;
290 size_t cmdlen
, cmdline_opts_len
, varlen
;
293 if (cmdline_opts
== NULL
)
295 cmdline_opts_len
= strlen(cmdline_opts
);
297 cmd
= strdup(command
);
300 cmdlen
= strlen(cmd
);
301 varlen
= sizeof("$CMDLINE_OPTS") - 1;
302 while ((p
= strstr(cmd
, "$CMDLINE_OPTS")) != NULL
) {
303 size_t prefixlen
= p
- cmd
;
304 size_t suffixlen
= cmdlen
- prefixlen
- varlen
;
305 size_t slen
= cmdlen
- varlen
+ cmdline_opts_len
;
306 char *suffix
= p
+ varlen
;
307 char *s
= malloc(slen
+ 1);
312 memcpy(s
, cmd
, p
- cmd
);
313 memcpy(s
+ prefixlen
, cmdline_opts
, cmdline_opts_len
);
314 memcpy(s
+ prefixlen
+ cmdline_opts_len
, suffix
, suffixlen
);
322 SHOW("%s %s\n", type
, cmd
);
326 setenv("MODPROBE_MODULE", modname
, 1);
328 unsetenv("MODPROBE_MODULE");
329 if (ret
== -1 || WEXITSTATUS(ret
)) {
330 LOG("Error running %s command for %s\n", type
, modname
);
332 ret
= -WEXITSTATUS(ret
);
340 static int rmmod_do_remove_module(struct kmod_module
*mod
)
342 const char *modname
= kmod_module_get_name(mod
);
343 struct kmod_list
*deps
, *itr
;
346 SHOW("rmmod %s\n", kmod_module_get_name(mod
));
352 flags
|= KMOD_REMOVE_FORCE
;
354 err
= kmod_module_remove_module(mod
, flags
);
355 if (err
== -EEXIST
) {
359 LOG("Module %s is not in kernel.\n", modname
);
362 deps
= kmod_module_get_dependencies(mod
);
364 kmod_list_foreach(itr
, deps
) {
365 struct kmod_module
*dep
= kmod_module_get_module(itr
);
366 if (kmod_module_get_refcnt(dep
) == 0)
367 rmmod_do_remove_module(dep
);
368 kmod_module_unref(dep
);
370 kmod_module_unref_list(deps
);
376 static int rmmod_do_module(struct kmod_module
*mod
, bool do_dependencies
);
378 static int rmmod_do_deps_list(struct kmod_list
*list
, bool stop_on_errors
)
382 kmod_list_foreach_reverse(l
, list
) {
383 struct kmod_module
*m
= kmod_module_get_module(l
);
384 int r
= rmmod_do_module(m
, false);
385 kmod_module_unref(m
);
387 if (r
< 0 && stop_on_errors
)
394 static int rmmod_do_module(struct kmod_module
*mod
, bool do_dependencies
)
396 const char *modname
= kmod_module_get_name(mod
);
397 struct kmod_list
*pre
= NULL
, *post
= NULL
;
398 const char *cmd
= NULL
;
401 if (!ignore_commands
) {
402 err
= kmod_module_get_softdeps(mod
, &pre
, &post
);
404 WRN("could not get softdeps of '%s': %s\n",
405 modname
, strerror(-err
));
409 cmd
= kmod_module_get_remove_commands(mod
);
412 if (cmd
== NULL
&& !ignore_loaded
) {
413 int state
= kmod_module_get_initstate(mod
);
417 LOG("Module %s is not in kernel.\n", modname
);
423 } else if (state
== KMOD_MODULE_BUILTIN
) {
424 LOG("Module %s is builtin.\n", modname
);
430 rmmod_do_deps_list(post
, false);
432 if (do_dependencies
&& remove_dependencies
) {
433 struct kmod_list
*deps
= kmod_module_get_dependencies(mod
);
435 err
= rmmod_do_deps_list(deps
, true);
440 if (!ignore_loaded
) {
441 int usage
= kmod_module_get_refcnt(mod
);
445 LOG("Module %s is in use.\n", modname
);
453 err
= rmmod_do_remove_module(mod
);
455 err
= command_do(mod
, "remove", cmd
, NULL
);
460 rmmod_do_deps_list(pre
, false);
463 kmod_module_unref_list(pre
);
464 kmod_module_unref_list(post
);
469 static int rmmod(struct kmod_ctx
*ctx
, const char *alias
)
471 struct kmod_list
*l
, *list
= NULL
;
474 err
= kmod_module_new_from_lookup(ctx
, alias
, &list
);
479 LOG("Module %s not found.\n", alias
);
483 kmod_list_foreach(l
, list
) {
484 struct kmod_module
*mod
= kmod_module_get_module(l
);
485 err
= rmmod_do_module(mod
, true);
486 kmod_module_unref(mod
);
491 kmod_module_unref_list(list
);
495 static int rmmod_all(struct kmod_ctx
*ctx
, char **args
, int nargs
)
499 for (i
= 0; i
< nargs
; i
++) {
500 int r
= rmmod(ctx
, args
[i
]);
508 static int handle_failed_lookup(struct kmod_ctx
*ctx
, const char *alias
)
510 struct kmod_module
*mod
;
513 DBG("lookup failed - trying to check if it's builtin\n");
515 err
= kmod_module_new_from_name(ctx
, alias
, &mod
);
519 state
= kmod_module_get_initstate(mod
);
520 kmod_module_unref(mod
);
522 if (state
!= KMOD_MODULE_BUILTIN
) {
523 LOG("Module %s not found.\n", alias
);
528 LOG("Module %s already in kernel (builtin).\n", alias
);
532 SHOW("builtin %s\n", alias
);
536 static void print_action(struct kmod_module
*m
, bool install
,
542 printf("install %s %s\n", kmod_module_get_install_commands(m
),
547 path
= kmod_module_get_path(m
);
550 assert(kmod_module_get_initstate(m
) == KMOD_MODULE_BUILTIN
);
551 printf("builtin %s\n", kmod_module_get_name(m
));
553 printf("insmod %s %s\n", kmod_module_get_path(m
), options
);
556 static int insmod(struct kmod_ctx
*ctx
, const char *alias
,
557 const char *extra_options
)
559 struct kmod_list
*l
, *list
= NULL
;
562 void (*show
)(struct kmod_module
*m
, bool install
,
563 const char *options
) = NULL
;
565 err
= kmod_module_new_from_lookup(ctx
, alias
, &list
);
570 return handle_failed_lookup(ctx
, alias
);
572 if (strip_modversion
|| force
)
573 flags
|= KMOD_PROBE_FORCE_MODVERSION
;
574 if (strip_vermagic
|| force
)
575 flags
|= KMOD_PROBE_FORCE_VERMAGIC
;
577 flags
|= KMOD_PROBE_IGNORE_COMMAND
;
579 flags
|= KMOD_PROBE_IGNORE_LOADED
;
581 flags
|= KMOD_PROBE_DRY_RUN
;
582 if (do_show
|| verbose
> DEFAULT_VERBOSE
)
583 show
= &print_action
;
585 flags
|= KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY
;
588 flags
|= KMOD_PROBE_APPLY_BLACKLIST
;
590 flags
|= KMOD_PROBE_FAIL_ON_LOADED
;
592 kmod_list_foreach(l
, list
) {
593 struct kmod_module
*mod
= kmod_module_get_module(l
);
596 printf("%s\n", kmod_module_get_name(mod
));
598 err
= kmod_module_probe_insert_module(mod
, flags
,
599 extra_options
, NULL
, NULL
, show
);
603 /* ignore flag return values such as a mod being blacklisted */
608 ERR("could not insert '%s': Module already in kernel\n",
609 kmod_module_get_name(mod
));
612 ERR("could not insert '%s': Unknown symbol in module, "
613 "or unknown parameter (see dmesg)\n",
614 kmod_module_get_name(mod
));
617 ERR("could not insert '%s': %s\n",
618 kmod_module_get_name(mod
),
624 kmod_module_unref(mod
);
627 kmod_module_unref_list(list
);
631 static int insmod_all(struct kmod_ctx
*ctx
, char **args
, int nargs
)
635 for (i
= 0; i
< nargs
; i
++) {
636 int r
= insmod(ctx
, args
[i
], NULL
);
644 static void env_modprobe_options_append(const char *value
)
646 const char *old
= getenv("MODPROBE_OPTIONS");
650 setenv("MODPROBE_OPTIONS", value
, 1);
654 if (asprintf(&env
, "%s %s", old
, value
) < 0) {
655 ERR("could not append value to $MODPROBE_OPTIONS\n");
659 if (setenv("MODPROBE_OPTIONS", env
, 1) < 0)
660 ERR("could not setenv(MODPROBE_OPTIONS, \"%s\")\n", env
);
664 static int options_from_array(char **args
, int nargs
, char **output
)
670 for (i
= 1; i
< nargs
; i
++) {
671 size_t len
= strlen(args
[i
]);
676 value
= strchr(args
[i
], '=');
679 if (*value
!= '"' && *value
!= '\'') {
680 if (strchr(value
, ' '))
685 tmp
= realloc(opts
, optslen
+ len
+ qlen
+ 2);
690 ERR("could not gather module options: out-of-memory\n");
699 memcpy(opts
+ optslen
, args
[i
], len
+ 1);
702 size_t keylen
= value
- args
[i
];
703 size_t valuelen
= len
- keylen
;
704 memcpy(opts
+ optslen
, args
[i
], keylen
);
708 memcpy(opts
+ optslen
, value
, valuelen
);
712 opts
[optslen
] = '\0';
720 static char **prepend_options_from_env(int *p_argc
, char **orig_argv
)
722 const char *p
, *env
= getenv("MODPROBE_OPTIONS");
723 char **new_argv
, *str_start
, *str_end
, *str
, *s
, *quote
;
724 int i
, argc
= *p_argc
;
725 size_t envlen
, space_count
= 0;
730 for (p
= env
; *p
!= '\0'; p
++) {
736 new_argv
= malloc(sizeof(char *) * (argc
+ space_count
+ 3 + envlen
));
737 if (new_argv
== NULL
)
740 new_argv
[0] = orig_argv
[0];
741 str_start
= str
= (char *) (new_argv
+ argc
+ space_count
+ 3);
742 memcpy(str
, env
, envlen
+ 1);
744 str_end
= str_start
+ envlen
;
747 for (i
= 1, s
= str
; *s
!= '\0'; s
++) {
754 } else if (*s
== '"' || *s
== '\'')
759 new_argv
[i
] = str
+ 1;
765 for (it
= quote
; it
< s
- 1; it
++)
767 for (it
= s
- 1; it
< str_end
- 2; it
++)
782 memcpy(new_argv
+ i
, orig_argv
+ 1, sizeof(char *) * (argc
- 1));
783 new_argv
[i
+ argc
] = NULL
;
784 *p_argc
= i
+ argc
- 1;
789 static void log_syslog(void *data
, int priority
, const char *file
, int line
,
790 const char *fn
, const char *format
, va_list args
)
793 const char *prioname
;
803 prioname
= "WARNING";
815 snprintf(buf
, sizeof(buf
), "LOG-%03d", priority
);
819 if (vasprintf(&str
, format
, args
) < 0)
822 syslog(LOG_NOTICE
, "%s: %s:%d %s() %s", prioname
, file
, line
, fn
, str
);
824 syslog(LOG_NOTICE
, "%s: %s", prioname
, str
);
830 static int do_modprobe(int argc
, char **orig_argv
)
832 struct kmod_ctx
*ctx
;
833 char **args
= NULL
, **argv
;
834 const char **config_paths
= NULL
;
835 int nargs
= 0, n_config_paths
= 0;
836 char dirname_buf
[PATH_MAX
];
837 const char *dirname
= NULL
;
838 const char *root
= NULL
;
839 const char *kversion
= NULL
;
842 int do_show_config
= 0;
843 int do_show_modversions
= 0;
846 argv
= prepend_options_from_env(&argc
, orig_argv
);
848 fputs("Error: could not prepend options from command line\n",
855 c
= getopt_long(argc
, argv
, cmdopts_s
, cmdopts
, &idx
);
860 log_priority
= LOG_WARNING
;
867 remove_dependencies
= 1;
885 strip_modversion
= 1;
899 do_show_modversions
= 1;
905 size_t bytes
= sizeof(char *) * (n_config_paths
+ 2);
906 void *tmp
= realloc(config_paths
, bytes
);
908 fputs("Error: out-of-memory\n", stderr
);
912 config_paths
[n_config_paths
] = optarg
;
914 config_paths
[n_config_paths
] = NULL
;
916 env_modprobe_options_append("-C");
917 env_modprobe_options_append(optarg
);
927 env_modprobe_options_append("-s");
931 env_modprobe_options_append("-q");
935 env_modprobe_options_append("-v");
939 puts(PACKAGE
" version " VERSION
);
940 if (argv
!= orig_argv
)
945 help(basename(argv
[0]));
946 if (argv
!= orig_argv
)
953 fprintf(stderr
, "Error: unexpected getopt_long() value '%c'.\n",
959 args
= argv
+ optind
;
960 nargs
= argc
- optind
;
962 if (!do_show_config
) {
964 fputs("Error: missing parameters. See -h.\n", stderr
);
969 if (root
!= NULL
|| kversion
!= NULL
) {
973 if (kversion
== NULL
) {
975 fprintf(stderr
, "Error: uname() failed: %s\n",
979 kversion
= u
.release
;
981 snprintf(dirname_buf
, sizeof(dirname_buf
),
982 "%s/lib/modules/%s", root
,
984 dirname
= dirname_buf
;
987 ctx
= kmod_new(dirname
, config_paths
);
989 fputs("Error: kmod_new() failed!\n", stderr
);
992 kmod_load_resources(ctx
);
994 kmod_set_log_priority(ctx
, verbose
);
996 openlog("modprobe", LOG_CONS
, LOG_DAEMON
);
997 kmod_set_log_fn(ctx
, log_syslog
, NULL
);
1001 err
= show_config(ctx
);
1002 else if (do_show_modversions
)
1003 err
= show_modversions(ctx
, args
[0]);
1005 err
= rmmod_all(ctx
, args
, nargs
);
1007 err
= insmod_all(ctx
, args
, nargs
);
1010 err
= options_from_array(args
, nargs
, &opts
);
1012 err
= insmod(ctx
, args
[0], opts
);
1022 if (argv
!= orig_argv
)
1026 return err
>= 0 ? EXIT_SUCCESS
: EXIT_FAILURE
;
1029 if (argv
!= orig_argv
)
1032 return EXIT_FAILURE
;
1037 const struct kmod_cmd kmod_cmd_compat_modprobe
= {
1040 .help
= "compat modprobe command",