#!/bin/sh
###############################################################################
# $Id: munge.init.in 705 2007-02-04 07:52:37Z dun $
###############################################################################
# Copyright (C) 2002-2007 The Regents of the University of California.
# Produced at Lawrence Livermore National Laboratory.
# Written by Chris Dunlap <cdunlap@llnl.gov>.
# UCRL-CODE-155910.
###############################################################################
# chkconfig:      - 40 60
###############################################################################
### BEGIN INIT INFO
# Provides:       munge
# Required-Start: $named $time
# Required-Stop:  $named $time
# Should-Start:   $local_fs $syslog
# Should-Stop:    $local_fs $syslog
# Default-Stop:   0 1 2 6
# Short-Description: Start/Stop the MUNGE authentication service.
# Description:    Start/Stop the MUNGE authentication service.
### END INIT INFO
###############################################################################

unset DESC DAEMON CONFIG OPTIONS PIDFILE NICE USER SIGHUP_RELOAD

prefix="/usr"
exec_prefix="/usr"
sbindir="/usr/sbin"
sysconfdir="/etc"
localstatedir="/var"

DESC="MUNGE"
DAEMON="$sbindir/munged"
#CONFIG=#_NOT_SUPPORTED_#
#OPTIONS=
PIDFILE="$localstatedir/run/munge/munged.pid"
#NICE=
USER="daemon"
#SIGHUP_RELOAD=#_NOT_SUPPORTED_#

###############################################################################

PATH=/sbin:/usr/sbin:/usr/local/sbin:/bin:/usr/bin:/usr/local/bin
DAEMON_NAME="`basename \"$DAEMON\"`"
SERVICE_NAME="munge"
SIGTERM_TIMEOUT="3"

# Read configuration defaults to override variables:
#   $CONFIG, $OPTIONS, $PIDFILE, $USER, $NICE, $SIGHUP_RELOAD
##
for dir in "$sysconfdir/default" "$sysconfdir/sysconfig"; do
  [ -r "$dir/$SERVICE_NAME" ] && . "$dir/$SERVICE_NAME"
done

# Exit if the package has been removed.
##
[ -x "$DAEMON" ] || exit 5                      # program not installed

# Exit if the configuration has been removed.
##
[ -z "$CONFIG" -o -r "$CONFIG" ] || exit 6      # program not configured

###############################################################################

service_init ()
{
# Determine the system type and initialize the environment.
#
# Note that the shell positional parameters must be preserved when calling
#   this function in order for SuSE to initialize its environment properly.
##
  STATUS=0
  [ -n "$SIGHUP_RELOAD" -a "$SIGHUP_RELOAD" != 0 ] \
    && RELOAD=1 || unset RELOAD
  expr -- "$NICE" : '[0-9]*$' >/dev/null 2>&1 && NICE="+$NICE"
  [ "`id | sed 's/^uid=\([0-9]*\).*/\1/'`" -ne 0 ] && unset USER

  if [ -f /etc/debian_version -a -x /sbin/start-stop-daemon ]; then
    SYSTEM="DEBIAN"
  elif [ -f /etc/redhat-release -a -r /etc/init.d/functions ]; then
    SYSTEM="REDHAT"
    . /etc/init.d/functions
    RH_SUBSYS_BASE="`basename \"$0\" | sed 's/^[SK][0-9][0-9]*//'`"
    RH_SUBSYS="/var/lock/subsys/$RH_SUBSYS_BASE"
  elif [ -f /etc/SuSE-release -a -r /etc/rc.status ]; then
    SYSTEM="SUSE"
    . /etc/rc.status
    rc_reset
  elif [ -r /lib/lsb/init-functions ]; then
    SYSTEM="LSB"
    . /lib/lsb/init-functions
  else
    SYSTEM="OTHER"
  fi
}

service_fini ()
{
# Return the exit status.
##
  case $SYSTEM in
    SUSE)
      rc_exit
      ;;
    DEBIAN|REDHAT|LSB|*)
      exit $STATUS
      ;;
  esac
}

