#! /bin/bash
# chkconfig: 2345 16 84
# description: Start/Stop script for Distributed SA
#
### BEGIN INIT INFO
# Provides:       dist_sa
# Required-Start: openibd
# Required-Stop:  openibd
# Default-Start:  2 3 5
# Default-Stop:	  0 1 2 6
### END INIT INFO
#
# Start, Stop, Restart the Distributed SA service
#
# Handles loading, unloading of the Distributed SA Service
# INTEL CONFIDENTIAL
# Copyright (c) 2008-2012  Intel Corporation All Rights Reserved.
# The source code contained or described herein and all documents related
# to the source code ("Material") are owned by Intel Corporation or its
# suppliers or licensors. Title to the Material remains with Intel Corporation
# or its suppliers and licensors. The Material contains trade secrets and
# proprietary and confidential information of Intel or its suppliers and
# licensors. The Material is protected by worldwide copyright and trade
# secret laws and treaty provisions. No part of the Material may be used,
# copied, reproduced, modified, published, uploaded, posted, transmitted,
# distributed, or disclosed in any way without Intel's prior express
# written permission.
#
# No license under any patent, copyright, trade secret or other intellectual
# property right is granted to or conferred upon you by disclosure or delivery
# of the Materials, either expressly, by implication, inducement, estoppel or
# otherwise. Any license under such intellectual property rights must be
# express and approved by Intel in writing.

# This software is available to you under a choice of one of two
# licenses.  You may choose to be licensed under the terms of the GNU
# General Public License (GPL) Version 2, available from the file
# COPYING in the main directory of this source tree, or the
# OpenIB.org BSD license below:
#
#     Redistribution and use in source and binary forms, with or
#     without modification, are permitted provided that the following
#     conditions are met:
#
#      - Redistributions of source code must retain the above
#        copyright notice, this list of conditions and the following
#        disclaimer.
#
#      - Redistributions in binary form must reproduce the above
#        copyright notice, this list of conditions and the following
#        disclaimer in the documentation and/or other materials
#        provided with the distribution.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

# [ICS VERSION STRING: @(#) ./config/init_dist_sa 7_4_3_0_2 [01/26/18 09:02]

IBA_SA_PATH=$PATH:/sbin:/usr/bin
if [ -e /etc/profile.d/ofed.sh ]; then
        . /etc/profile.d/ofed.sh
fi

CONFIG_FILE=/etc/sysconfig/iba/dist_sa.conf

# If RunDir has been set, make sure the run directory exists.
RUN_DIR=`grep -ve "^#" $CONFIG_FILE | grep RunDir | cut -d= -f2`
if [ ! -z "$RUN_DIR" ]; then
	DAEMON_COREFILE_LIMIT="unlimited"
	mkdir -p $RUN_DIR
fi

if [ -f /etc/init.d/functions ]; then
    . /etc/init.d/functions
elif [ -f /etc/rc.d/init.d/functions ] ; then
    . /etc/rc.d/init.d/functions
elif [[ -e /sbin/start_daemon && -e /sbin/killproc ]]; then
#This case is for SLES systems where we implement the functions
#available in RHEL systems using the commands available on SLES.
	daemon()
    {
       path=`which $1`;
       dir=`dirname $path`;
	   dir=$dir"/";
	   cmd=$dir${1+"$@"};

	   if [ ! -z "$DAEMON_COREFILE_LIMIT" ]; then 
            ulimit -S -c $DAEMON_COREFILE_LIMIT;
       fi

       /sbin/start_daemon $cmd
	   ret=$?
	   if [ $ret -eq 0 ]; then
			echo_success
       else
			echo_failure
	   fi
	   return $ret
	}

	killproc()
	{
	   path=`which $1`;
	   dir=`dirname $path`;
	   dir=$dir"/";
	   cmd=$dir${1+"$@"};
	   /sbin/killproc $cmd 
	   ret=$?
	   if [ $ret -eq 0 ]; then
			echo_success
       else
			echo_failure
	   fi
	   return $ret
	}

#Implementing the subsys file part of RHEL status function
	status()
	{
		echo $1
		if [ -f /var/lock/subsys/$1 ]; then
	        return 2
    	fi
	}
else
	echo "Supporting tools for starting daemon not present. Distributed SA daemon will not be started"
