mirror of
https://gitlab.steamos.cloud/holo/steamos-manager.git
synced 2025-07-17 11:46:46 -04:00
daemon: Start bringing up contexts and state loading/saving
This commit is contained in:
parent
23267c65e0
commit
c6113ee739
6 changed files with 275 additions and 19 deletions
|
@ -5,8 +5,40 @@
|
|||
* SPDX-License-Identifier: MIT
|
||||
*/
|
||||
|
||||
use anyhow::Result;
|
||||
use anyhow::{anyhow, Result};
|
||||
use std::io::ErrorKind;
|
||||
use tokio::fs::{create_dir_all, read_to_string, write};
|
||||
use tracing::{error, info};
|
||||
|
||||
pub(in crate::daemon) async fn read_config() -> Result<()> {
|
||||
use crate::daemon::DaemonContext;
|
||||
|
||||
pub(in crate::daemon) async fn read_state<C: DaemonContext>(context: &C) -> Result<C::State> {
|
||||
let path = context.state_path()?;
|
||||
let state = match read_to_string(path).await {
|
||||
Ok(state) => state,
|
||||
Err(e) => {
|
||||
if e.kind() == ErrorKind::NotFound {
|
||||
info!("No state file found, reloading default state");
|
||||
return Ok(C::State::default());
|
||||
}
|
||||
error!("Error loading state: {e}");
|
||||
return Err(e.into());
|
||||
}
|
||||
};
|
||||
Ok(toml::from_str(state.as_str())?)
|
||||
}
|
||||
|
||||
pub(in crate::daemon) async fn write_state<C: DaemonContext>(context: &C) -> Result<()> {
|
||||
let path = context.state_path()?;
|
||||
create_dir_all(path.parent().ok_or(anyhow!(
|
||||
"Context path {} has no parent dir",
|
||||
path.to_string_lossy()
|
||||
))?)
|
||||
.await?;
|
||||
let state = toml::to_string_pretty(&context.state())?;
|
||||
Ok(write(path, state.as_bytes()).await?)
|
||||
}
|
||||
|
||||
pub(in crate::daemon) async fn read_config<C: DaemonContext>(_context: &C) -> Result<C::Config> {
|
||||
todo!();
|
||||
}
|
||||
|
|
|
@ -6,6 +6,10 @@
|
|||
*/
|
||||
|
||||
use anyhow::{anyhow, ensure, Result};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt::Debug;
|
||||
use std::marker::PhantomData;
|
||||
use std::path::PathBuf;
|
||||
use tokio::signal::unix::{signal, SignalKind};
|
||||
use tokio::task::JoinSet;
|
||||
use tokio_util::sync::CancellationToken;
|
||||
|
@ -14,7 +18,7 @@ use tracing_subscriber::layer::SubscriberExt;
|
|||
use tracing_subscriber::registry::LookupSpan;
|
||||
use zbus::connection::Connection;
|
||||
|
||||
use crate::daemon::config::read_config;
|
||||
use crate::daemon::config::{read_config, read_state};
|
||||
use crate::sls::{LogLayer, LogReceiver};
|
||||
use crate::Service;
|
||||
|
||||
|
@ -25,16 +29,40 @@ mod user;
|
|||
pub use root::daemon as root;
|
||||
pub use user::daemon as user;
|
||||
|
||||
pub(crate) struct Daemon {
|
||||
services: JoinSet<Result<()>>,
|
||||
token: CancellationToken,
|
||||
pub(crate) trait DaemonContext: Sized {
|
||||
type State: for<'a> Deserialize<'a> + Serialize + Default + Debug;
|
||||
type Config: for<'a> Deserialize<'a> + Default + Debug;
|
||||
|
||||
fn state_path(&self) -> Result<PathBuf> {
|
||||
let config_path = self.user_config_path()?;
|
||||
Ok(config_path.join("state.toml"))
|
||||
}
|
||||
|
||||
fn user_config_path(&self) -> Result<PathBuf>;
|
||||
fn system_config_path(&self) -> Result<PathBuf>;
|
||||
fn state(&self) -> Self::State;
|
||||
|
||||
async fn start(
|
||||
&mut self,
|
||||
state: Self::State,
|
||||
config: Self::Config,
|
||||
daemon: &mut Daemon<Self>,
|
||||
) -> Result<()>;
|
||||
|
||||
async fn reload(&mut self, config: Self::Config, daemon: &mut Daemon<Self>) -> Result<()>;
|
||||
}
|
||||
|
||||
impl Daemon {
|
||||
pub(crate) struct Daemon<C: DaemonContext> {
|
||||
services: JoinSet<Result<()>>,
|
||||
token: CancellationToken,
|
||||
_context: PhantomData<C>,
|
||||
}
|
||||
|
||||
impl<C: DaemonContext> Daemon<C> {
|
||||
pub(crate) async fn new<S: SubscriberExt + Send + Sync + for<'a> LookupSpan<'a>>(
|
||||
subscriber: S,
|
||||
connection: Connection,
|
||||
) -> Result<Daemon> {
|
||||
) -> Result<Daemon<C>> {
|
||||
let services = JoinSet::new();
|
||||
let token = CancellationToken::new();
|
||||
|
||||
|
@ -43,7 +71,11 @@ impl Daemon {
|
|||
let subscriber = subscriber.with(remote_logger);
|
||||
tracing::subscriber::set_global_default(subscriber)?;
|
||||
|
||||
let mut daemon = Daemon { services, token };
|
||||
let mut daemon = Daemon {
|
||||
services,
|
||||
token,
|
||||
_context: PhantomData::default(),
|
||||
};
|
||||
daemon.add_service(log_receiver);
|
||||
|
||||
Ok(daemon)
|
||||
|
@ -57,12 +89,16 @@ impl Daemon {
|
|||
token
|
||||
}
|
||||
|
||||
pub(crate) async fn run(&mut self) -> Result<()> {
|
||||
pub(crate) async fn run(&mut self, mut context: C) -> Result<()> {
|
||||
ensure!(
|
||||
!self.services.is_empty(),
|
||||
"Can't run a daemon with no services attached."
|
||||
);
|
||||
|
||||
let state = read_state(&context).await?;
|
||||
let config = read_config(&context).await?;
|
||||
context.start(state, config, self).await?;
|
||||
|
||||
let mut res = loop {
|
||||
let mut sigterm = signal(SignalKind::terminate())?;
|
||||
let mut sigquit = signal(SignalKind::quit())?;
|
||||
|
@ -81,10 +117,14 @@ impl Daemon {
|
|||
},
|
||||
e = sighup.recv() => match e {
|
||||
Some(_) => {
|
||||
if let Err(error) = read_config().await {
|
||||
error!("Failed to reload configuration: {error}");
|
||||
match read_config(&context).await {
|
||||
Ok(config) =>
|
||||
context.reload(config, self).await,
|
||||
Err(error) => {
|
||||
error!("Failed to load configuration: {error}");
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
None => Err(anyhow!("SIGHUP machine broke")),
|
||||
},
|
||||
|
|
|
@ -6,17 +6,74 @@
|
|||
*/
|
||||
|
||||
use anyhow::{bail, Result};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::path::PathBuf;
|
||||
use tracing::error;
|
||||
use tracing_subscriber::prelude::*;
|
||||
use tracing_subscriber::{fmt, Registry};
|
||||
use zbus::connection::Connection;
|
||||
use zbus::ConnectionBuilder;
|
||||
|
||||
use crate::daemon::Daemon;
|
||||
use crate::daemon::{Daemon, DaemonContext};
|
||||
use crate::ds_inhibit::Inhibitor;
|
||||
use crate::manager::root::SteamOSManager;
|
||||
use crate::path;
|
||||
use crate::sls::ftrace::Ftrace;
|
||||
|
||||
#[derive(Copy, Clone, Default, Deserialize, Serialize, Debug)]
|
||||
pub(crate) struct RootConfig {
|
||||
pub services: RootServicesConfig,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Default, Deserialize, Serialize, Debug)]
|
||||
pub(crate) struct RootServicesConfig {}
|
||||
|
||||
#[derive(Copy, Clone, Default, Deserialize, Serialize, Debug)]
|
||||
pub(crate) struct RootState {
|
||||
pub services: RootServicesState,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Default, Deserialize, Serialize, Debug)]
|
||||
pub(crate) struct RootServicesState {}
|
||||
|
||||
struct RootContext {}
|
||||
|
||||
impl DaemonContext for RootContext {
|
||||
type State = RootState;
|
||||
type Config = RootConfig;
|
||||
|
||||
fn user_config_path(&self) -> Result<PathBuf> {
|
||||
Ok(path("/etc/steamos-manager"))
|
||||
}
|
||||
|
||||
fn system_config_path(&self) -> Result<PathBuf> {
|
||||
Ok(path("/usr/lib/steamos-manager/system.d"))
|
||||
}
|
||||
|
||||
fn state(&self) -> RootState {
|
||||
RootState::default()
|
||||
}
|
||||
|
||||
async fn start(
|
||||
&mut self,
|
||||
_state: RootState,
|
||||
_config: RootConfig,
|
||||
_daemon: &mut Daemon<RootContext>,
|
||||
) -> Result<()> {
|
||||
// Nothing to do yet
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn reload(
|
||||
&mut self,
|
||||
_config: RootConfig,
|
||||
_daemon: &mut Daemon<RootContext>,
|
||||
) -> Result<()> {
|
||||
// Nothing to do yet
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
async fn create_connection() -> Result<Connection> {
|
||||
let connection = ConnectionBuilder::system()?
|
||||
.name("com.steampowered.SteamOSManager1")?
|
||||
|
@ -45,6 +102,8 @@ pub async fn daemon() -> Result<()> {
|
|||
bail!(e);
|
||||
}
|
||||
};
|
||||
|
||||
let context = RootContext {};
|
||||
let mut daemon = Daemon::new(subscriber, connection.clone()).await?;
|
||||
|
||||
let ftrace = Ftrace::init(connection.clone()).await?;
|
||||
|
@ -53,5 +112,5 @@ pub async fn daemon() -> Result<()> {
|
|||
let inhibitor = Inhibitor::init().await?;
|
||||
daemon.add_service(inhibitor);
|
||||
|
||||
daemon.run().await
|
||||
daemon.run(context).await
|
||||
}
|
||||
|
|
|
@ -6,14 +6,80 @@
|
|||
*/
|
||||
|
||||
use anyhow::{bail, Result};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::path::PathBuf;
|
||||
use tracing::error;
|
||||
use tracing_subscriber::prelude::*;
|
||||
use tracing_subscriber::{fmt, Registry};
|
||||
#[cfg(not(test))]
|
||||
use xdg::BaseDirectories;
|
||||
use zbus::connection::Connection;
|
||||
use zbus::ConnectionBuilder;
|
||||
|
||||
use crate::daemon::Daemon;
|
||||
use crate::daemon::{Daemon, DaemonContext};
|
||||
use crate::manager::user::SteamOSManager;
|
||||
use crate::path;
|
||||
|
||||
#[derive(Copy, Clone, Default, Deserialize, Serialize, Debug)]
|
||||
struct UserConfig {
|
||||
pub services: UserServicesConfig,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Default, Deserialize, Serialize, Debug)]
|
||||
pub(crate) struct UserServicesConfig {}
|
||||
|
||||
#[derive(Copy, Clone, Default, Deserialize, Serialize, Debug)]
|
||||
struct UserState {
|
||||
pub services: UserServicesState,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Default, Deserialize, Serialize, Debug)]
|
||||
pub(crate) struct UserServicesState {}
|
||||
|
||||
struct UserContext {}
|
||||
|
||||
impl DaemonContext for UserContext {
|
||||
type State = UserState;
|
||||
type Config = UserConfig;
|
||||
|
||||
#[cfg(not(test))]
|
||||
fn user_config_path(&self) -> Result<PathBuf> {
|
||||
let xdg_base = BaseDirectories::new()?;
|
||||
Ok(xdg_base.get_config_file("steamos-manager"))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
fn user_config_path(&self) -> Result<PathBuf> {
|
||||
Ok(path("steamos-manager"))
|
||||
}
|
||||
|
||||
fn system_config_path(&self) -> Result<PathBuf> {
|
||||
Ok(path("/usr/lib/steamos-manager/user.d"))
|
||||
}
|
||||
|
||||
fn state(&self) -> UserState {
|
||||
UserState::default()
|
||||
}
|
||||
|
||||
async fn start(
|
||||
&mut self,
|
||||
_state: UserState,
|
||||
_config: UserConfig,
|
||||
_daemon: &mut Daemon<UserContext>,
|
||||
) -> Result<()> {
|
||||
// Nothing to do yet
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn reload(
|
||||
&mut self,
|
||||
_config: UserConfig,
|
||||
_daemon: &mut Daemon<UserContext>,
|
||||
) -> Result<()> {
|
||||
// Nothing to do yet
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
async fn create_connections() -> Result<(Connection, Connection)> {
|
||||
let system = Connection::system().await?;
|
||||
|
@ -47,7 +113,8 @@ pub async fn daemon() -> Result<()> {
|
|||
}
|
||||
};
|
||||
|
||||
let context = UserContext {};
|
||||
let mut daemon = Daemon::new(subscriber, system).await?;
|
||||
|
||||
daemon.run().await
|
||||
daemon.run(context).await
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue