From: Steven Barth Date: Tue, 17 Feb 2015 13:41:04 +0000 (+0100) Subject: example: prevent script from running in parallel to avoid races X-Git-Tag: debian/1.1+git20160131-1~33 X-Git-Url: https://git.decadent.org.uk/gitweb/?a=commitdiff_plain;h=29b9032cbef27bc60a20c955d607537fedd554af;p=odhcp6c.git example: prevent script from running in parallel to avoid races --- diff --git a/odhcp6c-example-script.sh b/odhcp6c-example-script.sh index 6215257..a1fb0fc 100755 --- a/odhcp6c-example-script.sh +++ b/odhcp6c-example-script.sh @@ -6,7 +6,7 @@ update_resolv() { local dns="$2" ( - flock -n 9 + flock 9 grep -v "#odhcp6c:$device:" /etc/resolv.conf > /tmp/resolv.conf.tmp for c in $dns; do echo "nameserver $c #odhcp6c:$device:" >> /tmp/resolv.conf.tmp @@ -131,23 +131,25 @@ teardown_interface() { update_resolv "$device" "" } -case "$2" in - bound) - teardown_interface "$1" - setup_interface "$1" - ;; - informed|updated|rebound|ra-updated) - setup_interface "$1" - ;; - stopped|unbound) - teardown_interface "$1" - ;; - started) - teardown_interface "$1" - ;; -esac - -# user rules -[ -f /etc/odhcp6c.user ] && . /etc/odhcp6c.user - -exit 0 +( + flock 9 + case "$2" in + bound) + teardown_interface "$1" + setup_interface "$1" + ;; + informed|updated|rebound|ra-updated) + setup_interface "$1" + ;; + stopped|unbound) + teardown_interface "$1" + ;; + started) + teardown_interface "$1" + ;; + esac + + # user rules + [ -f /etc/odhcp6c.user ] && . /etc/odhcp6c.user +) 9>/tmp/odhcp6c.lock.$1 +rm -f /tmp/odhcp6c.lock.$1