CPP-35902: Fix SHELL variable when running under WSL with ttyfix

Due to https://github.com/microsoft/WSL/issues/10718 `SHELL` var is set to `ttyfix` breaking `gdb`.

We now:
1. Check if first argument (command to execute) is a valid shell and set `SHELL` accordingly
2. If not, check user default shell. Be it a valid shell, we use it
3. Unset `SHELL` otherwise

Review:
https://jetbrains.team/p/ij/reviews/118965/timeline

GitOrigin-RevId: 425b6886b4b48e29f653413c62326afa8033c694
This commit is contained in:
Ilya.Kazakevich
2023-11-06 18:19:19 +01:00
committed by intellij-monorepo-bot
parent ccf881243d
commit 1ad4002475
3 changed files with 43 additions and 2 deletions

Binary file not shown.

Binary file not shown.

View File

@@ -2,10 +2,41 @@
#include <stdio.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <stdlib.h>
#include <string.h>
#include <pwd.h>
// Find appropriate shell
// command_to_execute is a first command provided to ttyfix
static char *detect_shell(char *command_to_execute) {
struct passwd *passwd_entry = getpwuid(geteuid());
char *user_shell = (passwd_entry != NULL) ? passwd_entry->pw_shell : NULL; //shell from /etc/passwd
int user_shell_ok = 0;
int command_ok = 0;
char *valid_shell;
while ((valid_shell = getusershell()) != NULL) { //List all valid shells from /etc/shells
if ((!user_shell_ok) && user_shell != NULL && (strcmp(user_shell, valid_shell) == 0)) {
user_shell_ok = 1; // User shell is ok, but we prefer to check command
}
if ((strcmp(valid_shell, command_to_execute) == 0)) { // First command is a valid shell, use it
command_ok = 1;
break; // Since command is the best choice, no need to check other shells
}
}
endusershell();
if (command_ok) {
return command_to_execute; // Command provided by user
} else if (user_shell_ok) {
return user_shell; // Use shell from /etc/passwd
}
return NULL;
}
// Workaround for https://github.com/microsoft/WSL/issues/10701
// Provide command to execute along with args. Command will see 100x100 terminal
int main(int argc, char *argv[], char *envp[]) {
int main(int argc, char *argv[]) {
if (argc < 2) {
fprintf(stderr, "No command provided");
return -1;
@@ -24,6 +55,16 @@ int main(int argc, char *argv[], char *envp[]) {
close(fd);
}
return execve(argv[1], argv + 1, envp); // Substitute self with provided command
// WSL sets SHELL to ttyfix: https://github.com/microsoft/WSL/issues/10718
char *shell_env = getenv("SHELL");
if (shell_env != NULL && (strcmp(argv[0], shell_env) == 0)) { // If SHELL == ttyfix
unsetenv("SHELL");
// Do our best to guess shell, or unset it otherwise
char *correct_shell = detect_shell(argv[1]);
if (correct_shell != NULL) {
setenv("SHELL", correct_shell, 1);
}
}
return execv(argv[1], argv + 1); // Substitute self with provided command
}