#!/bin/sh
#
# @(#) src/util/init/strace.sh
# Copyright (c) 2008-2015  Monavacon Limited <http://www.monavacon.com/>
# Copyright (c) 2001-2008  OpenSS7 Corporation <http://www.openss7.com/>
# Copyright (c) 1997-2001  Brian F. G. Bidulock <bidulock@openss7.org>
# All Rights Reserved.
#
# Distributed by OpenSS7 Corporation.  See the bottom of this script for copying
# permissions.
#
# These are arguments to update-rc.d ala chkconfig and lsb.  They are recognized
# by openss7 install_initd and remove_initd scripts.  Each line specifies
# arguments to add and remove links after the the name argument:
#
# strace:	start and stop strace facility
# update-rc.d:	start 35 S . stop 35 0 6 .
# lockfile:	/var/lock/subsys/strace
# config:	/etc/default/strace
# processname:	strace
# pidfile:	/var/run/strace.pid
# probe:	true
# hide:		false
# license:	AGPL
# description:	This STREAMS init script is part of Linux Fast-STREAMS.  It is \
#		responsible for starting and stopping the STREAMS trace \
#		logger.  The STREAMS trace logger should only be run under \
#		exceptional circumstances and this init script not activated \
#		automatically.
#
# LSB init script conventions
#
### BEGIN INIT INFO
# Provides: strace
# Required-Start: streams
# Required-Stop: streams
# Should-Start: $syslog strerr
# Should-Stop: $syslog strerr
# Default-Start: S
# Default-Stop:
# X-UnitedLinux-Default-Enabled: no
# Short-Description: start and stop strace
# License: AGPL
# Description:	This STREAMS init script is part of Linux Fast-STREAMS.  It is
#	responsible for starting and stopping the STREAMS trace logger.  The
#	STREAMS trace logger should only be run under exceptional circumstances
#	and this init script is not activated in any run level by default.
### END INIT INFO

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# Source init script functions library.
init_mode="standalone"
  if [ -r /etc/init.d/functions   ] ; then . /etc/init.d/functions   ; init_mode="chkconfig" ;
elif [ -r /etc/rc.status          ] ; then . /etc/rc.status          ; init_mode="insserv"   ;
elif [ -r /lib/lsb/init-functions ] ; then . /lib/lsb/init-functions ; init_mode="lsb"       ;
elif [ -r /sbin/start-stop-daemon ] ; then                             init_mode="debian"    ;
fi

