Sisyphus repository
Last update: 1 october 2023 | SRPMs: 18631 | Visits: 37492006
en ru br
ALT Linux repos
S:2.89-alt2
5.0: 2.45-alt2
4.1: 2.41-alt4.M41.1
4.0: 2.38-alt1
3.0: 2.22-alt2

Group :: System/Servers
RPM: dnsmasq

 Main   Changelog   Spec   Patches   Sources   Download   Gear   Bugs and FR  Repocop 

#!/bin/sh
changequote([[,]])dnl
changecom([[##]])dnl
#
# Startup script for the DNS caching server under ALTLinux ifdef([[M24]],[[2.4]],[[2.x]])
#
# chkconfig: 2345 20 55
# description: This script starts your DNS caching server
# processname: dnsmasq
# config: /etc/dnsmasq.conf
# pidfile: /var/run/dnsmasq.pid

ifdef([[M24]],[[dnl
# Do not load RH compatibility interface.
WITHOUT_RC_COMPAT=1
]])dnl
# Source function library.
. /etc/init.d/functions

if [ "$1" = "--trace-script" ]; then
[[shift]]
set -x
fi

# Source networking configuration and check that networking is up.
SourceIfNotEmpty /etc/sysconfig/network
ifdef([[M24]],[[dnl
is_yes "${NETWORKING}" || exit 0
]],[[dnl
[ "${NETWORKING}" = "no" ] && exit 0
]])dnl

dnsmasq=/usr/sbin/dnsmasq
[ -x $dnsmasq ] || exit 0

# Assign default values, then load configuration file.
OPTIONS=""
ALL=255.255.255.255
DOMAIN_SUFFIX=$(dnsdomainname 2>/dev/null || hostname --domain 2>/dev/null)
PPP_RESOLV_CONF=/etc/ppp/resolv.conf
SYNC_RESOLV_CONF=yes
SourceIfExists /etc/sysconfig/dnsmasq

# Build program command line
[ -n "${MAILHOSTNAME}" ] && OPTIONS="$OPTIONS -m $MAILHOSTNAME"
[ -n "${RESOLV_CONF}" ] && OPTIONS="$OPTIONS -r $RESOLV_CONF"
[ -n "${PPP_RESOLV_CONF}" ] && OPTIONS="$OPTIONS -r $PPP_RESOLV_CONF"
[ -n "${DOMAIN_SUFFIX}" ] && OPTIONS="$OPTIONS -s $DOMAIN_SUFFIX"
[ -n "${DHCP_LEASE}" ] && OPTIONS="$OPTIONS -l $DHCP_LEASE"

# Internal variables
PIDFILE=/var/run/dnsmasq.pid
LOCKFILE=/var/lock/subsys/dnsmasq
RETVAL=0

# External commands
CP=/bin/cp
MV=/bin/mv
RM=/bin/rm
TOUCH=/bin/touch
EGREP=/bin/egrep

#---------------------------------------------------------------
#
# /etc/resolv.conf
#

notify_resolver() {
# ExecIfExecutable /sbin/update_chrooted conf
ExecIfExecutable /etc/chroot.d/resolv.conf
}

function check_resolv_conf() {
case "$SYNC_RESOLV_CONF" in
YES|Yes|yes|Y|y|TRUE|True|true|ON|On|on|1 ) ;;
* ) return 1
esac
[ -z "$RESOLV_CONF" ] && return 1
[ "$RESOLV_CONF" = "/etc/resolv.conf" ] && return 1
return 0
}

function do_resolv_conf()
{
check_resolv_conf || return 1

if [ /etc/resolv.conf -nt "$RESOLV_CONF" ]; then
echo "Update/create $RESOLV_CONF"
# $EGREP -vi '(^ *nameserver 127.0.0.1)|(# ppp temp entry)' /etc/resolv.conf > "$RESOLV_CONF"
cp -af /etc/resolv.conf "$RESOLV_CONF"
fi

# Is /etc/resolv.conf already valid?
local n_ppp=`$EGREP -c '# ppp temp entry' /etc/resolv.conf`
local n_ns=`$EGREP -c '^ *nameserver' /etc/resolv.conf`
local n_localns=`$EGREP -c '^ *nameserver *127.0.0.1' /etc/resolv.conf`

if [ "$n_ppp" != "0" -o "$n_ns" != "$n_localns" ]; then
echo "Validate /etc/resolv.conf"
$EGREP -vi '(^ *nameserver *)|(# ppp temp entry)' /etc/resolv.conf > /etc/resolv.conf.tmpnew
echo -e "\nnameserver 127.0.0.1" >> /etc/resolv.conf.tmpnew
$MV -f /etc/resolv.conf.tmpnew /etc/resolv.conf
$TOUCH -r "$RESOLV_CONF" /etc/resolv.conf
notify_resolver
fi

return 0
}

function undo_resolv_conf()
{
check_resolv_conf || return 1
[ -r "$RESOLV_CONF" ] || return 1

if [ /etc/resolv.conf -nt "$RESOLV_CONF" ]; then
echo "WARNING: /etc/resolv.conf is newer than $RESOLV_CONF"
echo " It will be saved to /etc/resolv.conf.dnsmasqnew"
$MV -f /etc/resolv.conf /etc/resolv.conf.dnsmasqnew
fi

echo "Restore /etc/resolv.conf"
$CP -af "$RESOLV_CONF" /etc/resolv.conf
notify_resolver

return 0
}

#---------------------------------------------------------------
#
# Broadcast routing
#

function print_all_ifaces()
{
local linenum=0
netstat -vai | while read iface moredata; do
[ $[++linenum] -le 2 ] && continue
case "$iface" in
lo ) ;; # skip loopback
# *:* ) ;; # skip aliases?
* ) echo $iface ;;
esac
done
}

