branch Attitude Adjustment packages
[12.09/packages.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 $CONFIG_SECTION TimeServers
16                 config_get CLIENT_NETWORKS $CONFIG_SECTION ClientAccessFrom
17                 CHRONY_KEY=$(awk '/^1 / { print $2; }' /etc/chrony/chrony.keys)
18
19                 . /lib/functions/network.sh
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                 # ... wait until a reference ID is found ...
31                 while chronyc tracking | grep 'Reference ID.*0.0.0.0' >/dev/null ; do sleep 1; done
32                 # ... and finally, adjust the time in a single step
33                 # and allow clients to access the server
34                 (
35                         echo password $CHRONY_KEY
36                         echo makestep
37                         for NETNAME in $CLIENT_NETWORKS; do
38                                 local subnet
39                                 if network_get_subnet subnet "$NETNAME"; then
40                                         echo allow $subnet
41                                 fi
42                         done
43                 ) | chronyc > /dev/null 2>&1
44         )
45 }
46
47 stop() {
48         [ -r /var/run/chronyd.pid ] && PID=$(cat /var/run/chronyd.pid)
49         [ -n "$PID" ] && kill $PID
50 }
51
52 restart() {
53         stop
54         while [ -r /var/run/chronyd.pid ] ; do sleep 1; done
55         start
56 }