b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | #!/bin/sh /etc/rc.common |
| 2 | # Copyright (C) 2011 OpenWrt.org |
| 3 | |
| 4 | #START=98 |
| 5 | |
| 6 | USE_PROCD=1 |
| 7 | PROG=/usr/sbin/ntpd |
| 8 | HOTPLUG_SCRIPT=/usr/sbin/ntpd-hotplug |
| 9 | |
| 10 | get_dhcp_ntp_servers() { |
| 11 | local interfaces="$1" |
| 12 | local filter="*" |
| 13 | local interface ntpservers ntpserver |
| 14 | |
| 15 | for interface in $interfaces; do |
| 16 | [ "$filter" = "*" ] && filter="@.interface='$interface'" || filter="$filter,@.interface='$interface'" |
| 17 | done |
| 18 | |
| 19 | ntpservers=$(ubus call network.interface dump | jsonfilter -e "@.interface[$filter]['data']['ntpserver']") |
| 20 | |
| 21 | for ntpserver in $ntpservers; do |
| 22 | local duplicate=0 |
| 23 | local entry |
| 24 | for entry in $server; do |
| 25 | [ "$ntpserver" = "$entry" ] && duplicate=1 |
| 26 | done |
| 27 | [ "$duplicate" = 0 ] && server="$server $ntpserver" |
| 28 | done |
| 29 | } |
| 30 | |
| 31 | validate_ntp_section() { |
| 32 | uci_load_validate system timeserver "$1" "$2" \ |
| 33 | 'dhcp_interface:list(string)' \ |
| 34 | 'enable_server:bool:0' \ |
| 35 | 'enabled:bool:1' \ |
| 36 | 'interface:string' \ |
| 37 | 'server:list(host)' \ |
| 38 | 'use_dhcp:bool:1' |
| 39 | } |
| 40 | |
| 41 | start_ntpd_instance() { |
| 42 | local peer |
| 43 | |
| 44 | [ "$2" = 0 ] || { |
| 45 | echo "validation failed" |
| 46 | return 1 |
| 47 | } |
| 48 | |
| 49 | [ $enabled = 0 ] && return |
| 50 | |
| 51 | [ $use_dhcp = 1 ] && get_dhcp_ntp_servers "$dhcp_interface" |
| 52 | |
| 53 | [ -z "$server" -a "$enable_server" = "0" ] && return |
| 54 | |
| 55 | procd_open_instance |
| 56 | procd_set_param command "$PROG" -n -N |
| 57 | if [ "$enable_server" = "1" ]; then |
| 58 | procd_append_param command -l |
| 59 | [ -n "$interface" ] && { |
| 60 | local ifname |
| 61 | |
| 62 | network_get_device ifname "$interface" || \ |
| 63 | ifname="$interface" |
| 64 | procd_append_param command -I "$ifname" |
| 65 | procd_append_param netdev "$ifname" |
| 66 | } |
| 67 | fi |
| 68 | [ -x "$HOTPLUG_SCRIPT" ] && procd_append_param command -S "$HOTPLUG_SCRIPT" |
| 69 | for peer in $server; do |
| 70 | procd_append_param command -p $peer |
| 71 | done |
| 72 | # procd_set_param respawn |
| 73 | [ -x /sbin/ujail -a -e /etc/capabilities/ntpd.json ] && { |
| 74 | procd_add_jail ntpd ubus |
| 75 | procd_add_jail_mount "$HOTPLUG_SCRIPT" |
| 76 | procd_add_jail_mount "/usr/share/libubox/jshn.sh" |
| 77 | procd_add_jail_mount "/usr/bin/env" |
| 78 | procd_add_jail_mount "/usr/bin/jshn" |
| 79 | procd_add_jail_mount "/bin/ubus" |
| 80 | procd_set_param capabilities /etc/capabilities/ntpd.json |
| 81 | procd_set_param user ntp |
| 82 | procd_set_param group ntp |
| 83 | procd_set_param no_new_privs 1 |
| 84 | } |
| 85 | procd_close_instance |
| 86 | } |
| 87 | |
| 88 | start_service() { |
| 89 | . /lib/functions/network.sh |
| 90 | validate_ntp_section ntp start_ntpd_instance |
| 91 | } |
| 92 | |
| 93 | service_triggers() { |
| 94 | local script name use_dhcp enable_server interface |
| 95 | |
| 96 | script=$(readlink -f "$initscript") |
| 97 | name=$(basename ${script:-$initscript}) |
| 98 | |
| 99 | procd_add_config_trigger "config.change" "system" /etc/init.d/$name reload |
| 100 | |
| 101 | config_load system |
| 102 | config_get use_dhcp ntp use_dhcp 1 |
| 103 | |
| 104 | [ $use_dhcp = 1 ] && { |
| 105 | local dhcp_interface |
| 106 | config_get dhcp_interface ntp dhcp_interface |
| 107 | |
| 108 | if [ -n "$dhcp_interface" ]; then |
| 109 | for n in $dhcp_interface; do |
| 110 | procd_add_interface_trigger "interface.*" $n /etc/init.d/$name reload |
| 111 | done |
| 112 | else |
| 113 | procd_add_raw_trigger "interface.*" 1000 /etc/init.d/$name reload |
| 114 | fi |
| 115 | } |
| 116 | |
| 117 | config_get_bool enable_server ntp enable_server 0 |
| 118 | config_get interface ntp interface |
| 119 | |
| 120 | [ $enable_server -eq 1 ] && [ -n "$interface" ] && { |
| 121 | local ifname |
| 122 | |
| 123 | network_get_device ifname "$interface" || \ |
| 124 | ifname="$interface" |
| 125 | procd_add_interface_trigger "interface.*" "$ifname" \ |
| 126 | /etc/init.d/"$name" reload |
| 127 | } |
| 128 | |
| 129 | procd_add_validation validate_ntp_section |
| 130 | } |