patches and low-level development discussion
 help / color / mirror / code / Atom feed
* [PATCH] host/start-vmm: replace jq calls with miniserde
@ 2024-08-25 11:28 Yureka
  2024-08-25 11:45 ` Alyssa Ross
  0 siblings, 1 reply; 2+ messages in thread
From: Yureka @ 2024-08-25 11:28 UTC (permalink / raw)
  To: devel; +Cc: Yureka

---
 host/start-vmm/ch.rs | 38 +++++++++++++++-----------------------
 1 file changed, 15 insertions(+), 23 deletions(-)

diff --git a/host/start-vmm/ch.rs b/host/start-vmm/ch.rs
index cf164c6..abc6a00 100644
--- a/host/start-vmm/ch.rs
+++ b/host/start-vmm/ch.rs
@@ -9,7 +9,7 @@ use std::os::raw::{c_char, c_int};
 use std::os::unix::prelude::*;
 use std::process::{Command, Stdio};
 
-use miniserde::{json, Serialize};
+use miniserde::{json, Deserialize, Serialize};
 
 use crate::net::MacAddress;
 
@@ -126,34 +126,26 @@ pub fn create_vm(vm_name: &str, mut config: VmConfig) -> Result<(), String> {
 }
 
 pub fn add_net(vm_name: &str, net: &NetConfig) -> Result<OsString, NonZeroI32> {
-    let mut ch_remote = command(vm_name, "add-net")
+    let ch_output = command(vm_name, "add-net")
         .arg(format!("fd={},mac={}", net.fd, net.mac))
-        .stdout(Stdio::piped())
         .spawn()
-        .or(Err(EPERM))?;
+        .or(Err(EPERM))?
+        .wait_with_output()
+        .or(Err(EPROTO))?;
 
-    let jq_out = match Command::new("jq")
-        .args(["-j", ".id"])
-        .stdin(ch_remote.stdout.take().unwrap())
-        .stderr(Stdio::inherit())
-        .output()
-    {
-        Ok(o) => o,
-        Err(_) => {
-            // Try not to leave a zombie.
-            let _ = ch_remote.kill();
-            let _ = ch_remote.wait();
-            return Err(EPERM);
-        }
-    };
+    if !ch_output.status.success() {
+        return Err(EPROTO);
+    }
 
-    if let Ok(ch_remote_status) = ch_remote.wait() {
-        if ch_remote_status.success() && jq_out.status.success() {
-            return Ok(OsString::from_vec(jq_out.stdout));
-        }
+    #[derive(Deserialize)]
+    struct AddNetOutput {
+        id: String,
     }
 
-    Err(EPROTO)
+    let output_str = std::str::from_utf8(&ch_output.stdout).map_err(|_| EPROTO)?;
+    let output_parsed: AddNetOutput = json::from_str(output_str).map_err(|_| EPROTO)?;
+
+    Ok(OsString::from_vec(output_parsed.id.into()))
 }
 
 pub fn remove_device(vm_name: &str, device_id: &OsStr) -> Result<(), NonZeroI32> {
-- 
2.45.2


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] host/start-vmm: replace jq calls with miniserde
  2024-08-25 11:28 [PATCH] host/start-vmm: replace jq calls with miniserde Yureka
@ 2024-08-25 11:45 ` Alyssa Ross
  0 siblings, 0 replies; 2+ messages in thread
From: Alyssa Ross @ 2024-08-25 11:45 UTC (permalink / raw)
  To: Yureka; +Cc: devel

[-- Attachment #1: Type: text/plain, Size: 2829 bytes --]

Yureka <yuka@yuka.dev> writes:

> ---
>  host/start-vmm/ch.rs | 38 +++++++++++++++-----------------------
>  1 file changed, 15 insertions(+), 23 deletions(-)

Thanks a lot for the patch!  Could you please add a Signed-off-by and
copyright header?  (You already have one in release/combined/default.nix
— just needs to be like that but with the current year.)

> diff --git a/host/start-vmm/ch.rs b/host/start-vmm/ch.rs
> index cf164c6..abc6a00 100644
> --- a/host/start-vmm/ch.rs
> +++ b/host/start-vmm/ch.rs
> @@ -9,7 +9,7 @@ use std::os::raw::{c_char, c_int};
>  use std::os::unix::prelude::*;
>  use std::process::{Command, Stdio};
>  
> -use miniserde::{json, Serialize};
> +use miniserde::{json, Deserialize, Serialize};
>  
>  use crate::net::MacAddress;
>  
> @@ -126,34 +126,26 @@ pub fn create_vm(vm_name: &str, mut config: VmConfig) -> Result<(), String> {
>  }
>  
>  pub fn add_net(vm_name: &str, net: &NetConfig) -> Result<OsString, NonZeroI32> {
> -    let mut ch_remote = command(vm_name, "add-net")
> +    let ch_output = command(vm_name, "add-net")
>          .arg(format!("fd={},mac={}", net.fd, net.mac))
> -        .stdout(Stdio::piped())
>          .spawn()
> -        .or(Err(EPERM))?;
> +        .or(Err(EPERM))?
> +        .wait_with_output()
> +        .or(Err(EPROTO))?;
>  
> -    let jq_out = match Command::new("jq")
> -        .args(["-j", ".id"])
> -        .stdin(ch_remote.stdout.take().unwrap())
> -        .stderr(Stdio::inherit())
> -        .output()
> -    {
> -        Ok(o) => o,
> -        Err(_) => {
> -            // Try not to leave a zombie.
> -            let _ = ch_remote.kill();
> -            let _ = ch_remote.wait();
> -            return Err(EPERM);
> -        }
> -    };
> +    if !ch_output.status.success() {
> +        return Err(EPROTO);
> +    }
>  
> -    if let Ok(ch_remote_status) = ch_remote.wait() {
> -        if ch_remote_status.success() && jq_out.status.success() {
> -            return Ok(OsString::from_vec(jq_out.stdout));
> -        }
> +    #[derive(Deserialize)]
> +    struct AddNetOutput {
> +        id: String,
>      }
>  
> -    Err(EPROTO)
> +    let output_str = std::str::from_utf8(&ch_output.stdout).map_err(|_| EPROTO)?;

Can you use std::str at the top of the file?  That's the normal
convention in Spectrum.

> +    let output_parsed: AddNetOutput = json::from_str(output_str).map_err(|_| EPROTO)?;
> +
> +    Ok(OsString::from_vec(output_parsed.id.into()))

Should we just change this function to return String?  I think the only
reason to use OsString was that there was no reason to go through a
UTF-8 check before, but now miniserde is doing that anyway.  This would
be a bigger diff, so if you'd rather I did that myself as follow-up
that's fine too.

[-- Attachment #2: signature.asc --]
[-- Type: application/pgp-signature, Size: 832 bytes --]

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-08-25 11:45 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-08-25 11:28 [PATCH] host/start-vmm: replace jq calls with miniserde Yureka
2024-08-25 11:45 ` Alyssa Ross

Code repositories for project(s) associated with this public inbox

	https://spectrum-os.org/git/crosvm
	https://spectrum-os.org/git/doc
	https://spectrum-os.org/git/mktuntap
	https://spectrum-os.org/git/nixpkgs
	https://spectrum-os.org/git/spectrum
	https://spectrum-os.org/git/ucspi-vsock
	https://spectrum-os.org/git/www

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).