1 #!/bin/bash 2 # 3 # Copyright (c) International Business Machines Corp., 2005 4 # Author: Ram Pai (linuxram (at] us.ibm.com) 5 # 6 # This library is free software; you can redistribute it and/or 7 # modify it under the terms of the GNU Lesser General Public 8 # License as published by the Free Software Foundation; either 9 # version 2.1 of the License, or (at your option) any later version. 10 # 11 # This library is distributed in the hope that it will be useful, 12 # but WITHOUT ANY WARRANTY; without even the implied warranty of 13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 14 # Lesser General Public License for more details. 15 # 16 # You should have received a copy of the GNU Lesser General Public 17 # License along with this library; if not, write to the Free Software 18 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 # 20 21 22 lockfile="/.nslock" 23 SUCCESS=0 24 FAIL=1 25 otherpid= 26 startparent() 27 { 28 rm -f $lockfile 29 echo $$ >| ${lockfile}parent 30 while [ 1 ] 31 do 32 otherpid="$(cat ${lockfile}child 2> /dev/null)" 33 if [ -n "$otherpid" -a -d /proc/$otherpid ] 34 then 35 return 36 fi 37 done 38 } 39 40 startchild() 41 { 42 rm -f $lockfile 43 echo $$ >| ${lockfile}child 44 while [ 1 ] 45 do 46 otherpid="$(cat ${lockfile}parent 2> /dev/null)" 47 if [ -n "$otherpid" -a -d /proc/$otherpid ] 48 then 49 return 50 fi 51 done 52 } 53 54 iamgoingahead() 55 { 56 while [ 1 ] 57 do 58 if [ ! -d /proc/$otherpid ] 59 then 60 return $FAIL 61 fi 62 str=`cat $lockfile 2> /dev/null` 63 pid=$(echo $str | awk '{print $1}') 64 error=$(echo $str | awk '{print $2}') 65 if [ "$pid" == "$$" ] 66 then 67 return $error 68 fi 69 sleep 1 70 done 71 } 72 73 74 goahead() 75 { 76 set -x 77 ret=$SUCCESS 78 if [ -n "$1" ] 79 then 80 ret=$1 81 fi 82 echo "$otherpid $ret" >| $lockfile 83 set +x 84 } 85