#!/usr/bin/sh
# chkconfig: 345 88 20
# after vstorage-fs (S56) and after shaman (S57)
#
### BEGIN INIT INFO
# Provides: ostor-agentd
# Required-Start: $remote_fs
# Default-Start:  3 4 5
# Default-Stop: 0 1 2 6
# Short-Description: Object storage host agent service
### END INIT INFO

# Source function library.
source /etc/init.d/functions
PID_FILE="/run/ostor/0000000000000000.pid"
LOCK_FILE="/var/lib/ostor/services/0000000000000000/control/.lock"
AGENT_BIN="/usr/bin/ostor-agent"
SUBSYS="ostor-agentd"
USER="ostor"
LOGGER="/bin/logger"

if [ ! -d "/run/ostor" ]; then
        mkdir -p "/run/ostor"
        chgrp "$USER" "/run/ostor"
        chmod g+w "/run/ostor"
fi

[ ! -f "$LOGGER" ] && LOGGER=""

log_err() {
	[ -z "$LOGGER" ] && continue
	local msg="$1"
	$LOGGER -t "$SUBSYS" "$msg"
}

check_pid_name() {
	# check process own flock for
	pid=$(cat "$PID_FILE")
	ls -l /proc/$pid/fd 2>/dev/null | grep $LOCK_FILE >/dev/null
	return $?
}

check_monitor_pid() {
        __pids_var_run ignored "$PID_FILE"
        code="$?"
        case "$code" in
                0) # program is running (likely)
                        if check_pid_name ; then
                                # program is running
                                return 0
                        else
                                return 1
                        fi
                        ;;
                *)
                        return "$code"
                        ;;
        esac
}

kill_services() {
        local prefix="$1"
        local sbin="$2"
        for s in $(ls $prefix); do
                pid=$(cat $s 2>/dev/null | head -n 1)
                [ -z "$pid" ] && continue
                [ ! -d "/proc/$pid" ] && continue
                bin=$(cat "/proc/$pid/cmdline" 2>/dev/null | tr '\00' '\n' | head -n1)
                [ "$bin" != "$sbin" ] && continue
		log_err "kill orphan service $(basename $bin), pid file=$s, pid=$pid"
                kill -9 "$pid" 2>/dev/null
        done
}

cleanup_childs() {
	# kill orphan services if present
	kill_services "/run/ostor/08*.pid" "/usr/bin/ostor-ns"
	kill_services "/run/ostor/10*.pid" "/usr/bin/ostor-os"
	kill_services "/run/ostor/80*.pid" "/usr/bin/ostor-s3gw"
}

start_agentd() {
	echo -n "Starting ostor-agentd: "
	check_monitor_pid
	case $? in
		1) # program is dead, pid file exists
			pid=$(cat $PID_FILE 2>/dev/null | head -n 1)
			log_err "service was dead, pid=$pid"
			rm -f $PID_FILE
			;;
		0) # program is running
			echo_failure
			echo "Service is already running"
			RETVAL=1
			return
			;;
	esac

	if [ ! -f /var/lib/ostor/name -o ! -f  /var/lib/ostor/.id ]; then
		echo_failure
		echo "This host not joined to object storage"
		RETVAL=1
		return
	fi

	cleanup_childs 2>/dev/null

	$AGENT_BIN
	if [ $? -eq 0 ]; then
		[ -d "/var/lock/subsys" ] && touch "/var/lock/subsys/$SUBSYS"
		echo_success
	else
		echo_failure
		RETVAL=1
	fi
	echo
}

stop_agentd() {
	echo -n $"Shutting down ostor-agentd: "
        if [ ! -f "$PID_FILE" ]; then
		echo_success
		echo
		return
        fi

        if killproc -p "$PID_FILE" $AGENT_BIN; then
		[ -d "/var/lock/subsys" ] && rm -f "/var/lock/subsys/$SUBSYS"
                echo_success
	else
		echo_failure
		RETVAL=1
        fi

        echo
        return
}

agentd_status() {
        check_monitor_pid
	RETVAL=$?
        case $RETVAL in
                1)
			echo "Service is dead, pid file exists"
                        ;;
                0)
			echo "Service is running"
                        ;;
		3)
			echo "Service stopped"
			;;
        esac
}

agentd_condrestart() {
	[ ! -f "$PID_FILE" ] && return
        stop_agentd
        start_agentd
}


# See how we were called.
RETVAL=0
case "$1" in
  start)
	start_agentd
        ;;
  stop)
	stop_agentd
        ;;
  restart)
	stop_agentd
	start_agentd
	;;
  condrestart)
	agentd_condrestart
	;;
  status)
	agentd_status
        ;;
  *)
        echo $"Usage: $0 {start|stop|status|restart|condrestart}"
        exit 2
esac
exit $RETVAL


