Apparently wheezy-backports already had a similar fix, as can be seen in:

  debian-asterisk-778746-wheezy-backports-2-master.patch
  (don't apply this if you apply the files below)

So for wheezy-backports, this is just a cleanup patch. For master it contains the change above and the cleanup.

  debian-asterisk-778746-master.patch
  debian-asterisk-778746-wheezy-backports.patch

Cheers,
Walter Doekes
OSSO B.V.


diff --git a/debian/asterisk.init b/debian/asterisk.init
index 0ab18a7..c4d328a 100644
--- a/debian/asterisk.init
+++ b/debian/asterisk.init
@@ -82,7 +82,7 @@ then
 fi
 PARAMS="$PARAMS -U $USER"
 
-if [ "x$AST_DEBUG_PARAMS" = x ] 
+if [ "x$AST_DEBUG_PARAMS" = x ]
 then
   AST_DEBUG_PARAMS=-cvvvvvddddd
 fi
@@ -96,7 +96,8 @@ 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
+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
@@ -118,6 +119,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 +144,55 @@ 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
-	# processes is stopped. No bother to worry about cleanup: 
+	# This may hang in some cases. Specifically, when the asterisk
+	# process is stopped. Don't 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"). Also kill any/all stray asterisk -r processes.
+	start-stop-daemon --stop --quiet --oknodo --retry=0/2/TERM/2/KILL/5 \
+		--exec $DAEMON
+	start-stop-daemon --stop --quiet --oknodo --retry=0/2/TERM/2/KILL/5 \
+		--exec $CANARY
 	echo "."
 	;;
   reload)
diff --git a/debian/asterisk.init b/debian/asterisk.init
index 1462e21..d6e9a20 100644
--- a/debian/asterisk.init
+++ b/debian/asterisk.init
@@ -82,7 +82,7 @@ then
 fi
 PARAMS="$PARAMS -U $USER"
 
-if [ "x$AST_DEBUG_PARAMS" = x ] 
+if [ "x$AST_DEBUG_PARAMS" = x ]
 then
   AST_DEBUG_PARAMS=-cvvvvvddddd
 fi
@@ -96,7 +96,8 @@ 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
+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
@@ -118,6 +119,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,49 +144,55 @@ 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
-	# processes is stopped. No bother to worry about cleanup: 
+	# This may hang in some cases. Specifically, when the asterisk
+	# process is stopped. Don't 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
-	# just making sure it's really, really dead. 
-	# KILL is necessary just in case there's an asterisk -r in the background
-	start-stop-daemon --stop --quiet --oknodo --retry=0/2/TERM/2/KILL/5 --exec $DAEMON
-	start-stop-daemon --stop --quiet --oknodo --retry=0/2/TERM/2/KILL/5 --exec $CANARY
+	# 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"). Also kill any/all stray asterisk -r processes.
+	start-stop-daemon --stop --quiet --oknodo --retry=0/2/TERM/2/KILL/5 \
+		--exec $DAEMON
+	start-stop-daemon --stop --quiet --oknodo --retry=0/2/TERM/2/KILL/5 \
+		--exec $CANARY
 	echo "."
 	;;
   reload)
$ git diff master..wheezy-backports debian/asterisk.init 
diff --git a/debian/asterisk.init b/debian/asterisk.init
index 0ab18a7..1462e21 100644
--- a/debian/asterisk.init
+++ b/debian/asterisk.init
@@ -178,7 +178,10 @@ case "$1" in
                                  --pidfile $ASTSAFE_PIDFILE
                rm -f $ASTSAFE_PIDFILE
        fi
-       start-stop-daemon --stop --quiet --oknodo --exec $DAEMON --pidfile=$PIDFILE
+       # just making sure it's really, really dead. 
+       # KILL is necessary just in case there's an asterisk -r in the background
+       start-stop-daemon --stop --quiet --oknodo --retry=0/2/TERM/2/KILL/5 --exec $DAEMON
+       start-stop-daemon --stop --quiet --oknodo --retry=0/2/TERM/2/KILL/5 --exec $CANARY
        echo "."
        ;;
   reload)

Reply via email to