1#!/bin/sh
2# Verify that internal failure in chroot gives exact status.
3
4# Copyright (C) 2009-2023 Free Software Foundation, Inc.
5
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.
10
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.
15
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/>.
18
19
20. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
21print_ver_ chroot pwd
22
23# These tests verify exact status of internal failure; since none of
24# them actually run a command, we don't need root privileges
25returns_ 125 chroot || fail=1 # missing argument
26returns_ 125 chroot --- / true || fail=1 # unknown option
27
28# chroot("/") succeeds for non-root users on some systems, but not all.
29if chroot / true ; then
30  can_chroot_root=1
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
34else
35  test $? = 125 || fail=1
36  can_chroot_root=0
37fi
38
39# Ensure that --skip-chdir fails with a non-"/" argument.
40cat <<\EOF > exp || framework_failure_
41chroot: option --skip-chdir only permitted if NEWROOT is old '/'
42Try 'chroot --help' for more information.
43EOF
44chroot --skip-chdir . env pwd >out 2>err && fail=1
45compare /dev/null out || fail=1
46compare exp err || fail=1
47
48# Ensure we chdir("/") appropriately when NEWROOT is old "/".
49if 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
56
57    # Test the "--skip-chdir" option.
58    curdir=$(chroot --skip-chdir "$dir" env pwd) || fail=1
59    test "$curdir" = '/' && fail=1
60  done
61fi
62
63Exit $fail
64