Can't use subprocess.Popen() after os.chroot() - why?

Alain Ketterlin alain at dpt-info.u-strasbg.fr
Sun Sep 4 11:25:48 EDT 2011


Erik <erik.williamson at gmail.com> writes:

> import os
> from subprocess import Popen, PIPE
>
> os.chroot("/tmp/my_chroot")
> p = Popen("/bin/date", stdin=PIPE, stdout=PIPE, stderr=PIPE)
> stdout_val, stderr_val = p.communicate()
> print stdout_val
>
> but the Popen call is dying with the following exception:
>
> Traceback (most recent call last):
>   File "./test.py", line 7, in <module>
>     p = Popen("/bin/date", stdin=PIPE, stdout=PIPE, stderr=PIPE)
>   File "/home/erik/lib/python2.7/subprocess.py", line 679, in __init__
>   File "/home/erik/lib/python2.7/subprocess.py", line 1224, in _execute_child
>   File "/home/erik/lib/python2.7/pickle.py", line 1382, in loads
>   File "/home/erik/lib/python2.7/pickle.py", line 858, in load
>   File "/home/erik/lib/python2.7/pickle.py", line 971, in load_string
> LookupError: unknown encoding: string-escape
>
> Am I missing something here? does the chroot environment need to be
> populated with more than just the date executable in this case?

Yes, because /bin/date is probably dynamically linked: you need the libs
as well. (Try first with a statically linked executable, e.g.,
/bin/busybox, and everything should be ok).

I agree the message is strange. For some reason, subprocess is calling
pickle.loads, which calls rep.decode("string-escape"), which probably
needs to access some file and fails because of the chroot().

I woud advise to use the chroot command instead of chrooting your python
process, if that's possible for you.

-- Alain.



More information about the Python-list mailing list