From 9af2968a7dc47b89bf07ea9dc5e735084efcfa3a Mon Sep 17 00:00:00 2001 From: Sebastien Boeuf Date: Tue, 20 Jul 2021 15:31:32 +0200 Subject: [PATCH] api_client: Add ability to send file descriptors Allow the user to send a list of file descriptors along with the HTTP request. Signed-off-by: Sebastien Boeuf --- Cargo.lock | 3 +++ api_client/Cargo.toml | 3 +++ api_client/src/lib.rs | 25 ++++++++++++++++++++----- 3 files changed, 26 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6137b4e91..5696891ec 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -51,6 +51,9 @@ checksum = "595d3cfa7a60d4555cb5067b99f07142a08ea778de5cf993f7b75c7d8fabc486" [[package]] name = "api_client" version = "0.1.0" +dependencies = [ + "vmm-sys-util", +] [[package]] name = "arc-swap" diff --git a/api_client/Cargo.toml b/api_client/Cargo.toml index b3f76206d..9cac2a00c 100644 --- a/api_client/Cargo.toml +++ b/api_client/Cargo.toml @@ -3,3 +3,6 @@ name = "api_client" version = "0.1.0" authors = ["The Cloud Hypervisor Authors"] edition = "2018" + +[dependencies] +vmm-sys-util = "0.8.0" \ No newline at end of file diff --git a/api_client/src/lib.rs b/api_client/src/lib.rs index db553fe2b..1c36b05ad 100644 --- a/api_client/src/lib.rs +++ b/api_client/src/lib.rs @@ -5,10 +5,13 @@ use std::fmt; use std::io::{Read, Write}; +use std::os::unix::io::RawFd; +use vmm_sys_util::sock_ctrl_msg::ScmSocket; #[derive(Debug)] pub enum Error { Socket(std::io::Error), + SocketSendFds(vmm_sys_util::errno::Error), StatusCodeParsing(std::num::ParseIntError), MissingProtocol, ContentLengthParsing(std::num::ParseIntError), @@ -20,6 +23,7 @@ impl fmt::Display for Error { use Error::*; match self { Socket(e) => write!(f, "Error writing to or reading from HTTP socket: {}", e), + SocketSendFds(e) => write!(f, "Error writing to or reading from HTTP socket: {}", e), StatusCodeParsing(e) => write!(f, "Error parsing HTTP status code: {}", e), MissingProtocol => write!(f, "HTTP output is missing protocol statement"), ContentLengthParsing(e) => write!(f, "Error parsing HTTP Content-Length field: {}", e), @@ -133,21 +137,23 @@ fn parse_http_response(socket: &mut dyn Read) -> Result, Error> { } } -pub fn simple_api_command( +pub fn simple_api_command_with_fds( socket: &mut T, method: &str, c: &str, request_body: Option<&str>, + request_fds: Vec, ) -> Result<(), Error> { socket - .write_all( - format!( + .send_with_fds( + &[format!( "{} /api/v1/vm.{} HTTP/1.1\r\nHost: localhost\r\nAccept: */*\r\n", method, c ) - .as_bytes(), + .as_bytes()], + &request_fds, ) - .map_err(Error::Socket)?; + .map_err(Error::SocketSendFds)?; if let Some(request_body) = request_body { socket @@ -170,3 +176,12 @@ pub fn simple_api_command( } Ok(()) } + +pub fn simple_api_command( + socket: &mut T, + method: &str, + c: &str, + request_body: Option<&str>, +) -> Result<(), Error> { + simple_api_command_with_fds(socket, method, c, request_body, Vec::new()) +}