vhost-device-sound: add --socket-fd argument

This allows a service manager to start vhost-device-sound with an
already listening socket.  With this, the service manager can create
the socket in advance of starting any services, so there's no race in
between vhost-device-sound being started and being ready to accept
connections.

Signed-off-by: Alyssa Ross <hi@alyssa.is>
This commit is contained in:
Alyssa Ross 2025-09-16 19:58:19 +02:00 committed by Manos Pitsidianakis
parent f332d2241f
commit 3dca78e684
4 changed files with 31 additions and 4 deletions

View File

@ -3,6 +3,9 @@
### Added
- [[#909]](https://github.com/rust-vmm/vhost-device/pull/909)
`vhost-device-sound` now supports a `--socket-fd` argument.
### Changed
- [[#907]](https://github.com/rust-vmm/vhost-device/pull/907)

View File

@ -15,6 +15,10 @@ generated with help2man target/debug/vhost-device-sound |mandoc
--socket <SOCKET>
vhost-user Unix domain socket path
--socket-fd <FD>
listening vhost-user Unix domain socket file descriptor
(e.g. from a service manager)
--backend <BACKEND>
audio backend to be used [possible values: null, pipewire, alsa, gstreamer]

View File

@ -1,16 +1,25 @@
// SPDX-License-Identifier: Apache-2.0 or BSD-3-Clause
//! An arguments type for the binary interface of this library.
use std::os::fd::RawFd;
use std::path::PathBuf;
use clap::{Parser, ValueEnum};
use clap::{ArgGroup, Parser, ValueEnum};
#[derive(Parser, Debug)]
#[clap(version, about, long_about = None)]
#[clap(
version,
about,
long_about = None,
group(ArgGroup::new("socket group").required(true).args(&["socket", "socket_fd"])),
)]
pub struct SoundArgs {
/// vhost-user Unix domain socket path.
#[clap(long)]
pub socket: PathBuf,
pub socket: Option<PathBuf>,
/// vhost-user Unix domain socket FD.
#[clap(long)]
pub socket_fd: Option<RawFd>,
/// audio backend to be used
#[clap(long)]
#[clap(value_enum)]

View File

@ -2,6 +2,9 @@
// Stefano Garzarella <sgarzare@redhat.com>
// SPDX-License-Identifier: Apache-2.0 or BSD-3-Clause
use std::os::unix::net::UnixListener;
use std::os::unix::prelude::*;
use clap::Parser;
use vhost::vhost_user::Listener;
use vhost_device_sound::{args::SoundArgs, start_backend_server, SoundConfig};
@ -11,7 +14,15 @@ fn main() {
let args = SoundArgs::parse();
let config = SoundConfig::new(false, args.backend);
let mut listener = Listener::new(args.socket, true).unwrap();
let mut listener = if let Some(fd) = args.socket_fd {
// SAFETY: user has assured us this is safe.
unsafe { UnixListener::from_raw_fd(fd) }.into()
} else if let Some(path) = args.socket {
Listener::new(path, true).unwrap()
} else {
unreachable!()
};
loop {
start_backend_server(&mut listener, config.clone());