2024-03-27 02:54:39 +00:00
|
|
|
use crate::{
|
|
|
|
childwait::{ChildEvent, ChildWait},
|
|
|
|
death,
|
2024-04-12 00:34:46 -07:00
|
|
|
metrics::MetricsCollector,
|
2024-03-27 02:54:39 +00:00
|
|
|
};
|
2024-02-23 03:25:06 +00:00
|
|
|
use anyhow::Result;
|
2024-03-30 23:23:59 +00:00
|
|
|
use cgroups_rs::Cgroup;
|
2024-03-28 22:38:21 +00:00
|
|
|
use krata::idm::{
|
2024-04-21 21:00:32 -07:00
|
|
|
client::IdmInternalClient,
|
|
|
|
internal::{
|
|
|
|
event::Event as EventType, request::Request as RequestType,
|
|
|
|
response::Response as ResponseType, Event, ExitEvent, MetricsResponse, PingResponse,
|
|
|
|
Request, Response,
|
2024-04-12 00:34:46 -07:00
|
|
|
},
|
2024-03-28 22:38:21 +00:00
|
|
|
};
|
2024-04-12 00:34:46 -07:00
|
|
|
use log::debug;
|
2024-03-27 02:54:39 +00:00
|
|
|
use nix::unistd::Pid;
|
2024-04-12 00:34:46 -07:00
|
|
|
use tokio::{select, sync::broadcast};
|
2024-02-23 03:25:06 +00:00
|
|
|
|
2024-03-15 16:11:35 +00:00
|
|
|
pub struct GuestBackground {
|
2024-04-21 21:00:32 -07:00
|
|
|
idm: IdmInternalClient,
|
2024-02-23 03:25:06 +00:00
|
|
|
child: Pid,
|
2024-03-30 23:23:59 +00:00
|
|
|
_cgroup: Cgroup,
|
2024-02-23 03:25:06 +00:00
|
|
|
wait: ChildWait,
|
|
|
|
}
|
|
|
|
|
2024-03-15 16:11:35 +00:00
|
|
|
impl GuestBackground {
|
2024-04-21 21:00:32 -07:00
|
|
|
pub async fn new(
|
|
|
|
idm: IdmInternalClient,
|
|
|
|
cgroup: Cgroup,
|
|
|
|
child: Pid,
|
|
|
|
) -> Result<GuestBackground> {
|
2024-03-15 16:11:35 +00:00
|
|
|
Ok(GuestBackground {
|
2024-03-28 22:38:21 +00:00
|
|
|
idm,
|
2024-02-23 03:25:06 +00:00
|
|
|
child,
|
2024-03-30 23:23:59 +00:00
|
|
|
_cgroup: cgroup,
|
2024-02-23 03:25:06 +00:00
|
|
|
wait: ChildWait::new()?,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn run(&mut self) -> Result<()> {
|
2024-04-12 00:34:46 -07:00
|
|
|
let mut event_subscription = self.idm.subscribe().await?;
|
|
|
|
let mut requests_subscription = self.idm.requests().await?;
|
2024-02-23 03:25:06 +00:00
|
|
|
loop {
|
|
|
|
select! {
|
2024-04-12 00:34:46 -07:00
|
|
|
x = event_subscription.recv() => match x {
|
|
|
|
Ok(_event) => {
|
2024-03-28 22:38:21 +00:00
|
|
|
|
|
|
|
},
|
|
|
|
|
2024-04-12 00:34:46 -07:00
|
|
|
Err(broadcast::error::RecvError::Closed) => {
|
|
|
|
debug!("idm packet channel closed");
|
2024-03-28 22:38:21 +00:00
|
|
|
break;
|
2024-04-12 00:34:46 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
_ => {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
x = requests_subscription.recv() => match x {
|
2024-04-21 21:00:32 -07:00
|
|
|
Ok((id, request)) => {
|
|
|
|
self.handle_idm_request(id, request).await?;
|
2024-04-12 00:34:46 -07:00
|
|
|
},
|
|
|
|
|
|
|
|
Err(broadcast::error::RecvError::Closed) => {
|
|
|
|
debug!("idm packet channel closed");
|
|
|
|
break;
|
|
|
|
},
|
|
|
|
|
|
|
|
_ => {
|
|
|
|
continue;
|
2024-03-28 22:38:21 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2024-02-23 03:25:06 +00:00
|
|
|
event = self.wait.recv() => match event {
|
|
|
|
Some(event) => self.child_event(event).await?,
|
|
|
|
None => {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-04-21 21:00:32 -07:00
|
|
|
async fn handle_idm_request(&mut self, id: u64, packet: Request) -> Result<()> {
|
2024-04-12 00:34:46 -07:00
|
|
|
match packet.request {
|
2024-04-21 21:00:32 -07:00
|
|
|
Some(RequestType::Ping(_)) => {
|
2024-04-12 00:34:46 -07:00
|
|
|
self.idm
|
2024-04-21 21:00:32 -07:00
|
|
|
.respond(
|
|
|
|
id,
|
|
|
|
Response {
|
|
|
|
response: Some(ResponseType::Ping(PingResponse {})),
|
|
|
|
},
|
|
|
|
)
|
2024-04-12 00:34:46 -07:00
|
|
|
.await?;
|
|
|
|
}
|
|
|
|
|
2024-04-21 21:00:32 -07:00
|
|
|
Some(RequestType::Metrics(_)) => {
|
2024-04-12 00:34:46 -07:00
|
|
|
let metrics = MetricsCollector::new()?;
|
|
|
|
let root = metrics.collect()?;
|
2024-04-21 21:00:32 -07:00
|
|
|
let response = Response {
|
|
|
|
response: Some(ResponseType::Metrics(MetricsResponse { root: Some(root) })),
|
|
|
|
};
|
2024-04-12 00:34:46 -07:00
|
|
|
|
2024-04-21 21:00:32 -07:00
|
|
|
self.idm.respond(id, response).await?;
|
2024-04-12 00:34:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
None => {}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2024-02-23 03:25:06 +00:00
|
|
|
async fn child_event(&mut self, event: ChildEvent) -> Result<()> {
|
|
|
|
if event.pid == self.child {
|
2024-03-28 22:38:21 +00:00
|
|
|
self.idm
|
2024-04-21 21:00:32 -07:00
|
|
|
.emit(Event {
|
|
|
|
event: Some(EventType::Exit(ExitEvent { code: event.status })),
|
2024-03-28 22:38:21 +00:00
|
|
|
})
|
|
|
|
.await?;
|
2024-03-27 02:54:39 +00:00
|
|
|
death(event.status).await?;
|
2024-02-23 03:25:06 +00:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|