| 1 | #!/bin/bash |
| 2 | # |
| 3 | # Copyright (C) 2021 Masatake YAMATO <[email protected]> |
| 4 | # |
| 5 | # This file is part of util-linux. |
| 6 | # |
| 7 | # This file is free software; you can redistribute it and/or modify |
| 8 | # it under the terms of the GNU General Public License as published by |
| 9 | # the Free Software Foundation; either version 2 of the License, or |
| 10 | # (at your option) any later version. |
| 11 | # |
| 12 | # This file is distributed in the hope that it will be useful, |
| 13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | # GNU General Public License for more details. |
| 16 | # |
| 17 | |
| 18 | TS_TOPDIR="${0%/*}/../.." |
| 19 | TS_DESC="ownership and hierarchy" |
| 20 | |
| 21 | . "$TS_TOPDIR"/functions.sh |
| 22 | ts_init "$*" |
| 23 | |
| 24 | # ts_skip_nonroot |
| 25 | grep -q '#define HAVE_LINUX_NSFS_H' ${top_builddir}/config.h || ts_skip "no ioctl_ns support" |
| 26 | |
| 27 | [ -r /proc/self/ns/user ] || ts_skip "no USER namespace kernel support" |
| 28 | [ -r /proc/self/ns/pid ] || ts_skip "no PID namespace kernel support" |
| 29 | |
| 30 | ts_check_test_command "$TS_CMD_LSNS" |
| 31 | ts_check_test_command "$TS_CMD_UNSHARE" |
| 32 | ts_check_prog "stat" |
| 33 | ts_check_prog "mkfifo" |
| 34 | ts_check_prog "touch" |
| 35 | ts_check_prog "uniq" |
| 36 | |
| 37 | "$TS_CMD_LSNS" > /dev/null 2>&1 |
| 38 | if [ $? -eq 2 ]; then |
| 39 | ts_skip "ioctl not supported" |
| 40 | fi |
| 41 | |
| 42 | $TS_CMD_UNSHARE --user --pid --mount-proc --fork true &> /dev/null || ts_skip "no namespace support" |
| 43 | |
| 44 | ts_cd "$TS_OUTDIR" |
| 45 | |
| 46 | # The parent process receives namespaces ids via FIFO_DATA from bash |
| 47 | # run by unshare. The parent can wait till the bash process provides |
| 48 | # data. |
| 49 | FIFO_DATA=$TS_OUTDIR/FIFO_DATA-HIERARCHY |
| 50 | |
| 51 | # The bash process run by unshare waits with this fifo till the parent |
| 52 | # process exits. As a result, namespaecs referred by the bash |
| 53 | # process can be alive till the parent process exits. |
| 54 | FIFO_WAIT=$TS_OUTDIR/FIFO_WAIT-HIERARCHY |
| 55 | |
| 56 | function cleanup { |
| 57 | rm -f $FIFO_DATA |
| 58 | rm -f $FIFO_WAIT |
| 59 | } |
| 60 | |
| 61 | function init { |
| 62 | cleanup |
| 63 | mkfifo $FIFO_DATA |
| 64 | mkfifo $FIFO_WAIT |
| 65 | } |
| 66 | |
| 67 | init |
| 68 | ( |
| 69 | exec 4> $FIFO_WAIT |
| 70 | my_userns=$(stat -c %i -L /proc/self/ns/user) |
| 71 | my_pidns=$(stat -c %i -L /proc/self/ns/pid) |
| 72 | read child_userns < $FIFO_DATA |
| 73 | read child_pidns < $FIFO_DATA |
| 74 | |
| 75 | expected="$child_userns $my_userns $my_userns" |
| 76 | actual=$("$TS_CMD_LSNS" --type user --noheadings --output NS,PNS,ONS "$child_userns" | uniq) |
| 77 | test "$expected" = "$actual" |
| 78 | RESULT=$? |
| 79 | if [ $RESULT -ne 0 ]; then |
| 80 | echo |
| 81 | echo userns expected: "$expected" |
| 82 | echo userns actual: "$actual" |
| 83 | echo "$TS_CMD_LSNS": |
| 84 | LSNS_DEBUG=all "$TS_CMD_LSNS" |
| 85 | cleanup |
| 86 | exit $RESULT |
| 87 | fi |
| 88 | |
| 89 | expected="$child_pidns $my_pidns $child_userns" |
| 90 | actual=$("$TS_CMD_LSNS" -t pid -n -o NS,PNS,ONS "$child_pidns" | uniq) |
| 91 | test "$expected" = "$actual" |
| 92 | RESULT=$? |
| 93 | if [ $RESULT -ne 0 ]; then |
| 94 | echo |
| 95 | echo pidns expected: "$expected" |
| 96 | echo pidns actual: "$actual" |
| 97 | echo "$TS_CMD_LSNS": |
| 98 | LSNS_DEBUG=all "$TS_CMD_LSNS" |
| 99 | cleanup |
| 100 | fi |
| 101 | exit $RESULT |
| 102 | ) >> $TS_ERRLOG 2>&1 & |
| 103 | mainpid=$! |
| 104 | ( |
| 105 | exec 4< $FIFO_WAIT |
| 106 | $TS_CMD_UNSHARE --user --pid --mount-proc --fork bash -s > $FIFO_DATA <<'EOF' |
| 107 | stat -c %i -L /proc/self/ns/user |
| 108 | stat -c %i -L /proc/self/ns/pid |
| 109 | # Wait till FIFO_WAIT is closed. |
| 110 | read -u 4 |
| 111 | EOF |
| 112 | ) & |
| 113 | subpid=$! |
| 114 | |
| 115 | wait $subpid |
| 116 | wait $mainpid |
| 117 | |
| 118 | RESULT=$? |
| 119 | echo $RESULT >> $TS_OUTPUT |
| 120 | |
| 121 | cleanup |
| 122 | ts_finalize |