Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Fetching contributors…

Cannot retrieve contributors at this time

127 lines (109 sloc) 3.749 kB
#!/bin/bash
# #
# This code is written for Lunar Linux, see #
# http://lunar-linux.org #
# #
############################################################
# #
# $FUNCTIONS/locking #
# includes lock_file and unlock_file functions #
# #
# 20020526 #
# #
############################################################
# #
# Copyrighted Kagan Kongar 2002 under GPLv2 #
# #
############################################################
# function: lock_file
# usage : lock_file <absolute file name>
# purpose : locks a file or wait until. Better use as lock_file <file> && || etc
function lock_file() {
local TEMPFILE LOCKFILE STALE_PID
debug_msg "lock_file ($@)"
function file_lock() {
#locking is disabled if ln or rm not found!!
[ -x "/bin/ln" ] || return 0
[ -x "/bin/rm" ] || return 0
TEMPFILE="$1.$$"
LOCKFILE="$1.lock"
# test permission
if ! echo $$ > $TEMPFILE 2>/dev/null; then
message "${PROBLEM_COLOR}You don't have permission to access" \
"$TEMPFILE ${DEFAULT_COLOR}"
exit 1
fi
# file exists?
if [ ! -f "$1" ]; then
touch "$1"
fi
# set lockfile
if ln $TEMPFILE $LOCKFILE 2>/dev/null ; then
rm -f $TEMPFILE
return 0
# failed due to other lock?
elif [ ! -f "$LOCKFILE" ]; then
return 1
fi
# stale lock?
STALE_PID=`< $LOCKFILE`
if [ "$STALE_PID" -le "0" ]; then
return 1
fi
if kill -0 $STALE_PID 2>/dev/null; then
rm -f $TEMPFILE
return 1
fi
# yes, stale
rm -f $LOCKFILE 2>/dev/null &&
echo "Removed stale lock file of process $STALE_PID"
if ln $TEMPFILE $LOCKFILE 2>/dev/null; then
rm -f $TEMPFILE
return 0
fi
rm -f $TEMPFILE
return 1
}
while ! file_lock $1; do
message "${MESSAGE_COLOR}Waiting to lock the file $1${DEFAULT_COLOR}"
sleep 1
done
return 0
}
# function: unlock_file
# usage : unlock_file <absolute file name>
# purpose : unlocks a file
function unlock_file() {
debug_msg "unlock_file ($@)"
#unlocking is disabled if rm not found!!
[ -x "/bin/rm" ] || return 0
rm -f $1.lock 2>/dev/null && return 0
return 1
}
lin_locked() {
debug_msg "lin_locked ($@)"
[ -f /var/lock/installing.$1 ] &&
ps `cat "/var/lock/installing.$1"` |
grep -q "lin"
}
solo_locked() {
debug_msg "solo_locked ($@)"
for SOLO_MODULE in `cat "$SOLO"` ; do
if lin_locked $SOLO_MODULE ; then
message "${PROBLEM_COLOR}lining${DEFAULT_COLOR}${MESSAGE_COLOR} of other modules is disabled during a lin ${MODULE_COLOR}$SOLO_MODULE${DEFAULT_COLOR}"
return
fi
done
false
}
current_locked() {
debug_msg "current_locked ($@)"
if lin_locked $MODULE; then
message "${QUERY_COLOR}Detected lin lock file: ${FILE_COLOR}$linING${DEFAULT_COLOR}"
message "${MESSAGE_COLOR}Now waiting for ${MODULE_COLOR}${MODULE}${DEFAULT_COLOR}${MESSAGE_COLOR} to finish installing or for the lock to clear.${DEFAULT_COLOR}"
while lin_locked $MODULE; do sleep 2; done
false
else
false
fi
}
Jump to Line
Something went wrong with that request. Please try again.