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