update network service examples
[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 pool="us.pool.ntp.org" # replace "us" with your country code
10
11 service="${PWD##*/}"
12 rundir="/var/run/service/$service"
13 default_p_opt="-p 0.$pool -p 1.$pool -p 2.$pool -p 3.$pool"
14
15
16 # Make sure rundir/ exists
17 mkdir -p "$rundir" 2>/dev/null
18 chown -R "$user": "$rundir"
19 chmod -R a=rX "$rundir"
20 rm -rf rundir 2>/dev/null
21 ln -s "$rundir" rundir
22
23
24 echo "* Checking network"
25 test -f /var/run/service/fw/up || exec sleep 7
26 sleep 5  # to let it settle
27
28 # Grab config from dhcp
29 cfg=-1
30 for f in rundir/*.ntpconf; do
31         test -f "$f" || continue
32         . "$f"
33 done
34
35 # Select peers
36 p_opt=""
37 cfg=0
38 while test x"${ntpip[$cfg]}" != x""; do
39         p_opt="$p_opt -p ${ntpip[$cfg]}"
40         let cfg=cfg+1
41 done
42 test x"$p_opt" == x"" && p_opt="$default_p_opt"
43
44 if test x"$p_opt" == x""; then
45         echo "* No NTP peers configured, stopping"
46         sv o .
47         exec sleep 1
48 fi
49
50
51 # Let others know that we are up
52 date '+%Y-%m-%d %H:%M:%S %Z' >rundir/up
53
54 # Go go go
55 echo "* Starting ntpd[$$]"
56 exec \
57 env - PATH="$PATH" \
58 softlimit \
59 setuidgid "$user" \
60 ntpd -ddnNl -S ./ntp.script $p_opt