summary refs log tree commit diff
path: root/src/main.rs
diff options
context:
space:
mode:
authorZach Reizner <zachr@google.com>2019-02-21 20:43:21 -0800
committerchrome-bot <chrome-bot@chromium.org>2019-02-28 03:24:25 -0800
commit7898632b42e52109601bc06e7fc380c475430874 (patch)
tree5d1542b35b6556e4666f6e0b6ba1d9c448c7155f /src/main.rs
parenta60744b42ee2589e9318029cf3fd7d87fd73f29d (diff)
downloadcrosvm-7898632b42e52109601bc06e7fc380c475430874.tar
crosvm-7898632b42e52109601bc06e7fc380c475430874.tar.gz
crosvm-7898632b42e52109601bc06e7fc380c475430874.tar.bz2
crosvm-7898632b42e52109601bc06e7fc380c475430874.tar.lz
crosvm-7898632b42e52109601bc06e7fc380c475430874.tar.xz
crosvm-7898632b42e52109601bc06e7fc380c475430874.tar.zst
crosvm-7898632b42e52109601bc06e7fc380c475430874.zip
main: log responses to command line vm control requests
Now that the connection oriented seqpacket sockets are used for vm
control messages, a response can be received by the requested. This
change prints out that response.

TEST=crosvm suspend|resume|balloon|stop <socket>
BUG=chromium:848187

Change-Id: I18ac23c26127332e2be498113cc0c3310fd09a7d
Reviewed-on: https://chromium-review.googlesource.com/1482370
Commit-Ready: Zach Reizner <zachr@chromium.org>
Tested-by: kokoro <noreply+kokoro@google.com>
Tested-by: Zach Reizner <zachr@chromium.org>
Reviewed-by: Daniel Verkamp <dverkamp@chromium.org>
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs115
1 files changed, 41 insertions, 74 deletions
diff --git a/src/main.rs b/src/main.rs
index 32380b7..70dbf4d 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -55,8 +55,8 @@ use qcow::QcowFile;
 use sys_util::{getpid, kill_process_group, net::UnixSeqpacket, reap_child, syslog};
 
 use argument::{print_help, set_arguments, Argument};
-use msg_socket::{MsgSender, Sender};
-use vm_control::VmRequest;
+use msg_socket::{MsgReceiver, MsgSender, MsgSocket};
+use vm_control::{VmRequest, VmResponse};
 
 static SECCOMP_POLICY_DIR: &'static str = "/usr/share/policy/crosvm";
 
@@ -709,32 +709,35 @@ fn run_vm(args: std::env::Args) -> std::result::Result<(), ()> {
     }
 }
 
