forge
Explore
Log in

Don't you dare go Hollow

Add dinit support (#246)

Browse files at this commit

431f0704813721c80f58e7b196b79891bfb4d4bfUnverifiedmetentParent: 9cbbffc
* Add dinit support

- Add --notify-fd cli flag for ready notifications
- Set dinit activation environment when "dinit" feature flag is enabled

* Make systemd and dinit environment activation additive

* Use NOTIFY_FD env variable instead of --notify-fd cli flag for sending ready notifications

* Format with rustfmt
Cargo.tomlmodified+2 -0
@@ -107,6 +107,8 @@ systemd = ["dbus"]
107 xdp-gnome-screencast = ["dbus", "pipewire"]107 xdp-gnome-screencast = ["dbus", "pipewire"]
108 # Enables the Tracy profiler instrumentation.108 # Enables the Tracy profiler instrumentation.
109 profile-with-tracy = ["profiling/profile-with-tracy", "tracy-client/default"]109 profile-with-tracy = ["profiling/profile-with-tracy", "tracy-client/default"]
110+# Enables dinit integration (global environment)
111+dinit = []
110 112
111 [profile.release]113 [profile.release]
112 debug = "line-tables-only"114 debug = "line-tables-only"
src/main.rsmodified+30 -5
@@ -1,8 +1,10 @@
1 #[macro_use]1 #[macro_use]
2 extern crate tracing;2 extern crate tracing;
3 3
4+use std::fmt::Write as _;
4 use std::fs::{self, File};5 use std::fs::{self, File};
5 use std::io::{self, Write};6 use std::io::{self, Write};
7+use std::os::fd::FromRawFd;
6 use std::path::PathBuf;8 use std::path::PathBuf;
7 use std::process::Command;9 use std::process::Command;
8 use std::{env, mem};10 use std::{env, mem};
@@ -211,6 +213,11 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
211 warn!("error notifying systemd: {err:?}");213 warn!("error notifying systemd: {err:?}");
212 };214 };
213 215
216+ // Send ready notification to specified file descriptor
217+ if let Err(err) = notify_fd() {
218+ warn!("error notifying fd: {err:?}");
219+ }
220+
214 // Set up config file watcher.221 // Set up config file watcher.
215 let _watcher = if let Some(path) = path.clone() {222 let _watcher = if let Some(path) = path.clone() {
216 let (tx, rx) = calloop::channel::sync_channel(1);223 let (tx, rx) = calloop::channel::sync_channel(1);
@@ -258,16 +265,23 @@ fn import_environment() {
258 ]265 ]
259 .join(" ");266 .join(" ");
260 267
261- #[cfg(feature = "systemd")]268+ let mut init_system_import = String::new();
262- let systemctl = format!("systemctl --user import-environment {variables} && ");269+ if cfg!(feature = "systemd") {
263- #[cfg(not(feature = "systemd"))]270+ write!(
264- let systemctl = String::new();271+ init_system_import,
272+ "systemctl --user import-environment {variables};"
273+ )
274+ .unwrap();
275+ }
276+ if cfg!(feature = "dinit") {
277+ write!(init_system_import, "dinitctl setenv {variables};").unwrap();
278+ }
265 279
266 let rv = Command::new("/bin/sh")280 let rv = Command::new("/bin/sh")
267 .args([281 .args([
268 "-c",282 "-c",
269 &format!(283 &format!(
270- "{systemctl}\284+ "{init_system_import}\
271 hash dbus-update-activation-environment 2>/dev/null && \285 hash dbus-update-activation-environment 2>/dev/null && \
272 dbus-update-activation-environment {variables}"286 dbus-update-activation-environment {variables}"
273 ),287 ),
@@ -302,3 +316,14 @@ fn default_config_path() -> Option<PathBuf> {
302 path.push("config.kdl");316 path.push("config.kdl");
303 Some(path)317 Some(path)
304 }318 }
319+
320+fn notify_fd() -> anyhow::Result<()> {
321+ let fd = match env::var("NOTIFY_FD") {
322+ Ok(notify_fd) => notify_fd.parse()?,
323+ Err(env::VarError::NotPresent) => return Ok(()),
324+ Err(err) => return Err(err.into()),
325+ };
326+ let mut notif = unsafe { File::from_raw_fd(fd) };
327+ notif.write_all(b"READY=1\n")?;
328+ Ok(())
329+}