summaryrefslogtreecommitdiffstats
path: root/tests/ts/lsns/netnsid
blob: 72c14de6c4dcc54b212d44f433e07c0febf65248 (plain) (blame)
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
#!/bin/bash
#
# Copyright (C) 2017 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="NETNSID compare to ip-link"

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

ts_check_test_command "$TS_CMD_LSNS"
ts_check_prog "ip"
ts_check_prog "dd"
ts_check_prog "mkfifo"
ts_check_prog "sed"
ts_skip_nonroot

grep -q '#define HAVE_LINUX_NET_NAMESPACE_H' ${top_builddir}/config.h || ts_skip "no netns support"
ts_cd "$TS_OUTDIR"

vetha=lsns-vetha
vethb=lsns-vethb
NS=LSNS-TEST-NETNSID-NS
FIFO=$TS_OUTDIR/FIFO-NETNSID
NULL=/dev/null

function cleanup {
	ip link delete $vetha 2> $NULL || :
	ip netns delete $NS 2> $NULL || :
	rm -f $FIFO
}

cleanup
mkfifo $FIFO

if ip netns add $NS &&
	ip link add name $vetha type veth peer name $vethb &&
	ip link set $vethb netns $NS; then
    ip netns exec $NS dd if=$FIFO bs=1 count=2 of=$NULL 2> $NULL &
    PID=$!
else
    cleanup
    ts_skip "failed to initialize"
fi
{
    dd if=/dev/zero bs=1 count=1 2> $NULL
    {
	ip -o link show dev $vetha > $NULL
	IP_ID=$(ip -o link show dev $vetha | sed -ne 's/.* *link-netnsid *\([0-9]*\)/\1/p')
	LSNS_ID=$($TS_CMD_LSNS -n -o NETNSID --type net --task $PID | { read VAL; echo $VAL; } )
    }
    dd if=/dev/zero bs=1 count=1 2> $NULL
} > $FIFO

test "$IP_ID" = "$LSNS_ID"
echo $? >> $TS_OUTPUT

cleanup
ts_finalize