Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / tools / verification / rv / src / rv.c
blob1ddb855328165f2ac11600da33bb96494eb202f7
1 // SPDX-License-Identifier: GPL-2.0
2 /*
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>
7 */
9 #include <stdlib.h>
10 #include <signal.h>
11 #include <unistd.h>
13 #include <trace.h>
14 #include <utils.h>
15 #include <in_kernel.h>
17 static int stop_session;
20 * stop_rv - tell monitors to stop
22 static void stop_rv(int sig)
24 stop_session = 1;
27 /**
28 * should_stop - check if the monitor should stop.
30 * Returns 1 if the monitor should stop, 0 otherwise.
32 int should_stop(void)
34 return stop_session;
38 * rv_list - list all available monitors
40 static void rv_list(int argc, char **argv)
42 static const char *const usage[] = {
43 "",
44 " usage: rv list [-h]",
45 "",
46 " list all available monitors",
47 "",
48 " -h/--help: print this menu",
49 NULL,
51 int i;
53 if (argc > 1) {
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"))
62 exit(0);
63 else
64 exit(1);
67 ikm_list_monitors();
68 exit(0);
72 * rv_mon - try to run a monitor passed as argument
74 static void rv_mon(int argc, char **argv)
76 char *monitor_name;
77 int i, run = 0;
79 static const char *const usage[] = {
80 "",
81 " usage: rv mon [-h] monitor [monitor options]",
82 "",
83 " run a monitor",
84 "",
85 " -h/--help: print this menu",
86 "",
87 " monitor [monitor options]: the monitor, passing",
88 " the arguments to the [monitor options]",
89 NULL,
92 /* requires at least one argument */
93 if (argc == 1) {
95 fprintf(stderr, "rv version %s\n", VERSION);
97 for (i = 0; usage[i]; i++)
98 fprintf(stderr, "%s\n", usage[i]);
99 exit(1);
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]);
106 exit(0);
109 monitor_name = argv[1];
111 * Call all possible monitor implementations, looking
112 * for the [monitor].
114 run += ikm_run_monitor(monitor_name, argc-1, &argv[1]);
116 if (!run)
117 err_msg("rv: monitor %s does not exist\n", monitor_name);
118 exit(!run);
121 static void usage(int exit_val, const char *fmt, ...)
123 char message[1024];
124 va_list ap;
125 int i;
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",
139 NULL,
142 va_start(ap, fmt);
143 vsnprintf(message, sizeof(message), fmt, ap);
144 va_end(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]);
151 exit(exit_val);
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)
164 if (geteuid())
165 usage(1, "%s needs root permission", argv[0]);
167 if (argc <= 1)
168 usage(1, "%s requires a command", argv[0]);
170 if (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))
171 usage(0, "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.
179 * and exit.
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]);