summaryrefslogtreecommitdiffstats
path: root/package/base-files/files/etc/hotplug.d/iface/40-rdate
diff options
context:
space:
mode:
authorjow <jow@3c298f89-4303-0410-b956-a3cf2f4a3e73>2010-03-27 17:13:33 +0000
committerjow <jow@3c298f89-4303-0410-b956-a3cf2f4a3e73>2010-03-27 17:13:33 +0000
commit1ac5ad687b35793ca0563d7cb9a4ce59e314a483 (patch)
tree5ea210b471db86e04135159e5ef7f40d3eb083da /package/base-files/files/etc/hotplug.d/iface/40-rdate
parent2dde85630069fe4e531ce5c8bd41aeb7f5c00222 (diff)
[package] base-files: add a hotplug handler for rdate
git-svn-id: svn://svn.openwrt.org/openwrt/trunk@20518 3c298f89-4303-0410-b956-a3cf2f4a3e73
Diffstat (limited to 'package/base-files/files/etc/hotplug.d/iface/40-rdate')
-rw-r--r--package/base-files/files/etc/hotplug.d/iface/40-rdate46
1 files changed, 46 insertions, 0 deletions
diff --git a/package/base-files/files/etc/hotplug.d/iface/40-rdate b/package/base-files/files/etc/hotplug.d/iface/40-rdate
new file mode 100644
index 000000000..cf56c02ac
--- /dev/null
+++ b/package/base-files/files/etc/hotplug.d/iface/40-rdate
@@ -0,0 +1,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