forked from lunar-linux/lunar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlocking.lunar
124 lines (109 loc) · 3.66 KB
/
locking.lunar
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/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
}