| 1 | #!/bin/bash |
| 2 | # |
| 3 | # Copyright (C) 2024 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 | # This test case is based on the issue (#1884) submitted by @hesch |
| 19 | TS_TOPDIR="${0%/*}/../.." |
| 20 | TS_DESC="list the namespace pointed by a file descriptor" |
| 21 | |
| 22 | . "$TS_TOPDIR"/functions.sh |
| 23 | ts_init "$*" |
| 24 | |
| 25 | ts_check_test_command "$TS_CMD_LSNS" |
| 26 | ts_check_test_command "$TS_CMD_LSFD" |
| 27 | ts_check_test_command "$TS_HELPER_MKFDS" |
| 28 | ts_check_test_command "$TS_HELPER_SYSINFO" |
| 29 | |
| 30 | ts_check_prog "ip" |
| 31 | |
| 32 | ts_skip_nonroot |
| 33 | |
| 34 | if [ "$($TS_HELPER_SYSINFO ns-gettype-ok)" == "0" ]; then |
| 35 | ts_skip "NS_GET_NSTYPE ioctl cmd not available" |
| 36 | fi |
| 37 | |
| 38 | if [ "$($TS_HELPER_SYSINFO ns-getuserns-ok)" == "0" ]; then |
| 39 | ts_skip "NS_GET_USERNS ioctl cmd not available" |
| 40 | fi |
| 41 | |
| 42 | FD=4 |
| 43 | NS=LSNS-TEST-FILEDESC-NS |
| 44 | FILE=/run/netns/$NS |
| 45 | PID= |
| 46 | |
| 47 | cleanup() |
| 48 | { |
| 49 | ip netns delete $NS 2> /dev/null || : |
| 50 | } |
| 51 | |
| 52 | cleanup |
| 53 | |
| 54 | if ! ip netns add $NS; then |
| 55 | ts_skip "failed to make a namespace" |
| 56 | fi |
| 57 | trap "cleanup" EXIT |
| 58 | |
| 59 | { |
| 60 | coproc MKFDS { "$TS_HELPER_MKFDS" ro-regular-file $FD file=$FILE; } |
| 61 | if read -u ${MKFDS[0]} PID; then |
| 62 | # Make the namespace invisible from the file system tree. |
| 63 | cleanup |
| 64 | lsfd_expr="PID == ${PID} and FD == $FD" |
| 65 | lsfd_inode=$(${TS_CMD_LSFD} -n --raw -o INODE -Q "${lsfd_expr}") |
| 66 | lsns_expr="NS == $lsfd_inode" |
| 67 | lsns_output=$(${TS_CMD_LSNS} --noheadings --raw --output TYPE,NPROCS,USER -Q "${lsns_expr}") |
| 68 | if ! [ "${lsns_output}" == "net 0 root" ]; then |
| 69 | echo lsfd_inode: $lsfd_inode |
| 70 | echo lsns_output: $lsns_output |
| 71 | echo LSFD: |
| 72 | ${TS_CMD_LSFD} -Q "PID == $PID" |
| 73 | echo LSNS: |
| 74 | LSNS_DEBUG=all ${TS_CMD_LSNS} |
| 75 | echo lsns_status: $? |
| 76 | fi |
| 77 | echo DONE >&"${MKFDS[1]}" |
| 78 | fi |
| 79 | wait "${MKFDS_PID}" |
| 80 | } > $TS_OUTPUT 2>&1 |
| 81 | ts_finalize |