diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 2faf30f..0146599 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -45,7 +45,7 @@ jobs: run: | sudo apt-get update -qq sudo apt-get install -y --no-install-recommends \ - libdbus-1-dev libpam0g-dev libbluetooth-dev \ + dbus-daemon libdbus-1-dev libpam0g-dev libbluetooth-dev \ pkg-config - name: Install Rust (stable) @@ -75,7 +75,7 @@ jobs: run: | sudo apt-get update -qq sudo apt-get install -y --no-install-recommends \ - libdbus-1-dev libpam0g-dev libbluetooth-dev \ + dbus-daemon libdbus-1-dev libpam0g-dev libbluetooth-dev \ pkg-config - name: Install Rust (stable) @@ -106,7 +106,7 @@ jobs: run: | sudo apt-get update -qq sudo apt-get install -y --no-install-recommends \ - libdbus-1-dev libpam0g-dev libbluetooth-dev \ + dbus-daemon libdbus-1-dev libpam0g-dev libbluetooth-dev \ pkg-config - name: Install Rust (stable) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 9718015..634fd96 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -53,7 +53,7 @@ jobs: - name: Install the Debian 12 build toolchain run: | apt-get update -qq - apt-get install -y --no-install-recommends ca-certificates curl git build-essential pkg-config libdbus-1-dev libpam0g-dev libbluetooth-dev libssl-dev python3 + apt-get install -y --no-install-recommends ca-certificates curl git build-essential pkg-config dbus-daemon libdbus-1-dev libpam0g-dev libbluetooth-dev libssl-dev python3 - uses: actions/checkout@v4 - uses: dtolnay/rust-toolchain@stable - uses: Swatinem/rust-cache@v2 diff --git a/Cargo.lock b/Cargo.lock index adf5b9c..c7bd1bb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2291,6 +2291,7 @@ dependencies = [ "tracing", "tracing-subscriber", "uuid", + "zbus", ] [[package]] diff --git a/crates/syauth-cli/src/status.rs b/crates/syauth-cli/src/status.rs index 7a3beec..dc85577 100644 --- a/crates/syauth-cli/src/status.rs +++ b/crates/syauth-cli/src/status.rs @@ -167,6 +167,8 @@ pub enum DaemonProbeState { /// timeout; the daemon's per-peer rows + boot wall-clock time /// are carried in. Up { + /// Live controller and advertisement readiness, if supported by the daemon. + transport_ready: Option, /// Boot wall-clock time the daemon reported. Serialized as /// epoch seconds on the JSON path for non-Rust consumers. #[serde(with = "epoch_seconds")] @@ -549,7 +551,15 @@ fn probe_daemon(socket: &Path) -> DaemonProbeState { }; } match read_frame_blocking::<_, Response>(&mut stream) { - Ok(Response::Status { peers, started_at }) => DaemonProbeState::Up { started_at, peers }, + Ok(Response::Status { + peers, + started_at, + transport_ready, + }) => DaemonProbeState::Up { + started_at, + peers, + transport_ready, + }, Ok(_) => DaemonProbeState::Down { reason: "unexpected-response".to_owned(), }, @@ -576,7 +586,7 @@ fn connect_error_reason(err: &io::Error) -> String { /// so a daemon-down case is never a silent empty section. pub fn write_daemon_section(writer: &mut dyn Write, report: &CliStatusReport) -> Result<(), StatusError> { match &report.daemon { - DaemonProbeState::Up { started_at, peers } => { + DaemonProbeState::Up { started_at, peers, .. } => { let ts = format_rfc3339(*started_at); writeln!(writer, "daemon=up started_at={ts}")?; writeln!( diff --git a/crates/syauth-cli/tests/doctor_flow.rs b/crates/syauth-cli/tests/doctor_flow.rs index 62ca0fd..ddbea55 100644 --- a/crates/syauth-cli/tests/doctor_flow.rs +++ b/crates/syauth-cli/tests/doctor_flow.rs @@ -72,6 +72,7 @@ impl FakeDaemon { if let Ok(req) = read_frame_blocking::<_, Request>(&mut stream) { if matches!(req, Request::Status) { let resp = Response::Status { + transport_ready: Some(true), peers: vec![], started_at: SystemTime::now(), }; diff --git a/crates/syauth-cli/tests/status_flow.rs b/crates/syauth-cli/tests/status_flow.rs index 96f6dab..2924520 100644 --- a/crates/syauth-cli/tests/status_flow.rs +++ b/crates/syauth-cli/tests/status_flow.rs @@ -63,6 +63,10 @@ struct FakeStatusDaemon { impl FakeStatusDaemon { fn new(socket: PathBuf, peers: Vec, started_at: SystemTime) -> Self { + Self::with_transport_health(socket, peers, started_at, true) + } + + fn with_transport_health(socket: PathBuf, peers: Vec, started_at: SystemTime, ready: bool) -> Self { let listener = UnixListener::bind(&socket).expect("bind fake daemon listener"); listener.set_nonblocking(false).expect("blocking listener"); let (tx, _rx) = mpsc::channel::<()>(); @@ -71,7 +75,11 @@ impl FakeStatusDaemon { let _ = stream.set_read_timeout(Some(Duration::from_secs(2))); let _ = stream.set_write_timeout(Some(Duration::from_secs(2))); if let Ok(Request::Status) = read_frame_blocking::<_, Request>(&mut stream) { - let resp = Response::Status { peers, started_at }; + let resp = Response::Status { + peers, + started_at, + transport_ready: Some(ready), + }; let _ = write_frame_blocking(&mut stream, &resp); } let mut buf = [0u8; 1]; @@ -188,3 +196,20 @@ fn json_mode_emits_typed_object() { let state = daemon.get("state").and_then(|v| v.as_str()).expect("state token"); assert!(matches!(state, "up" | "down"), "unexpected state token {state}"); } + +#[test] +fn json_reports_transport_failure_independently_of_daemon_liveness() { + let td = TempDir::new().expect("tempdir"); + let socket = td.path().join("auth.sock"); + let _daemon = FakeStatusDaemon::with_transport_health(socket.clone(), vec![peer_row("phone")], SystemTime::now(), false); + let out = syauth() + .args(["status", "--json", "--socket"]) + .arg(&socket) + .args(["--adapter", "absent-adapter", "--bond-dir"]) + .arg(td.path()) + .assert() + .success(); + let value: serde_json::Value = serde_json::from_slice(&out.get_output().stdout).expect("JSON"); + assert_eq!(value["daemon"]["state"], "up"); + assert_eq!(value["daemon"]["transport_ready"], false); +} diff --git a/crates/syauth-presenced/Cargo.toml b/crates/syauth-presenced/Cargo.toml index 96858e6..eda0ec8 100644 --- a/crates/syauth-presenced/Cargo.toml +++ b/crates/syauth-presenced/Cargo.toml @@ -127,3 +127,5 @@ time = { version = "0.3", features = ["macros"] } # transport's `Peripheral` trait talks `uuid::Uuid` and the rotation # audit short-hex render uses the same crate. uuid = { version = "1" } +# Exercise the real BlueZ D-Bus boundary on a private bus, without a radio. +zbus = { version = "5", default-features = false, features = ["tokio"] } diff --git a/crates/syauth-presenced/dist/syauth-presenced.service b/crates/syauth-presenced/dist/syauth-presenced.service index d0fa854..4842f64 100644 --- a/crates/syauth-presenced/dist/syauth-presenced.service +++ b/crates/syauth-presenced/dist/syauth-presenced.service @@ -17,6 +17,8 @@ Documentation=https://github.com/dmytrogajewski/syauth/blob/master/specs/unlock- # locks ${XDG_RUNTIME_DIR}/syauth/presenced.pid (S-001), so the # XDG_RUNTIME_DIR must exist before the unit starts. ConditionPathIsDirectory=%t +# Keep retrying while a controller is absent, including suspend and USB resets. +StartLimitIntervalSec=0 [Service] Type=simple @@ -28,10 +30,7 @@ ExecStart=/usr/local/libexec/syauth-presenced # drops the pidfile lock + unlinks the pidfile + exits within ~1s. KillSignal=SIGTERM TimeoutStopSec=5 -# Restart on crash so a transient BlueZ adapter blip (S-003+) does -# not require operator intervention. S-001 has no crash conditions -# of its own; this setting is forward-looking for the BLE event loop -# that arrives in S-003+. +# Rebuild the GATT application and rotating advertisement after transport loss. Restart=on-failure RestartSec=2 # Sandboxing aligned with SPEC §7 Security ("only the daemon's own diff --git a/crates/syauth-presenced/src/orchestrator.rs b/crates/syauth-presenced/src/orchestrator.rs index f9a7cff..72a9701 100644 --- a/crates/syauth-presenced/src/orchestrator.rs +++ b/crates/syauth-presenced/src/orchestrator.rs @@ -24,7 +24,10 @@ use std::{ collections::{BTreeMap, HashMap, HashSet, VecDeque}, path::{Path, PathBuf}, - sync::Arc, + sync::{ + Arc, + atomic::{AtomicBool, Ordering}, + }, time::{Duration as StdDuration, SystemTime, UNIX_EPOCH}, }; @@ -393,6 +396,7 @@ pub struct ReloadCommand { /// minimal `add_peer` / `remove_peer` calls before re-publishing the /// fresh UUID union. pub struct Orchestrator { + transport_ready: AtomicBool, peripheral: Arc, /// In-memory mirror of the live peer set keyed by `peer_id`. /// `BTreeMap` so iteration order is deterministic across runs @@ -551,6 +555,7 @@ impl Orchestrator { let (reload_tx, reload_rx) = mpsc::channel(RELOAD_CHANNEL_CAPACITY); Self { peripheral, + transport_ready: AtomicBool::new(false), peers: tokio::sync::Mutex::new(map), bonds_file, keys_dir, @@ -561,6 +566,32 @@ impl Orchestrator { } } + /// True after successful advertising and while transport health checks pass. + #[must_use] + pub fn transport_ready(&self) -> bool { + self.transport_ready.load(Ordering::Acquire) + } + + /// Bound health probing so an unresponsive BlueZ cannot stall recovery. + pub async fn check_transport(&self) -> Result<(), PeripheralError> { + let result = match tokio::time::timeout(StdDuration::from_secs(1), self.peripheral.check_health()).await { + Ok(result) => result, + Err(_) => Err(PeripheralError::Backend { + reason: "transport health probe timed out".into(), + }), + }; + if result.is_err() { + self.transport_ready.store(false, Ordering::Release); + } + result?; + if !self.transport_ready() { + return Err(PeripheralError::Backend { + reason: "advertising unavailable".into(), + }); + } + Ok(()) + } + /// Attach an audit-log appender after construction. Returns the /// previously-installed log (if any). Useful for tests that /// construct the orchestrator first and only later open the @@ -634,7 +665,9 @@ impl Orchestrator { let minute = minute_index(now); let snapshot = self.snapshot_peers().await; let union = build_uuid_union(&snapshot, minute); - match self.peripheral.set_session_uuids(union.clone()).await { + let result = self.peripheral.set_session_uuids(union.clone()).await; + self.transport_ready.store(result.is_ok(), Ordering::Release); + match result { Ok(()) => { for (peer_id, _) in &snapshot { let uuid_bytes = session_uuid_for(snapshot_key(&snapshot, peer_id), minute); diff --git a/crates/syauth-presenced/src/rpc.rs b/crates/syauth-presenced/src/rpc.rs index 489636e..6ba6e96 100644 --- a/crates/syauth-presenced/src/rpc.rs +++ b/crates/syauth-presenced/src/rpc.rs @@ -95,6 +95,10 @@ pub enum Response { /// Outcome of `Request::Status`. S-002 ships the wire shape with /// an empty `peers` list; S-017 fills in the per-peer columns. Status { + /// Advertising and controller health, independent of phone presence. + /// Missing for daemons predating transport health reporting. + #[serde(default)] + transport_ready: Option, /// Per-peer liveness rows. peers: Vec, /// Daemon boot wall-clock time (UTC). Serialized as the @@ -381,9 +385,28 @@ mod tests { assert_eq!(round_trip_response(&original), original); } + #[test] + fn legacy_status_has_unknown_transport_health() { + #[derive(Serialize)] + struct LegacyStatus { + kind: &'static str, + peers: Vec, + started_at: u64, + } + let bytes = encode_frame(&LegacyStatus { + kind: "status", + peers: vec![], + started_at: 0, + }) + .expect("encode"); + let response: Response = read_frame_blocking(&mut std::io::Cursor::new(bytes)).expect("legacy decode"); + assert!(matches!(response, Response::Status { transport_ready: None, .. })); + } + #[test] fn response_status_roundtrips() { let original = Response::Status { + transport_ready: Some(true), peers: vec![PeerStatus { peer_id: "peer-a".to_string(), last_challenge_ms_ago: Some(42_000), diff --git a/crates/syauth-presenced/src/runtime.rs b/crates/syauth-presenced/src/runtime.rs index 1a5b061..a14dc30 100644 --- a/crates/syauth-presenced/src/runtime.rs +++ b/crates/syauth-presenced/src/runtime.rs @@ -79,6 +79,9 @@ pub enum RunError { /// failed (S-002 `ServeError`). #[error(transparent)] Serve(#[from] ServeError), + /// Bluetooth setup or advertising failed; systemd must rebuild transport. + #[error("Bluetooth transport unavailable: {0}")] + Transport(String), } /// Reason the daemon's main loop exited. Useful for tests that want @@ -214,14 +217,14 @@ pub async fn run(config: Config) -> Result { ); let (orch_shutdown_tx, orch_shutdown_rx) = tokio::sync::oneshot::channel::<()>(); - let (orch_task, reload_tx, orch_handle) = maybe_spawn_orchestrator(&config, orch_shutdown_rx).await; + let (orch_task, reload_tx, orch_handle) = maybe_spawn_orchestrator(&config, orch_shutdown_rx).await?; let (shutdown_tx, shutdown_rx) = tokio::sync::oneshot::channel::<()>(); let serve_config = ServeConfig { socket_path: config.socket.clone(), expected_uid: config.expected_uid, reload_tx: reload_tx.clone(), - orchestrator: orch_handle, + orchestrator: orch_handle.clone(), test_fixed_nonce: config.test_fixed_nonce, started_at: Some(std::time::SystemTime::now()), }; @@ -234,12 +237,18 @@ pub async fn run(config: Config) -> Result { let inotify_task = reload_tx.as_ref().map(|tx| spawn_inotify_watcher(&config.bonds_file, tx.clone())); - let reason = wait_for_reason(&mut sigterm, &mut sigint, &mut sighup, reload_tx.as_ref()).await; - tracing::info!(?reason, "shutdown signal received, draining"); + let reason = tokio::select! { + reason = wait_for_reason(&mut sigterm, &mut sigint, &mut sighup, reload_tx.as_ref()) => Ok(reason), + error = wait_for_transport_failure(orch_handle.as_ref()) => Err(error), + }; + tracing::info!(?reason, "draining daemon for signal or transport recovery"); let _ = shutdown_tx.send(()); let _ = orch_shutdown_tx.send(()); - if let Some(handle) = orch_task { - let _ = handle.await; + if let Some(mut handle) = orch_task { + if tokio::time::timeout(TRANSPORT_HEALTH_INTERVAL, &mut handle).await.is_err() { + handle.abort(); + let _ = handle.await; + } } if let Some(handle) = inotify_task { handle.abort(); @@ -259,8 +268,23 @@ pub async fn run(config: Config) -> Result { // Drop runs here (function returns); the kernel releases the // F_OFD_SETLK lock and the pidfile is unlinked on the way out. drop(lock); - tracing::info!("syauth-presenced stopped cleanly"); - Ok(reason) + tracing::info!("syauth-presenced transport and socket drained"); + reason +} + +/// Cadence for detecting a lost controller independently of minute rotation. +const TRANSPORT_HEALTH_INTERVAL: Duration = Duration::from_secs(2); + +async fn wait_for_transport_failure(orchestrator: Option<&Arc>) -> RunError { + loop { + tokio::time::sleep(TRANSPORT_HEALTH_INTERVAL).await; + let Some(orchestrator) = orchestrator else { + return RunError::Transport("no transport running".into()); + }; + if let Err(err) = orchestrator.check_transport().await { + return RunError::Transport(err.to_string()); + } + } } async fn wait_for_reason( @@ -287,11 +311,10 @@ async fn wait_for_reason( } } -/// Try to construct an [`Orchestrator`] over a [`PersistentPeripheral`] -/// and spawn it. Returns `(None, None)` (and logs a `warn`) if no -/// non-revoked bond exists, the key file is missing or malformed, or -/// the BlueZ adapter cannot be opened — in any of those cases the -/// daemon stays up with only the S-002 Unix-socket loop. +/// Construct and spawn an orchestrator, including pairing with zero bonds. +/// Bluetooth initialization failures propagate so systemd retries on the +/// available controller. Store failures return an absent orchestrator, which +/// the runtime health monitor also rejects. /// /// On success the orchestrator is constructed over the cold-start /// peer (a single bond for now; multi-peer hydration on cold start @@ -308,16 +331,19 @@ async fn wait_for_reason( async fn maybe_spawn_orchestrator( config: &Config, shutdown: tokio::sync::oneshot::Receiver<()>, -) -> ( - Option>, - Option>, - Option>, -) { +) -> Result< + ( + Option>, + Option>, + Option>, + ), + RunError, +> { let store = match BondStore::load(&config.bonds_file) { Ok(s) => s, Err(err) => { warn_no_orchestrator(&format!("bonds.toml load failed: {err}")); - return (None, None, None); + return Ok((None, None, None)); } }; let first_active = store.list().iter().find(|b| matches!(b.status, BondStatus::Bonded)).cloned(); @@ -332,8 +358,7 @@ async fn maybe_spawn_orchestrator( PeripheralMode::Real => match PersistentPeripheral::new_default().await { Ok((p, rx)) => (p, Some(rx)), Err(err) => { - warn_no_orchestrator(&format!("BlueZ adapter open failed: {err}")); - return (None, None, None); + return Err(RunError::Transport(err.to_string())); } }, PeripheralMode::Fake => (seed_fake_peripheral(config), None), @@ -349,7 +374,7 @@ async fn maybe_spawn_orchestrator( Ok(k) => { if let Err(err) = peripheral.add_peer(&b.peer_id, &k).await { warn_no_orchestrator(&format!("peripheral add_peer failed: {err}")); - return (None, None, None); + return Ok((None, None, None)); } seed_peers.push((b, k)); } @@ -396,7 +421,7 @@ async fn maybe_spawn_orchestrator( } let orchestrator_handle = Arc::clone(&orchestrator); let handle = tokio::spawn(Arc::clone(&orchestrator).run(shutdown)); - (Some(handle), Some(reload_tx), Some(orchestrator_handle)) + Ok((Some(handle), Some(reload_tx), Some(orchestrator_handle))) } /// Spawn a tokio task that hosts a `notify::recommended_watcher` @@ -615,3 +640,20 @@ fn persist_pair_bond_record(bonds_path: &Path, bond: Bond) -> Result<(), syauth_ store.save(bonds_path)?; Ok(()) } + +#[cfg(test)] +mod recovery_tests { + use super::*; + + #[test] + fn controller_absence_does_not_exhaust_systemd_restart_limit() { + let unit = include_str!("../dist/syauth-presenced.service"); + assert!(unit.contains("StartLimitIntervalSec=0")); + assert!(unit.contains("Restart=on-failure")); + } + + #[tokio::test(start_paused = true)] + async fn absent_transport_requests_supervised_restart() { + assert!(matches!(wait_for_transport_failure(None).await, RunError::Transport(_))); + } +} diff --git a/crates/syauth-presenced/src/server.rs b/crates/syauth-presenced/src/server.rs index 0295e6a..b744a84 100644 --- a/crates/syauth-presenced/src/server.rs +++ b/crates/syauth-presenced/src/server.rs @@ -373,7 +373,12 @@ async fn dispatch( Some(o) => o.peers_snapshot().await, None => Vec::new(), }; - Response::Status { peers, started_at } + let transport_ready = Some(orchestrator.is_some_and(|o| o.transport_ready())); + Response::Status { + peers, + started_at, + transport_ready, + } } } } diff --git a/crates/syauth-presenced/tests/lifecycle_smoke.rs b/crates/syauth-presenced/tests/lifecycle_smoke.rs index 6fc47e7..afb8ec3 100644 --- a/crates/syauth-presenced/tests/lifecycle_smoke.rs +++ b/crates/syauth-presenced/tests/lifecycle_smoke.rs @@ -36,6 +36,27 @@ const REFUSAL_BUDGET: Duration = Duration::from_secs(5); /// the smoke tests responsive without spinning. const POLL_INTERVAL: Duration = Duration::from_millis(25); +#[test] +fn unavailable_bluez_exits_for_supervised_recovery() { + let env = TestEnv::new(); + let mut child = Command::new(daemon_bin_path()) + .args(["--peripheral", "real", "--pidfile"]) + .arg(env.pidfile_path()) + .arg("--bonds-file") + .arg(&env.bonds_file) + .env("DBUS_SYSTEM_BUS_ADDRESS", "unix:path=/nonexistent/syauth-test-bus") + .stdout(Stdio::null()) + .stderr(Stdio::null()) + .spawn() + .expect("spawn"); + let result = wait_for_exit(&mut child, SHUTDOWN_BUDGET); + if result.is_err() { + child.kill().expect("kill failed daemon"); + child.wait().expect("reap"); + } + assert!(!result.expect("transport failure must exit for restart").success()); +} + #[test] fn starts_and_terminates_cleanly() { let env = TestEnv::new(); diff --git a/crates/syauth-presenced/tests/rotation.rs b/crates/syauth-presenced/tests/rotation.rs index 14c2c43..77a817d 100644 --- a/crates/syauth-presenced/tests/rotation.rs +++ b/crates/syauth-presenced/tests/rotation.rs @@ -286,3 +286,64 @@ impl Visit for MessageVisitor { } } } + +#[tokio::test] +async fn transport_is_not_ready_before_advertising() { + let orchestrator = Orchestrator::new(FakePeripheral::new(), fixture_bond(), BOND_KEY, tokio::time::Instant::now()); + assert!(!orchestrator.transport_ready()); +} + +#[tokio::test] +async fn advertising_makes_transport_ready_without_a_phone() { + let orchestrator = Arc::new(Orchestrator::new( + FakePeripheral::new(), + fixture_bond(), + BOND_KEY, + tokio::time::Instant::now(), + )); + let (tx, rx) = oneshot::channel(); + let handle = tokio::spawn(orchestrator.clone().run(rx)); + tokio::task::yield_now().await; + let ready = orchestrator.transport_ready(); + tx.send(()).expect("shutdown"); + handle.await.expect("join"); + assert!(ready); +} + +#[tokio::test(start_paused = true)] +async fn failed_advertisement_clears_readiness() { + let fake = FakePeripheral::new(); + let start = tokio::time::Instant::now() + Duration::from_secs(SECONDS_PER_MINUTE); + let orchestrator = Arc::new(Orchestrator::new(fake.clone(), fixture_bond(), BOND_KEY, start)); + let (tx, rx) = oneshot::channel(); + let handle = tokio::spawn(orchestrator.clone().run(rx)); + tokio::task::yield_now().await; + assert!(orchestrator.transport_ready()); + fake.set_transport_failed(true); + tokio::time::advance(Duration::from_secs(SECONDS_PER_MINUTE)).await; + tokio::task::yield_now().await; + let ready = orchestrator.transport_ready(); + tx.send(()).expect("shutdown"); + handle.await.expect("join"); + assert!(!ready); +} + +#[tokio::test] +async fn controller_loss_is_detected_before_next_rotation() { + let fake = FakePeripheral::new(); + let orchestrator = Arc::new(Orchestrator::new( + fake.clone(), + fixture_bond(), + BOND_KEY, + tokio::time::Instant::now() + Duration::from_secs(SECONDS_PER_MINUTE), + )); + let (tx, rx) = oneshot::channel(); + let handle = tokio::spawn(orchestrator.clone().run(rx)); + tokio::task::yield_now().await; + fake.set_transport_failed(true); + let result = orchestrator.check_transport().await; + tx.send(()).expect("shutdown"); + handle.await.expect("join"); + assert!(result.is_err()); + assert!(!orchestrator.transport_ready()); +} diff --git a/crates/syauth-presenced/tests/transport_recovery.rs b/crates/syauth-presenced/tests/transport_recovery.rs new file mode 100644 index 0000000..3238ed2 --- /dev/null +++ b/crates/syauth-presenced/tests/transport_recovery.rs @@ -0,0 +1,243 @@ +//! Exercise controller removal through the real daemon and a private BlueZ bus. +use std::{ + collections::HashMap, + io::{BufRead, BufReader}, + process::{Child, Command, Stdio}, + sync::{ + Arc, + atomic::{AtomicU8, Ordering}, + }, + time::Duration, +}; + +use syauth_core::{Bond, BondStatus, BondStore, peer_id_from_pubkey}; +use syauth_presenced::{Request, Response, read_frame, write_frame}; +use zbus::zvariant::{OwnedObjectPath, OwnedValue}; + +type Options = HashMap; + +#[derive(Default)] +struct Adapter { + powered: bool, + discoverable: bool, + pairable: bool, +} +#[zbus::interface(name = "org.bluez.Adapter1")] +impl Adapter { + #[zbus(property)] + fn powered(&self) -> bool { + self.powered + } + #[zbus(property)] + fn set_powered(&mut self, value: bool) { + self.powered = value; + } + #[zbus(property)] + fn discoverable(&self) -> bool { + self.discoverable + } + #[zbus(property)] + fn set_discoverable(&mut self, value: bool) { + self.discoverable = value; + } + #[zbus(property)] + fn pairable(&self) -> bool { + self.pairable + } + #[zbus(property)] + fn set_pairable(&mut self, value: bool) { + self.pairable = value; + } +} +struct Advertising(Arc); +#[zbus::interface(name = "org.bluez.LEAdvertisingManager1")] +impl Advertising { + fn register_advertisement(&self, _path: OwnedObjectPath, _options: Options) { + self.0.store(1, Ordering::SeqCst); + } + fn unregister_advertisement(&self, _path: OwnedObjectPath) { + self.0.store(0, Ordering::SeqCst); + } + #[zbus(property)] + fn active_instances(&self) -> u8 { + self.0.load(Ordering::SeqCst) + } +} +struct Gatt; +#[zbus::interface(name = "org.bluez.GattManager1")] +impl Gatt { + fn register_application(&self, _path: OwnedObjectPath, _options: Options) {} + fn unregister_application(&self, _path: OwnedObjectPath) {} +} +struct Agent; +#[zbus::interface(name = "org.bluez.AgentManager1")] +impl Agent { + fn register_agent(&self, _path: OwnedObjectPath, _capability: String) {} + fn request_default_agent(&self, _path: OwnedObjectPath) {} + fn unregister_agent(&self, _path: OwnedObjectPath) {} +} + +struct Process(Child); +impl Drop for Process { + fn drop(&mut self) { + let _ = self.0.kill(); + let _ = self.0.wait(); + } +} + +async fn add_adapter(bus: &zbus::Connection, name: &str) -> Arc { + let path = format!("/org/bluez/{name}"); + let active = Arc::new(AtomicU8::new(0)); + bus.object_server().at(path.as_str(), Adapter::default()).await.expect("adapter"); + bus.object_server() + .at(path.as_str(), Advertising(active.clone())) + .await + .expect("advertising"); + bus.object_server().at(path.as_str(), Gatt).await.expect("GATT"); + active +} + +async fn status(socket: &std::path::Path) -> Option { + let mut stream = tokio::net::UnixStream::connect(socket).await.ok()?; + write_frame(&mut stream, &Request::Status).await.ok()?; + read_frame(&mut stream).await.ok() +} + +#[tokio::test] +async fn removed_controller_exits_and_relaunch_advertises_on_replacement() { + let root = tempfile::tempdir().expect("tempdir"); + use std::os::unix::fs::PermissionsExt; + std::fs::set_permissions(root.path(), std::fs::Permissions::from_mode(0o700)).expect("private bond directory"); + let pubkey = [0xAC; 32]; + let peer_id = peer_id_from_pubkey(&pubkey); + let mut bonds = BondStore::empty(); + bonds + .add(Bond { + peer_id: peer_id.clone(), + pubkey, + name: "phone".into(), + created_at: time::OffsetDateTime::UNIX_EPOCH, + status: BondStatus::Bonded, + }) + .expect("bond"); + let bonds_file = root.path().join("bonds.toml"); + bonds.save(&bonds_file).expect("save bond"); + let original_bonds = std::fs::read(&bonds_file).expect("original bond bytes"); + std::fs::create_dir(root.path().join("keys")).expect("keys dir"); + let key_path = root.path().join("keys").join(format!("{peer_id}.bin")); + std::fs::write(&key_path, [0xAB; 32]).expect("key"); + let mut broker = Process( + Command::new("dbus-daemon") + .args(["--session", "--nofork", "--print-address=1"]) + .stdout(Stdio::piped()) + .stderr(Stdio::null()) + .spawn() + .expect("private D-Bus"), + ); + let mut address = String::new(); + BufReader::new(broker.0.stdout.take().expect("bus address")) + .read_line(&mut address) + .expect("read address"); + let bus = zbus::connection::Builder::address(address.trim()) + .expect("bus builder") + .name("org.bluez") + .expect("name") + .serve_at("/", zbus::fdo::ObjectManager) + .expect("objects") + .serve_at("/org/bluez", Agent) + .expect("agent") + .build() + .await + .expect("bus"); + let active = add_adapter(&bus, "hci0").await; + let socket = root.path().join("auth.sock"); + let spawn = || { + Process( + Command::new(env!("CARGO_BIN_EXE_syauth-presenced")) + .arg("--socket") + .arg(&socket) + .arg("--pidfile") + .arg(root.path().join("pid")) + .arg("--bonds-file") + .arg(root.path().join("bonds.toml")) + .arg("--keys-dir") + .arg(root.path().join("keys")) + .arg("--audit-log") + .arg(root.path().join("audit.log")) + .env("DBUS_SYSTEM_BUS_ADDRESS", address.trim()) + .stdout(Stdio::null()) + .stderr(Stdio::piped()) + .spawn() + .expect("daemon"), + ) + }; + let mut daemon = spawn(); + tokio::time::timeout(Duration::from_secs(5), async { + loop { + if matches!( + status(&socket).await, + Some(Response::Status { + transport_ready: Some(true), + .. + }) + ) { + break; + } + if let Some(code) = daemon.0.try_wait().expect("wait") { + let output = daemon.0.stderr.take().expect("stderr"); + let log = std::io::read_to_string(output).expect("log"); + panic!("daemon exited {code}: {log}"); + } + tokio::time::sleep(Duration::from_millis(20)).await; + } + }) + .await + .expect("initial advertising"); + assert_eq!(active.load(Ordering::SeqCst), 1); + bus.object_server() + .remove::("/org/bluez/hci0") + .await + .expect("remove controller"); + bus.object_server() + .remove::("/org/bluez/hci0") + .await + .expect("remove advertising"); + bus.object_server().remove::("/org/bluez/hci0").await.expect("remove GATT"); + let replacement = add_adapter(&bus, "hci1").await; + let code = tokio::time::timeout(Duration::from_secs(5), async { + loop { + if let Some(code) = daemon.0.try_wait().expect("wait") { + break code; + } + tokio::time::sleep(Duration::from_millis(20)).await; + } + }) + .await + .expect("transport failure exits for systemd restart"); + assert!(!code.success()); + assert!(!socket.exists(), "failed daemon cleans its RPC socket"); + let _replacement_daemon = spawn(); + tokio::time::timeout(Duration::from_secs(5), async { + loop { + if matches!( + status(&socket).await, + Some(Response::Status { + transport_ready: Some(true), + .. + }) + ) { + break; + } + tokio::time::sleep(Duration::from_millis(20)).await; + } + }) + .await + .expect("replacement controller advertises"); + assert_eq!(replacement.load(Ordering::SeqCst), 1); + let Some(Response::Status { peers, .. }) = status(&socket).await else { + panic!("replacement status") + }; + assert_eq!(peers[0].peer_id, peer_id, "recovery hydrates the existing bond"); + assert_eq!(std::fs::read(bonds_file).expect("bond"), original_bonds); + assert_eq!(std::fs::read(key_path).expect("key"), [0xAB; 32]); +} diff --git a/crates/syauth-transport/src/peripheral.rs b/crates/syauth-transport/src/peripheral.rs index d54b77d..c27289b 100644 --- a/crates/syauth-transport/src/peripheral.rs +++ b/crates/syauth-transport/src/peripheral.rs @@ -31,6 +31,7 @@ //! refactored by S-003 — it remains byte-identical until S-009 deletes //! it. The library API is a strict superset. +mod health; mod notification; use std::{collections::HashMap, sync::Arc, time::Duration}; @@ -192,6 +193,11 @@ impl From for PeripheralError { /// method. #[async_trait] pub trait Peripheral: Send + Sync { + /// Check controller and advertising health independently of phone presence. + async fn check_health(&self) -> Result<(), PeripheralError> { + Ok(()) + } + /// Register a bonded peer's challenge + response characteristics /// with the long-lived GATT application. /// @@ -287,6 +293,7 @@ pub struct PersistentPeripheral { /// to `bluetoothd`). Held so the adapter and the application stay /// alive for the lifetime of the daemon. _session: bluer::Session, + adapter_lifetime: health::AdapterLifetime, /// Adapter the peripheral operates on (e.g. `hci0`). adapter: bluer::Adapter, /// Live GATT application registration. Replaced on every @@ -412,7 +419,12 @@ impl PersistentPeripheral { // concurrent retries without back-pressuring the BlueZ // GATT-write thread. let (pair_event_tx, pair_event_rx) = mpsc::channel::<([u8; PAIR_PUBKEY_LEN], [u8; PAIR_PUBKEY_LEN])>(8); + let events = session.events().await.map_err(|err| PeripheralError::Backend { + reason: format!("adapter lifecycle subscription: {err}"), + })?; + let adapter_lifetime = health::AdapterLifetime::watch(events, adapter_id.to_owned()); let peripheral = Arc::new(Self { + adapter_lifetime, _session: session, adapter, app_handle: Mutex::new(None), @@ -819,6 +831,28 @@ impl PersistentPeripheral { #[async_trait] impl Peripheral for PersistentPeripheral { + async fn check_health(&self) -> Result<(), PeripheralError> { + if self.adapter_lifetime.is_lost() { + return Err(PeripheralError::Backend { + reason: "registered controller was removed".into(), + }); + } + // Serialize against advertisement replacement to avoid observing its gap. + let advertisement = self.adv_slot.lock().await; + let probe = + async { Ok::<_, bluer::Error>(self.adapter.is_powered().await? && self.adapter.active_advertising_instances().await? > 0) } + .await + .map_err(|err| PeripheralError::Backend { + reason: format!("controller health: {err}"), + })?; + if advertisement.is_none() || !probe { + return Err(PeripheralError::Backend { + reason: "controller or advertisement unavailable".into(), + }); + } + Ok(()) + } + async fn add_peer(&self, peer_id: &str, bond_key: &BondKey) -> Result<(), PeripheralError> { let peers = self.peers.lock().await; if peers.contains_key(peer_id) { @@ -937,6 +971,7 @@ impl Peripheral for PersistentPeripheral { #[cfg(any(test, feature = "test-fake"))] #[derive(Default)] struct FakeState { + transport_failed: bool, /// Peers in insertion order so tests can assert on the order /// after a `remove_peer` in the middle of the sequence. peers_in_order: Vec, @@ -971,6 +1006,11 @@ pub struct FakePeripheral { #[cfg(any(test, feature = "test-fake"))] impl FakePeripheral { + /// Simulate controller loss without changing the bonded peer set. + pub fn set_transport_failed(&self, failed: bool) { + self.lock_state().transport_failed = failed; + } + /// Construct a fresh `FakePeripheral` with no peers, no advertised /// UUIDs, and no recorded calls. #[must_use] @@ -1049,6 +1089,15 @@ impl Default for FakePeripheral { #[cfg(any(test, feature = "test-fake"))] #[async_trait] impl Peripheral for FakePeripheral { + async fn check_health(&self) -> Result<(), PeripheralError> { + if self.lock_state().transport_failed { + return Err(PeripheralError::Backend { + reason: "controller removed".into(), + }); + } + Ok(()) + } + async fn add_peer(&self, peer_id: &str, bond_key: &BondKey) -> Result<(), PeripheralError> { let mut state = self.lock_state(); if state.peer_keys.contains_key(peer_id) { @@ -1074,6 +1123,11 @@ impl Peripheral for FakePeripheral { async fn set_session_uuids(&self, uuids: std::collections::HashSet) -> Result<(), PeripheralError> { let mut state = self.lock_state(); + if state.transport_failed { + return Err(PeripheralError::Backend { + reason: "controller removed".into(), + }); + } state.session_uuid_calls.push(uuids); Ok(()) } diff --git a/crates/syauth-transport/src/peripheral/health.rs b/crates/syauth-transport/src/peripheral/health.rs new file mode 100644 index 0000000..750ae84 --- /dev/null +++ b/crates/syauth-transport/src/peripheral/health.rs @@ -0,0 +1,73 @@ +//! Latch controller removal even when BlueZ reuses the same adapter name. + +use std::sync::{ + Arc, + atomic::{AtomicBool, Ordering}, +}; + +use futures::{Stream, StreamExt}; + +pub(super) struct AdapterLifetime { + lost: Arc, + task: tokio::task::JoinHandle<()>, +} +impl AdapterLifetime { + pub(super) fn watch(events: impl Stream + Send + 'static, name: String) -> Self { + let lost = Arc::new(AtomicBool::new(false)); + let flag = lost.clone(); + let task = tokio::spawn(async move { + futures::pin_mut!(events); + while let Some(event) = events.next().await { + if event == bluer::SessionEvent::AdapterRemoved(name.clone()) { + break; + } + } + flag.store(true, Ordering::Release); + }); + Self { lost, task } + } + pub(super) fn is_lost(&self) -> bool { + self.lost.load(Ordering::Acquire) + } +} +impl Drop for AdapterLifetime { + fn drop(&mut self) { + self.task.abort(); + } +} + +#[cfg(test)] +mod tests { + use futures::channel::mpsc; + + use super::*; + + #[tokio::test] + async fn unrelated_controller_removal_keeps_transport_live() { + let (tx, rx) = mpsc::unbounded(); + let lifetime = AdapterLifetime::watch(rx, "hci1".into()); + tx.unbounded_send(bluer::SessionEvent::AdapterRemoved("hci0".into())) + .expect("remove"); + tokio::task::yield_now().await; + assert!(!lifetime.is_lost()); + } + + #[tokio::test] + async fn closed_controller_event_stream_invalidates_transport() { + let lifetime = AdapterLifetime::watch(futures::stream::empty(), "hci0".into()); + tokio::task::yield_now().await; + assert!(lifetime.is_lost()); + } + + #[tokio::test] + async fn removed_controller_stays_invalid_after_same_name_returns() { + let (tx, rx) = mpsc::unbounded(); + let lifetime = AdapterLifetime::watch(rx, "hci0".into()); + tx.unbounded_send(bluer::SessionEvent::AdapterRemoved("hci0".into())) + .expect("remove"); + tx.unbounded_send(bluer::SessionEvent::AdapterAdded("hci0".into())) + .expect("replace"); + tokio::task::yield_now().await; + assert!(lifetime.is_lost()); + } +} diff --git a/deploy/debian/control b/deploy/debian/control index d8b392c..2c7cb7d 100644 --- a/deploy/debian/control +++ b/deploy/debian/control @@ -10,6 +10,7 @@ Build-Depends: debhelper-compat (= 13), libsecret-1-dev, libssl-dev, pkg-config, + dbus-daemon, gzip Standards-Version: 4.6.2 Homepage: https://github.com/sy/syauth diff --git a/deploy/fedora/syauth.spec b/deploy/fedora/syauth.spec index f034290..41e88f7 100644 --- a/deploy/fedora/syauth.spec +++ b/deploy/fedora/syauth.spec @@ -43,6 +43,7 @@ BuildRequires: rust >= 1.85 BuildRequires: pam-devel BuildRequires: pkgconf-pkg-config BuildRequires: bluez-libs-devel +BuildRequires: dbus-daemon BuildRequires: systemd-rpm-macros BuildRequires: openssl-devel BuildRequires: gzip diff --git a/docs/how-to/troubleshoot.md b/docs/how-to/troubleshoot.md index b60100c..9576bd0 100644 --- a/docs/how-to/troubleshoot.md +++ b/docs/how-to/troubleshoot.md @@ -69,3 +69,19 @@ Include the CLI and APK versions, distribution, BlueZ version, phone model and A The [repository Bluetooth guide](https://github.com/dmytrogajewski/syauth/blob/master/docs/bluetooth.md) includes the emulator harness and its qualifications. Arch Linux KDE users can also consult the optional [package update checker](https://github.com/dmytrogajewski/syauth/blob/master/docs/arch-update-check.md). + +## Controller reset leaves authentication unavailable + +Check `syauth status --json` and the service journal. `daemon.state: up` means +that RPC works. `daemon.transport_ready: true` means the last transport check +found a powered controller and an active advertisement. Neither confirms that +the phone is nearby. + +The daemon checks transport every two seconds. Controller removal, missing +advertising, and advertisement update failures cause a failure exit. The managed +service retries every two seconds, rebuilding GATT and advertising on the +available default controller. Existing bonds stay on disk. An absent controller +does not exhaust the service restart limit. + +An unanswered phone approval does not trigger transport recovery. Bring the phone +back and retry authentication. Each successful request still needs phone approval. diff --git a/docs/reference/cli.md b/docs/reference/cli.md index 758996b..33f42ef 100644 --- a/docs/reference/cli.md +++ b/docs/reference/cli.md @@ -14,7 +14,7 @@ The desktop provides `syauth` for administration and `syauth-presenced` for the `pair --force` replaces the application's existing peer record; it does not reset the Bluetooth system bond. `pair --yes` skips interactive confirmation and is intended for controlled test rigs. `--waybar` and `--yes` are mutually exclusive. -`status --json` reports the daemon section in machine-readable form. A peer's `last_connect_ms_ago` is currently stamped by challenge activity, not a GATT connection event. Use notification subscription logs to establish readiness. +`status --json` reports the daemon section in machine-readable form. A peer's `last_connect_ms_ago` is currently stamped by challenge activity, not a GATT connection event. `daemon.transport_ready` reports controller and advertisement health, sampled every two seconds. A missing or null value means the daemon cannot report transport health. This field does not report phone presence. Use notification subscription logs and an approved challenge to verify the phone connection. `doctor --skip-bluez`, `--skip-systemctl`, and `--skip-daemon` explicitly omit those probes for restricted environments. A skipped probe does not establish that its component works. diff --git a/specs/bugs/BUG-20261107-0012.md b/specs/bugs/BUG-20261107-0012.md new file mode 100644 index 0000000..831d128 --- /dev/null +++ b/specs/bugs/BUG-20261107-0012.md @@ -0,0 +1,78 @@ +# BUG-20261107-0012: Controller loss leaves a running but unusable daemon + +## Summary + +The daemon retained a removed BlueZ controller, logged advertisement failures, +and continued serving status as up while authentication could not reach the phone. + +## Reproduction + +On the affected Fedora laptop, the process started November 1. Advertisement +rotation failed from November 4 at 17:53 with +`target object was either not present or removed`. On November 7, BlueZ exposed +`hci1` with zero active advertisements while RPC still reported `state: up`. +A managed-service restart restored advertising and a phone subscription. A fresh +sudo challenge at 00:14:07.930 completed successfully at 00:14:10.549. + +The kernel separately logged an `hci0` MediaTek coredump on November 6 at +20:09:46 and `hci1` initialization seven seconds later. This later reset does +not establish the cause of the first November 4 advertisement failure. + +## Expected + +Controller loss invalidates transport readiness. The managed service rebuilds +GATT and advertising on the available controller with the existing bond. Phone +absence or an unanswered approval does not invalidate local transport health. + +## Actual + +The rotation loop warned indefinitely. Startup controller selection did not +repair a controller handle invalidated after construction. + +## Root Cause + +PersistentPeripheral retained its original Adapter for its lifetime. The +orchestrator only logged advertisement failures. Neither runtime supervision +nor status distinguished Bluetooth health from RPC liveness. BlueZ setup +failure also left a socket-only process instead of requesting recovery. + +## Fix + +- Subscribe to controller removal events and latch loss even if its name returns. +- Probe controller power and active advertising every two seconds, serialized + against advertisement replacement. Bound the probe to one second. +- Track successful advertisement publication separately from daemon liveness. +- Drain and exit with failure after transport loss or initialization failure. + The service retries every two seconds with no restart limit, selecting the + available default controller and loading the existing bond from disk. +- Add optional `transport_ready` to Status RPC and CLI JSON. Absence means + unknown for older daemons. The field does not report phone presence. +- Bound orchestrator shutdown so a stalled Bluetooth call cannot block recovery. + +## Traceability + +- `tests/lifecycle_smoke.rs::unavailable_bluez_exits_for_supervised_recovery` + failed before startup transport errors propagated. +- `tests/rotation.rs` covers readiness before advertising, healthy advertising + without a phone, failed updates, and controller loss before minute rotation. +- `tests/transport_recovery.rs` drives the real daemon on a private D-Bus with + BlueZ interfaces, removes hci0, observes failure exit and socket cleanup, then + relaunches on hci1 and checks bond hydration and unchanged bond/key bytes. +- `peripheral/health.rs` covers controller name reuse, unrelated removal, and + event-stream termination. +- RPC roundtrip and legacy decode tests cover wire compatibility. CLI tests + preserve transport failure independently of `daemon.state: up`. +- Display consumer: sibling sy `BUG-20261107-0012` and sy-auth applet regressions. + +## Validation + +The isolated PR worktree passes `make lint` and `make test`, with 460 passing +test results, zero failures, and 10 ignored. The full host working tree also +passes with 486 results, including independent PAM work outside this change. + +The native RPM was installed on November 7. The managed service started at +01:36:09, advertised at 01:36:11, and the phone subscribed at 01:36:17. A fresh +sudo challenge completed through phone approval at 01:36:33.173–01:36:35.308. +`rpm -V syauth` passes. All seven bond/key files retain their original contents, +ownership, and private modes. Physical removal is covered on the private bus; +normal advertising and authentication are verified on the live host.