remove stupid feature from ntpclient hotplug script and remove whitespace errors
[openwrt/svn-archive/archive.git] / net / ntpclient / files / ntpclient.hotplug
index a3ce8b859ab2e36282ffa99247fe60455cab899e..87f0c0c0e39d197021d4f392d67b9e2028d77f0e 100644 (file)
@@ -3,32 +3,63 @@
 
 . /etc/functions.sh
 
-DONE=0
-config_cb() {
-       local cfg="$CONFIG_SECTION"
-       local cfgtype
-       config_get cfgtype "$cfg" TYPE
-
-       case "$cfgtype" in
-               ntpclient)
-                       config_get hostname     $cfg hostname
-                       config_get port         $cfg port
-                       config_get count        $cfg count                      
-
-                       [ "$DONE" = "1" ] && exit 0
-                       ps x | grep 'bin/[n]tpclient' >&- || {
-                               route -n 2>&- | grep '^0.0.0.0' >&- && {
-                                       /usr/sbin/ntpclient -c ${count:-1} -s -h $hostname -p ${port:-123} 2>&- >&- && DONE=1
-                               }
-                       }
-               ;;
-       esac
+unset SERVER
+unset PORT
+unset INTERVAL
+unset COUNT
+NTPC=`which ntpclient`
+
+check_server() {
+       local hostname
+       local port
+       [ -n "$SERVER" ] && return
+       config_get hostname $1 hostname
+       config_get port $1 port
+       [ -z "$hostname" ] && return
+       $NTPC -c 1 -p ${port:-123} -h $hostname > /dev/null && { SERVER=$hostname; PORT=${port:-123}; }
+}
+
+set_drift() {
+       config_get freq $1 freq
+       [ -n "$freq" ] && adjtimex -f $freq >/dev/null
+}
+
+start_ntpclient() {
+       config_foreach set_drift ntpdrift
+       config_foreach check_server ntpserver
+       [ -z "$SERVER" ] && exit 0
+       logger starting ntpclient
+       $NTPC ${COUNT:+-c $COUNT} ${INTERVAL:+-i $INTERVAL} -D -p $PORT -h $SERVER 2> /dev/null
+}
+
+stop_ntpclient() {
+       logger stopping ntpclient
+       killall ntpclient
+}
+
+load_settings() {
+       local interval
+       local count
+       local iface
+       
+       config_get interval $1 interval
+       config_get count $1 count
+       
+       [ -n "$count" ] && COUNT=$count
+       [ -n "$interval" ] && INTERVAL=$interval
 }
+
+config_load ntpclient
+config_foreach load_settings ntpclient
+
+DEF_ROUTE=`route -n | grep '^0.0.0.0'`
+NTP_RUNNING=`ps  | grep $NTPC | grep -v grep`
+
 case "${ACTION:-ifup}" in
        ifup)
-               config_load ntpclient&
+               [ -n "$DEF_ROUTE" -a -z "$NTP_RUNNING" ] && start_ntpclient 
        ;;
        ifdown)
-               route -n 2>&- | grep '^0.0.0.0' >&- || killall ntpclient 2>&- >&-
+               [ -n "$NTP_RUNNING" ] && stop_ntpclient 
        ;;
 esac