Skip to content

Commit

Permalink
runtime-rs: add validate method and refine the new method
Browse files Browse the repository at this point in the history
We need first do validate the QmpSocket fields and make
them valid. And we'll pass QMP socket fd to Qemu cmdline
when launching qemu process.

Fixes: kata-containers#9603

Signed-off-by: Alex Lyn <alex.lyn@antgroup.com>
  • Loading branch information
Apokleos committed May 7, 2024
1 parent 3e64728 commit e59dcf0
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions src/runtime-rs/crates/hypervisor/src/qemu/cmdline_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ use crate::{kernel_param::KernelParams, Address, HypervisorConfig};

use anyhow::{anyhow, Context, Result};
use async_trait::async_trait;
use kata_types::config::KATA_PATH;
use std::collections::HashMap;
use std::fmt::Display;
use std::fs::{read_to_string, File};
use std::os::fd::AsRawFd;
use std::os::fd::{AsRawFd, FromRawFd, IntoRawFd};
use std::os::unix::net::UnixListener;
use tokio;

// These should have been called MiB and GiB for better readability but the
Expand Down Expand Up @@ -1238,16 +1240,44 @@ pub struct QmpSocket {
}

impl QmpSocket {
fn new(_sid: &str) -> Result<Self> {
fn new(sid: &str) -> Result<Self> {
let sock_path = [KATA_PATH, sid, QMP_SOCKET_FILE].join("/");
let listener = UnixListener::bind(sock_path).context("unix listener bind failed.")?;

let raw_fd = listener.into_raw_fd();
clear_cloexec(raw_fd).context("clearing unix listenser O_CLOEXEC failed")?;
let sock_file = unsafe { File::from_raw_fd(raw_fd) };

Ok(QmpSocket {
socket_type: QMP_SOCKET_TYPE.to_owned(),
protocol: MonitorProtocol::new("qmp"),
fd: None,
fd: Some(sock_file),
name: QMP_SOCKET_FILE.to_owned(),
server: true,
nowait: true,
})
}

// validate returns true if the QmpSocket structure is valid and complete.
#[allow(dead_code)]
fn validate(&self) -> bool {
if self.socket_type.as_str() != QMP_SOCKET_TYPE {
return false;
}

if self.name.is_empty() && self.fd.is_none() {
return false;
}

if self.protocol != MonitorProtocol::Hmp
&& self.protocol != MonitorProtocol::Qmp
&& self.protocol != MonitorProtocol::QmpPretty
{
return false;
}

true
}
}

#[async_trait]
Expand Down

0 comments on commit e59dcf0

Please sign in to comment.