-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathpcllock
More file actions
executable file
·460 lines (438 loc) · 18.1 KB
/
pcllock
File metadata and controls
executable file
·460 lines (438 loc) · 18.1 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
#!/bin/sh
######################################################################
#
# PCLLOCK - A Cleaner Command for "pexlock" and "pshlock"
# (remove the expired and orphaned lockfiles in the lockdirectory)
#
# Usage : pcllock [options]
# -d <lockdir> ...... the directory for lockfiles.
# The lockfile directory will be decided
# as the following rule.
# 1) this option "-d" if specified
# 2) the environment varriable "PLOCKDIR"
# if specified
# 3) the current working directory (default)
# -w <maxwaiting> ... maximum waiting seconds to succeed unlocking
# a shared lock
# (-1 means waiting infinitely)
# The default value is 10.
# -l <lifetime> ..... the maximum lifetime of the lockfile (sec)
# The default value is 600.
# Older lockfiles than that will be ignored
# and removed.
# (-1 means infinitely)
# Return : $? ==0 ... one or more cleaning were success
# !=0 ... all failure
# stdout ....... enerated path of the lockfile (just lock-id)
#
# Example : write the following line into the crontab file someone owned
# * * * * * pcllock -l 300 -w 10 -d /PATH/TO/LOCKDIR
#
# Notice : The lockfile is written with rw-rw-rw for sharing.
# If you want not to share it with others,
# you have to give the lockdir rwxrwx--- or rwx------ permisson.
#
# Written by Shell-Shoccar Japan (@shellshoccarjpn) on 2020-12-06
#
# This is a public-domain software (CC0). It means that all of the
# people can use this for any purposes with no restrictions at all.
# By the way, We are fed up with the side effects which are brought
# about by the major licenses.
#
# The latest version is distributed at the following page.
# https://github.com/ShellShoccar-jpn/misc-tools
#
######################################################################
######################################################################
# Initial Configuration
######################################################################
# === Initialize shell environment ===================================
set -u
umask 0022
export LC_ALL=C
export PATH="$(command -p getconf PATH 2>/dev/null)${PATH+:}${PATH-}"
case $PATH in :*) PATH=${PATH#?};; esac
export UNIX_STD=2003 # to make HP-UX conform to POSIX
# === Define the functions for printing usage and error message ======
print_usage_and_exit () {
cat <<-USAGE 1>&2
Usage : ${0##*/} [options]
Options : -d <lockdir> ...... the directory for lockfiles.
The lockfile directory will be decided
as the following rule.
1) this option "-d" if specified
2) the environment varriable "PLOCKDIR"
if specified
3) the current working directory (default)
-w <maxwaiting> ... maximum waiting seconds to succeed unlocking
a shared lock
(-1 means waiting infinitely)
The default value is 10.
-l <lifetime> ..... the maximum lifetime of the lockfile (sec)
The default value is 600.
Older lockfiles than that will be ignored
and removed.
(-1 means infinitely)
Return : \$? ==0 ... one or more cleaning were success
!=0 ... all failure
stdout ....... enerated path of the lockfile (just lock-id)
Example : write the following line into the crontab file someone owned
* * * * * pcllock -l 300 -w 10 -d /PATH/TO/LOCKDIR
Version : 2020-12-06 02:53:41 JST
USAGE
exit 1
}
warning() {
${1+:} false && echo "${0##*/}: $1" 1>&2
}
error_exit() {
${2+:} false && echo "${0##*/}: $2" 1>&2
exit $1
}
exit_trap () {
trap '' EXIT HUP INT QUIT PIPE ALRM TERM
rm -f "${tmpf_pids:-}" "${tmpf_time:-}" "${File_busy:-}" >/dev/null 2>&1
i=$?
trap - EXIT HUP INT QUIT PIPE ALRM TERM
case $# in 0) exit $i;; *) exit ${1:-0};; esac
}
# === FUNC: Calcurate the expire date-and-time (for touch command) ===
# arg : number of second from now which you know the date-and-time
# ret : 0
# stdout: path of the generated temporary file
expire_date_and_time() {
awk '
BEGIN {
now_gm="'$(TZ=UTC+0 date '+%Y%m%d%H%M%S')'";
now_lo="'$( date '+%Y%m%d%H%M%S')'";
unixtime2YYYYMMDDhhmmss_init("LOCALTIME");
now_ut = YYYYMMDDhhmmss2unixtime(now_lo);
bor_rt = unixtime2YYYYMMDDhhmmss(now_ut-'"$1"'-1);
print substr(bor_rt,1,12) "." substr(bor_rt,13);
}
function YYYYMMDDhhmmss2unixtime(YYYYMMDDhhmmss, Y,M,D,h,m,s,l) {
# 1) seperate the units
l = length(YYYYMMDDhhmmss);
if (l < 5) { # invalid
return -1;
} else if (l < 8) { # YYMMMDD only
Y = substr(YYYYMMDDhhmmss, 1,l-4)*1+substr(now_lo,1,2)*100;
M = substr(YYYYMMDDhhmmss,l-3, 2)*1;
D = substr(YYYYMMDDhhmmss,l-1, 2)*1;
h = 0; m = 0; s = 0;
} else if (l < 12) { # YYYYMMDD only
Y = substr(YYYYMMDDhhmmss, 1,l-4)*1;
M = substr(YYYYMMDDhhmmss,l-3, 2)*1;
D = substr(YYYYMMDDhhmmss,l-1 )*1;
h = 0; m = 0; s = 0;
} else { # YYYYMMDDhhmmss
Y = substr(YYYYMMDDhhmmss, 1,l-10)*1;
M = substr(YYYYMMDDhhmmss,l-9, 2)*1;
D = substr(YYYYMMDDhhmmss,l-7, 2)*1;
h = substr(YYYYMMDDhhmmss,l-5, 2)*1;
m = substr(YYYYMMDDhhmmss,l-3, 2)*1;
s = substr(YYYYMMDDhhmmss,l-1 )*1;
}
# 2) validate
if ((s>60) || (m>59) || (h>23) || (M>12)) {return -1;}
days_of_month[2] = (Y%4!=0)?28:(Y%100!=0)?29:(Y%400!=0)?28:29;
if (D > days_of_month[M] ) {return -1;}
# 3) adjust the value of year and month
if (M<3) {M+=12; Y--;}
# 4) calculate unixtime
return (365*Y+int(Y/4)-int(Y/100)+int(Y/400)+int(306*(M+1)/10)-428+D-719163)*86400+(h*3600)+(m*60)+s-offset;
}
function unixtime2YYYYMMDDhhmmss_init(localtime_flag, gm,lo) {
max_calced_year = 1970; # To remember everydays on Jan.1 from
days_on_Jan1st_from_epoch[1970] = 0; # the Epoch which was calculated once
split("31 0 31 30 31 30 31 31 30 31 30 31", days_of_month);
if (localtime_flag == "LOCALTIME") {
gm = YYYYMMDDhhmmss2unixtime(now_gm);
lo = YYYYMMDDhhmmss2unixtime(now_lo);
offset = lo - gm;
offset -= (offset%2); # cancel the timelag of the two date starting time
} else {
offset = 0;
}
}
function unixtime2YYYYMMDDhhmmss(ut, Y,M,D,h,m,s,t,i,j) {
# 0) timezone adjustment
ut += offset;
if (ut < 0) {return -1;}
# 1) calculate hour,minute,second and number of days from the Epoch
s = ut % 60; t = int(ut/60);
m = t % 60; t = int( t/60);
h = t % 24;
days_from_epoch = int( t/24);
# 2) calculate year
Y = int(days_from_epoch/365.2425)+1970+1;
if (Y > max_calced_year) {
i = days_on_Jan1st_from_epoch[max_calced_year];
for (j=max_calced_year; j<Y; j++) {
i += (j%4!=0)?365:(j%100!=0)?366:(j%400!=0)?365:366;
days_on_Jan1st_from_epoch[j+1] = i;
}
max_calced_year = Y;
}
for (;;Y--) {
if (days_from_epoch >= days_on_Jan1st_from_epoch[Y]) {
break;
}
}
# 3) calculate month,day
days_of_month[2] = (Y%4!=0)?28:(Y%100!=0)?29:(Y%400!=0)?28:29;
D = days_from_epoch - days_on_Jan1st_from_epoch[Y] + 1;
for (M=1; ; M++) {
if (D > days_of_month[M]) {
D -= days_of_month[M];
} else {
break;
}
}
return sprintf("%04d%02d%02d%02d%02d%02d",Y,M,D,h,m,s);
}
'
}
# === FUNC: Make a Temporary File ====================================
# arg : (none)
# ret : 0 when succeeded
# stdout: path of the generated temporary file
LFs=$(printf '\\\n_');LFs=${LFs%_} # Use as a "\n" in s-command of sed
mktempf0() {
# --- parameters, etc. ---------------------------------------------
num_of_digits=16 # Length of Random String
n=10 # Max Times of Retrying
chrs='0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_'
# Available letters
# --- call built-in mktemp if exists -------------------------------
type mktemp >/dev/null 2>&1 && {
mktemp -t tmp.${0##*/}.$$.XXXXXXXXXXXXXXXX
return $?
}
# --- set the temporary directory ----------------------------------
Dir_tmp=$(set | grep -q ^TMPDIR= | sed 's/^[^=]\{1,\}=//');
case "$Dir_tmp" in
'') Dir_tmp='/tmp' ;;
/) Dir_tmp='' ;;
*) Dir_tmp=${Dir_tmp%/};;
esac
# --- mktemp loop --------------------------------------------------
while [ $n -ge 0 ]; do
# --- Generate Ramdom string -------------------------------------
# calculate the number of words which required
nw=$(echo "${num_of_digits}*l(${#chrs})/11.09+1" | # 11.09=ln(65536)
bc -l |
sed 's/\..*$//' )
# make a random hexadecimal digit
if [ -c /dev/urandom ]; then
hstr=$(dd if=/dev/urandom bs=2 count=$nw 2>/dev/null |
od -A n -t x2 -v |
tr 'abcdef ' 'ABCDEF\n' |
tr -Cd 0123456789ABCDEF )
else
hstr=$( (ps -Ao pid,etime,pcpu,vsz; date) |
od -t d4 -A n -v |
sed 's/[^0-9]\{1,\}/'"$LFs"'/g' |
grep '[0-9]' |
tail -n 42 |
sed 's/.*\(.\{8\}\)$/\1/g' |
awk 'BEGIN{a=-2147483648;} #
{a+=$1; } #
END { #
srand(a); #
for(i=0;i<'$nw';i++){ #
printf("%02X",int(rand()*65536)); #
} #
}' )
fi
# make a random string from the hexadecimal digit
s=$(echo "obase=${#chrs};ibase=16;$hstr" |
bc |
tr -d '\\\n' |
tr ' ' '\n' |
awk 'BEGIN {for(i=1;i<'$num_of_digits';i++){print 0;}} #
/[0-9]/{print; }' |
awk 'BEGIN {ORS=""; #
s="'"$chrs"'"; #
for(i=0;i<length(s);i++){ #
c[i]=substr(s,i+1,1); #
} } #
/[0-9]/{print c[$0*1]; }' |
tail -c $num_of_digits )
# --- Making the file path ---------------------------------------
Path_target="${Dir_tmp}/tmp.${0##*/}.$$.$s"
# --- Making the file --------------------------------------------
(set -C; umask 177; : > "$Path_target") 2>/dev/null || {
[ -f "$Path_target" ] && { n=$((n-1)); continue; }
n=-1; break;
}
break
done
# --- print error message when failed to make a file ---------------
case "$n" in -1) return 1;; esac
# --- print the path of the file -----------------------------------
printf '%s\n' "$Path_target"
# --- return successfully ------------------------------------------
return 0
}
######################################################################
# Argument Parsing
######################################################################
# === Set default values =============================================
optd=''
max_waiting_secs=10
max_lifetime_secs=600
# === Parse arguments ================================================
optmode=''
while [ $# -gt 0 ]; do
case "$optmode" in
'') case "$1" in
--) shift
break
;;
-[hv]|--help|--version)
print_usage_and_exit
;;
-[dwl]*) ret=$(printf '%s\n' "${1#-}" |
awk '{opt = substr($0,1,1); #
opt_str = (length($0)>1) ? substr($0,2) : ""; #
printf("%s %s", opt, opt_str); }')
ret1=${ret%% *}
ret2=${ret#* }
case "$ret1$ret2" in
d) optmode='d' ;;
d*) optd=$ret2 ;;
w) optmode='w' ;;
w*) max_waiting_secs=$ret2 ;;
l) optmode='l' ;;
l*) max_lifetime_secs=$ret2 ;;
esac
;;
-*) print_usage_and_exit
;;
*) break
;;
esac
;;
d) optd=$1
optmode=''
;;
w) max_waiting_secs=$1
optmode=''
;;
l) max_lifetime_secs=$1
optmode=''
;;
esac
shift
done
# === Validate arguments =============================================
case $# in [!0]*) print_usage_and_exit;; esac
echo "_$max_waiting_secs" | grep -Eq '^_-?[0-9]+$' || {
error_exit 1 'Maximum waiting seconds parameter is invalid'
}
echo "_$max_lifetime_secs" | grep -Eq '^_-?[0-9]+$' || {
error_exit 1 'Maximum lifetime parameter is invalid'
}
[ $max_lifetime_secs -lt 0 ] && exit
Dir_lock=''
while :; do
case "$optd" in '') :;; *) Dir_lock="${optd%/}/"
break ;; esac
case "${PLOCKDIR:-}" in '') :;; *) Dir_lock="${PLOCKDIR%/}/"
break ;; esac
case 'default' in *) Dir_lock=$(pwd)
Dir_lock=${Dir_lock%/}
break ;; esac
done
case "$Dir_lock" in [!/]*) s=$(pwd);Dir_lock="${s%/}/$Dir_lock";; esac
######################################################################
# Main Routine
######################################################################
# === Decide the target directory and change to there ================
cd "$Dir_lock" 2>/dev/null || {
error_exit 1 'No such directory or permission denied: '"$Dir_lock"
}
# === Get PIDs for making sure every owners of lockfile is no longer alive
trap 'exit_trap' EXIT HUP INT QUIT PIPE ALRM TERM
tmpf_pids=$(mktempf0) || {
error_exit 1 'Failed to make a temporary file #1'
}
ps -Ao pid | tr -d ' ' | grep -E '^[0-9]+$' | sort -k1,1 > "$tmpf_pids"
# === make a temporary file for listing the too old files ============
tmpf_time=$(mktempf0) || {
error_exit 1 'Failed to make a temporary file #'
}
# === Remove the orphaned ex-lockfiles in the target directory =======
# (And also remove the orphaned sh-lock access token file "*.busy")
touch -t $(expire_date_and_time $max_lifetime_secs) "$tmpf_time"
find './' -type f \( \! -newer "$tmpf_time" \) |
grep -E '^./[^/]+($|/[^/]+\.busy$)' | # <- with sh-lock access
# 1:old_ex-files # # token file "*.busy"
xargs grep '^' /dev/null |
grep ':[0-9]\{1,\}$' |
# 1:old_ex-files 2:pid_of_owner #
sort -t : -k 2,2 |
join -t : -1 1 -2 2 -v 2 -o 2.1,2.2 "$tmpf_pids" - |
# 1:old_and_orphaned_ex-files 2:pid_of_owner #
sed 's/:[0-9]\{1,\}$//' |
# 1:old_and_orphaned_ex-files #
xargs rm -f
# === Remove the orphaned sh-lockfiles in the target directory =======
IFS0=$IFS
IFS='
'
for lockdir in $(find './' -type d |
grep -E '^./[^/]+/?$' |
sed '/[^/]$/s/$/\//' )
do
lockdir=${lockdir%/}
lockname=${lockdir#./}
# 1) try to create the accesing right token file "*.busy"
if [ $max_waiting_secs -ge 0 ]; then
try=$((max_waiting_secs+1))
else
try=1
fi
while [ $try -gt 0 ]; do
File_busy="${Dir_lock}$lockname/$lockname/.busy"
(set -C; umask 0000; echo $$ > "$File_busy") 2>/dev/null || {
[ $max_waiting_secs -ge 0 ] && try=$((try-1)) # retry if already exists
case $try in 0) :;; *) sleep 1;; esac
continue
}
break
done
case $try in
0) warning "$lockname"': timeout, try again later\n'
continue
;;
esac
# 2) remove the orphaned sh-sublock files
touch -t $(expire_date_and_time $max_lifetime_secs) "$tmpf_time"
find "./$lockname/$lockname" -type d \( \! -newer "$tmpf_time" \) |
grep -E '/[0-9]{14}\.[0-9]+\.[0-9]+$' |
sed 's!^.*/[0-9]\{14\}.[0-9]\{1,\}\.\([0-9]\{1,\}\)$!\1 &!' |
sort -k1,1 |
join -1 1 -2 1 -v 2 -o 2.1,2.2 "$tmpf_pids" - |
sed 's/^[^ ]\{1,\} //' |
xargs rmdir
# 3) if the current number of the locking processes is 0,
# remove the sh-lock directory
n=$(ls -f1 "./$lockname/$lockname" 2>/dev/null |
awk 'BEGIN{n=0}; /^[0-9]/{n++;}; END{print n;}')
case $n in
0) rm -rf "$Dir_lock$lockname"
File_busy=''
continue
;;
esac
# 4) finish the loop successfully
rm -f "$File_busy"
File_busy=''
done
IFS=$IFS0
######################################################################
# Finish
######################################################################
exit_trap 0