Skip to content

Commit

Permalink
Don't forget to push the arguments to scripts being interpreted.
Browse files Browse the repository at this point in the history
Includes an integration test that passes on Linux as well.
  • Loading branch information
anholt committed Sep 29, 2024
1 parent 831c8f8 commit d4a83de
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
12 changes: 11 additions & 1 deletion kernel/process/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -563,21 +563,31 @@ fn setup_userspace(

fn do_script_binfmt(
executable_path: &Arc<PathComponent>,
_argv: &[&[u8]], // TODO: Ignoring this seems wrong
script_argv: &[&[u8]],
envp: &[&[u8]],
root_fs: &Arc<SpinLock<RootFs>>,
buf: &[u8],
) -> Result<UserspaceEntry> {
// Set up argv[] with the interpreter and its arguments from the shebang line.
let mut argv: Vec<&[u8]> = buf[2..buf.iter().position(|&ch| ch == b'\n').unwrap()]
.split(|&ch| ch == b' ')
.collect();
if argv.is_empty() {
return Err(Errno::EINVAL.into());
}

// Push the path to the script file as the first argument to the
// interpreter.
let executable_pathbuf = executable_path.resolve_absolute_path();
argv.push(executable_pathbuf.as_str().as_bytes());

// Push the original arguments to the script on after the new script
// invocation (leaving out argv[0] of the previous path of invoking the
// script.)
for arg in script_argv.iter().skip(1) {
argv.push(arg);
}

let shebang_path = root_fs.lock().lookup_path(
Path::new(core::str::from_utf8(argv[0]).map_err(|_| Error::new(Errno::EINVAL))?),
true,
Expand Down
6 changes: 6 additions & 0 deletions testing/integration_tests/echo_args.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#!/bin/sh

for arg in "$@"; do
echo $arg
done

22 changes: 22 additions & 0 deletions testing/integration_tests/script_args.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/sh

if [ -z "$TESTS_DIR" ]; then
TESTS_DIR="."
fi

result=`${TESTS_DIR}/echo_args.sh 1 2 "3 4"`

expected=$(cat <<EOF
1
2
3 4
EOF
)

if [ "$result" != "$expected" ]; then
echo "Failure:"
echo $result
exit 1
fi

echo Pass

0 comments on commit d4a83de

Please sign in to comment.