summaryrefslogtreecommitdiff
path: root/package/utils/busybox/files/sysntpd
blob: cc1b4d8cc68b8ffadac43a741eae199296391258 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/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_validate_section system timeserver "${1}" \
		'server:list(host)' 'enabled:bool:1' 'enable_server:bool:0' 'use_dhcp:bool:1' 'dhcp_interface:list(string)'
}

start_service() {
	local server enabled enable_server use_dhcp dhcp_interface peer

	validate_ntp_section ntp || {
		echo "validation failed"
		return 1
	}

	[ $enabled = 0 ] && return

	[ $use_dhcp = 1 ] && get_dhcp_ntp_servers "$dhcp_interface"

	[ -z "$server" ] && 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
}

service_triggers() {
	local script name use_dhcp

	script=$(readlink -f "$initscript")
	name=$(basename ${script:-$initscript})

	procd_open_trigger
	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_close_trigger

	procd_add_validation validate_ntp_section
}