From e24999ab1255e75804d45120e2aa35c8b9de58df Mon Sep 17 00:00:00 2001
From: Guido Trotter <ultrotter@google.com>
Date: Thu, 6 Aug 2009 00:02:17 +0100
Subject: [PATCH] example ethers hook: use lockfile-progs

Rather than writing our own locking routing, use the one implemented by
the lockfile-create program.

Signed-off-by: Guido Trotter <ultrotter@google.com>
Reviewed-by: Iustin Pop <iustin@google.com>
---
 doc/examples/hooks/ethers | 22 ++++++++--------------
 1 file changed, 8 insertions(+), 14 deletions(-)

diff --git a/doc/examples/hooks/ethers b/doc/examples/hooks/ethers
index 469af6b25..8725f0377 100755
--- a/doc/examples/hooks/ethers
+++ b/doc/examples/hooks/ethers
@@ -35,9 +35,13 @@
 # bridge to /etc/ethers.
 TARGET_BRIDGE="br0"
 DAEMON_PID_FILE="/var/run/dnsmasq.pid"
+
+# In order to handle concurrent execution of this lock, we use the $LOCKFILE.
+# LOCKFILE_CREATE and LOCKFILE_REMOVE are the path names for the lockfile-progs
+# programs which we use as helpers.
 LOCKFILE="/var/lock/ganeti_ethers.lock"
-LOCKTIMEOUT=10
-LOCKSLEEP=2
+LOCKFILE_CREATE="/usr/bin/lockfile-create"
+LOCKFILE_REMOVE="/usr/bin/lockfile-remove"
 
 hooks_path=$GANETI_HOOKS_PATH
 [ -n "$hooks_path" ] || exit 1
@@ -46,18 +50,8 @@ instance=$GANETI_INSTANCE_NAME
 nic_count=$GANETI_INSTANCE_NIC_COUNT
 
 acquire_lockfile() {
-  NOW=$(date +%s)
-  TIMEOUT=$(($NOW + $LOCKTIMEOUT))
-  while ! ( set -o noclobber; echo "$$" > $LOCKFILE) 2> /dev/null ; do
-    NOW=$(date +%s)
-    if [ $NOW -ge $TIMEOUT ]; then
-      logger -s "Cannot acquire lockfile for ethers update, giving up"
-      exit 1
-    fi
-    logger -s "Cannot acquire lockfile for ethers update, waiting"
-    sleep $LOCKSLEEP
-  done
-  trap "rm -f $LOCKFILE" EXIT
+  $LOCKFILE_CREATE -l $LOCKFILE || exit 1
+  trap "$LOCKFILE_REMOVE -l $LOCKFILE" EXIT
 }
 
 update_ethers_from_new() {
-- 
GitLab