main: return error on invalid length of arguments

For the commands that verify the length of arguments, returning
Ok on an invalid length will confuse any external programs that
launch crosvm because they still get exit code of success from
crosvm even when the command failed.

Also add a missed return of 'create_qcow2' sub-command.

BUG=None
TEST=some basic tests:
() cargo check
() verify the return value from changed sub-commands is not 0 with
an invalid argument length.
() launch a VM with concierge_client from a root shell

Change-Id: I8278107a4d2fcf3cb6fafb65f30f431f97f7deb1
Signed-off-by: Jianxun Zhang <jianxun.zhang@intel.com>
Reviewed-on: https://chromium-review.googlesource.com/1501552
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Zach Reizner <zachr@chromium.org>
This commit is contained in:
Jianxun Zhang 2019-03-04 14:38:24 -08:00 committed by chrome-bot
parent e4ece32798
commit 56497d23ad

View file

@ -799,7 +799,7 @@ fn stop_vms(args: std::env::Args) -> std::result::Result<(), ()> {
if args.len() == 0 {
print_help("crosvm stop", "VM_SOCKET...", &[]);
println!("Stops the crosvm instance listening on each `VM_SOCKET` given.");
return Ok(());
return Err(());
}
vms_request(&VmRequest::Exit, args)
}
@ -808,7 +808,7 @@ fn suspend_vms(args: std::env::Args) -> std::result::Result<(), ()> {
if args.len() == 0 {
print_help("crosvm suspend", "VM_SOCKET...", &[]);
println!("Suspends the crosvm instance listening on each `VM_SOCKET` given.");
return Ok(());
return Err(());
}
vms_request(&VmRequest::Suspend, args)
}
@ -817,7 +817,7 @@ fn resume_vms(args: std::env::Args) -> std::result::Result<(), ()> {
if args.len() == 0 {
print_help("crosvm resume", "VM_SOCKET...", &[]);
println!("Resumes the crosvm instance listening on each `VM_SOCKET` given.");
return Ok(());
return Err(());
}
vms_request(&VmRequest::Resume, args)
}
@ -826,7 +826,7 @@ fn balloon_vms(mut args: std::env::Args) -> std::result::Result<(), ()> {
if args.len() < 2 {
print_help("crosvm balloon", "SIZE VM_SOCKET...", &[]);
println!("Set the ballon size of the crosvm instance to `SIZE` bytes.");
return Ok(());
return Err(());
}
let num_bytes = match args.nth(0).unwrap().parse::<u64>() {
Ok(n) => n,
@ -843,6 +843,7 @@ fn create_qcow2(mut args: std::env::Args) -> std::result::Result<(), ()> {
if args.len() != 2 {
print_help("crosvm create_qcow2", "PATH SIZE", &[]);
println!("Create a new QCOW2 image at `PATH` of the specified `SIZE` in bytes.");
return Err(());
}
let file_path = args.nth(0).unwrap();
let size: u64 = match args.nth(0).unwrap().parse::<u64>() {
@ -875,7 +876,7 @@ fn disk_cmd(mut args: std::env::Args) -> std::result::Result<(), ()> {
println!("Manage attached virtual disk devices.");
println!("Subcommands:");
println!(" resize DISK_INDEX NEW_SIZE VM_SOCKET");
return Ok(());
return Err(());
}
let subcommand: &str = &args.nth(0).unwrap();