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