service_start ()
{
# Start the service.
# Required by LSB.
#
# According to LSB, running "start" on a service already running should be
#   considered successful.
##
  # If there is no key then exit as a configuration error.
  if [ ! -f /etc/munge/munge.key ] ; then
    echo "/etc/munge/munge.key does not exist, generate with create-munge-key"
    exit 6
  fi 


  printf "Starting $DESC: "
  case $SYSTEM in
    DEBIAN)
      printf "$DAEMON_NAME"
      if $0 status >/dev/null 2>&1; then
        STATUS=0
      else
        ERROR=`start-stop-daemon --start --quiet \
          ${NICE:+"--nicelevel"} ${NICE:+"$NICE"} \
          ${USER:+"--chuid"} ${USER:+"$USER"} \
          ${PIDFILE:+"--pidfile"} ${PIDFILE:+"$PIDFILE"} \
          --exec "$DAEMON" -- $OPTIONS 2>&1`
        STATUS=$?
      fi
      [ $STATUS -eq 0 ] && echo "." || echo " (failed)."
      [ $STATUS -ne 0 -a -n "$ERROR" ] && echo "$ERROR"
      ;;
    REDHAT)
      if $0 status >/dev/null 2>&1; then
        STATUS=0
      else
        daemon ${NICE:+"$NICE"} ${USER:+"--user"} ${USER:+"$USER"} \
          "$DAEMON" $OPTIONS
        STATUS=$?
      fi
      echo
      [ $STATUS -eq 0 ] && touch "$RH_SUBSYS" >/dev/null 2>&1
      ;;
    SUSE)
      ERROR=`startproc ${NICE:+"-n"} ${NICE:+"$NICE"} \
        ${USER:+"-u"} ${USER:+"$USER"} \
        ${PIDFILE:+"-p"} ${PIDFILE:+"$PIDFILE"} \
        "$DAEMON" $OPTIONS 2>&1`
      rc_status -v
      STATUS=$?
      [ $STATUS -ne 0 -a -n "$ERROR" ] && echo "$ERROR"
      ;;
    LSB)
      if [ -n "$USER" ]; then
        ERROR=`su "$USER" -c "/sbin/start_daemon \
          ${NICE:+\"-n\"} ${NICE:+\"$NICE\"} \
          ${PIDFILE:+\"-p\"} ${PIDFILE:+\"$PIDFILE\"} \
          \"$DAEMON\" $OPTIONS" 2>&1`
      else
        ERROR=`start_daemon ${NICE:+"-n"} ${NICE:+"$NICE"} \
          ${PIDFILE:+"-p"} ${PIDFILE:+"$PIDFILE"} "$DAEMON" $OPTIONS 2>&1`
      fi
      STATUS=$?
      [ $STATUS -eq 0 ] && log_success_msg || log_failure_msg
      [ $STATUS -ne 0 -a -n "$ERROR" ] && echo "$ERROR"
      ;;
    *)
      pids=`query_pids "$DAEMON" "$PIDFILE"`
      rc=$?
      if [ $rc -eq 0 -a -n "$pids" ]; then
        STATUS=0
      else
        [ -n "$NICE" ] && nice="nice -n $NICE"
        if [ -n "$USER" ]; then
          ERROR=`su "$USER" -c "$nice \"$DAEMON\" $OPTIONS" 2>&1`
        else
          ERROR=`$nice "$DAEMON" $OPTIONS 2>&1`
        fi
        STATUS=$?
      fi
      [ $STATUS -eq 0 ] && echo "success" || echo "failed"
      [ $STATUS -ne 0 -a -n "$ERROR" ] && echo "$ERROR"
      ;;
  esac
}

service_stop ()
{
# Stop the service.
# Required by LSB.
#
# According to LSB, running "stop" on a service already stopped or not running
#   should be considered successful.
##
  printf "Stopping $DESC: "
  case $SYSTEM in
    DEBIAN)
      printf "$DAEMON_NAME"
      if ! $0 status >/dev/null 2>&1; then
        STATUS=0
      else
        start-stop-daemon --stop --quiet \
          ${PIDFILE:+"--pidfile"} ${PIDFILE:+"$PIDFILE"} \
          --name "$DAEMON_NAME" ${SIGTERM_TIMEOUT:+"--retry"} \
          ${SIGTERM_TIMEOUT:+"$SIGTERM_TIMEOUT"} >/dev/null 2>&1
        STATUS=$?
      fi
      [ $STATUS -eq 0 ] && echo "." || echo " (failed)."
      ;;
    REDHAT)
      if ! $0 status >/dev/null 2>&1; then
        STATUS=0
      else
        killproc "$DAEMON"
        STATUS=$?
      fi
      echo
      [ $STATUS -eq 0 ] && rm -f "$RH_SUBSYS" >/dev/null 2>&1
      ;;
    SUSE)
      killproc ${PIDFILE:+"-p"} ${PIDFILE:+"$PIDFILE"} \
        ${SIGTERM_TIMEOUT:+"-t"} ${SIGTERM_TIMEOUT:+"$SIGTERM_TIMEOUT"} \
        "$DAEMON"
      rc_status -v
      ;;
    LSB)
      killproc ${PIDFILE:+"-p"} ${PIDFILE:+"$PIDFILE"} "$DAEMON"
      STATUS=$?
      [ $STATUS -eq 0 ] && log_success_msg || log_failure_msg
      ;;
    *)
      signal_process "$DAEMON"
      rc=$?
      [ $rc -eq 0 -o $rc -eq 2 ] && STATUS=0 || STATUS=1
      [ $STATUS -eq 0 ] && echo "success" || echo "failed"
      ;;
  esac
}

