-
Notifications
You must be signed in to change notification settings - Fork 48
/
S50date
executable file
·184 lines (149 loc) · 4.37 KB
/
S50date
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#!/bin/bash
PROG_DATE="/bin/date"
PROG_NTPD="/usr/sbin/chronyd"
PROG_SNTP="/usr/bin/sntp"
LOG_SNTP="/var/log/sntp.log"
SYS_CONF="/etc/date.conf"
BOOT_CONF="/boot/date.conf"
CONF="/data/etc/date.conf"
SYS_NTP_CONF="/etc/ntp.conf"
BOOT_NTP_CONF="/boot/ntp.conf"
NTP_CONF="/data/etc/ntp.conf"
SAVED_DATE_FILE="/var/lib/saved_date"
test -n "${OS_VERSION}" || source /etc/init.d/base
prepare_conf ${CONF} ${SYS_CONF} ${BOOT_CONF}
prepare_conf ${NTP_CONF} ${SYS_NTP_CONF} ${BOOT_NTP_CONF}
test -s ${CONF} || exit 0
test "${OS_NETWORKLESS}" == "true" && exit 0
DATE_TIMEOUT=10
DATE_METHOD=sntp
DATE_HOST="google.com"
DATE_INTERVAL="900"
DATE_NTP_SERVER=""
source ${CONF}
set_current_date_http() {
curl_args="-v -s -m ${DATE_TIMEOUT} -H \"Cache-Control: no-cache\" -X GET"
url="http://${DATE_HOST}?_=${RANDOM}"
date_str=$(curl ${curl_args} ${url} 2>&1 | grep Date | sed -e 's/< Date: //')
if [[ -n "${date_str}" ]]; then
${PROG_DATE} -u -D "%a, %d %b %Y %H:%M:%S" -s "${date_str}" > /dev/null
logger -t date "current system date/time set to $(date) via HTTP"
return 0
else
logger -t date "failed to set current system date/time via HTTP"
return 1
fi
}
set_current_date_ntp() {
if [[ -n "${DATE_NTP_SERVER}" ]]; then
sed -i "s/pool .*/pool ${DATE_NTP_SERVER} iburst/" ${NTP_CONF}
fi
${PROG_NTPD} -f ${NTP_CONF} -q -t ${DATE_TIMEOUT} &>/dev/null
if [[ $? == 0 ]]; then
logger -t date "current system date/time set to $(date) via NTP"
return 0
else
logger -t date "failed to set current system date/time via NTP"
return 1
fi
}
set_current_date_sntp() {
sntp_args="-t ${DATE_TIMEOUT} -K /dev/null -Ss"
server=$(cat ${NTP_CONF} | grep pool | head -n 1 | cut -d ' ' -f 2)
# Retry command 3 times
truncate -s0 ${LOG_SNTP}
ok=false
for (( i = 0; i < 3; i++ )); do
${PROG_SNTP} ${sntp_args} ${server} &>>${LOG_SNTP} && { ok=true; break; }
sleep 1
done
if [[ ${ok} == true ]]; then
logger -t date "current system date/time set to $(date) via SNTP"
return 0
else
logger -t date "failed to set current system date/time via SNTP"
return 1
fi
}
start_http() {
msg_begin "Setting current date using HTTP"
if set_current_date_http; then
sleep_interval=${DATE_INTERVAL}
msg_done "$(${PROG_DATE})"
else
sleep_interval=${DATE_TIMEOUT}
msg_fail
fi
msg_begin "Starting http date updater"
while true; do
sleep ${sleep_interval}
if set_current_date_http; then
sleep_interval=${DATE_INTERVAL}
else
sleep_interval=${DATE_TIMEOUT}
fi
done &
msg_done
}
start_ntp() {
if [[ -n "${DATE_NTP_SERVER}" ]]; then
sed -i "s/pool .*/pool ${DATE_NTP_SERVER} iburst/" ${NTP_CONF}
fi
if [[ "${DATE_METHOD}" == "sntp" ]]; then
msg_begin "Setting current date using SNTP"
set_current_date_sntp
test $? == 0 && msg_done "$(${PROG_DATE})" || msg_fail
else # assuming ntp
msg_begin "Setting current date using NTP"
set_current_date_ntp
test $? == 0 && msg_done "$(${PROG_DATE})" || msg_fail
fi
msg_begin "Starting ntpd"
${PROG_NTPD} -f ${NTP_CONF}
test $? == 0 && msg_done || msg_fail
}
stop_http() {
msg_begin "Stopping date updater"
ps | grep S50date | grep -v $$ | grep -v grep | tr -s ' ' | sed -e 's/^\s//' | cut -d ' ' -f 1 | xargs -r kill
test $? == 0 && msg_done || msg_fail
}
stop_ntp() {
msg_begin "Stopping ntpd"
killall -q $(basename ${PROG_NTPD})
test $? == 0 && msg_done || msg_fail
}
start() {
# Load saved date first
test -s ${SAVED_DATE_FILE} && date -s $(cat ${SAVED_DATE_FILE}) -D%s >/dev/null
if [[ "${DATE_METHOD}" == "http" ]]; then
start_http
else # ntp or sntp
start_ntp
fi
echo "system date is $(${PROG_DATE} '+%Y-%m-%d %H:%M:%S')" > /dev/kmsg
}
stop() {
if [[ "${DATE_METHOD}" == "http" ]]; then
stop_http
else # ntp or sntp
stop_ntp
fi
# Save current date to disk for next time
date +%s > ${SAVED_DATE_FILE}
}
case "$1" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
*)
echo "Usage: $0 {start|stop|restart}"
exit 1
esac
exit $?