I'd like to start a thread dedicated to the Asterisk package.
I have written an improved startup script (as /etc/init.d/S60asterisk) in order to fix three problems present in the current releases found by http://nthill.free.fr/openwrt/tracker/p … e=Asterisk :
1. The "registry" file astdb is located on flash (in /usr/lib/asterisk). Being this file re-written often, the flash chip can be worn out. My startup script, when called with argument "start", moves it to a RAM-based partition (/var/spool/asterisk) and puts in place a symbolic link; when called with "stop" it saves it back to its original place.
2. Asterisk runs as root, which is definitely not_good for a server with exposure to the big bad Internet. As "su" is unavailable on OpenWRT, my script chmod's +s the executable and changes its owner to "asterisk.asterisk" (also libraries, config files etc. are chown'd to the same user, but only if at least one of them is owned by root: this avoids unnecessary directory updates unless the user creates a new file there and forgets to chown it "asterisk.asterisk"). If such user or group do not exist, appropriate entries are created in /etc/passwd and /etc/group with UID and GID both equal to 5060. If such UID or GID are already taken by other users, the script terminates with an error message and the user has to fix it manually.
3. In many installations (such as mine :-) ) Asterisk runs on machines with a dynamically assigned IP address. Starting Asterisk before the establishment of the connection to the Internet seems to create problems, even if Asterisk binds to 0.0.0.0 rather than the external interface. So, I have put in place a loop that delays the startup until a default route is defined (it checks every 2 seconds for 0.0.0.0 in the output of "route"). This gives pppd the time to connect to the Internet (generally through PPPoE).
The script follows; bug reports are welcome.
Enzo
#!/bin/sh
DEFAULT=/etc/default/asterisk
OPTIONS=""
[ -f $DEFAULT ] && . $DEFAULT
[ "$ENABLE_ASTERISK" = "yes" ] || exit 0
# do a "chown -R" only if some file in the dir is owned by root
cond_chown_r() { # user file-or-dir
MATCHLIST=$(ls -ld $2 | grep 'root')
MATCHLIST=$MATCHLIST$(ls -l $2 | grep 'root')
if [ ! -z "$MATCHLIST" ] ; then
chown -R $1 $2
echo chown -R $1 $2
fi
}
# replace symlinks to /rom/... with actual copies
deromize() {
if [ -L $1 ] ; then
POINTEDFILE="$(ls -l /etc/group | cut -d '>' -f 2-)"
rm $1
cp $POINTEDFILE $1
fi
}
case $1 in
start)
[ -d /var/run ] || mkdir -p /var/run
[ -d /var/log/asterisk ] || mkdir -p /var/log/asterisk
[ -d /var/spool/asterisk ] || mkdir -p /var/spool/asterisk
deromize /etc/group
# create the group "asterisk", unless already there
if [ -z "$(grep '^asterisk:' /etc/group)" ] ; then
MATCHING="$(grep ':5060:' /etc/group)"
if [ -z "$MATCHING" ] ; then
echo "asterisk:x:5060:" >> /etc/group
else
echo "** error in ${0} - GID 5060 already in use by others:"
echo $MATCHING
exit 1
fi
fi
deromize /etc/passwd
# create the user "asterisk", unless already there
if [ -z "$(grep '^asterisk:' /etc/passwd)" ] ; then
MATCHING="$(grep ':5060:' /etc/passwd)"
if [ -z "$MATCHING" ] ; then
echo "asterisk:*:5060:5060:asterisk:/tmp:/bin/ash" >> /etc/passwd
else
echo "** error in ${0} - UID 5060 already in use by others:"
echo $MATCHING
exit 1
fi
fi
# change ownership of all relevant directories
cond_chown_r asterisk.asterisk /var/log/asterisk
cond_chown_r asterisk.asterisk /var/spool/asterisk
cond_chown_r asterisk.asterisk /etc/asterisk
cond_chown_r asterisk.asterisk /usr/lib/asterisk
cond_chown_r asterisk.asterisk /usr/sbin/asterisk
# make the binary suid, so it won't run as root
chmod +s /usr/sbin/asterisk
while [ -z "$(route | grep '0\.0\.0\.0')" ] ; do
echo "waiting for default route to exist..."
logger -p user.info "waiting for default route to exist..Æ?."
sleep 2
done
if [ -f /usr/lib/asterisk/astdb ] ; then
mv /usr/lib/asterisk/astdb /var/spool/asterisk/astdb
ln -s /var/spool/asterisk/astdb /usr/lib/asterisk/astdb
fi
/usr/sbin/asterisk $OPTIONS
;;
stop)
[ -f /var/run/asterisk.pid ] && kill $(cat /var/run/asterisk.pid) >/dev/null 2>&1
if [ -L /usr/lib/asterisk/astdb ] ; then
rm /usr/lib/asterisk/astdb
fi
if [ -f /var/spool/asterisk/astdb ] ; then
mv -f /var/spool/asterisk/astdb /usr/lib/asterisk/astdb
fi
;;
*)
echo "usage: $0 (start|stop)"
exit 1
esac
exit $?
(Last edited by enzo on 23 Sep 2005, 07:05)