Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 1 | /* |
| 2 | * kmod-insmod - insert modules into linux kernel using libkmod. |
| 3 | * |
Lucas De Marchi | e6b0e49 | 2013-01-16 11:27:21 -0200 | [diff] [blame] | 4 | * Copyright (C) 2011-2013 ProFUSION embedded systems |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -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 | 72c51a9 | 2011-12-10 22:19:41 -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 | 72c51a9 | 2011-12-10 22:19:41 -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 | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 18 | */ |
Lucas De Marchi | cb451f3 | 2011-12-12 18:24:35 -0200 | [diff] [blame] | 19 | |
Lucas De Marchi | c2e4286 | 2014-10-03 01:41:42 -0300 | [diff] [blame] | 20 | #include <errno.h> |
| 21 | #include <getopt.h> |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 24 | #include <string.h> |
Lucas De Marchi | c2e4286 | 2014-10-03 01:41:42 -0300 | [diff] [blame] | 25 | |
Lucas De Marchi | 5b0436a | 2015-01-14 14:22:23 -0200 | [diff] [blame] | 26 | #include <shared/util.h> |
| 27 | |
Lucas De Marchi | f357866 | 2015-01-02 12:38:27 -0200 | [diff] [blame] | 28 | #include <libkmod/libkmod.h> |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 29 | |
Lucas De Marchi | 4a2e20d | 2012-11-06 16:54:17 -0200 | [diff] [blame] | 30 | #include "kmod.h" |
| 31 | |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 32 | static const char cmdopts_s[] = "psfVh"; |
| 33 | static const struct option cmdopts[] = { |
| 34 | {"version", no_argument, 0, 'V'}, |
| 35 | {"help", no_argument, 0, 'h'}, |
| 36 | {NULL, 0, 0, 0} |
| 37 | }; |
| 38 | |
Lucas De Marchi | 4a2e20d | 2012-11-06 16:54:17 -0200 | [diff] [blame] | 39 | static void help(void) |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 40 | { |
Lucas De Marchi | 34e06bf | 2012-11-06 17:32:41 -0200 | [diff] [blame] | 41 | printf("Usage:\n" |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 42 | "\t%s [options] filename [args]\n" |
| 43 | "Options:\n" |
| 44 | "\t-V, --version show version\n" |
| 45 | "\t-h, --help show this help\n", |
Lucas De Marchi | 7c04aee | 2012-11-06 19:20:09 -0200 | [diff] [blame] | 46 | program_invocation_short_name); |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | static const char *mod_strerror(int err) |
| 50 | { |
| 51 | switch (err) { |
| 52 | case ENOEXEC: |
| 53 | return "Invalid module format"; |
| 54 | case ENOENT: |
| 55 | return "Unknown symbol in module"; |
| 56 | case ESRCH: |
| 57 | return "Module has wrong symbol version"; |
| 58 | case EINVAL: |
| 59 | return "Invalid parameters"; |
| 60 | default: |
| 61 | return strerror(err); |
| 62 | } |
| 63 | } |
| 64 | |
Lucas De Marchi | ad60269 | 2011-12-22 03:45:07 -0200 | [diff] [blame] | 65 | static int do_insmod(int argc, char *argv[]) |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 66 | { |
| 67 | struct kmod_ctx *ctx; |
| 68 | struct kmod_module *mod; |
| 69 | const char *filename; |
| 70 | char *opts = NULL; |
| 71 | size_t optslen = 0; |
| 72 | int i, err; |
Lucas De Marchi | 2411c07 | 2011-12-12 15:41:02 -0200 | [diff] [blame] | 73 | const char *null_config = NULL; |
Philippe De Swert | 2b2a503 | 2015-10-07 18:36:58 +0300 | [diff] [blame] | 74 | unsigned int flags = 0; |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 75 | |
| 76 | for (;;) { |
| 77 | int c, idx = 0; |
| 78 | c = getopt_long(argc, argv, cmdopts_s, cmdopts, &idx); |
| 79 | if (c == -1) |
| 80 | break; |
| 81 | switch (c) { |
| 82 | case 'p': |
| 83 | case 's': |
Marc-Antoine Perennou | d64b286 | 2015-11-20 22:23:14 +0100 | [diff] [blame] | 84 | /* ignored, for compatibility only */ |
| 85 | break; |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 86 | case 'f': |
Philippe De Swert | 2b2a503 | 2015-10-07 18:36:58 +0300 | [diff] [blame] | 87 | flags |= KMOD_PROBE_FORCE_MODVERSION; |
| 88 | flags |= KMOD_PROBE_FORCE_VERMAGIC; |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 89 | break; |
| 90 | case 'h': |
Lucas De Marchi | 4a2e20d | 2012-11-06 16:54:17 -0200 | [diff] [blame] | 91 | help(); |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 92 | return EXIT_SUCCESS; |
| 93 | case 'V': |
| 94 | puts(PACKAGE " version " VERSION); |
Lucas De Marchi | 655de27 | 2015-06-07 02:46:22 -0300 | [diff] [blame] | 95 | puts(KMOD_FEATURES); |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 96 | return EXIT_SUCCESS; |
| 97 | case '?': |
| 98 | return EXIT_FAILURE; |
| 99 | default: |
Lucas De Marchi | e2f9478 | 2012-11-05 15:08:54 -0200 | [diff] [blame] | 100 | ERR("unexpected getopt_long() value '%c'.\n", |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 101 | c); |
| 102 | return EXIT_FAILURE; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | if (optind >= argc) { |
Lucas De Marchi | e2f9478 | 2012-11-05 15:08:54 -0200 | [diff] [blame] | 107 | ERR("missing filename.\n"); |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 108 | return EXIT_FAILURE; |
| 109 | } |
| 110 | |
| 111 | filename = argv[optind]; |
Lucas De Marchi | 5b0436a | 2015-01-14 14:22:23 -0200 | [diff] [blame] | 112 | if (streq(filename, "-")) { |
Lucas De Marchi | e2f9478 | 2012-11-05 15:08:54 -0200 | [diff] [blame] | 113 | ERR("this tool does not support loading from stdin!\n"); |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 114 | return EXIT_FAILURE; |
| 115 | } |
| 116 | |
| 117 | for (i = optind + 1; i < argc; i++) { |
| 118 | size_t len = strlen(argv[i]); |
| 119 | void *tmp = realloc(opts, optslen + len + 2); |
| 120 | if (tmp == NULL) { |
Lucas De Marchi | e2f9478 | 2012-11-05 15:08:54 -0200 | [diff] [blame] | 121 | ERR("out of memory\n"); |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 122 | free(opts); |
| 123 | return EXIT_FAILURE; |
| 124 | } |
| 125 | opts = tmp; |
| 126 | if (optslen > 0) { |
| 127 | opts[optslen] = ' '; |
| 128 | optslen++; |
| 129 | } |
| 130 | memcpy(opts + optslen, argv[i], len); |
| 131 | optslen += len; |
| 132 | opts[optslen] = '\0'; |
| 133 | } |
| 134 | |
Lucas De Marchi | 2411c07 | 2011-12-12 15:41:02 -0200 | [diff] [blame] | 135 | ctx = kmod_new(NULL, &null_config); |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 136 | if (!ctx) { |
Lucas De Marchi | e2f9478 | 2012-11-05 15:08:54 -0200 | [diff] [blame] | 137 | ERR("kmod_new() failed!\n"); |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 138 | free(opts); |
| 139 | return EXIT_FAILURE; |
| 140 | } |
| 141 | |
| 142 | err = kmod_module_new_from_path(ctx, filename, &mod); |
| 143 | if (err < 0) { |
Lucas De Marchi | e2f9478 | 2012-11-05 15:08:54 -0200 | [diff] [blame] | 144 | ERR("could not load module %s: %s\n", filename, |
| 145 | strerror(-err)); |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 146 | goto end; |
| 147 | } |
| 148 | |
Philippe De Swert | 2b2a503 | 2015-10-07 18:36:58 +0300 | [diff] [blame] | 149 | err = kmod_module_insert_module(mod, flags, opts); |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 150 | if (err < 0) { |
Lucas De Marchi | e2f9478 | 2012-11-05 15:08:54 -0200 | [diff] [blame] | 151 | ERR("could not insert module %s: %s\n", filename, |
| 152 | mod_strerror(-err)); |
Gustavo Sverzut Barbieri | 72c51a9 | 2011-12-10 22:19:41 -0200 | [diff] [blame] | 153 | } |
| 154 | kmod_module_unref(mod); |
| 155 | |
| 156 | end: |
| 157 | kmod_unref(ctx); |
| 158 | free(opts); |
| 159 | return err >= 0 ? EXIT_SUCCESS : EXIT_FAILURE; |
| 160 | } |
Lucas De Marchi | ad60269 | 2011-12-22 03:45:07 -0200 | [diff] [blame] | 161 | |
Lucas De Marchi | ad60269 | 2011-12-22 03:45:07 -0200 | [diff] [blame] | 162 | const struct kmod_cmd kmod_cmd_compat_insmod = { |
| 163 | .name = "insmod", |
| 164 | .cmd = do_insmod, |
| 165 | .help = "compat insmod command", |
| 166 | }; |