mirror of
https://github.com/zed-industries/zed.git
synced 2024-11-08 07:35:01 +03:00
shell: Load SHELL from passwd entry if launched as desktop app (#11758)
This fixes #8794 and other related problems. The problem, in short, is this: `$SHELL` might be outdated. This code ensures that we update `$SHELL` to what we can deem the newest version, if we're started as a desktop application. The background is that you can get the user's preferred shell in two ways: 1. Read the `SHELL` env variable 2. Read the `/etc/passwd` file and check which shell is set Most applications should and do prefer (1) over (2). Why is it preferred? Reading `SHELL` means that processes can inherit the variable from each other. And you can do something like `SHELL=/bin/cool-shell ./my-cool-app` But what happens if the application was launched from the desktop? Which SHELL env does it inherit then? It inherits the env from the process that launched it, which is Finder.app or launchd or GNOME or something else — these are all long-running processes that get their environment when the user logs in. They do *not* get a new environment unless restarted (either process restarted or computer restarted) That means the `SHELL` env variable they have might be outdated. That's a problem if you, for example, change your shell with `chsh` and then launch the app from the desktop. That change of the default shell is not reflected in the app if the app only reads from SHELL. Because that hasn’t been updated. Instead it should read from passwd file to get the newest value. Release Notes: - Fixed SHELL being outdated if Zed was launched via Finder or Raycast or other desktop launchers. ([#8794](https://github.com/zed-industries/zed/issues/8794))
This commit is contained in:
parent
15e1895159
commit
43da37b0ab
@ -306,6 +306,10 @@ fn main() {
|
||||
Task::ready(())
|
||||
} else {
|
||||
app.background_executor().spawn(async {
|
||||
#[cfg(unix)]
|
||||
{
|
||||
load_shell_from_passwd().await.log_err();
|
||||
}
|
||||
load_login_shell_environment().await.log_err();
|
||||
})
|
||||
};
|
||||
@ -678,6 +682,56 @@ fn init_stdout_logger() {
|
||||
})
|
||||
.init();
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
async fn load_shell_from_passwd() -> Result<()> {
|
||||
let buflen = match unsafe { libc::sysconf(libc::_SC_GETPW_R_SIZE_MAX) } {
|
||||
n if n < 0 => 1024,
|
||||
n => n as usize,
|
||||
};
|
||||
let mut buffer = Vec::with_capacity(buflen);
|
||||
|
||||
let mut pwd: std::mem::MaybeUninit<libc::passwd> = std::mem::MaybeUninit::uninit();
|
||||
let mut result: *mut libc::passwd = std::ptr::null_mut();
|
||||
|
||||
let uid = unsafe { libc::getuid() };
|
||||
let status = unsafe {
|
||||
libc::getpwuid_r(
|
||||
uid,
|
||||
pwd.as_mut_ptr(),
|
||||
buffer.as_mut_ptr() as *mut libc::c_char,
|
||||
buflen,
|
||||
&mut result,
|
||||
)
|
||||
};
|
||||
let entry = unsafe { pwd.assume_init() };
|
||||
|
||||
anyhow::ensure!(
|
||||
status == 0,
|
||||
"call to getpwuid_r failed. uid: {}, status: {}",
|
||||
uid,
|
||||
status
|
||||
);
|
||||
anyhow::ensure!(!result.is_null(), "passwd entry for uid {} not found", uid);
|
||||
anyhow::ensure!(
|
||||
entry.pw_uid == uid,
|
||||
"passwd entry has different uid ({}) than getuid ({}) returned",
|
||||
entry.pw_uid,
|
||||
uid,
|
||||
);
|
||||
|
||||
let shell = unsafe { std::ffi::CStr::from_ptr(entry.pw_shell).to_str().unwrap() };
|
||||
if env::var("SHELL").map_or(true, |shell_env| shell_env != shell) {
|
||||
log::info!(
|
||||
"updating SHELL environment variable to value from passwd entry: {:?}",
|
||||
shell,
|
||||
);
|
||||
env::set_var("SHELL", shell);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn load_login_shell_environment() -> Result<()> {
|
||||
let marker = "ZED_LOGIN_SHELL_START";
|
||||
let shell = env::var("SHELL").context(
|
||||
|
Loading…
Reference in New Issue
Block a user