summaryrefslogtreecommitdiffstats
path: root/tests/ts/lsfd/mkfds-unix-in-netns
blob: 9ed456701c240397ef3ae3b5f8dade70045dfde7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/bin/bash
#
# Copyright (C) 2022 Masatake YAMATO <yamato@redhat.com>
#
# This file is part of util-linux.
#
# This file is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This file is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
TS_TOPDIR="${0%/*}/../.."
TS_DESC="UNIX sockets made in a differenct net namespace"

. "$TS_TOPDIR"/functions.sh
ts_init "$*"
ts_skip_nonroot

. "$TS_SELF/lsfd-functions.bash"

ts_check_test_command "$TS_CMD_LSFD"

ts_check_test_command "$TS_HELPER_MKFDS"

ts_cd "$TS_OUTDIR"

PID=
FDSELFNS=3
FDALTNS=4
FDSOCK=5

EXPR='((TYPE == "UNIX") or (TYPE == "UNIX-STREAM")) and (FD == 5)'

compare_net_namespaces()
{
    local type=$1
    local pid=$2
    local altns_inode
    local sock_netns

    altns_inode=$(${TS_CMD_LSFD} -n -o INODE -p "${pid}" -Q '(FD == 4)')
    sock_netns=$(${TS_CMD_LSFD} -n -o SOCK.NETNS -p "${pid}" -Q '(FD == 5)')

    if [[ "${altns_inode}" == "${sock_netns}" ]]; then
	echo "the netns for the $type socket is extracted as expectedly"
    else
	echo "the netns for the $type socket is not extracted well"
	echo "altns_inode=${altns_inode}"
	echo "sock_netns=${sock_netns}"
    fi
}

strip_type_stream()
{
    if [ "$1" = stream ]; then
	lsfd_strip_type_stream
    else
	cat
    fi
}

for t in stream dgram seqpacket; do
    ts_init_subtest "$t"
    {
	coproc MKFDS { "$TS_HELPER_MKFDS" unix-in-netns $FDSELFNS $FDALTNS $FDSOCK \
					  path=test_mkfds-unix-$t-ns \
					  type=$t ; }
	if read -r -u "${MKFDS[0]}" PID; then
	    ${TS_CMD_LSFD} -n \
			   -o ASSOC,STTYPE,NAME,SOCK.STATE,SOCK.TYPE,SOCK.LISTENING,UNIX.PATH \
			   -p "${PID}" -Q "${EXPR}" | strip_type_stream $t
	    echo 'ASSOC,STTYPE,NAME,SOCK.STATE,SOCK.TYPE,SOCK.LISTENING,UNIX.PATH': ${PIPESTATUS[0]}

	    compare_net_namespaces "$t" "${PID}"

	    echo DONE >&"${MKFDS[1]}"
	fi
	wait "${MKFDS_PID}"

	coproc MKFDS { "$TS_HELPER_MKFDS" unix-in-netns $FDSELFNS $FDALTNS $FDSOCK \
					  path=test_mkfds-unix-$t-ns \
					  abstract=true \
					  type=$t ; }
	if read -r -u "${MKFDS[0]}" PID; then
	    ${TS_CMD_LSFD} -n \
			   -o ASSOC,STTYPE,NAME,SOCK.STATE,SOCK.TYPE,SOCK.LISTENING,UNIX.PATH \
			   -p "${PID}" -Q "${EXPR}" | strip_type_stream $t
	    echo 'ASSOC,STTYPE,NAME,SOCK.STATE,SOCK.TYPE,SOCK.LISTENING,UNIX.PATH': ${PIPESTATUS[0]}

	    compare_net_namespaces "abstract $t" "${PID}"

	    echo DONE >&"${MKFDS[1]}"
	fi
	wait "${MKFDS_PID}"
    } > "$TS_OUTPUT" 2>&1
    if [ "$?" == "$EPERM" ]; then
	ts_skip_subtest "unshare(2) is not permitted on this platform"
	continue
    fi
    ts_finalize_subtest
done


ts_finalize