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