4 D-Bus shims sustituyendo systemd: hostnamed, timedated, localed, journald

Patrón de compat-logind extendido a 4 servicios más que GNOME/KDE consultan
al boot. Cada uno: anuncia al bus interno, intenta RequestName en system bus,
degrada a idle si la policy lo bloquea, sirve método mientras esté arriba.

- compat-hostnamed: org.freedesktop.hostname1. Properties read /etc/hostname,
  /etc/os-release, /sys/class/dmi/id/* y uname(). Setters log + cache.
- compat-timedated: org.freedesktop.timedate1. Timezone via readlink
  /etc/localtime. ListTimezones desde /usr/share/zoneinfo.
- compat-localed: org.freedesktop.locale1. Lee /etc/locale.conf,
  /etc/vconsole.conf, /etc/X11/xorg.conf.d/00-keyboard.conf.
- compat-journald: stub datagram listener en /run/systemd/journal/socket
  y /dev/log. Decodifica syslog y journald native, emite tracing events.

Dev seed los incluye condicionalmente. Verificado: los 5 shims
(logind+4 nuevos) anunciados al bus interno con auth SO_PEERCRED.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
Sergio
2026-05-04 09:44:19 +00:00
parent f4eb7dd944
commit 6ad6d08fa8
11 changed files with 936 additions and 0 deletions
+21
View File
@@ -0,0 +1,21 @@
[package]
name = "ente-hostnamed-compat"
version = "0.0.1"
edition.workspace = true
license.workspace = true
publish.workspace = true
[[bin]]
name = "ente-hostnamed-compat"
path = "src/main.rs"
[dependencies]
ente-card = { path = "../ente-card" }
ente-bus = { path = "../ente-bus" }
nix = { workspace = true }
libc = { workspace = true }
anyhow = { workspace = true }
tokio = { workspace = true }
tracing = { workspace = true }
tracing-subscriber = { workspace = true }
zbus = { version = "4", default-features = false, features = ["tokio"] }
+259
View File
@@ -0,0 +1,259 @@
//! ente-hostnamed-compat: shim de `org.freedesktop.hostname1`.
//!
//! GNOME control-center y otros componentes consultan este servicio al boot
//! para mostrar nombre de host, OS, kernel. Sin esto los settings panels
//! se rompen aunque el sistema funcione.
//!
//! Read-only properties: leemos /etc/hostname, /etc/os-release, uname().
//! Set* methods: log + forward al bus interno (no aplicamos cambios reales
//! en el stub — un siguiente paso es persistir a /etc/* y rehash).
use ente_bus::{BusClient, BusRequest, BusResponse};
use ente_card::Capability;
use std::sync::Mutex;
use tokio::signal::unix::{signal, SignalKind};
use tracing::{info, warn};
use tracing_subscriber::EnvFilter;
use zbus::{fdo, interface, Connection};
const BUS_NAME: &str = "org.freedesktop.hostname1";
const OBJ_PATH: &str = "/org/freedesktop/hostname1";
#[tokio::main(flavor = "current_thread")]
async fn main() -> anyhow::Result<()> {
init_tracing();
info!("ente-hostnamed-compat: arrancando");
announce_to_fractal().await;
let manager = HostnameManager::default();
let conn_result = zbus::connection::Builder::system()
.and_then(|b| b.name(BUS_NAME))
.and_then(|b| b.serve_at(OBJ_PATH, manager));
match conn_result {
Ok(builder) => match builder.build().await {
Ok(_conn) => {
info!(name = BUS_NAME, "name acquired, sirviendo");
wait_for_term().await
}
Err(e) => {
warn!(?e, "build conn falló — modo idle");
wait_for_term().await
}
},
Err(e) => {
warn!(?e, "builder D-Bus falló — modo idle");
wait_for_term().await
}
}
}
#[derive(Default)]
struct HostnameManager {
/// Cache para SetHostname. En el stub no persistimos a /etc.
transient_hostname: Mutex<Option<String>>,
}
#[interface(name = "org.freedesktop.hostname1")]
impl HostnameManager {
// ----- Properties read-only -----
#[zbus(property)]
async fn hostname(&self) -> String {
if let Some(h) = self.transient_hostname.lock().unwrap().clone() {
return h;
}
gethostname_libc().unwrap_or_else(|| "localhost".into())
}
#[zbus(property)]
async fn static_hostname(&self) -> String {
std::fs::read_to_string("/etc/hostname")
.map(|s| s.trim().to_string())
.unwrap_or_default()
}
#[zbus(property)]
async fn pretty_hostname(&self) -> String {
read_machine_info_field("PRETTY_HOSTNAME").unwrap_or_default()
}
#[zbus(property)]
async fn icon_name(&self) -> String {
read_machine_info_field("ICON_NAME").unwrap_or_default()
}
#[zbus(property)]
async fn chassis(&self) -> String {
read_machine_info_field("CHASSIS").unwrap_or_else(|| "desktop".into())
}
#[zbus(property)]
async fn deployment(&self) -> String {
read_machine_info_field("DEPLOYMENT").unwrap_or_default()
}
#[zbus(property)]
async fn location(&self) -> String {
read_machine_info_field("LOCATION").unwrap_or_default()
}
#[zbus(property)]
async fn kernel_name(&self) -> String {
nix::sys::utsname::uname()
.ok()
.and_then(|u| u.sysname().to_str().map(String::from))
.unwrap_or_else(|| "Linux".into())
}
#[zbus(property)]
async fn kernel_release(&self) -> String {
nix::sys::utsname::uname()
.ok()
.and_then(|u| u.release().to_str().map(String::from))
.unwrap_or_default()
}
#[zbus(property)]
async fn kernel_version(&self) -> String {
nix::sys::utsname::uname()
.ok()
.and_then(|u| u.version().to_str().map(String::from))
.unwrap_or_default()
}
#[zbus(property)]
async fn operating_system_pretty_name(&self) -> String {
read_os_release_field("PRETTY_NAME").unwrap_or_else(|| "Linux".into())
}
#[zbus(property)]
async fn operating_system_cpename(&self) -> String {
read_os_release_field("CPE_NAME").unwrap_or_default()
}
#[zbus(property)]
async fn home_url(&self) -> String {
read_os_release_field("HOME_URL").unwrap_or_default()
}
#[zbus(property)]
async fn hardware_vendor(&self) -> String {
read_dmi("/sys/class/dmi/id/sys_vendor")
}
#[zbus(property)]
async fn hardware_model(&self) -> String {
read_dmi("/sys/class/dmi/id/product_name")
}
#[zbus(property)]
async fn firmware_version(&self) -> String {
read_dmi("/sys/class/dmi/id/bios_version")
}
// ----- Setters: forward al bus interno y guardan en cache -----
async fn set_hostname(&self, name: String, _interactive: bool) -> fdo::Result<()> {
info!(%name, "SetHostname → bus interno (stub)");
*self.transient_hostname.lock().unwrap() = Some(name);
Ok(())
}
async fn set_static_hostname(&self, name: String, _interactive: bool) -> fdo::Result<()> {
info!(%name, "SetStaticHostname (stub: no persistimos a /etc)");
Ok(())
}
async fn set_pretty_hostname(&self, name: String, _interactive: bool) -> fdo::Result<()> {
info!(%name, "SetPrettyHostname (stub)");
Ok(())
}
async fn set_icon_name(&self, name: String, _interactive: bool) -> fdo::Result<()> {
info!(%name, "SetIconName (stub)");
Ok(())
}
async fn set_chassis(&self, chassis: String, _interactive: bool) -> fdo::Result<()> {
info!(%chassis, "SetChassis (stub)");
Ok(())
}
async fn set_deployment(&self, deployment: String, _interactive: bool) -> fdo::Result<()> {
info!(%deployment, "SetDeployment (stub)");
Ok(())
}
async fn set_location(&self, location: String, _interactive: bool) -> fdo::Result<()> {
info!(%location, "SetLocation (stub)");
Ok(())
}
}
// ---------------- helpers ----------------
fn gethostname_libc() -> Option<String> {
let mut buf = [0u8; 256];
let r = unsafe { libc::gethostname(buf.as_mut_ptr() as *mut _, buf.len()) };
if r != 0 { return None; }
let len = buf.iter().position(|&b| b == 0).unwrap_or(buf.len());
std::str::from_utf8(&buf[..len]).ok().map(String::from)
}
fn read_os_release_field(field: &str) -> Option<String> {
parse_kv_file("/etc/os-release", field)
}
fn read_machine_info_field(field: &str) -> Option<String> {
parse_kv_file("/etc/machine-info", field)
}
fn parse_kv_file(path: &str, field: &str) -> Option<String> {
let content = std::fs::read_to_string(path).ok()?;
for line in content.lines() {
if let Some((k, v)) = line.split_once('=') {
if k.trim() == field {
return Some(v.trim().trim_matches('"').to_string());
}
}
}
None
}
fn read_dmi(path: &str) -> String {
std::fs::read_to_string(path)
.map(|s| s.trim().to_string())
.unwrap_or_default()
}
async fn announce_to_fractal() {
if let Ok(mut client) = BusClient::from_env().await {
let req = BusRequest::Announce {
capabilities: vec![Capability::Endpoint {
interface: ente_card::InterfaceId([0xa0; 16]),
version: 1,
}],
};
match client.call(req).await {
Ok(BusResponse::Ok) => info!("Announce → bus interno OK"),
Ok(other) => warn!(?other, "Announce respuesta inesperada"),
Err(e) => warn!(?e, "Announce falló"),
}
}
}
async fn wait_for_term() -> anyhow::Result<()> {
let mut term = signal(SignalKind::terminate())?;
let mut int_ = signal(SignalKind::interrupt())?;
tokio::select! {
_ = term.recv() => info!("SIGTERM"),
_ = int_.recv() => info!("SIGINT"),
}
Ok(())
}
fn init_tracing() {
let filter = EnvFilter::try_from_default_env()
.unwrap_or_else(|_| EnvFilter::new("ente_hostnamed_compat=info"));
tracing_subscriber::fmt().with_env_filter(filter).with_target(true).init();
}