summaryrefslogtreecommitdiffstats
path: root/package/base-files/files/etc/hotplug.d/iface/40-rdate
blob: cf56c02ac53547cbeb9fb724d28ae9d13ddaaec7 (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
uci_get_one()
{
	for var in "$@"; do
		uci -P /var/state get "$var" 2>/dev/null && break
	done
}

rand()
{
	random=$(awk 'BEGIN { srand(); print int(rand() * 10 + 1); }')
}

sync_rdate()
{
	local servers=$(uci_get_one "network.$INTERFACE.lease_timesrv" \
		"system.@rdate[0].server")

	if [ -n "$servers" ]; then
		match=0
		tries=3
		rand

		while [ $match = 0 ] && [ $tries != 0 ]; do
			for server in $servers; do
				if [ $((--random)) = 0 ]; then
					rdate -s $server >/dev/null 2>/dev/null && {
						logger -t rdate "Synced with $server"
						match=1
					} || {
						logger -t rdate "Failed to sync with $server"
						let tries="$tries - 1"
						rand
					}

					break
				fi
			done
		done
	else
		logger -t rdate "No usable time server found"
	fi
}

case "$ACTION" in
	ifup) route -n | grep -q ^0.0.0.0 && sync_rdate;;
esac