Skip to content
Snippets Groups Projects
Commit 2fbf3e88 authored by Walter Doekes's avatar Walter Doekes
Browse files

safe_asterisk: Cleanup and debian compatibility.

Cleans up the safe_asterisk script and adds the ASTSAFE_FOREGROUND
option that allows the debian asterisk init script to capture the
right pid.

* Drop the vim #modeline which wasn't used. Use test consistently
  without the odd configure xno syntax. Double quote all paths.
  General cleanup.
* Don't output message()s to the console but only to TTY if set.
* Allow TTY to be "no" as well as empty (debian compatibility with
  debian/patches/safe_asterisk-config).
* Add option to export ASTSAFE_FOREGROUND=1 from the init script
  that calls this to disable backgrounding. Debian uses a similar
  method in debian/patches/safe_asterisk-nobg).

ASTERISK-23492 #close
Review: https://reviewboard.asterisk.org/r/3574/
........

Merged revisions 415132 from http://svn.asterisk.org/svn/asterisk/branches/1.8


git-svn-id: https://origsvn.digium.com/svn/asterisk/branches/11@415171 65c4cc65-6c06-0410-ace0-fbb531ad65f3
parent b9838a99
Branches
Tags
No related merge requests found
#!/bin/sh #!/bin/sh
# vim:textwidth=80:tabstop=4:shiftwidth=4:smartindent:autoindent
ASTSBINDIR="__ASTERISK_SBIN_DIR__"
ASTSBINDIR=__ASTERISK_SBIN_DIR__ ASTVARRUNDIR="__ASTERISK_VARRUN_DIR__"
ASTVARRUNDIR=__ASTERISK_VARRUN_DIR__ ASTVARLOGDIR="__ASTERISK_LOG_DIR__"
ASTVARLOGDIR=__ASTERISK_LOG_DIR__
CLIARGS="$*" # Grab any args passed to safe_asterisk
CLIARGS="$*" # Grab any args passed to safe_asterisk TTY=9 # TTY (if you want one) for Asterisk to run on
TTY=9 # TTY (if you want one) for Asterisk to run on CONSOLE=yes # Whether or not you want a console
CONSOLE=yes # Whether or not you want a console #NOTIFY=root@localhost # Who to notify about crashes
#NOTIFY=root@localhost.localdomain # Who to notify about crashes #EXEC=/path/to/somescript # Run this command if Asterisk crashes
NOTIFY=${NOTIFY:-} # Who to notify about crashes #LOGFILE="${ASTVARLOGDIR}/safe_asterisk.log" # Where to place the normal logfile (disabled if blank)
#EXEC=/path/to/somescript # Run this command if Asterisk crashes #SYSLOG=local0 # Which syslog facility to use (disabled if blank)
#LOGFILE=${ASTVARLOGDIR}/safe_asterisk.log # Where to place the normal logfile (disabled if blank) MACHINE=`hostname` # To specify which machine has crashed when getting the mail
SYSLOG=${SYSLOG:-} # Which syslog facility to use (disabled if blank) DUMPDROP="${DUMPDROP:-/tmp}"
MACHINE=`hostname` # To specify which machine has crashed when getting the mail RUNDIR="${RUNDIR:-/tmp}"
DUMPDROP=${DUMPDROP:-/tmp}
RUNDIR=${RUNDIR:-/tmp}
SLEEPSECS=4 SLEEPSECS=4
ASTPIDFILE=${ASTVARRUNDIR}/asterisk.pid ASTPIDFILE="${ASTVARRUNDIR}/asterisk.pid"
# comment this line out to have this script _not_ kill all mpg123 processes when # comment this line out to have this script _not_ kill all mpg123 processes when
# asterisk exits # asterisk exits
...@@ -38,19 +36,21 @@ PRIORITY=0 ...@@ -38,19 +36,21 @@ PRIORITY=0
# MAXFILES=32768 # MAXFILES=32768
message() { message() {
echo "$1" >&2 if test -n "$TTY" && test "$TTY" != "no"; then
if test "x$SYSLOG" != "x" ; then echo "$1" >/dev/${TTY}
logger -p "${SYSLOG}.warn" -t safe_asterisk[$$] "$1" fi
if test -n "$SYSLOG"; then
logger -p "${SYSLOG}.warn" -t safe_asterisk[$$] "$1"
fi fi
if test "x$LOGFILE" != "x" ; then if test -n "$LOGFILE"; then
echo "safe_asterisk[$$]: $1" >> "$LOGFILE" echo "safe_asterisk[$$]: $1" >>"$LOGFILE"
fi fi
} }
# Check if Asterisk is already running. If it is, then bug out, because # Check if Asterisk is already running. If it is, then bug out, because
# starting safe_asterisk when Asterisk is running is very bad. # starting safe_asterisk when Asterisk is running is very bad.
VERSION=`${ASTSBINDIR}/asterisk -nrx 'core show version' 2>/dev/null` VERSION=`"${ASTSBINDIR}/asterisk" -nrx 'core show version' 2>/dev/null`
if test "`echo $VERSION | cut -c 1-8`" = "Asterisk" ; then if test "`echo $VERSION | cut -c 1-8`" = "Asterisk"; then
message "Asterisk is already running. $0 will exit now." message "Asterisk is already running. $0 will exit now."
exit 1 exit 1
fi fi
...@@ -59,7 +59,7 @@ fi ...@@ -59,7 +59,7 @@ fi
# root. if running asterisk as other users, pass that to asterisk on the command # root. if running asterisk as other users, pass that to asterisk on the command
# line. # line.
# if we're not root, fall back to standard everything. # if we're not root, fall back to standard everything.
if test `id -u` != 0 ; then if test `id -u` != 0; then
echo "Oops. I'm not root. Falling back to standard prio and file max." >&2 echo "Oops. I'm not root. Falling back to standard prio and file max." >&2
echo "This is NOT suitable for large systems." >&2 echo "This is NOT suitable for large systems." >&2
PRIORITY=0 PRIORITY=0
...@@ -68,9 +68,9 @@ else ...@@ -68,9 +68,9 @@ else
if `uname -s | grep Linux >/dev/null 2>&1`; then if `uname -s | grep Linux >/dev/null 2>&1`; then
# maximum number of open files is set to the system maximum divided by two if # maximum number of open files is set to the system maximum divided by two if
# MAXFILES is not set. # MAXFILES is not set.
if test "x$MAXFILES" = "x" ; then if test -z "$MAXFILES"; then
# just check if file-max is readable # just check if file-max is readable
if test -r /proc/sys/fs/file-max ; then if test -r /proc/sys/fs/file-max; then
MAXFILES=$(( `cat /proc/sys/fs/file-max` / 2 )) MAXFILES=$(( `cat /proc/sys/fs/file-max` / 2 ))
fi fi
fi fi
...@@ -80,8 +80,8 @@ else ...@@ -80,8 +80,8 @@ else
fi fi
if test "x$SYSMAXFILES" != "x"; then if test -n "$SYSMAXFILES"; then
if test "x$SYSCTL_MAXFILES" != "x"; then if test -n "$SYSCTL_MAXFILES"; then
sysctl -w $SYSCTL_MAXFILES=$SYSMAXFILES sysctl -w $SYSCTL_MAXFILES=$SYSMAXFILES
fi fi
fi fi
...@@ -89,14 +89,14 @@ else ...@@ -89,14 +89,14 @@ else
# set the process's filemax to whatever set above # set the process's filemax to whatever set above
ulimit -n $MAXFILES ulimit -n $MAXFILES
if test ! -d ${ASTVARRUNDIR} ; then if test ! -d "${ASTVARRUNDIR}"; then
mkdir -p ${ASTVARRUNDIR} mkdir -p "${ASTVARRUNDIR}"
chmod 770 ${ASTVARRUNDIR} chmod 770 "${ASTVARRUNDIR}"
fi fi
fi fi
if test "x$UMASK" != "x"; then if test -n "$UMASK"; then
umask $UMASK umask $UMASK
fi fi
...@@ -109,31 +109,30 @@ ulimit -c unlimited ...@@ -109,31 +109,30 @@ ulimit -c unlimited
# Don't fork when running "safely" # Don't fork when running "safely"
# #
ASTARGS="" ASTARGS=""
if test "x$TTY" != "x" ; then if test -n "$TTY" && test "$TTY" != "no"; then
if test -c /dev/tty${TTY} ; then if test -c /dev/tty${TTY}; then
TTY=tty${TTY} TTY=tty${TTY}
elif test -c /dev/vc/${TTY} ; then elif test -c /dev/vc/${TTY}; then
TTY=vc/${TTY} TTY=vc/${TTY}
else else
message "Cannot find specified TTY (${TTY})" message "Cannot find specified TTY (${TTY})"
exit 1 exit 1
fi fi
ASTARGS="${ASTARGS} -vvvg" ASTARGS="${ASTARGS} -vvvg"
if test "x$CONSOLE" != "xno" ; then if test "$CONSOLE" != "no"; then
ASTARGS="${ASTARGS} -c" ASTARGS="${ASTARGS} -c"
fi fi
fi fi
if test ! -d "${RUNDIR}" ; then if test ! -d "${RUNDIR}"; then
message "${RUNDIR} does not exist, creating" message "${RUNDIR} does not exist, creating"
mkdir -p "${RUNDIR}" if ! mkdir -p "${RUNDIR}"; then
if test ! -d "${RUNDIR}" ; then
message "Unable to create ${RUNDIR}" message "Unable to create ${RUNDIR}"
exit 1 exit 1
fi fi
fi fi
if test ! -w "${DUMPDROP}" ; then if test ! -w "${DUMPDROP}"; then
message "Cannot write to ${DUMPDROP}" message "Cannot write to ${DUMPDROP}"
exit 1 exit 1
fi fi
...@@ -147,9 +146,9 @@ trap '' PIPE ...@@ -147,9 +146,9 @@ trap '' PIPE
# Run scripts to set any environment variables or do any other system-specific setup needed # Run scripts to set any environment variables or do any other system-specific setup needed
# #
if test -d /etc/asterisk/startup.d ; then if test -d /etc/asterisk/startup.d; then
for script in /etc/asterisk/startup.d/*.sh; do for script in /etc/asterisk/startup.d/*.sh; do
if test -r ${script} ; then if test -r ${script}; then
. ${script} . ${script}
fi fi
done done
...@@ -157,58 +156,61 @@ fi ...@@ -157,58 +156,61 @@ fi
run_asterisk() run_asterisk()
{ {
while :; do while :; do
if test -n "$TTY" && test "$TTY" != "no"; then
if test "x$TTY" != "x" ; then
cd "${RUNDIR}" cd "${RUNDIR}"
stty sane < /dev/${TTY} stty sane </dev/${TTY}
nice -n $PRIORITY ${ASTSBINDIR}/asterisk -f ${CLIARGS} ${ASTARGS} > /dev/${TTY} 2>&1 < /dev/${TTY} nice -n $PRIORITY "${ASTSBINDIR}/asterisk" -f ${CLIARGS} ${ASTARGS} >/dev/${TTY} 2>&1 </dev/${TTY}
else else
cd "${RUNDIR}" cd "${RUNDIR}"
nice -n $PRIORITY ${ASTSBINDIR}/asterisk -f ${CLIARGS} ${ASTARGS} nice -n $PRIORITY "${ASTSBINDIR}/asterisk" -f ${CLIARGS} ${ASTARGS} >/dev/null 2>&1 </dev/null
fi fi
EXITSTATUS=$? EXITSTATUS=$?
message "Asterisk ended with exit status $EXITSTATUS" message "Asterisk ended with exit status $EXITSTATUS"
if test "x$EXITSTATUS" = "x0" ; then if test $EXITSTATUS -eq 0; then
# Properly shutdown.... # Properly shutdown....
message "Asterisk shutdown normally." message "Asterisk shutdown normally."
exit 0 exit 0
elif test "0$EXITSTATUS" -gt "128" ; then elif test $EXITSTATUS -gt 128; then
EXITSIGNAL=$(($EXITSTATUS - 128)) EXITSIGNAL=$((EXITSTATUS - 128))
echo "Asterisk exited on signal $EXITSIGNAL." echo "Asterisk exited on signal $EXITSIGNAL."
if test "x$NOTIFY" != "x" ; then if test -n "$NOTIFY"; then
echo "Asterisk on $MACHINE exited on signal $EXITSIGNAL. Might want to take a peek." | \ echo "Asterisk on $MACHINE exited on signal $EXITSIGNAL. Might want to take a peek." | \
mail -s "Asterisk Died" $NOTIFY mail -s "Asterisk Died" $NOTIFY
message "Exited on signal $EXITSIGNAL" message "Exited on signal $EXITSIGNAL"
fi fi
if test "x$EXEC" != "x" ; then if test -n "$EXEC"; then
$EXEC $EXEC
fi fi
PID=`cat ${ASTPIDFILE}` PID=`cat ${ASTPIDFILE}`
DATE=`date "+%Y-%m-%dT%H:%M:%S%z"` DATE=`date "+%Y-%m-%dT%H:%M:%S%z"`
if test -f ${RUNDIR}/core.${PID} ; then if test -f "${RUNDIR}/core.${PID}"; then
mv ${RUNDIR}/core.${PID} ${DUMPDROP}/core.`hostname`-$DATE & mv "${RUNDIR}/core.${PID}" "${DUMPDROP}/core.`hostname`-$DATE" &
elif test -f ${RUNDIR}/core ; then elif test -f "${RUNDIR}/core"; then
mv ${RUNDIR}/core ${DUMPDROP}/core.`hostname`-$DATE & mv "${RUNDIR}/core" "${DUMPDROP}/core.`hostname`-$DATE" &
fi fi
else else
message "Asterisk died with code $EXITSTATUS." message "Asterisk died with code $EXITSTATUS."
PID=`cat ${ASTPIDFILE}` PID=`cat ${ASTPIDFILE}`
DATE=`date "+%Y-%m-%dT%H:%M:%S%z"` DATE=`date "+%Y-%m-%dT%H:%M:%S%z"`
if test -f ${RUNDIR}/core.${PID} ; then if test -f "${RUNDIR}/core.${PID}"; then
mv ${RUNDIR}/core.${PID} ${DUMPDROP}/core.`hostname`-$DATE & mv "${RUNDIR}/core.${PID}" "${DUMPDROP}/core.`hostname`-$DATE" &
elif test -f ${RUNDIR}/core ; then elif test -f "${RUNDIR}/core"; then
mv ${RUNDIR}/core ${DUMPDROP}/core.`hostname`-$DATE & mv "${RUNDIR}/core" "${DUMPDROP}/core.`hostname`-$DATE" &
fi fi
fi fi
message "Automatically restarting Asterisk." message "Automatically restarting Asterisk."
sleep $SLEEPSECS sleep $SLEEPSECS
if test "0$KILLALLMPG123" -gt "0" ; then if test "0$KILLALLMPG123" -gt 0; then
pkill -9 mpg123 pkill -9 mpg123
fi fi
done done
} }
run_asterisk & if test -n "$ASTSAFE_FOREGROUND"; then
run_asterisk
else
run_asterisk &
fi
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment