| 1 | #!/bin/bashscip-shell filesystem . . `/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 | grepscip-shell system . . grep. -q '#define HAVE_LINUX_NSFS_H' ${top_builddir}/config.h || ts_skip "no ioctl_ns support" |
| 26 | |
| 27 | [scip-shell system . . `[`. -r /proc/self/ns/userscip-shell filesystem . . `/proc/self/ns/user`. ] || ts_skip "no USER namespace kernel support" |
| 28 | [scip-shell system . . `[`. -r /proc/self/ns/pidscip-shell filesystem . . `/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/nullscip-shell filesystem . . `/dev/null`. 2>&1 |
| 38 | if [scip-shell system . . `[`. $? -eq 2 ]; then |
| 39 | ts_skip "ioctl not supported" |
| 40 | fi |
| 41 | |
| 42 | $TS_CMD_UNSHARE --user --pid --mount-proc --fork true &> /dev/nullscip-shell filesystem . . `/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 cleanupscip-shell shell util-linux 2.42.2-1 cleanup(). { |
| 57 | rmscip-shell system . . rm. -f $FIFO_DATA |
| 58 | rmscip-shell system . . rm. -f $FIFO_WAIT |
| 59 | } |
| 60 | |
| 61 | function initscip-shell shell util-linux 2.42.2-1 init(). { |
| 62 | cleanupscip-shell shell util-linux 2.42.2-1 cleanup(). |
| 63 | mkfifoscip-shell system . . mkfifo. $FIFO_DATA |
| 64 | mkfifoscip-shell system . . mkfifo. $FIFO_WAIT |
| 65 | } |
| 66 | |
| 67 | initscip-shell shell util-linux 2.42.2-1 init(). |
| 68 | ( |
| 69 | exec 4> $FIFO_WAIT |
| 70 | my_userns=$(statscip-shell system . . stat. -c %i -L /proc/self/ns/userscip-shell filesystem . . `/proc/self/ns/user`.) |
| 71 | my_pidns=$(statscip-shell system . . stat. -c %i -L /proc/self/ns/pidscip-shell filesystem . . `/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" | uniqscip-shell system . . uniq.) |
| 77 | testscip-shell system . . test. "$expected" = "$actual" |
| 78 | RESULT=$? |
| 79 | if [scip-shell system . . `[`. $RESULT -ne 0 ]; then |
| 80 | echoscip-shell system . . echo. |
| 81 | echoscip-shell system . . echo. userns expected: "$expected" |
| 82 | echoscip-shell system . . echo. userns actual: "$actual" |
| 83 | echoscip-shell system . . echo. "$TS_CMD_LSNS": |
| 84 | LSNS_DEBUG=all "$TS_CMD_LSNS" |
| 85 | cleanupscip-shell shell util-linux 2.42.2-1 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" | uniqscip-shell system . . uniq.) |
| 91 | testscip-shell system . . test. "$expected" = "$actual" |
| 92 | RESULT=$? |
| 93 | if [scip-shell system . . `[`. $RESULT -ne 0 ]; then |
| 94 | echoscip-shell system . . echo. |
| 95 | echoscip-shell system . . echo. pidns expected: "$expected" |
| 96 | echoscip-shell system . . echo. pidns actual: "$actual" |
| 97 | echoscip-shell system . . echo. "$TS_CMD_LSNS": |
| 98 | LSNS_DEBUG=all "$TS_CMD_LSNS" |
| 99 | cleanupscip-shell shell util-linux 2.42.2-1 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 | echoscip-shell system . . echo. $RESULT >> $TS_OUTPUT |
| 120 | |
| 121 | cleanupscip-shell shell util-linux 2.42.2-1 cleanup(). |
| 122 | ts_finalize |