2022-06-22 12:22:30 -04:00
|
|
|
// SPDX-License-Identifier: GPL-3.0-only
|
2022-06-22 14:09:59 -04:00
|
|
|
#[macro_use]
|
|
|
|
|
extern crate tracing;
|
|
|
|
|
|
2022-06-24 11:43:48 -04:00
|
|
|
mod comp;
|
2022-06-22 14:09:59 -04:00
|
|
|
mod process;
|
2022-07-22 11:48:03 -04:00
|
|
|
mod service;
|
2022-07-12 11:55:53 -04:00
|
|
|
mod systemd;
|
2022-06-22 14:09:59 -04:00
|
|
|
|
2022-06-24 11:43:48 -04:00
|
|
|
use async_signals::Signals;
|
2022-06-22 12:22:30 -04:00
|
|
|
use color_eyre::{eyre::WrapErr, Result};
|
2022-06-24 11:43:48 -04:00
|
|
|
use futures_util::StreamExt;
|
2022-09-19 13:47:44 -04:00
|
|
|
use launch_pad::{process::Process, ProcessManager};
|
2022-07-11 10:47:00 -04:00
|
|
|
use tokio::sync::{mpsc, oneshot};
|
2022-06-24 11:43:48 -04:00
|
|
|
use tokio_util::sync::CancellationToken;
|
2022-06-22 12:22:30 -04:00
|
|
|
use tracing::metadata::LevelFilter;
|
|
|
|
|
use tracing_subscriber::{fmt, prelude::*, EnvFilter};
|
2022-07-22 11:48:03 -04:00
|
|
|
use zbus::ConnectionBuilder;
|
2022-06-22 12:22:30 -04:00
|
|
|
|
|
|
|
|
#[tokio::main(flavor = "current_thread")]
|
|
|
|
|
async fn main() -> Result<()> {
|
|
|
|
|
color_eyre::install().wrap_err("failed to install color_eyre error handler")?;
|
|
|
|
|
|
|
|
|
|
tracing_subscriber::registry()
|
2022-07-11 11:25:34 -04:00
|
|
|
.with(tracing_journald::layer().wrap_err("failed to connect to journald")?)
|
2022-06-22 12:22:30 -04:00
|
|
|
.with(fmt::layer())
|
|
|
|
|
.with(
|
|
|
|
|
EnvFilter::builder()
|
|
|
|
|
.with_default_directive(LevelFilter::INFO.into())
|
|
|
|
|
.from_env_lossy(),
|
|
|
|
|
)
|
|
|
|
|
.try_init()
|
2022-06-23 10:45:57 -04:00
|
|
|
.wrap_err("failed to initialize logger")?;
|
2022-06-22 14:09:59 -04:00
|
|
|
|
|
|
|
|
info!("Starting cosmic-session");
|
2022-06-22 12:22:30 -04:00
|
|
|
|
2022-09-23 14:52:14 -04:00
|
|
|
let process_manager = ProcessManager::new().await;
|
2022-06-24 11:43:48 -04:00
|
|
|
let token = CancellationToken::new();
|
2022-06-28 16:17:43 -04:00
|
|
|
let (socket_tx, socket_rx) = mpsc::unbounded_channel();
|
2022-07-11 10:47:00 -04:00
|
|
|
let (env_tx, env_rx) = oneshot::channel();
|
2022-09-23 14:52:14 -04:00
|
|
|
let compositor_handle =
|
|
|
|
|
comp::run_compositor(&process_manager, token.child_token(), socket_rx, env_tx)
|
|
|
|
|
.wrap_err("failed to start compositor")?;
|
2022-07-12 11:55:53 -04:00
|
|
|
systemd::start_systemd_target()
|
|
|
|
|
.await
|
|
|
|
|
.wrap_err("failed to start systemd target")?;
|
2022-07-15 11:05:23 -04:00
|
|
|
// Always stop the target when the process exits or panics.
|
|
|
|
|
scopeguard::defer! {
|
2022-07-21 14:26:32 -04:00
|
|
|
if let Err(error) = systemd::stop_systemd_target() {
|
|
|
|
|
error!("failed to stop systemd target: {:?}", error);
|
2022-07-15 11:05:23 -04:00
|
|
|
}
|
|
|
|
|
}
|
2022-07-11 10:47:00 -04:00
|
|
|
let env_vars = env_rx
|
|
|
|
|
.await
|
|
|
|
|
.expect("failed to receive environmental variables")
|
|
|
|
|
.into_iter()
|
|
|
|
|
.collect::<Vec<_>>();
|
2022-06-28 13:40:16 -04:00
|
|
|
info!("got environmental variables: {:?}", env_vars);
|
2022-06-24 11:43:48 -04:00
|
|
|
|
2022-06-30 10:35:02 -04:00
|
|
|
let mut sockets = Vec::with_capacity(2);
|
|
|
|
|
|
2022-10-23 17:41:28 -04:00
|
|
|
let (env, _) = comp::create_privileged_socket(&mut sockets, &env_vars)
|
2022-07-20 08:05:01 -07:00
|
|
|
.wrap_err("failed to create panel socket")?;
|
2022-09-23 14:52:14 -04:00
|
|
|
process_manager
|
|
|
|
|
.start(Process::new().with_executable("cosmic-panel").with_env(env))
|
|
|
|
|
.await
|
|
|
|
|
.expect("failed to start panel");
|
2022-10-23 17:41:28 -04:00
|
|
|
let (env, _) = comp::create_privileged_socket(&mut sockets, &env_vars)
|
2022-08-23 18:35:29 -07:00
|
|
|
.wrap_err("failed to create applet host")?;
|
2022-09-23 14:52:14 -04:00
|
|
|
process_manager
|
|
|
|
|
.start(
|
|
|
|
|
Process::new()
|
|
|
|
|
.with_executable("cosmic-applet-host")
|
|
|
|
|
.with_env(env),
|
|
|
|
|
)
|
|
|
|
|
.await
|
|
|
|
|
.expect("failed to start applet host");
|
2022-10-23 17:41:28 -04:00
|
|
|
let (env, _) = comp::create_privileged_socket(&mut sockets, &env_vars)
|
2022-08-23 18:35:29 -07:00
|
|
|
.wrap_err("failed to create applet host")?;
|
2022-09-23 14:52:14 -04:00
|
|
|
process_manager
|
|
|
|
|
.start(
|
|
|
|
|
Process::new()
|
2022-10-23 17:41:28 -04:00
|
|
|
.with_executable("cosmic-bg")
|
2022-09-23 14:52:14 -04:00
|
|
|
.with_env(env),
|
|
|
|
|
)
|
|
|
|
|
.await
|
2022-10-23 17:41:28 -04:00
|
|
|
.expect("failed to start cosmic-bg");
|
2022-08-23 18:35:29 -07:00
|
|
|
socket_tx.send(sockets).unwrap();
|
2022-09-23 14:52:14 -04:00
|
|
|
process_manager
|
|
|
|
|
.start(Process::new().with_executable("cosmic-settings-daemon"))
|
|
|
|
|
.await
|
|
|
|
|
.expect("failed to start settings daemon");
|
2022-06-30 10:35:02 -04:00
|
|
|
|
2022-07-22 11:48:03 -04:00
|
|
|
let (exit_tx, exit_rx) = oneshot::channel();
|
|
|
|
|
let _ = ConnectionBuilder::session()?
|
|
|
|
|
.name("com.system76.CosmicSession")?
|
|
|
|
|
.serve_at("/com/system76/CosmicSession", service::SessionService {
|
|
|
|
|
exit_tx: Some(exit_tx),
|
|
|
|
|
})?
|
|
|
|
|
.build()
|
|
|
|
|
.await?;
|
|
|
|
|
|
2022-06-24 11:43:48 -04:00
|
|
|
let mut signals = Signals::new(vec![libc::SIGTERM, libc::SIGINT]).unwrap();
|
2022-07-12 15:57:10 -04:00
|
|
|
loop {
|
|
|
|
|
tokio::select! {
|
|
|
|
|
_ = compositor_handle => {
|
|
|
|
|
info!("compositor exited");
|
2022-06-24 11:43:48 -04:00
|
|
|
break;
|
2022-07-12 15:57:10 -04:00
|
|
|
},
|
2022-07-22 11:48:03 -04:00
|
|
|
_ = exit_rx => {
|
|
|
|
|
info!("session exited by request");
|
|
|
|
|
break;
|
|
|
|
|
},
|
2022-07-12 15:57:10 -04:00
|
|
|
signal = signals.next() => match signal {
|
|
|
|
|
Some(libc::SIGTERM | libc::SIGINT) => {
|
|
|
|
|
info!("received request to terminate");
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
Some(signal) => unreachable!("received unhandled signal {}", signal),
|
|
|
|
|
None => break,
|
2022-06-24 11:43:48 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-07-12 15:57:10 -04:00
|
|
|
token.cancel();
|
|
|
|
|
tokio::time::sleep(std::time::Duration::from_secs(2)).await;
|
2022-06-22 12:22:30 -04:00
|
|
|
Ok(())
|
|
|
|
|
}
|