Add chrony from (#1388)
[openwrt/svn-archive/archive.git] / net / chrony / files / ntpd.init
1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2006 OpenWrt.org
3
4 START=60
5
6 start() {
7 [ ! -f /var/run/chronyd.pid ] && (
8 /usr/sbin/chronyd -f /etc/chrony/chrony.conf
9
10 local NTP_SERVERS
11 local NTP_SERVER
12 local CLIENT_NETWORKS
13 local NETNAME
14 config_load ntpd
15 config_get NTP_SERVERS cfg1 TimeServers
16 config_get CLIENT_NETWORKS cfg1 ClientAccessFrom
17 CHRONY_KEY=$(awk '/^1 / { print $2; }' /etc/chrony/chrony.keys)
18 include /lib/network
19 scan_interfaces
20
21 # Define servers and do a rapid series of time measurements
22 # to get up to date
23 (
24 echo password $CHRONY_KEY
25 for NTP_SERVER in $NTP_SERVERS; do
26 echo add server $NTP_SERVER
27 done
28 echo burst 5/5
29 ) | chronyc > /dev/null 2>&1
30
31 # ... wait until a reference ID is found ...
32 while chronyc tracking | grep 'Reference ID.*0.0.0.0' >/dev/null ; do sleep 1; done
33
34
35 # ... and finally, adjust the time in a single step
36 # and allow clients to access the server
37 (
38 echo password $CHRONY_KEY
39 echo makestep
40 for NETNAME in $CLIENT_NETWORKS; do
41 local INTERFACE
42 local IP
43 local NETMASK
44 local BROADCAST
45 local NETWORK
46 local PREFIX
47
48 config_get INTERFACE "$NETNAME" ifname
49 if [ -n "$INTERFACE" ] ; then
50 eval $(ifconfig $INTERFACE | grep inet | sed -e 's/.*addr:/IP=/' -e 's/ .*Mask:/; NETMASK=/')
51 eval $(ipcalc $IP $NETMASK)
52 echo allow $NETWORK/$PREFIX
53 fi
54 done
55 ) | chronyc > /dev/null 2>&1
56
57 )
58 }
59
60 stop() {
61 [ -r /var/run/chronyd.pid ] && PID=$(cat /var/run/chronyd.pid)
62 [ -n "$PID" ] && kill $PID
63 }
64
65 restart() {
66 stop
67 while [ -r /var/run/chronyd.pid ] ; do sleep 1; done
68 start
69 }