mirror of
git://git.sv.gnu.org/coreutils.git
synced 2026-04-21 03:12:48 +02:00
chroot: don't chdir() if not changing root
This allows chroot to be used as a light weight tool to change user identification for a command, while not changing the current working directory. It also makes `chroot / true` consistently succeed on all platforms for non root users. * src/chroot.c (main): If the same root is specified. i.e. '/' then don't change the current working directory, and avoid the overhead of the other redundant calls. * tests/misc/chroot-fail.sh: Remove failure guard previously needed on some systems. Also add an explicit case to ensure we don't change directory. * NEWS: Mention the change in behavior.
This commit is contained in:
@@ -28,16 +28,18 @@ test $? = 125 || fail=1
|
||||
chroot --- / true # unknown option
|
||||
test $? = 125 || fail=1
|
||||
|
||||
# chroot("/") succeeds for non-root users on some systems, but not all.
|
||||
if chroot / true ; then
|
||||
chroot / sh -c 'exit 2' # exit status propagation
|
||||
test $? = 2 || fail=1
|
||||
chroot / . # invalid command
|
||||
test $? = 126 || fail=1
|
||||
chroot / no_such # no such command
|
||||
test $? = 127 || fail=1
|
||||
else
|
||||
test $? = 125 || fail=1
|
||||
fi
|
||||
# Note chroot("/") succeeds for non-root users on some systems, but not all,
|
||||
# however we avoid the chroot() with "/" to have common behvavior.
|
||||
chroot / sh -c 'exit 2' # exit status propagation
|
||||
test $? = 2 || fail=1
|
||||
chroot / . # invalid command
|
||||
test $? = 126 || fail=1
|
||||
chroot / no_such # no such command
|
||||
test $? = 127 || fail=1
|
||||
|
||||
# Ensure we don't chdir("/") when not changing root
|
||||
# to allow only changing user ids for a command.
|
||||
curdir=$(chroot / env pwd) || fail=1
|
||||
test "$curdir" = '/' && fail=1
|
||||
|
||||
Exit $fail
|
||||
|
||||
Reference in New Issue
Block a user