controller: implement automatic exit when process has exited

This commit is contained in:
2024-02-23 05:26:32 +00:00
parent 496cdd37be
commit 89e4f1a23d
3 changed files with 34 additions and 46 deletions

View File

@@ -1,5 +1,11 @@
use anyhow::{anyhow, Result};
use std::{process::exit, time::Duration};
use anyhow::{anyhow, Result};
use log::warn;
use tokio::time::sleep;
use xenstore::client::XsdInterface;
use super::destroy::ControllerDestroy;
use crate::console::XenConsole;
use super::ControllerContext;
@@ -22,7 +28,24 @@ impl ControllerConsole<'_> {
let domid = info.domid;
let tty = self.context.xen.get_console_path(domid).await?;
let console = XenConsole::new(&tty).await?;
console.attach().await?;
Ok(())
let dom_path = self.context.xen.store.get_domain_path(domid).await?;
tokio::task::spawn(async move {
if let Err(error) = console.attach().await {
warn!("failed to attach to console: {}", error);
}
});
let exit_code_path = format!("{}/krata/guest/exit-code", dom_path);
loop {
let Some(code) = self.context.xen.store.read_string(&exit_code_path).await? else {
sleep(Duration::from_secs(1)).await;
continue;
};
let mut destroy = ControllerDestroy::new(self.context);
destroy.perform(&domid.to_string()).await?;
exit(code.parse::<i32>()?);
}
}
}