4 # Required-Start: $network $remote_fs $syslog
5 # Required-Stop: $network $remote_fs $syslog
6 # Default-Start: 2 3 4 5
8 # Description: DHCP and DNS server
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
16 DESC
="DNS forwarder and DHCP server"
18 # Most configuration options in /etc/default/dnsmasq are deprecated
21 if [ -r /etc
/default
/$NAME ]; then
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
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
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
59 elif [ $1 -eq 255 ]; then
60 /bin
/echo -e " (warning)."
62 /bin
/echo -e " failed!"
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
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
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
]
84 RESOLV_CONF
=/var
/run
/dnsmasq
/resolv.conf
87 for INTERFACE
in $DNSMASQ_INTERFACE; do
88 DNSMASQ_INTERFACES
="$DNSMASQ_INTERFACES -i $INTERFACE"
91 for INTERFACE
in $DNSMASQ_EXCEPT; do
92 DNSMASQ_INTERFACES
="$DNSMASQ_INTERFACES -I $INTERFACE"
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"
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} \
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
147 if [ -x /sbin
/resolvconf
] ; then
148 echo "nameserver 127.0.0.1" |
/sbin
/resolvconf
-a lo.
$NAME
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
162 [ "$RETVAL" = 2 ] && return 2
168 if [ -x /sbin
/resolvconf
] ; then
169 /sbin
/resolvconf
-d lo.
$NAME
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
183 0) [ -e "/var/run/dnsmasq/$NAME.pid" ] && return 1 ; return 3 ;;
191 test "$ENABLED" != "0" ||
exit 0
192 log_daemon_msg
"Starting $DESC" "$NAME"
201 log_success_msg
"(already running)"
212 if [ "$ENABLED" != "0" ]; then
213 log_daemon_msg
"Stopping $DESC" "$NAME"
217 if [ "$ENABLED" = "0" ]; then
219 0) log_daemon_msg
"Stopping $DESC" "$NAME"; log_end_msg
0 ;;
224 0) log_end_msg
0 ; exit 0 ;;
225 1) log_warning_msg
"(not running)" ; exit 0 ;;
226 *) log_end_msg
1; exit 1 ;;
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"
240 log_daemon_msg
"Restarting $DESC" "$NAME"
264 log_daemon_msg
"Checking $DESC" "$NAME"
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 ;;
274 kill -s USR1
`cat /var/run/dnsmasq/$NAME.pid`
276 systemd-start-resolvconf
)
279 systemd-stop-resolvconf
)
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