#!/bin/bash
#
# condor	This script allows for starting and stopping Condor.
#
# chkconfig: - 98 10
# description: Condor is a high throughput computing batch processing
# 	       platform.
# processname: condor_master
# config: /etc/condor/condor_config
# pidfile: /var/run/condor/condor_master.pid

### BEGIN INIT INFO
# Provides: condor
# Required-Start: $local_fs $network
# Required-Stop: $local_fs $network
# Short-Description: start and stop Condor
# Description: Condor HTC computing platform
### END INIT INFO

# The program being managed
prog=condor_master

lockfile=/var/lock/subsys/condor
pidfile=/var/run/condor/$prog.pid

# Source function library
. /etc/init.d/functions

# Source networking configuration
[ -f /etc/sysconfig/network ] && . /etc/sysconfig/network

# Source Condor configuration
[ -f /etc/sysconfig/condor ] && . /etc/sysconfig/condor

# Check that networking is up
[ "${NETWORKING}" = "no" ] && exit 1

if [ -n "$ULIMIT_FLAGS" ]; then
  ulimit $ULIMIT_FLAGS > /dev/null 2>&1
fi

start() {
    echo -n $"Starting Condor daemons: "
    daemon --pidfile $pidfile --check $prog $prog -pidfile $pidfile
    RETVAL=$?
    echo
    [ $RETVAL -eq 0 ] && touch $lockfile
    return $RETVAL
}

stop() {
    echo -n $"Stopping Condor daemons: "
    killproc -p $pidfile $prog -QUIT
    RETVAL=$?
    echo
    wait_pid $pidfile 15
    if [ $? -ne 0 ]; then
	# If this happens during a restart the start is likely to see
	# condor still running and just return 0, which means when
	# condor exits it won't be restarted
	echo $"Warning: $prog may not have exited, start/restart may fail"
	RETVAL=1
    fi
    [ $RETVAL -eq 0 ] && rm -f $lockfile
    return $RETVAL
}

reload() {
    echo -n $"Reloading Condor daemons: "
    kill -HUP `cat $pidfile` &>/dev/null
    RETVAL=$?
    echo_success
    echo
    return $RETVAL
}

peaceful_off() {
    condor_off -peaceful
    rc=$?
    exit $rc
}

#
# Determine if a process is running only by looking in a pidfile.
# There is no use of pidof, which can find processes that are not
# started by this script.
#
# ASSUMPTION: The pidfile will exist if the process does, see false
# negative warning.
#
# WARNING: A false positive is possible if the process that dropped
# the pid file has crashed and the pid has been recycled. A false
# negative is possible if the process has not yet dropped the pidfile,
# or it contains the incorrect pid.
#
# Usage: pid_status <pidfile> <lockfile>
# Result: 0 = pid exists
#         1 = pid does not exist, but pidfile does
#         2 = pid does not exist, but lockfile does
#         3 = pidfile does not exist, thus pid does not exist
#         4 = status unknown
#
pid_status() {
    pid=$(get_pid $1)
    case $? in
	1) return 3 ;;
	2) return 4 ;;
    esac

    ps $pid &>/dev/null
    if [ $? -ne 0 ]; then
	if [ -e $2 ]; then
	    return 2
	fi

	return 1
    fi

    return 0
}

#
# Wait for the pid in the pidfile to disappear, but only do so for at
# most timeout seconds.
#
# Usage: wait_pid <pidfile> <timeout>
# Result: 0 = pid was not found (doesn't exist or not accessible)
#         1 = pid still exists after timeout
wait_pid() {
    pid=$(get_pid $1)
    if [ $? -ne 0 ]; then
	return 0
    fi

    wait=0
    while [ $wait -lt $2 ]; do
	pid_status $1 /dev/null   # ingore lockfile
	if [ $? -ne 0 ]; then
	    return 0
        fi

	sleep 1
	wait=$((wait + 1))
    done

    return 1
}

#
# Retrieve pid from a pidfile
#
# Usage: get_pid <pidfile>
# Result: 0 = pid returned
#         1 = pidfile not found
#         2 = pidfile not accessible or didn't contain pid
# Stdout: pid
#
get_pid() {
    if [ -s $1 ]; then
	pid=$(cat $1 2>/dev/null)
	#pid=`cat $1` &>/dev/null
	if [ $? -ne 0 -o -z "$pid" ]; then
	    return 2
	fi

	echo -n $pid
	return 0
    fi

    return 1
}


pid_status $pidfile $lockfile
running=$?

if [ "$1" != "status" ]; then
    # Report that $prog does not exist, or is not executable
    if [ ! -x /usr/sbin/$prog ]; then
	echo $"$0: error: program not installed"
	exit 5
    fi

    [ $running -eq 4 ] && echo $"$0: error: insufficient privileges" && exit 7
fi

case "$1" in
    start)
	[ $running -eq 0 ] && exit 0
	start
	RETVAL=$?
	;;
    stop)
	[ $running -eq 0 ] || exit 0
	stop
	RETVAL=$?
	;;
    restart)
	RETVAL=0
	if [ $running -eq 0 ]; then
	    stop
	    RETVAL=$?
	fi
	[ $RETVAL -eq 0 ] && start
	RETVAL=$?
	;;
    condrestart|try-restart)
	[ $running -eq 0 ] || exit 0
	stop
	[ $? -eq 0 ] && start
	RETVAL=$?
	;;
    reload|force-reload)
	if [ $running -ne 0 ]; then
	    echo $"$0: error: $prog is not running"
	    exit 7
	fi
	reload
	RETVAL=$?
	;;
    status)
	if [ $running -ne 0 ]; then
	    case "$running" in
		1) echo $"$prog dead but pid file exists" ;;
		2) echo $"$prog dead but subsys locked" ;;
		3) echo $"$prog is stopped" ;;
		4) echo $"$prog status is unknown" ;;
	    esac

	    exit $running
	fi

	# WARNING: status uses pidof and may find more pids than it
	# should.
	status -p $pidfile $prog
	RETVAL=$?
	;;

    peaceful-off)
	[ $running -eq 0 ] || exit 0
	peaceful_off
	RETVAL=$?
       ;;
    *)
	echo $"Usage: $0 {start|stop|restart|condrestart|try-restart|reload|force-reload|status|peaceful-off}"
	RETVAL=2
esac

exit $RETVAL
