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