[package] base-files: save the connect time of interfaces in uci state
[openwrt.git] / package / base-files / files / etc / hotplug.d / iface / 40-rdate
1 IFACE_GLOBAL=$(uci_get "system.@rdate[0].interface")
2 SERVERS=
3 MAX=0
4 SYNCED=
5
6 do_rdate()
7 {
8 local server="$1"
9
10 rdate -s "$server" >/dev/null 2>/dev/null && {
11 logger -t rdate "Synced with $server"
12 SYNCED="$server"
13 } || {
14 logger -t rdate "Failed to sync with $server"
15 }
16 }
17
18 add_server()
19 {
20 local section="$1"
21
22 local server
23 config_get server "$section" hostname
24 [ -z "$server" ] && return
25
26 local iface
27 config_get iface "$section" interface
28 [ -z "$iface" ] && iface=$IFACE_GLOBAL
29 [ -n "$iface" ] && {
30 [ "$iface" = "$INTERFACE" ] || return
31 }
32
33 SERVERS="${SERVERS} $server"; : $((MAX++))
34 }
35
36 sync_time()
37 {
38 local server
39 server=$(uci_get_state "network.$INTERFACE.lease_timesrv")
40 [ -n "$server" ] && do_rdate "$server"
41 [ -n "$SYNCED" ] && return
42
43 config_load timeserver
44 config_foreach add_server timeserver
45
46 local servers
47 while [ $MAX -gt 0 ] && [ -z "$SYNCED" ]; do
48 unset servers; random=$(awk "BEGIN { srand(); print int(rand() * $MAX + 1); }")
49 for server in $SERVERS; do
50 [ $((--random)) -eq 0 ] && { do_rdate "$server"; continue; }
51 servers="${servers} $server"
52 done
53 SERVERS="${servers}"; : $((MAX--))
54 done
55
56 [ -z "$SYNCED" ] && logger -t rdate "No usable time server for $INTERFACE found"
57 }
58
59 case "${ACTION:-ifup}" in
60 ifup)
61 sync_time
62 ;;
63 esac
This page took 0.043811 seconds and 5 git commands to generate.