case "$init_mode" in
    (chkconfig)
	# RedHat's LSB support is not so good, use native functions first.
	daemon_start () {
	    local force= nicelevel= base=
	    while [ "$1" != "${1##[-+]}" ] ; do
		case $1 in
		    (-f) force="--force" ; shift 1 ;;
		    (-n) nicelevel="$2"  ; shift 2 ;;
		esac
	    done
	    base=`echo $1 | sed -e 's,.*/,,'`
	    echo -en "Starting daemon $base: "
	    daemon $force $nicelevel "$@"
	    RETVAL=$?
	    echo ""
	    return $RETVAL
	}
	daemon_stop () {
	    local base=
	    base=`echo $1 | sed -e 's,.*/,,'`; shift 1
	    echo -en "Stopping daemon $base: "
	    killproc $base "$@"
	    RETVAL=$?
	    echo ""
	    return $RETVAL
	}
	daemon_reload () {
	    local base=
	    base=`echo $1 | sed -e 's,.*/,,'`; shift 1
	    echo -en "Reloading daemon $base: "
	    killproc $base -HUP
	    RETVAL=$?
	    echo ""
	    return $RETVAL
	}
	;;
    (insserv)
	# SuSE's LSB support is not so good, use native functions first.
	daemon_start () {
	    local force= nicelevel= base=
	    while [ "$1" != "${1##[-+]}" ] ; do
		case $1 in
		    (-f) force="-f" ; shift 1 ;;
		    (-n) nicelevel="-n $2"  ; shift 2 ;;
		esac
	    done
	    base=`echo $1 | sed -e 's,.*/,,'`
	    echo -en "Starting daemon $base: "
	    startproc $force $nicelevel "$@"
	    RETVAL=$?
	    [ $RETVAL -eq 0 ] || rc_failed $RETVAL
	    rc_status -v
	    return $RETVAL
	}
	daemon_stop () {
	    local base=
	    base=`echo $1 | sed -e 's,.*/,,'`; shift 1
	    echo -en "Stopping daemon $base: "
	    killproc $base "$@"
	    RETVAL=$?
	    [ $RETVAL -eq 0 ] || rc_failed $RETVAL
	    rc_status -v
	    return $RETVAL
	}
	daemon_reload () {
	    local base=
	    base=`echo $1 | sed -e 's,.*/,,'`; shift 1
	    echo -en "Reloading daemon $base: "
	    killproc $base -HUP
	    RETVAL=$?
	    [ $RETVAL -eq 0 ] || rc_failed $RETVAL
	    rc_status -v
	    return $RETVAL
	}
	action () {
	    echo -en "$1"
	    shift 1
	    ${1+"$@"} >/dev/null
	    RETVAL=$?
	    [ $RETVAL -eq 0 ] || rc_failed $RETVAL
	    rc_status -v
	    return $RETVAL
	}
	;;
    (lsb)
	daemon_start () {
	    local force= nicelevel= base=
	    while [ "$1" != "${1##[-+]}" ] ; do
		case $1 in
		    (-f) force="-f" ; shift 1 ;;
		    (-n) nicelevel="-n $2"  ; shift 2 ;;
		esac
	    done
	    base=`echo $1 | sed -e 's,.*/,,'`
	    echo -en "Starting daemon $base: "
	    start_daemon $force $nicelevel "$@"
	    RETVAL=$?
	    [ $RETVAL -eq 0 ] && log_success_msg || log_failure_msg
	    return $RETVAL
	}
	daemon_stop () {
	    local base=
	    base=`echo $1 | sed -e 's,.*/,,'`; shift 1
	    echo -en "Stopping daemon $base: "
	    killproc $base "$@"
	    RETVAL=$?
	    [ $RETVAL -eq 0 ] && log_success_msg || log_failure_msg
	    return $RETVAL
	}
	daemon_reload () {
	    local base=
	    base=`echo $1 | sed -e 's,.*/,,'`; shift 1
	    echo -en "Reloading daemon $base: "
	    killproc $base -HUP
	    RETVAL=$?
	    [ $RETVAL -eq 0 ] && log_success_msg || log_failure_msg
	    return $RETVAL
	}
	action () {
	    echo -en "$1"
	    shift 1
	    ${1+"$@"} >/dev/null
	    RETVAL=$?
	    [ $RETVAL -eq 0 ] && log_success_msg || log_failure_msg
	    return $RETVAL
	}
	;;
    (debian)
	# a resonably modern debian system will have LSB support
	daemon_start () {
	    local force= nicelevel= base=
	    while [ "$1" != "${1##[+-]}" ] ; do
		case $1 in
		    (-f) force="" ; shift 1 ;;
		    (-n) nicelevel="-N $2"  ; shift 2 ;;
		esac
	    done
	    base=`echo $1 | sed -e 's,.*/,,'`
	    echo -en "Starting daemon $base: "
	    start-stop-daemon --start $force $nicelevel --quiet \
		--pidfile $pidfile --exec $execfile -- "$@"
	    RETVAL=$?
	    [ $RETVAL -eq 0 ] || echo "(failed)"; echo "."
	    return $RETVAL
	}
	daemon_stop () {
	    local base=
	    base=`echo $1 | sed -e 's,.*/,,'`; shift 1
	    echo -en "Stopping daemon $base: "
	    start-stop-daemon --stop --quiet --retry=1 --oknodo \
		--pidfile $pidfile --exec $execfile
	    RETVAL=$?
	      if [ $RETVAL -eq   0 ] ; then echo "."
	    elif [ $RETVAL -eq 255 ] ; then echo "(warning)."
	    else                            echo "failed!"
	    fi
	    return $RETVAL
	}
	daemon_reload () {
	    local base=
	    base=`echo $1 | sed -e 's,.*/,,'`; shift 1
	    echo -en "Reloading daemon $base: "
	    killproc $base -HUP
	    RETVAL=$?
	    [ $RETVAL -eq 0 ] || echo "(failed)"; echo "."
	    return $RETVAL
	}
	action () {
	    echo -en "$1"
	    shift 1
	    eval "\${1+\"\$@\"} $redir"
	    RETVAL=$?
	    [ $RETVAL -eq 0 ] || echo "(failed)"; echo "."
	    return $RETVAL
	}
	;;
    (standalone|*)
	if [ -x /sbin/pidof ] ; then
	    pidofproc () {
		/sbin/pidof $@
	    }
	elif [ -x /sbin/pidofproc ] ; then
	    pidofproc () {
		/sbin/pidofproc $@
	    }
	else
	    pidofproc () {
		[ -r $pidfile ] && cat $pidfile
	    }
	fi
	if [ -x /sbin/start-stop-daemon ] ; then
	    killproc () {
		local base execfile
		base=$1
		execfile=$2
		shift 2
		start-stop-daemon --stop --quiet --retry=1 --oknodo \
		    --pidfile $pidfile \
		    --exec $execfile
	    }
	elif [ -x /sbin/killproc ] ; then
	    killproc () {
		/sbin/killproc $@
	    }
	elif [ -x /usr/bin/killall ] ; then
	    killproc () {
		/usr/bin/killall $@
	    }
	else
	    killproc () {
		[ -r $pidfile ] && kill "$2" `cat $pidfile`
	    }
	fi
	daemon_start () {
	    local force= nicecmd= base=
	    while [ "$1" != "${1##[-+]}" ] ; do
		case $1 in
		    (-f) force="--force"       ; shift 1 ;;
		    (-n) nicecmd="nice -n $2"  ; shift 2 ;;
		esac
	    done
	    base=`echo $1 | sed -e 's,.*/,,'`
	    echo -en "Starting daemon $base: "
	    eval "( $nicecmd \"\$@\" >/dev/null 2>&1 </dev/null & )"
	    RETVAL=$?
	    [ $RETVAL -eq 0 ] && echo -en "\t...SUCCESS" || echo -en "\t....FAILED"
	    return $RETVAL
	}
	daemon_stop () {
	    local base=
	    base=`echo $1 | sed -e 's,.*/,,'`; shift 1
	    echo -en "Stopping daemon $base: "
	    if pidofproc $base >/dev/null 2>&1 ; then
		RETVAL=0
	    else
		killproc $base "$@"
		RETVAL=$?
	    fi
	    [ $RETVAL -eq 0 ] && log_success_msg || log_failure_msg
	    return $RETVAL
	}
	daemon_reload () {
	    local base=
	    base=`echo $1 | sed -e 's,.*/,,'`; shift 1
	    echo -en "Reloading daemon $base: "
	    killproc $base -HUP
	    RETVAL=$?
	    [ $RETVAL -eq 0 ] && echo -en "\t...SUCCESS" || echo -en "\t....FAILED"
	    return $RETVAL
	}
	action () {
	    echo -en "$1"
	    shift 1
	    ${1+"$@"} >/dev/null
	    RETVAL=$?
	    [ $RETVAL -eq 0 ] && echo -e "\t...SUCCESS" || echo -e "\t....FAILED"
	    return $?
	}
	;;
