Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wsl.exe sets first argument as SHELL breaking posix compatibility and gdb #10718

Closed
1 of 2 tasks
throwable-one opened this issue Nov 6, 2023 · 4 comments
Closed
1 of 2 tasks
Assignees

Comments

@throwable-one
Copy link

Windows Version

Microsoft Windows [Version 10.0.19045.3570]

WSL Version

1.2.5.0

Are you using WSL 1 or WSL 2?

  • WSL 2
  • WSL 1

Kernel Version

Linux version 5.15.90.1-microsoft-standard-WSL2 (oe-user@oe-host) (x86_64-msft-linux-gcc (GCC) 9.3.0, GNU ld (GNU Binutils) 2.34.0.20200220) #1 SMP Fri Jan 27 02:56:13 UTC 2023

Distro Version

Ubuntu 20.04

Other Software

python3
gdb

Repro Steps

wsl.exe --distribution Ubuntu-20.04 --exec "python3" -c "import os; print(os.environ['SHELL'])"
python3

The output is python, but according to POSIX SHELL variable should point to the valid language interpreter.
https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html

SHELL
This variable shall represent a pathname of the user's preferred command language interpreter. If this interpreter does not conform to the Shell Command Language in XCU Shell Command Language, utilities may behave differently from those described in POSIX.1-2017.

That is a problem.
gdb uses SHELL var to execute commands:
https://ftp.gnu.org/old-gnu/Manuals/gdb/html_node/gdb_19.html#SEC20

Warning: On Unix systems, GDB runs your program using the shell indicated by your SHELL environment variable if it exists (or /bin/sh if not)

As result. gdb doesn't work:

c:\>wsl.exe --distribution Ubuntu-20.04 --exec "/bin/gdb"
GNU gdb (Ubuntu 9.2-0ubuntu1~20.04.1) 9.2
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word".
(gdb) file /bin/bash
Reading symbols from /bin/bash...
(No debugging symbols found in /bin/bash)
(gdb) run
Starting program: /usr/bin/bash
GNU gdb (Ubuntu 9.2-0ubuntu1~20.04.1) 9.2
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "x86_64-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word".
/mnt/c/exec /usr/bin/bash : No such file or directory.
(gdb)

Expected Behavior

SHELL shouldn't be set at all or point to valid shell

Actual Behavior

SHELL points to the first argument

Diagnostic Logs

No response

@throwable-one throwable-one changed the title wsl.exe sets first argument as SHELL breaking posix compatibility wsl.exe sets first argument as SHELL breaking posix compatibility and gdb Nov 6, 2023
intellij-monorepo-bot pushed a commit to JetBrains/intellij-community that referenced this issue Nov 7, 2023
Due to microsoft/WSL#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
@benhillis benhillis self-assigned this Nov 7, 2023
@zcobol
Copy link

zcobol commented Nov 8, 2023

@throwable-one why not use -- instead of --exec.

From wsl.exe help:

Arguments for running Linux binaries:

    If no command line is provided, wsl.exe launches the default shell.

    --exec, -e <CommandLine>
        Execute the specified command without using the default Linux shell.

    --shell-type <standard|login|none>
        Execute the specified command with the provided shell type.

    --
        Pass the remaining command line as-is.

You'll notice that the environment is not the same, depending on invocation method:

PS C:\Users\zcobol> wsl.exe --exec python3 -c "import os; print(os.environ['SHELL'])"
python3

PS C:\Users\zcobol> wsl.exe -- python3 -c "import os; print(os.environ['SHELL'])"
/bin/bash

Use print(os.environ) to see the all differences.

@benhillis
Copy link
Member

@asampal
Copy link

asampal commented Nov 10, 2023

It seems wsl is doing the right thing when using --exec, as @zcobol pointed out. Why was a fix needed?

@throwable-one
Copy link
Author

@zcobol But WSL would use shell then, not my bare command.

@asampal There is nothing in WSL documentation that says I can't use arbitrary command with --exec, hence --exec python is valid usage. But POSIX claims explicitly that SHELL variable should point to a shell.

@benhillis thank you for this fix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants