From 2fbf3e8811ad6cf9c0ffca5d3823a0921aca3111 Mon Sep 17 00:00:00 2001 From: Walter Doekes Date: Wed, 4 Jun 2014 20:12:36 +0000 Subject: [PATCH] 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 --- contrib/scripts/safe_asterisk | 132 +++++++++++++++++----------------- 1 file changed, 67 insertions(+), 65 deletions(-) diff --git a/contrib/scripts/safe_asterisk b/contrib/scripts/safe_asterisk index 2e661b4766..daf261608c 100644 --- a/contrib/scripts/safe_asterisk +++ b/contrib/scripts/safe_asterisk @@ -1,23 +1,21 @@ #!/bin/sh -# vim:textwidth=80:tabstop=4:shiftwidth=4:smartindent:autoindent - -ASTSBINDIR=__ASTERISK_SBIN_DIR__ -ASTVARRUNDIR=__ASTERISK_VARRUN_DIR__ -ASTVARLOGDIR=__ASTERISK_LOG_DIR__ - -CLIARGS="$*" # Grab any args passed to safe_asterisk -TTY=9 # TTY (if you want one) for Asterisk to run on -CONSOLE=yes # Whether or not you want a console -#NOTIFY=root@localhost.localdomain # Who to notify about crashes -NOTIFY=${NOTIFY:-} # Who to notify about crashes -#EXEC=/path/to/somescript # Run this command if Asterisk crashes -#LOGFILE=${ASTVARLOGDIR}/safe_asterisk.log # Where to place the normal logfile (disabled if blank) -SYSLOG=${SYSLOG:-} # Which syslog facility to use (disabled if blank) -MACHINE=`hostname` # To specify which machine has crashed when getting the mail -DUMPDROP=${DUMPDROP:-/tmp} -RUNDIR=${RUNDIR:-/tmp} + +ASTSBINDIR="__ASTERISK_SBIN_DIR__" +ASTVARRUNDIR="__ASTERISK_VARRUN_DIR__" +ASTVARLOGDIR="__ASTERISK_LOG_DIR__" + +CLIARGS="$*" # Grab any args passed to safe_asterisk +TTY=9 # TTY (if you want one) for Asterisk to run on +CONSOLE=yes # Whether or not you want a console +#NOTIFY=root@localhost # Who to notify about crashes +#EXEC=/path/to/somescript # Run this command if Asterisk crashes +#LOGFILE="${ASTVARLOGDIR}/safe_asterisk.log" # Where to place the normal logfile (disabled if blank) +#SYSLOG=local0 # Which syslog facility to use (disabled if blank) +MACHINE=`hostname` # To specify which machine has crashed when getting the mail +DUMPDROP="${DUMPDROP:-/tmp}" +RUNDIR="${RUNDIR:-/tmp}" SLEEPSECS=4 -ASTPIDFILE=${ASTVARRUNDIR}/asterisk.pid +ASTPIDFILE="${ASTVARRUNDIR}/asterisk.pid" # comment this line out to have this script _not_ kill all mpg123 processes when # asterisk exits @@ -38,19 +36,21 @@ PRIORITY=0 # MAXFILES=32768 message() { - echo "$1" >&2 - if test "x$SYSLOG" != "x" ; then - logger -p "${SYSLOG}.warn" -t safe_asterisk[$$] "$1" + if test -n "$TTY" && test "$TTY" != "no"; then + echo "$1" >/dev/${TTY} + fi + if test -n "$SYSLOG"; then + logger -p "${SYSLOG}.warn" -t safe_asterisk[$$] "$1" fi - if test "x$LOGFILE" != "x" ; then - echo "safe_asterisk[$$]: $1" >> "$LOGFILE" + if test -n "$LOGFILE"; then + echo "safe_asterisk[$$]: $1" >>"$LOGFILE" fi } # Check if Asterisk is already running. If it is, then bug out, because # starting safe_asterisk when Asterisk is running is very bad. -VERSION=`${ASTSBINDIR}/asterisk -nrx 'core show version' 2>/dev/null` -if test "`echo $VERSION | cut -c 1-8`" = "Asterisk" ; then +VERSION=`"${ASTSBINDIR}/asterisk" -nrx 'core show version' 2>/dev/null` +if test "`echo $VERSION | cut -c 1-8`" = "Asterisk"; then message "Asterisk is already running. $0 will exit now." exit 1 fi @@ -59,7 +59,7 @@ fi # root. if running asterisk as other users, pass that to asterisk on the command # line. # 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 "This is NOT suitable for large systems." >&2 PRIORITY=0 @@ -68,9 +68,9 @@ else 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 # MAXFILES is not set. - if test "x$MAXFILES" = "x" ; then + if test -z "$MAXFILES"; then # 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 )) fi fi @@ -80,8 +80,8 @@ else fi - if test "x$SYSMAXFILES" != "x"; then - if test "x$SYSCTL_MAXFILES" != "x"; then + if test -n "$SYSMAXFILES"; then + if test -n "$SYSCTL_MAXFILES"; then sysctl -w $SYSCTL_MAXFILES=$SYSMAXFILES fi fi @@ -89,14 +89,14 @@ else # set the process's filemax to whatever set above ulimit -n $MAXFILES - if test ! -d ${ASTVARRUNDIR} ; then - mkdir -p ${ASTVARRUNDIR} - chmod 770 ${ASTVARRUNDIR} + if test ! -d "${ASTVARRUNDIR}"; then + mkdir -p "${ASTVARRUNDIR}" + chmod 770 "${ASTVARRUNDIR}" fi fi -if test "x$UMASK" != "x"; then +if test -n "$UMASK"; then umask $UMASK fi @@ -109,31 +109,30 @@ ulimit -c unlimited # Don't fork when running "safely" # ASTARGS="" -if test "x$TTY" != "x" ; then - if test -c /dev/tty${TTY} ; then +if test -n "$TTY" && test "$TTY" != "no"; then + if test -c /dev/tty${TTY}; then TTY=tty${TTY} - elif test -c /dev/vc/${TTY} ; then + elif test -c /dev/vc/${TTY}; then TTY=vc/${TTY} else message "Cannot find specified TTY (${TTY})" exit 1 fi ASTARGS="${ASTARGS} -vvvg" - if test "x$CONSOLE" != "xno" ; then + if test "$CONSOLE" != "no"; then ASTARGS="${ASTARGS} -c" fi fi -if test ! -d "${RUNDIR}" ; then +if test ! -d "${RUNDIR}"; then message "${RUNDIR} does not exist, creating" - mkdir -p "${RUNDIR}" - if test ! -d "${RUNDIR}" ; then + if ! mkdir -p "${RUNDIR}"; then message "Unable to create ${RUNDIR}" exit 1 fi fi -if test ! -w "${DUMPDROP}" ; then +if test ! -w "${DUMPDROP}"; then message "Cannot write to ${DUMPDROP}" exit 1 fi @@ -147,9 +146,9 @@ trap '' PIPE # 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 - if test -r ${script} ; then + if test -r ${script}; then . ${script} fi done @@ -157,58 +156,61 @@ fi run_asterisk() { - while :; do - - if test "x$TTY" != "x" ; then + while :; do + if test -n "$TTY" && test "$TTY" != "no"; then cd "${RUNDIR}" - stty sane < /dev/${TTY} - nice -n $PRIORITY ${ASTSBINDIR}/asterisk -f ${CLIARGS} ${ASTARGS} > /dev/${TTY} 2>&1 < /dev/${TTY} + stty sane /dev/${TTY} 2>&1 /dev/null 2>&1