Tarun Kundu | 12e3b2e | 2024-08-15 16:16:53 -0700 | [diff] [blame^] | 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 |
| 10 | |
| 11 | # Don't exit on error status |
| 12 | set +e |
| 13 | |
| 14 | # The following test ensures the dnsmasq service is not started, when the |
| 15 | # package 'dnsmasq' is removed but not purged, even if the dnsmasq-base |
| 16 | # package is still in place. |
| 17 | if [ -r /usr/share/dnsmasq/init-system-common ]; then |
| 18 | # 'dnsmasq' is installed: source initial code used also with systemd. |
| 19 | . /usr/share/dnsmasq/init-system-common |
| 20 | else |
| 21 | # 'dnsmasq' is removed but not purged, or damaged: do nothing. |
| 22 | exit 0 |
| 23 | fi |
| 24 | |
| 25 | # Double-check 'dnsmasq-base' or 'dnsmasq-base-lua' is installed. |
| 26 | test -x ${DAEMON} || exit 0 |
| 27 | |
| 28 | # Source the SysV init-functions which should always be available. |
| 29 | . /lib/lsb/init-functions || exit 0 |
| 30 | |
| 31 | start() |
| 32 | { |
| 33 | # Return |
| 34 | # 0 if daemon has been started |
| 35 | # 1 if daemon was already running |
| 36 | # 2 if daemon could not be started |
| 37 | |
| 38 | # /run may be volatile, so we need to ensure that |
| 39 | # /run/dnsmasq exists here as well as in postinst |
| 40 | if [ ! -d /run/dnsmasq ]; then |
| 41 | mkdir /run/dnsmasq || { [ -d /run/dnsmasq ] || return 2 ; } |
| 42 | chown dnsmasq:nogroup /run/dnsmasq || return 2 |
| 43 | fi |
| 44 | [ -x /sbin/restorecon ] && /sbin/restorecon /run/dnsmasq |
| 45 | |
| 46 | start-stop-daemon --start --quiet --pidfile /run/dnsmasq/${NAME}${INSTANCE:+.${INSTANCE}}.pid --exec ${DAEMON} --test > /dev/null || return 1 |
| 47 | start-stop-daemon --start --quiet --pidfile /run/dnsmasq/${NAME}${INSTANCE:+.${INSTANCE}}.pid --exec ${DAEMON} -- \ |
| 48 | -x /run/dnsmasq/${NAME}${INSTANCE:+.${INSTANCE}}.pid \ |
| 49 | ${MAILHOSTNAME:+ -m ${MAILHOSTNAME}} \ |
| 50 | ${MAILTARGET:+ -t ${MAILTARGET}} \ |
| 51 | ${DNSMASQ_USER:+ -u ${DNSMASQ_USER}} \ |
| 52 | ${DNSMASQ_INTERFACES:+ ${DNSMASQ_INTERFACES}} \ |
| 53 | ${DHCP_LEASE:+ -l ${DHCP_LEASE}} \ |
| 54 | ${DOMAIN_SUFFIX:+ -s ${DOMAIN_SUFFIX}} \ |
| 55 | ${RESOLV_CONF:+ -r ${RESOLV_CONF}} \ |
| 56 | ${CACHESIZE:+ -c ${CACHESIZE}} \ |
| 57 | ${CONFIG_DIR:+ -7 ${CONFIG_DIR}} \ |
| 58 | ${DNSMASQ_OPTS:+ ${DNSMASQ_OPTS}} \ |
| 59 | || return 2 |
| 60 | } |
| 61 | |
| 62 | stop() |
| 63 | { |
| 64 | # Return |
| 65 | # 0 if daemon has been stopped |
| 66 | # 1 if daemon was already stopped |
| 67 | # 2 if daemon could not be stopped |
| 68 | # other if a failure occurred |
| 69 | start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile /run/dnsmasq/${NAME}${INSTANCE:+.${INSTANCE}}.pid --name ${NAME} |
| 70 | } |
| 71 | |
| 72 | status() |
| 73 | { |
| 74 | # Return |
| 75 | # 0 if daemon is running |
| 76 | # 1 if daemon is dead and pid file exists |
| 77 | # 3 if daemon is not running |
| 78 | # 4 if daemon status is unknown |
| 79 | start-stop-daemon --start --quiet --pidfile /run/dnsmasq/${NAME}${INSTANCE:+.${INSTANCE}}.pid --exec ${DAEMON} --test > /dev/null |
| 80 | case "${?}" in |
| 81 | 0) [ -e "/run/dnsmasq/${NAME}${INSTANCE:+.${INSTANCE}}.pid" ] && return 1 ; return 3 ;; |
| 82 | 1) return 0 ;; |
| 83 | *) return 4 ;; |
| 84 | esac |
| 85 | } |
| 86 | |
| 87 | case "${1}" in |
| 88 | start) |
| 89 | log_daemon_msg "Starting ${DESC}" "${NAME}${INSTANCE:+.${INSTANCE}}" |
| 90 | start |
| 91 | case "${?}" in |
| 92 | 0) |
| 93 | log_end_msg 0 |
| 94 | start_resolvconf |
| 95 | exit 0 |
| 96 | ;; |
| 97 | 1) |
| 98 | log_success_msg "(already running)" |
| 99 | exit 0 |
| 100 | ;; |
| 101 | *) |
| 102 | log_end_msg 1 |
| 103 | exit 1 |
| 104 | ;; |
| 105 | esac |
| 106 | ;; |
| 107 | stop) |
| 108 | stop_resolvconf |
| 109 | log_daemon_msg "Stopping ${DESC}" "${NAME}${INSTANCE:+.${INSTANCE}}" |
| 110 | stop |
| 111 | RETVAL="${?}" |
| 112 | case "${RETVAL}" in |
| 113 | 0) log_end_msg 0 ; exit 0 ;; |
| 114 | 1) log_warning_msg "(not running)" ; exit 0 ;; |
| 115 | *) log_end_msg 1; exit 1 ;; |
| 116 | esac |
| 117 | ;; |
| 118 | restart|force-reload) |
| 119 | checkconfig |
| 120 | if [ ${?} -ne 0 ]; then |
| 121 | NAME="configuration syntax check" |
| 122 | RETVAL="2" |
| 123 | else |
| 124 | stop_resolvconf |
| 125 | stop |
| 126 | RETVAL="${?}" |
| 127 | fi |
| 128 | log_daemon_msg "Restarting ${DESC}" "${NAME}${INSTANCE:+.${INSTANCE}}" |
| 129 | case "${RETVAL}" in |
| 130 | 0|1) |
| 131 | sleep 2 |
| 132 | start |
| 133 | case "${?}" in |
| 134 | 0) |
| 135 | log_end_msg 0 |
| 136 | start_resolvconf |
| 137 | exit 0 |
| 138 | ;; |
| 139 | *) |
| 140 | log_end_msg 1 |
| 141 | exit 1 |
| 142 | ;; |
| 143 | esac |
| 144 | ;; |
| 145 | *) |
| 146 | log_end_msg 1 |
| 147 | exit 1 |
| 148 | ;; |
| 149 | esac |
| 150 | ;; |
| 151 | status) |
| 152 | log_daemon_msg "Checking ${DESC}" "${NAME}${INSTANCE:+.${INSTANCE}}" |
| 153 | status |
| 154 | case "${?}" in |
| 155 | 0) log_success_msg "(running)" ; exit 0 ;; |
| 156 | 1) log_success_msg "(dead, pid file exists)" ; exit 1 ;; |
| 157 | 3) log_success_msg "(not running)" ; exit 3 ;; |
| 158 | *) log_success_msg "(unknown)" ; exit 4 ;; |
| 159 | esac |
| 160 | ;; |
| 161 | dump-stats) |
| 162 | kill -s USR1 `cat /run/dnsmasq/${NAME}${INSTANCE:+.${INSTANCE}}.pid` |
| 163 | ;; |
| 164 | *) |
| 165 | echo "Usage: /etc/init.d/${NAME} {start|stop|restart|force-reload|dump-stats|status}" >&2 |
| 166 | exit 3 |
| 167 | ;; |
| 168 | esac |
| 169 | |
| 170 | exit 0 |