krata/xen/xenstore/src/bus.rs

148 lines
4.3 KiB
Rust
Raw Normal View History

use crate::sys::{XsdMessageHeader, XSD_ERROR};
2024-01-17 13:22:47 +00:00
use std::ffi::{CString, FromVecWithNulError, IntoStringError, NulError};
2024-01-08 20:43:16 +00:00
use std::fs::metadata;
2024-01-30 09:49:56 +00:00
use std::io;
2024-01-08 20:43:16 +00:00
use std::io::{Read, Write};
use std::mem::size_of;
use std::net::Shutdown;
2024-01-08 23:04:06 +00:00
use std::num::ParseIntError;
2024-01-08 20:43:16 +00:00
use std::os::unix::net::UnixStream;
use std::str::Utf8Error;
use std::string::FromUtf8Error;
2024-01-30 09:49:56 +00:00
use thiserror::Error;
2024-01-08 20:43:16 +00:00
const XEN_BUS_PATHS: &[&str] = &["/var/run/xenstored/socket"];
2024-01-08 20:43:16 +00:00
fn find_bus_path() -> Option<String> {
for path in XEN_BUS_PATHS {
match metadata(path) {
Ok(_) => return Some(String::from(*path)),
Err(_) => continue,
2024-01-08 20:43:16 +00:00
}
}
None
}
2024-01-30 09:49:56 +00:00
#[derive(Error, Debug)]
pub enum XsdBusError {
#[error("io issue encountered")]
Io(#[from] io::Error),
#[error("utf8 string decode failed")]
Utf8DecodeString(#[from] FromUtf8Error),
#[error("utf8 str decode failed")]
Utf8DecodeStr(#[from] Utf8Error),
#[error("unable to decode cstring as utf8")]
Utf8DecodeCstring(#[from] IntoStringError),
#[error("nul byte found in string")]
NulByteFoundString(#[from] NulError),
#[error("unable to find nul byte in vec")]
VecNulByteNotFound(#[from] FromVecWithNulError),
#[error("unable to parse integer")]
ParseInt(#[from] ParseIntError),
#[error("bus was not found on any available path")]
BusNotFound,
#[error("store responded with error: `{0}`")]
ResponseError(String),
#[error("invalid permissions provided")]
InvalidPermissions,
2024-01-17 13:22:47 +00:00
}
2024-01-08 20:43:16 +00:00
pub struct XsdSocket {
handle: UnixStream,
2024-01-08 20:43:16 +00:00
}
#[derive(Debug)]
pub struct XsdResponse {
pub header: XsdMessageHeader,
pub payload: Vec<u8>,
2024-01-08 20:43:16 +00:00
}
impl XsdResponse {
2024-01-08 23:04:06 +00:00
pub fn parse_string(&self) -> Result<String, XsdBusError> {
2024-01-17 13:22:47 +00:00
Ok(CString::from_vec_with_nul(self.payload.clone())?.into_string()?)
2024-01-08 23:04:06 +00:00
}
2024-01-08 20:43:16 +00:00
pub fn parse_string_vec(&self) -> Result<Vec<String>, XsdBusError> {
let mut strings: Vec<String> = Vec::new();
let mut buffer: Vec<u8> = Vec::new();
for b in &self.payload {
if *b == 0 {
let string = String::from_utf8(buffer.clone())?;
strings.push(string);
buffer.clear();
continue;
}
buffer.push(*b);
2024-01-08 20:43:16 +00:00
}
Ok(strings)
}
pub fn parse_bool(&self) -> Result<bool, XsdBusError> {
2024-01-18 14:15:42 +00:00
Ok(true)
}
2024-01-08 20:43:16 +00:00
}
impl XsdSocket {
pub fn dial() -> Result<XsdSocket, XsdBusError> {
let path = match find_bus_path() {
Some(path) => path,
2024-01-30 09:49:56 +00:00
None => return Err(XsdBusError::BusNotFound),
2024-01-08 20:43:16 +00:00
};
let stream = UnixStream::connect(path)?;
Ok(XsdSocket { handle: stream })
}
pub fn send(&mut self, tx: u32, typ: u32, buf: &[u8]) -> Result<XsdResponse, XsdBusError> {
let header = XsdMessageHeader {
typ,
req: 0,
tx,
len: buf.len() as u32,
2024-01-08 20:43:16 +00:00
};
self.handle.write_all(bytemuck::bytes_of(&header))?;
self.handle.write_all(buf)?;
let mut result_buf = vec![0u8; size_of::<XsdMessageHeader>()];
self.handle.read_exact(result_buf.as_mut_slice())?;
2024-01-08 20:43:16 +00:00
let result_header = bytemuck::from_bytes::<XsdMessageHeader>(&result_buf);
let mut payload = vec![0u8; result_header.len as usize];
self.handle.read_exact(payload.as_mut_slice())?;
if result_header.typ == XSD_ERROR {
let error = CString::from_vec_with_nul(payload)?;
2024-01-30 09:49:56 +00:00
return Err(XsdBusError::ResponseError(error.into_string()?));
2024-01-08 20:43:16 +00:00
}
2024-01-08 22:16:33 +00:00
let response = XsdResponse { header, payload };
2024-01-08 20:43:16 +00:00
Ok(response)
}
pub fn send_single(
&mut self,
tx: u32,
typ: u32,
string: &str,
) -> Result<XsdResponse, XsdBusError> {
2024-01-09 23:40:17 +00:00
let text = CString::new(string)?;
let buf = text.as_bytes_with_nul();
self.send(tx, typ, buf)
2024-01-08 20:43:16 +00:00
}
2024-01-09 23:40:17 +00:00
pub fn send_multiple(
&mut self,
tx: u32,
typ: u32,
array: &[&str],
) -> Result<XsdResponse, XsdBusError> {
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())
}
2024-01-08 20:43:16 +00:00
}
impl Drop for XsdSocket {
fn drop(&mut self) {
self.handle.shutdown(Shutdown::Both).unwrap()
}
}