Home | History | Annotate | Download | only in bin
      1 #!/bin/bash
      2 
      3 lockfile="/.nslock"
      4 SUCCESS=0
      5 FAIL=1
      6 otherpid=
      7 startparent()
      8 {
      9         rm -f $lockfile
     10         echo $$ >| ${lockfile}parent
     11         while [ 1 ]
     12         do
     13                 otherpid="$(cat ${lockfile}child 2> /dev/null)"
     14                 if [ -n "$otherpid" -a -d /proc/$otherpid ]
     15                 then
     16                         return
     17                 fi
     18         done
     19 }
     20 
     21 startchild()
     22 {
     23         rm -f $lockfile
     24         echo $$ >| ${lockfile}child
     25         while [ 1 ]
     26         do
     27                 otherpid="$(cat ${lockfile}parent 2> /dev/null)"
     28                 if [ -n "$otherpid" -a -d /proc/$otherpid ]
     29                 then
     30                         return
     31                 fi
     32         done
     33 }
     34 
     35 iamgoingahead()
     36 {
     37         while [ 1 ]
     38         do
     39                 if [ ! -d /proc/$otherpid ]
     40                 then
     41                         return $FAIL
     42                 fi
     43                 str=`cat $lockfile 2> /dev/null`
     44                 pid=$(echo $str | awk '{print $1}')
     45                 error=$(echo $str | awk '{print $2}')
     46                 if [ "$pid" == "$$" ]
     47                 then
     48                         return $error
     49                 fi
     50                 sleep 1
     51         done
     52 }
     53 
     54 goahead()
     55 {
     56         set -x
     57         ret=$SUCCESS
     58         if [ -n "$1" ]
     59         then
     60                 ret=$1
     61         fi
     62         echo "$otherpid $ret" >| $lockfile
     63         set +x
     64 }
     65