From 95b852177325aa80b7c397c350bd343f0d8a64a8 Mon Sep 17 00:00:00 2001 From: Vladmir Saveliev Date: Sun, 12 Jan 2014 11:21:28 +0800 Subject: [PATCH] LU-4030 tests: use free_fd() to allocate file descriptor free_fd() lists /proc/self/fd to find the smallest unused file descriptor sanity test_31n is changed to use free_fd() instead of using hardcoded 173 sanity test_236 is changed to use free_fd() instead of using "{FD}<>" which is not available on eariler bash Since test_31n now uses the function free_fd to find an unused file descriptor, it no longer depends on fd 173 being free. This change also removes that test on whether fd 173 is in use. This patch is back-ported from the following ones: Lustre-commit: 1f9235152b2f44c7bd64c5c021066f1984f341e6 Lustre-change: http://review.whamcloud.com/8181 and Lustre-commit: 73e816e57167eb92425b6cf29fc570e56c88f6bd Lustre-change: http://review.whamcloud.com/8622 Signed-off-by: Vladmir Saveliev Change-Id: I0c9c04787d45dfe6ba5ed01adb0a8ee265c6b3c5 Signed-off-by: Jian Yu Reviewed-on: http://review.whamcloud.com/8811 Tested-by: Jenkins Tested-by: Maloo Reviewed-by: Andreas Dilger Reviewed-by: Bob Glossman --- lustre/tests/sanity.sh | 25 +++++++++++++++---------- lustre/tests/test-framework.sh | 12 ++++++++++++ 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/lustre/tests/sanity.sh b/lustre/tests/sanity.sh index 5436029..421fc14 100644 --- a/lustre/tests/sanity.sh +++ b/lustre/tests/sanity.sh @@ -2115,18 +2115,20 @@ test_31m() { run_test 31m "link to file: the same, non-existing, dir===============" test_31n() { - [ -e /proc/self/fd/173 ] && echo "skipping, fd 173 is in use" && return touch $DIR/$tfile || error "cannot create '$DIR/$tfile'" nlink=$(stat --format=%h $DIR/$tfile) [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1" - exec 173<$DIR/$tfile - trap "exec 173<&-" EXIT - nlink=$(stat --dereference --format=%h /proc/self/fd/173) + local fd=$(free_fd) + local cmd="exec $fd<$DIR/$tfile" + eval $cmd + cmd="exec $fd<&-" + trap "eval $cmd" EXIT + nlink=$(stat --dereference --format=%h /proc/self/fd/$fd) [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1" rm $DIR/$tfile || error "cannot remove '$DIR/$tfile'" - nlink=$(stat --dereference --format=%h /proc/self/fd/173) + nlink=$(stat --dereference --format=%h /proc/self/fd/$fd) [ ${nlink:--1} -eq 0 ] || error "nlink is $nlink, expected 0" - exec 173<&- + eval $cmd } run_test 31n "check link count of unlinked file" @@ -11783,11 +11785,14 @@ test_236() { cp $ref1 $file1 || error "cp $ref1 $file1 failed: rc = $?" $SETSTRIPE -c 2 $file2 || error "cannot setstripe on '$file2': rc = $?" cp $ref2 $file2 || error "cp $ref2 $file2 failed: rc = $?" - exec {FD}<>$file2 + local fd=$(free_fd) + local cmd="exec $fd<>$file2" + eval $cmd rm $file2 - $LFS swap_layouts $file1 /proc/self/fd/${FD} || - error "cannot swap layouts of '$file1' and /proc/self/fd/${FD}" - exec {FD}>&- + $LFS swap_layouts $file1 /proc/self/fd/${fd} || + error "cannot swap layouts of '$file1' and /proc/self/fd/${fd}" + cmd="exec $fd>&-" + eval $cmd cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)" #cleanup diff --git a/lustre/tests/test-framework.sh b/lustre/tests/test-framework.sh index efa2263..24057df 100644 --- a/lustre/tests/test-framework.sh +++ b/lustre/tests/test-framework.sh @@ -6576,3 +6576,15 @@ test_mkdir() { fi return $rc } + +# find the smallest and not in use file descriptor +free_fd() +{ + local max_fd=$(ulimit -n) + local fd=3 + while [[ $fd -le $max_fd && -e /proc/self/fd/$fd ]]; do + ((++fd)) + done + [ $fd -lt $max_fd ] || error "finding free file descriptor failed" + echo $fd +} -- 1.8.3.1