mirror of
				https://github.com/edera-dev/krata.git
				synced 2025-11-03 23:29:39 +00:00 
			
		
		
		
	async-ify xenstore and xenclient
This commit is contained in:
		@ -14,6 +14,7 @@ memchr = { workspace = true }
 | 
			
		||||
slice-copy = { workspace = true }
 | 
			
		||||
log = { workspace = true }
 | 
			
		||||
uuid = { workspace = true }
 | 
			
		||||
tokio = { workspace = true }
 | 
			
		||||
 | 
			
		||||
[dependencies.xencall]
 | 
			
		||||
path = "../xencall"
 | 
			
		||||
 | 
			
		||||
@ -2,7 +2,8 @@ use std::{env, process};
 | 
			
		||||
use xenclient::error::Result;
 | 
			
		||||
use xenclient::{DomainConfig, XenClient};
 | 
			
		||||
 | 
			
		||||
fn main() -> Result<()> {
 | 
			
		||||
#[tokio::main]
 | 
			
		||||
async fn main() -> Result<()> {
 | 
			
		||||
    env_logger::init();
 | 
			
		||||
 | 
			
		||||
    let args: Vec<String> = env::args().collect();
 | 
			
		||||
@ -12,7 +13,7 @@ fn main() -> Result<()> {
 | 
			
		||||
    }
 | 
			
		||||
    let kernel_image_path = args.get(1).expect("argument not specified");
 | 
			
		||||
    let initrd_path = args.get(2).expect("argument not specified");
 | 
			
		||||
    let mut client = XenClient::open()?;
 | 
			
		||||
    let mut client = XenClient::open().await?;
 | 
			
		||||
    let config = DomainConfig {
 | 
			
		||||
        backend_domid: 0,
 | 
			
		||||
        name: "xenclient-test",
 | 
			
		||||
@ -28,7 +29,7 @@ fn main() -> Result<()> {
 | 
			
		||||
        extra_keys: vec![],
 | 
			
		||||
        event_channels: vec![],
 | 
			
		||||
    };
 | 
			
		||||
    let domid = client.create(&config)?;
 | 
			
		||||
    let domid = client.create(&config).await?;
 | 
			
		||||
    println!("created domain {}", domid);
 | 
			
		||||
    Ok(())
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -82,38 +82,43 @@ pub struct DomainConfig<'a> {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl XenClient {
 | 
			
		||||
    pub fn open() -> Result<XenClient> {
 | 
			
		||||
        let store = XsdClient::open()?;
 | 
			
		||||
    pub async fn open() -> Result<XenClient> {
 | 
			
		||||
        let store = XsdClient::open().await?;
 | 
			
		||||
        let call = XenCall::open()?;
 | 
			
		||||
        Ok(XenClient { store, call })
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub fn create(&mut self, config: &DomainConfig) -> Result<u32> {
 | 
			
		||||
    pub async fn create(&mut self, config: &DomainConfig<'_>) -> Result<u32> {
 | 
			
		||||
        let domain = CreateDomain {
 | 
			
		||||
            max_vcpus: config.max_vcpus,
 | 
			
		||||
            ..Default::default()
 | 
			
		||||
        };
 | 
			
		||||
        let domid = self.call.create_domain(domain)?;
 | 
			
		||||
        match self.init(domid, &domain, config) {
 | 
			
		||||
        match self.init(domid, &domain, config).await {
 | 
			
		||||
            Ok(_) => Ok(domid),
 | 
			
		||||
            Err(err) => {
 | 
			
		||||
                // ignore since destroying a domain is best
 | 
			
		||||
                // effort when an error occurs
 | 
			
		||||
                let _ = self.destroy(domid);
 | 
			
		||||
                let _ = self.destroy(domid).await;
 | 
			
		||||
                Err(err)
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn init(&mut self, domid: u32, domain: &CreateDomain, config: &DomainConfig) -> Result<()> {
 | 
			
		||||
    async fn init(
 | 
			
		||||
        &mut self,
 | 
			
		||||
        domid: u32,
 | 
			
		||||
        domain: &CreateDomain,
 | 
			
		||||
        config: &DomainConfig<'_>,
 | 
			
		||||
    ) -> Result<()> {
 | 
			
		||||
        trace!(
 | 
			
		||||
            "XenClient init domid={} domain={:?} config={:?}",
 | 
			
		||||
            domid,
 | 
			
		||||
            domain,
 | 
			
		||||
            config
 | 
			
		||||
        );
 | 
			
		||||
        let backend_dom_path = self.store.get_domain_path(0)?;
 | 
			
		||||
        let dom_path = self.store.get_domain_path(domid)?;
 | 
			
		||||
        let backend_dom_path = self.store.get_domain_path(0).await?;
 | 
			
		||||
        let dom_path = self.store.get_domain_path(domid).await?;
 | 
			
		||||
        let uuid_string = Uuid::from_bytes(domain.handle).to_string();
 | 
			
		||||
        let vm_path = format!("/vm/{}", uuid_string);
 | 
			
		||||
 | 
			
		||||
@ -139,57 +144,76 @@ impl XenClient {
 | 
			
		||||
        }];
 | 
			
		||||
 | 
			
		||||
        {
 | 
			
		||||
            let mut tx = self.store.transaction()?;
 | 
			
		||||
            let mut tx = self.store.transaction().await?;
 | 
			
		||||
 | 
			
		||||
            tx.rm(dom_path.as_str())?;
 | 
			
		||||
            tx.mknod(dom_path.as_str(), ro_perm)?;
 | 
			
		||||
            tx.rm(dom_path.as_str()).await?;
 | 
			
		||||
            tx.mknod(dom_path.as_str(), ro_perm).await?;
 | 
			
		||||
 | 
			
		||||
            tx.rm(vm_path.as_str())?;
 | 
			
		||||
            tx.mknod(vm_path.as_str(), ro_perm)?;
 | 
			
		||||
            tx.rm(vm_path.as_str()).await?;
 | 
			
		||||
            tx.mknod(vm_path.as_str(), ro_perm).await?;
 | 
			
		||||
 | 
			
		||||
            tx.mknod(vm_path.as_str(), no_perm)?;
 | 
			
		||||
            tx.mknod(format!("{}/device", vm_path).as_str(), no_perm)?;
 | 
			
		||||
            tx.mknod(vm_path.as_str(), no_perm).await?;
 | 
			
		||||
            tx.mknod(format!("{}/device", vm_path).as_str(), no_perm)
 | 
			
		||||
                .await?;
 | 
			
		||||
 | 
			
		||||
            tx.write_string(format!("{}/vm", dom_path).as_str(), &vm_path)?;
 | 
			
		||||
            tx.write_string(format!("{}/vm", dom_path).as_str(), &vm_path)
 | 
			
		||||
                .await?;
 | 
			
		||||
 | 
			
		||||
            tx.mknod(format!("{}/cpu", dom_path).as_str(), ro_perm)?;
 | 
			
		||||
            tx.mknod(format!("{}/memory", dom_path).as_str(), ro_perm)?;
 | 
			
		||||
            tx.mknod(format!("{}/cpu", dom_path).as_str(), ro_perm)
 | 
			
		||||
                .await?;
 | 
			
		||||
            tx.mknod(format!("{}/memory", dom_path).as_str(), ro_perm)
 | 
			
		||||
                .await?;
 | 
			
		||||
 | 
			
		||||
            tx.mknod(format!("{}/control", dom_path).as_str(), ro_perm)?;
 | 
			
		||||
            tx.mknod(format!("{}/control", dom_path).as_str(), ro_perm)
 | 
			
		||||
                .await?;
 | 
			
		||||
 | 
			
		||||
            tx.mknod(format!("{}/control/shutdown", dom_path).as_str(), rw_perm)?;
 | 
			
		||||
            tx.mknod(format!("{}/control/shutdown", dom_path).as_str(), rw_perm)
 | 
			
		||||
                .await?;
 | 
			
		||||
            tx.mknod(
 | 
			
		||||
                format!("{}/control/feature-poweroff", dom_path).as_str(),
 | 
			
		||||
                rw_perm,
 | 
			
		||||
            )?;
 | 
			
		||||
            )
 | 
			
		||||
            .await?;
 | 
			
		||||
            tx.mknod(
 | 
			
		||||
                format!("{}/control/feature-reboot", dom_path).as_str(),
 | 
			
		||||
                rw_perm,
 | 
			
		||||
            )?;
 | 
			
		||||
            )
 | 
			
		||||
            .await?;
 | 
			
		||||
            tx.mknod(
 | 
			
		||||
                format!("{}/control/feature-suspend", dom_path).as_str(),
 | 
			
		||||
                rw_perm,
 | 
			
		||||
            )?;
 | 
			
		||||
            tx.mknod(format!("{}/control/sysrq", dom_path).as_str(), rw_perm)?;
 | 
			
		||||
            )
 | 
			
		||||
            .await?;
 | 
			
		||||
            tx.mknod(format!("{}/control/sysrq", dom_path).as_str(), rw_perm)
 | 
			
		||||
                .await?;
 | 
			
		||||
 | 
			
		||||
            tx.mknod(format!("{}/data", dom_path).as_str(), rw_perm)?;
 | 
			
		||||
            tx.mknod(format!("{}/drivers", dom_path).as_str(), rw_perm)?;
 | 
			
		||||
            tx.mknod(format!("{}/feature", dom_path).as_str(), rw_perm)?;
 | 
			
		||||
            tx.mknod(format!("{}/attr", dom_path).as_str(), rw_perm)?;
 | 
			
		||||
            tx.mknod(format!("{}/error", dom_path).as_str(), rw_perm)?;
 | 
			
		||||
            tx.mknod(format!("{}/data", dom_path).as_str(), rw_perm)
 | 
			
		||||
                .await?;
 | 
			
		||||
            tx.mknod(format!("{}/drivers", dom_path).as_str(), rw_perm)
 | 
			
		||||
                .await?;
 | 
			
		||||
            tx.mknod(format!("{}/feature", dom_path).as_str(), rw_perm)
 | 
			
		||||
                .await?;
 | 
			
		||||
            tx.mknod(format!("{}/attr", dom_path).as_str(), rw_perm)
 | 
			
		||||
                .await?;
 | 
			
		||||
            tx.mknod(format!("{}/error", dom_path).as_str(), rw_perm)
 | 
			
		||||
                .await?;
 | 
			
		||||
 | 
			
		||||
            tx.write_string(
 | 
			
		||||
                format!("{}/uuid", vm_path).as_str(),
 | 
			
		||||
                &Uuid::from_bytes(domain.handle).to_string(),
 | 
			
		||||
            )?;
 | 
			
		||||
            tx.write_string(format!("{}/name", dom_path).as_str(), config.name)?;
 | 
			
		||||
            tx.write_string(format!("{}/name", vm_path).as_str(), config.name)?;
 | 
			
		||||
            )
 | 
			
		||||
            .await?;
 | 
			
		||||
            tx.write_string(format!("{}/name", dom_path).as_str(), config.name)
 | 
			
		||||
                .await?;
 | 
			
		||||
            tx.write_string(format!("{}/name", vm_path).as_str(), config.name)
 | 
			
		||||
                .await?;
 | 
			
		||||
 | 
			
		||||
            for (key, value) in &config.extra_keys {
 | 
			
		||||
                tx.write_string(format!("{}/{}", dom_path, key).as_str(), value)?;
 | 
			
		||||
                tx.write_string(format!("{}/{}", dom_path, key).as_str(), value)
 | 
			
		||||
                    .await?;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
            tx.commit()?;
 | 
			
		||||
            tx.commit().await?;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        self.call.set_max_vcpus(domid, config.max_vcpus)?;
 | 
			
		||||
@ -220,52 +244,63 @@ impl XenClient {
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        {
 | 
			
		||||
            let mut tx = self.store.transaction()?;
 | 
			
		||||
            tx.write_string(format!("{}/image/os_type", vm_path).as_str(), "linux")?;
 | 
			
		||||
            let mut tx = self.store.transaction().await?;
 | 
			
		||||
            tx.write_string(format!("{}/image/os_type", vm_path).as_str(), "linux")
 | 
			
		||||
                .await?;
 | 
			
		||||
            tx.write_string(
 | 
			
		||||
                format!("{}/image/kernel", vm_path).as_str(),
 | 
			
		||||
                config.kernel_path,
 | 
			
		||||
            )?;
 | 
			
		||||
            )
 | 
			
		||||
            .await?;
 | 
			
		||||
            tx.write_string(
 | 
			
		||||
                format!("{}/image/ramdisk", vm_path).as_str(),
 | 
			
		||||
                config.initrd_path,
 | 
			
		||||
            )?;
 | 
			
		||||
            )
 | 
			
		||||
            .await?;
 | 
			
		||||
            tx.write_string(
 | 
			
		||||
                format!("{}/image/cmdline", vm_path).as_str(),
 | 
			
		||||
                config.cmdline,
 | 
			
		||||
            )?;
 | 
			
		||||
            )
 | 
			
		||||
            .await?;
 | 
			
		||||
 | 
			
		||||
            tx.write_string(
 | 
			
		||||
                format!("{}/memory/static-max", dom_path).as_str(),
 | 
			
		||||
                &(config.mem_mb * 1024).to_string(),
 | 
			
		||||
            )?;
 | 
			
		||||
            )
 | 
			
		||||
            .await?;
 | 
			
		||||
            tx.write_string(
 | 
			
		||||
                format!("{}/memory/target", dom_path).as_str(),
 | 
			
		||||
                &(config.mem_mb * 1024).to_string(),
 | 
			
		||||
            )?;
 | 
			
		||||
            tx.write_string(format!("{}/memory/videoram", dom_path).as_str(), "0")?;
 | 
			
		||||
            tx.write_string(format!("{}/domid", dom_path).as_str(), &domid.to_string())?;
 | 
			
		||||
            )
 | 
			
		||||
            .await?;
 | 
			
		||||
            tx.write_string(format!("{}/memory/videoram", dom_path).as_str(), "0")
 | 
			
		||||
                .await?;
 | 
			
		||||
            tx.write_string(format!("{}/domid", dom_path).as_str(), &domid.to_string())
 | 
			
		||||
                .await?;
 | 
			
		||||
            tx.write_string(
 | 
			
		||||
                format!("{}/store/port", dom_path).as_str(),
 | 
			
		||||
                &xenstore_evtchn.to_string(),
 | 
			
		||||
            )?;
 | 
			
		||||
            )
 | 
			
		||||
            .await?;
 | 
			
		||||
            tx.write_string(
 | 
			
		||||
                format!("{}/store/ring-ref", dom_path).as_str(),
 | 
			
		||||
                &xenstore_mfn.to_string(),
 | 
			
		||||
            )?;
 | 
			
		||||
            )
 | 
			
		||||
            .await?;
 | 
			
		||||
            for i in 0..config.max_vcpus {
 | 
			
		||||
                let path = format!("{}/cpu/{}", dom_path, i);
 | 
			
		||||
                tx.mkdir(&path)?;
 | 
			
		||||
                tx.set_perms(&path, ro_perm)?;
 | 
			
		||||
                tx.mkdir(&path).await?;
 | 
			
		||||
                tx.set_perms(&path, ro_perm).await?;
 | 
			
		||||
                let path = format!("{}/cpu/{}/availability", dom_path, i);
 | 
			
		||||
                tx.write_string(&path, "online")?;
 | 
			
		||||
                tx.set_perms(&path, ro_perm)?;
 | 
			
		||||
                tx.write_string(&path, "online").await?;
 | 
			
		||||
                tx.set_perms(&path, ro_perm).await?;
 | 
			
		||||
            }
 | 
			
		||||
            tx.commit()?;
 | 
			
		||||
            tx.commit().await?;
 | 
			
		||||
        }
 | 
			
		||||
        if !self
 | 
			
		||||
            .store
 | 
			
		||||
            .introduce_domain(domid, xenstore_mfn, xenstore_evtchn)?
 | 
			
		||||
            .introduce_domain(domid, xenstore_mfn, xenstore_evtchn)
 | 
			
		||||
            .await?
 | 
			
		||||
        {
 | 
			
		||||
            return Err(Error::IntroduceDomainFailed);
 | 
			
		||||
        }
 | 
			
		||||
@ -277,7 +312,8 @@ impl XenClient {
 | 
			
		||||
            0,
 | 
			
		||||
            Some(console_evtchn),
 | 
			
		||||
            Some(console_mfn),
 | 
			
		||||
        )?;
 | 
			
		||||
        )
 | 
			
		||||
        .await?;
 | 
			
		||||
 | 
			
		||||
        for (index, _) in config.consoles.iter().enumerate() {
 | 
			
		||||
            self.console_device_add(
 | 
			
		||||
@ -288,7 +324,8 @@ impl XenClient {
 | 
			
		||||
                index + 1,
 | 
			
		||||
                None,
 | 
			
		||||
                None,
 | 
			
		||||
            )?;
 | 
			
		||||
            )
 | 
			
		||||
            .await?;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        for (index, disk) in config.disks.iter().enumerate() {
 | 
			
		||||
@ -299,7 +336,8 @@ impl XenClient {
 | 
			
		||||
                domid,
 | 
			
		||||
                index,
 | 
			
		||||
                disk,
 | 
			
		||||
            )?;
 | 
			
		||||
            )
 | 
			
		||||
            .await?;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        for (index, filesystem) in config.filesystems.iter().enumerate() {
 | 
			
		||||
@ -310,7 +348,8 @@ impl XenClient {
 | 
			
		||||
                domid,
 | 
			
		||||
                index,
 | 
			
		||||
                filesystem,
 | 
			
		||||
            )?;
 | 
			
		||||
            )
 | 
			
		||||
            .await?;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        for (index, vif) in config.vifs.iter().enumerate() {
 | 
			
		||||
@ -321,7 +360,8 @@ impl XenClient {
 | 
			
		||||
                domid,
 | 
			
		||||
                index,
 | 
			
		||||
                vif,
 | 
			
		||||
            )?;
 | 
			
		||||
            )
 | 
			
		||||
            .await?;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        for channel in &config.event_channels {
 | 
			
		||||
@ -330,23 +370,25 @@ impl XenClient {
 | 
			
		||||
                .evtchn_alloc_unbound(domid, config.backend_domid)?;
 | 
			
		||||
            let channel_path = format!("{}/evtchn/{}", dom_path, channel.name);
 | 
			
		||||
            self.store
 | 
			
		||||
                .write_string(&format!("{}/name", channel_path), channel.name)?;
 | 
			
		||||
                .write_string(&format!("{}/name", channel_path), channel.name)
 | 
			
		||||
                .await?;
 | 
			
		||||
            self.store
 | 
			
		||||
                .write_string(&format!("{}/channel", channel_path), &id.to_string())?;
 | 
			
		||||
                .write_string(&format!("{}/channel", channel_path), &id.to_string())
 | 
			
		||||
                .await?;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        self.call.unpause_domain(domid)?;
 | 
			
		||||
        Ok(())
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn disk_device_add(
 | 
			
		||||
    async fn disk_device_add(
 | 
			
		||||
        &mut self,
 | 
			
		||||
        dom_path: &str,
 | 
			
		||||
        backend_dom_path: &str,
 | 
			
		||||
        backend_domid: u32,
 | 
			
		||||
        domid: u32,
 | 
			
		||||
        index: usize,
 | 
			
		||||
        disk: &DomainDisk,
 | 
			
		||||
        disk: &DomainDisk<'_>,
 | 
			
		||||
    ) -> Result<()> {
 | 
			
		||||
        let id = (202 << 8) | (index << 4) as u64;
 | 
			
		||||
        let backend_items: Vec<(&str, String)> = vec![
 | 
			
		||||
@ -386,12 +428,13 @@ impl XenClient {
 | 
			
		||||
            domid,
 | 
			
		||||
            frontend_items,
 | 
			
		||||
            backend_items,
 | 
			
		||||
        )?;
 | 
			
		||||
        )
 | 
			
		||||
        .await?;
 | 
			
		||||
        Ok(())
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    #[allow(clippy::too_many_arguments, clippy::unnecessary_unwrap)]
 | 
			
		||||
    fn console_device_add(
 | 
			
		||||
    async fn console_device_add(
 | 
			
		||||
        &mut self,
 | 
			
		||||
        dom_path: &str,
 | 
			
		||||
        backend_dom_path: &str,
 | 
			
		||||
@ -444,18 +487,19 @@ impl XenClient {
 | 
			
		||||
            domid,
 | 
			
		||||
            frontend_entries,
 | 
			
		||||
            backend_entries,
 | 
			
		||||
        )?;
 | 
			
		||||
        )
 | 
			
		||||
        .await?;
 | 
			
		||||
        Ok(())
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn fs_9p_device_add(
 | 
			
		||||
    async fn fs_9p_device_add(
 | 
			
		||||
        &mut self,
 | 
			
		||||
        dom_path: &str,
 | 
			
		||||
        backend_dom_path: &str,
 | 
			
		||||
        backend_domid: u32,
 | 
			
		||||
        domid: u32,
 | 
			
		||||
        index: usize,
 | 
			
		||||
        filesystem: &DomainFilesystem,
 | 
			
		||||
        filesystem: &DomainFilesystem<'_>,
 | 
			
		||||
    ) -> Result<()> {
 | 
			
		||||
        let id = 90 + index as u64;
 | 
			
		||||
        let backend_items: Vec<(&str, String)> = vec![
 | 
			
		||||
@ -481,18 +525,19 @@ impl XenClient {
 | 
			
		||||
            domid,
 | 
			
		||||
            frontend_items,
 | 
			
		||||
            backend_items,
 | 
			
		||||
        )?;
 | 
			
		||||
        )
 | 
			
		||||
        .await?;
 | 
			
		||||
        Ok(())
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn vif_device_add(
 | 
			
		||||
    async fn vif_device_add(
 | 
			
		||||
        &mut self,
 | 
			
		||||
        dom_path: &str,
 | 
			
		||||
        backend_dom_path: &str,
 | 
			
		||||
        backend_domid: u32,
 | 
			
		||||
        domid: u32,
 | 
			
		||||
        index: usize,
 | 
			
		||||
        vif: &DomainNetworkInterface,
 | 
			
		||||
        vif: &DomainNetworkInterface<'_>,
 | 
			
		||||
    ) -> Result<()> {
 | 
			
		||||
        let id = 20 + index as u64;
 | 
			
		||||
        let mut backend_items: Vec<(&str, String)> = vec![
 | 
			
		||||
@ -538,12 +583,13 @@ impl XenClient {
 | 
			
		||||
            domid,
 | 
			
		||||
            frontend_items,
 | 
			
		||||
            backend_items,
 | 
			
		||||
        )?;
 | 
			
		||||
        )
 | 
			
		||||
        .await?;
 | 
			
		||||
        Ok(())
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    #[allow(clippy::too_many_arguments)]
 | 
			
		||||
    fn device_add(
 | 
			
		||||
    async fn device_add(
 | 
			
		||||
        &mut self,
 | 
			
		||||
        typ: &str,
 | 
			
		||||
        id: u64,
 | 
			
		||||
@ -589,36 +635,36 @@ impl XenClient {
 | 
			
		||||
            },
 | 
			
		||||
        ];
 | 
			
		||||
 | 
			
		||||
        let mut tx = self.store.transaction()?;
 | 
			
		||||
        tx.mknod(&frontend_path, frontend_perms)?;
 | 
			
		||||
        let mut tx = self.store.transaction().await?;
 | 
			
		||||
        tx.mknod(&frontend_path, frontend_perms).await?;
 | 
			
		||||
        for (p, value) in &frontend_items {
 | 
			
		||||
            let path = format!("{}/{}", frontend_path, *p);
 | 
			
		||||
            tx.write_string(&path, value)?;
 | 
			
		||||
            tx.write_string(&path, value).await?;
 | 
			
		||||
            if !console_zero {
 | 
			
		||||
                tx.set_perms(&path, frontend_perms)?;
 | 
			
		||||
                tx.set_perms(&path, frontend_perms).await?;
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        tx.mknod(&backend_path, backend_perms)?;
 | 
			
		||||
        tx.mknod(&backend_path, backend_perms).await?;
 | 
			
		||||
        for (p, value) in &backend_items {
 | 
			
		||||
            let path = format!("{}/{}", backend_path, *p);
 | 
			
		||||
            tx.write_string(&path, value)?;
 | 
			
		||||
            tx.write_string(&path, value).await?;
 | 
			
		||||
        }
 | 
			
		||||
        tx.commit()?;
 | 
			
		||||
        tx.commit().await?;
 | 
			
		||||
        Ok(())
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub fn destroy(&mut self, domid: u32) -> Result<()> {
 | 
			
		||||
        if let Err(err) = self.destroy_store(domid) {
 | 
			
		||||
    pub async fn destroy(&mut self, domid: u32) -> Result<()> {
 | 
			
		||||
        if let Err(err) = self.destroy_store(domid).await {
 | 
			
		||||
            warn!("failed to destroy store for domain {}: {}", domid, err);
 | 
			
		||||
        }
 | 
			
		||||
        self.call.destroy_domain(domid)?;
 | 
			
		||||
        Ok(())
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn destroy_store(&mut self, domid: u32) -> Result<()> {
 | 
			
		||||
        let dom_path = self.store.get_domain_path(domid)?;
 | 
			
		||||
        let vm_path = self.store.read_string(&format!("{}/vm", dom_path))?;
 | 
			
		||||
        if vm_path.is_empty() {
 | 
			
		||||
    async fn destroy_store(&mut self, domid: u32) -> Result<()> {
 | 
			
		||||
        let dom_path = self.store.get_domain_path(domid).await?;
 | 
			
		||||
        let vm_path = self.store.read_string(&format!("{}/vm", dom_path)).await?;
 | 
			
		||||
        if vm_path.is_none() {
 | 
			
		||||
            return Err(Error::DomainNonExistent);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@ -626,20 +672,27 @@ impl XenClient {
 | 
			
		||||
        let console_frontend_path = format!("{}/console", dom_path);
 | 
			
		||||
        let console_backend_path = self
 | 
			
		||||
            .store
 | 
			
		||||
            .read_string_optional(format!("{}/backend", console_frontend_path).as_str())?;
 | 
			
		||||
            .read_string(format!("{}/backend", console_frontend_path).as_str())
 | 
			
		||||
            .await?;
 | 
			
		||||
 | 
			
		||||
        for device_category in self
 | 
			
		||||
            .store
 | 
			
		||||
            .list_any(format!("{}/device", dom_path).as_str())?
 | 
			
		||||
            .list(format!("{}/device", dom_path).as_str())
 | 
			
		||||
            .await?
 | 
			
		||||
        {
 | 
			
		||||
            for device_id in self
 | 
			
		||||
                .store
 | 
			
		||||
                .list_any(format!("{}/device/{}", dom_path, device_category).as_str())?
 | 
			
		||||
                .list(format!("{}/device/{}", dom_path, device_category).as_str())
 | 
			
		||||
                .await?
 | 
			
		||||
            {
 | 
			
		||||
                let device_path = format!("{}/device/{}/{}", dom_path, device_category, device_id);
 | 
			
		||||
                let backend_path = self
 | 
			
		||||
                let Some(backend_path) = self
 | 
			
		||||
                    .store
 | 
			
		||||
                    .read_string(format!("{}/backend", device_path).as_str())?;
 | 
			
		||||
                    .read_string(format!("{}/backend", device_path).as_str())
 | 
			
		||||
                    .await?
 | 
			
		||||
                else {
 | 
			
		||||
                    continue;
 | 
			
		||||
                };
 | 
			
		||||
                backend_paths.push(backend_path);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
@ -647,16 +700,16 @@ impl XenClient {
 | 
			
		||||
        for backend in &backend_paths {
 | 
			
		||||
            let state_path = format!("{}/state", backend);
 | 
			
		||||
            let online_path = format!("{}/online", backend);
 | 
			
		||||
            let mut tx = self.store.transaction()?;
 | 
			
		||||
            let state = tx.read_string(&state_path)?;
 | 
			
		||||
            let mut tx = self.store.transaction().await?;
 | 
			
		||||
            let state = tx.read_string(&state_path).await?.unwrap_or(String::new());
 | 
			
		||||
            if state.is_empty() {
 | 
			
		||||
                break;
 | 
			
		||||
            }
 | 
			
		||||
            tx.write_string(&online_path, "0")?;
 | 
			
		||||
            tx.write_string(&online_path, "0").await?;
 | 
			
		||||
            if !state.is_empty() && u32::from_str(&state).unwrap_or(0) != 6 {
 | 
			
		||||
                tx.write_string(&state_path, "5")?;
 | 
			
		||||
                tx.write_string(&state_path, "5").await?;
 | 
			
		||||
            }
 | 
			
		||||
            tx.commit()?;
 | 
			
		||||
            tx.commit().await?;
 | 
			
		||||
 | 
			
		||||
            let mut count: u32 = 0;
 | 
			
		||||
            loop {
 | 
			
		||||
@ -664,7 +717,9 @@ impl XenClient {
 | 
			
		||||
                    warn!("unable to safely destroy backend: {}", backend);
 | 
			
		||||
                    break;
 | 
			
		||||
                }
 | 
			
		||||
                let state = self.store.read_string(&state_path)?;
 | 
			
		||||
                let Some(state) = self.store.read_string(&state_path).await? else {
 | 
			
		||||
                    break;
 | 
			
		||||
                };
 | 
			
		||||
                let state = i64::from_str(&state).unwrap_or(-1);
 | 
			
		||||
                if state == 6 {
 | 
			
		||||
                    break;
 | 
			
		||||
@ -674,7 +729,7 @@ impl XenClient {
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        let mut tx = self.store.transaction()?;
 | 
			
		||||
        let mut tx = self.store.transaction().await?;
 | 
			
		||||
        let mut backend_removals: Vec<String> = Vec::new();
 | 
			
		||||
        backend_removals.extend_from_slice(backend_paths.as_slice());
 | 
			
		||||
        if let Some(backend) = console_backend_path {
 | 
			
		||||
@ -683,20 +738,23 @@ impl XenClient {
 | 
			
		||||
        for path in &backend_removals {
 | 
			
		||||
            let path = PathBuf::from(path);
 | 
			
		||||
            let parent = path.parent().ok_or(Error::PathParentNotFound)?;
 | 
			
		||||
            tx.rm(parent.to_str().ok_or(Error::PathStringConversion)?)?;
 | 
			
		||||
            tx.rm(parent.to_str().ok_or(Error::PathStringConversion)?)
 | 
			
		||||
                .await?;
 | 
			
		||||
        }
 | 
			
		||||
        tx.rm(&vm_path)?;
 | 
			
		||||
        tx.rm(&dom_path)?;
 | 
			
		||||
        tx.commit()?;
 | 
			
		||||
        if let Some(vm_path) = vm_path {
 | 
			
		||||
            tx.rm(&vm_path).await?;
 | 
			
		||||
        }
 | 
			
		||||
        tx.rm(&dom_path).await?;
 | 
			
		||||
        tx.commit().await?;
 | 
			
		||||
        Ok(())
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub fn get_console_path(&mut self, domid: u32) -> Result<String> {
 | 
			
		||||
        let dom_path = self.store.get_domain_path(domid)?;
 | 
			
		||||
    pub async fn get_console_path(&mut self, domid: u32) -> Result<String> {
 | 
			
		||||
        let dom_path = self.store.get_domain_path(domid).await?;
 | 
			
		||||
        let console_tty_path = format!("{}/console/tty", dom_path);
 | 
			
		||||
        let mut tty: Option<String> = None;
 | 
			
		||||
        for _ in 0..5 {
 | 
			
		||||
            tty = self.store.read_string_optional(&console_tty_path)?;
 | 
			
		||||
            tty = self.store.read_string(&console_tty_path).await?;
 | 
			
		||||
            if tty.is_some() {
 | 
			
		||||
                break;
 | 
			
		||||
            }
 | 
			
		||||
 | 
			
		||||
@ -11,6 +11,11 @@ path = "src/lib.rs"
 | 
			
		||||
thiserror = { workspace = true }
 | 
			
		||||
libc = { workspace = true }
 | 
			
		||||
log = { workspace = true }
 | 
			
		||||
tokio = { workspace = true }
 | 
			
		||||
async-trait = { workspace = true }
 | 
			
		||||
 | 
			
		||||
[dev-dependencies]
 | 
			
		||||
futures = { workspace = true }
 | 
			
		||||
 | 
			
		||||
[dependencies.bytemuck]
 | 
			
		||||
workspace = true
 | 
			
		||||
 | 
			
		||||
@ -1,9 +1,10 @@
 | 
			
		||||
use futures::executor::block_on;
 | 
			
		||||
use xenstore::client::{XsdClient, XsdInterface};
 | 
			
		||||
use xenstore::error::Result;
 | 
			
		||||
use xenstore::sys::XSD_ERROR_EINVAL;
 | 
			
		||||
 | 
			
		||||
fn list_recursive(client: &mut XsdClient, level: usize, path: &str) -> Result<()> {
 | 
			
		||||
    let children = match client.list(path) {
 | 
			
		||||
    let children = match block_on(client.list(path)) {
 | 
			
		||||
        Ok(children) => children,
 | 
			
		||||
        Err(error) => {
 | 
			
		||||
            return if error.to_string() == XSD_ERROR_EINVAL.error {
 | 
			
		||||
@ -16,20 +17,16 @@ fn list_recursive(client: &mut XsdClient, level: usize, path: &str) -> Result<()
 | 
			
		||||
 | 
			
		||||
    for child in children {
 | 
			
		||||
        let full = format!("{}/{}", if path == "/" { "" } else { path }, child);
 | 
			
		||||
        let value = client.read(full.as_str())?;
 | 
			
		||||
        println!(
 | 
			
		||||
            "{}{} = {:?}",
 | 
			
		||||
            " ".repeat(level),
 | 
			
		||||
            child,
 | 
			
		||||
            String::from_utf8(value)?
 | 
			
		||||
        );
 | 
			
		||||
        let value = block_on(client.read_string(full.as_str()))?.expect("expected value");
 | 
			
		||||
        println!("{}{} = {:?}", " ".repeat(level), child, value,);
 | 
			
		||||
        list_recursive(client, level + 1, full.as_str())?;
 | 
			
		||||
    }
 | 
			
		||||
    Ok(())
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn main() -> Result<()> {
 | 
			
		||||
    let mut client = XsdClient::open()?;
 | 
			
		||||
#[tokio::main]
 | 
			
		||||
async fn main() -> Result<()> {
 | 
			
		||||
    let mut client = XsdClient::open().await?;
 | 
			
		||||
    list_recursive(&mut client, 0, "/")?;
 | 
			
		||||
    Ok(())
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -5,7 +5,8 @@ use std::fs::{self, metadata, File};
 | 
			
		||||
use std::io::{Read, Write};
 | 
			
		||||
use std::mem::size_of;
 | 
			
		||||
use std::os::unix::fs::FileTypeExt;
 | 
			
		||||
use std::os::unix::net::UnixStream;
 | 
			
		||||
use tokio::io::{AsyncReadExt, AsyncWriteExt};
 | 
			
		||||
use tokio::net::UnixStream;
 | 
			
		||||
 | 
			
		||||
const XEN_BUS_PATHS: &[&str] = &["/dev/xen/xenbus", "/var/run/xenstored/socket"];
 | 
			
		||||
 | 
			
		||||
@ -19,18 +20,21 @@ fn find_bus_path() -> Option<String> {
 | 
			
		||||
    None
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[async_trait::async_trait]
 | 
			
		||||
trait XsdTransport {
 | 
			
		||||
    fn xsd_write_all(&mut self, buf: &[u8]) -> Result<()>;
 | 
			
		||||
    fn xsd_read_exact(&mut self, buf: &mut [u8]) -> Result<()>;
 | 
			
		||||
    async fn xsd_write_all(&mut self, buf: &[u8]) -> Result<()>;
 | 
			
		||||
    async fn xsd_read_exact(&mut self, buf: &mut [u8]) -> Result<()>;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[async_trait::async_trait]
 | 
			
		||||
impl XsdTransport for UnixStream {
 | 
			
		||||
    fn xsd_write_all(&mut self, buf: &[u8]) -> Result<()> {
 | 
			
		||||
        Ok(self.write_all(buf)?)
 | 
			
		||||
    async fn xsd_write_all(&mut self, buf: &[u8]) -> Result<()> {
 | 
			
		||||
        Ok(self.write_all(buf).await?)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn xsd_read_exact(&mut self, buf: &mut [u8]) -> Result<()> {
 | 
			
		||||
        Ok(self.read_exact(buf)?)
 | 
			
		||||
    async fn xsd_read_exact(&mut self, buf: &mut [u8]) -> Result<()> {
 | 
			
		||||
        self.read_exact(buf).await?;
 | 
			
		||||
        Ok(())
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -45,12 +49,13 @@ impl XsdFileTransport {
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[async_trait::async_trait]
 | 
			
		||||
impl XsdTransport for XsdFileTransport {
 | 
			
		||||
    fn xsd_read_exact(&mut self, buf: &mut [u8]) -> Result<()> {
 | 
			
		||||
    async fn xsd_read_exact(&mut self, buf: &mut [u8]) -> Result<()> {
 | 
			
		||||
        Ok(self.handle.read_exact(buf)?)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn xsd_write_all(&mut self, buf: &[u8]) -> Result<()> {
 | 
			
		||||
    async fn xsd_write_all(&mut self, buf: &[u8]) -> Result<()> {
 | 
			
		||||
        self.handle.write_all(buf)?;
 | 
			
		||||
        self.handle.flush()?;
 | 
			
		||||
        Ok(())
 | 
			
		||||
@ -93,7 +98,7 @@ impl XsdResponse {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl XsdSocket {
 | 
			
		||||
    pub fn dial() -> Result<XsdSocket> {
 | 
			
		||||
    pub async fn open() -> Result<XsdSocket> {
 | 
			
		||||
        let path = match find_bus_path() {
 | 
			
		||||
            Some(path) => path,
 | 
			
		||||
            None => return Err(Error::BusNotFound),
 | 
			
		||||
@ -102,7 +107,7 @@ impl XsdSocket {
 | 
			
		||||
        let metadata = fs::metadata(&path)?;
 | 
			
		||||
        let file_type = metadata.file_type();
 | 
			
		||||
        if file_type.is_socket() {
 | 
			
		||||
            let stream = UnixStream::connect(&path)?;
 | 
			
		||||
            let stream = UnixStream::connect(&path).await?;
 | 
			
		||||
            return Ok(XsdSocket {
 | 
			
		||||
                handle: Box::new(stream),
 | 
			
		||||
            });
 | 
			
		||||
@ -113,7 +118,7 @@ impl XsdSocket {
 | 
			
		||||
        })
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub fn send(&mut self, tx: u32, typ: u32, buf: &[u8]) -> Result<XsdResponse> {
 | 
			
		||||
    pub async fn send(&mut self, tx: u32, typ: u32, buf: &[u8]) -> Result<XsdResponse> {
 | 
			
		||||
        let header = XsdMessageHeader {
 | 
			
		||||
            typ,
 | 
			
		||||
            req: 0,
 | 
			
		||||
@ -124,12 +129,14 @@ impl XsdSocket {
 | 
			
		||||
        let mut composed: Vec<u8> = Vec::new();
 | 
			
		||||
        composed.extend_from_slice(header_bytes);
 | 
			
		||||
        composed.extend_from_slice(buf);
 | 
			
		||||
        self.handle.xsd_write_all(&composed)?;
 | 
			
		||||
        self.handle.xsd_write_all(&composed).await?;
 | 
			
		||||
        let mut result_buf = vec![0u8; size_of::<XsdMessageHeader>()];
 | 
			
		||||
        self.handle.xsd_read_exact(result_buf.as_mut_slice())?;
 | 
			
		||||
        self.handle
 | 
			
		||||
            .xsd_read_exact(result_buf.as_mut_slice())
 | 
			
		||||
            .await?;
 | 
			
		||||
        let result_header = bytemuck::from_bytes::<XsdMessageHeader>(&result_buf);
 | 
			
		||||
        let mut payload = vec![0u8; result_header.len as usize];
 | 
			
		||||
        self.handle.xsd_read_exact(payload.as_mut_slice())?;
 | 
			
		||||
        self.handle.xsd_read_exact(payload.as_mut_slice()).await?;
 | 
			
		||||
        if result_header.typ == XSD_ERROR {
 | 
			
		||||
            let error = CString::from_vec_with_nul(payload)?;
 | 
			
		||||
            return Err(Error::ResponseError(error.into_string()?));
 | 
			
		||||
@ -138,18 +145,23 @@ impl XsdSocket {
 | 
			
		||||
        Ok(response)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub fn send_single(&mut self, tx: u32, typ: u32, string: &str) -> Result<XsdResponse> {
 | 
			
		||||
    pub async fn send_single(&mut self, tx: u32, typ: u32, string: &str) -> Result<XsdResponse> {
 | 
			
		||||
        let text = CString::new(string)?;
 | 
			
		||||
        let buf = text.as_bytes_with_nul();
 | 
			
		||||
        self.send(tx, typ, buf)
 | 
			
		||||
        self.send(tx, typ, buf).await
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub fn send_multiple(&mut self, tx: u32, typ: u32, array: &[&str]) -> Result<XsdResponse> {
 | 
			
		||||
    pub async fn send_multiple(
 | 
			
		||||
        &mut self,
 | 
			
		||||
        tx: u32,
 | 
			
		||||
        typ: u32,
 | 
			
		||||
        array: &[&str],
 | 
			
		||||
    ) -> Result<XsdResponse> {
 | 
			
		||||
        let mut buf: Vec<u8> = Vec::new();
 | 
			
		||||
        for item in array {
 | 
			
		||||
            buf.extend_from_slice(item.as_bytes());
 | 
			
		||||
            buf.push(0);
 | 
			
		||||
        }
 | 
			
		||||
        self.send(tx, typ, buf.as_slice())
 | 
			
		||||
        self.send(tx, typ, buf.as_slice()).await
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -35,85 +35,71 @@ impl XsPermission {
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
#[allow(async_fn_in_trait)]
 | 
			
		||||
pub trait XsdInterface {
 | 
			
		||||
    fn list(&mut self, path: &str) -> Result<Vec<String>>;
 | 
			
		||||
    fn read(&mut self, path: &str) -> Result<Vec<u8>>;
 | 
			
		||||
    fn read_string(&mut self, path: &str) -> Result<String>;
 | 
			
		||||
    fn write(&mut self, path: &str, data: Vec<u8>) -> Result<bool>;
 | 
			
		||||
    fn write_string(&mut self, path: &str, data: &str) -> Result<bool>;
 | 
			
		||||
    fn mkdir(&mut self, path: &str) -> Result<bool>;
 | 
			
		||||
    fn rm(&mut self, path: &str) -> Result<bool>;
 | 
			
		||||
    fn set_perms(&mut self, path: &str, perms: &[XsPermission]) -> Result<bool>;
 | 
			
		||||
    async fn list(&mut self, path: &str) -> Result<Vec<String>>;
 | 
			
		||||
    async fn read(&mut self, path: &str) -> Result<Option<Vec<u8>>>;
 | 
			
		||||
    async fn read_string(&mut self, path: &str) -> Result<Option<String>>;
 | 
			
		||||
    async fn write(&mut self, path: &str, data: Vec<u8>) -> Result<bool>;
 | 
			
		||||
    async fn write_string(&mut self, path: &str, data: &str) -> Result<bool>;
 | 
			
		||||
    async fn mkdir(&mut self, path: &str) -> Result<bool>;
 | 
			
		||||
    async fn rm(&mut self, path: &str) -> Result<bool>;
 | 
			
		||||
    async fn set_perms(&mut self, path: &str, perms: &[XsPermission]) -> Result<bool>;
 | 
			
		||||
 | 
			
		||||
    fn mknod(&mut self, path: &str, perms: &[XsPermission]) -> Result<bool> {
 | 
			
		||||
        let result1 = self.write_string(path, "")?;
 | 
			
		||||
        let result2 = self.set_perms(path, perms)?;
 | 
			
		||||
    async fn mknod(&mut self, path: &str, perms: &[XsPermission]) -> Result<bool> {
 | 
			
		||||
        let result1 = self.write_string(path, "").await?;
 | 
			
		||||
        let result2 = self.set_perms(path, perms).await?;
 | 
			
		||||
        Ok(result1 && result2)
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn read_string_optional(&mut self, path: &str) -> Result<Option<String>> {
 | 
			
		||||
        Ok(match self.read_string(path) {
 | 
			
		||||
            Ok(value) => Some(value),
 | 
			
		||||
            Err(error) => {
 | 
			
		||||
                if error.is_noent_response() {
 | 
			
		||||
                    None
 | 
			
		||||
                } else {
 | 
			
		||||
                    return Err(error);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        })
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn list_any(&mut self, path: &str) -> Result<Vec<String>> {
 | 
			
		||||
        Ok(match self.list(path) {
 | 
			
		||||
            Ok(value) => value,
 | 
			
		||||
            Err(error) => {
 | 
			
		||||
                if error.is_noent_response() {
 | 
			
		||||
                    Vec::new()
 | 
			
		||||
                } else {
 | 
			
		||||
                    return Err(error);
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        })
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl XsdClient {
 | 
			
		||||
    pub fn open() -> Result<XsdClient> {
 | 
			
		||||
        let socket = XsdSocket::dial()?;
 | 
			
		||||
    pub async fn open() -> Result<XsdClient> {
 | 
			
		||||
        let socket = XsdSocket::open().await?;
 | 
			
		||||
        Ok(XsdClient { socket })
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn list(&mut self, tx: u32, path: &str) -> Result<Vec<String>> {
 | 
			
		||||
    async fn list(&mut self, tx: u32, path: &str) -> Result<Vec<String>> {
 | 
			
		||||
        trace!("list tx={tx} path={path}");
 | 
			
		||||
        let response = self.socket.send_single(tx, XSD_DIRECTORY, path)?;
 | 
			
		||||
        let response = self.socket.send_single(tx, XSD_DIRECTORY, path).await?;
 | 
			
		||||
        response.parse_string_vec()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn read(&mut self, tx: u32, path: &str) -> Result<Vec<u8>> {
 | 
			
		||||
    async fn read(&mut self, tx: u32, path: &str) -> Result<Option<Vec<u8>>> {
 | 
			
		||||
        trace!("read tx={tx} path={path}");
 | 
			
		||||
        let response = self.socket.send_single(tx, XSD_READ, path)?;
 | 
			
		||||
        Ok(response.payload)
 | 
			
		||||
        match self.socket.send_single(tx, XSD_READ, path).await {
 | 
			
		||||
            Ok(response) => Ok(Some(response.payload)),
 | 
			
		||||
            Err(error) => {
 | 
			
		||||
                if error.is_noent_response() {
 | 
			
		||||
                    Ok(None)
 | 
			
		||||
                } else {
 | 
			
		||||
                    Err(error)
 | 
			
		||||
                }
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn write(&mut self, tx: u32, path: &str, data: Vec<u8>) -> Result<bool> {
 | 
			
		||||
    async fn write(&mut self, tx: u32, path: &str, data: Vec<u8>) -> Result<bool> {
 | 
			
		||||
        trace!("write tx={tx} path={path} data={:?}", data);
 | 
			
		||||
        let mut buffer = Vec::new();
 | 
			
		||||
        let path = CString::new(path)?;
 | 
			
		||||
        buffer.extend_from_slice(path.as_bytes_with_nul());
 | 
			
		||||
        buffer.extend_from_slice(data.as_slice());
 | 
			
		||||
        let response = self.socket.send(tx, XSD_WRITE, buffer.as_slice())?;
 | 
			
		||||
        let response = self.socket.send(tx, XSD_WRITE, buffer.as_slice()).await?;
 | 
			
		||||
        response.parse_bool()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn mkdir(&mut self, tx: u32, path: &str) -> Result<bool> {
 | 
			
		||||
    async fn mkdir(&mut self, tx: u32, path: &str) -> Result<bool> {
 | 
			
		||||
        trace!("mkdir tx={tx} path={path}");
 | 
			
		||||
        self.socket.send_single(tx, XSD_MKDIR, path)?.parse_bool()
 | 
			
		||||
        self.socket
 | 
			
		||||
            .send_single(tx, XSD_MKDIR, path)
 | 
			
		||||
            .await?
 | 
			
		||||
            .parse_bool()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn rm(&mut self, tx: u32, path: &str) -> Result<bool> {
 | 
			
		||||
    async fn rm(&mut self, tx: u32, path: &str) -> Result<bool> {
 | 
			
		||||
        trace!("rm tx={tx} path={path}");
 | 
			
		||||
        let result = self.socket.send_single(tx, XSD_RM, path);
 | 
			
		||||
        let result = self.socket.send_single(tx, XSD_RM, path).await;
 | 
			
		||||
        if let Err(error) = result {
 | 
			
		||||
            if error.is_noent_response() {
 | 
			
		||||
                return Ok(true);
 | 
			
		||||
@ -123,7 +109,7 @@ impl XsdClient {
 | 
			
		||||
        result.unwrap().parse_bool()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn set_perms(&mut self, tx: u32, path: &str, perms: &[XsPermission]) -> Result<bool> {
 | 
			
		||||
    async fn set_perms(&mut self, tx: u32, path: &str, perms: &[XsPermission]) -> Result<bool> {
 | 
			
		||||
        trace!("set_perms tx={tx} path={path} perms={:?}", perms);
 | 
			
		||||
        let mut items: Vec<String> = Vec::new();
 | 
			
		||||
        items.push(path.to_string());
 | 
			
		||||
@ -131,36 +117,46 @@ impl XsdClient {
 | 
			
		||||
            items.push(perm.encode()?);
 | 
			
		||||
        }
 | 
			
		||||
        let items_str: Vec<&str> = items.iter().map(|x| x.as_str()).collect();
 | 
			
		||||
        let response = self.socket.send_multiple(tx, XSD_SET_PERMS, &items_str)?;
 | 
			
		||||
        let response = self
 | 
			
		||||
            .socket
 | 
			
		||||
            .send_multiple(tx, XSD_SET_PERMS, &items_str)
 | 
			
		||||
            .await?;
 | 
			
		||||
        response.parse_bool()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub fn transaction(&mut self) -> Result<XsdTransaction> {
 | 
			
		||||
    pub async fn transaction(&mut self) -> Result<XsdTransaction> {
 | 
			
		||||
        trace!("transaction start");
 | 
			
		||||
        let response = self.socket.send_single(0, XSD_TRANSACTION_START, "")?;
 | 
			
		||||
        let response = self
 | 
			
		||||
            .socket
 | 
			
		||||
            .send_single(0, XSD_TRANSACTION_START, "")
 | 
			
		||||
            .await?;
 | 
			
		||||
        let str = response.parse_string()?;
 | 
			
		||||
        let tx = str.parse::<u32>()?;
 | 
			
		||||
        Ok(XsdTransaction { client: self, tx })
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub fn get_domain_path(&mut self, domid: u32) -> Result<String> {
 | 
			
		||||
        let response =
 | 
			
		||||
            self.socket
 | 
			
		||||
                .send_single(0, XSD_GET_DOMAIN_PATH, domid.to_string().as_str())?;
 | 
			
		||||
    pub async fn get_domain_path(&mut self, domid: u32) -> Result<String> {
 | 
			
		||||
        let response = self
 | 
			
		||||
            .socket
 | 
			
		||||
            .send_single(0, XSD_GET_DOMAIN_PATH, domid.to_string().as_str())
 | 
			
		||||
            .await?;
 | 
			
		||||
        response.parse_string()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub fn introduce_domain(&mut self, domid: u32, mfn: u64, evtchn: u32) -> Result<bool> {
 | 
			
		||||
    pub async fn introduce_domain(&mut self, domid: u32, mfn: u64, evtchn: u32) -> Result<bool> {
 | 
			
		||||
        trace!("introduce domain domid={domid} mfn={mfn} evtchn={evtchn}");
 | 
			
		||||
        let response = self.socket.send_multiple(
 | 
			
		||||
            0,
 | 
			
		||||
            XSD_INTRODUCE,
 | 
			
		||||
            &[
 | 
			
		||||
                domid.to_string().as_str(),
 | 
			
		||||
                mfn.to_string().as_str(),
 | 
			
		||||
                evtchn.to_string().as_str(),
 | 
			
		||||
            ],
 | 
			
		||||
        )?;
 | 
			
		||||
        let response = self
 | 
			
		||||
            .socket
 | 
			
		||||
            .send_multiple(
 | 
			
		||||
                0,
 | 
			
		||||
                XSD_INTRODUCE,
 | 
			
		||||
                &[
 | 
			
		||||
                    domid.to_string().as_str(),
 | 
			
		||||
                    mfn.to_string().as_str(),
 | 
			
		||||
                    evtchn.to_string().as_str(),
 | 
			
		||||
                ],
 | 
			
		||||
            )
 | 
			
		||||
            .await?;
 | 
			
		||||
        response.parse_bool()
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -171,89 +167,104 @@ pub struct XsdTransaction<'a> {
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl XsdInterface for XsdClient {
 | 
			
		||||
    fn list(&mut self, path: &str) -> Result<Vec<String>> {
 | 
			
		||||
        self.list(0, path)
 | 
			
		||||
    async fn list(&mut self, path: &str) -> Result<Vec<String>> {
 | 
			
		||||
        self.list(0, path).await
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn read(&mut self, path: &str) -> Result<Vec<u8>> {
 | 
			
		||||
        self.read(0, path)
 | 
			
		||||
    async fn read(&mut self, path: &str) -> Result<Option<Vec<u8>>> {
 | 
			
		||||
        self.read(0, path).await
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn read_string(&mut self, path: &str) -> Result<String> {
 | 
			
		||||
        Ok(String::from_utf8(self.read(0, path)?)?)
 | 
			
		||||
    async fn read_string(&mut self, path: &str) -> Result<Option<String>> {
 | 
			
		||||
        match self.read(0, path).await {
 | 
			
		||||
            Ok(value) => match value {
 | 
			
		||||
                Some(value) => Ok(Some(String::from_utf8(value)?)),
 | 
			
		||||
                None => Ok(None),
 | 
			
		||||
            },
 | 
			
		||||
            Err(error) => Err(error),
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn write(&mut self, path: &str, data: Vec<u8>) -> Result<bool> {
 | 
			
		||||
        self.write(0, path, data)
 | 
			
		||||
    async fn write(&mut self, path: &str, data: Vec<u8>) -> Result<bool> {
 | 
			
		||||
        self.write(0, path, data).await
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn write_string(&mut self, path: &str, data: &str) -> Result<bool> {
 | 
			
		||||
        self.write(0, path, data.as_bytes().to_vec())
 | 
			
		||||
    async fn write_string(&mut self, path: &str, data: &str) -> Result<bool> {
 | 
			
		||||
        self.write(0, path, data.as_bytes().to_vec()).await
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn mkdir(&mut self, path: &str) -> Result<bool> {
 | 
			
		||||
        self.mkdir(0, path)
 | 
			
		||||
    async fn mkdir(&mut self, path: &str) -> Result<bool> {
 | 
			
		||||
        self.mkdir(0, path).await
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn rm(&mut self, path: &str) -> Result<bool> {
 | 
			
		||||
        self.rm(0, path)
 | 
			
		||||
    async fn rm(&mut self, path: &str) -> Result<bool> {
 | 
			
		||||
        self.rm(0, path).await
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn set_perms(&mut self, path: &str, perms: &[XsPermission]) -> Result<bool> {
 | 
			
		||||
        self.set_perms(0, path, perms)
 | 
			
		||||
    async fn set_perms(&mut self, path: &str, perms: &[XsPermission]) -> Result<bool> {
 | 
			
		||||
        self.set_perms(0, path, perms).await
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl XsdInterface for XsdTransaction<'_> {
 | 
			
		||||
    fn list(&mut self, path: &str) -> Result<Vec<String>> {
 | 
			
		||||
        self.client.list(self.tx, path)
 | 
			
		||||
    async fn list(&mut self, path: &str) -> Result<Vec<String>> {
 | 
			
		||||
        self.client.list(self.tx, path).await
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn read(&mut self, path: &str) -> Result<Vec<u8>> {
 | 
			
		||||
        self.client.read(self.tx, path)
 | 
			
		||||
    async fn read(&mut self, path: &str) -> Result<Option<Vec<u8>>> {
 | 
			
		||||
        self.client.read(self.tx, path).await
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn read_string(&mut self, path: &str) -> Result<String> {
 | 
			
		||||
        Ok(String::from_utf8(self.client.read(self.tx, path)?)?)
 | 
			
		||||
    async fn read_string(&mut self, path: &str) -> Result<Option<String>> {
 | 
			
		||||
        match self.client.read(self.tx, path).await {
 | 
			
		||||
            Ok(value) => match value {
 | 
			
		||||
                Some(value) => Ok(Some(String::from_utf8(value)?)),
 | 
			
		||||
                None => Ok(None),
 | 
			
		||||
            },
 | 
			
		||||
            Err(error) => Err(error),
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn write(&mut self, path: &str, data: Vec<u8>) -> Result<bool> {
 | 
			
		||||
        self.client.write(self.tx, path, data)
 | 
			
		||||
    async fn write(&mut self, path: &str, data: Vec<u8>) -> Result<bool> {
 | 
			
		||||
        self.client.write(self.tx, path, data).await
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn write_string(&mut self, path: &str, data: &str) -> Result<bool> {
 | 
			
		||||
        self.client.write(self.tx, path, data.as_bytes().to_vec())
 | 
			
		||||
    async fn write_string(&mut self, path: &str, data: &str) -> Result<bool> {
 | 
			
		||||
        self.client
 | 
			
		||||
            .write(self.tx, path, data.as_bytes().to_vec())
 | 
			
		||||
            .await
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn mkdir(&mut self, path: &str) -> Result<bool> {
 | 
			
		||||
        self.client.mkdir(self.tx, path)
 | 
			
		||||
    async fn mkdir(&mut self, path: &str) -> Result<bool> {
 | 
			
		||||
        self.client.mkdir(self.tx, path).await
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn rm(&mut self, path: &str) -> Result<bool> {
 | 
			
		||||
        self.client.rm(self.tx, path)
 | 
			
		||||
    async fn rm(&mut self, path: &str) -> Result<bool> {
 | 
			
		||||
        self.client.rm(self.tx, path).await
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    fn set_perms(&mut self, path: &str, perms: &[XsPermission]) -> Result<bool> {
 | 
			
		||||
        self.client.set_perms(self.tx, path, perms)
 | 
			
		||||
    async fn set_perms(&mut self, path: &str, perms: &[XsPermission]) -> Result<bool> {
 | 
			
		||||
        self.client.set_perms(self.tx, path, perms).await
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
impl XsdTransaction<'_> {
 | 
			
		||||
    pub fn end(&mut self, abort: bool) -> Result<bool> {
 | 
			
		||||
    pub async fn end(&mut self, abort: bool) -> Result<bool> {
 | 
			
		||||
        let abort_str = if abort { "F" } else { "T" };
 | 
			
		||||
 | 
			
		||||
        trace!("transaction end abort={}", abort);
 | 
			
		||||
        self.client
 | 
			
		||||
            .socket
 | 
			
		||||
            .send_single(self.tx, XSD_TRANSACTION_END, abort_str)?
 | 
			
		||||
            .send_single(self.tx, XSD_TRANSACTION_END, abort_str)
 | 
			
		||||
            .await?
 | 
			
		||||
            .parse_bool()
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub fn commit(&mut self) -> Result<bool> {
 | 
			
		||||
        self.end(false)
 | 
			
		||||
    pub async fn commit(&mut self) -> Result<bool> {
 | 
			
		||||
        self.end(false).await
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    pub fn abort(&mut self) -> Result<bool> {
 | 
			
		||||
        self.end(true)
 | 
			
		||||
    pub async fn abort(&mut self) -> Result<bool> {
 | 
			
		||||
        self.end(true).await
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user