mirror of
https://chromium.googlesource.com/crosvm/crosvm
synced 2025-02-06 02:25:23 +00:00
crosvm: vm_control: make handle_request generic over AsRef<Path>
All the Path::new(string var) are very repeptitive and completely unnecessary Change-Id: I05e86bcf9ad35519cd74ed8de78c4ddabd88abe0 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3680296 Reviewed-by: Daniel Verkamp <dverkamp@chromium.org> Tested-by: kokoro <noreply+kokoro@google.com> Commit-Queue: Anton Romanov <romanton@google.com>
This commit is contained in:
parent
d19b575fd1
commit
9ac4427d7b
2 changed files with 39 additions and 38 deletions
50
src/main.rs
50
src/main.rs
|
@ -2717,48 +2717,40 @@ filter=(default|override) - if the msr is filtered in KVM, whether to override o
|
|||
}
|
||||
|
||||
fn stop_vms(cmd: crosvm::StopCommand) -> std::result::Result<(), ()> {
|
||||
let socket_path = Path::new(&cmd.socket_path);
|
||||
vms_request(&VmRequest::Exit, socket_path)
|
||||
vms_request(&VmRequest::Exit, cmd.socket_path)
|
||||
}
|
||||
|
||||
fn suspend_vms(cmd: crosvm::SuspendCommand) -> std::result::Result<(), ()> {
|
||||
let socket_path = Path::new(&cmd.socket_path);
|
||||
vms_request(&VmRequest::Suspend, socket_path)
|
||||
vms_request(&VmRequest::Suspend, cmd.socket_path)
|
||||
}
|
||||
|
||||
fn resume_vms(cmd: crosvm::ResumeCommand) -> std::result::Result<(), ()> {
|
||||
let socket_path = Path::new(&cmd.socket_path);
|
||||
vms_request(&VmRequest::Resume, socket_path)
|
||||
vms_request(&VmRequest::Resume, cmd.socket_path)
|
||||
}
|
||||
|
||||
fn powerbtn_vms(cmd: crosvm::PowerbtnCommand) -> std::result::Result<(), ()> {
|
||||
let socket_path = Path::new(&cmd.socket_path);
|
||||
vms_request(&VmRequest::Powerbtn, socket_path)
|
||||
vms_request(&VmRequest::Powerbtn, cmd.socket_path)
|
||||
}
|
||||
|
||||
fn sleepbtn_vms(cmd: crosvm::SleepCommand) -> std::result::Result<(), ()> {
|
||||
let socket_path = Path::new(&cmd.socket_path);
|
||||
vms_request(&VmRequest::Sleepbtn, socket_path)
|
||||
vms_request(&VmRequest::Sleepbtn, cmd.socket_path)
|
||||
}
|
||||
|
||||
fn inject_gpe(cmd: crosvm::GpeCommand) -> std::result::Result<(), ()> {
|
||||
let socket_path = Path::new(&cmd.socket_path);
|
||||
vms_request(&VmRequest::Gpe(cmd.gpe), socket_path)
|
||||
vms_request(&VmRequest::Gpe(cmd.gpe), cmd.socket_path)
|
||||
}
|
||||
|
||||
fn balloon_vms(cmd: crosvm::BalloonCommand) -> std::result::Result<(), ()> {
|
||||
let command = BalloonControlCommand::Adjust {
|
||||
num_bytes: cmd.num_bytes,
|
||||
};
|
||||
let socket_path = Path::new(&cmd.socket_path);
|
||||
vms_request(&VmRequest::BalloonCommand(command), socket_path)
|
||||
vms_request(&VmRequest::BalloonCommand(command), cmd.socket_path)
|
||||
}
|
||||
|
||||
fn balloon_stats(cmd: crosvm::BalloonStatsCommand) -> std::result::Result<(), ()> {
|
||||
let command = BalloonControlCommand::Stats {};
|
||||
let request = &VmRequest::BalloonCommand(command);
|
||||
let socket_path = Path::new(&cmd.socket_path);
|
||||
let response = handle_request(request, socket_path)?;
|
||||
let response = handle_request(request, cmd.socket_path)?;
|
||||
match serde_json::to_string_pretty(&response) {
|
||||
Ok(response_json) => println!("{}", response_json),
|
||||
Err(e) => {
|
||||
|
@ -2773,9 +2765,12 @@ fn balloon_stats(cmd: crosvm::BalloonStatsCommand) -> std::result::Result<(), ()
|
|||
}
|
||||
|
||||
fn modify_battery(cmd: crosvm::BatteryCommand) -> std::result::Result<(), ()> {
|
||||
let socket_path = Path::new(&cmd.socket_path);
|
||||
|
||||
do_modify_battery(socket_path, &cmd.battery_type, &cmd.property, &cmd.target)
|
||||
do_modify_battery(
|
||||
cmd.socket_path,
|
||||
&cmd.battery_type,
|
||||
&cmd.property,
|
||||
&cmd.target,
|
||||
)
|
||||
}
|
||||
|
||||
fn modify_vfio(cmd: crosvm::VfioCrosvmCommand) -> std::result::Result<(), ()> {
|
||||
|
@ -2801,7 +2796,7 @@ fn modify_vfio(cmd: crosvm::VfioCrosvmCommand) -> std::result::Result<(), ()> {
|
|||
error!("Invalid host sysfs path: {:?}", vfio_path);
|
||||
return Err(());
|
||||
}
|
||||
handle_request(&request, Path::new(&socket_path))?;
|
||||
handle_request(&request, socket_path)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
@ -2968,33 +2963,28 @@ fn disk_cmd(cmd: crosvm::DiskCommand) -> std::result::Result<(), ()> {
|
|||
new_size: cmd.disk_size,
|
||||
},
|
||||
};
|
||||
let socket_path = Path::new(&cmd.socket_path);
|
||||
vms_request(&request, socket_path)
|
||||
vms_request(&request, cmd.socket_path)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn make_rt(cmd: crosvm::MakeRTCommand) -> std::result::Result<(), ()> {
|
||||
let socket_path = Path::new(&cmd.socket_path);
|
||||
vms_request(&VmRequest::MakeRT, socket_path)
|
||||
vms_request(&VmRequest::MakeRT, cmd.socket_path)
|
||||
}
|
||||
|
||||
fn usb_attach(cmd: crosvm::UsbAttachCommand) -> ModifyUsbResult<UsbControlResult> {
|
||||
let (bus, addr, vid, pid) = cmd.addr;
|
||||
let socket_path = Path::new(&cmd.socket_path);
|
||||
let dev_path = Path::new(&cmd.dev_path);
|
||||
|
||||
do_usb_attach(socket_path, bus, addr, vid, pid, dev_path)
|
||||
do_usb_attach(cmd.socket_path, bus, addr, vid, pid, dev_path)
|
||||
}
|
||||
|
||||
fn usb_detach(cmd: crosvm::UsbDetachCommand) -> ModifyUsbResult<UsbControlResult> {
|
||||
let socket_path = Path::new(&cmd.socket_path);
|
||||
do_usb_detach(socket_path, cmd.port)
|
||||
do_usb_detach(cmd.socket_path, cmd.port)
|
||||
}
|
||||
|
||||
fn usb_list(cmd: crosvm::UsbListCommand) -> ModifyUsbResult<UsbControlResult> {
|
||||
let socket_path = Path::new(&cmd.socket_path);
|
||||
do_usb_list(socket_path)
|
||||
do_usb_list(cmd.socket_path)
|
||||
}
|
||||
|
||||
fn modify_usb(cmd: crosvm::UsbCommand) -> std::result::Result<(), ()> {
|
||||
|
|
|
@ -65,14 +65,17 @@ fn raw_descriptor_from_path(path: &Path) -> ModifyUsbResult<RawDescriptor> {
|
|||
|
||||
pub type VmsRequestResult = std::result::Result<(), ()>;
|
||||
|
||||
pub fn vms_request(request: &VmRequest, socket_path: &Path) -> VmsRequestResult {
|
||||
pub fn vms_request<T: AsRef<Path> + std::fmt::Debug>(
|
||||
request: &VmRequest,
|
||||
socket_path: T,
|
||||
) -> VmsRequestResult {
|
||||
let response = handle_request(request, socket_path)?;
|
||||
info!("request response was {}", response);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn do_usb_attach(
|
||||
socket_path: &Path,
|
||||
pub fn do_usb_attach<T: AsRef<Path> + std::fmt::Debug>(
|
||||
socket_path: T,
|
||||
bus: u8,
|
||||
addr: u8,
|
||||
vid: u16,
|
||||
|
@ -106,7 +109,10 @@ pub fn do_usb_attach(
|
|||
}
|
||||
}
|
||||
|
||||
pub fn do_usb_detach(socket_path: &Path, port: u8) -> ModifyUsbResult<UsbControlResult> {
|
||||
pub fn do_usb_detach<T: AsRef<Path> + std::fmt::Debug>(
|
||||
socket_path: T,
|
||||
port: u8,
|
||||
) -> ModifyUsbResult<UsbControlResult> {
|
||||
let request = VmRequest::UsbCommand(UsbControlCommand::DetachDevice { port });
|
||||
let response =
|
||||
handle_request(&request, socket_path).map_err(|_| ModifyUsbError::SocketFailed)?;
|
||||
|
@ -116,7 +122,9 @@ pub fn do_usb_detach(socket_path: &Path, port: u8) -> ModifyUsbResult<UsbControl
|
|||
}
|
||||
}
|
||||
|
||||
pub fn do_usb_list(socket_path: &Path) -> ModifyUsbResult<UsbControlResult> {
|
||||
pub fn do_usb_list<T: AsRef<Path> + std::fmt::Debug>(
|
||||
socket_path: T,
|
||||
) -> ModifyUsbResult<UsbControlResult> {
|
||||
let mut ports: [u8; USB_CONTROL_MAX_PORTS] = Default::default();
|
||||
for (index, port) in ports.iter_mut().enumerate() {
|
||||
*port = index as u8
|
||||
|
@ -132,8 +140,8 @@ pub fn do_usb_list(socket_path: &Path) -> ModifyUsbResult<UsbControlResult> {
|
|||
|
||||
pub type DoModifyBatteryResult = std::result::Result<(), ()>;
|
||||
|
||||
pub fn do_modify_battery(
|
||||
socket_path: &Path,
|
||||
pub fn do_modify_battery<T: AsRef<Path> + std::fmt::Debug>(
|
||||
socket_path: T,
|
||||
battery_type: &str,
|
||||
property: &str,
|
||||
target: &str,
|
||||
|
@ -163,7 +171,10 @@ pub fn do_modify_battery(
|
|||
|
||||
pub type HandleRequestResult = std::result::Result<VmResponse, ()>;
|
||||
|
||||
pub fn handle_request(request: &VmRequest, socket_path: &Path) -> HandleRequestResult {
|
||||
pub fn handle_request<T: AsRef<Path> + std::fmt::Debug>(
|
||||
request: &VmRequest,
|
||||
socket_path: T,
|
||||
) -> HandleRequestResult {
|
||||
match UnixSeqpacket::connect(&socket_path) {
|
||||
Ok(s) => {
|
||||
let socket = Tube::new(s);
|
||||
|
|
Loading…
Reference in a new issue