-fn vms_request(
-    cmd_name: &str,
-    cmd_help: &str,
-    request: &VmRequest,
-    args: std::env::Args,
-) -> std::result::Result<(), ()> {
-    if args.len() == 0 {
-        print_help(cmd_name, "VM_SOCKET...", &[]);
-        println!("{}", cmd_help);
-    }
-
+fn vms_request(request: &VmRequest, args: std::env::Args) -> std::result::Result<(), ()> {
     let mut return_result = Ok(());
     for socket_path in args {
         match UnixSeqpacket::connect(&socket_path) {
             Ok(s) => {
-                let sender = Sender::<VmRequest>::new(s);
-                if let Err(e) = sender.send(request) {
+                let socket = MsgSocket::<VmRequest, VmResponse>::new(s);
+                if let Err(e) = socket.send(request) {
                     error!(
                         "failed to send request to socket at '{}': {}",
                         socket_path, e
                     );
+                    return_result = Err(());
+                    continue;
+                }
+                match socket.recv() {
+                    Ok(response) => info!("request response was {}", response),
+                    Err(e) => {
+                        error!(
+                            "failed to send request to socket at2 '{}': {}",
+                            socket_path, e
+                        );
+                        return_result = Err(());
+                        continue;
+                    }
                 }
             }
             Err(e) => {
                 error!("failed to connect to socket at '{}': {}", socket_path, e);
-                return_result = Err(());;
+                return_result = Err(());
             }
         }
     }
@@ -743,36 +746,37 @@ fn vms_request(
 }
 
 fn stop_vms(args: std::env::Args) -> std::result::Result<(), ()> {
-    vms_request(
-        "crosvm stop",
-        "Stops the crosvm instance listening on each `VM_SOCKET` given.",
-        &VmRequest::Exit,
-        args,
-    )
+    if args.len() == 0 {
+        print_help("crosvm stop", "VM_SOCKET...", &[]);
+        println!("Stops the crosvm instance listening on each `VM_SOCKET` given.");
+        return Ok(());
+    }
+    vms_request(&VmRequest::Exit, args)
 }
 
 fn suspend_vms(args: std::env::Args) -> std::result::Result<(), ()> {
-    vms_request(
-        "crosvm suspend",
-        "Suspends the crosvm instance listening on each `VM_SOCKET` given.",
-        &VmRequest::Suspend,
-        args,
-    )
+    if args.len() == 0 {
+        print_help("crosvm suspend", "VM_SOCKET...", &[]);
+        println!("Suspends the crosvm instance listening on each `VM_SOCKET` given.");
+        return Ok(());
+    }
+    vms_request(&VmRequest::Suspend, args)
 }
 
 fn resume_vms(args: std::env::Args) -> std::result::Result<(), ()> {
-    vms_request(
-        "crosvm resume",
-        "Suspends the crosvm instance listening on each `VM_SOCKET` given.",
-        &VmRequest::Resume,
-        args,
-    )
+    if args.len() == 0 {
+        print_help("crosvm resume", "VM_SOCKET...", &[]);
+        println!("Resumes the crosvm instance listening on each `VM_SOCKET` given.");
+        return Ok(());
+    }
+    vms_request(&VmRequest::Resume, args)
 }
 
 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(());
     }
     let num_bytes = match args.nth(0).unwrap().parse::<u64>() {
         Ok(n) => n,
@@ -782,26 +786,7 @@ fn balloon_vms(mut args: std::env::Args) -> std::result::Result<(), ()> {
         }
     };
 
-    let mut return_result = Ok(());
-    for socket_path in args {
-        match UnixSeqpacket::connect(&socket_path) {
-            Ok(s) => {
-                let sender = Sender::<VmRequest>::new(s);
-                if let Err(e) = sender.send(&VmRequest::BalloonAdjust(num_bytes)) {
-                    error!(
-                        "failed to send balloon request to socket at '{}': {}",
-                        socket_path, e
-                    );
-                }
-            }
-            Err(e) => {
-                error!("failed to connect to socket at '{}': {}", socket_path, e);
-                return_result = Err(());
-            }
-        }
-    }
-
-    return_result
+    vms_request(&VmRequest::BalloonAdjust(num_bytes), args)
 }
 
 fn create_qcow2(mut args: std::env::Args) -> std::result::Result<(), ()> {
@@ -840,6 +825,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(());
     }
     let subcommand: &str = &args.nth(0).unwrap();
 
@@ -872,26 +858,7 @@ fn disk_cmd(mut args: std::env::Args) -> std::result::Result<(), ()> {
         }
     };
 
-    let mut return_result = Ok(());
-    for socket_path in args {
-        match UnixSeqpacket::connect(&socket_path) {
-            Ok(s) => {
-                let sender = Sender::<VmRequest>::new(s);
-                if let Err(e) = sender.send(&request) {
-                    error!(
-                        "failed to send disk request to socket at '{}': {}",
-                        socket_path, e
-                    );
-                }
-            }
-            Err(e) => {
-                error!("failed to connect to socket at '{}': {}", socket_path, e);
-                return_result = Err(());
-            }
-        }
-    }
-
-    return_result
+    vms_request(&request, args)
 }
 
 fn print_usage() {