Skip to content

Commit c441641

Browse files
committed
tests/common.sh: fix nsenter flags
`nsenter` should not be called when `unshare` was called without `-r`. Close rootless-containers#235 Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
1 parent a63a968 commit c441641

File tree

1 file changed

+31
-4
lines changed

1 file changed

+31
-4
lines changed

tests/common.sh

+31-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,37 @@
11
#!/bin/bash
22

3+
function nsenter_flags {
4+
pid=$1
5+
flags="--target=${pid}"
6+
userns="$(readlink /proc/${pid}/ns/user)"
7+
mntns="$(readlink /proc/${pid}/ns/mnt)"
8+
netns="$(readlink /proc/${pid}/ns/net)"
9+
10+
self_userns="$(readlink /proc/self/ns/user)"
11+
self_mntns="$(readlink /proc/self/ns/mnt)"
12+
self_netns="$(readlink /proc/self/ns/net)"
13+
14+
if [ "${userns}" != "${self_userns}" ]; then
15+
flags="$flags --preserve-credentials -U"
16+
fi
17+
if [ "${mntns}" != "${self_mntns}" ]; then
18+
flags="$flags -m"
19+
fi
20+
if [ "${netns}" != "${self_netns}" ]; then
21+
flags="$flags -n"
22+
fi
23+
echo "${flags}"
24+
}
25+
326
function wait_for_network_namespace {
427
# Wait that the namespace is ready.
528
COUNTER=0
629
while [ $COUNTER -lt 40 ]; do
7-
if nsenter --preserve-credentials -U -n --target=$1 true; then
30+
flags=$(nsenter_flags $1)
31+
if $(echo $flags | grep -qvw -- -n); then
32+
flags="$flags -n"
33+
fi
34+
if nsenter ${flags} true >/dev/null 2>&1; then
835
break
936
else
1037
sleep 0.5
@@ -17,7 +44,7 @@ function wait_for_network_device {
1744
# Wait that the device appears.
1845
COUNTER=0
1946
while [ $COUNTER -lt 40 ]; do
20-
if nsenter --preserve-credentials -U -n --target=$1 ip addr show $2; then
47+
if nsenter $(nsenter_flags $1) ip addr show $2; then
2148
break
2249
else
2350
sleep 0.5
@@ -41,7 +68,7 @@ function wait_process_exits {
4168
function wait_for_ping_connectivity {
4269
COUNTER=0
4370
while [ $COUNTER -lt 40 ]; do
44-
if nsenter --preserve-credentials -U -n --target=$1 ping -c 1 -w 1 $2; then
71+
if nsenter $(nsenter_flags $1) ping -c 1 -w 1 $2; then
4572
break
4673
else
4774
sleep 0.5
@@ -53,7 +80,7 @@ function wait_for_ping_connectivity {
5380
function wait_for_connectivity {
5481
COUNTER=0
5582
while [ $COUNTER -lt 40 ]; do
56-
if echo "wait_for_connectivity" | nsenter --preserve-credentials -U -n --target=$1 ncat -v $2 $3; then
83+
if echo "wait_for_connectivity" | nsenter $(nsenter_flags $1) ncat -v $2 $3; then
5784
break
5885
else
5986
sleep 0.5

0 commit comments

Comments
 (0)