blob: 5ebe70a053c9acf73cb0bd774294a685a1ee9374 [file] [log] [blame]
Lucas De Marchi8900b912011-12-22 02:33:36 -02001/*
2 * kmod - one tool to rule them all
3 *
Lucas De Marchie6b0e492013-01-16 11:27:21 -02004 * Copyright (C) 2011-2013 ProFUSION embedded systems
Lucas De Marchi8900b912011-12-22 02:33:36 -02005 *
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 Marchi8900b912011-12-22 02:33:36 -020020#include <errno.h>
Lucas De Marchibc854322011-12-23 02:56:27 -020021#include <getopt.h>
Lucas De Marchic2e42862014-10-03 01:41:42 -030022#include <stdio.h>
23#include <stdlib.h>
Lucas De Marchi8900b912011-12-22 02:33:36 -020024#include <string.h>
Lucas De Marchic2e42862014-10-03 01:41:42 -030025
Lucas De Marchi7db094c2015-01-14 12:28:02 -020026#include <shared/util.h>
27
Lucas De Marchif3578662015-01-02 12:38:27 -020028#include <libkmod/libkmod.h>
Lucas De Marchic2e42862014-10-03 01:41:42 -030029
Lucas De Marchi8900b912011-12-22 02:33:36 -020030#include "kmod.h"
31
Lucas De Marchibc854322011-12-23 02:56:27 -020032static const char options_s[] = "+hV";
33static const struct option options[] = {
34 { "help", no_argument, NULL, 'h' },
35 { "version", no_argument, NULL, 'V' },
36 {}
37};
38
Lucas De Marchi8900b912011-12-22 02:33:36 -020039static const struct kmod_cmd kmod_cmd_help;
40
41static const struct kmod_cmd *kmod_cmds[] = {
42 &kmod_cmd_help,
Caio Marcelo de Oliveira Filho03761882015-03-07 10:32:53 -030043 &kmod_cmd_insert,
Lucas De Marchi252c51a2011-12-23 11:33:02 -020044 &kmod_cmd_list,
Caio Marcelo de Oliveira Filho03761882015-03-07 10:32:53 -030045 &kmod_cmd_remove,
Tom Gundersendb6f2fc2013-04-16 22:39:55 +020046 &kmod_cmd_static_nodes,
Lucas De Marchi8900b912011-12-22 02:33:36 -020047};
48
Lucas De Marchi6fcf69e2011-12-22 03:01:45 -020049static const struct kmod_cmd *kmod_compat_cmds[] = {
50 &kmod_cmd_compat_lsmod,
Lucas De Marchif712ebc2011-12-22 03:39:11 -020051 &kmod_cmd_compat_rmmod,
Lucas De Marchiad602692011-12-22 03:45:07 -020052 &kmod_cmd_compat_insmod,
Lucas De Marchi769becb2011-12-22 03:50:54 -020053 &kmod_cmd_compat_modinfo,
Lucas De Marchifa29c0e2011-12-22 03:54:46 -020054 &kmod_cmd_compat_modprobe,
Lucas De Marchif6cf14c2011-12-27 19:56:33 -020055 &kmod_cmd_compat_depmod,
Lucas De Marchi6fcf69e2011-12-22 03:01:45 -020056};
57
Lucas De Marchi8900b912011-12-22 02:33:36 -020058static int kmod_help(int argc, char *argv[])
59{
60 size_t i;
61
Lucas De Marchia458e362011-12-23 11:35:48 -020062 printf("kmod - Manage kernel modules: list, load, unload, etc\n"
Lucas De Marchibc854322011-12-23 02:56:27 -020063 "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 Marchi8900b912011-12-22 02:33:36 -020069
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 Marchi6fcf69e2011-12-22 03:01:45 -020077 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 Marchi35a29aa2011-12-22 19:21:11 -020085
Lucas De Marchi8900b912011-12-22 02:33:36 -020086 return EXIT_SUCCESS;
87}
88
89static const struct kmod_cmd kmod_cmd_help = {
90 .name = "help",
91 .cmd = kmod_help,
92 .help = "Show help message",
93};
94
Lucas De Marchi35a29aa2011-12-22 19:21:11 -020095static int handle_kmod_commands(int argc, char *argv[])
Lucas De Marchi8900b912011-12-22 02:33:36 -020096{
97 const char *cmd;
98 int err = 0;
99 size_t i;
100
Lucas De Marchibc854322011-12-23 02:56:27 -0200101 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 Marchi9b966da2011-12-23 11:44:28 -0200124 fputs("missing command\n", stderr);
125 goto fail;
Lucas De Marchi8900b912011-12-22 02:33:36 -0200126 }
127
Lucas De Marchibc854322011-12-23 02:56:27 -0200128 cmd = argv[optind];
Lucas De Marchi8900b912011-12-22 02:33:36 -0200129
Lucas De Marchi9b966da2011-12-23 11:44:28 -0200130 for (i = 0, err = -EINVAL; i < ARRAY_SIZE(kmod_cmds); i++) {
Lucas De Marchi7db094c2015-01-14 12:28:02 -0200131 if (streq(kmod_cmds[i]->name, cmd)) {
Caio Marcelo de Oliveira Filhoace71982015-01-14 12:02:16 -0200132 err = kmod_cmds[i]->cmd(--argc, ++argv);
133 break;
134 }
Lucas De Marchi8900b912011-12-22 02:33:36 -0200135 }
136
Lucas De Marchi8900b912011-12-22 02:33:36 -0200137 if (err < 0) {
Lucas De Marchi9b966da2011-12-23 11:44:28 -0200138 fprintf(stderr, "invalid command '%s'\n", cmd);
139 goto fail;
Lucas De Marchi8900b912011-12-22 02:33:36 -0200140 }
141
142 return err;
Lucas De Marchi9b966da2011-12-23 11:44:28 -0200143
144fail:
145 kmod_help(argc, argv);
146 return EXIT_FAILURE;
Lucas De Marchi8900b912011-12-22 02:33:36 -0200147}
Lucas De Marchi35a29aa2011-12-22 19:21:11 -0200148
Lucas De Marchi6fcf69e2011-12-22 03:01:45 -0200149
150static int handle_kmod_compat_commands(int argc, char *argv[])
151{
152 const char *cmd;
Lucas De Marchi6fcf69e2011-12-22 03:01:45 -0200153 size_t i;
154
155 cmd = basename(argv[0]);
156
157 for (i = 0; i < ARRAY_SIZE(kmod_compat_cmds); i++) {
Lucas De Marchi7db094c2015-01-14 12:28:02 -0200158 if (streq(kmod_compat_cmds[i]->name, cmd))
Leandro Pereira4783d692011-12-27 18:22:30 -0200159 return kmod_compat_cmds[i]->cmd(argc, argv);
Lucas De Marchi6fcf69e2011-12-22 03:01:45 -0200160 }
161
Leandro Pereira4783d692011-12-27 18:22:30 -0200162 return -ENOENT;
Lucas De Marchi6fcf69e2011-12-22 03:01:45 -0200163}
164
Lucas De Marchi35a29aa2011-12-22 19:21:11 -0200165int main(int argc, char *argv[])
166{
Lucas De Marchi35a29aa2011-12-22 19:21:11 -0200167 int err;
168
Lucas De Marchi7db094c2015-01-14 12:28:02 -0200169 if (streq(program_invocation_short_name, "kmod"))
Lucas De Marchi35a29aa2011-12-22 19:21:11 -0200170 err = handle_kmod_commands(argc, argv);
171 else
Lucas De Marchi6fcf69e2011-12-22 03:01:45 -0200172 err = handle_kmod_compat_commands(argc, argv);
Lucas De Marchi35a29aa2011-12-22 19:21:11 -0200173
174 return err;
175}