In order to get a dump of the running process, we need to find the
pid of the main asterisk process. This can be tricky if there are
also instances of "asterisk -r" running or if an alternate location
for asterisk.conf was specified on the command line with the -C
option that also specified an alternation location for the pid file.
So now...
1. We find the asterisk executable with "which" or the --asterisk-bin
command line option.
2. If there's only 1 process with an executable path that matches,
we use that pid. If not...
3. We try "<asterisk-bin> -rx 'core show settings'" and parse the
output to find the pidfile, then read that for the pid. If that
didn't work...
4. We get a list of all the pids matching <asterisk-bin> and look
in /proc/<pid>/cmdline for a -C argument and retry the "core show
settings" using the same -C option. We can't parse the output
of "ps" to get the -C path because it may contain spaces. The
contents of /proc/<pid>/cmdline are delimited by NULLs. For BSDs
we may have to mount /proc first. :(
ASTERISK-28221
Reported by: Andrew Nagy
Change-Id: I8aa1f3f912f949df2b5348908803c636bde1d57c
read -p "WARNING: Taking a core dump of the running asterisk instance will suspend call processing while the dump is saved. Do you wish to continue? (y/N) " answer
unset pid
# Simplest case first...
pids=$(pgrep -f "$asterisk_bin")
pidcount=$(echo $pids | wc -w)
if [ $pidcount -eq 0 ] ; then
>&2 echo "Asterisk is not running"
exit 1
fi
# Single process, great.
if [ $pidcount -eq 1 ] ; then
pid=$pids
echo "Found a single asterisk instance running as process $pid"
fi
# More than 1 asterisk process running
if [ x"$pid" = x ] ; then
# More than 1 process running, let's try asking asterisk for it's
# pidfile
pidfile=$("$asterisk_bin" -rx "core show settings" 2>/dev/null | sed -n -r -e "s/^\s*pid file:\s+(.*)/\1/gpi")
pidfile=$("$asterisk_bin" -C "$astetcconf" -rx "core show settings" 2>/dev/null | sed -n -r -e "s/^\s*pid file:\s+(.*)/\1/gpi")
if [ x"$pidfile" != x -a -f "$pidfile" ] ; then
pid=$(cat "$pidfile")
echo "Found pidfile $pidfile the hard way with process $pid"
break
fi
fi
done
if [ $mounted_proc -eq 1 ] ; then
echo "Unmounting /proc"
umount /proc
fi
fi
if [ x"$pid" = x ] ; then
>&2 echo "Can't determine pid of the running asterisk instance"
exit 1
fi
if $RUNNING ; then
answer=Y
else
read -p "WARNING: Taking a core dump of the running asterisk instance will suspend call processing while the dump is saved. Do you wish to continue? (y/N) " answer