]> git.pld-linux.org Git - packages/chrony.git/blob - chronyd.init
- rel 6
[packages/chrony.git] / chronyd.init
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 #
9 # $Id$
10
11 # Source function library
12 . /etc/rc.d/init.d/functions
13
14 # Get network config
15 . /etc/sysconfig/network
16
17 # default user if not overriden by config
18 NTPD_USER="ntp"
19
20 # Get service config - may override defaults
21 [ -f /etc/sysconfig/chronyd ] && . /etc/sysconfig/chronyd
22
23 # Check that networking is up.
24 if is_yes "${NETWORKING}"; then
25         if [ ! -f /var/lock/subsys/network -a "$1" != stop -a "$1" != status ]; then
26                 msg_network_down "Chronyd NTPD"
27                 exit 1
28         fi
29 else
30         exit 0
31 fi
32
33 config=/etc/ntp/chrony.conf
34 keyfile=/etc/ntp/keys
35 chronyc=/usr/bin/chronyc
36
37 get_key() {
38     awk '/^[ \t]*'$1'[ \t]*/ { print $2; exit }' < $keyfile
39 }
40
41 get_commandkeyid() {
42     awk '/^[ \t]*commandkey[ \t]*/ { keyid=$2 } END { print keyid }' < $config
43 }
44
45 chrony_command() {
46     commandkeyid=$(get_commandkeyid)
47     [ -z "$commandkeyid" ] && return 1
48     commandkey=$(get_key $commandkeyid)
49     [ -z "$commandkey" ] && return 2
50
51     ! (
52         $chronyc <<EOF &
53 password $commandkey
54 $1
55 EOF
56         chronycpid=$!
57
58         # chronyc will hang if the daemon doesn't respond, kill it after 3 s
59         (sleep 3; kill $chronycpid) < /dev/null > /dev/null 2>&1 &
60         killerpid=$!
61
62         wait $chronycpid &> /dev/null
63         kill $killerpid &> /dev/null || echo "chronyd not responding"
64     ) | grep -v '200 OK'
65 }
66
67 generate_commandkey() {
68     commandkeyid=$(get_commandkeyid)
69     [ -z "$commandkeyid" ] && return 1
70     commandkey=$(get_key $commandkeyid)
71     [ -z "$commandkey" ] || return 0
72
73         show "Generating Chrony command key"; busy
74     commandkey=$(tr -c -d '[\041-\176]' < /dev/urandom | head -c 8)
75     [ -n "$commandkey" ] && echo "$commandkeyid $commandkey" >> $keyfile && ok || fail
76 }
77
78 start() {
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"
88         daemon /usr/sbin/chronyd -u $NTPD_USER $OPTIONS
89         RETVAL=$?
90         [ $RETVAL -eq 0 ] && touch /var/lock/subsys/chronyd
91 }
92
93 stop() {
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
105 condrestart() {
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
116 RETVAL=0
117 # See how we were called.
118 case "$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         ;;
135   cyclelogs|flush-logs)
136         status chronyd >/dev/null 2>&1 || exit 7
137         chrony_command cyclelogs
138         ;;
139   online|offline)
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=$?
150         if [ $RETVAL = 0 ]; then
151                 chrony_command sources
152         fi
153         ;;
154   *)
155         msg_usage "$0 {start|stop|restart|try-restart|force-reload|online|offline|cyclelogs|command|status}"
156         exit 3
157 esac
158
159 exit $RETVAL
This page took 0.076208 seconds and 3 git commands to generate.