Files
ghostty-rs/crates/ghostty-apprt-gtk/src/post_fork.rs
T
ddidderr be6d706ba5 test(core): add headless end-to-end tests and fix bugs they found
Adds crates/ghostty-core/tests/e2e_*.rs: 81 tests that drive the real
core Surface, termio, pty and child processes through a fake runtime
(recording apprt actions, in-memory clipboard) with a headless renderer
(ghostty-renderer/src/headless.rs). They cover keys (legacy, DECCKM,
kitty, modifyOtherKeys), keybinding actions and sequences, mouse
selection/reporting/scrolling/links, clipboard incl. OSC 52 and paste
safety, resize, focus, titles/pwd/bell/notifications/progress, child
exit, and IME preedit.

Fixes for issues found by the tests and by the Codex audit
(docs/rewrite/notes/review-core-codex.md, all 9 items):
- replacing a selection with copy-on-select panicked (stale tracked
  handles); global new_window no longer inherits a parent; the child
  environment uses the runtime's env wholesale so GTK's removals stick;
  Flatpak host resources path; locale recovery (ensureLocale) in
  apprt-gtk before GTK init and for CLI actions; failed screen-file
  opens propagate errors; OSC 52 base64 rules, CSI/ESC binding length
  limits and GHOSTTY_LOG parsing match Zig.
- Also includes V2's termio cleanups (tmux viewer wiring in the stream
  handler, Pty slave semantics docs, shared cgroup wait loop).

apprt-gtk gains a debug input replay hook (GHOSTTY_DEBUG_INPUT_SCRIPT)
exercising the real GTK key path. With it, the real app was verified on
Wayland: typing and output, new tab, splits, select-all/copy/paste and
font size all work (docs/rewrite/notes/verification-V1.md). Not yet
exercised at GTK level: mouse, real IM-context IME, clipboard dialogs.

Test Plan:
- cargo test --workspace (2983 passed)
- cargo test -p ghostty-core --tests (81 e2e tests)
2026-10-12 03:58:19 +02:00

101 lines
3.5 KiB
Rust

//! Parent post-fork hook creating the transient scope (port of
//! `apprt/gtk/post_fork.zig`).
use ghostty_config::config::Config;
use ghostty_config::types_b::{GtkSingleInstance, LinuxCgroup};
use gtk::gio;
use gtk::prelude::*;
use crate::cgroup;
/// 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(());
}
if ghostty_os::cgroup::wait_for_scope(pid, &expected) {
log::debug!("transition to transient systemd scope {expected} complete");
} else {
// 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");
}
Ok(())
}