Preserve SSH connection settings when opening new windows

Forward the invoking shell's remote executable and agent through the Linux prefork launcher. Resolve mounted tabs by stable ID for the New Window shortcut and reconnect to nested destinations from the local host.
This commit is contained in:
James Magahern
2026-11-17 16:20:54 -08:00
parent 153838061e
commit cc2acb51e4
9 changed files with 408 additions and 125 deletions
+107 -64
View File
@@ -77,7 +77,7 @@ impl Zygote {
pub fn open_window(
&mut self,
remote: Option<String>,
remote: Option<protocol::RemoteWindow>,
session: protocol::SessionTarget,
) -> Result<()> {
let executable = executable_path()?;
@@ -90,11 +90,20 @@ impl Zygote {
// ready. If startup fails, a later launch can retry this stopped one.
*self = Self::start(&self.server_socket)?;
}
match self.request(&LaunchRequest::Launch {
server_socket: self.server_socket.clone(),
remote,
session,
})? {
let server_socket = self.server_socket.clone();
let request = match remote {
Some(remote) => LaunchRequest::LaunchRemote {
server_socket,
remote,
session,
},
None => LaunchRequest::Launch {
server_socket,
remote: None,
session,
},
};
match self.request(&request)? {
LaunchResponse::Launched { pid } => {
log::info!("Forked psyTerm GUI {pid} from zygote {}", self.child.id());
Ok(())
@@ -172,6 +181,12 @@ enum LaunchRequest {
session: protocol::SessionTarget,
},
Shutdown,
// Keep Launch readable when a running older daemon refreshes its helper.
LaunchRemote {
server_socket: PathBuf,
remote: protocol::RemoteWindow,
session: protocol::SessionTarget,
},
}
#[derive(Debug, Serialize, Deserialize)]
@@ -214,13 +229,16 @@ pub fn run_zygote(socket: &Path) -> Result<()> {
continue;
}
};
match request {
LaunchRequest::Ping => protocol::write_message(
&mut stream,
&LaunchResponse::Ready {
pid: std::process::id(),
},
)?,
let (server_socket, remote, session, ssh) = match request {
LaunchRequest::Ping => {
protocol::write_message(
&mut stream,
&LaunchResponse::Ready {
pid: std::process::id(),
},
)?;
continue;
}
LaunchRequest::Shutdown => {
protocol::write_message(&mut stream, &LaunchResponse::Stopped)?;
return Ok(());
@@ -229,58 +247,55 @@ pub fn run_zygote(socket: &Path) -> Result<()> {
server_socket,
remote,
session,
} => {
if let Err(error) = ensure_single_threaded() {
protocol::write_message(
&mut stream,
&LaunchResponse::Error(error.to_string()),
)?;
continue;
}
// SAFETY: this process is single-threaded (checked above) and
// has never initialized GPUI/Wayland/Vulkan or an executor. All
// Rust allocator/logger locks can only have belonged to this
// thread and are unlocked here. The child closes inherited IPC
// descriptors, resets SIGCHLD, and owns its fresh GUI runtime.
let pid = unsafe { libc::fork() };
if pid == -1 {
protocol::write_message(
&mut stream,
&LaunchResponse::Error(io::Error::last_os_error().to_string()),
)?;
continue;
}
if pid == 0 {
// SAFETY: this only labels our forked GUI process for tools.
unsafe {
libc::prctl(libc::PR_SET_NAME, c"psyterm-gui".as_ptr());
}
drop(stream);
drop(listener);
let result = set_child_reaping(false).and_then(|()| {
crate::run_gui(
server_socket,
remote,
session,
protocol::SshOptions::from_env(),
)
});
let code = if let Err(error) = result {
log::error!("GUI startup failed: {error:#}");
1
} else {
0
};
// SAFETY: this fork child must not run parent scope cleanup
// (particularly the shared listening socket's unlink guard).
unsafe { libc::_exit(code) };
}
protocol::write_message(
&mut stream,
&LaunchResponse::Launched { pid: pid as u32 },
)?;
}
} => (
server_socket,
remote,
session,
protocol::SshOptions::from_env(),
),
LaunchRequest::LaunchRemote {
server_socket,
remote,
session,
} => (server_socket, Some(remote.destination), session, remote.ssh),
};
if let Err(error) = ensure_single_threaded() {
protocol::write_message(&mut stream, &LaunchResponse::Error(error.to_string()))?;
continue;
}
// SAFETY: this process is single-threaded (checked above) and
// has never initialized GPUI/Wayland/Vulkan or an executor. All
// Rust allocator/logger locks can only have belonged to this
// thread and are unlocked here. The child closes inherited IPC
// descriptors, resets SIGCHLD, and owns its fresh GUI runtime.
let pid = unsafe { libc::fork() };
if pid == -1 {
protocol::write_message(
&mut stream,
&LaunchResponse::Error(io::Error::last_os_error().to_string()),
)?;
continue;
}
if pid == 0 {
// SAFETY: this only labels our forked GUI process for tools.
unsafe {
libc::prctl(libc::PR_SET_NAME, c"psyterm-gui".as_ptr());
}
drop(stream);
drop(listener);
let result = set_child_reaping(false)
.and_then(|()| crate::run_gui(server_socket, remote, session, ssh));
let code = if let Err(error) = result {
log::error!("GUI startup failed: {error:#}");
1
} else {
0
};
// SAFETY: this fork child must not run parent scope cleanup
// (particularly the shared listening socket's unlink guard).
unsafe { libc::_exit(code) };
}
protocol::write_message(&mut stream, &LaunchResponse::Launched { pid: pid as u32 })?;
}
}
@@ -391,6 +406,7 @@ mod tests {
let request = LaunchRequest::Launch {
server_socket: PathBuf::from("/private/psyterm.sock"),
remote: Some("work-host".into()),
session: protocol::SessionTarget::New { cwd: None },
};
let mut bytes = Vec::new();
protocol::write_message(&mut bytes, &request).unwrap();
@@ -398,6 +414,33 @@ mod tests {
assert!(
matches!(decoded, LaunchRequest::Launch { remote: Some(host), .. } if host == "work-host")
);
let request = LaunchRequest::LaunchRemote {
server_socket: PathBuf::from("/private/psyterm.sock"),
remote: protocol::RemoteWindow {
destination: "work-host".into(),
ssh: protocol::SshOptions {
executable: Some("/opt/remote apps/psyterm".into()),
agent_socket: Some(PathBuf::from("/private/agent.sock")),
},
},
session: protocol::SessionTarget::New { cwd: None },
};
let mut bytes = Vec::new();
protocol::write_message(&mut bytes, &request).unwrap();
let decoded: LaunchRequest = protocol::read_message(&mut bytes.as_slice()).unwrap();
let LaunchRequest::LaunchRemote { remote, .. } = decoded else {
panic!()
};
assert_eq!(remote.destination, "work-host");
assert_eq!(
remote.ssh.executable.as_deref(),
Some("/opt/remote apps/psyterm")
);
assert_eq!(
remote.ssh.agent_socket.as_deref(),
Some(Path::new("/private/agent.sock"))
);
}
#[test]