summaryrefslogtreecommitdiffstats
path: root/tests/functions.sh
diff options
context:
space:
mode:
authorKarel Zak2017-12-07 14:41:08 +0100
committerKarel Zak2017-12-07 14:41:08 +0100
commit7acd4b463afc02e848eeec68716d72bd1522763c (patch)
treecf37fd800fd458b5155caf118ec62b4f8a2eb8d4 /tests/functions.sh
parenttests: add mdadm locks (diff)
downloadkernel-qcow2-util-linux-7acd4b463afc02e848eeec68716d72bd1522763c.tar.gz
kernel-qcow2-util-linux-7acd4b463afc02e848eeec68716d72bd1522763c.tar.xz
kernel-qcow2-util-linux-7acd4b463afc02e848eeec68716d72bd1522763c.zip
tests: support multiple locks
Signed-off-by: Karel Zak <kzak@redhat.com>
Diffstat (limited to 'tests/functions.sh')
-rw-r--r--tests/functions.sh53
1 files changed, 47 insertions, 6 deletions
diff --git a/tests/functions.sh b/tests/functions.sh
index 181fe7972..c32770b90 100644
--- a/tests/functions.sh
+++ b/tests/functions.sh
@@ -274,6 +274,13 @@ function ts_init_env {
TS_NOLOCKS=$(ts_has_option "nolocks" "$*")
TS_LOCKDIR="$top_builddir/tests/output"
+ if [ ! -d "/proc/self/fd" ]; then
+ TS_NOLOCKS="yes"
+ fi
+
+ # Don't lock if flock(1) is missing
+ type "flock" >/dev/null 2>&1 || TS_NOLOCKS="yes"
+
ts_init_core_env
TS_VERBOSE=$(ts_has_option "verbose" "$*")
@@ -686,24 +693,58 @@ function ts_fdisk_clean {
}
+function ts_get_lock_fd {
+ local proc=$1
+ local lockfile=$2
+
+ for fd in $(ls /proc/$proc/fd); do
+ file=$(readlink "/proc/$proc/fd/$fd")
+ if [ x"$file" = x"$lockfile" ]; then
+ echo "$fd"
+ return 0
+ fi
+ done
+ return 1
+}
+
function ts_lock {
local resource="$1"
local lockfile="${TS_LOCKDIR}/${resource}.lock"
+ local fd
if [ "$TS_NOLOCKS" == "yes" ]; then
return 0
fi
- # Don't lock if flock(1) is missing
- type "flock" >/dev/null 2>&1 || return 1
+ # Don't lock again
+ fd=$(ts_get_lock_fd $$ $lockfile)
+ if [ -n "$fd" ]; then
+ echo "${resource} already locked!"
+ return 0
+ fi
- eval "exec 200>$lockfile"
- flock --exclusive --timeout 30 200 || ts_skip "failed to lock $resource"
+ fd=$(( $(ls /proc/$$/fd/ | sort | tail -1) + 1))
+
+ eval "exec $fd>$lockfile"
+ flock --exclusive --timeout 30 $fd || ts_skip "failed to lock $resource"
+
+ echo "[$$] Locked $lockfile"
}
-# Note that flock(2) lock is released on FD close.
function ts_unlock {
- 200<&- || :
+ local resource="$1"
+ local lockfile="${TS_LOCKDIR}/${resource}.lock"
+ local fd
+
+ if [ "$TS_NOLOCKS" == "yes" ]; then
+ return 0
+ fi
+
+ fd=$(ts_get_lock_fd $$ $lockfile)
+ if [ -n "$fd" ]; then
+ echo "[$$] Unlocking $lockfile"
+ eval "exec $fd<&-"
+ fi
}
function ts_scsi_debug_init {