Lucas De Marchi | 8900b91 | 2011-12-22 02:33:36 -0200 | [diff] [blame] | 1 | /* |
| 2 | * kmod - one tool to rule them all |
| 3 | * |
Lucas De Marchi | e6b0e49 | 2013-01-16 11:27:21 -0200 | [diff] [blame] | 4 | * Copyright (C) 2011-2013 ProFUSION embedded systems |
Lucas De Marchi | 8900b91 | 2011-12-22 02:33:36 -0200 | [diff] [blame] | 5 | * |
| 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. |
| 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 |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 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/>. |
| 18 | */ |
| 19 | |
Lucas De Marchi | 8900b91 | 2011-12-22 02:33:36 -0200 | [diff] [blame] | 20 | #include <errno.h> |
Lucas De Marchi | bc85432 | 2011-12-23 02:56:27 -0200 | [diff] [blame] | 21 | #include <getopt.h> |
Lucas De Marchi | c2e4286 | 2014-10-03 01:41:42 -0300 | [diff] [blame] | 22 | #include <stdio.h> |
| 23 | #include <stdlib.h> |
Lucas De Marchi | 8900b91 | 2011-12-22 02:33:36 -0200 | [diff] [blame] | 24 | #include <string.h> |
Lucas De Marchi | c2e4286 | 2014-10-03 01:41:42 -0300 | [diff] [blame] | 25 | |
Lucas De Marchi | 7db094c | 2015-01-14 12:28:02 -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> |
Lucas De Marchi | c2e4286 | 2014-10-03 01:41:42 -0300 | [diff] [blame] | 29 | |
Lucas De Marchi | 8900b91 | 2011-12-22 02:33:36 -0200 | [diff] [blame] | 30 | #include "kmod.h" |
| 31 | |
Lucas De Marchi | bc85432 | 2011-12-23 02:56:27 -0200 | [diff] [blame] | 32 | static const char options_s[] = "+hV"; |
| 33 | static const struct option options[] = { |
| 34 | { "help", no_argument, NULL, 'h' }, |
| 35 | { "version", no_argument, NULL, 'V' }, |
| 36 | {} |
| 37 | }; |
| 38 | |
Lucas De Marchi | 8900b91 | 2011-12-22 02:33:36 -0200 | [diff] [blame] | 39 | static const struct kmod_cmd kmod_cmd_help; |
| 40 | |
| 41 | static const struct kmod_cmd *kmod_cmds[] = { |
| 42 | &kmod_cmd_help, |
Lucas De Marchi | 252c51a | 2011-12-23 11:33:02 -0200 | [diff] [blame] | 43 | &kmod_cmd_list, |
Tom Gundersen | db6f2fc | 2013-04-16 22:39:55 +0200 | [diff] [blame] | 44 | &kmod_cmd_static_nodes, |
Lucas De Marchi | 013e855 | 2015-06-06 23:26:31 -0300 | [diff] [blame] | 45 | |
| 46 | #ifdef ENABLE_EXPERIMENTAL |
| 47 | &kmod_cmd_insert, |
| 48 | &kmod_cmd_remove, |
| 49 | #endif |
Lucas De Marchi | 8900b91 | 2011-12-22 02:33:36 -0200 | [diff] [blame] | 50 | }; |
| 51 | |
Lucas De Marchi | 6fcf69e | 2011-12-22 03:01:45 -0200 | [diff] [blame] | 52 | static const struct kmod_cmd *kmod_compat_cmds[] = { |
| 53 | &kmod_cmd_compat_lsmod, |
Lucas De Marchi | f712ebc | 2011-12-22 03:39:11 -0200 | [diff] [blame] | 54 | &kmod_cmd_compat_rmmod, |
Lucas De Marchi | ad60269 | 2011-12-22 03:45:07 -0200 | [diff] [blame] | 55 | &kmod_cmd_compat_insmod, |
Lucas De Marchi | 769becb | 2011-12-22 03:50:54 -0200 | [diff] [blame] | 56 | &kmod_cmd_compat_modinfo, |
Lucas De Marchi | fa29c0e | 2011-12-22 03:54:46 -0200 | [diff] [blame] | 57 | &kmod_cmd_compat_modprobe, |
Lucas De Marchi | f6cf14c | 2011-12-27 19:56:33 -0200 | [diff] [blame] | 58 | &kmod_cmd_compat_depmod, |
Lucas De Marchi | 6fcf69e | 2011-12-22 03:01:45 -0200 | [diff] [blame] | 59 | }; |
| 60 | |
Lucas De Marchi | 8900b91 | 2011-12-22 02:33:36 -0200 | [diff] [blame] | 61 | static int kmod_help(int argc, char *argv[]) |
| 62 | { |
| 63 | size_t i; |
| 64 | |
Lucas De Marchi | a458e36 | 2011-12-23 11:35:48 -0200 | [diff] [blame] | 65 | printf("kmod - Manage kernel modules: list, load, unload, etc\n" |
Lucas De Marchi | bc85432 | 2011-12-23 02:56:27 -0200 | [diff] [blame] | 66 | "Usage:\n" |
| 67 | "\t%s [options] command [command_options]\n\n" |
| 68 | "Options:\n" |
| 69 | "\t-V, --version show version\n" |
| 70 | "\t-h, --help show this help\n\n" |
| 71 | "Commands:\n", basename(argv[0])); |
Lucas De Marchi | 8900b91 | 2011-12-22 02:33:36 -0200 | [diff] [blame] | 72 | |
| 73 | for (i = 0; i < ARRAY_SIZE(kmod_cmds); i++) { |
| 74 | if (kmod_cmds[i]->help != NULL) { |
| 75 | printf(" %-12s %s\n", kmod_cmds[i]->name, |
| 76 | kmod_cmds[i]->help); |
| 77 | } |
| 78 | } |
| 79 | |
Lucas De Marchi | 6fcf69e | 2011-12-22 03:01:45 -0200 | [diff] [blame] | 80 | puts("\nkmod also handles gracefully if called from following symlinks:"); |
| 81 | |
| 82 | for (i = 0; i < ARRAY_SIZE(kmod_compat_cmds); i++) { |
| 83 | if (kmod_compat_cmds[i]->help != NULL) { |
| 84 | printf(" %-12s %s\n", kmod_compat_cmds[i]->name, |
| 85 | kmod_compat_cmds[i]->help); |
| 86 | } |
| 87 | } |
Lucas De Marchi | 35a29aa | 2011-12-22 19:21:11 -0200 | [diff] [blame] | 88 | |
Lucas De Marchi | 8900b91 | 2011-12-22 02:33:36 -0200 | [diff] [blame] | 89 | return EXIT_SUCCESS; |
| 90 | } |
| 91 | |
| 92 | static const struct kmod_cmd kmod_cmd_help = { |
| 93 | .name = "help", |
| 94 | .cmd = kmod_help, |
| 95 | .help = "Show help message", |
| 96 | }; |
| 97 | |
Lucas De Marchi | 35a29aa | 2011-12-22 19:21:11 -0200 | [diff] [blame] | 98 | static int handle_kmod_commands(int argc, char *argv[]) |
Lucas De Marchi | 8900b91 | 2011-12-22 02:33:36 -0200 | [diff] [blame] | 99 | { |
| 100 | const char *cmd; |
| 101 | int err = 0; |
| 102 | size_t i; |
| 103 | |
Lucas De Marchi | bc85432 | 2011-12-23 02:56:27 -0200 | [diff] [blame] | 104 | for (;;) { |
| 105 | int c; |
| 106 | |
| 107 | c = getopt_long(argc, argv, options_s, options, NULL); |
| 108 | if (c == -1) |
| 109 | break; |
| 110 | |
| 111 | switch (c) { |
| 112 | case 'h': |
| 113 | kmod_help(argc, argv); |
| 114 | return EXIT_SUCCESS; |
| 115 | case 'V': |
Lucas De Marchi | b149909 | 2015-06-08 21:30:11 -0300 | [diff] [blame] | 116 | puts(PACKAGE " version " VERSION); |
Lucas De Marchi | 655de27 | 2015-06-07 02:46:22 -0300 | [diff] [blame] | 117 | puts(KMOD_FEATURES); |
Lucas De Marchi | bc85432 | 2011-12-23 02:56:27 -0200 | [diff] [blame] | 118 | return EXIT_SUCCESS; |
| 119 | case '?': |
| 120 | return EXIT_FAILURE; |
| 121 | default: |
| 122 | fprintf(stderr, "Error: unexpected getopt_long() value '%c'.\n", c); |
| 123 | return EXIT_FAILURE; |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | if (optind >= argc) { |
Lucas De Marchi | 9b966da | 2011-12-23 11:44:28 -0200 | [diff] [blame] | 128 | fputs("missing command\n", stderr); |
| 129 | goto fail; |
Lucas De Marchi | 8900b91 | 2011-12-22 02:33:36 -0200 | [diff] [blame] | 130 | } |
| 131 | |
Lucas De Marchi | bc85432 | 2011-12-23 02:56:27 -0200 | [diff] [blame] | 132 | cmd = argv[optind]; |
Lucas De Marchi | 8900b91 | 2011-12-22 02:33:36 -0200 | [diff] [blame] | 133 | |
Lucas De Marchi | 9b966da | 2011-12-23 11:44:28 -0200 | [diff] [blame] | 134 | for (i = 0, err = -EINVAL; i < ARRAY_SIZE(kmod_cmds); i++) { |
Lucas De Marchi | 7db094c | 2015-01-14 12:28:02 -0200 | [diff] [blame] | 135 | if (streq(kmod_cmds[i]->name, cmd)) { |
Caio Marcelo de Oliveira Filho | ace7198 | 2015-01-14 12:02:16 -0200 | [diff] [blame] | 136 | err = kmod_cmds[i]->cmd(--argc, ++argv); |
| 137 | break; |
| 138 | } |
Lucas De Marchi | 8900b91 | 2011-12-22 02:33:36 -0200 | [diff] [blame] | 139 | } |
| 140 | |
Lucas De Marchi | 8900b91 | 2011-12-22 02:33:36 -0200 | [diff] [blame] | 141 | if (err < 0) { |
Lucas De Marchi | 9b966da | 2011-12-23 11:44:28 -0200 | [diff] [blame] | 142 | fprintf(stderr, "invalid command '%s'\n", cmd); |
| 143 | goto fail; |
Lucas De Marchi | 8900b91 | 2011-12-22 02:33:36 -0200 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | return err; |
Lucas De Marchi | 9b966da | 2011-12-23 11:44:28 -0200 | [diff] [blame] | 147 | |
| 148 | fail: |
| 149 | kmod_help(argc, argv); |
| 150 | return EXIT_FAILURE; |
Lucas De Marchi | 8900b91 | 2011-12-22 02:33:36 -0200 | [diff] [blame] | 151 | } |
Lucas De Marchi | 35a29aa | 2011-12-22 19:21:11 -0200 | [diff] [blame] | 152 | |
Lucas De Marchi | 6fcf69e | 2011-12-22 03:01:45 -0200 | [diff] [blame] | 153 | |
| 154 | static int handle_kmod_compat_commands(int argc, char *argv[]) |
| 155 | { |
| 156 | const char *cmd; |
Lucas De Marchi | 6fcf69e | 2011-12-22 03:01:45 -0200 | [diff] [blame] | 157 | size_t i; |
| 158 | |
| 159 | cmd = basename(argv[0]); |
| 160 | |
| 161 | for (i = 0; i < ARRAY_SIZE(kmod_compat_cmds); i++) { |
Lucas De Marchi | 7db094c | 2015-01-14 12:28:02 -0200 | [diff] [blame] | 162 | if (streq(kmod_compat_cmds[i]->name, cmd)) |
Leandro Pereira | 4783d69 | 2011-12-27 18:22:30 -0200 | [diff] [blame] | 163 | return kmod_compat_cmds[i]->cmd(argc, argv); |
Lucas De Marchi | 6fcf69e | 2011-12-22 03:01:45 -0200 | [diff] [blame] | 164 | } |
| 165 | |
Leandro Pereira | 4783d69 | 2011-12-27 18:22:30 -0200 | [diff] [blame] | 166 | return -ENOENT; |
Lucas De Marchi | 6fcf69e | 2011-12-22 03:01:45 -0200 | [diff] [blame] | 167 | } |
| 168 | |
Lucas De Marchi | 35a29aa | 2011-12-22 19:21:11 -0200 | [diff] [blame] | 169 | int main(int argc, char *argv[]) |
| 170 | { |
Lucas De Marchi | 35a29aa | 2011-12-22 19:21:11 -0200 | [diff] [blame] | 171 | int err; |
| 172 | |
Lucas De Marchi | 7db094c | 2015-01-14 12:28:02 -0200 | [diff] [blame] | 173 | if (streq(program_invocation_short_name, "kmod")) |
Lucas De Marchi | 35a29aa | 2011-12-22 19:21:11 -0200 | [diff] [blame] | 174 | err = handle_kmod_commands(argc, argv); |
| 175 | else |
Lucas De Marchi | 6fcf69e | 2011-12-22 03:01:45 -0200 | [diff] [blame] | 176 | err = handle_kmod_compat_commands(argc, argv); |
Lucas De Marchi | 35a29aa | 2011-12-22 19:21:11 -0200 | [diff] [blame] | 177 | |
| 178 | return err; |
| 179 | } |