You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
1.9 KiB

  1. #!/bin/sh /etc/rc.common
  2. # Copyright (C) 2006-2011 OpenWrt.org
  3. START=65
  4. STOP=65
  5. USE_PROCD=1
  6. PROG=/sbin/ntpd
  7. HOTPLUG_HELPER=/usr/sbin/ntpd.hotplug-helper
  8. config_file=/var/run/ntpd.conf
  9. trunc() {
  10. echo -n "" > $config_file
  11. }
  12. emit() {
  13. echo -e "$@" >> $config_file
  14. }
  15. validate_ntp_section() {
  16. uci_validate_section system timeserver "${1}" \
  17. 'server:list(host)' 'enabled:bool:1' 'enable_server:bool:0' \
  18. 'interface:list(string)'
  19. }
  20. start_service() {
  21. local server enabled enable_server interface intf
  22. validate_ntp_section ntp || {
  23. echo "validation failed"
  24. return 1
  25. }
  26. [ "$enabled" = 0 ] && return
  27. [ -z "$server" -a "$enable_server" = 0 ] && return
  28. # not sure that the interfaces enumerated should be validated,
  29. # since some of them might be dynamic interfaces (like IPsec
  30. # tunnels) which aren't known by ubus.
  31. trunc
  32. emit "driftfile /var/lib/ntp/ntp.drift\n"
  33. if [ "$enable_server" != 0 ]; then
  34. emit "restrict default limited kod nomodify notrap nopeer"
  35. emit "restrict -6 default limited kod nomodify notrap nopeer"
  36. else
  37. emit "restrict -4 default noserve"
  38. emit "restrict -6 default noserve"
  39. fi
  40. emit "restrict source noquery"
  41. emit "\n# No limits for local monitoring"
  42. emit "restrict 127.0.0.1"
  43. emit "restrict -6 ::1\n"
  44. if [ -n "$interface" ]; then
  45. local loopback=$(ubus call network.interface dump | jsonfilter -e "@.interface[@.interface='loopback']['device']")
  46. local saw_lo=
  47. for intf in $interface; do
  48. emit "interface listen $intf"
  49. [ "$intf" = "$loopback" ] && saw_lo=1
  50. done
  51. [ -z "$saw_lo" ] && emit "interface listen $loopback"
  52. emit ""
  53. fi
  54. emit "server $server iburst"
  55. mkdir -p /var/lib/ntp
  56. chown -R ntp:ntp /var/lib/ntp
  57. procd_open_instance
  58. procd_set_param command $PROG -g -u ntp:ntp -p /var/run/ntpd.pid -n \
  59. -c $config_file
  60. procd_close_instance
  61. procd_open_instance
  62. procd_set_param command $HOTPLUG_HELPER
  63. procd_close_instance
  64. }