aboutsummaryrefslogtreecommitdiff
path: root/examples/var_service/ntpd/run
blob: 5c90aad412c016c8fed48b8458788e8640c93c8b (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
#!/bin/bash
# (using bashism (arrays) in dhcp config)

#exec >/dev/null
exec 2>&1
exec </dev/null

user=root
start_delay=15
net_down_delay=5
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"

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

# Make sure rundir/ exists
mkdir -p "$rundir" 2>/dev/null
chown -R "$user": "$rundir"
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
	test -f "$f" || continue
	. "$f"
done

# Select peers
p_opt=""
cfg=0
while test x"${ntpip[$cfg]}" != x""; do
	p_opt="$p_opt -p ${ntpip[$cfg]}"
	let cfg=cfg+1
done
test x"$p_opt" == x"" && p_opt="$default_p_opt"

if test x"$p_opt" == x""; then
	echo "* No NTP peers configured, stopping"
	svc -o .
	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