mirror of
https://gitlab.steamos.cloud/holo/steamos-manager.git
synced 2025-07-05 22:20:33 -04:00
udev: Add udev listener service
This service lives in the user daemon and has the path /com/steampowered/SteamOSManager1/UdevEvents
This commit is contained in:
parent
67736cef01
commit
6dc04e70d2
6 changed files with 239 additions and 6 deletions
40
Cargo.lock
generated
40
Cargo.lock
generated
|
@ -563,6 +563,17 @@ dependencies = [
|
||||||
"libc",
|
"libc",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "io-lifetimes"
|
||||||
|
version = "1.0.11"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "eae7b9aee968036d54dce06cebaefd919e4472e753296daccd6d344e3e2df0c2"
|
||||||
|
dependencies = [
|
||||||
|
"hermit-abi",
|
||||||
|
"libc",
|
||||||
|
"windows-sys 0.48.0",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "itertools"
|
name = "itertools"
|
||||||
version = "0.12.1"
|
version = "0.12.1"
|
||||||
|
@ -584,6 +595,16 @@ version = "0.2.154"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "ae743338b92ff9146ce83992f766a31066a91a8c84a45e0e9f21e7cf6de6d346"
|
checksum = "ae743338b92ff9146ce83992f766a31066a91a8c84a45e0e9f21e7cf6de6d346"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "libudev-sys"
|
||||||
|
version = "0.1.4"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3c8469b4a23b962c1396b9b451dda50ef5b283e8dd309d69033475fa9b334324"
|
||||||
|
dependencies = [
|
||||||
|
"libc",
|
||||||
|
"pkg-config",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "linux-raw-sys"
|
name = "linux-raw-sys"
|
||||||
version = "0.4.13"
|
version = "0.4.13"
|
||||||
|
@ -724,6 +745,12 @@ dependencies = [
|
||||||
"futures-io",
|
"futures-io",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "pkg-config"
|
||||||
|
version = "0.3.30"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d231b230927b5e4ad203db57bbcbee2802f6bce620b1e4a9024a07d94e2907ec"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "polling"
|
name = "polling"
|
||||||
version = "3.7.0"
|
version = "3.7.0"
|
||||||
|
@ -945,6 +972,7 @@ dependencies = [
|
||||||
"toml",
|
"toml",
|
||||||
"tracing",
|
"tracing",
|
||||||
"tracing-subscriber",
|
"tracing-subscriber",
|
||||||
|
"udev",
|
||||||
"xdg",
|
"xdg",
|
||||||
"zbus",
|
"zbus",
|
||||||
"zbus_xml",
|
"zbus_xml",
|
||||||
|
@ -1141,6 +1169,18 @@ version = "1.17.0"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825"
|
checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "udev"
|
||||||
|
version = "0.8.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "50051c6e22be28ee6f217d50014f3bc29e81c20dc66ff7ca0d5c5226e1dcc5a1"
|
||||||
|
dependencies = [
|
||||||
|
"io-lifetimes",
|
||||||
|
"libc",
|
||||||
|
"libudev-sys",
|
||||||
|
"pkg-config",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "uds_windows"
|
name = "uds_windows"
|
||||||
version = "1.1.0"
|
version = "1.1.0"
|
||||||
|
|
|
@ -18,7 +18,7 @@ config = { version = "0.14", default-features = false, features = ["async", "tom
|
||||||
inotify = { version = "0.10", default-features = false, features = ["stream"] }
|
inotify = { version = "0.10", default-features = false, features = ["stream"] }
|
||||||
libc = "0.2"
|
libc = "0.2"
|
||||||
itertools = "0.12"
|
itertools = "0.12"
|
||||||
nix = { version = "0.28", default-features = false, features = ["fs", "signal"] }
|
nix = { version = "0.28", default-features = false, features = ["fs", "poll", "signal"] }
|
||||||
serde = { version = "1.0", default-features = false, features = ["derive"] }
|
serde = { version = "1.0", default-features = false, features = ["derive"] }
|
||||||
tokio = { version = "1", default-features = false, features = ["fs", "io-std", "io-util", "macros", "process", "rt-multi-thread", "signal", "sync"] }
|
tokio = { version = "1", default-features = false, features = ["fs", "io-std", "io-util", "macros", "process", "rt-multi-thread", "signal", "sync"] }
|
||||||
tokio-stream = { version = "0.1", default-features = false }
|
tokio-stream = { version = "0.1", default-features = false }
|
||||||
|
@ -26,5 +26,6 @@ tokio-util = { version = "0.7", default-features = false }
|
||||||
toml = "0.8"
|
toml = "0.8"
|
||||||
tracing = { version = "0.1", default-features = false }
|
tracing = { version = "0.1", default-features = false }
|
||||||
tracing-subscriber = { version = "0.3", default-features = false, features = ["fmt"] }
|
tracing-subscriber = { version = "0.3", default-features = false, features = ["fmt"] }
|
||||||
|
udev = "0.8"
|
||||||
xdg = "2.5"
|
xdg = "2.5"
|
||||||
zbus = { version = "4", default-features = false, features = ["tokio"] }
|
zbus = { version = "4", default-features = false, features = ["tokio"] }
|
||||||
|
|
|
@ -346,4 +346,18 @@
|
||||||
</method>
|
</method>
|
||||||
</interface>
|
</interface>
|
||||||
|
|
||||||
|
<!--
|
||||||
|
com.steampowered.SteamOSManager1.UdevEvents
|
||||||
|
@short_description: Specific udev events that are monitored to be
|
||||||
|
bubbled up to the desktop interface
|
||||||
|
|
||||||
|
Version available: 9
|
||||||
|
-->
|
||||||
|
<interface name="com.steampowered.SteamOSManager1.UdevEvents">
|
||||||
|
<signal name="OverCurrent">
|
||||||
|
<arg type="s" name="devpath"/>
|
||||||
|
<arg type="s" name="port"/>
|
||||||
|
<arg type="t" name="count"/>
|
||||||
|
</signal>
|
||||||
|
</interface>
|
||||||
</node>
|
</node>
|
||||||
|
|
|
@ -19,6 +19,7 @@ use zbus::ConnectionBuilder;
|
||||||
use crate::daemon::{channel, Daemon, DaemonContext};
|
use crate::daemon::{channel, Daemon, DaemonContext};
|
||||||
use crate::manager::user::SteamOSManager;
|
use crate::manager::user::SteamOSManager;
|
||||||
use crate::path;
|
use crate::path;
|
||||||
|
use crate::udev::UdevMonitor;
|
||||||
|
|
||||||
#[derive(Copy, Clone, Default, Deserialize, Serialize, Debug)]
|
#[derive(Copy, Clone, Default, Deserialize, Serialize, Debug)]
|
||||||
struct UserConfig {
|
struct UserConfig {
|
||||||
|
@ -36,7 +37,9 @@ struct UserState {
|
||||||
#[derive(Copy, Clone, Default, Deserialize, Serialize, Debug)]
|
#[derive(Copy, Clone, Default, Deserialize, Serialize, Debug)]
|
||||||
pub(crate) struct UserServicesState {}
|
pub(crate) struct UserServicesState {}
|
||||||
|
|
||||||
struct UserContext {}
|
struct UserContext {
|
||||||
|
session: Connection,
|
||||||
|
}
|
||||||
|
|
||||||
impl DaemonContext for UserContext {
|
impl DaemonContext for UserContext {
|
||||||
type State = UserState;
|
type State = UserState;
|
||||||
|
@ -66,9 +69,11 @@ impl DaemonContext for UserContext {
|
||||||
&mut self,
|
&mut self,
|
||||||
_state: UserState,
|
_state: UserState,
|
||||||
_config: UserConfig,
|
_config: UserConfig,
|
||||||
_daemon: &mut Daemon<UserContext>,
|
daemon: &mut Daemon<UserContext>,
|
||||||
) -> Result<()> {
|
) -> Result<()> {
|
||||||
// Nothing to do yet
|
let udev = UdevMonitor::init(&self.session).await?;
|
||||||
|
daemon.add_service(udev);
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -115,7 +120,7 @@ pub async fn daemon() -> Result<()> {
|
||||||
let subscriber = Registry::default().with(stdout_log);
|
let subscriber = Registry::default().with(stdout_log);
|
||||||
let (_tx, rx) = channel::<UserContext>();
|
let (_tx, rx) = channel::<UserContext>();
|
||||||
|
|
||||||
let (_session, system) = match create_connections().await {
|
let (session, system) = match create_connections().await {
|
||||||
Ok(c) => c,
|
Ok(c) => c,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
let _guard = tracing::subscriber::set_default(subscriber);
|
let _guard = tracing::subscriber::set_default(subscriber);
|
||||||
|
@ -124,7 +129,7 @@ pub async fn daemon() -> Result<()> {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
let context = UserContext {};
|
let context = UserContext { session };
|
||||||
let mut daemon = Daemon::new(subscriber, system, rx).await?;
|
let mut daemon = Daemon::new(subscriber, system, rx).await?;
|
||||||
|
|
||||||
daemon.run(context).await
|
daemon.run(context).await
|
||||||
|
|
|
@ -20,6 +20,7 @@ mod process;
|
||||||
mod sls;
|
mod sls;
|
||||||
mod systemd;
|
mod systemd;
|
||||||
mod thread;
|
mod thread;
|
||||||
|
mod udev;
|
||||||
|
|
||||||
pub mod cec;
|
pub mod cec;
|
||||||
pub mod daemon;
|
pub mod daemon;
|
||||||
|
|
172
src/udev.rs
Normal file
172
src/udev.rs
Normal file
|
@ -0,0 +1,172 @@
|
||||||
|
/*
|
||||||
|
* Copyright © 2023 Collabora Ltd.
|
||||||
|
* Copyright © 2024 Valve Software
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: MIT
|
||||||
|
*/
|
||||||
|
|
||||||
|
use anyhow::{anyhow, bail, ensure, Result};
|
||||||
|
use nix::poll::{poll, PollFd, PollFlags, PollTimeout};
|
||||||
|
use nix::unistd::pipe;
|
||||||
|
use std::os::fd::{AsFd, AsRawFd, OwnedFd};
|
||||||
|
use tokio::net::unix::pipe::Sender;
|
||||||
|
use tokio::sync::mpsc::{unbounded_channel, UnboundedSender};
|
||||||
|
use tracing::debug;
|
||||||
|
use udev::{Event, EventType, MonitorBuilder};
|
||||||
|
use zbus::{self, interface, Connection, InterfaceRef, SignalContext};
|
||||||
|
|
||||||
|
use crate::thread::spawn;
|
||||||
|
use crate::Service;
|
||||||
|
|
||||||
|
const PATH: &str = "/com/steampowered/SteamOSManager1/UdevEvents";
|
||||||
|
|
||||||
|
pub(crate) struct UdevMonitor
|
||||||
|
where
|
||||||
|
Self: 'static + Send,
|
||||||
|
{
|
||||||
|
shutdown_sender: Sender,
|
||||||
|
shutdown_receiver: Option<OwnedFd>,
|
||||||
|
udev_object: InterfaceRef<UdevDbusObject>,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct UdevDbusObject
|
||||||
|
where
|
||||||
|
Self: 'static + Send, {}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
enum UdevEvent {
|
||||||
|
OverCurrent {
|
||||||
|
devpath: String,
|
||||||
|
port: String,
|
||||||
|
count: u64,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Service for UdevMonitor {
|
||||||
|
const NAME: &'static str = "udev-monitor";
|
||||||
|
|
||||||
|
async fn run(&mut self) -> Result<()> {
|
||||||
|
let (ev_sender, mut ev_receiver) = unbounded_channel();
|
||||||
|
let shutdown_receiver = self
|
||||||
|
.shutdown_receiver
|
||||||
|
.take()
|
||||||
|
.ok_or(anyhow!("UdevMonitor cannot be run twice"))?;
|
||||||
|
let mut handle = spawn(move || run_udev(ev_sender, shutdown_receiver));
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let handle = &mut handle;
|
||||||
|
let ev = tokio::select! {
|
||||||
|
r = handle => break r,
|
||||||
|
r = ev_receiver.recv() => r.ok_or(anyhow!("udev event pipe broke"))?,
|
||||||
|
};
|
||||||
|
match ev {
|
||||||
|
UdevEvent::OverCurrent {
|
||||||
|
devpath,
|
||||||
|
port,
|
||||||
|
count,
|
||||||
|
} => {
|
||||||
|
UdevDbusObject::over_current(
|
||||||
|
self.udev_object.signal_context(),
|
||||||
|
devpath.as_str(),
|
||||||
|
port.as_str(),
|
||||||
|
count,
|
||||||
|
)
|
||||||
|
.await?
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn shutdown(&mut self) -> Result<()> {
|
||||||
|
self.shutdown_sender.try_write(&[0u8])?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl UdevMonitor {
|
||||||
|
pub async fn init(connection: &Connection) -> Result<UdevMonitor> {
|
||||||
|
let object_server = connection.object_server();
|
||||||
|
ensure!(
|
||||||
|
object_server.at(PATH, UdevDbusObject {}).await?,
|
||||||
|
"Could not register UdevEvents"
|
||||||
|
);
|
||||||
|
let udev_object: InterfaceRef<UdevDbusObject> = object_server.interface(PATH).await?;
|
||||||
|
let (shutdown_receiver, shutdown_sender) = pipe()?;
|
||||||
|
Ok(UdevMonitor {
|
||||||
|
udev_object,
|
||||||
|
shutdown_sender: Sender::from_owned_fd(shutdown_sender)?,
|
||||||
|
shutdown_receiver: Some(shutdown_receiver),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[interface(name = "com.steampowered.SteamOSManager1.UdevEvents")]
|
||||||
|
impl UdevDbusObject {
|
||||||
|
#[zbus(signal)]
|
||||||
|
async fn over_current(
|
||||||
|
signal_ctxt: &SignalContext<'_>,
|
||||||
|
devpath: &str,
|
||||||
|
port: &str,
|
||||||
|
count: u64,
|
||||||
|
) -> zbus::Result<()>;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn run_udev(tx: UnboundedSender<UdevEvent>, rx: OwnedFd) -> Result<()> {
|
||||||
|
let usb_monitor = MonitorBuilder::new()?
|
||||||
|
.match_subsystem_devtype("usb", "usb_interface")?
|
||||||
|
.listen()?;
|
||||||
|
let fd = usb_monitor.as_fd();
|
||||||
|
let mut iter = usb_monitor.iter();
|
||||||
|
let ev_poller = PollFd::new(fd, PollFlags::POLLIN);
|
||||||
|
let shutdown_poller = PollFd::new(rx.as_fd(), PollFlags::POLLIN);
|
||||||
|
debug!(
|
||||||
|
"Listening on event poller {} and shutdown poller {}",
|
||||||
|
ev_poller.as_fd().as_raw_fd(),
|
||||||
|
shutdown_poller.as_fd().as_raw_fd()
|
||||||
|
);
|
||||||
|
loop {
|
||||||
|
let fds = &mut [ev_poller, shutdown_poller];
|
||||||
|
let ret = poll(fds, PollTimeout::NONE)?;
|
||||||
|
if ret < 0 {
|
||||||
|
return Err(std::io::Error::from_raw_os_error(-ret).into());
|
||||||
|
}
|
||||||
|
let [ev_poller, shutdown_poller] = fds;
|
||||||
|
match ev_poller.any() {
|
||||||
|
None => bail!("Event poller encountered unknown flags"),
|
||||||
|
Some(true) => {
|
||||||
|
let ev = iter
|
||||||
|
.next()
|
||||||
|
.ok_or(anyhow!("Poller said event was present, but it was not"))?;
|
||||||
|
process_usb_event(ev, &tx)?;
|
||||||
|
}
|
||||||
|
Some(false) => (),
|
||||||
|
}
|
||||||
|
match shutdown_poller.any() {
|
||||||
|
None => bail!("Shutdown poller encountered unknown flags"),
|
||||||
|
Some(true) => break Ok(()),
|
||||||
|
Some(false) => (),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn process_usb_event(ev: Event, tx: &UnboundedSender<UdevEvent>) -> Result<()> {
|
||||||
|
debug!("Got USB event {ev:?}");
|
||||||
|
if ev.event_type() != EventType::Change {
|
||||||
|
return Ok(());
|
||||||
|
}
|
||||||
|
let port = match ev.property_value("OVER_CURRENT_PORT") {
|
||||||
|
None => return Ok(()),
|
||||||
|
Some(port) => port.to_string_lossy().to_string(),
|
||||||
|
};
|
||||||
|
let count: u64 = match ev.property_value("OVER_CURRENT_COUNT") {
|
||||||
|
None => return Ok(()),
|
||||||
|
Some(count) => count.to_string_lossy().parse()?,
|
||||||
|
};
|
||||||
|
let devpath = ev.devpath().to_string_lossy().to_string();
|
||||||
|
tx.send(UdevEvent::OverCurrent {
|
||||||
|
devpath,
|
||||||
|
port,
|
||||||
|
count,
|
||||||
|
})?;
|
||||||
|
Ok(())
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue