Start 1.33.0 development cycle
[oweals/busybox.git] / examples / var_service / ntpd / run
1 #!/bin/bash
2 # (using bashism (arrays) in dhcp config)
3
4 #exec >/dev/null
5 exec 2>&1
6 exec </dev/null
7
8 user=root
9 start_delay=15
10 net_down_delay=5
11 pool="us.pool.ntp.org" # replace "us" with your country code
12
13 service="${PWD##*/}"
14 rundir="/var/run/service/$service"
15 default_p_opt="-p 0.$pool -p 1.$pool -p 2.$pool -p 3.$pool"
16
17 echo "* Checking network"
18 test -f /var/run/service/fw/up || exec sleep $net_down_delay
19
20 # With multiple interfaces (e.g. wired+wireless) going up,
21 # networking scripts may restart ntpd service several times
22 # in quick succession. Do not be too eager to start sending
23 # NTP requests:
24 sleep $start_delay
25
26 # Make sure rundir/ exists
27 mkdir -p "$rundir" 2>/dev/null
28 chown -R "$user": "$rundir"
29 chmod -R a=rX "$rundir"
30 rm -rf rundir 2>/dev/null
31 ln -s "$rundir" rundir
32
33 # Grab config from dhcp
34 cfg=-1
35 for f in rundir/*.ntpconf; do
36         test -f "$f" || continue
37         . "$f"
38 done
39
40 # Select peers
41 p_opt=""
42 cfg=0
43 while test x"${ntpip[$cfg]}" != x""; do
44         p_opt="$p_opt -p ${ntpip[$cfg]}"
45         let cfg=cfg+1
46 done
47 test x"$p_opt" == x"" && p_opt="$default_p_opt"
48
49 if test x"$p_opt" == x""; then
50         echo "* No NTP peers configured, stopping"
51         svc -o .
52         exec sleep 1
53 fi
54
55
56 # Let others know that we are up
57 date '+%Y-%m-%d %H:%M:%S %Z' >rundir/up
58
59 # Go go go
60 echo "* Starting ntpd[$$]"
61 exec \
62 env - PATH="$PATH" \
63 softlimit \
64 setuidgid "$user" \
65 ntpd -ddnNl -S ./ntp.script $p_opt