dnsmasq 2.71
[tomato.git] / release / src / router / dnsmasq / debian / init
blob0b65d1447ce0973b5b0b04499fcbce9fe5e19325
1 #!/bin/sh
2 ### BEGIN INIT INFO
3 # Provides: dnsmasq
4 # Required-Start: $network $remote_fs $syslog
5 # Required-Stop: $network $remote_fs $syslog
6 # Default-Start: 2 3 4 5
7 # Default-Stop: 0 1 6
8 # Description: DHCP and DNS server
9 ### END INIT INFO
11 set +e # Don't exit on error status
13 PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
14 DAEMON=/usr/sbin/dnsmasq
15 NAME=dnsmasq
16 DESC="DNS forwarder and DHCP server"
18 # Most configuration options in /etc/default/dnsmasq are deprecated
19 # but still honoured.
20 ENABLED=1
21 if [ -r /etc/default/$NAME ]; then
22 . /etc/default/$NAME
25 # Get the system locale, so that messages are in the correct language, and the
26 # charset for IDN is correct
27 if [ -r /etc/default/locale ]; then
28 . /etc/default/locale
29 export LANG
32 # /etc/dnsmasq.d/README is a non-conffile installed by the dnsmasq package.
33 # Should the dnsmasq package be removed, the following test ensures that
34 # the daemon is no longer started, even if the dnsmasq-base package is
35 # still in place.
36 test -e /etc/dnsmasq.d/README || exit 0
38 test -x $DAEMON || exit 0
40 # Provide skeleton LSB log functions for backports which don't have LSB functions.
41 if [ -f /lib/lsb/init-functions ]; then
42 . /lib/lsb/init-functions
43 else
44 log_warning_msg () {
45 echo "${@}."
48 log_success_msg () {
49 echo "${@}."
52 log_daemon_msg () {
53 echo -n "${1}: $2"
56 log_end_msg () {
57 if [ $1 -eq 0 ]; then
58 echo "."
59 elif [ $1 -eq 255 ]; then
60 /bin/echo -e " (warning)."
61 else
62 /bin/echo -e " failed!"
67 # RESOLV_CONF:
68 # If the resolvconf package is installed then use the resolv conf file
69 # that it provides as the default. Otherwise use /etc/resolv.conf as
70 # the default.
72 # If IGNORE_RESOLVCONF is set in /etc/default/dnsmasq or an explicit
73 # filename is set there then this inhibits the use of the resolvconf-provided
74 # information.
76 # Note that if the resolvconf package is installed it is not possible to
77 # override it just by configuration in /etc/dnsmasq.conf, it is necessary
78 # to set IGNORE_RESOLVCONF=yes in /etc/default/dnsmasq.
80 if [ ! "$RESOLV_CONF" ] &&
81 [ "$IGNORE_RESOLVCONF" != "yes" ] &&
82 [ -x /sbin/resolvconf ]
83 then
84 RESOLV_CONF=/var/run/dnsmasq/resolv.conf
87 for INTERFACE in $DNSMASQ_INTERFACE; do
88 DNSMASQ_INTERFACES="$DNSMASQ_INTERFACES -i $INTERFACE"
89 done
91 for INTERFACE in $DNSMASQ_EXCEPT; do
92 DNSMASQ_INTERFACES="$DNSMASQ_INTERFACES -I $INTERFACE"
93 done
95 if [ ! "$DNSMASQ_USER" ]; then
96 DNSMASQ_USER="dnsmasq"
99 # This tells dnsmasq to ignore DNS requests that don't come from a local network.
100 # It's automatically ignored if --interface --except-interface, --listen-address
101 # or --auth-server exist in the configuration, so for most installations, it will
102 # have no effect, but for otherwise-unconfigured installations, it stops dnsmasq
103 # from being vulnerable to DNS-reflection attacks.
105 DNSMASQ_OPTS="$DNSMASQ_OPTS --local-service"
107 start()
109 # Return
110 # 0 if daemon has been started
111 # 1 if daemon was already running
112 # 2 if daemon could not be started
114 # /var/run may be volatile, so we need to ensure that
115 # /var/run/dnsmasq exists here as well as in postinst
116 if [ ! -d /var/run/dnsmasq ]; then
117 mkdir /var/run/dnsmasq || return 2
118 chown dnsmasq:nogroup /var/run/dnsmasq || return 2
121 start-stop-daemon --start --quiet --pidfile /var/run/dnsmasq/$NAME.pid --exec $DAEMON --test > /dev/null || return 1
122 start-stop-daemon --start --quiet --pidfile /var/run/dnsmasq/$NAME.pid --exec $DAEMON -- \
123 -x /var/run/dnsmasq/$NAME.pid \
124 ${MAILHOSTNAME:+ -m $MAILHOSTNAME} \
125 ${MAILTARGET:+ -t $MAILTARGET} \
126 ${DNSMASQ_USER:+ -u $DNSMASQ_USER} \
127 ${DNSMASQ_INTERFACES:+ $DNSMASQ_INTERFACES} \
128 ${DHCP_LEASE:+ -l $DHCP_LEASE} \
129 ${DOMAIN_SUFFIX:+ -s $DOMAIN_SUFFIX} \
130 ${RESOLV_CONF:+ -r $RESOLV_CONF} \
131 ${CACHESIZE:+ -c $CACHESIZE} \
132 ${CONFIG_DIR:+ -7 $CONFIG_DIR} \
133 ${DNSMASQ_OPTS:+ $DNSMASQ_OPTS} \
134 || return 2
137 start_resolvconf()
139 # If interface "lo" is explicitly disabled in /etc/default/dnsmasq
140 # Then dnsmasq won't be providing local DNS, so don't add it to
141 # the resolvconf server set.
142 for interface in $DNSMASQ_EXCEPT
144 [ $interface = lo ] && return
145 done
147 if [ -x /sbin/resolvconf ] ; then
148 echo "nameserver 127.0.0.1" | /sbin/resolvconf -a lo.$NAME
150 return 0
153 stop()
155 # Return
156 # 0 if daemon has been stopped
157 # 1 if daemon was already stopped
158 # 2 if daemon could not be stopped
159 # other if a failure occurred
160 start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile /var/run/dnsmasq/$NAME.pid --name $NAME
161 RETVAL="$?"
162 [ "$RETVAL" = 2 ] && return 2
163 return "$RETVAL"
166 stop_resolvconf()
168 if [ -x /sbin/resolvconf ] ; then
169 /sbin/resolvconf -d lo.$NAME
171 return 0
174 status()
176 # Return
177 # 0 if daemon is running
178 # 1 if daemon is dead and pid file exists
179 # 3 if daemon is not running
180 # 4 if daemon status is unknown
181 start-stop-daemon --start --quiet --pidfile /var/run/dnsmasq/$NAME.pid --exec $DAEMON --test > /dev/null
182 case "$?" in
183 0) [ -e "/var/run/dnsmasq/$NAME.pid" ] && return 1 ; return 3 ;;
184 1) return 0 ;;
185 *) return 4 ;;
186 esac
189 case "$1" in
190 start)
191 test "$ENABLED" != "0" || exit 0
192 log_daemon_msg "Starting $DESC" "$NAME"
193 start
194 case "$?" in
196 log_end_msg 0
197 start_resolvconf
198 exit 0
201 log_success_msg "(already running)"
202 exit 0
205 log_end_msg 1
206 exit 1
208 esac
210 stop)
211 stop_resolvconf
212 if [ "$ENABLED" != "0" ]; then
213 log_daemon_msg "Stopping $DESC" "$NAME"
215 stop
216 RETVAL="$?"
217 if [ "$ENABLED" = "0" ]; then
218 case "$RETVAL" in
219 0) log_daemon_msg "Stopping $DESC" "$NAME"; log_end_msg 0 ;;
220 esac
221 exit 0
223 case "$RETVAL" in
224 0) log_end_msg 0 ; exit 0 ;;
225 1) log_warning_msg "(not running)" ; exit 0 ;;
226 *) log_end_msg 1; exit 1 ;;
227 esac
229 restart|force-reload)
230 test "$ENABLED" != "0" || exit 1
231 $DAEMON --test ${CONFIG_DIR:+ -7 $CONFIG_DIR} ${DNSMASQ_OPTS:+ $DNSMASQ_OPTS} >/dev/null 2>&1
232 if [ $? -ne 0 ]; then
233 NAME="configuration syntax check"
234 RETVAL="2"
235 else
236 stop_resolvconf
237 stop
238 RETVAL="$?"
240 log_daemon_msg "Restarting $DESC" "$NAME"
241 case "$RETVAL" in
242 0|1)
243 sleep 2
244 start
245 case "$?" in
247 log_end_msg 0
248 start_resolvconf
249 exit 0
252 log_end_msg 1
253 exit 1
255 esac
258 log_end_msg 1
259 exit 1
261 esac
263 status)
264 log_daemon_msg "Checking $DESC" "$NAME"
265 status
266 case "$?" in
267 0) log_success_msg "(running)" ; exit 0 ;;
268 1) log_success_msg "(dead, pid file exists)" ; exit 1 ;;
269 3) log_success_msg "(not running)" ; exit 3 ;;
270 *) log_success_msg "(unknown)" ; exit 4 ;;
271 esac
273 dump-stats)
274 kill -s USR1 `cat /var/run/dnsmasq/$NAME.pid`
276 systemd-start-resolvconf)
277 start_resolvconf
279 systemd-stop-resolvconf)
280 stop_resolvconf
282 systemd-exec)
283 # --pid-file without argument disables writing a PIDfile, we don't need one with sytemd.
284 # Enable DBus by default because we use DBus activation with systemd.
285 exec $DAEMON --keep-in-foreground --pid-file --enable-dbus \
286 ${MAILHOSTNAME:+ -m $MAILHOSTNAME} \
287 ${MAILTARGET:+ -t $MAILTARGET} \
288 ${DNSMASQ_USER:+ -u $DNSMASQ_USER} \
289 ${DNSMASQ_INTERFACES:+ $DNSMASQ_INTERFACES} \
290 ${DHCP_LEASE:+ -l $DHCP_LEASE} \
291 ${DOMAIN_SUFFIX:+ -s $DOMAIN_SUFFIX} \
292 ${RESOLV_CONF:+ -r $RESOLV_CONF} \
293 ${CACHESIZE:+ -c $CACHESIZE} \
294 ${CONFIG_DIR:+ -7 $CONFIG_DIR} \
295 ${DNSMASQ_OPTS:+ $DNSMASQ_OPTS}
298 echo "Usage: /etc/init.d/$NAME {start|stop|restart|force-reload|dump-stats|status}" >&2
299 exit 3
301 esac
303 exit 0