#!/usr/local/bin/bash
#
# Copyright (c) 2008, Christopher Cowart and contributors
# All rights reserved.
# 
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions 
# are met:
# * Redistributions of source code must retain the above copyright 
#   notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright 
#   notice, this list of conditions and the following disclaimer in the 
#   documentation and/or other materials provided with the distribution.
# 
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 
# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
# TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# $Id: conflock.in 450 2009-10-05 04:38:20Z blee $

# The locking mechanisms had to be factored out into a separate script so
# they could be sudo'd as necessary.

# Atomically checks and obtains the specified lock file or returns non-zero
function lock {
    local pid="$1"
    local lockfile="$2"
    local count=0

    if [ -z "$lockfile" ] ; then
        echo "No lock file specified in conflock" >&2; exit 2;
    fi

    if ! [ -d `dirname "$lockfile"` ] ; then
        echo "Lock file directory does not exist" >&2; exit 2;
    fi

    if [ -f "$lockfile" ] && [ "x`cat $lockfile`" = "x$pid" ] ; then
        return 0
    fi

    # Ensure that the lock is world-readable
    umask 022

    # This is a race-free bourne lock. 
    until ( set -o noclobber; echo "$pid" > "$lockfile"; ) 2>/dev/null ; do
        if ps `cat $lockfile` >/dev/null 2>&1 ; then

            if [ "${count}" -eq 10 ] ; then
                echo "Giving up..." >&2
                return 1
            fi

            echo "waiting for lock on ${lockfile}..." >&2
            sleep 1
            count=$((${count} + 1))
        else
            echo "Removing lockfile for dead process" \
                "`cat $lockfile`: $lockfile" >&2
            rm -f "$lockfile"
        fi
    done
    return 0
}

# Immediately deletes the lock file
function unlock {
    local pid="$1"
    local lockfile="$2"

    if [ -z "$lockfile" ] ; then
        echo "No lock file specified in conflock" >&2; exit 2;
    fi

    if ! [ -f "$lockfile" ] ; then
        echo "Cannot unlock: not locked." >&2
        exit 2
    fi

    if [ "x`cat $lockfile`" = "x$pid" ] ; then
        rm -f "$lockfile"
    else
        echo "Cannot unlock: PID does not match." >&2
        exit 2
    fi
}

function haslock {
    local pid="$1"
    local lockfile="$2"

    if [ -z "$lockfile" ] ; then
        echo "No lock file specified in conflock" >&2; exit 2;
    fi

    if [ -f "$lockfile" ] && [ "x`cat $lockfile`" = "x$pid" ] ; then
        return 0
    else
        return 1
    fi
}

action="$1"
shift

case "$action" in
    lock)       lock "$@";;
    unlock)     unlock "$@";;
    haslock)    haslock "$@";;
    *)      echo "Unknown argument to conflock: $1" >&2; exit 2;;
esac

exit $?

