Files
ghostty-rs/crates/ghostty-apprt-gtk/src/post_fork.rs
T
ddidderr 88cc13871c feat(apprt-gtk): port the GTK4/libadwaita app runtime
Ports ZIG/src/apprt/gtk into ghostty-apprt-gtk: every GObject class
(application, window, tab, split tree, surface + scrolled window,
child-exited banner, search/key-state/resize overlays, command palette,
global shortcuts, all dialogs, config wrapper) as glib subclasses with
the same GType names, properties, signals and template children; GDK
key translation; X11 and Wayland window-protocol helpers (blur, CSD,
quick terminal via gtk4-layer-shell); single-instance D-Bus IPC;
gsettings; cgroup scope creation.

build.rs compiles Ghostty's blueprints with blueprint-compiler and
bundles UI, CSS and icons into a gresource, as Ghostty's build does.

The core is not ported yet, so the crate defines the boundary it needs
in src/core.rs (CoreApp, CoreSurface, RtApp, RtSurface, Waker); the
spec is in docs/rewrite/notes/ghostty-apprt-gtk.md. ghostty-core will
implement these traits (the crate no longer depends on core to avoid a
cycle). examples/demo.rs runs the app with a dummy core that clears the
GLArea; it launches on Wayland without GTK criticals.

Stubs/open items: toggle_visibility, float_window, secure_input and
close_all_windows return false; inspector is a no-op; X11/Wayland
protocol code is untested against real compositors; gtk4-rs doesn't
expose the GLArea framebuffer id, so the renderer must query it.

Also registers ghostty-apprt and ghostty-cli in workspace.dependencies.

Test Plan:
- cargo test -p ghostty-apprt-gtk (36 tests)
- DEMO_QUIT_AFTER_MS=3000 cargo run -p ghostty-apprt-gtk --example demo
2026-10-11 22:57:15 +02:00

113 lines
3.8 KiB
Rust

//! Parent post-fork hook creating the transient scope (port of
//! `apprt/gtk/post_fork.zig`).
use std::time::{Duration, Instant};
use ghostty_config::config::Config;
use ghostty_config::types_b::{GtkSingleInstance, LinuxCgroup};
use gtk::gio;
use gtk::prelude::*;
use crate::cgroup;
use crate::pre_exec::current_scope_matches;
/// Allowable error from a post-fork hook; fails surface creation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
#[error("post fork hook failed")]
pub struct PostForkError;
/// Config values needed by [`post_fork`].
#[derive(Debug, Clone, Copy)]
pub struct PostForkInfo {
pub gtk_single_instance: GtkSingleInstance,
pub linux_cgroup: LinuxCgroup,
pub linux_cgroup_hard_fail: bool,
pub linux_cgroup_memory_limit: Option<u64>,
pub linux_cgroup_processes_limit: Option<u64>,
}
impl PostForkInfo {
pub fn new(cfg: &Config) -> Self {
Self {
gtk_single_instance: cfg.gtk_single_instance,
linux_cgroup: cfg.linux_cgroup,
linux_cgroup_hard_fail: cfg.linux_cgroup_hard_fail,
linux_cgroup_memory_limit: cfg.linux_cgroup_memory_limit,
linux_cgroup_processes_limit: cfg.linux_cgroup_processes_limit,
}
}
}
/// Called in the parent after fork with the child's `pid`. Must run on the
/// GTK main thread (uses the default `gio::Application` D-Bus connection)
/// or at least where the application's connection is usable.
pub fn post_fork(info: &PostForkInfo, pid: Option<u32>) -> Result<(), PostForkError> {
match info.linux_cgroup {
LinuxCgroup::Always => {}
LinuxCgroup::Never => return Ok(()),
LinuxCgroup::SingleInstance => match info.gtk_single_instance {
GtkSingleInstance::True => {}
GtkSingleInstance::False => return Ok(()),
GtkSingleInstance::Detect => {
log::error!("gtk-single-instance is set to detect which should be impossible!");
return Err(PostForkError);
}
},
}
let Some(pid) = pid else {
log::error!("PID of child not known!");
return Err(PostForkError);
};
let expected = cgroup::fmt_scope(pid);
log::debug!("beginning transition to transient systemd scope {expected}");
let Some(dbus) = gio::Application::default().and_then(|a| a.dbus_connection()) else {
if info.linux_cgroup_hard_fail {
log::error!("dbus connection required for cgroup isolation, exiting");
return Err(PostForkError);
}
return Ok(());
};
if let Err(err) = cgroup::create_scope(
&dbus,
pid,
cgroup::Options {
memory_high: info.linux_cgroup_memory_limit,
tasks_max: info.linux_cgroup_processes_limit,
},
) {
if info.linux_cgroup_hard_fail {
log::error!("unable to create transient systemd scope {expected}: {err}");
return Err(PostForkError);
}
log::warn!("unable to create transient systemd scope {expected}: {err}");
return Ok(());
}
let start = Instant::now();
loop {
if start.elapsed() > Duration::from_millis(250) {
// Zig checks `rt_pre_exec_info.linux_cgroup_hard_fail` here;
// both infos come from the same config so they're identical.
if info.linux_cgroup_hard_fail {
log::error!("transition to new transient systemd scope {expected} took too long");
return Err(PostForkError);
}
log::warn!("transition to transient systemd scope {expected} took too long");
break;
}
if current_scope_matches(pid, &expected) {
log::debug!("transition to transient systemd scope {expected} complete");
break;
}
std::thread::sleep(Duration::from_millis(25));
}
Ok(())
}