casaroli opened a new pull request, #20599:
URL: https://github.com/apache/nuttx/pull/20599

   ## Summary
   
   `exec()` and `posix_spawn()` of a script failed with `ENOEXEC`: only ELF, 
NXFLAT and builtin programs could run. A shell runs a script without `#!` 
itself, but scripts for other interpreters, for example `#!/usr/bin/env 
python3`, and programs that run scripts directly could not work.
   
   This PR adds `CONFIG_BINFMT_SCRIPT` (default `n`). With it, 
`exec_internal()` reads the first line of the file. If the line starts with 
`#!`, it runs the interpreter named there with the argument list that Linux 
uses: the interpreter, the optional argument from the line, the path of the 
script, then the other arguments. A bare interpreter name is searched for in 
`PATH`, as `load_module()` does, and the interpreter can itself be a script, up 
to four levels.
   
   ## Impact
   
   None unless `CONFIG_BINFMT_SCRIPT` is enabled. With it, `exec()` of a `#!` 
file runs its interpreter instead of failing with `ENOEXEC`.
   
   ## Testing
   
   The run-time tests ran on a local branch that has this change and other 
pending changes on top of master; the builds are of this branch alone.
   
   qemu-armv8a, kernel build, QEMU 11 with HVF, `CONFIG_BINFMT_SCRIPT=y`, 
scripts started from an interactive shell:
   
   ```
   ./t.sh one two          -> sh script: 0=./t.sh args=one two
   ./t-e.sh  (#!/bin/sh -e) -> sh -e script: flags=ehB
   ./t.pl three            -> perl script: three
   ./t.py four (#!/usr/bin/env python3) -> python script: ['./t.py', 'four']
   ./t-nest five (#!/mnt/work/pt/interp.sh, itself a #! script) -> interp.sh 
got: ./t-nest five
   ./plain.sh (no #!)      -> no-shebang file run by ./plain.sh
   t.sh via-path (found in PATH) -> sh script: 0=/mnt/work/pt/t.sh args=via-path
   ```
   
   All exit with 0.
   
   Builds with `-Wno-cpp -Werror`: `qemu-armv8a:knsh`, `qemu-armv8a:knsh` with 
`CONFIG_BINFMT_SCRIPT=y`, `qemu-armv8a:nsh`. `tools/checkpatch.sh -c -u -m -g` 
passes.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to