sysntpd 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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_load_validate system timeserver "$1" "$2" \
  26. 'dhcp_interface:list(string)' \
  27. 'enable_server:bool:0' \
  28. 'enabled:bool:1' \
  29. 'interface:string' \
  30. 'server:list(host)' \
  31. 'use_dhcp:bool:1'
  32. }
  33. start_ntpd_instance() {
  34. local peer
  35. [ "$2" = 0 ] || {
  36. echo "validation failed"
  37. return 1
  38. }
  39. [ $enabled = 0 ] && return
  40. [ $use_dhcp = 1 ] && get_dhcp_ntp_servers "$dhcp_interface"
  41. [ -z "$server" -a "$enable_server" = "0" ] && return
  42. procd_open_instance
  43. procd_set_param command "$PROG" -n -N
  44. if [ "$enable_server" = "1" ]; then
  45. procd_append_param command -l
  46. [ -n "$interface" ] && {
  47. local ifname
  48. network_get_device ifname "$interface" || \
  49. ifname="$interface"
  50. procd_append_param command -I "$ifname"
  51. procd_append_param netdev "$ifname"
  52. }
  53. fi
  54. [ -x "$HOTPLUG_SCRIPT" ] && procd_append_param command -S "$HOTPLUG_SCRIPT"
  55. for peer in $server; do
  56. procd_append_param command -p $peer
  57. done
  58. procd_set_param respawn
  59. [ -x /sbin/ujail -a -e /etc/capabilities/ntpd.json ] && {
  60. procd_add_jail ntpd ubus
  61. procd_add_jail_mount "$HOTPLUG_SCRIPT"
  62. procd_add_jail_mount "/usr/share/libubox/jshn.sh"
  63. procd_add_jail_mount "/usr/bin/env"
  64. procd_add_jail_mount "/usr/bin/jshn"
  65. procd_add_jail_mount "/bin/ubus"
  66. procd_set_param capabilities /etc/capabilities/ntpd.json
  67. procd_set_param user ntp
  68. procd_set_param group ntp
  69. procd_set_param no_new_privs 1
  70. }
  71. procd_close_instance
  72. }
  73. start_service() {
  74. . /lib/functions/network.sh
  75. validate_ntp_section ntp start_ntpd_instance
  76. }
  77. service_triggers() {
  78. local script name use_dhcp enable_server interface
  79. script=$(readlink -f "$initscript")
  80. name=$(basename ${script:-$initscript})
  81. procd_add_config_trigger "config.change" "system" /etc/init.d/$name reload
  82. config_load system
  83. config_get use_dhcp ntp use_dhcp 1
  84. [ $use_dhcp = 1 ] && {
  85. local dhcp_interface
  86. config_get dhcp_interface ntp dhcp_interface
  87. if [ -n "$dhcp_interface" ]; then
  88. for n in $dhcp_interface; do
  89. procd_add_interface_trigger "interface.*" $n /etc/init.d/$name reload
  90. done
  91. else
  92. procd_add_raw_trigger "interface.*" 1000 /etc/init.d/$name reload
  93. fi
  94. }
  95. config_get_bool enable_server ntp enable_server 0
  96. config_get interface ntp interface
  97. [ $enable_server -eq 1 ] && [ -n "$interface" ] && {
  98. local ifname
  99. network_get_device ifname "$interface" || \
  100. ifname="$interface"
  101. procd_add_interface_trigger "interface.*" "$ifname" \
  102. /etc/init.d/"$name" reload
  103. }
  104. procd_add_validation validate_ntp_section
  105. }