service_restart ()
{
# Stop and restart the service if it is already running;
#   otherwise, start the service.
# Required by LSB.
#
# According to LSB, running "restart" on a service already stopped or not
#   running should be considered successful.
##
  if $0 status >/dev/null 2>&1; then
    $0 stop && $0 start
  else
    $0 start
  fi

  case $SYSTEM in
    SUSE)
      rc_status
      ;;
    DEBIAN|REDHAT|LSB|*)
      STATUS=$?
      ;;
  esac
}

service_try_restart ()
{
# Restart the service if it is already running.
# Optional for LSB.  Also known as "condrestart" by RedHat.
#
# According to LSB, running "try-restart" on a service already stopped or not
#   running should be considered successful.
##
  case $SYSTEM in
    REDHAT)
      [ -f "$RH_SUBSYS" ] && $0 restart || :
      STATUS=$?
      ;;
    SUSE)
      $0 status >/dev/null 2>&1 && $0 restart || rc_reset
      rc_status
      ;;
    DEBIAN|LSB|*)
      $0 status >/dev/null 2>&1 && $0 restart || :
      STATUS=$?
      ;;
  esac
}

service_reload ()
{
# Reload the configuration without stopping and restarting the service.
# Optional for LSB.
##
  [ -z "$RELOAD" ] && STATUS=3          # unimplemented feature

  printf "Reloading $DESC: "
  case $SYSTEM in
    DEBIAN)
      printf "$DAEMON_NAME"
      if [ -n "$RELOAD" ]; then
        start-stop-daemon --stop --quiet --signal HUP \
          ${PIDFILE:+"--pidfile"} ${PIDFILE:+"$PIDFILE"} \
          --name "$DAEMON_NAME" >/dev/null 2>&1
        STATUS=$?
      fi
      [ $STATUS -eq 0 ] && echo "." || echo " (failed)."
      ;;
    REDHAT)
      if [ -n "$RELOAD" ]; then
        killproc "$DAEMON" -HUP
        STATUS=$?
      else
        echo_failure
      fi
      echo
      ;;
    SUSE)
      if [ -n "$RELOAD" ]; then
        killproc -HUP ${PIDFILE:+"-p"} ${PIDFILE:+"$PIDFILE"} "$DAEMON"
      else
        rc_failed $STATUS
      fi
      rc_status -v
      ;;
    LSB)
      if [ -n "$RELOAD" ]; then
        killproc ${PIDFILE:+"-p"} ${PIDFILE:+"$PIDFILE"} "$DAEMON" -HUP
        STATUS=$?
      fi
      [ $STATUS -eq 0 ] && log_success_msg || log_failure_msg
      ;;
    *)
      if [ -n "$RELOAD" ]; then
        signal_process "$DAEMON" "HUP"
        STATUS=$?
      fi
      [ $STATUS -eq 0 ] && echo "success" || echo "failed"
      ;;
  esac
}

service_force_reload ()
{
# Reload the configuration if the service supports this;
#   otherwise, restart the service if it is already running.
# Required by LSB.
#
# According to LSB, running "force-reload" on a service already stopped or
#   not running should be considered successful.
##
  if [ -n "$RELOAD" ]; then
    $0 reload
  else
    $0 try-restart
  fi

  case $SYSTEM in
    SUSE)
      rc_status
      ;;
    DEBIAN|REDHAT|LSB|*)
      STATUS=$?
      ;;
  esac
}

