Add systemd feature flag for systemd-specific things

This commit is contained in:
Ivan Molodetskikh 2024-02-20 13:54:16 +04:00
parent 9f1b4ee299
commit d58a45a96c
4 changed files with 22 additions and 9 deletions

View File

@ -97,9 +97,11 @@ proptest-derive = "0.4.0"
xshell = "0.2.5" xshell = "0.2.5"
[features] [features]
default = ["dbus", "xdp-gnome-screencast"] default = ["dbus", "systemd", "xdp-gnome-screencast"]
# Enables DBus support (required for xdp-gnome and power button inhibiting). # Enables D-Bus support (serve various freedesktop and GNOME interfaces, power button handling).
dbus = ["zbus", "async-channel", "async-io", "notify-rust", "url"] dbus = ["zbus", "async-channel", "async-io", "notify-rust", "url"]
# Enables systemd integration (global environment, apps in transient scopes).
systemd = ["dbus"]
# Enables screencasting support through xdg-desktop-portal-gnome. # Enables screencasting support through xdg-desktop-portal-gnome.
xdp-gnome-screencast = ["dbus", "pipewire"] xdp-gnome-screencast = ["dbus", "pipewire"]
# Enables the Tracy profiler instrumentation. # Enables the Tracy profiler instrumentation.

View File

@ -38,7 +38,15 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
REMOVE_ENV_RUST_LIB_BACKTRACE.store(true, Ordering::Relaxed); REMOVE_ENV_RUST_LIB_BACKTRACE.store(true, Ordering::Relaxed);
} }
IS_SYSTEMD_SERVICE.store(env::var_os("NOTIFY_SOCKET").is_some(), Ordering::Relaxed); if env::var_os("NOTIFY_SOCKET").is_some() {
IS_SYSTEMD_SERVICE.store(true, Ordering::Relaxed);
#[cfg(not(feature = "systemd"))]
warn!(
"running as a systemd service, but systemd support is compiled out. \
Are you sure you did not forget to set `--features systemd`?"
);
}
let directives = env::var("RUST_LOG").unwrap_or_else(|_| "niri=debug".to_owned()); let directives = env::var("RUST_LOG").unwrap_or_else(|_| "niri=debug".to_owned());
let env_filter = EnvFilter::builder().parse_lossy(directives); let env_filter = EnvFilter::builder().parse_lossy(directives);
@ -249,11 +257,16 @@ fn import_environment() {
] ]
.join(" "); .join(" ");
#[cfg(feature = "systemd")]
let systemctl = format!("systemctl --user import-environment {variables} && ");
#[cfg(not(feature = "systemd"))]
let systemctl = String::new();
let rv = Command::new("/bin/sh") let rv = Command::new("/bin/sh")
.args([ .args([
"-c", "-c",
&format!( &format!(
"systemctl --user import-environment {variables} && \ "{systemctl}\
hash dbus-update-activation-environment 2>/dev/null && \ hash dbus-update-activation-environment 2>/dev/null && \
dbus-update-activation-environment {variables}" dbus-update-activation-environment {variables}"
), ),
@ -273,7 +286,7 @@ fn import_environment() {
} }
}, },
Err(err) => { Err(err) => {
warn!("error spawning shell to import environment into systemd: {err:?}"); warn!("error spawning shell to import environment: {err:?}");
} }
} }
} }

View File

@ -1063,8 +1063,6 @@ impl Niri {
pub fn inhibit_power_key(&mut self) -> anyhow::Result<()> { pub fn inhibit_power_key(&mut self) -> anyhow::Result<()> {
let conn = zbus::blocking::ConnectionBuilder::system()?.build()?; let conn = zbus::blocking::ConnectionBuilder::system()?.build()?;
// logind-zbus has a wrong signature for this method, so do it manually.
// https://gitlab.com/flukejones/logind-zbus/-/merge_requests/5
let message = conn.call_method( let message = conn.call_method(
Some("org.freedesktop.login1"), Some("org.freedesktop.login1"),
"/org/freedesktop/login1", "/org/freedesktop/login1",

View File

@ -229,7 +229,7 @@ fn spawn_sync(command: impl AsRef<OsStr>, args: impl IntoIterator<Item = impl As
trace!("spawned PID: {pid}"); trace!("spawned PID: {pid}");
// Start a systemd scope for the grandchild. // Start a systemd scope for the grandchild.
#[cfg(feature = "dbus")] #[cfg(feature = "systemd")]
if let Err(err) = start_systemd_scope(command, child.id(), pid as u32) { if let Err(err) = start_systemd_scope(command, child.id(), pid as u32) {
trace!("error starting systemd scope for spawned command: {err:?}"); trace!("error starting systemd scope for spawned command: {err:?}");
} }
@ -292,7 +292,7 @@ pub static IS_SYSTEMD_SERVICE: AtomicBool = AtomicBool::new(false);
/// ///
/// This separates the pid from the compositor scope, which for example prevents the OOM killer /// This separates the pid from the compositor scope, which for example prevents the OOM killer
/// from bringing down the compositor together with a misbehaving client. /// from bringing down the compositor together with a misbehaving client.
#[cfg(feature = "dbus")] #[cfg(feature = "systemd")]
fn start_systemd_scope(name: &OsStr, intermediate_pid: u32, child_pid: u32) -> anyhow::Result<()> { fn start_systemd_scope(name: &OsStr, intermediate_pid: u32, child_pid: u32) -> anyhow::Result<()> {
use std::fmt::Write as _; use std::fmt::Write as _;
use std::path::Path; use std::path::Path;