Visual Studio Code, rebuilt in Rust
Rust
fix(terminal): read the login shell with getpwuid_r
rokartur2780f87b69ae
diff --git a/native/crates/app/src/shell_env.rs b/native/crates/app/src/shell_env.rsindex e9bf270f0ef..cfc1ecd81d2 100644--- a/native/crates/app/src/shell_env.rs+++ b/native/crates/app/src/shell_env.rs@@ -58,6 +58,7 @@ fn do_resolve_unix_shell_env(.stdout(Stdio::piped()).stderr(Stdio::null());// Node's `detached: true`: a new session, so an interactive shell cannot take over or stop on our terminal.+ // SAFETY: the closure runs between fork and exec and only calls setsid, which is async-signal-safe.unsafe {cmd.pre_exec(|| {libc::setsid();@@ -138,15 +139,7 @@ fn system_shell(env: &HashMap<String, String>) -> String {/// `os.userInfo().shell`.fn passwd_shell() -> Option<String> {- // getpwuid's static buffer is read before any other passwd call can overwrite it.- unsafe {- let entry = libc::getpwuid(libc::getuid());- if entry.is_null() || (*entry).pw_shell.is_null() {- return None;- }- let shell = std::ffi::CStr::from_ptr((*entry).pw_shell).to_string_lossy().into_owned();- (!shell.is_empty()).then_some(shell)- }+ crate::terminal::profiles::login_shell().filter(|shell| !shell.is_empty())}/// The shell branches of `doResolveUnixShellEnv`. Upstream runs Electron as node to printdiff --git a/native/crates/app/src/terminal/profiles.rs b/native/crates/app/src/terminal/profiles.rsindex db84dd190a4..191f0cf991d 100644--- a/native/crates/app/src/terminal/profiles.rs+++ b/native/crates/app/src/terminal/profiles.rs@@ -130,12 +130,25 @@ pub fn system_shell(env: &HashMap<String, String>) -> String {}#[cfg(unix)]-fn login_shell() -> Option<String> {- let entry = unsafe { libc::getpwuid(libc::getuid()) };- if entry.is_null() || unsafe { (*entry).pw_shell.is_null() } {+pub(crate) fn login_shell() -> Option<String> {+ // SAFETY: passwd is integers and pointers, valid when zeroed.+ let mut entry: libc::passwd = unsafe { std::mem::zeroed() };+ let mut buffer = vec![0; 1024];+ let mut found = std::ptr::null_mut();+ // getpwuid_r, not getpwuid: getpwuid's static buffer races with a call on another thread.+ loop {+ // SAFETY: `entry` and `found` are locals the call fills; `buffer` is writable for the length given.+ let status = unsafe { libc::getpwuid_r(libc::getuid(), &mut entry, buffer.as_mut_ptr(), buffer.len(), &mut found) };+ if status != libc::ERANGE {+ break;+ }+ buffer.resize(buffer.len() * 2, 0);+ }+ if found.is_null() || entry.pw_shell.is_null() {return None;}- Some(unsafe { std::ffi::CStr::from_ptr((*entry).pw_shell) }.to_string_lossy().into_owned())+ // SAFETY: getpwuid_r found the entry, so `pw_shell` is a NUL-terminated string inside `buffer`, alive here.+ Some(unsafe { std::ffi::CStr::from_ptr(entry.pw_shell) }.to_string_lossy().into_owned())}// shortcut: no PowerShell detection yet (shell.ts `getSystemShellWindows`), so $SHELL or sh; port/ledger.md M24 row app/src/terminal/profiles.rs:133.@@ -149,6 +162,13 @@ mod tests {use super::*;use serde_json::json;+ // macOS only: a container's user may have no passwd entry.+ #[cfg(target_os = "macos")]+ #[test]+ fn the_login_shell_is_the_passwd_entry() {+ assert!(login_shell().is_some_and(|shell| shell.starts_with('/')));+ }+#[test]fn profiles_come_from_etc_shells_and_settings() {let dir = std::env::temp_dir().join(format!("code-gpui-profiles-{}", std::process::id()));