2010-01-14 02:37:34 +05:30
|
|
|
#!/bin/bash
|
|
|
|
# (using bashism (arrays) in dhcp config)
|
|
|
|
|
|
|
|
#exec >/dev/null
|
|
|
|
exec 2>&1
|
|
|
|
exec </dev/null
|
|
|
|
|
|
|
|
user=root
|
2018-07-03 20:32:50 +05:30
|
|
|
start_delay=15
|
|
|
|
net_down_delay=5
|
2010-01-14 02:37:34 +05:30
|
|
|
pool="us.pool.ntp.org" # replace "us" with your country code
|
|
|
|
|
|
|
|
service="${PWD##*/}"
|
|
|
|
rundir="/var/run/service/$service"
|
|
|
|
default_p_opt="-p 0.$pool -p 1.$pool -p 2.$pool -p 3.$pool"
|
|
|
|
|
2018-07-03 20:32:50 +05:30
|
|
|
echo "* Checking network"
|
|
|
|
test -f /var/run/service/fw/up || exec sleep $net_down_delay
|
|
|
|
|
|
|
|
# With multiple interfaces (e.g. wired+wireless) going up,
|
|
|
|
# networking scripts may restart ntpd service several times
|
|
|
|
# in quick succession. Do not be too eager to start sending
|
|
|
|
# NTP requests:
|
|
|
|
sleep $start_delay
|
2010-01-14 02:37:34 +05:30
|
|
|
|
|
|
|
# Make sure rundir/ exists
|
|
|
|
mkdir -p "$rundir" 2>/dev/null
|
2015-10-24 18:25:33 +05:30
|
|
|
chown -R "$user": "$rundir"
|
2010-01-14 02:37:34 +05:30
|
|
|
chmod -R a=rX "$rundir"
|
|
|
|
rm -rf rundir 2>/dev/null
|
|
|
|
ln -s "$rundir" rundir
|
|
|
|
|
|
|
|
# Grab config from dhcp
|
|
|
|
cfg=-1
|
|
|
|
for f in rundir/*.ntpconf; do
|
2018-07-03 20:32:50 +05:30
|
|
|
test -f "$f" || continue
|
|
|
|
. "$f"
|
2010-01-14 02:37:34 +05:30
|
|
|
done
|
|
|
|
|
|
|
|
# Select peers
|
|
|
|
p_opt=""
|
|
|
|
cfg=0
|
|
|
|
while test x"${ntpip[$cfg]}" != x""; do
|
2018-07-03 20:32:50 +05:30
|
|
|
p_opt="$p_opt -p ${ntpip[$cfg]}"
|
|
|
|
let cfg=cfg+1
|
2010-01-14 02:37:34 +05:30
|
|
|
done
|
|
|
|
test x"$p_opt" == x"" && p_opt="$default_p_opt"
|
|
|
|
|
|
|
|
if test x"$p_opt" == x""; then
|
|
|
|
echo "* No NTP peers configured, stopping"
|
2018-07-03 20:32:50 +05:30
|
|
|
svc -o .
|
2010-01-14 02:37:34 +05:30
|
|
|
exec sleep 1
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
|
|
|
# Let others know that we are up
|
|
|
|
date '+%Y-%m-%d %H:%M:%S %Z' >rundir/up
|
|
|
|
|
|
|
|
# Go go go
|
|
|
|
echo "* Starting ntpd[$$]"
|
|
|
|
exec \
|
|
|
|
env - PATH="$PATH" \
|
|
|
|
softlimit \
|
|
|
|
setuidgid "$user" \
|
|
|
|
ntpd -ddnNl -S ./ntp.script $p_opt
|