summaryrefslogtreecommitdiffstats
path: root/projectroot/etc/init.d/chrony
blob: e7e937ae3c01f6bc5a99cfe55fa13c8118de1042 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#!/bin/sh
#
# This is a chrony init.d script which ist called by init(1)
# with [start|stop] as argument. This version does not
# need an additional start-stop daemon.
#
# Last change:  Bjørn Bürger <b.buerger@pengutronix.de>
# Date:		Tue Mar  6 16:00:30 UTC 2007

PATH=/sbin:/bin:/usr/bin:/usr/sbin
BINARY="/usr/sbin/chronyd"
CONFIG="/etc/chrony/chrony.conf"
PREFIX="chrony: "
# This system doesn´t have full rtc ioctl support for
# chrony statistic functions
RTC_IOCTL="incomplete"

# some chronyc commands need prior autentication: extract keys from config
KEY=$(awk '$1 ~ /^commandkey$/ { print $2; exit}' /etc/chrony/chrony.conf)
PASSWORD=`awk '$1 ~ /^'$KEY'$/ {print $2; exit}' /etc/chrony/chrony.keys`

# convenience functions
message(){
	echo "${PREFIX}$*" >&2
}

message_n(){
	echo -n "${PREFIX}$*" >&2
}

bailout(){
	echo "${PREFIX}ERROR   --- $*" >&2
	exit 1
}

usage(){
	echo "Usage: $0 {start|stop|restart|force-reload|online|offline|set-rtc|status|statistics}"
}

killproc() {
	killall $1
}

# main functions
start_proc() {
	message_n "Reading system time from RealTimeClock ..."
	/sbin/hwclock --hctosys || message_n " ### FAILED ### "
	message "DONE"
	message_n "Starting NTP server: chronyd ..."
	[ -e "$CONFIG" ] || bailout "Configfile $CONFIG not found, PANIC!"
	$BINARY -f $CONFIG
	message "DONE"
}

stop_proc() {
	message_n "Stopping NTP server: chronyd ..."
	killproc chronyd
	message "DONE"
	message_n "Writing system time to RealTimeClock ..."
	/sbin/hwclock --systohc || message_n " ### FAILED ### "
	message "DONE"
}

set_online(){
        message_n "Setting NTP server ONLINE ... "
	/bin/pidof chronyd > /dev/null || bailout " chronyd is not running "
	/usr/bin/chronyc <<-EOF
	password $PASSWORD
	online
	burst 5/10
	quit
	EOF
	message "DONE"
	exit 0
}

set_offline(){
        message_n "Setting NTP server OFFLINE ... "
	/bin/pidof chronyd > /dev/null || bailout " chronyd is not running "
	/usr/bin/chronyc <<-EOF
	password $PASSWORD
	offline
	EOF
	message "DONE"
	exit 0
}

set_rtc(){
	/bin/pidof chronyd > /dev/null || bailout " chronyd is not running "
	if [ "$RTC_IOCTL" == "incomplete" ] ; then 
	# We are running on a system with limited rtc support,
	# so we cannot let the ntp client do the job. 
	stop_proc
	sleep 1
	start_proc
	exit 0
	else
	# This requires enhanced rtc support
        message_n "Setting NTP time to RTC ... "
	cat <<-EOF | /usr/bin/chronyc
	password $PASSWORD
	trimrtc
	writertc
	dump
	EOF
	message "DONE"
	exit 0
	fi
}

status(){
	/bin/pidof chronyd > /dev/null || bailout " chronyd is not running "
	cat <<-EOF | /usr/bin/chronyc
	password $PASSWORD
	tracking
	sources
	EOF
}

statistics(){
	/bin/pidof chronyd > /dev/null || bailout " chronyd is not running "
	cat <<-EOF | /usr/bin/chronyc
	password $PASSWORD
	sourcestats
	EOF
}

case "$1" in
        start)
        	start_proc
		;;
	stop)
		stop_proc
		;;
        restart|force-reload)
                message "Restarting NTP server: chronyd ... "
                stop_proc
		sleep 1
                start_proc
                ;;
        reload)
                message "Reload is not supported"
		exit 1
		;;
	online)
		set_online
		;;
	offline)
		set_offline
		;;
	set-rtc)
		set_rtc
		;;
	status)
		status
		;;
	statistics)
		statistics
		;;
        *)
                usage
		exit 1
                ;;
esac

exit 0