1 // SPDX-License-Identifier: GPL-2.0
3 * rv tool, the interface for the Linux kernel RV subsystem and home of
4 * user-space controlled monitors.
6 * Copyright (C) 2022 Red Hat Inc, Daniel Bristot de Oliveira <bristot@kernel.org>
15 #include <in_kernel.h>
17 static int stop_session
;
20 * stop_rv - tell monitors to stop
22 static void stop_rv(int sig
)
28 * should_stop - check if the monitor should stop.
30 * Returns 1 if the monitor should stop, 0 otherwise.
38 * rv_list - list all available monitors
40 static void rv_list(int argc
, char **argv
)
42 static const char *const usage
[] = {
44 " usage: rv list [-h]",
46 " list all available monitors",
48 " -h/--help: print this menu",
54 fprintf(stderr
, "rv version %s\n", VERSION
);
56 /* more than 1 is always usage */
57 for (i
= 0; usage
[i
]; i
++)
58 fprintf(stderr
, "%s\n", usage
[i
]);
60 /* but only -h is valid */
61 if (!strcmp(argv
[1], "-h") || !strcmp(argv
[1], "--help"))
72 * rv_mon - try to run a monitor passed as argument
74 static void rv_mon(int argc
, char **argv
)
79 static const char *const usage
[] = {
81 " usage: rv mon [-h] monitor [monitor options]",
85 " -h/--help: print this menu",
87 " monitor [monitor options]: the monitor, passing",
88 " the arguments to the [monitor options]",
92 /* requires at least one argument */
95 fprintf(stderr
, "rv version %s\n", VERSION
);
97 for (i
= 0; usage
[i
]; i
++)
98 fprintf(stderr
, "%s\n", usage
[i
]);
100 } else if (!strcmp(argv
[1], "-h") || !strcmp(argv
[1], "--help")) {
102 fprintf(stderr
, "rv version %s\n", VERSION
);
104 for (i
= 0; usage
[i
]; i
++)
105 fprintf(stderr
, "%s\n", usage
[i
]);
109 monitor_name
= argv
[1];
111 * Call all possible monitor implementations, looking
114 run
+= ikm_run_monitor(monitor_name
, argc
-1, &argv
[1]);
117 err_msg("rv: monitor %s does not exist\n", monitor_name
);
121 static void usage(int exit_val
, const char *fmt
, ...)
127 static const char *const usage
[] = {
129 " usage: rv command [-h] [command_options]",
131 " -h/--help: print this menu",
133 " command: run one of the following command:",
134 " list: list all available monitors",
135 " mon: run a monitor",
137 " [command options]: each command has its own set of options",
138 " run rv command -h for further information",
143 vsnprintf(message
, sizeof(message
), fmt
, ap
);
146 fprintf(stderr
, "rv version %s: %s\n", VERSION
, message
);
148 for (i
= 0; usage
[i
]; i
++)
149 fprintf(stderr
, "%s\n", usage
[i
]);
155 * main - select which main sending the command
157 * main itself redirects the arguments to the sub-commands
158 * to handle the options.
160 * subcommands should exit.
162 int main(int argc
, char **argv
)
165 usage(1, "%s needs root permission", argv
[0]);
168 usage(1, "%s requires a command", argv
[0]);
170 if (!strcmp(argv
[1], "-h") || !strcmp(argv
[1], "--help"))
173 if (!strcmp(argv
[1], "list"))
174 rv_list(--argc
, &argv
[1]);
176 if (!strcmp(argv
[1], "mon")) {
178 * monitor's main should monitor should_stop() function.
181 signal(SIGINT
, stop_rv
);
183 rv_mon(argc
- 1, &argv
[1]);
186 /* invalid sub-command */
187 usage(1, "%s does not know the %s command, old version?", argv
[0], argv
[1]);