unbound.initd 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #!/usr/bin/openrc-run
  2. # Copyright 1999-2018 Gentoo Foundation
  3. # Distributed under the terms of the GNU General Public License v2
  4. UNBOUND_BINARY=${UNBOUND_BINARY:-"/usr/bin/unbound"}
  5. UNBOUND_CACHEFILE=${UNBOUND_CACHEFILE:-"/var/lib/unbound/${SVCNAME}.cache"}
  6. UNBOUND_CHECKCONF=${UNBOUND_CHECKCONF:-"/usr/bin/unbound-checkconf"}
  7. UNBOUND_CONFFILE=${UNBOUND_CONFFILE:-"/etc/unbound/${SVCNAME}.conf"}
  8. UNBOUND_CONTROL=${UNBOUND_CONTROL:-"/usr/bin/unbound-control"}
  9. UNBOUND_PIDFILE=${UNBOUND_PIDFILE:-"/run/unbound.pid"}
  10. UNBOUND_SSDARGS=${UNBOUND_SSDARGS:-"--wait 1000"}
  11. UNBOUND_TERMTIMEOUT=${UNBOUND_TERMTIMEOUT:-"TERM/25/KILL/5"}
  12. UNBOUND_OPTS=${UNBOUND_OPTS:-""}
  13. UNBOUND_LOAD_CACHE_TIMEOUT=${UNBOUND_LOAD_CACHE_TIMEOUT:-"30"}
  14. getconfig() {
  15. local key="$1"
  16. local value_default="$2"
  17. local value=
  18. if service_started ; then
  19. value="$(service_get_value "${key}")"
  20. fi
  21. if [ -z "${value}" ] && [ -n "${UNBOUND_CONFFILE}" ] && [ -r "${UNBOUND_CONFFILE}" ] ; then
  22. value=$("${UNBOUND_CHECKCONF}" -o ${key} "${UNBOUND_CONFFILE}")
  23. fi
  24. if [ -z "${value}" ] ; then
  25. # Value not explicitly set in the configfile or configfile does not exist
  26. # or is not readable
  27. echo "${value_default}"
  28. else
  29. echo "${value}"
  30. fi
  31. return 0
  32. }
  33. command=${UNBOUND_BINARY}
  34. command_args="${UNBOUND_OPTS} -c \"${UNBOUND_CONFFILE}\""
  35. start_stop_daemon_args="${UNBOUND_SSDARGS}"
  36. pidfile="$(getconfig pidfile /run/unbound.pid)"
  37. retry="${UNBOUND_TERMTIMEOUT}"
  38. required_files="${UNBOUND_CONFFILE}"
  39. name="unbound daemon"
  40. extra_commands="configtest"
  41. extra_started_commands="reload save_cache"
  42. description="unbound is a Domain Name Server (DNS) that is used to resolve host names to IP address."
  43. description_configtest="Run syntax tests for configuration files only."
  44. description_reload="Kills all children and reloads the configuration."
  45. description_save_cache="Saves the current cache to disk."
  46. depend() {
  47. use net logger
  48. provide dns
  49. after auth-dns
  50. }
  51. configtest() {
  52. local _config_status=
  53. ebegin "Checking ${SVCNAME} configuration"
  54. "${UNBOUND_CHECKCONF}" "${UNBOUND_CONFFILE}" 1>/dev/null 2>&1
  55. _config_status=$?
  56. if [ ${_config_status} -ne 0 ] ; then
  57. # Run command again but this time we will show the output
  58. # Ugly, but ...
  59. "${UNBOUND_CHECKCONF}" "${UNBOUND_CONFFILE}"
  60. else
  61. if [ -n "${UNBOUND_PRESERVE_CACHE}" ] ; then
  62. local _is_control_enabled=$(getconfig control-enable no)
  63. if [ "${_is_control_enabled}" != "yes" ] ; then
  64. eerror "Cannot preserve cache: control-enable is 'no' in the config file!"
  65. _config_status=2
  66. fi
  67. fi
  68. fi
  69. eend ${_config_status} "failed, please correct errors above"
  70. }
  71. save_cache() {
  72. if [ "${RC_CMD}" != "restart" ] ; then
  73. UNBOUND_PRESERVE_CACHE=1 configtest || return 1
  74. fi
  75. ebegin "Saving cache to '${UNBOUND_CACHEFILE}'"
  76. ${UNBOUND_CONTROL} -c "${UNBOUND_CONFFILE}" dump_cache > "${UNBOUND_CACHEFILE}"
  77. eend $?
  78. }
  79. start_pre() {
  80. if [ "${RC_CMD}" != "restart" ] ; then
  81. configtest || return 1
  82. fi
  83. }
  84. start_post() {
  85. if [ -n "${UNBOUND_PRESERVE_CACHE}" ] ; then
  86. if [ -s "${UNBOUND_CACHEFILE}" ] ; then
  87. ebegin "Loading cache from '${UNBOUND_CACHEFILE}'"
  88. # Loading cache can fail which would block this runscript.
  89. # Using `timeout` from coreutils will be our safeguard ...
  90. timeout -k 5 ${UNBOUND_LOAD_CACHE_TIMEOUT} ${UNBOUND_CONTROL} -q -c "${UNBOUND_CONFFILE}" load_cache < "${UNBOUND_CACHEFILE}"
  91. eend $?
  92. else
  93. ewarn "Loading cache from '${UNBOUND_CACHEFILE}' skipped: File does not exists or is empty!"
  94. fi
  95. fi
  96. # It is not a fatal error if preserved cache could not be loaded
  97. return 0
  98. }
  99. stop_pre() {
  100. if [ "${RC_CMD}" = "restart" ] ; then
  101. configtest || return 1
  102. fi
  103. if [ -n "${UNBOUND_PRESERVE_CACHE}" ] ; then
  104. save_cache
  105. fi
  106. # It is not a fatal error if cache cannot be preserved
  107. return 0
  108. }
  109. reload() {
  110. configtest || return 1
  111. ebegin "Reloading ${SVCNAME}"
  112. start-stop-daemon --signal HUP --pidfile "${pidfile}"
  113. eend $?
  114. }