51c95dfedd58798ff362009d70d929ae912e6629
[pti/rtp.git] / meta-stx / recipes-support / dnsmasq / dnsmasq / init
1 #!/bin/sh
2 DAEMON=/usr/bin/dnsmasq
3 NAME=dnsmasq
4 DESC="DNS forwarder and DHCP server"
5 ARGS="-7 /etc/dnsmasq.d"
6
7 test -f $DAEMON || exit 0
8
9 set -e
10
11 if [ -r /etc/default/$NAME ]
12 then
13         . /etc/default/$NAME
14 fi
15
16 DNSMASQ_CONF="/etc/dnsmasq.conf"
17 test "/etc/dnsmasq.d/*" != '/etc/dnsmasq.d/*' && DNSMASQ_CONF="${DNSMASQ_CONF} /etc/dnsmasq.d/*"
18
19 test -z "${PIDFILE}" && PIFILE="/run/dnsmasq.pid"
20
21 if [ -z "$IGNORE_RESOLVCONF" ]
22 then
23         egrep -h -q '^no-resolv' ${DNSMASQ_CONF} && IGNORE_RESOLVCONF="yes"
24 fi
25
26 # RESOLV_CONF:
27 # If the resolvconf package is installed then use the resolv conf file
28 # that it provides as the default.  Otherwise use /etc/resolv.conf as
29 # the default.
30 #
31 # If IGNORE_RESOLVCONF is set in /etc/default/dnsmasq or an explicit
32 # filename is set there then this inhibits the use of the resolvconf-provided
33 # information.
34 #
35 # Note that if the resolvconf package is installed it is not possible to
36 # override it just by configuration in /etc/dnsmasq.conf, it is necessary
37 # to set IGNORE_RESOLVCONF=yes in /etc/default/dnsmasq.
38
39 test -z "$RESOLV_CONF" -a "$IGNORE_RESOLVCONF" != "yes" -a -x /sbin/resolvconf && \
40         RESOLV_CONF=/run/dnsmasq/resolv.conf
41
42 start_resolvconf()
43 {
44         if [ "$IGNORE_RESOLVCONF" != "yes" -a -x /sbin/resolvconf ]
45         then
46                 echo "nameserver 127.0.0.1" | /sbin/resolvconf -a lo.$NAME
47         fi
48         :
49 }
50
51 stop_resolvconf()
52 {
53         if [ "$IGNORE_RESOLVCONF" != "yes" -a -x /sbin/resolvconf ]
54         then
55                 /sbin/resolvconf -d lo.$NAME
56         fi
57         :
58 }
59
60 case "$1" in
61     start)
62         echo -n "starting $DESC: $NAME... "
63         test -d /var/lib/misc/ || mkdir /var/lib/misc/
64         start-stop-daemon -S -x $DAEMON -- $ARGS \
65                 ${RESOLV_CONF:+ -r $RESOLV_CONF} \
66                 ${PIDFILE:+ -x $PIDFILE}
67         test $? -eq 0 && start_resolvconf
68         echo "done."
69         ;;
70     stop)
71         echo -n "stopping $DESC: $NAME... "
72         stop_resolvconf
73         start-stop-daemon -K -x $DAEMON
74         echo "done."
75         ;;
76     status)
77         echo -n "dnsmasq "
78         start-stop-daemon -q -K -t -x $DAEMON
79         RET=$?
80         if [ "$RET" = "0" ]; then
81                 PID=`cat ${PIDFILE}`
82                 echo "($PID) is running"
83         else
84                 echo "is not running"
85                 exit $RET
86         fi
87         ;;
88     restart)
89         echo "restarting $DESC: $NAME... "
90         $0 stop
91         $0 start
92         echo "done."
93         ;;
94     reload)
95         echo -n "reloading $DESC: $NAME... "
96         killall -HUP $(basename ${DAEMON})
97         echo "done."
98         ;;
99     systemd-start-resolvconf)
100         start_resolvconf
101         ;;
102     systemd-stop-resolvconf)
103         stop_resolvconf
104         ;;
105     systemd-exec)
106         test -d /var/lib/misc/ || mkdir /var/lib/misc/
107         exec $DAEMON --keep-in-foreground $ARGS \
108                 ${RESOLV_CONF:+ -r $RESOLV_CONF} \
109                 ${PIDFILE:+ -x $PIDFILE}
110         ;;
111     *)
112         echo "Usage: $0 {start|stop|status|restart|reload}"
113         exit 1
114         ;;
115 esac
116
117 exit 0