esac

name='strace'
script='strace.sh'
ucname="STRACE"
lockfile="/var/lock/subsys/$name"
config="/etc/sysconfig/$name /etc/default/$name"
processname="$name"
pidfile="/var/run/$processname.pid"
execfile="/usr/sbin/$processname"
desc="the STREAMS trace logger"

[ -x $execfile -o "$1" = "stop" ] || exit 5

# Specify defaults

STRACEOPTIONS=
STRACE_DIRECTORY="/var/log/streams"
STRACE_BASENAME="trace"
STRACE_OUTFILE=
STRACE_ERRFILE=
STRACE_LOGDEVICE="/dev/streams/clone/log"
STRACE_MODULES=
STRACE_OPTIONS=

# Source redhat and/or debian config file
for file in $config ; do
    [ -f $file ] && . $file
done

RETVAL=0

umask 077

case ":$VERBOSE" in
    :no|:NO|:false|:FALSE|:0|:)
	redir='>/dev/null 2>&1'
	;;
    *)
	redir=
	;;
esac

build_options () {
    # Build up the options string
    STRACE_OPTIONS="-n -p $pidfile"
    [ -n "$STRACEOPTIONS" ] && \
	STRACE_OPTIONS="${STRACE_OPTIONS}${STRACE_OPTIONS:+ }${STRACEOPTIONS}"
    [ -n "$STRACE_DIRECTORY" ] && \
	STRACE_OPTIONS="${STRACE_OPTIONS}${STRACE_OPTIONS:+ }-d ${STRACE_DIRECTORY}"
    [ -n "$STRACE_BASENAME" ] && \
	STRACE_OPTIONS="${STRACE_OPTIONS}${STRACE_OPTIONS:+ }-b ${STRACE_BASENAME}"
    [ -n "$STRACE_OUTFILE" ] && \
	STRACE_OPTIONS="${STRACE_OPTIONS}${STRACE_OPTIONS:+ }-o ${STRACE_OUTFILE}"
    [ -n "$STRACE_ERRFILE" ] && \
	STRACE_OPTIONS="${STRACE_OPTIONS}${STRACE_OPTIONS:+ }-e ${STRACE_ERRFILE}"
    [ -n "$STRACE_LOGDEVICE" ] && \
	STRACE_OPTIONS="${STRACE_OPTIONS}${STRACE_OPTIONS:+ }-l ${STRACE_LOGDEVICE}"
    [ -n "$STRACE_MODULES" ] && \
	STRACE_OPTIONS="${STRACE_OPTIONS}${STRACE_OPTIONS:+ }${STRACE_MODULES}"
}

