#!/bin/sh
#
# Backtrace I/O start-up script for coronerd crash consolidation server.
#
# Copyright (C) 2014 Backtrace I/O.
# All Rights Reserved.
#
# All information contained herein is, and remains the property of Backtrace
# I/O and its suppliers, if any.  The intellectual and technical concepts
# contained herein are proprietary to Backtrace I/O and its suppliers and may
# be covered by U.S. and Foreign Patents, patents in process, and are protected
# by trade secret or copyright law.  Dissemination of this information or
# reproduction of this material is strictly forbidden unless prior written
# permission is obtained from Backtrace I/O Incorporated.
#
### BEGIN INIT INFO
# Provides:		coronerd
# Required-Start:	$syslog $local_fs $remote_fs $network
# Default-Start:	2 3 4 5
# Default-Stop:		0 1 6
# Short-Description:	Backtrace I/O crash consolidation server
# Description:		Starts the Backtrace I/O crash consolidation server
### END INIT INFO

EXIT_SUCCESS=0
EXIT_FAILURE=1

PATH="/sbin:/bin:/usr/sbin:/usr/bin"
export PATH

NAME=coronerd
DESC="Backtrace I/O consolidation server"
PID_DIR=/var/run/coronerd
PID_FILE=${PID_DIR}/${NAME}.pid
SCRIPTNAME=/etc/init.d/${NAME}
MONITOR="/opt/backtrace/bin/monitor"
TRACE="/opt/backtrace/bin/coronerd-crash-report.sh"

if [ -f /etc/default/coronerd ]; then
	. /etc/default/coronerd
fi

# Set to sane defaults if commented in /etc/default/coronerd
: ${CORONERD_WATCHDOG_INTERVAL:=120}
: ${CORONERD_WATCHDOG_START_INTERVAL:=300}
: ${CORONERD_CONFIG_FILE:="/etc/coronerd/coronerd.conf"}
: ${CORONERD_ASAN_LOG_PATH:="/var/run/coronerd/asan.log"}

CORONERD_PATH="/opt/backtrace/sbin/coronerd"
if [ "${CORONERD_SANITIZED:-}" = "yes" ] ; then
	export ASAN_OPTIONS="${CORONERD_ASAN_OPTIONS:-}"
	if [ -n "${CORONERD_ASAN_LOG_PATH}" ]; then
		export ASAN_OPTIONS="${ASAN_OPTIONS}:log_path=${CORONERD_ASAN_LOG_PATH}"
	fi
	CORONERD_PATH="/opt/backtrace/sbin/coronerd.asan"
fi

OPTIONS="-f -c ${CORONERD_CONFIG_FILE}"

. /lib/lsb/init-functions

uid=$(/usr/bin/id -u) && [ "$uid" = "0" ] || exit 4

# Verify that all relevant binaries are present.
if test ! -x $CORONERD_PATH; then
	log_failure_msg "$CORONERD_PATH not found"
	exit $EXIT_FAILURE
fi

mkdir -m0755 -p ${PID_DIR}
if [ $? -ne 0 ] ; then
    log_failure_msg "Failed to create ${PID_DIR}"
    exit $EXIT_FAILURE
fi

case "$1" in
start)
	test -f $CORONERD_CONFIG_FILE || exit 6
	log_daemon_msg "Starting $DESC" $NAME
	start-stop-daemon --start --oknodo --quiet	\
	    --pidfile $PID_FILE						\
	    --exec $MONITOR --						\
	    --pidfile="$PID_FILE"					\
	    --glider="$TRACE"						\
	    --watchdog-interval="$CORONERD_WATCHDOG_INTERVAL"			\
	    --watchdog-start-interval="$CORONERD_WATCHDOG_START_INTERVAL"	\
	    $CORONERD_PATH $OPTIONS
	log_end_msg $?
	;;
stop)
	log_daemon_msg "Stopping $DESC" $NAME
	start-stop-daemon --stop --retry 30 --oknodo --quiet \
	    --pidfile $PID_FILE --exec $MONITOR
	RC=$?
	if test "$RC"; then
		rm -f $PID_FILE
	fi
	log_end_msg $RC
	;;
reload)
	monpid=$(cat $PID_FILE 2>/dev/null)
	[ -n "$monpid" ] && crpid=$(ps -ho command ${monpid} 2>/dev/null | awk -F"child " '{print $2}')
	[ -n "$crpid" ] && kill -HUP $crpid
	;;
restart)
	$0 stop
	$0 start
	;;
status)
	status_of_proc -p "$PID_FILE" "$MONITOR" $CORONERD_PATH && \
	    exit 0 || exit $?
	;;
*)
	echo "Usage: $SCRIPTNAME {start|stop|reload|restart|status}" >&2
	exit $EXIT_FAILURE
	;;
esac

exit $EXIT_SUCCESS
