execve fails with wrong arguments

Viewed 48

I want to replace my program with an interactive Bash shell. Launching works in Golang (with os/exec.Command) and Python (os.system) but not when I want to replace the process using syscall.Exec() or os.execve().

Demo Python code:

import os

# first try with fork: OK
os.system("/bin/bash --rcfile env.sh -i")

# second try with execve: FAIL (exit after reading env.sh)
os.execve("/bin/bash", ["--rcfile", "env.sh", "-i"], os.environ)

Contents of env.sh:

export PS1='interactive shell $ '
echo inside env.sh
ls -la /proc/self/fd

Python output:

$ echo "Start: $$"; python test.py; echo "End: $$";
Start: 684875
inside env.sh
total 0
lrwx------ 1 willem willem 64 mrt 31 14:03 0 -> /dev/pts/1
lrwx------ 1 willem willem 64 mrt 31 14:03 1 -> /dev/pts/1
lrwx------ 1 willem willem 64 mrt 31 14:03 2 -> /dev/pts/1
lr-x------ 1 willem willem 64 mrt 31 14:03 3 -> /proc/737221/fd
interactive shell $ exit <--- Control-D from me
inside env.sh
total 0
lrwx------ 1 willem willem 64 mrt 31 14:03 0 -> /dev/pts/1
lrwx------ 1 willem willem 64 mrt 31 14:03 1 -> /dev/pts/1
lrwx------ 1 willem willem 64 mrt 31 14:03 2 -> /dev/pts/1
lr-x------ 1 willem willem 64 mrt 31 14:03 3 -> /proc/737223/fd
End: 684875

Any ideas how to debug why Bash would exit at the second run?

1 Answers

It's execve("/bin/bash", ["/bin/bash", "--rcfile", ...]).

You are running bash env.sh -i - executing script env.sh with one argument -i, and process name set to --rcfile.

Related