Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 1 | /* |
Lucas De Marchi | 8b01376 | 2012-01-12 17:14:30 -0200 | [diff] [blame] | 2 | * kmod-modprobe - manage linux kernel modules using libkmod. |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 3 | * |
Lucas De Marchi | e6b0e49 | 2013-01-16 11:27:21 -0200 | [diff] [blame] | 4 | * Copyright (C) 2011-2013 ProFUSION embedded systems |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 5 | * |
Lucas De Marchi | cb451f3 | 2011-12-12 18:24:35 -0200 | [diff] [blame] | 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. |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 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 |
Lucas De Marchi | cb451f3 | 2011-12-12 18:24:35 -0200 | [diff] [blame] | 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 15 | * |
Lucas De Marchi | cb451f3 | 2011-12-12 18:24:35 -0200 | [diff] [blame] | 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/>. |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 18 | */ |
Lucas De Marchi | cb451f3 | 2011-12-12 18:24:35 -0200 | [diff] [blame] | 19 | |
Lucas De Marchi | 4744ebc | 2012-03-15 00:14:35 -0300 | [diff] [blame] | 20 | #include <assert.h> |
Lucas De Marchi | c2e4286 | 2014-10-03 01:41:42 -0300 | [diff] [blame] | 21 | #include <errno.h> |
| 22 | #include <getopt.h> |
| 23 | #include <limits.h> |
| 24 | #include <stdbool.h> |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 27 | #include <string.h> |
Lucas De Marchi | c2e4286 | 2014-10-03 01:41:42 -0300 | [diff] [blame] | 28 | #include <unistd.h> |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 29 | #include <sys/stat.h> |
Lucas De Marchi | c2e4286 | 2014-10-03 01:41:42 -0300 | [diff] [blame] | 30 | #include <sys/types.h> |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 31 | #include <sys/utsname.h> |
Thierry Vignaud | eff917c | 2012-01-17 17:32:48 -0200 | [diff] [blame] | 32 | #include <sys/wait.h> |
Lucas De Marchi | 74d1df6 | 2014-10-03 00:22:36 -0300 | [diff] [blame] | 33 | |
| 34 | #include <shared/array.h> |
Lucas De Marchi | 576dd43 | 2014-10-02 22:03:19 -0300 | [diff] [blame] | 35 | #include <shared/macro.h> |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 36 | |
Lucas De Marchi | f357866 | 2015-01-02 12:38:27 -0200 | [diff] [blame] | 37 | #include <libkmod/libkmod.h> |
Lucas De Marchi | c2e4286 | 2014-10-03 01:41:42 -0300 | [diff] [blame] | 38 | |
Lucas De Marchi | 4a2e20d | 2012-11-06 16:54:17 -0200 | [diff] [blame] | 39 | #include "kmod.h" |
| 40 | |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 41 | static int log_priority = LOG_CRIT; |
| 42 | static int use_syslog = 0; |
Lucas De Marchi | fcb0ce9 | 2012-11-06 19:01:59 -0200 | [diff] [blame] | 43 | #define LOG(...) log_printf(log_priority, __VA_ARGS__) |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 44 | |
| 45 | #define DEFAULT_VERBOSE LOG_WARNING |
| 46 | static int verbose = DEFAULT_VERBOSE; |
Gustavo Sverzut Barbieri | 525fa07 | 2012-01-08 13:58:28 -0200 | [diff] [blame] | 47 | static int do_show = 0; |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 48 | static int dry_run = 0; |
| 49 | static int ignore_loaded = 0; |
Lucas De Marchi | 8122985 | 2011-12-16 02:58:48 -0200 | [diff] [blame] | 50 | static int lookup_only = 0; |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 51 | static int first_time = 0; |
| 52 | static int ignore_commands = 0; |
| 53 | static int use_blacklist = 0; |
| 54 | static int force = 0; |
| 55 | static int strip_modversion = 0; |
| 56 | static int strip_vermagic = 0; |
| 57 | static int remove_dependencies = 0; |
Lucas De Marchi | d8a6c0c | 2012-01-01 06:07:46 -0200 | [diff] [blame] | 58 | static int quiet_inuse = 0; |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 59 | |
Dave Reisner | cc98830 | 2012-01-26 11:36:35 -0500 | [diff] [blame] | 60 | static const char cmdopts_s[] = "arRibfDcnC:d:S:sqvVh"; |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 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}, |
| 73 | |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 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 | |
| 80 | {"dry-run", no_argument, 0, 'n'}, |
| 81 | {"show", no_argument, 0, 'n'}, |
| 82 | |
| 83 | {"config", required_argument, 0, 'C'}, |
| 84 | {"dirname", required_argument, 0, 'd'}, |
| 85 | {"set-version", required_argument, 0, 'S'}, |
| 86 | |
| 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'}, |
| 92 | {NULL, 0, 0, 0} |
| 93 | }; |
| 94 | |
Lucas De Marchi | 4a2e20d | 2012-11-06 16:54:17 -0200 | [diff] [blame] | 95 | static void help(void) |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 96 | { |
Lucas De Marchi | 34e06bf | 2012-11-06 17:32:41 -0200 | [diff] [blame] | 97 | printf("Usage:\n" |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 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" |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 102 | "\t%s [options] -c\n" |
| 103 | "\t%s [options] --dump-modversions filename\n" |
| 104 | "Management Options:\n" |
Gustavo Sverzut Barbieri | ab70dce | 2011-12-19 13:02:15 -0200 | [diff] [blame] | 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" |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 120 | "\n" |
| 121 | "Query Options:\n" |
Gustavo Sverzut Barbieri | ab70dce | 2011-12-19 13:02:15 -0200 | [diff] [blame] | 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" |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 127 | "\n" |
| 128 | "General Options:\n" |
Gustavo Sverzut Barbieri | ab70dce | 2011-12-19 13:02:15 -0200 | [diff] [blame] | 129 | "\t-n, --dry-run Do not execute operations, just print out\n" |
| 130 | "\t-n, --show Same as --dry-run\n" |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 131 | |
Lucas De Marchi | 2c96693 | 2011-12-20 16:39:59 -0200 | [diff] [blame] | 132 | "\t-C, --config=FILE Use FILE instead of default search paths\n" |
Dave Reisner | c5b37db | 2012-09-27 11:00:42 -0400 | [diff] [blame] | 133 | "\t-d, --dirname=DIR Use DIR as filesystem root for /lib/modules\n" |
Gustavo Sverzut Barbieri | ab70dce | 2011-12-19 13:02:15 -0200 | [diff] [blame] | 134 | "\t-S, --set-version=VERSION Use VERSION instead of `uname -r`\n" |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 135 | |
| 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", |
Lucas De Marchi | 7c04aee | 2012-11-06 19:20:09 -0200 | [diff] [blame] | 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); |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 144 | } |
| 145 | |
Lucas De Marchi | 1958af8 | 2013-04-21 16:16:18 -0300 | [diff] [blame] | 146 | _printf_format_(1, 2) |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 147 | static inline void _show(const char *fmt, ...) |
| 148 | { |
| 149 | va_list args; |
| 150 | |
Gustavo Sverzut Barbieri | 525fa07 | 2012-01-08 13:58:28 -0200 | [diff] [blame] | 151 | if (!do_show && verbose <= DEFAULT_VERBOSE) |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 152 | return; |
| 153 | |
| 154 | va_start(args, fmt); |
| 155 | vfprintf(stdout, fmt, args); |
| 156 | fflush(stdout); |
| 157 | va_end(args); |
| 158 | } |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 159 | #define SHOW(...) _show(__VA_ARGS__) |
| 160 | |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 161 | static int show_config(struct kmod_ctx *ctx) |
| 162 | { |
Lucas De Marchi | bc43496 | 2012-01-13 02:35:34 -0200 | [diff] [blame] | 163 | struct config_iterators { |
| 164 | const char *name; |
| 165 | struct kmod_config_iter *(*get_iter)(const struct kmod_ctx *ctx); |
| 166 | } ci[] = { |
| 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 }, |
Elan Ruusamäe | 02629fa | 2012-03-22 12:00:16 -0300 | [diff] [blame] | 171 | { "options", kmod_config_get_options }, |
Lucas De Marchi | bc43496 | 2012-01-13 02:35:34 -0200 | [diff] [blame] | 172 | { "softdep", kmod_config_get_softdeps }, |
| 173 | }; |
| 174 | size_t i; |
| 175 | |
| 176 | for (i = 0; i < ARRAY_SIZE(ci); i++) { |
| 177 | struct kmod_config_iter *iter = ci[i].get_iter(ctx); |
| 178 | |
| 179 | if (iter == NULL) |
| 180 | continue; |
| 181 | |
| 182 | while (kmod_config_iter_next(iter)) { |
| 183 | const char *val; |
| 184 | |
| 185 | printf("%s %s", ci[i].name, |
| 186 | kmod_config_iter_get_key(iter)); |
| 187 | val = kmod_config_iter_get_value(iter); |
| 188 | if (val != NULL) { |
| 189 | putchar(' '); |
| 190 | puts(val); |
| 191 | } else |
| 192 | putchar('\n'); |
| 193 | } |
| 194 | |
| 195 | kmod_config_iter_free_iter(iter); |
| 196 | } |
| 197 | |
Lucas De Marchi | 28f32c6 | 2012-01-27 23:56:46 -0200 | [diff] [blame] | 198 | puts("\n# End of configuration files. Dumping indexes now:\n"); |
Lucas De Marchi | 09e9ae5 | 2012-01-17 10:05:02 -0200 | [diff] [blame] | 199 | fflush(stdout); |
| 200 | |
Lucas De Marchi | 49a1637 | 2012-01-16 16:00:35 -0200 | [diff] [blame] | 201 | kmod_dump_index(ctx, KMOD_INDEX_MODULES_ALIAS, STDOUT_FILENO); |
| 202 | kmod_dump_index(ctx, KMOD_INDEX_MODULES_SYMBOL, STDOUT_FILENO); |
| 203 | |
Lucas De Marchi | bc43496 | 2012-01-13 02:35:34 -0200 | [diff] [blame] | 204 | return 0; |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | static int show_modversions(struct kmod_ctx *ctx, const char *filename) |
| 208 | { |
Gustavo Sverzut Barbieri | 0e3e2f4 | 2011-12-19 12:45:22 -0200 | [diff] [blame] | 209 | struct kmod_list *l, *list = NULL; |
| 210 | struct kmod_module *mod; |
| 211 | int err = kmod_module_new_from_path(ctx, filename, &mod); |
| 212 | if (err < 0) { |
| 213 | LOG("Module %s not found.\n", filename); |
| 214 | return err; |
| 215 | } |
| 216 | |
| 217 | err = kmod_module_get_versions(mod, &list); |
| 218 | if (err < 0) { |
Dave Reisner | 6369837 | 2012-01-04 10:41:50 -0500 | [diff] [blame] | 219 | LOG("could not get modversions of %s: %s\n", |
Gustavo Sverzut Barbieri | 0e3e2f4 | 2011-12-19 12:45:22 -0200 | [diff] [blame] | 220 | filename, strerror(-err)); |
| 221 | kmod_module_unref(mod); |
| 222 | return err; |
| 223 | } |
| 224 | |
| 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); |
| 229 | } |
| 230 | kmod_module_versions_free_list(list); |
| 231 | kmod_module_unref(mod); |
| 232 | return 0; |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 233 | } |
| 234 | |
Lucas De Marchi | 8f19221 | 2012-01-11 15:38:50 -0200 | [diff] [blame] | 235 | static int command_do(struct kmod_module *module, const char *type, |
| 236 | const char *command, const char *cmdline_opts) |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 237 | { |
| 238 | const char *modname = kmod_module_get_name(module); |
| 239 | char *p, *cmd = NULL; |
| 240 | size_t cmdlen, cmdline_opts_len, varlen; |
| 241 | int ret = 0; |
| 242 | |
| 243 | if (cmdline_opts == NULL) |
| 244 | cmdline_opts = ""; |
| 245 | cmdline_opts_len = strlen(cmdline_opts); |
| 246 | |
| 247 | cmd = strdup(command); |
| 248 | if (cmd == NULL) |
| 249 | return -ENOMEM; |
| 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); |
| 258 | if (s == NULL) { |
| 259 | free(cmd); |
| 260 | return -ENOMEM; |
| 261 | } |
| 262 | memcpy(s, cmd, p - cmd); |
| 263 | memcpy(s + prefixlen, cmdline_opts, cmdline_opts_len); |
| 264 | memcpy(s + prefixlen + cmdline_opts_len, suffix, suffixlen); |
| 265 | s[slen] = '\0'; |
| 266 | |
| 267 | free(cmd); |
| 268 | cmd = s; |
| 269 | cmdlen = slen; |
| 270 | } |
| 271 | |
| 272 | SHOW("%s %s\n", type, cmd); |
| 273 | if (dry_run) |
| 274 | goto end; |
| 275 | |
| 276 | setenv("MODPROBE_MODULE", modname, 1); |
| 277 | ret = system(cmd); |
| 278 | unsetenv("MODPROBE_MODULE"); |
| 279 | if (ret == -1 || WEXITSTATUS(ret)) { |
| 280 | LOG("Error running %s command for %s\n", type, modname); |
| 281 | if (ret != -1) |
| 282 | ret = -WEXITSTATUS(ret); |
| 283 | } |
| 284 | |
| 285 | end: |
| 286 | free(cmd); |
| 287 | return ret; |
| 288 | } |
| 289 | |
Lucas De Marchi | a872bba | 2012-01-12 15:23:51 -0200 | [diff] [blame] | 290 | static int rmmod_do_remove_module(struct kmod_module *mod) |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 291 | { |
Lucas De Marchi | a872bba | 2012-01-12 15:23:51 -0200 | [diff] [blame] | 292 | const char *modname = kmod_module_get_name(mod); |
Dave Reisner | 69a1974 | 2012-01-30 17:16:50 -0500 | [diff] [blame] | 293 | struct kmod_list *deps, *itr; |
Lucas De Marchi | a872bba | 2012-01-12 15:23:51 -0200 | [diff] [blame] | 294 | int flags = 0, err; |
Gustavo Sverzut Barbieri | e793f1e | 2011-12-16 22:35:28 -0200 | [diff] [blame] | 295 | |
Lucas De Marchi | a872bba | 2012-01-12 15:23:51 -0200 | [diff] [blame] | 296 | SHOW("rmmod %s\n", kmod_module_get_name(mod)); |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 297 | |
Lucas De Marchi | a872bba | 2012-01-12 15:23:51 -0200 | [diff] [blame] | 298 | if (dry_run) |
| 299 | return 0; |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 300 | |
Lucas De Marchi | a872bba | 2012-01-12 15:23:51 -0200 | [diff] [blame] | 301 | if (force) |
| 302 | flags |= KMOD_REMOVE_FORCE; |
Gustavo Sverzut Barbieri | e793f1e | 2011-12-16 22:35:28 -0200 | [diff] [blame] | 303 | |
Lucas De Marchi | a872bba | 2012-01-12 15:23:51 -0200 | [diff] [blame] | 304 | err = kmod_module_remove_module(mod, flags); |
| 305 | if (err == -EEXIST) { |
| 306 | if (!first_time) |
| 307 | err = 0; |
Gustavo Sverzut Barbieri | e793f1e | 2011-12-16 22:35:28 -0200 | [diff] [blame] | 308 | else |
Lucas De Marchi | a872bba | 2012-01-12 15:23:51 -0200 | [diff] [blame] | 309 | LOG("Module %s is not in kernel.\n", modname); |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 310 | } |
Gustavo Sverzut Barbieri | e793f1e | 2011-12-16 22:35:28 -0200 | [diff] [blame] | 311 | |
Dave Reisner | 69a1974 | 2012-01-30 17:16:50 -0500 | [diff] [blame] | 312 | deps = kmod_module_get_dependencies(mod); |
| 313 | if (deps != NULL) { |
| 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); |
| 319 | } |
| 320 | kmod_module_unref_list(deps); |
| 321 | } |
| 322 | |
Gustavo Sverzut Barbieri | e793f1e | 2011-12-16 22:35:28 -0200 | [diff] [blame] | 323 | return err; |
| 324 | } |
| 325 | |
Lucas De Marchi | a872bba | 2012-01-12 15:23:51 -0200 | [diff] [blame] | 326 | static int rmmod_do_module(struct kmod_module *mod, bool do_dependencies); |
| 327 | |
| 328 | static int rmmod_do_deps_list(struct kmod_list *list, bool stop_on_errors) |
Gustavo Sverzut Barbieri | e793f1e | 2011-12-16 22:35:28 -0200 | [diff] [blame] | 329 | { |
Lucas De Marchi | a872bba | 2012-01-12 15:23:51 -0200 | [diff] [blame] | 330 | struct kmod_list *l; |
| 331 | |
| 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); |
| 336 | |
| 337 | if (r < 0 && stop_on_errors) |
| 338 | return r; |
| 339 | } |
| 340 | |
| 341 | return 0; |
Gustavo Sverzut Barbieri | e793f1e | 2011-12-16 22:35:28 -0200 | [diff] [blame] | 342 | } |
| 343 | |
Lucas De Marchi | a872bba | 2012-01-12 15:23:51 -0200 | [diff] [blame] | 344 | static int rmmod_do_module(struct kmod_module *mod, bool do_dependencies) |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 345 | { |
| 346 | const char *modname = kmod_module_get_name(mod); |
Lucas De Marchi | a872bba | 2012-01-12 15:23:51 -0200 | [diff] [blame] | 347 | struct kmod_list *pre = NULL, *post = NULL; |
| 348 | const char *cmd = NULL; |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 349 | int err; |
| 350 | |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 351 | if (!ignore_commands) { |
Gustavo Sverzut Barbieri | e793f1e | 2011-12-16 22:35:28 -0200 | [diff] [blame] | 352 | err = kmod_module_get_softdeps(mod, &pre, &post); |
| 353 | if (err < 0) { |
| 354 | WRN("could not get softdeps of '%s': %s\n", |
Lucas De Marchi | 8f19221 | 2012-01-11 15:38:50 -0200 | [diff] [blame] | 355 | modname, strerror(-err)); |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 356 | return err; |
Gustavo Sverzut Barbieri | e793f1e | 2011-12-16 22:35:28 -0200 | [diff] [blame] | 357 | } |
| 358 | |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 359 | cmd = kmod_module_get_remove_commands(mod); |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 360 | } |
| 361 | |
Lucas De Marchi | a872bba | 2012-01-12 15:23:51 -0200 | [diff] [blame] | 362 | if (cmd == NULL && !ignore_loaded) { |
Lucas De Marchi | 9bf60d2 | 2011-12-19 02:18:14 -0200 | [diff] [blame] | 363 | int state = kmod_module_get_initstate(mod); |
| 364 | |
Lucas De Marchi | e5e2a68 | 2011-12-19 02:26:34 -0200 | [diff] [blame] | 365 | if (state < 0) { |
Dave Reisner | f758caf | 2012-03-14 22:15:21 -0400 | [diff] [blame] | 366 | if (first_time) { |
| 367 | LOG("Module %s is not in kernel.\n", modname); |
| 368 | err = -ENOENT; |
| 369 | } else { |
| 370 | err = 0; |
| 371 | } |
Lucas De Marchi | e4e1e64 | 2012-01-12 15:36:54 -0200 | [diff] [blame] | 372 | goto error; |
Lucas De Marchi | e5e2a68 | 2011-12-19 02:26:34 -0200 | [diff] [blame] | 373 | } else if (state == KMOD_MODULE_BUILTIN) { |
Lucas De Marchi | 9bf60d2 | 2011-12-19 02:18:14 -0200 | [diff] [blame] | 374 | LOG("Module %s is builtin.\n", modname); |
Lucas De Marchi | e4e1e64 | 2012-01-12 15:36:54 -0200 | [diff] [blame] | 375 | err = -ENOENT; |
| 376 | goto error; |
Lucas De Marchi | 9bf60d2 | 2011-12-19 02:18:14 -0200 | [diff] [blame] | 377 | } |
| 378 | } |
| 379 | |
Lucas De Marchi | a872bba | 2012-01-12 15:23:51 -0200 | [diff] [blame] | 380 | rmmod_do_deps_list(post, false); |
| 381 | |
| 382 | if (do_dependencies && remove_dependencies) { |
| 383 | struct kmod_list *deps = kmod_module_get_dependencies(mod); |
| 384 | |
| 385 | err = rmmod_do_deps_list(deps, true); |
Lucas De Marchi | 9bf60d2 | 2011-12-19 02:18:14 -0200 | [diff] [blame] | 386 | if (err < 0) |
Lucas De Marchi | a872bba | 2012-01-12 15:23:51 -0200 | [diff] [blame] | 387 | goto error; |
Lucas De Marchi | 9bf60d2 | 2011-12-19 02:18:14 -0200 | [diff] [blame] | 388 | } |
| 389 | |
Johannes Berg | c010f02 | 2013-05-02 15:23:28 +0200 | [diff] [blame] | 390 | if (!ignore_loaded && !cmd) { |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 391 | int usage = kmod_module_get_refcnt(mod); |
Lucas De Marchi | d8a6c0c | 2012-01-01 06:07:46 -0200 | [diff] [blame] | 392 | |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 393 | if (usage > 0) { |
Lucas De Marchi | d8a6c0c | 2012-01-01 06:07:46 -0200 | [diff] [blame] | 394 | if (!quiet_inuse) |
| 395 | LOG("Module %s is in use.\n", modname); |
| 396 | |
Gustavo Sverzut Barbieri | e793f1e | 2011-12-16 22:35:28 -0200 | [diff] [blame] | 397 | err = -EBUSY; |
Gustavo Sverzut Barbieri | e793f1e | 2011-12-16 22:35:28 -0200 | [diff] [blame] | 398 | goto error; |
| 399 | } |
| 400 | } |
| 401 | |
Lucas De Marchi | a872bba | 2012-01-12 15:23:51 -0200 | [diff] [blame] | 402 | if (cmd == NULL) |
| 403 | err = rmmod_do_remove_module(mod); |
| 404 | else |
| 405 | err = command_do(mod, "remove", cmd, NULL); |
Lucas De Marchi | d8a6c0c | 2012-01-01 06:07:46 -0200 | [diff] [blame] | 406 | |
Lucas De Marchi | a872bba | 2012-01-12 15:23:51 -0200 | [diff] [blame] | 407 | if (err < 0) |
| 408 | goto error; |
Lucas De Marchi | d8a6c0c | 2012-01-01 06:07:46 -0200 | [diff] [blame] | 409 | |
Lucas De Marchi | a872bba | 2012-01-12 15:23:51 -0200 | [diff] [blame] | 410 | rmmod_do_deps_list(pre, false); |
Dave Reisner | 0e9bd2d | 2011-12-31 18:02:45 -0500 | [diff] [blame] | 411 | |
Gustavo Sverzut Barbieri | e793f1e | 2011-12-16 22:35:28 -0200 | [diff] [blame] | 412 | error: |
| 413 | kmod_module_unref_list(pre); |
| 414 | kmod_module_unref_list(post); |
Lucas De Marchi | a872bba | 2012-01-12 15:23:51 -0200 | [diff] [blame] | 415 | |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 416 | return err; |
| 417 | } |
| 418 | |
Lucas De Marchi | 569f160 | 2012-01-21 02:45:06 -0200 | [diff] [blame] | 419 | static int rmmod(struct kmod_ctx *ctx, const char *alias) |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 420 | { |
| 421 | struct kmod_list *l, *list = NULL; |
| 422 | int err; |
| 423 | |
| 424 | err = kmod_module_new_from_lookup(ctx, alias, &list); |
Lucas De Marchi | e5e2a68 | 2011-12-19 02:26:34 -0200 | [diff] [blame] | 425 | if (err < 0) |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 426 | return err; |
Lucas De Marchi | e5e2a68 | 2011-12-19 02:26:34 -0200 | [diff] [blame] | 427 | |
Dave Reisner | f758caf | 2012-03-14 22:15:21 -0400 | [diff] [blame] | 428 | if (list == NULL) { |
Lucas De Marchi | e5e2a68 | 2011-12-19 02:26:34 -0200 | [diff] [blame] | 429 | LOG("Module %s not found.\n", alias); |
Dave Reisner | f758caf | 2012-03-14 22:15:21 -0400 | [diff] [blame] | 430 | err = -ENOENT; |
| 431 | } |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 432 | |
| 433 | kmod_list_foreach(l, list) { |
| 434 | struct kmod_module *mod = kmod_module_get_module(l); |
Lucas De Marchi | a872bba | 2012-01-12 15:23:51 -0200 | [diff] [blame] | 435 | err = rmmod_do_module(mod, true); |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 436 | kmod_module_unref(mod); |
| 437 | if (err < 0) |
| 438 | break; |
| 439 | } |
| 440 | |
| 441 | kmod_module_unref_list(list); |
| 442 | return err; |
| 443 | } |
| 444 | |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 445 | static int rmmod_all(struct kmod_ctx *ctx, char **args, int nargs) |
| 446 | { |
| 447 | int i, err = 0; |
| 448 | |
| 449 | for (i = 0; i < nargs; i++) { |
| 450 | int r = rmmod(ctx, args[i]); |
| 451 | if (r < 0) |
| 452 | err = r; |
| 453 | } |
| 454 | |
| 455 | return err; |
| 456 | } |
| 457 | |
Lucas De Marchi | 2e9dcd7 | 2012-01-30 19:01:24 -0200 | [diff] [blame] | 458 | static void print_action(struct kmod_module *m, bool install, |
| 459 | const char *options) |
| 460 | { |
Lucas De Marchi | 4744ebc | 2012-03-15 00:14:35 -0300 | [diff] [blame] | 461 | const char *path; |
| 462 | |
| 463 | if (install) { |
Lucas De Marchi | 2e9dcd7 | 2012-01-30 19:01:24 -0200 | [diff] [blame] | 464 | printf("install %s %s\n", kmod_module_get_install_commands(m), |
| 465 | options); |
Lucas De Marchi | 4744ebc | 2012-03-15 00:14:35 -0300 | [diff] [blame] | 466 | return; |
| 467 | } |
| 468 | |
| 469 | path = kmod_module_get_path(m); |
| 470 | |
| 471 | if (path == NULL) { |
Lucas De Marchi | ace742f | 2013-03-21 02:33:25 -0300 | [diff] [blame] | 472 | /* |
| 473 | * Either a builtin module, or an alias, print only for |
| 474 | * builtin |
| 475 | */ |
| 476 | if (kmod_module_get_initstate(m) == KMOD_MODULE_BUILTIN) |
| 477 | printf("builtin %s\n", kmod_module_get_name(m)); |
Lucas De Marchi | 4744ebc | 2012-03-15 00:14:35 -0300 | [diff] [blame] | 478 | } else |
| 479 | printf("insmod %s %s\n", kmod_module_get_path(m), options); |
Lucas De Marchi | 2e9dcd7 | 2012-01-30 19:01:24 -0200 | [diff] [blame] | 480 | } |
| 481 | |
Lucas De Marchi | 569f160 | 2012-01-21 02:45:06 -0200 | [diff] [blame] | 482 | static int insmod(struct kmod_ctx *ctx, const char *alias, |
Lucas De Marchi | 8f19221 | 2012-01-11 15:38:50 -0200 | [diff] [blame] | 483 | const char *extra_options) |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 484 | { |
| 485 | struct kmod_list *l, *list = NULL; |
Lucas De Marchi | 2e9dcd7 | 2012-01-30 19:01:24 -0200 | [diff] [blame] | 486 | int err, flags = 0; |
| 487 | |
| 488 | void (*show)(struct kmod_module *m, bool install, |
| 489 | const char *options) = NULL; |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 490 | |
| 491 | err = kmod_module_new_from_lookup(ctx, alias, &list); |
Lucas De Marchi | e5e2a68 | 2011-12-19 02:26:34 -0200 | [diff] [blame] | 492 | |
Laura Abbott | b87d01d | 2015-09-28 15:39:11 -0700 | [diff] [blame] | 493 | if (list == NULL || err < 0) { |
| 494 | LOG("Module %s not found in directory %s\n", alias, |
| 495 | ctx ? kmod_get_dirname(ctx) : "(missing)"); |
Lucas De Marchi | e2719b3 | 2015-02-27 15:39:36 -0300 | [diff] [blame] | 496 | return -ENOENT; |
| 497 | } |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 498 | |
Lucas De Marchi | 2e9dcd7 | 2012-01-30 19:01:24 -0200 | [diff] [blame] | 499 | if (strip_modversion || force) |
| 500 | flags |= KMOD_PROBE_FORCE_MODVERSION; |
| 501 | if (strip_vermagic || force) |
| 502 | flags |= KMOD_PROBE_FORCE_VERMAGIC; |
| 503 | if (ignore_commands) |
| 504 | flags |= KMOD_PROBE_IGNORE_COMMAND; |
| 505 | if (ignore_loaded) |
| 506 | flags |= KMOD_PROBE_IGNORE_LOADED; |
| 507 | if (dry_run) |
| 508 | flags |= KMOD_PROBE_DRY_RUN; |
| 509 | if (do_show || verbose > DEFAULT_VERBOSE) |
| 510 | show = &print_action; |
| 511 | |
Lucas De Marchi | 36ddee6 | 2012-08-17 09:42:47 -0300 | [diff] [blame] | 512 | flags |= KMOD_PROBE_APPLY_BLACKLIST_ALIAS_ONLY; |
Lucas De Marchi | 2e9dcd7 | 2012-01-30 19:01:24 -0200 | [diff] [blame] | 513 | |
| 514 | if (use_blacklist) |
| 515 | flags |= KMOD_PROBE_APPLY_BLACKLIST; |
| 516 | if (first_time) |
Lucas De Marchi | 814a57b | 2012-02-06 12:46:39 -0200 | [diff] [blame] | 517 | flags |= KMOD_PROBE_FAIL_ON_LOADED; |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 518 | |
| 519 | kmod_list_foreach(l, list) { |
| 520 | struct kmod_module *mod = kmod_module_get_module(l); |
Lucas De Marchi | 8b01376 | 2012-01-12 17:14:30 -0200 | [diff] [blame] | 521 | |
Lucas De Marchi | 8122985 | 2011-12-16 02:58:48 -0200 | [diff] [blame] | 522 | if (lookup_only) |
| 523 | printf("%s\n", kmod_module_get_name(mod)); |
Lucas De Marchi | 8b01376 | 2012-01-12 17:14:30 -0200 | [diff] [blame] | 524 | else { |
Lucas De Marchi | 2e9dcd7 | 2012-01-30 19:01:24 -0200 | [diff] [blame] | 525 | err = kmod_module_probe_insert_module(mod, flags, |
| 526 | extra_options, NULL, NULL, show); |
Lucas De Marchi | 8b01376 | 2012-01-12 17:14:30 -0200 | [diff] [blame] | 527 | } |
Lucas De Marchi | 2e9dcd7 | 2012-01-30 19:01:24 -0200 | [diff] [blame] | 528 | |
Dave Reisner | 297a318 | 2012-01-30 20:57:36 -0500 | [diff] [blame] | 529 | if (err >= 0) |
| 530 | /* ignore flag return values such as a mod being blacklisted */ |
| 531 | err = 0; |
| 532 | else { |
| 533 | switch (err) { |
| 534 | case -EEXIST: |
| 535 | ERR("could not insert '%s': Module already in kernel\n", |
| 536 | kmod_module_get_name(mod)); |
| 537 | break; |
Dave Reisner | ccb64d1 | 2012-04-16 10:37:32 -0400 | [diff] [blame] | 538 | case -ENOENT: |
| 539 | ERR("could not insert '%s': Unknown symbol in module, " |
| 540 | "or unknown parameter (see dmesg)\n", |
| 541 | kmod_module_get_name(mod)); |
| 542 | break; |
Dave Reisner | 297a318 | 2012-01-30 20:57:36 -0500 | [diff] [blame] | 543 | default: |
| 544 | ERR("could not insert '%s': %s\n", |
| 545 | kmod_module_get_name(mod), |
| 546 | strerror(-err)); |
| 547 | break; |
| 548 | } |
Lucas De Marchi | 2e9dcd7 | 2012-01-30 19:01:24 -0200 | [diff] [blame] | 549 | } |
| 550 | |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 551 | kmod_module_unref(mod); |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | kmod_module_unref_list(list); |
| 555 | return err; |
| 556 | } |
| 557 | |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 558 | static int insmod_all(struct kmod_ctx *ctx, char **args, int nargs) |
| 559 | { |
| 560 | int i, err = 0; |
| 561 | |
| 562 | for (i = 0; i < nargs; i++) { |
| 563 | int r = insmod(ctx, args[i], NULL); |
| 564 | if (r < 0) |
| 565 | err = r; |
| 566 | } |
| 567 | |
| 568 | return err; |
| 569 | } |
| 570 | |
| 571 | static void env_modprobe_options_append(const char *value) |
| 572 | { |
| 573 | const char *old = getenv("MODPROBE_OPTIONS"); |
| 574 | char *env; |
| 575 | |
| 576 | if (old == NULL) { |
| 577 | setenv("MODPROBE_OPTIONS", value, 1); |
| 578 | return; |
| 579 | } |
| 580 | |
| 581 | if (asprintf(&env, "%s %s", old, value) < 0) { |
| 582 | ERR("could not append value to $MODPROBE_OPTIONS\n"); |
| 583 | return; |
| 584 | } |
| 585 | |
| 586 | if (setenv("MODPROBE_OPTIONS", env, 1) < 0) |
| 587 | ERR("could not setenv(MODPROBE_OPTIONS, \"%s\")\n", env); |
| 588 | free(env); |
| 589 | } |
| 590 | |
| 591 | static int options_from_array(char **args, int nargs, char **output) |
| 592 | { |
| 593 | char *opts = NULL; |
| 594 | size_t optslen = 0; |
| 595 | int i, err = 0; |
| 596 | |
| 597 | for (i = 1; i < nargs; i++) { |
| 598 | size_t len = strlen(args[i]); |
| 599 | size_t qlen = 0; |
| 600 | const char *value; |
| 601 | void *tmp; |
| 602 | |
| 603 | value = strchr(args[i], '='); |
| 604 | if (value) { |
| 605 | value++; |
| 606 | if (*value != '"' && *value != '\'') { |
| 607 | if (strchr(value, ' ')) |
| 608 | qlen = 2; |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | tmp = realloc(opts, optslen + len + qlen + 2); |
| 613 | if (!tmp) { |
| 614 | err = -errno; |
| 615 | free(opts); |
| 616 | opts = NULL; |
| 617 | ERR("could not gather module options: out-of-memory\n"); |
| 618 | break; |
| 619 | } |
| 620 | opts = tmp; |
| 621 | if (optslen > 0) { |
| 622 | opts[optslen] = ' '; |
| 623 | optslen++; |
| 624 | } |
| 625 | if (qlen == 0) { |
| 626 | memcpy(opts + optslen, args[i], len + 1); |
| 627 | optslen += len; |
| 628 | } else { |
| 629 | size_t keylen = value - args[i]; |
| 630 | size_t valuelen = len - keylen; |
| 631 | memcpy(opts + optslen, args[i], keylen); |
| 632 | optslen += keylen; |
| 633 | opts[optslen] = '"'; |
| 634 | optslen++; |
| 635 | memcpy(opts + optslen, value, valuelen); |
| 636 | optslen += valuelen; |
| 637 | opts[optslen] = '"'; |
| 638 | optslen++; |
| 639 | opts[optslen] = '\0'; |
| 640 | } |
| 641 | } |
| 642 | |
| 643 | *output = opts; |
| 644 | return err; |
| 645 | } |
| 646 | |
| 647 | static char **prepend_options_from_env(int *p_argc, char **orig_argv) |
| 648 | { |
| 649 | const char *p, *env = getenv("MODPROBE_OPTIONS"); |
| 650 | char **new_argv, *str_start, *str_end, *str, *s, *quote; |
| 651 | int i, argc = *p_argc; |
| 652 | size_t envlen, space_count = 0; |
| 653 | |
| 654 | if (env == NULL) |
| 655 | return orig_argv; |
| 656 | |
| 657 | for (p = env; *p != '\0'; p++) { |
| 658 | if (*p == ' ') |
| 659 | space_count++; |
| 660 | } |
| 661 | |
| 662 | envlen = p - env; |
| 663 | new_argv = malloc(sizeof(char *) * (argc + space_count + 3 + envlen)); |
| 664 | if (new_argv == NULL) |
| 665 | return NULL; |
| 666 | |
| 667 | new_argv[0] = orig_argv[0]; |
| 668 | str_start = str = (char *) (new_argv + argc + space_count + 3); |
| 669 | memcpy(str, env, envlen + 1); |
| 670 | |
| 671 | str_end = str_start + envlen; |
| 672 | |
| 673 | quote = NULL; |
| 674 | for (i = 1, s = str; *s != '\0'; s++) { |
| 675 | if (quote == NULL) { |
| 676 | if (*s == ' ') { |
| 677 | new_argv[i] = str; |
| 678 | i++; |
| 679 | *s = '\0'; |
| 680 | str = s + 1; |
| 681 | } else if (*s == '"' || *s == '\'') |
| 682 | quote = s; |
| 683 | } else { |
| 684 | if (*s == *quote) { |
| 685 | if (quote == str) { |
| 686 | new_argv[i] = str + 1; |
| 687 | i++; |
| 688 | *s = '\0'; |
| 689 | str = s + 1; |
| 690 | } else { |
| 691 | char *it; |
| 692 | for (it = quote; it < s - 1; it++) |
| 693 | it[0] = it[1]; |
| 694 | for (it = s - 1; it < str_end - 2; it++) |
| 695 | it[0] = it[2]; |
| 696 | str_end -= 2; |
| 697 | *str_end = '\0'; |
| 698 | s -= 2; |
| 699 | } |
| 700 | quote = NULL; |
| 701 | } |
| 702 | } |
| 703 | } |
| 704 | if (str < s) { |
| 705 | new_argv[i] = str; |
| 706 | i++; |
| 707 | } |
| 708 | |
| 709 | memcpy(new_argv + i, orig_argv + 1, sizeof(char *) * (argc - 1)); |
| 710 | new_argv[i + argc] = NULL; |
| 711 | *p_argc = i + argc - 1; |
| 712 | |
| 713 | return new_argv; |
| 714 | } |
| 715 | |
Lucas De Marchi | fa29c0e | 2011-12-22 03:54:46 -0200 | [diff] [blame] | 716 | static int do_modprobe(int argc, char **orig_argv) |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 717 | { |
| 718 | struct kmod_ctx *ctx; |
| 719 | char **args = NULL, **argv; |
Gustavo Sverzut Barbieri | cb8d4d3 | 2011-12-11 20:37:01 -0200 | [diff] [blame] | 720 | const char **config_paths = NULL; |
| 721 | int nargs = 0, n_config_paths = 0; |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 722 | char dirname_buf[PATH_MAX]; |
| 723 | const char *dirname = NULL; |
| 724 | const char *root = NULL; |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 725 | const char *kversion = NULL; |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 726 | int use_all = 0; |
| 727 | int do_remove = 0; |
| 728 | int do_show_config = 0; |
| 729 | int do_show_modversions = 0; |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 730 | int err; |
| 731 | |
| 732 | argv = prepend_options_from_env(&argc, orig_argv); |
| 733 | if (argv == NULL) { |
Lucas De Marchi | d9a2e15 | 2012-11-01 13:02:10 -0200 | [diff] [blame] | 734 | ERR("Could not prepend options from command line\n"); |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 735 | return EXIT_FAILURE; |
| 736 | } |
| 737 | |
| 738 | for (;;) { |
| 739 | int c, idx = 0; |
| 740 | c = getopt_long(argc, argv, cmdopts_s, cmdopts, &idx); |
| 741 | if (c == -1) |
| 742 | break; |
| 743 | switch (c) { |
| 744 | case 'a': |
| 745 | log_priority = LOG_WARNING; |
| 746 | use_all = 1; |
| 747 | break; |
| 748 | case 'r': |
| 749 | do_remove = 1; |
| 750 | break; |
| 751 | case 5: |
| 752 | remove_dependencies = 1; |
| 753 | break; |
| 754 | case 'R': |
Lucas De Marchi | 8122985 | 2011-12-16 02:58:48 -0200 | [diff] [blame] | 755 | lookup_only = 1; |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 756 | break; |
| 757 | case 3: |
| 758 | first_time = 1; |
| 759 | break; |
| 760 | case 'i': |
| 761 | ignore_commands = 1; |
| 762 | break; |
| 763 | case 'b': |
| 764 | use_blacklist = 1; |
| 765 | break; |
| 766 | case 'f': |
| 767 | force = 1; |
| 768 | break; |
| 769 | case 2: |
| 770 | strip_modversion = 1; |
| 771 | break; |
| 772 | case 1: |
| 773 | strip_vermagic = 1; |
| 774 | break; |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 775 | case 'D': |
| 776 | ignore_loaded = 1; |
| 777 | dry_run = 1; |
Gustavo Sverzut Barbieri | 525fa07 | 2012-01-08 13:58:28 -0200 | [diff] [blame] | 778 | do_show = 1; |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 779 | break; |
| 780 | case 'c': |
| 781 | do_show_config = 1; |
| 782 | break; |
| 783 | case 4: |
| 784 | do_show_modversions = 1; |
| 785 | break; |
| 786 | case 'n': |
| 787 | dry_run = 1; |
| 788 | break; |
Gustavo Sverzut Barbieri | cb8d4d3 | 2011-12-11 20:37:01 -0200 | [diff] [blame] | 789 | case 'C': { |
| 790 | size_t bytes = sizeof(char *) * (n_config_paths + 2); |
| 791 | void *tmp = realloc(config_paths, bytes); |
| 792 | if (!tmp) { |
Lucas De Marchi | d9a2e15 | 2012-11-01 13:02:10 -0200 | [diff] [blame] | 793 | ERR("out-of-memory\n"); |
| 794 | err = -1; |
| 795 | goto done; |
Gustavo Sverzut Barbieri | cb8d4d3 | 2011-12-11 20:37:01 -0200 | [diff] [blame] | 796 | } |
| 797 | config_paths = tmp; |
| 798 | config_paths[n_config_paths] = optarg; |
| 799 | n_config_paths++; |
| 800 | config_paths[n_config_paths] = NULL; |
| 801 | |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 802 | env_modprobe_options_append("-C"); |
| 803 | env_modprobe_options_append(optarg); |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 804 | break; |
Gustavo Sverzut Barbieri | cb8d4d3 | 2011-12-11 20:37:01 -0200 | [diff] [blame] | 805 | } |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 806 | case 'd': |
| 807 | root = optarg; |
| 808 | break; |
| 809 | case 'S': |
| 810 | kversion = optarg; |
| 811 | break; |
| 812 | case 's': |
| 813 | env_modprobe_options_append("-s"); |
| 814 | use_syslog = 1; |
| 815 | break; |
| 816 | case 'q': |
| 817 | env_modprobe_options_append("-q"); |
Lucas De Marchi | ae7ebe8 | 2012-03-15 01:11:10 -0300 | [diff] [blame] | 818 | verbose = LOG_EMERG; |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 819 | break; |
| 820 | case 'v': |
| 821 | env_modprobe_options_append("-v"); |
| 822 | verbose++; |
| 823 | break; |
| 824 | case 'V': |
| 825 | puts(PACKAGE " version " VERSION); |
Lucas De Marchi | 655de27 | 2015-06-07 02:46:22 -0300 | [diff] [blame] | 826 | puts(KMOD_FEATURES); |
Lucas De Marchi | d9a2e15 | 2012-11-01 13:02:10 -0200 | [diff] [blame] | 827 | err = 0; |
Lucas De Marchi | 4434d8b | 2012-10-31 21:29:54 -0200 | [diff] [blame] | 828 | goto done; |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 829 | case 'h': |
Lucas De Marchi | 4a2e20d | 2012-11-06 16:54:17 -0200 | [diff] [blame] | 830 | help(); |
Lucas De Marchi | d9a2e15 | 2012-11-01 13:02:10 -0200 | [diff] [blame] | 831 | err = 0; |
Lucas De Marchi | 4434d8b | 2012-10-31 21:29:54 -0200 | [diff] [blame] | 832 | goto done; |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 833 | case '?': |
Lucas De Marchi | d9a2e15 | 2012-11-01 13:02:10 -0200 | [diff] [blame] | 834 | err = -1; |
| 835 | goto done; |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 836 | default: |
Lucas De Marchi | d9a2e15 | 2012-11-01 13:02:10 -0200 | [diff] [blame] | 837 | ERR("unexpected getopt_long() value '%c'.\n", c); |
| 838 | err = -1; |
| 839 | goto done; |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 840 | } |
| 841 | } |
| 842 | |
| 843 | args = argv + optind; |
| 844 | nargs = argc - optind; |
| 845 | |
Lucas De Marchi | 92aad74 | 2012-11-06 18:04:09 -0200 | [diff] [blame] | 846 | log_open(use_syslog); |
Lucas De Marchi | d9a2e15 | 2012-11-01 13:02:10 -0200 | [diff] [blame] | 847 | |
Dave Reisner | b09668c | 2011-12-31 16:51:40 -0500 | [diff] [blame] | 848 | if (!do_show_config) { |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 849 | if (nargs == 0) { |
Lucas De Marchi | d9a2e15 | 2012-11-01 13:02:10 -0200 | [diff] [blame] | 850 | ERR("missing parameters. See -h.\n"); |
| 851 | err = -1; |
| 852 | goto done; |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 853 | } |
| 854 | } |
| 855 | |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 856 | if (root != NULL || kversion != NULL) { |
| 857 | struct utsname u; |
| 858 | if (root == NULL) |
| 859 | root = ""; |
| 860 | if (kversion == NULL) { |
| 861 | if (uname(&u) < 0) { |
Lucas De Marchi | d9a2e15 | 2012-11-01 13:02:10 -0200 | [diff] [blame] | 862 | ERR("uname() failed: %m\n"); |
| 863 | err = -1; |
| 864 | goto done; |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 865 | } |
| 866 | kversion = u.release; |
| 867 | } |
Lucas De Marchi | 8f19221 | 2012-01-11 15:38:50 -0200 | [diff] [blame] | 868 | snprintf(dirname_buf, sizeof(dirname_buf), |
Dave Reisner | c5b37db | 2012-09-27 11:00:42 -0400 | [diff] [blame] | 869 | "%s/lib/modules/%s", root, |
Lucas De Marchi | 8f19221 | 2012-01-11 15:38:50 -0200 | [diff] [blame] | 870 | kversion); |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 871 | dirname = dirname_buf; |
| 872 | } |
| 873 | |
Gustavo Sverzut Barbieri | cb8d4d3 | 2011-12-11 20:37:01 -0200 | [diff] [blame] | 874 | ctx = kmod_new(dirname, config_paths); |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 875 | if (!ctx) { |
Lucas De Marchi | d9a2e15 | 2012-11-01 13:02:10 -0200 | [diff] [blame] | 876 | ERR("kmod_new() failed!\n"); |
| 877 | err = -1; |
| 878 | goto done; |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 879 | } |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 880 | |
Lucas De Marchi | 52a50fe | 2012-11-06 18:26:34 -0200 | [diff] [blame] | 881 | log_setup_kmod_log(ctx, verbose); |
Lucas De Marchi | d9a2e15 | 2012-11-01 13:02:10 -0200 | [diff] [blame] | 882 | |
| 883 | kmod_load_resources(ctx); |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 884 | |
Dave Reisner | b09668c | 2011-12-31 16:51:40 -0500 | [diff] [blame] | 885 | if (do_show_config) |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 886 | err = show_config(ctx); |
| 887 | else if (do_show_modversions) |
| 888 | err = show_modversions(ctx, args[0]); |
| 889 | else if (do_remove) |
Lucas De Marchi | c1b8454 | 2012-03-15 00:27:18 -0300 | [diff] [blame] | 890 | err = rmmod_all(ctx, args, nargs); |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 891 | else if (use_all) |
| 892 | err = insmod_all(ctx, args, nargs); |
| 893 | else { |
| 894 | char *opts; |
| 895 | err = options_from_array(args, nargs, &opts); |
| 896 | if (err == 0) { |
| 897 | err = insmod(ctx, args[0], opts); |
| 898 | free(opts); |
| 899 | } |
| 900 | } |
| 901 | |
| 902 | kmod_unref(ctx); |
| 903 | |
Lucas De Marchi | d9a2e15 | 2012-11-01 13:02:10 -0200 | [diff] [blame] | 904 | done: |
Lucas De Marchi | 92aad74 | 2012-11-06 18:04:09 -0200 | [diff] [blame] | 905 | log_close(); |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 906 | |
| 907 | if (argv != orig_argv) |
| 908 | free(argv); |
Lucas De Marchi | 8f19221 | 2012-01-11 15:38:50 -0200 | [diff] [blame] | 909 | |
Gustavo Sverzut Barbieri | cb8d4d3 | 2011-12-11 20:37:01 -0200 | [diff] [blame] | 910 | free(config_paths); |
Lucas De Marchi | d9a2e15 | 2012-11-01 13:02:10 -0200 | [diff] [blame] | 911 | |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 912 | return err >= 0 ? EXIT_SUCCESS : EXIT_FAILURE; |
Gustavo Sverzut Barbieri | c3d0a5f | 2011-12-11 19:36:18 -0200 | [diff] [blame] | 913 | } |
Lucas De Marchi | fa29c0e | 2011-12-22 03:54:46 -0200 | [diff] [blame] | 914 | |
Lucas De Marchi | fa29c0e | 2011-12-22 03:54:46 -0200 | [diff] [blame] | 915 | const struct kmod_cmd kmod_cmd_compat_modprobe = { |
| 916 | .name = "modprobe", |
| 917 | .cmd = do_modprobe, |
| 918 | .help = "compat modprobe command", |
| 919 | }; |