2
0
mirror of https://github.com/checkpoint-restore/criu synced 2025-08-29 13:28:27 +00:00

crtools: simplify check for cpuinfo subcommands

The cpuinfo command requires a "dump" or "check" subcommand. Thus, we
replace `CR_CPUINFO` with `CR_CPUINFO_DUMP` and `CR_CPUINFO_CHECK`.
This allows us to remove unnecessary subcommand check in
`image_dir_mode()` and perform all parsing in `parse_criu_mode()`.

With this change the check for validating the cpuinfo subcommand is
now done only once with `CR_CPUINFO_DUMP` or `CR_CPUINFO_CHECK` enum.

Signed-off-by: Liana Koleva <43767763+lianakoleva@users.noreply.github.com>
Signed-off-by: Radostin Stoyanov <rstoyanov@fedoraproject.org>
This commit is contained in:
Liana Koleva 2025-03-26 17:41:51 +00:00 committed by Radostin Stoyanov
parent fd353fdd8e
commit 3cbb864ae0
3 changed files with 32 additions and 30 deletions

View File

@ -1261,7 +1261,7 @@ static int handle_cpuinfo(int sk, CriuReq *msg)
if (pid == 0) { if (pid == 0) {
int ret = 1; int ret = 1;
opts.mode = CR_CPUINFO; opts.mode = (msg->type == CRIU_REQ_TYPE__CPUINFO_DUMP) ? CR_CPUINFO_DUMP : CR_CPUINFO_CHECK;
if (setup_opts_from_req(sk, msg->opts)) if (setup_opts_from_req(sk, msg->opts))
goto cout; goto cout;

View File

@ -54,19 +54,17 @@ void flush_early_log_to_stderr(void)
flush_early_log_buffer(STDERR_FILENO); flush_early_log_buffer(STDERR_FILENO);
} }
static int image_dir_mode(char *argv[], int optind) static int image_dir_mode(void)
{ {
switch (opts.mode) { switch (opts.mode) {
case CR_DUMP: case CR_DUMP:
/* fallthrough */ /* fallthrough */
case CR_CPUINFO_DUMP:
/* fallthrough */
case CR_PRE_DUMP: case CR_PRE_DUMP:
return O_DUMP; return O_DUMP;
case CR_RESTORE: case CR_RESTORE:
return O_RSTR; return O_RSTR;
case CR_CPUINFO:
if (!strcmp(argv[optind + 1], "dump"))
return O_DUMP;
/* fallthrough */
default: default:
return -1; return -1;
} }
@ -76,7 +74,7 @@ static int image_dir_mode(char *argv[], int optind)
return -1; return -1;
} }
static int parse_criu_mode(char *mode) static int parse_criu_mode(char *mode, char *subcommand)
{ {
if (!strcmp(mode, "dump")) if (!strcmp(mode, "dump"))
opts.mode = CR_DUMP; opts.mode = CR_DUMP;
@ -96,8 +94,12 @@ static int parse_criu_mode(char *mode)
opts.mode = CR_SWRK; opts.mode = CR_SWRK;
else if (!strcmp(mode, "dedup")) else if (!strcmp(mode, "dedup"))
opts.mode = CR_DEDUP; opts.mode = CR_DEDUP;
else if (!strcmp(mode, "cpuinfo")) else if (!strcmp(mode, "cpuinfo") && subcommand == NULL)
opts.mode = CR_CPUINFO; return -2;
else if (!strcmp(mode, "cpuinfo") && !strcmp(subcommand, "dump"))
opts.mode = CR_CPUINFO_DUMP;
else if (!strcmp(mode, "cpuinfo") && !strcmp(subcommand, "check"))
opts.mode = CR_CPUINFO_CHECK;
else if (!strcmp(mode, "exec")) else if (!strcmp(mode, "exec"))
opts.mode = CR_EXEC_DEPRECATED; opts.mode = CR_EXEC_DEPRECATED;
else if (!strcmp(mode, "show")) else if (!strcmp(mode, "show"))
@ -115,6 +117,7 @@ int main(int argc, char *argv[], char *envp[])
bool has_exec_cmd = false; bool has_exec_cmd = false;
bool has_sub_command; bool has_sub_command;
int state = PARSING_GLOBAL_CONF; int state = PARSING_GLOBAL_CONF;
char *subcommand;
BUILD_BUG_ON(CTL_32 != SYSCTL_TYPE__CTL_32); BUILD_BUG_ON(CTL_32 != SYSCTL_TYPE__CTL_32);
BUILD_BUG_ON(__CTL_STR != SYSCTL_TYPE__CTL_STR); BUILD_BUG_ON(__CTL_STR != SYSCTL_TYPE__CTL_STR);
@ -165,9 +168,15 @@ int main(int argc, char *argv[], char *envp[])
return 1; return 1;
} }
if (parse_criu_mode(argv[optind])) { has_sub_command = (argc - optind) > 1;
subcommand = has_sub_command ? argv[optind + 1] : NULL;
ret = parse_criu_mode(argv[optind], subcommand);
if (ret == -1) {
pr_err("unknown command: %s\n", argv[optind]); pr_err("unknown command: %s\n", argv[optind]);
goto usage; goto usage;
} else if (ret == -2) {
pr_err("cpuinfo requires an action: dump or check\n");
goto usage;
} }
/* /*
* util_init initializes criu_run_id and compel_run_id so that sockets * util_init initializes criu_run_id and compel_run_id so that sockets
@ -223,25 +232,20 @@ int main(int argc, char *argv[], char *envp[])
return 1; return 1;
memcpy(opts.exec_cmd, &argv[optind + 1], (argc - optind - 1) * sizeof(char *)); memcpy(opts.exec_cmd, &argv[optind + 1], (argc - optind - 1) * sizeof(char *));
opts.exec_cmd[argc - optind - 1] = NULL; opts.exec_cmd[argc - optind - 1] = NULL;
} else { } else if (opts.mode != CR_CPUINFO_DUMP && opts.mode != CR_CPUINFO_CHECK && has_sub_command) {
/* No subcommands except for cpuinfo and restore --exec-cmd */ /* No subcommands except for cpuinfo and restore --exec-cmd */
if (opts.mode != CR_CPUINFO && has_sub_command) { pr_err("excessive parameter%s for command %s\n", (argc - optind) > 2 ? "s" : "", argv[optind]);
pr_err("excessive parameter%s for command %s\n", (argc - optind) > 2 ? "s" : "", argv[optind]); goto usage;
goto usage;
} else if (opts.mode == CR_CPUINFO && !has_sub_command) {
pr_err("cpuinfo requires an action: dump or check\n");
goto usage;
}
} }
if (opts.stream && image_dir_mode(argv, optind) == -1) { if (opts.stream && image_dir_mode() == -1) {
pr_err("--stream cannot be used with the %s command\n", argv[optind]); pr_err("--stream cannot be used with the %s command\n", argv[optind]);
goto usage; goto usage;
} }
/* We must not open imgs dir, if service is called */ /* We must not open imgs dir, if service is called */
if (opts.mode != CR_SERVICE) { if (opts.mode != CR_SERVICE) {
ret = open_image_dir(opts.imgs_dir, image_dir_mode(argv, optind)); ret = open_image_dir(opts.imgs_dir, image_dir_mode());
if (ret < 0) { if (ret < 0) {
pr_err("Couldn't open image dir %s\n", opts.imgs_dir); pr_err("Couldn't open image dir %s\n", opts.imgs_dir);
return 1; return 1;
@ -335,15 +339,12 @@ int main(int argc, char *argv[], char *envp[])
if (opts.mode == CR_DEDUP) if (opts.mode == CR_DEDUP)
return cr_dedup() != 0; return cr_dedup() != 0;
if (opts.mode == CR_CPUINFO) { if (opts.mode == CR_CPUINFO_DUMP) {
if (!argv[optind + 1]) { return cpuinfo_dump();
pr_err("cpuinfo requires an action: dump or check\n"); }
goto usage;
} if (opts.mode == CR_CPUINFO_CHECK) {
if (!strcmp(argv[optind + 1], "dump")) return cpuinfo_check();
return cpuinfo_dump();
else if (!strcmp(argv[optind + 1], "check"))
return cpuinfo_check();
} }
if (opts.mode == CR_EXEC_DEPRECATED) { if (opts.mode == CR_EXEC_DEPRECATED) {

View File

@ -125,7 +125,8 @@ enum criu_mode {
CR_SERVICE, CR_SERVICE,
CR_SWRK, CR_SWRK,
CR_DEDUP, CR_DEDUP,
CR_CPUINFO, CR_CPUINFO_DUMP,
CR_CPUINFO_CHECK,
CR_EXEC_DEPRECATED, CR_EXEC_DEPRECATED,
CR_SHOW_DEPRECATED, CR_SHOW_DEPRECATED,
}; };