function print_listening_ifaces()
{
local line words dev
netstat -ltnp | egrep '[0-9]*/dnsmasq' | egrep ':53 ' | while read line; do
words=($line)
dev="${words[3]%:*}"
if [ "$dev" = "0.0.0.0" ]; then
print_all_ifaces
break
fi
echo "$dev"
done | uniq
}

function do_route() {
[ -n "$ALL_DEV" ] || return
local msg='Broadcast device ALL_DEV is defined, but destaddr ALL is empty!'
if [ -z "$ALL" ]; then
echo -n $msg
ifdef([[M24]],[[failure "$msg"; echo]],[[echo ' ..failed!']])
return 1
fi
local cmd=$1
[[shift]]
echo -n $*
ifdef([[M24]],[[start_daemon --no-announce --]],[[daemon]]) \
/sbin/ip route $cmd $ALL dev $ALL_DEV
ifdef([[M24]],,[[echo]])
}

function cleanup_routing()
{
[ -z "$ALL" ] && return
/sbin/ip route list | $EGREP "^$ALL dev " | while read dst dev_kword iface tail; do
/sbin/ip route del $dst dev $iface $tail
done
}

#---------------------------------------------------------------
#
# Main routines
#

function start()
{
do_resolv_conf
ifdef([[M24]],[[dnl
start_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --expect-user nobody -- $dnsmasq $OPTIONS
RETVAL=$?
]],[[dnl
echo -n "Starting dnsmasq: "
daemon $dnsmasq $OPTIONS
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && $TOUCH $LOCKFILE
]])dnl
do_route add "Adding local broadcast host route:"
return $RETVAL
}

function start_debug()
{
do_resolv_conf
# don't adds/removes routing!
$dnsmasq -d -q $OPTIONS
undo_resolv_conf
}

function is_loaded() {
[ -z "`/sbin/pidof dnsmasq`" ] && return 1
return 0
}

function stop()
{
undo_resolv_conf
do_route del "Removing host route defined at startup:"
ifdef([[M24]],[[dnl
stop_daemon --pidfile "$PIDFILE" --lockfile "$LOCKFILE" --expect-user nobody -- $dnsmasq
RETVAL=$?
]],[[dnl
echo -n "Shutting down dnsmasq: "
killproc dnsmasq
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && $RM -f $LOCKFILE
]])dnl
return $RETVAL
}

function my_status() {
ifdef([[M24]],[[dnl
status --pidfile "$PIDFILE" --expect-user nobody -- $dnsmasq
]],[[dnl
status dnsmasq
]])dnl
RETVAL=$?
if [ $RETVAL = 0 ]; then
echo "Listening interfaces:"
print_listening_ifaces
fi
return $RETVAL
}

function restart() {
stop
start
RETVAL=$?
return $RETVAL
}

function reload() {
ifdef([[M24]],[[dnl
msg_reloading dnsmasq
stop_daemon --pidfile "$PIDFILE" --expect-user nobody -HUP -- $dnsmasq
RETVAL=$?
]],[[dnl
echo -n "Reloading dnsmasq configuration: "
killproc dnsmasq -SIGHUP
RETVAL=$?
echo
]])dnl
return $RETVAL
}

function clean() {
stop
$RM -f $LOCKFILE $PID_FILE
check_resolv_conf && $RM -f "$RESOLV_CONF"
cleanup_routing
RETVAL=0
}

case "$1" in
start) start ;;
stop) stop ;;
status) my_status ;;
restart) restart ;;
reload) reload ;;
clean) clean ;;
startdebug) start_debug ;;
condrestart) is_loaded && restart ;;
condstop) is_loaded && stop ;;
*)
echo "Usage: ${0##*/} {start|stop|restart|reload|condrestart|condstop|clean|status}"
exit 1
esac

exit $RETVAL

## EOF ##
 
design & coding: Vladimir Lettiev aka crux © 2004-2005, Andrew Avramenko aka liks © 2007-2008
current maintainer: Michael Shigorin