Bug#778746: asterisk: running /etc/init.d/asterisk restart may end up without a running asterisk
root
wjdoekes+debianrepo at osso.nl
Thu Feb 19 09:07:26 UTC 2015
Package: asterisk
Version: 1:13.1.0~dfsg-1
Severity: normal
Dear Maintainer,
when running /etc/init.d/asterisk restart in rapid succession, sometimes
I end up with no running asterisk at all.
The cause is this:
restart calls:
- stop, then start
stop calls:
- asterisk_rx 'core stop gracefully'
- start-stop-daemon --stop --quiet --oknodo \
--exec $DAEMON --pidfile=$PIDFILE
at this point, asterisk is shutting down, but start-stop-daemon
hasn't confirmed that it is down yet.
start calls:
- status
and status happens to find the asterisk which is in <defunct> mode
while it is reaped by init,
at this point, "start" aborts, saying "asterisk is already running"
while asterisk is actually shutting down.
end result: no running asterisk.
The fix is this:
append the --retry argument to the start-stop-daemon call in stop,
like this:
- start-stop-daemon --stop --quiet --oknodo --retry=8/TERM/30/KILL/5 \
--exec $DAEMON --pidfile=$PIDFILE
I took the liberty of changing around a bit more in /etc/init.d/asterisk,
resulting in the diff at the bottom of this report [1].
The other changes are:
- add the safe_status check as was mentioned in the TODO
- swap the order of safe vs. non-safe startup in "start" for clarity;
after all, there is a positive '= "yes"' check in "stop" too.
- add a bit of comments surrounding the "stop" parts.
I've created and tested these changed against my custom built 13.2 package
which is basically the debian/ dir from 1:13.1.0~dfsg-1. I suppose the
changes would work equally well on asterisk 1:11.x~dfsg.
Cheers,
Walter Doekes
OSSO B.V.
-- System Information:
Debian Release: 7.8
APT prefers stable-updates
APT policy: (500, 'stable-updates'), (500, 'stable')
Architecture: amd64 (x86_64)
Kernel: Linux 3.2.0-4-amd64 (SMP w/8 CPU cores)
Locale: LANG=en_US.UTF-8, LC_CTYPE=en_US.UTF-8 (charmap=UTF-8)
Shell: /bin/sh linked to /bin/dash
Versions of packages asterisk depends on:
ii adduser 3.113+nmu3
ii asterisk-config 1:13.2.0-acos1+o0+2up
ii asterisk-core-sounds-en [asterisk-prompt-en] 1.4.22-1
ii asterisk-core-sounds-en-gsm 1.4.22-1
ii asterisk-modules 1:13.2.0-acos1+o0+2up
ii libc6 2.13-38+deb7u7
ii libcap2 1:2.22-1.2
ii libedit2 2.11-20080614-5
ii libgcc1 1:4.7.2-5
ii libjansson4 2.3.1-2
ii libpopt0 1.16-7
ii libsqlite3-0 3.7.13-1+deb7u1
ii libssl1.0.0 1.0.1e-2+deb7u14
ii libstdc++6 4.7.2-5
ii libtinfo5 5.9-10
ii libuuid1 2.20.1-5.3
ii libxml2 2.8.0+dfsg1-7+wheezy3
ii libxslt1.1 1.1.26-14.1
Versions of packages asterisk recommends:
ii asterisk-moh-opsound-gsm 2.03-1
ii asterisk-voicemail [asterisk-voicemail-storage] 1:13.2.0-acos1+o0+2up
ii sox 14.4.0-3+deb7u1
Versions of packages asterisk suggests:
pn asterisk-dahdi <none>
pn asterisk-dev <none>
pn asterisk-doc <none>
pn asterisk-ooh323 <none>
pn asterisk-vpb <none>
-- Configuration Files:
/etc/default/asterisk changed:
RUNASTSAFE=yes
/etc/init.d/asterisk changed:
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
NAME=asterisk
USER=$NAME
GROUP=$USER
DAEMON=/usr/sbin/$NAME
CANARY=/usr/sbin/astcanary
DESC="Asterisk PBX"
PIDFILE="/var/run/asterisk/asterisk.pid"
ASTSAFE_PIDFILE="/var/run/asterisk/asterisk_safe.pid"
UMASK=007 # by default
. /lib/lsb/init-functions
PARAMS=""
CHDIR_PARM=""
AST_REALTIME="yes"
RUNASTERISK="yes"
AST_DUMPCORE="no"
AST_DUMPCORE_DIR="/var/spool/asterisk" # only used if AST_DUMPCORE != no
if [ -r /etc/default/$NAME ]; then . /etc/default/$NAME; fi
if [ "$RUNASTERISK" != "yes" ];then
echo "Asterisk not yet configured. Edit /etc/default/asterisk first."
exit 0
fi
if [ "$AST_REALTIME" != "no" ]
then
PARAMS="$PARAMS -p"
fi
if [ "$AST_DUMPCORE" != "no" ]
then
PARAMS="$PARAMS -g"
if [ "$CORE_PATTERN" != '' ]
then
echo "$CORE_PATTERN" >/proc/sys/kernel/core_pattern
fi
if [ -d "$AST_DUMPCORE_DIR" ]
then
CHDIR_PARM="--chdir $AST_DUMPCORE_DIR"
fi
fi
if [ "x$USER" = "x" ]
then
echo "Error: empty USER name"
exit 1
fi
if [ `id -u "$USER"` = 0 ]
then
echo "Starting as root not supported."
exit 1
fi
PARAMS="$PARAMS -U $USER"
if [ "x$AST_DEBUG_PARAMS" = x ]
then
AST_DEBUG_PARAMS=-cvvvvvddddd
fi
if [ "$RUNASTSAFE" = "yes" ];then
# The value of WRAPPER_DAEMON in can be set in /etc/default/asterisk
WRAPPER_DAEMON=${WRAPPER_DAEMON:-/usr/sbin/safe_asterisk}
REALDAEMON="$WRAPPER_DAEMON"
else
REALDAEMON="$DAEMON"
fi
test -x $DAEMON || exit 0
for dir in /var/run/asterisk /var/log/asterisk /var/log/asterisk/cdr-csv /var/log/asterisk/cdr-custom; do
[ -d $dir ] || install -d -o $USER -g $GROUP $dir
[ -x /sbin/restorecon ] && /sbin/restorecon $dir
done
set -e
if [ "$UMASK" != '' ]
then
umask $UMASK
fi
if [ "$MAXFILES" != '' ]
then
ulimit -n $MAXFILES
fi
status() {
status_of_proc -p "$PIDFILE" "$NAME" "$DESC" && return 0 || return $?
}
safe_status() {
status_of_proc -p "$ASTSAFE_PIDFILE" "$NAME" "$DESC" && return 0 || return $?
}
asterisk_rx() {
if ! status >/dev/null; then return 0; fi
# if $HOME is set, asterisk -rx writes a .asterisk_history there
(
unset HOME
$DAEMON -rx "$1"
)
}
case "$1" in
debug)
# we add too many special parameters that I don't want to skip
# accidentally. I'm afraid that skipping -U once may cause
# confusing results. I also want to maintain the user's choice
# of -p
echo "Debugging $DESC: "
$DAEMON $PARAMS $AST_DEBUG_PARAMS
exit 0
;;
start)
if [ "$RUNASTSAFE" = "yes" ];then
if safe_status >/dev/null; then
echo "$DESC is already running. Use restart."
exit 0
fi
echo -n "Starting $DESC: "
export ASTSAFE_FOREGROUND=1
start-stop-daemon --start --group $GROUP \
--background --make-pidfile \
$CHDIR_PARM --pidfile "$ASTSAFE_PIDFILE" \
--exec $REALDAEMON -- $PARAMS
else
if status >/dev/null; then
echo "$DESC is already running. Use restart."
exit 0
fi
echo -n "Starting $DESC: "
start-stop-daemon --start --group $GROUP --pidfile "$PIDFILE" \
$CHDIR_PARM \
--exec $REALDAEMON -- $PARAMS > /dev/null
fi
echo "$NAME."
;;
stop)
echo -n "Stopping $DESC: $NAME"
# Try gracefully.
# This may hang in some cases. Specifically, when the asterisk
# processes is stopped. No bother to worry about cleanup:
# it will either fail or die when asterisk dies.
( asterisk_rx 'core stop now' > /dev/null 2>&1 & ) &
if [ "$RUNASTSAFE" = "yes" ];then
# If you're switching back and forth between RUNASTSAFE
# you may get a warning about a stale pidfile. Ignore
# it.
start-stop-daemon --stop --quiet --oknodo \
--pidfile $ASTSAFE_PIDFILE
rm -f $ASTSAFE_PIDFILE
fi
# Sometimes during a quick restart cycle, the 'core stop now'
# from above won't reach the daemon -- perhaps it wasn't
# listening yet. At this point we want TERM to kick in.
# In any case, we must be certain that it is stopped before we
# exit the "stop" case; otherwise a "restart" might complete
# with no asterisk running at all (because of the status checks
# in "start").
start-stop-daemon --stop --quiet --oknodo --retry=8/TERM/30/KILL/5 \
--exec $DAEMON --pidfile=$PIDFILE
echo "."
;;
reload)
echo "Reloading $DESC configuration files."
asterisk_rx 'module reload'
;;
logger-reload)
asterisk_rx 'logger reload'
;;
extensions-reload|dialplan-reload)
echo "Reloading $DESC configuration files."
asterisk_rx 'dialplan reload'
;;
restart-convenient)
asterisk_rx 'core restart when convenient'
;;
restart|force-reload)
$0 stop
$0 start
;;
status)
status
exit $?
;;
*)
N=/etc/init.d/$NAME
echo "Usage: $N {start|stop|restart|reload|status|debug|logger-reload|extensions-reload|restart-convenient|force-reload}" >&2
exit 1
;;
esac
exit 0
-- no debconf information
[1] Patch against asterisk.init:
diff --git a/init.d/asterisk b/init.d/asterisk
index 0ab18a7..a929190 100755
--- a/init.d/asterisk
+++ b/init.d/asterisk
@@ -118,6 +118,10 @@ status() {
status_of_proc -p "$PIDFILE" "$NAME" "$DESC" && return 0 || return $?
}
+safe_status() {
+ status_of_proc -p "$ASTSAFE_PIDFILE" "$NAME" "$DESC" && return 0 || return $?
+}
+
asterisk_rx() {
if ! status >/dev/null; then return 0; fi
@@ -139,46 +143,53 @@ case "$1" in
exit 0
;;
start)
- if status > /dev/null; then
- echo "$DESC is already running. Use restart."
- exit 0
- fi
- echo -n "Starting $DESC: "
- if [ "$RUNASTSAFE" != "yes" ];then
- # TODO: what if we cought the wrapper just as its asterisk
- # was killed? status should check for the wrapper if we're in
- # "safe mode"
- if status > /dev/null; then
+ if [ "$RUNASTSAFE" = "yes" ];then
+ if safe_status >/dev/null; then
echo "$DESC is already running. Use restart."
exit 0
fi
- start-stop-daemon --start --group $GROUP --pidfile "$PIDFILE" \
- $CHDIR_PARM \
- --exec $REALDAEMON -- $PARAMS > /dev/null
- else
+ echo -n "Starting $DESC: "
export ASTSAFE_FOREGROUND=1
start-stop-daemon --start --group $GROUP \
--background --make-pidfile \
$CHDIR_PARM --pidfile "$ASTSAFE_PIDFILE" \
--exec $REALDAEMON -- $PARAMS
+ else
+ if status >/dev/null; then
+ echo "$DESC is already running. Use restart."
+ exit 0
+ fi
+ echo -n "Starting $DESC: "
+ start-stop-daemon --start --group $GROUP --pidfile "$PIDFILE" \
+ $CHDIR_PARM \
+ --exec $REALDAEMON -- $PARAMS > /dev/null
fi
-
-
echo "$NAME."
;;
stop)
echo -n "Stopping $DESC: $NAME"
# Try gracefully.
- # this may hang in some cases. Specifically, when the asterisk
+ # This may hang in some cases. Specifically, when the asterisk
# processes is stopped. No bother to worry about cleanup:
# it will either fail or die when asterisk dies.
( asterisk_rx 'core stop now' > /dev/null 2>&1 & ) &
if [ "$RUNASTSAFE" = "yes" ];then
+ # If you're switching back and forth between RUNASTSAFE
+ # you may get a warning about a stale pidfile. Ignore
+ # it.
start-stop-daemon --stop --quiet --oknodo \
--pidfile $ASTSAFE_PIDFILE
rm -f $ASTSAFE_PIDFILE
fi
- start-stop-daemon --stop --quiet --oknodo --exec $DAEMON --pidfile=$PIDFILE
+ # Sometimes during a quick restart cycle, the 'core stop now'
+ # from above won't reach the daemon -- perhaps it wasn't
+ # listening yet. At this point we want TERM to kick in.
+ # In any case, we must be certain that it is stopped before we
+ # exit the "stop" case; otherwise a "restart" might complete
+ # with no asterisk running at all (because of the status checks
+ # in "start").
+ start-stop-daemon --stop --quiet --oknodo --retry=8/TERM/30/KILL/5 \
+ --exec $DAEMON --pidfile=$PIDFILE
echo "."
;;
reload)
More information about the Pkg-voip-maintainers
mailing list