Project

General

Profile

1
#!/bin/sh
2
# Wraps lockfile/dotlockfile
3
# Usage: env [interval=...] self lockfile
4

    
5
selfDir="$(dirname -- "$0")"
6

    
7
: "${interval=5}" # s
8

    
9
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
if test "$(uname)" = Darwin; then
18
    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
    while ! shlock -p "$PPID" -f "$1"; do sleep "$interval"; done
26
else
27
    # 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
    export SHELL="$0" inner=1 pid="$PPID"
38
    newgrp "$(file_group "$(dirname -- "$lockfile")")"
39
fi
(38-38/79)