Openwrt/package/utils/busybox/files/sysntpd
Jeffery To d13e86d4c2 procd: Add wrapper for uci_validate_section()
This adds a wrapper (uci_load_validate) for uci_validate_section() that
allows callers (through a callback function) to access the values set by
uci_validate_section(), without having to manually declare a
(potentially long) list of local variables.

The callback function receives two arguments when called, the config
section name and the return value of uci_validate_section().

If no callback function is given, then the wrapper exits with the value
returned by uci_validate_section().

This also updates several init scripts to use the new wrapper function.

Signed-off-by: Jeffery To <jeffery.to@gmail.com>
2019-01-22 09:05:59 +01:00

91 lines
2.1 KiB
Bash
Executable File

#!/bin/sh /etc/rc.common
# Copyright (C) 2011 OpenWrt.org
START=98
USE_PROCD=1
PROG=/usr/sbin/ntpd
HOTPLUG_SCRIPT=/usr/sbin/ntpd-hotplug
get_dhcp_ntp_servers() {
local interfaces="$1"
local filter="*"
local interface ntpservers ntpserver
for interface in $interfaces; do
[ "$filter" = "*" ] && filter="@.interface='$interface'" || filter="$filter,@.interface='$interface'"
done
ntpservers=$(ubus call network.interface dump | jsonfilter -e "@.interface[$filter]['data']['ntpserver']")
for ntpserver in $ntpservers; do
local duplicate=0
local entry
for entry in $server; do
[ "$ntpserver" = "$entry" ] && duplicate=1
done
[ "$duplicate" = 0 ] && server="$server $ntpserver"
done
}
validate_ntp_section() {
uci_load_validate system timeserver "$1" "$2" \
'server:list(host)' 'enabled:bool:1' 'enable_server:bool:0' 'use_dhcp:bool:1' 'dhcp_interface:list(string)'
}
start_ntpd_instance() {
local peer
[ "$2" = 0 ] || {
echo "validation failed"
return 1
}
[ $enabled = 0 ] && return
[ $use_dhcp = 1 ] && get_dhcp_ntp_servers "$dhcp_interface"
[ -z "$server" -a "$enable_server" = "0" ] && return
procd_open_instance
procd_set_param command "$PROG" -n -N
[ "$enable_server" = "1" ] && procd_append_param command -l
[ -x "$HOTPLUG_SCRIPT" ] && procd_append_param command -S "$HOTPLUG_SCRIPT"
for peer in $server; do
procd_append_param command -p $peer
done
procd_set_param respawn
procd_close_instance
}
start_service() {
validate_ntp_section ntp start_ntpd_instance
}
service_triggers() {
local script name use_dhcp
script=$(readlink -f "$initscript")
name=$(basename ${script:-$initscript})
procd_add_config_trigger "config.change" "system" /etc/init.d/$name reload
config_load system
config_get use_dhcp ntp use_dhcp 1
[ $use_dhcp = 1 ] && {
local dhcp_interface
config_get dhcp_interface ntp dhcp_interface
if [ -n "$dhcp_interface" ]; then
for n in $dhcp_interface; do
procd_add_interface_trigger "interface.*" $n /etc/init.d/$name reload
done
else
procd_add_raw_trigger "interface.*" 1000 /etc/init.d/$name reload
fi
}
procd_add_validation validate_ntp_section
}