start () {
    [ -d "$STRACE_DIRECTORY" -o -z "$STRACE_DIRECTORY" ] || \
	action $"Creating $STRACE_DIRECTORY: " \
	    mkdir -p -- "$STRACE_DIRECTORY" \
	    || RETVAL=$?
    build_options
    daemon_start $execfile $STRACE_OPTIONS \
	|| RETVAL=$?
    [ $RETVAL -eq 0 ] && touch $lockfile
    return $RETVAL
}

stop () {
    if [ -r $pidfile ]; then
	daemon_stop $execfile || RETVAL=$?
    else
	RETVAL=0
    fi
    [ $RETVAL -eq 0 ] && rm -f -- $lockfile
    return $RETVAL
}

restart () {
    stop  || RETVAL=$?
    start || RETVAL=$?
    return $RETVAL
}

reload () {
    daemon_reload $execfile || RETVAL=$?
    return $RETVAL
}

show () {
    echo "$script: show: not yet implemented." >&2
    RETVAL=1
    return $RETVAL
}

usage () {
    echo "Usage: $script (start|stop|status|restart|try-restart|condrestart|force-reload|reload|probe|show)" >&2
    RETVAL=1
    return $RETVAL
}

case "$1" in
    (start|stop|reload|restart|show)
	$1 || RETVAL=$?
	;;
    (force-reload)
	reload || RETVAL=$?
	;;
    (status)
	$1 $name || RETVAL=$?
	;;
    (try-restart|condrestart)
	[ -f $lockfile ] && restart || RETVAL=$?
	;;
    (probe)
	if pidofproc $processname >/dev/null 2>&1 ; then
	    # running
	    if [ "`pidofproc $processname`" -eq "`cat $pidfile`" ] ; then
		# running with the right pid
		if [ ! -f $lockfile ] ; then
		    # running but subsystem unlocked, need to reload
		    echo 'reload'
		else
		    # subsystem locked
		    for file in $config ; do
			if [ -f $file -a $file -nt $lockfile ] ; then
			    # configuration file updated, need to reload
			    echo 'reload'
			    break
			fi
		    done
		fi
	    else
		# running, but with the wrong pid, need to restart
		echo 'restart'
	    fi
	else
	    # not running
	    if [ ! -f $lockfile ] ; then
		# subsystem unlocked, need to start
		echo 'start'
	    else
		# dead but subsystem locked, need to restart
		echo 'restart'
	    fi
	fi
	# do not need to do anything
	RETVAL=$?
	;;
    (*)
	usage || RETVAL=$?
	;;
esac

[ "${0##*/}" = "$script" ] && exit $RETVAL

# =============================================================================
# 
# @(#) src/util/init/strace.sh
#
# -----------------------------------------------------------------------------
#
# Copyright (c) 2008-2015  Monavacon Limited <http://www.monavacon.com/>
# Copyright (c) 2001-2008  OpenSS7 Corporation <http://www.openss7.com/>
# Copyright (c) 1997-2001  Brian F. G. Bidulock <bidulock@openss7.org>
#
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation; version 3 of the License.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program.  If not, see <http://www.gnu.org/licenses/>, or write to
# the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# -----------------------------------------------------------------------------
#
# U.S. GOVERNMENT RESTRICTED RIGHTS.  If you are licensing this Software on
# behalf of the U.S. Government ("Government"), the following provisions apply
# to you.  If the Software is supplied by the Department of Defense ("DoD"), it
# is classified as "Commercial Computer Software" under paragraph 252.227-7014
# of the DoD Supplement to the Federal Acquisition Regulations ("DFARS") (or any
# successor regulations) and the Government is acquiring only the license rights
# granted herein (the license rights customarily provided to non-Government
# users).  If the Software is supplied to any unit or agency of the Government
# other than DoD, it is classified as "Restricted Computer Software" and the
# Government's rights in the Software are defined in paragraph 52.227-19 of the
# Federal Acquisition Regulations ("FAR") (or any successor regulations) or, in
# the cases of NASA, in paragraph 18.52.227-86 of the NASA Supplement to the FAR
# (or any successor regulations).
#
# -----------------------------------------------------------------------------
#
# Commercial licensing and support of this software is available from OpenSS7
# Corporation at a fee.  See http://www.openss7.com/
#
# =============================================================================

# vim: ft=sh sw=4 tw=80
