Project

General

Profile

1 7259 aaronmk
#!/bin/sh
2
# Wraps lockfile/dotlockfile
3 7260 aaronmk
# Usage: env [interval=...] self lockfile
4 7259 aaronmk
5 7426 aaronmk
selfDir="$(dirname -- "$0")"
6
7 7262 aaronmk
: "${interval=5}" # s
8 7260 aaronmk
9 7429 aaronmk
if test -n "$inner"; then
10
    "$selfDir/dotlockfile" -l -r 65535 -p "$lockfile" || exit
11
    echo "$pid" >"$lockfile" # use the outer process's PPID
12
    exit
13
fi
14
15
statFmtFlag="$(test "$(uname)" = Darwin && echo f || echo c)"
16
17 7260 aaronmk
if test "$(uname)" = Darwin; then
18 7429 aaronmk
    file_group () { stat -f %Sg "$@"; }
19
else
20
    file_group () { stat -c %G "$@"; }
21
fi
22
23
export lockfile="$1"
24
if test "$(uname)" = Darwin; then
25 7260 aaronmk
    while ! shlock -p "$PPID" -f "$1"; do sleep "$interval"; done
26
else
27 7430 aaronmk
    # Need to change primary group of the dotlockfile process to the group of
28
    # the dir to contain the lockfile, because dotlockfile otherwise reports a
29
    # "permission denied" error (even though the directory is actually writable,
30
    # dotlockfile thinks it isn't).
31
    # newgrp, the command that does this, does not pass arguments to the new
32
    # process, so they must instead be passed via environment variables and a
33
    # recursive invocation of lockfile (with the $inner recursion flag set).
34
    # Additionally, exec cannot be used to propagate the PPID (needed by
35
    # dotlockfile) because newgrp creates a new process rather than using exec,
36
    # so it must be manually entered into the lockfile after dotlockfile runs.
37 7429 aaronmk
    export SHELL="$0" inner=1 pid="$PPID"
38
    newgrp "$(file_group "$(dirname -- "$lockfile")")"
39 7260 aaronmk
fi