clean up ntpclient uci
authorJohn Crispin <john@openwrt.org>
Thu, 14 Aug 2008 12:03:55 +0000 (12:03 +0000)
committerJohn Crispin <john@openwrt.org>
Thu, 14 Aug 2008 12:03:55 +0000 (12:03 +0000)
- add global section for default values
- find working server from servers named in config
- patches bin to support daemon

SVN-Revision: 12305

net/ntpclient/Makefile
net/ntpclient/files/ntpclient.config
net/ntpclient/files/ntpclient.hotplug
net/ntpclient/files/ntpclient.init [deleted file]
net/ntpclient/patches/100-daemon.patch [new file with mode: 0644]

index ef7b761..657b9a0 100644 (file)
@@ -41,8 +41,6 @@ define Package/ntpclient/install
        $(INSTALL_DIR) $(1)/etc/config
        $(INSTALL_CONF) ./files/ntpclient.config $(1)/etc/config/ntpclient
        $(INSTALL_DIR) $(1)/usr/sbin
-       $(INSTALL_DIR) $(1)/etc/init.d
-       $(INSTALL_BIN) ./files/ntpclient.init $(1)/etc/init.d/ntpclient
        $(INSTALL_BIN) $(PKG_BUILD_DIR)/ntpclient $(1)/usr/sbin/
        $(INSTALL_BIN) $(PKG_BUILD_DIR)/adjtimex $(1)/usr/sbin/
        $(INSTALL_BIN) $(PKG_BUILD_DIR)/rate.awk $(1)/usr/sbin/
index 1695113..f7d24c3 100644 (file)
@@ -1,22 +1,23 @@
-config ntpclient
+config ntpserver
        option hostname '0.openwrt.pool.ntp.org'
        option port     '123'
-       option count    '0'
 
-config ntpclient
+config ntpserver
        option hostname '1.openwrt.pool.ntp.org'
        option port     '123'
-       option count    '0'
 
-config ntpclient
+config ntpserver
        option hostname '2.openwrt.pool.ntp.org'
        option port     '123'
-       option count    '0'
 
-config ntpclient
+config ntpserver
        option hostname '3.openwrt.pool.ntp.org'
        option port     '123'
-       option count    '0'
 
 config ntpdrift
        option freq     '0'
+
+config ntpclient
+       option interval 60
+       option iface    wan
+       #option count   10
index e1274c5..673afd3 100644 (file)
@@ -3,43 +3,69 @@
 
 . /etc/functions.sh
 
-DONE=0
-config_cb() {
-       local cfg="$CONFIG_SECTION"
-       local cfgtype
-       config_get cfgtype "$cfg" TYPE
-
-       # initial frequency offset, if configured
-       case "$cfgtype" in
-               ntpdrift)
-                       config_get freq         $cfg freq
-
-                       if [ ! "$freq" = "" ]; then
-                               adjtimex -f $freq >/dev/null
-                       fi
-               ;;
-       esac
-
-       case "$cfgtype" in
-               ntpclient)
-                       config_get hostname     $cfg hostname
-                       config_get port         $cfg port
-                       config_get count        $cfg count
-
-                       [ "$DONE" = "1" ] && exit 0
-                       ps | 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 IFACE
+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
+       config_get iface $1 iface
+       
+       [ -n "$iface" ] && IFACE=$iface
+       [ -n "$count" ] && COUNT=$count
+       [ -n "$interval" ] && INTERVAL=$interval
+}
+
+config_load ntpclient
+config_foreach load_settings ntpclient
+
+# check if we only listen to a single iface
+[ -n "$IFACE" -a "$IFACE" != "$INTERFACE" ] && exit 0 
+
+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
diff --git a/net/ntpclient/files/ntpclient.init b/net/ntpclient/files/ntpclient.init
deleted file mode 100644 (file)
index 7c975f6..0000000
+++ /dev/null
@@ -1,28 +0,0 @@
-#!/bin/sh /etc/rc.common
-# Copyright (C) 2008 OpenWrt.org
-
-START=80 
-
-config_cb() {
-       local cfg="$CONFIG_SECTION"
-       local cfgtype
-       config_get cfgtype "$cfg" TYPE
-
-       if [ "$cfgtype" = ntpclient ]
-       then
-               config_get hostname     $cfg hostname
-               config_get port         $cfg port
-               config_get count        $cfg count
-
-               ps | grep -q 'bin/[n]tpclient' || \
-                       /usr/sbin/ntpclient -s -c ${count:-1} -l -h $hostname -p ${port:-123} 2>&- >&- &
-       fi
-}
-
-start() {
-       config_load ntpclient&
-}
-
-stop() {
-       killall ntpclient 2>&- >&-
-}
diff --git a/net/ntpclient/patches/100-daemon.patch b/net/ntpclient/patches/100-daemon.patch
new file mode 100644 (file)
index 0000000..0ff8d5e
--- /dev/null
@@ -0,0 +1,24 @@
+Index: ntpclient-2007/ntpclient.c
+===================================================================
+--- ntpclient-2007.orig/ntpclient.c    2008-08-12 03:11:34.000000000 +0200
++++ ntpclient-2007/ntpclient.c 2008-08-12 03:11:36.000000000 +0200
+@@ -614,7 +614,7 @@
+       ntpc.cross_check=1;
+       for (;;) {
+-              c = getopt( argc, argv, "c:" DEBUG_OPTION "f:g:h:i:lp:q:" REPLAY_OPTION "st");
++              c = getopt( argc, argv, "c:" DEBUG_OPTION "f:g:h:i:lp:q:" REPLAY_OPTION "stD");
+               if (c == EOF) break;
+               switch (c) {
+                       case 'c':
+@@ -663,6 +663,10 @@
+                               (ntpc.cross_check)=0;
+                               break;
++                      case 'D':
++                              daemon();
++                              break;
++
+                       default:
+                               usage(argv[0]);
+                               exit(1);