fi

# /etc/init.d/functions which has been sourced in previous step modifies the
# shell's PATH variable, which is why the Distributed SA binary placed at a 
# location other than /sbin:/usr/sbin/ are not found.  Hence, the Distributed SA
# retains the PATH variable set by openibd script.  We cannot do away with 
# /etc/init.d/functions because "daemon" and "killproc" are obtained from that file.

PATH=$IBA_SA_PATH

if [ -z "${BOOTUP:-}" ]; then
  if [ -f /etc/sysconfig/init ]; then
      . /etc/sysconfig/init
  else
    # This all seem confusing? Look in /etc/sysconfig/init,
    # or in /usr/doc/initscripts-*/sysconfig.txt
    BOOTUP=color
    RES_COL=60
    MOVE_TO_COL="echo -en \\033[${RES_COL}G"
    SETCOLOR_SUCCESS="echo -en \\033[1;32m"
    SETCOLOR_FAILURE="echo -en \\033[1;31m"
    SETCOLOR_WARNING="echo -en \\033[1;33m"
    SETCOLOR_NORMAL="echo -en \\033[0;39m"
    LOGLEVEL=1
  fi
  if [ "$CONSOLETYPE" = "serial" ]; then
      BOOTUP=serial
      MOVE_TO_COL=
      SETCOLOR_SUCCESS=
      SETCOLOR_FAILURE=
      SETCOLOR_WARNING=
      SETCOLOR_NORMAL=
  fi
fi

if [ "${BOOTUP:-}" != "verbose" ]; then
   INITLOG_ARGS="-q"
else
   INITLOG_ARGS=
fi


test -f  || exit 0

echo_success() {
  echo -n $@
  [ "$BOOTUP" = "color" ] && $MOVE_TO_COL
  echo -n "[  "
  [ "$BOOTUP" = "color" ] && $SETCOLOR_SUCCESS
  echo -n $"OK"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo -n "  ]"
  echo -e "\r"
  return 0
}

echo_failure() {
  echo -n $@
  [ "$BOOTUP" = "color" ] && $MOVE_TO_COL
  echo -n "["
  [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE
  echo -n $"FAILED"
  [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL
  echo -n "]"
  echo -e "\r"
  return 1
}

echo_log() {
	logger "$@" -p user.err
	echo "Please refer to /var/log/messages"	
}

print_status()
{
	if [ $1 -eq 0 ]
	then
		echo_log "Success"
	else
		echo_log "Failure"
	fi
	echo_log ""
}

function set_environment()
{
	if [  -f /etc/init.d/openibd  ]
	then
		return 0
	fi

	echo "Your InfiniBand Stack has not properly been installed"
	return 1
}

function start()
{
    echo -n "Starting Distributed SA Service"
	touch /var/lock/subsys/iba_dsc
	
	if [  -f $CONFIG_FILE  ]
	then
		daemon iba_dsc -f $CONFIG_FILE Daemon=yes
	else
		daemon iba_dsc Daemon=yes
	fi
	res=$?

	if [ $res != 0 ]; then
	        echo "Could not start Distributed SA Service"
	        echo_log "Try starting service manually by running command"
	else
	        touch /var/lock/subsys/iba_dsc
	fi
}

function stop()
{
    echo -n "Stopping Distributed SA Service"
    killproc iba_dsc
    res=$?
    status iba_dsc >/dev/null 2>&1 || rm -f /var/lock/subsys/iba_dsc
}

function restart()
{
	stop
	start
}

iba_dsc_running()
{
	echo "Distributed SA (pid $1) is running"
}

iba_dsc_not_running()
{
	echo "Distributed SA is not running"
}

running_status() {
    pid=`cat "/var/run/ib_iba_dsc.pid" 2>/dev/null`
    if [ "$pid" = '' ]
    then
        iba_dsc_not_running
        return
    fi

    ps -p $pid | grep $pid > /dev/null 2>&1
    ret=$?
    if [ $ret -eq 0 ]
    then
        iba_dsc_running $pid
        return
    fi

    iba_dsc_not_running
}

set_environment
case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        restart
        ;;
    status)
        running_status
        ;;
    *)
       echo $"Usage: $0 {start|stop|restart|status}"
       exit 1
esac

exit 0
