Initial import from FreeBSD RELENG_4:
[dragonfly.git] / contrib / ntp / scripts / support / etc / rc
1 #!/bin/sh
2 # NTP time synchronisation
3 #
4 # /src/NTP/REPOSITORY/v3/supportscripts/etc/rc,v 1.11 1993/07/09 13:17:00 kardel Exp
5 #
6 # rc,v
7 # Revision 1.11  1993/07/09  13:17:00  kardel
8 # local NTPROOT
9 #
10 # Revision 1.10  1993/07/09  11:37:29  kardel
11 # Initial restructured version + GPS support
12 #
13 # Revision 1.9  1993/06/23  14:10:36  kardel
14 # June 21st reconcilation
15 #
16 # Revision 1.7  1993/06/02  12:04:43  kardel
17 # May 28th reconcilation & clenaup
18 #
19 #
20 # non reference clock hosts will try to do an ntpdate on NTPSERVERS
21 #
22 NTPSERVERS="ntps1-0 ntps1-1 ntps2-0 ntps2-1"
23 NTPROOT=/local/NTP
24
25 #
26 # get the initial setup
27 #
28 if [ ! -r $NTPROOT/etc/setup ]; then
29         echo "ERROR: $NTPROOT/etc/setup missing - incorrect installation."
30         exit 1
31 else
32         . $NTPROOT/etc/setup
33 fi
34
35 umask 022                           # SITE SPECIFIC: local policy - watch out for NFS and "root" rights
36
37 msg=""
38 #
39 # default configuration files are named $NTPROOT/conf/<ARCH>.<KARCH>
40 #
41 CF=$NTPROOT/conf/$ARCH.$KARCH     # default configuration file
42 #
43 # Host specific config file (reference clocks) have the hostname tagged on
44 #
45 CFH="$CF"."$HOSTNAME"             # specific configuration file
46 #
47 # where to find the tickadj command
48 #
49 KFIX=$NTPROOT/bin/tickadj         # kernel variable fix
50 #
51 # where to find special tickadj parameters
52 #
53 TC=$NTPROOT/conf/tickconf         # special tickadj parameters
54 #
55 # where to find the keys file (if not found $KEY.dumb will be used)
56 #
57 KEY=$NTPROOT/conf/ntp.keys        # private key file
58 #
59 # the daemon
60 #
61 XD=$NTPROOT/bin/xntpd             # NTP daemon
62 #
63 # HP adjtimed
64 #
65 ADJTIMED=$NTPROOT/bin/adjtimed    # HP special (adjtime() emulation)
66 #
67 # ntpdate command
68 #
69 NTPDATE=$NTPROOT/bin/ntpdate
70
71 #
72 # secondary timed support
73 # The word "TIMED" must be in the config file for timed to start
74 # Note that this times is a special version which does not ever set or
75 # adjust the time. Ask time@informatik.uni-erlangen.de for patches
76 #
77 TIMED=$NTPROOT/bin/timed          # timed (Berkeley) secondary time service
78                                   # here used in a *HARMLESS* version
79                                   # to provide time to "inferior" systems
80 #
81 # ISREFHOST is a command that returns exit status 0 for a reference host
82 # Site specific: sample for dcf77 is given
83 #
84 ISREFHOST="[ -f $NTPROOT/.karch.$KARCH/sys/OBJ/parsestreams.o -a -f /dev/refclock-0 ]"
85 #
86 # SETUP_REFCLOCK
87 #
88 # what to do in order to set up a local reference clock
89 # usually this will load a STREAMS module or initialize other things
90 # needed
91 #
92 SETUP_REFCLOCK() {
93   if modstat | grep -s 'PARSE'; then
94           ECHO "loadable PARSER STREAMS module already loaded."
95   else
96           ECHO "attempting to load PARSER STREAMS module..."
97           MDLFILE="/tmp/mdl.$$"
98           if modload $NTPROOT/.karch.$KARCH/sys/OBJ/parsestreams.o -o $MDLFILE 2>&1; then
99             modstat
100           else
101             echo WARNING: load FAILED
102           fi | LOG
103           rm -f $MDLFILE
104           unset MDLFILE
105   fi
106 }
107
108 kargs() {
109   MATCH=NO
110   HOSTID="`(hostid) 2>/dev/null || echo 000000`"
111   if [ -r "$TC" ]; then
112     exec 0< "$TC"
113     while [ "$MATCH" != "YES" ] && read HOST ID PARAM; do
114       if [ "$HOST" = "DEFAULT" ]; then
115         DEFAULT="$ID $PARAM"
116       else
117         if [ "$ID" = "$HOSTID" -o "$HOST" = "$HOSTNAME" ]; then
118           echo "$PARAM"
119           MATCH=YES
120         fi
121       fi
122     done
123     if [ "$MATCH" != "YES" ]; then
124       if [ -z "$DEFAULT" ]; then
125         echo "-A -p -s -q";
126       else
127         echo "$DEFAULT";
128       fi
129     fi
130   else 
131     echo "-A -p -s -q";
132   fi
133 }
134
135 if [ -x $XD ]; then
136   if [ -x "$ADJTIMED" ]; then
137      $ADJTIMED && ECHO "adjusttimesupport: adjtimed."
138   fi
139   #
140   # WARNING: check ps command first, or you might kill things you don't want to
141   #
142   PID="`(ps -efa 2>/dev/null || ps auxww 2>/dev/null || echo "") | grep xntp | grep -v grep | awk '{ print $2 }'`"
143
144   if [ ! -z "$PID" ]; then
145     ECHO "killing old NTP daemon (PID=$PID)"
146     #
147     # enable this after checking for correctness
148     # kill $PID
149     ECHO "should do a kill $PID, if this is the right PID - check rc script"
150   fi
151   #
152   # try an ntpdate when timeservers are configured
153   #
154   if [ ! -z "$NTPSERVERS" -a -x $NTPDATE ]; then
155         ECHO "NTP initial time setting"
156         $NTPDATE -v $NTPSERVERS | LOG
157   fi
158   #
159   # look for reference clock equipment
160   #
161   if $ISREFHOST; then
162         ECHO "REFERENCE CLOCK SUPPORT (initializing...)"
163         SETUP_REFCLOCK
164   fi
165
166   if [ -r "$CFH" ]; then
167     CF="$CFH"
168   else
169     if [ ! -r "$KEY" ]; then
170       KEY="$KEY.dumb"
171     fi
172   fi
173
174   ECHO "NTP configuration file: $CF"
175   ECHO -n "time daemon startup:"
176
177   if [ -r "$CF" ]; then
178     if [ -x "$KFIX" ]; then
179       KARGS="`kargs`"
180       if [ ! -z "$KARGS" ]; then
181         $KFIX $KARGS && ECHO -n "tickadj $KARGS"
182       fi
183     fi
184     $XD -c "$CF" -k "$KEY" && ECHO -n ' xntpd'
185     if [ -x "$TIMED" ] && grep -s TIMED "$CF"; then
186       $TIMED -M -N && ECHO -n ' timed'
187     fi
188   else
189     msg="configuration file ($CF) not present."
190   fi
191 else
192   msg="daemon binary ($XD) not present."
193 fi
194 ECHO "."
195
196 if [ "$msg" ]; then
197   NLECHO "WARNING: NO NTP time sychronisation: $msg"
198 fi