sysntpd 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #!/bin/sh /etc/rc.common
  2. # Copyright (C) 2011 OpenWrt.org
  3. START=98
  4. USE_PROCD=1
  5. PROG=/usr/sbin/ntpd
  6. HOTPLUG_SCRIPT=/usr/sbin/ntpd-hotplug
  7. get_dhcp_ntp_servers() {
  8. local interfaces="$1"
  9. local filter="*"
  10. local interface ntpservers ntpserver
  11. for interface in $interfaces; do
  12. [ "$filter" = "*" ] && filter="@.interface='$interface'" || filter="$filter,@.interface='$interface'"
  13. done
  14. ntpservers=$(ubus call network.interface dump | jsonfilter -e "@.interface[$filter]['data']['ntpserver']")
  15. for ntpserver in $ntpservers; do
  16. local duplicate=0
  17. local entry
  18. for entry in $server; do
  19. [ "$ntpserver" = "$entry" ] && duplicate=1
  20. done
  21. [ "$duplicate" = 0 ] && server="$server $ntpserver"
  22. done
  23. }
  24. validate_ntp_section() {
  25. uci_validate_section system timeserver "${1}" \
  26. 'server:list(host)' 'enabled:bool:1' 'enable_server:bool:0' 'use_dhcp:bool:1' 'dhcp_interface:list(string)'
  27. }
  28. start_service() {
  29. local server enabled enable_server use_dhcp dhcp_interface peer
  30. validate_ntp_section ntp || {
  31. echo "validation failed"
  32. return 1
  33. }
  34. [ $enabled = 0 ] && return
  35. [ $use_dhcp = 1 ] && get_dhcp_ntp_servers "$dhcp_interface"
  36. [ -z "$server" ] && return
  37. procd_open_instance
  38. procd_set_param command "$PROG" -n -N
  39. [ "$enable_server" = "1" ] && procd_append_param command -l
  40. [ -x "$HOTPLUG_SCRIPT" ] && procd_append_param command -S "$HOTPLUG_SCRIPT"
  41. for peer in $server; do
  42. procd_append_param command -p $peer
  43. done
  44. procd_set_param respawn
  45. procd_close_instance
  46. }
  47. service_triggers() {
  48. local script name use_dhcp
  49. script=$(readlink -f "$initscript")
  50. name=$(basename ${script:-$initscript})
  51. procd_add_config_trigger "config.change" "system" /etc/init.d/$name reload
  52. config_load system
  53. config_get use_dhcp ntp use_dhcp 1
  54. [ $use_dhcp = 1 ] && {
  55. local dhcp_interface
  56. config_get dhcp_interface ntp dhcp_interface
  57. if [ -n "$dhcp_interface" ]; then
  58. for n in $dhcp_interface; do
  59. procd_add_interface_trigger "interface.*" $n /etc/init.d/$name reload
  60. done
  61. else
  62. procd_add_raw_trigger "interface.*" 1000 /etc/init.d/$name reload
  63. fi
  64. }
  65. procd_add_validation validate_ntp_section
  66. }