summaryrefslogtreecommitdiff
path: root/recipes-support/ntp/files/ntpd-init.patch
blob: e3107e1e96a5851c2affc5a42911622e192436d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
diff -Naru orig/ntpd new/ntpd
--- orig/ntpd	2019-03-18 10:49:41.255374972 -0500
+++ new/ntpd	2019-03-18 11:36:56.635291031 -0500
@@ -1,4 +1,4 @@
-#! /bin/sh
+#! /bin/bash
 
 ### BEGIN INIT INFO
 # Provides:        ntp
@@ -9,7 +9,9 @@
 # Short-Description: Start NTP daemon
 ### END INIT INFO
 
+. /etc/default/ntpd
 PATH=/sbin:/bin:/usr/bin:/usr/sbin
+GNSSRST=/sys/devices/platform/mts-io/gnss-reset
 
 DAEMON=/usr/sbin/ntpd
 PIDFILE=/var/run/ntpd.pid
@@ -34,13 +36,40 @@
 	}
 }
 startdaemon(){
-	# The -g option allows ntpd to step the time to correct it just
-	# once.  The daemon will exit if the clock drifts too much after
-	# this.  If ntpd seems to disappear after a while assume TICKADJ
-	# above is set to a totally incorrect value.
-	echo -n "Starting ntpd: "
-	start-stop-daemon --start --quiet --oknodo --pidfile $PIDFILE --startas $DAEMON -- -u ntp:ntp -p $PIDFILE "$@"
-	echo "done"
+	/usr/sbin/start-stop-daemon -N -20 --start -x /usr/sbin/ntpd --test -- -u ntp:ntp -p /var/run/ntp.pid "$@" \
+	  || return 1
+	if [[ -x /usr/sbin/gpsd_settime ]] && ((GPSD_REQUIRED == 1)) && (( $(/usr/sbin/mts-io-sysfs show capability/gps) == 1 )) && [[ -f "${GNSSRST}" ]]; then
+		. /etc/default/gpsd
+                [[ ${VERBOSE} != no ]] && logger -t 'etc_ntpd' -p daemon.info 'Have a GPS and it is required'
+		if ((SET_SYSTEM_CLOCK == 1)) && /usr/sbin/gpsd_settime ; then
+                   [[ ${VERBOSE} != no ]] && logger -t 'etc_ntpd' -p daemon.info 'Need to set the time next with ntpd'
+		   # We just set the system time by the GPS.  Should be within 2 seconds.
+		   # Now that we are close to the correct system time, we
+		   # use the ntpd one shot option to get to less than
+		   # 250mS error so that we don't waste time adjusting the clock.
+		   /usr/sbin/ntpd -gq
+	           echo -n "Starting ntpd: "
+		   start-stop-daemon -N -20 --start --quiet --oknodo --pidfile $PIDFILE --startas $DAEMON -- -u ntp:ntp -p $PIDFILE "$@"
+	           echo "done"
+		else
+                  [[ ${VERBOSE} != no ]] && logger -t 'etc_ntpd' -p daemon.info 'Clock not set, try later'
+		  # Need a GPS fix before startint ntp.  Try again later.
+		  /usr/sbin/start-stop-daemon -b -n z1e9d3qb -N -20 --start -x /bin/bash -- -c "sleep $GPSD_WAIT_TIME;/etc/init.d/ntpd start"
+		fi
+	else
+	  # The -g option allows ntpd to step the time to correct it just
+	  # once.  The daemon will exit if the clock drifts too much after
+	  # this.  If ntpd seems to disappear after a while assume TICKADJ
+	  # above is set to a totally incorrect value.
+	  if ((SET_SYSTEM_CLOCK == 1)) ; then
+	    # Sets the clock and exits.
+	    ntpd -gq
+	    shift
+	  fi
+	  echo -n "Starting ntpd: "
+	  start-stop-daemon -N -20 --start --quiet --oknodo --pidfile $PIDFILE --startas $DAEMON -- -u ntp:ntp -p $PIDFILE "$@"
+	  echo "done"
+	fi
 }
 stopdaemon(){
 	echo -n "Stopping ntpd: "
@@ -50,8 +79,22 @@
 
 case "$1" in
   start)
-  	settick
-	startdaemon -g
+	if ! [[ $ENABLED =~ ^[yY][eE][sS]$ ]] ; then
+		exit 0
+	fi
+	if [[ -n "$CONFIGFILE" ]] ; then
+		CONFIGOPT="-c ${CONFIGFILE}"
+		STATSDIR=$(grep "^statsdir" ${CONFIGFILE} | sed -r 's/[^[:space:]]+[[:space:]]//')
+		if ((${#STATSDIR} > 0)) && ! [[ -d ${STATSDIR} ]] ; then
+			echo Make ${STATSDIR}	
+			mkdir -m 0755 -p ${STATSDIR}
+		fi
+		if [[ -d ${STATSDIR} ]] ; then
+			chown ntp:ntp ${STATSDIR} >/dev/null 2>&1
+		fi
+	fi
+	settick
+	startdaemon -g $CONFIGOPT
 	;;
   stop)
   	stopdaemon