service_status ()
{
# Print the current status of the service.
# Required by LSB.
##
  case $SYSTEM in
    REDHAT)
      status "$DAEMON"
      STATUS=$?
      ;;
    SUSE)
      printf "Checking for $DESC: "
      checkproc ${PIDFILE:+"-p"} ${PIDFILE:+"$PIDFILE"} "$DAEMON"
      rc_status -v
      ;;
    LSB)
      pids=`pidofproc ${PIDFILE:+"-p"} ${PIDFILE:+"$PIDFILE"} \
        "$DAEMON" 2>/dev/null`
      STATUS=$?
      if [ $STATUS -eq 0 -a -n "$pids" ]; then
        echo "$DAEMON_NAME (pid $pids) is running"
      else
        echo "$DAEMON_NAME is stopped"
      fi
      ;;
    DEBIAN|*)
      pids=`query_pids "$DAEMON" "$PIDFILE"`
      rc=$?
      if [ $rc -eq 0 -a -n "$pids" ]; then
        echo "$DAEMON_NAME (pid $pids) is running"
        STATUS=0
      elif [ $rc -ne 0 ]; then
        echo "$DAEMON_NAME is stopped"
        STATUS=3                        # program is not running
      else
        echo "$DAEMON_NAME status is unknown"
        STATUS=4                        # program status unknown
      fi
      ;;
  esac
}

query_pids ()
{
# Writes the matching PIDs to stdout.
# Returns 0 on success (ie, pids found).
##
  PROCNAME="$1"
  PIDFILE="$2"

  if query_exec pgrep; then
    pids=`pgrep -d ' ' -x "\`basename \"$PROCNAME\"\`" 2>/dev/null`
    rc=$?
  elif query_exec pidof; then
    pids=`pidof -o $$ -x "$PROCNAME" 2>/dev/null`
    rc=$?
  else
    pids=`(ps awx -o pid -o command || ps -e -f -o pid -o args) 2>/dev/null \
      | tail +2 | egrep "( |/)$PROCNAME( |$)" | grep -v egrep \
      | sed 's/ *\([0-9]*\).*/\1/' | sort -n | tr '\012' ' '`
    [ -n "$pids" ] && rc=0 || rc=1
  fi

  unset pids_running
  if [ -n "$pids" -a -r "$PIDFILE" ]; then
    read pid_line < "$PIDFILE"
    for pid in $pid_line; do
      expr -- "$pid" : '[0-9]*$' >/dev/null 2>&1 \
        && expr -- " $pids " : ".* $pid .*" >/dev/null 2>&1 \
        && pids_running="$pids_running $pid"
    done
    [ -n "$pids_running" ] && pids=$pids_running
  fi

  echo $pids
  return $rc
}

query_exec ()
{
# Returns 0 if EXECNAME is an executable file within the PATH.
##
  EXECNAME="$1"
  rc=1

  IFS_BAK="$IFS"
  IFS=:
  for dir in $PATH; do
    [ -x "$dir/$EXECNAME" ] || continue
    rc=0
    break
  done
  IFS="$IFS_BAK"
  return $rc
}

signal_process ()
{
# Returns 0 on success, 1 if kill failed, 2 if PROCNAME is not running.
##
  PROCNAME="$1"
  SIGNUM="$2"

  pids=`query_pids "$DAEMON" "$PIDFILE"`
  [ $? -ne 0 -o -z "$pids" ] && return 2

  kill ${SIGNUM:+"-$SIGNUM"} $pids >/dev/null 2>&1
  [ $? -ne 0 ] && return 1
  [ -n "$SIGNUM" ] && return 0

  pids=`query_pids "$DAEMON" "$PIDFILE"`
  [ $? -ne 0 -o -z "$pids" ] && return 0
  [ -z "$SIGTERM_TIMEOUT" ] && return 1

  sleep "$SIGTERM_TIMEOUT"
  kill -KILL $pids >/dev/null 2>&1
  pids=`query_pids "$DAEMON" "$PIDFILE"`
  [ $? -ne 0 -o -z "$pids" ] && return 0
  return 1
}

###############################################################################

service_init "$@"

case "$1" in
  start)
    service_start
    ;;
  stop)
    service_stop
    ;;
  restart)
    service_restart
    ;;
  try-restart|condrestart)
    service_try_restart
    ;;
  reload)
    service_reload
    ;;
  force-reload)
    service_force_reload
    ;;
  status)
    service_status
    ;;
  *)
    COMMANDS="start|stop|restart|try-restart|reload|force-reload|status"
    echo "Usage: `basename \"$0\"` ($COMMANDS)" >&2
    exit 2                              # invalid or excess argument(s)
    ;;
esac

service_fini
