1
Description: Use flock for a more robust lock file handling
2
Debian-Bug: http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=568565
3
Author: Michael Biebl <biebl@debian.org>
4
diff --git a/pm/HOWTO.hooks b/pm/HOWTO.hooks
5
index ad7d4a7..ce9e8c4 100644
8
@@ -99,7 +99,7 @@ the hook. This will make the following convenience functions available:
10
Wrapper around try_lock. Second parameter is the number of seconds
11
to wait for the lock before giving up. If no second parameter
12
- is passed, this function will wait forever.
13
+ is passed, this function will wait for 60 seconds.
15
Release a previously acquired lock. First parameter is the name of the
17
diff --git a/pm/functions.in b/pm/functions.in
18
index 8388c65..b07bf44 100644
21
@@ -22,13 +22,13 @@ is_set "${PM_DEBUG}" && set -x
24
# $1 = file to use as lockfile
25
- # $2 (optional) content to write to the lockfile,
26
- # extra newline will be appended
27
+ local lock="${LOCKDIR}/${1##*/}"
29
# make sure the directory where the lockfile should be exists
31
- local lock="${LOCKDIR}/${1##*/}"
32
- # we use noclobber to make sure there are no race conditions
33
- (set -o noclobber; echo "${2}" > "${lock}") 2> /dev/null || return 1
36
+ flock -x -n 3 || return 1
40
@@ -37,13 +37,15 @@ try_lock()
44
- # $2 = optional timeout
46
- while ! try_lock $1; do
47
- [ "x$2" != "x" ] && [ $(( $elapsed == $2 )) -ne 0 ] && return 1
48
- elapsed=$(($elapsed + 1))
51
+ # $2 = optional timeout (default is 60 secs)
52
+ local lock="${LOCKDIR}/${1##*/}"
53
+ local timeout="${2:-60}"
55
+ mkdir -p "${LOCKDIR}"
58
+ flock -x -w "${timeout}" 3 || return 1