Caio Marcelo de Oliveira Filho | 0376188 | 2015-03-07 10:32:53 -0300 | [diff] [blame] | 1 | /* |
| 2 | * kmod-insert - insert a module into the kernel. |
| 3 | * |
| 4 | * Copyright (C) 2015 Intel Corporation. All rights reserved. |
| 5 | * Copyright (C) 2011-2013 ProFUSION embedded systems |
| 6 | * |
| 7 | * This program is free software: you can redistribute it and/or modify |
| 8 | * it under the terms of the GNU General Public License as published by |
| 9 | * the Free Software Foundation, either version 2 of the License, or |
| 10 | * (at your option) any later version. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License |
| 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | #include <errno.h> |
| 22 | #include <getopt.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <string.h> |
| 25 | |
| 26 | #include <libkmod/libkmod.h> |
| 27 | |
| 28 | #include "kmod.h" |
| 29 | |
| 30 | static const char cmdopts_s[] = "h"; |
| 31 | static const struct option cmdopts[] = { |
| 32 | {"help", no_argument, 0, 'h'}, |
| 33 | { } |
| 34 | }; |
| 35 | |
| 36 | static void help(void) |
| 37 | { |
| 38 | printf("Usage:\n" |
| 39 | "\t%s insert [options] module\n" |
| 40 | "Options:\n" |
| 41 | "\t-h, --help show this help\n", |
| 42 | program_invocation_short_name); |
| 43 | } |
| 44 | |
| 45 | static const char *mod_strerror(int err) |
| 46 | { |
| 47 | switch (err) { |
| 48 | case KMOD_PROBE_APPLY_BLACKLIST: |
| 49 | return "Module is blacklisted"; |
| 50 | case -EEXIST: |
| 51 | return "Module already in kernel"; |
| 52 | case -ENOENT: |
| 53 | return "Unknown symbol in module or unknown parameter (see dmesg)"; |
| 54 | default: |
| 55 | return strerror(-err); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | static int do_insert(int argc, char *argv[]) |
| 60 | { |
| 61 | struct kmod_ctx *ctx; |
| 62 | struct kmod_list *list = NULL, *l; |
| 63 | const char *name; |
| 64 | int err, r = EXIT_SUCCESS; |
| 65 | |
| 66 | for (;;) { |
| 67 | int c, idx = 0; |
| 68 | c = getopt_long(argc, argv, cmdopts_s, cmdopts, &idx); |
| 69 | if (c == -1) |
| 70 | break; |
| 71 | switch (c) { |
| 72 | case 'h': |
| 73 | help(); |
| 74 | return EXIT_SUCCESS; |
| 75 | default: |
| 76 | ERR("Unexpected getopt_long() value '%c'.\n", c); |
| 77 | return EXIT_FAILURE; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | if (optind >= argc) { |
| 82 | ERR("Missing module name\n"); |
| 83 | return EXIT_FAILURE; |
| 84 | } |
| 85 | |
| 86 | ctx = kmod_new(NULL, NULL); |
| 87 | if (!ctx) { |
| 88 | ERR("kmod_new() failed!\n"); |
| 89 | return EXIT_FAILURE; |
| 90 | } |
| 91 | |
| 92 | name = argv[optind]; |
| 93 | err = kmod_module_new_from_lookup(ctx, name, &list); |
| 94 | if (err < 0) { |
| 95 | ERR("Could not lookup module matching '%s': %s\n", name, strerror(-err)); |
| 96 | r = EXIT_FAILURE; |
| 97 | goto end; |
| 98 | } |
| 99 | |
| 100 | if (list == NULL) { |
| 101 | ERR("No module matches '%s'\n", name); |
| 102 | r = EXIT_FAILURE; |
| 103 | goto end; |
| 104 | } |
| 105 | |
| 106 | kmod_list_foreach(l, list) { |
| 107 | struct kmod_module *mod = kmod_module_get_module(l); |
| 108 | |
| 109 | err = kmod_module_probe_insert_module(mod, KMOD_PROBE_APPLY_BLACKLIST, NULL, NULL, NULL, NULL); |
| 110 | if (err != 0) { |
| 111 | r = EXIT_FAILURE; |
| 112 | ERR("Could not insert '%s': %s\n", kmod_module_get_name(mod), mod_strerror(err)); |
| 113 | } |
| 114 | |
| 115 | kmod_module_unref(mod); |
| 116 | } |
| 117 | |
| 118 | kmod_module_unref_list(list); |
| 119 | end: |
| 120 | kmod_unref(ctx); |
| 121 | return r; |
| 122 | } |
| 123 | |
| 124 | const struct kmod_cmd kmod_cmd_insert = { |
| 125 | .name = "insert", |
| 126 | .cmd = do_insert, |
| 127 | .help = "insert a module into the kernel", |
| 128 | }; |