6006d20178
greeter: fix Wayland socket timeout (45s wait, dual-path probe) sessiond: add D-Bus socket candidate probing with better error logging config: align greeter/sddm runtime dirs and DRM wait timeouts
247 lines
8.2 KiB
Rust
247 lines
8.2 KiB
Rust
mod acpi_watcher;
|
|
mod control;
|
|
mod device_map;
|
|
mod manager;
|
|
mod runtime_state;
|
|
mod seat;
|
|
mod session;
|
|
|
|
use std::{
|
|
env,
|
|
error::Error,
|
|
process,
|
|
time::Duration,
|
|
};
|
|
|
|
use device_map::DeviceMap;
|
|
use manager::LoginManager;
|
|
use seat::LoginSeat;
|
|
use session::LoginSession;
|
|
use runtime_state::shared_runtime;
|
|
use tokio::runtime::Builder as RuntimeBuilder;
|
|
use zbus::{
|
|
Address,
|
|
connection::Builder as ConnectionBuilder,
|
|
zvariant::OwnedObjectPath,
|
|
};
|
|
|
|
const BUS_NAME: &str = "org.freedesktop.login1";
|
|
const MANAGER_PATH: &str = "/org/freedesktop/login1";
|
|
const SESSION_PATH: &str = "/org/freedesktop/login1/session/c1";
|
|
const SEAT_PATH: &str = "/org/freedesktop/login1/seat/seat0";
|
|
const USER_PATH: &str = "/org/freedesktop/login1/user/current";
|
|
|
|
enum Command {
|
|
Run,
|
|
Help,
|
|
}
|
|
|
|
fn usage() -> &'static str {
|
|
"Usage: redbear-sessiond [--help]"
|
|
}
|
|
|
|
fn parse_args() -> Result<Command, String> {
|
|
let mut args = env::args().skip(1);
|
|
|
|
match args.next() {
|
|
None => Ok(Command::Run),
|
|
Some(arg) if arg == "--help" || arg == "-h" => {
|
|
if args.next().is_some() {
|
|
return Err(String::from("unexpected extra arguments after --help"));
|
|
}
|
|
|
|
Ok(Command::Help)
|
|
}
|
|
Some(arg) => Err(format!("unrecognized argument '{arg}'")),
|
|
}
|
|
}
|
|
|
|
fn parse_object_path(path: &str) -> Result<OwnedObjectPath, Box<dyn Error>> {
|
|
Ok(OwnedObjectPath::try_from(path.to_owned())?)
|
|
}
|
|
|
|
/// Candidate D-Bus system bus socket locations, in order of preference.
|
|
///
|
|
/// Red Bear OS may launch `dbus-daemon --system` with the standard Redox path
|
|
/// (`/run/dbus/system_bus_socket`) or with one of the FHS-style fallback paths
|
|
/// that D-Bus itself accepts (`/var/run/dbus/...`). The `DBUS_STARTER_ADDRESS`
|
|
/// and `DBUS_SYSTEM_BUS_ADDRESS` env vars take precedence when set.
|
|
fn dbus_socket_candidates() -> Vec<String> {
|
|
let mut candidates: Vec<String> = Vec::new();
|
|
|
|
for env_name in ["DBUS_STARTER_ADDRESS", "DBUS_SYSTEM_BUS_ADDRESS"] {
|
|
if let Ok(addr) = env::var(env_name) {
|
|
if let Some(path) = addr.strip_prefix("unix:path=") {
|
|
candidates.push(path.to_string());
|
|
} else if addr.starts_with('/') {
|
|
// Bare path provided by the caller.
|
|
candidates.push(addr);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Standard locations, in the order D-Bus itself tries them.
|
|
for fallback in [
|
|
"/run/dbus/system_bus_socket",
|
|
"/var/run/dbus/system_bus_socket",
|
|
] {
|
|
if !candidates.iter().any(|c| c == fallback) {
|
|
candidates.push(fallback.to_string());
|
|
}
|
|
}
|
|
|
|
candidates
|
|
}
|
|
|
|
async fn wait_for_dbus_socket() {
|
|
let candidates = dbus_socket_candidates();
|
|
eprintln!(
|
|
"redbear-sessiond: probing {} D-Bus socket candidate(s): {:?}",
|
|
candidates.len(),
|
|
candidates
|
|
);
|
|
|
|
// 30s budget total, polled at 1s intervals. We cycle through candidates
|
|
// each tick so a freshly-bound socket on a non-default path is picked up
|
|
// without having to wait a full 30s for the env-driven path.
|
|
for tick in 0..30 {
|
|
for candidate in &candidates {
|
|
if tokio::net::UnixStream::connect(candidate).await.is_ok() {
|
|
eprintln!(
|
|
"redbear-sessiond: D-Bus socket reachable at {candidate} after {tick}s"
|
|
);
|
|
return;
|
|
}
|
|
}
|
|
if tick == 0 {
|
|
eprintln!("redbear-sessiond: still waiting for D-Bus socket (up to 30s)");
|
|
} else if tick % 5 == 0 {
|
|
eprintln!("redbear-sessiond: still waiting for D-Bus socket ({tick}s elapsed)");
|
|
}
|
|
tokio::time::sleep(Duration::from_secs(1)).await;
|
|
}
|
|
eprintln!(
|
|
"redbear-sessiond: timed out waiting for D-Bus socket, last tried: {:?}",
|
|
candidates
|
|
);
|
|
}
|
|
|
|
fn system_connection_builder() -> Result<ConnectionBuilder<'static>, Box<dyn Error>> {
|
|
// Prefer explicit env var, then D-Bus starter address (set by activation),
|
|
// then Red Bear's configured system bus address, then the standard path.
|
|
let addr_str = env::var("DBUS_STARTER_ADDRESS")
|
|
.or_else(|_| env::var("DBUS_SYSTEM_BUS_ADDRESS"))
|
|
.unwrap_or_else(|_| "unix:path=/run/dbus/system_bus_socket".to_string());
|
|
Ok(ConnectionBuilder::address(Address::try_from(addr_str.as_str())?)?)
|
|
}
|
|
|
|
#[cfg(all(unix, not(target_os = "redox")))]
|
|
async fn wait_for_shutdown(mut shutdown_rx: tokio::sync::watch::Receiver<bool>) -> Result<(), Box<dyn Error>> {
|
|
use tokio::signal::unix::{SignalKind, signal};
|
|
|
|
let mut terminate = signal(SignalKind::terminate())?;
|
|
|
|
tokio::select! {
|
|
_ = terminate.recv() => Ok(()),
|
|
_ = tokio::signal::ctrl_c() => Ok(()),
|
|
_ = shutdown_rx.changed() => Ok(()),
|
|
}
|
|
}
|
|
|
|
#[cfg(target_os = "redox")]
|
|
async fn wait_for_shutdown(mut shutdown_rx: tokio::sync::watch::Receiver<bool>) -> Result<(), Box<dyn Error>> {
|
|
tokio::select! {
|
|
_ = std::future::pending::<()>() => Ok(()),
|
|
_ = shutdown_rx.changed() => Ok(()),
|
|
}
|
|
}
|
|
|
|
#[cfg(all(not(unix), not(target_os = "redox")))]
|
|
async fn wait_for_shutdown(mut shutdown_rx: tokio::sync::watch::Receiver<bool>) -> Result<(), Box<dyn Error>> {
|
|
tokio::select! {
|
|
_ = tokio::signal::ctrl_c() => Ok(()),
|
|
_ = shutdown_rx.changed() => Ok(()),
|
|
}
|
|
}
|
|
|
|
async fn run_daemon() -> Result<(), Box<dyn Error>> {
|
|
wait_for_dbus_socket().await;
|
|
|
|
let (shutdown_tx, shutdown_rx) = tokio::sync::watch::channel(false);
|
|
|
|
// Resolve the bus address once so retry logs can name the actual endpoint
|
|
// we are connecting to (was previously hidden, making transient I/O errors
|
|
// impossible to triage).
|
|
let bus_addr = env::var("DBUS_STARTER_ADDRESS")
|
|
.or_else(|_| env::var("DBUS_SYSTEM_BUS_ADDRESS"))
|
|
.unwrap_or_else(|_| "unix:path=/run/dbus/system_bus_socket".to_string());
|
|
|
|
let mut last_err = None;
|
|
for attempt in 1..=3 {
|
|
let session_path = parse_object_path(SESSION_PATH)?;
|
|
let seat_path = parse_object_path(SEAT_PATH)?;
|
|
let user_path = parse_object_path(USER_PATH)?;
|
|
let runtime = shared_runtime();
|
|
let device_map = DeviceMap::discover();
|
|
|
|
let session = LoginSession::new(seat_path.clone(), user_path.clone(), device_map, runtime.clone());
|
|
let seat = LoginSeat::new(session_path.clone(), runtime.clone());
|
|
let manager = LoginManager::new(session_path, seat_path, user_path, runtime.clone());
|
|
|
|
match system_connection_builder()?
|
|
.name(BUS_NAME)?
|
|
.serve_at(MANAGER_PATH, manager)?
|
|
.serve_at(SESSION_PATH, session)?
|
|
.serve_at(SEAT_PATH, seat)?
|
|
.build()
|
|
.await
|
|
{
|
|
Ok(connection) => {
|
|
eprintln!("redbear-sessiond: registered {BUS_NAME} on the system bus at {bus_addr}");
|
|
control::start_control_socket(runtime.clone(), shutdown_tx.clone());
|
|
tokio::spawn(acpi_watcher::watch_and_emit(connection.clone(), runtime.clone()));
|
|
wait_for_shutdown(shutdown_rx).await?;
|
|
drop(connection);
|
|
return Ok(());
|
|
}
|
|
Err(err) => {
|
|
if attempt < 3 {
|
|
eprintln!(
|
|
"redbear-sessiond: attempt {attempt}/3 failed on {bus_addr} ({err}), retrying in 1s..."
|
|
);
|
|
tokio::time::sleep(Duration::from_secs(1)).await;
|
|
}
|
|
last_err = Some(err.into());
|
|
}
|
|
}
|
|
}
|
|
Err(last_err.unwrap())
|
|
}
|
|
|
|
fn main() {
|
|
match parse_args() {
|
|
Ok(Command::Help) => {
|
|
println!("{}", usage());
|
|
}
|
|
Ok(Command::Run) => {
|
|
let runtime = match RuntimeBuilder::new_multi_thread().enable_all().build() {
|
|
Ok(runtime) => runtime,
|
|
Err(err) => {
|
|
eprintln!("redbear-sessiond: failed to create tokio runtime: {err}");
|
|
process::exit(1);
|
|
}
|
|
};
|
|
|
|
if let Err(err) = runtime.block_on(run_daemon()) {
|
|
eprintln!("redbear-sessiond: fatal error: {err}");
|
|
process::exit(1);
|
|
}
|
|
}
|
|
Err(err) => {
|
|
eprintln!("redbear-sessiond: {err}");
|
|
eprintln!("{}", usage());
|
|
process::exit(1);
|
|
}
|
|
}
|
|
}
|