base-files: add a hotplug handler for rdate
authorJo-Philipp Wich <jow@openwrt.org>
Sat, 27 Mar 2010 17:13:33 +0000 (17:13 +0000)
committerJo-Philipp Wich <jow@openwrt.org>
Sat, 27 Mar 2010 17:13:33 +0000 (17:13 +0000)
SVN-Revision: 20518

package/base-files/Makefile
package/base-files/files/etc/config/system
package/base-files/files/etc/hotplug.d/iface/40-rdate [new file with mode: 0644]

index 88ab6101872f34d399a5193f9b76e3029b0e63e4..856340f667b925eb598e8afa136451f79cb2de29 100644 (file)
@@ -10,7 +10,7 @@ include $(TOPDIR)/rules.mk
 include $(INCLUDE_DIR)/kernel.mk
 
 PKG_NAME:=base-files
-PKG_RELEASE:=40
+PKG_RELEASE:=41
 
 PKG_FILE_DEPENDS:=$(PLATFORM_DIR)/ $(GENERIC_PLATFORM_DIR)/base-files/
 
index b0726b7b7bb9112cc19c02c33b5deddf7c5b3725..3f121bd4d3671efe1ab13a8f6487bb06afbf11d9 100644 (file)
@@ -1,3 +1,12 @@
 config system
        option hostname OpenWrt
        option timezone UTC
+
+config rdate
+       list server ac-ntp0.net.cmu.edu
+       list server ptbtime1.ptb.de
+       list server ac-ntp1.net.cmu.edu
+       list server ntp.xs4all.nl
+       list server ptbtime2.ptb.de
+       list server cudns.cit.cornell.edu
+       list server ptbtime3.ptb.de
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 (file)
index 0000000..cf56c02
--- /dev/null
@@ -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