2 # Verify that internal failure in chroot gives exact status.
4 # Copyright (C) 2009-2024 Free Software Foundation, Inc.
6 # This program is free software: you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation, either version 3 of the License, or
9 # (at your option) any later version.
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 # GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License
17 # along with this program. If not, see <https://www.gnu.org/licenses/>.
20 .
"${srcdir=.}/tests/init.sh"; path_prepend_ .
/src
23 # These tests verify exact status of internal failure; since none of
24 # them actually run a command, we don't need root privileges
25 returns_
125 chroot || fail
=1 # missing argument
26 returns_
125 chroot
--- / true || fail
=1 # unknown option
28 # chroot("/") succeeds for non-root users on some systems, but not all.
29 if chroot
/ true
; then
31 returns_
2 chroot
/ sh
-c 'exit 2' || fail
=1 # exit status propagation
32 returns_
126 chroot
/ . || fail
=1# invalid command
33 returns_
127 chroot
/ no_such || fail
=1 # no such command
35 test $?
= 125 || fail
=1
39 # Ensure that --skip-chdir fails with a non-"/" argument.
40 cat <<\EOF
> exp || framework_failure_
41 chroot
: option
--skip-chdir only permitted
if NEWROOT is old
'/'
42 Try
'chroot --help' for more information.
44 chroot
--skip-chdir . env
pwd >out
2>err
&& fail
=1
45 compare
/dev
/null out || fail
=1
46 compare exp err || fail
=1
48 # Ensure we chdir("/") appropriately when NEWROOT is old "/".
49 if test $can_chroot_root = 1; then
50 ln -s / isroot || framework_failure_
51 for dir
in '/' '/.' '/../' isroot
; do
52 # Verify that chroot(1) succeeds and performs chdir("/")
53 # (chroot(1) of coreutils-8.23 failed to run the latter).
54 curdir
=$
(chroot
"$dir" env
pwd) || fail
=1
55 test "$curdir" = '/' || fail
=1
57 # Test the "--skip-chdir" option.
58 curdir
=$
(chroot
--skip-chdir "$dir" env
pwd) || fail
=1
59 test "$curdir" = '/' && fail
=1