#!/bin/sh
#
# Copyright 2010-2015 (C) VMware, Inc.  All rights reserved.
#
# rotatecore:
#  Setup core file rotation so we don't fill up the disk with cores.
#
# chkconfig: 345 01 99
# description: Setup core file rotation
#

# The following INIT INFO block is required to make the
# init scripts LSB compliant. Some of the fields are explained
# below (from http://wiki.debian.org/LSBInitScripts)
#
# Required-Start : defines the facilities that are required
#                  to start this service
# Should-Start   : defines the facilities that if present should be
#                  started, but the facility provided by this script
#                  should be able to start if these are missing.
# Required-Stop  : the service provided by this current facility should
#                  be stopped before stopping the listed facilities
# Should-Stop    : defines facilities that if present should be stopped
#                  after this service.

### BEGIN INIT INFO
# Provides:       rotatecore
# Required-Start: $syslog $local_fs
# Should-Start:   $syslog $local_fs
# Required-Stop:  $syslog $local_fs
# Should-Stop:
# Default-Start:  3
# Default-Stop:   0 1 2 4 5 6
# Short-Description: Core rotation service
# Description:    Core rotation service cores dumped under /var/core
### END INIT INFO

PROG_NAME=$(basename $0)

CONFIG_FILE="/etc/rotatecore.config"
LOCK_FILE="/var/run/rotatecore/lock"

PATTERN_PATH="/proc/sys/kernel/core_pattern"
ACTIVE_PATTERN="|/usr/sbin/rotatecore %e %p %t"

# Defaults
CORE_DIR="/var/core"
ROTATE=""
COMPRESS=""

INACTIVE_PATTERN="${CORE_DIR}/core-%e-%p-%t"

Log() {
   LEVEL=$1
   shift
   logger -p daemon.${LEVEL} -t "${PROG_NAME}" "$*"
}

GetConfigOptions() {
    LOG_CONFIG=$1

    # Use defaults if there's no config file.
    if [ ! -r "${CONFIG_FILE}" ]
    then
        if [ "$LOG_CONFIG" = "y" ]
        then
            LINE="No readable config file. Using defaults:"
            LINE="${LINE} ${CORE_DIR}, No Rotation, No Compression."
            Log info ${LINE}
        fi
        return
    fi

    . ${CONFIG_FILE}

    # Log the configuration, if necessary.
    if [ "$LOG_CONFIG" = "y" ]
    then
        LINE="Using config: Cores in ${CORE_DIR},"
        if [ -n "${ROTATE}" ]
        then
            LINE="${LINE} Rotate ${ROTATE},"
        else
            LINE="${LINE} No Rotation,"
        fi

        if [ -n "${COMPRESS}" ]
        then
            LINE="${LINE} Use Compression."
        else
            LINE="${LINE} No Compression."
        fi
        Log info "${LINE}"
    fi

    INACTIVE_PATTERN="${CORE_DIR}/core-%e-%p-%t"
}

RotatecoreStart(){
    GetConfigOptions y

    # Clean out stale lockfiles.
    rm -f ${LOCK_FILE}

    # Clean out any stale partial cores
    rm -f ${CORE_DIR}/.core*

    # Make cores go to the core rotation tool
    echo ${ACTIVE_PATTERN} > ${PATTERN_PATH}
    sleep 1

    Log info "Core dump rotation enabled."

    RETVAL=0
}

RotatecoreStop(){
    # Load options from the config file
    GetConfigOptions n

    # Don't rotate cores. Just drop them into the core directory.
    echo ${INACTIVE_PATTERN} > ${PATTERN_PATH}
    sleep 1

    LINE="Core dump rotation disabled. Cores in"
    LINE="${LINE} $(dirname ${INACTIVE_PATTERN})"
    Log info "${LINE}"

    RETVAL=0
}

RotatecoreStatus(){
    CURRENT_PATTERN=$(cat ${PATTERN_PATH})
    if [ "${CURRENT_PATTERN}" = "${ACTIVE_PATTERN}" ]
    then
        echo $"${base} is running..."
    else
        echo $"${base} is stopped..."
    fi
    RETVAL=0
}

RotatecoreUsage(){
    echo "$0 {start|stop|restart|status}"
    RETVAL=11
}

##
## Main
##

RETVAL=0

case "$1" in
    start)
        RotatecoreStart
	;;
    stop)
        RotatecoreStop
	;;
    restart)
        RotatecoreStop
        RotatecoreStart
	;;
    status)
        RotatecoreStatus
        ;;
    *)
        RotatecoreUsage
	;;
esac

exit $RETVAL

#eof
