Path: blob/main/devices/src/virtio/vhost_user_backend/handler/sys/linux.rs
5394 views
// Copyright 2022 The ChromiumOS Authors1// Use of this source code is governed by a BSD-style license that can be2// found in the LICENSE file.34use anyhow::Context;5use anyhow::Result;6use base::info;7use base::AsRawDescriptor;8use base::SafeDescriptor;9use cros_async::AsyncWrapper;10use cros_async::Executor;11use vmm_vhost::BackendServer;12use vmm_vhost::Error as VhostError;1314/// Performs the run loop for an already-constructor request handler.15pub async fn run_handler<S>(mut backend_server: BackendServer<S>, ex: &Executor) -> Result<()>16where17S: vmm_vhost::Backend,18{19let h = SafeDescriptor::try_from(&backend_server as &dyn AsRawDescriptor)20.map(AsyncWrapper::new)21.context("failed to get safe descriptor for handler")?;22let handler_source = ex23.async_from(h)24.context("failed to create an async source")?;2526loop {27handler_source28.wait_readable()29.await30.context("failed to wait for the handler to become readable")?;31let (hdr, files) = match backend_server.recv_header() {32Ok((hdr, files)) => (hdr, files),33Err(VhostError::ClientExit) => {34info!("vhost-user connection closed");35// Exit as the client closed the connection.36std::process::exit(0);37}38Err(e) => {39return Err(e.into());40}41};4243if backend_server.needs_wait_for_payload(&hdr) {44handler_source45.wait_readable()46.await47.context("failed to wait for the handler to become readable")?;48}49backend_server.process_message(hdr, files)?;50}51}525354