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