Advertisement
gusibsd

locking.sh

Mar 17th, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. #
  2. # Copyright (c) 2005 XenSource Ltd.
  3. # Copyright (c) 2007 Red Hat
  4. #
  5. # This library is free software; you can redistribute it and/or
  6. # modify it under the terms of version 2.1 of the GNU Lesser General Public
  7. # License as published by the Free Software Foundation.
  8. #
  9. # This library is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. # Lesser General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU Lesser General Public
  15. # License along with this library; if not, write to the Free Software
  16. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. #
  18.  
  19. LOCK_BASEDIR=/var/run/xen-hotplug
  20. OLD_IFS=$IFS
  21.  
  22. # Initialise the array of locks
  23.  
  24. if [ -z ${_lockdict_size} ]; then
  25. _lockdict_size=0
  26. fi
  27.  
  28. # As this has to work with the default FreeBSD shell (which is sh) we
  29. # need to simulate arrays, because sh does not provide them.
  30. # So instead of declaring an array, we use multiple variables named _lockdict$i
  31. # where $i would be the $i-th element in the array.
  32. # Dereferencing them is a bit trick, we need eval to convert \$_lockdict$i into
  33. # $_lockdict1
  34.  
  35. # A lock would behave like a reentrant mutex, a process may hold a lock
  36. # many times. It would need to release the lock the same amount of times to free
  37. # the lock. If the process exits, the lock would be automatically released by the
  38. # operating system.
  39.  
  40. # The array will start from 0 and $_lockdict_size would be the amount of locks
  41. # acquired. Note that if a lock is acquired two times, it will appear two times
  42. # in the array.
  43.  
  44. # Informal description
  45. # Precondition is: true
  46. # The Invariant would be: the number of appereances of the argument $1 (the name
  47. # of the lock file) would be the number of times the current process has acquired that lock
  48. # Postcondition: we reserve
  49.  
  50. _setlockfile(){
  51. local i
  52. IFS=''
  53. i=0
  54. while [ $i -lt ${_lockdict_size} ]; do
  55. [ -z "\$_lockdict$i" -o "\$_lockdict$i" = "$1" ] && break
  56. i=`expr $i + 1`
  57. done
  58. eval _lockdict$i="$1"
  59. _lockdict_size=`expr ${_lockdict_size} + 1`
  60. _lockfile="$LOCK_BASEDIR/$1"
  61. IFS=$OLDIFS
  62. }
  63.  
  64. # First we keep track of the new lock by calling _setlockfile.
  65. # Then we'll loop waiting 10 seconds for a lock (would be good for debug purpouses)
  66. # Once we get the lock the echo commands would be executed. If lockf is unable to get
  67. # the lock that command wouldn't be executed and an error code would be returned
  68. # instead. We use that to check whether the lock was acquired or not.
  69. # If the lock could be acquired we exit the loop, if not we try to get the lock again.
  70.  
  71. claim_lock(){
  72. mkdir -p "$LOCK_BASEDIR"
  73. _setlockfile $1
  74. while true; do
  75. lockf -k -s -t 10 $LOCK_BASEDIR/$1 echo 'Acquiring lock $LOCK_BASEDIR/$1' > /tmp/hotplug.log
  76. if [ "$?" -eq "0" ]; then
  77. break
  78. fi
  79. done
  80. }
  81.  
  82. # We check for all the ocurrences of the lock, we just release one of them.
  83. # We should never have $_lockdict_size < 0, because that variable would only be
  84. # decremented if a lock previously reserved is found.
  85.  
  86. release_lock(){
  87. local i
  88. local j
  89. local temp
  90. local found
  91. local more
  92. more=0
  93. found=0
  94. i=0
  95. j=${_lockdict_size}
  96. while [ "$i" -lt "$j" ]; do
  97. temp=$(eval echo \$_lockdict$i)
  98. echo $temp
  99. if [ $temp = "$1" ]; then
  100. # if found is 1, that would mean we previously found the
  101. # lock in the array, and that would mean it was acquired
  102. # more than one time, and so we can't remove the lock
  103. # file.
  104. if [ "$found" = "1" ]; then
  105. more=1
  106. fi
  107. eval _lockdict_size=`expr $_lockdict_size - 1`
  108. found=1
  109. fi
  110. i=`expr $i + 1`
  111. done
  112. # we can't only release this lock if was found and if it is the last acquisition
  113. # of the lock
  114. echo more: $more
  115. echo found: $found
  116.  
  117. if [ "$more" = "0" -a "$found" = "1" ]; then
  118. if [ -e $LOCK_BASEDIR/$1 ]; then
  119. rm $LOCK_BASEDIR/$1
  120. fi
  121. fi
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement