]> git.pld-linux.org Git - packages/chrony.git/blame - chronyd.init
- up to 3.0
[packages/chrony.git] / chronyd.init
CommitLineData
8aaf2ebf
ER
1#!/bin/sh
2#
3# chronyd chronyd short service description
4#
5# chkconfig: 2345 58 74
6# description: Client/server for the Network Time Protocol, \
7# this program keeps your computer's clock accurate.
8#
8aaf2ebf
ER
9
10# Source function library
11. /etc/rc.d/init.d/functions
12
13# Get network config
14. /etc/sysconfig/network
15
90646ccb
ER
16# default user if not overriden by config
17NTPD_USER="ntp"
18
8aaf2ebf
ER
19# Get service config - may override defaults
20[ -f /etc/sysconfig/chronyd ] && . /etc/sysconfig/chronyd
21
22# Check that networking is up.
23if is_yes "${NETWORKING}"; then
24 if [ ! -f /var/lock/subsys/network -a "$1" != stop -a "$1" != status ]; then
25 msg_network_down "Chronyd NTPD"
26 exit 1
27 fi
28else
29 exit 0
30fi
31
32config=/etc/ntp/chrony.conf
33keyfile=/etc/ntp/keys
34chronyc=/usr/bin/chronyc
35
36get_key() {
833f1004 37 awk '/^[ \t]*'$1'[ \t]*/ { print $2; exit }' < $keyfile
8aaf2ebf
ER
38}
39
40get_commandkeyid() {
833f1004 41 awk '/^[ \t]*commandkey[ \t]*/ { keyid=$2 } END { print keyid }' < $config
8aaf2ebf
ER
42}
43
44chrony_command() {
45 commandkeyid=$(get_commandkeyid)
46 [ -z "$commandkeyid" ] && return 1
47 commandkey=$(get_key $commandkeyid)
48 [ -z "$commandkey" ] && return 2
49
50 ! (
51 $chronyc <<EOF &
52password $commandkey
53$1
54EOF
55 chronycpid=$!
56
30e9ceb6 57 # chronyc will hang if the daemon doesn't respond, kill it after 3 s
dc8feb35 58 #'
95855725 59 (sleep 3; kill $chronycpid) < /dev/null > /dev/null 2>&1 &
8aaf2ebf
ER
60 killerpid=$!
61
dc8feb35
ER
62 wait $chronycpid >/dev/null 2>&1
63 kill $killerpid >/dev/null 2>&1 || echo "chronyd not responding"
8aaf2ebf
ER
64 ) | grep -v '200 OK'
65}
66
67generate_commandkey() {
68 commandkeyid=$(get_commandkeyid)
69 [ -z "$commandkeyid" ] && return 1
70 commandkey=$(get_key $commandkeyid)
71 [ -z "$commandkey" ] || return 0
72
f0cc05dc 73 show "Generating Chrony command key"; busy
8aaf2ebf 74 commandkey=$(tr -c -d '[\041-\176]' < /dev/urandom | head -c 8)
f0cc05dc 75 [ -n "$commandkey" ] && echo "$commandkeyid $commandkey" >> $keyfile && ok || fail
8aaf2ebf
ER
76}
77
78start() {
79 # Check if the service is already running?
80 if [ -f /var/lock/subsys/chronyd ]; then
81 msg_already_running "Chronyd NTPD"
82 return
83 fi
84
85 generate_commandkey
86
87 msg_starting "Chronyd NTPD"
90646ccb 88 daemon /usr/sbin/chronyd -u $NTPD_USER $OPTIONS
8aaf2ebf
ER
89 RETVAL=$?
90 [ $RETVAL -eq 0 ] && touch /var/lock/subsys/chronyd
91}
92
93stop() {
94 if [ ! -f /var/lock/subsys/chronyd ]; then
95 msg_not_running "Chronyd NTPD"
96 return
97 fi
98
99 # Stop daemons.
100 msg_stopping "Chronyd NTPD"
101 killproc chronyd
102 rm -f /var/lock/subsys/chronyd
103}
104
105condrestart() {
106 if [ ! -f /var/lock/subsys/chronyd ]; then
107 msg_not_running "Chronyd NTPD"
108 RETVAL=$1
109 return
110 fi
111
112 stop
113 start
114}
115
116RETVAL=0
117# See how we were called.
118case "$1" in
119 start)
120 start
121 ;;
122 stop)
123 stop
124 ;;
125 restart)
126 stop
127 start
128 ;;
129 try-restart)
130 condrestart 0
131 ;;
132 force-reload)
133 condrestart 7
134 ;;
30e9ceb6 135 cyclelogs|flush-logs)
40804fc2
ER
136 status chronyd >/dev/null 2>&1 || exit 7
137 chrony_command cyclelogs
138 ;;
30e9ceb6 139 online|offline)
8aaf2ebf
ER
140 status chronyd >/dev/null 2>&1 || exit 7
141 chrony_command $1
142 ;;
143 command)
144 status chronyd >/dev/null 2>&1 || exit 7
145 chrony_command "$2"
146 ;;
147 status)
148 status chronyd
149 RETVAL=$?
1c33df65
ER
150 if [ $RETVAL = 0 ]; then
151 chrony_command sources
152 fi
8aaf2ebf
ER
153 ;;
154 *)
833f1004 155 msg_usage "$0 {start|stop|restart|try-restart|force-reload|online|offline|cyclelogs|command|status}"
8aaf2ebf
ER
156 exit 3
157esac
158
159exit $RETVAL
This page took 0.050409 seconds and 4 git commands to generate.