Denys Vlasenko | 4fc82e0 | 2010-01-13 22:07:34 +0100 | [diff] [blame] | 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 |
Denys Vlasenko | aa75a7d | 2018-07-03 17:02:50 +0200 | [diff] [blame] | 9 | start_delay=15 |
| 10 | net_down_delay=5 |
Denys Vlasenko | 4fc82e0 | 2010-01-13 22:07:34 +0100 | [diff] [blame] | 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 | |
Denys Vlasenko | aa75a7d | 2018-07-03 17:02:50 +0200 | [diff] [blame] | 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 |
Denys Vlasenko | 4fc82e0 | 2010-01-13 22:07:34 +0100 | [diff] [blame] | 25 | |
| 26 | # Make sure rundir/ exists |
| 27 | mkdir -p "$rundir" 2>/dev/null |
Denys Vlasenko | 4f8ecf2 | 2015-10-24 14:55:33 +0200 | [diff] [blame] | 28 | chown -R "$user": "$rundir" |
Denys Vlasenko | 4fc82e0 | 2010-01-13 22:07:34 +0100 | [diff] [blame] | 29 | chmod -R a=rX "$rundir" |
| 30 | rm -rf rundir 2>/dev/null |
| 31 | ln -s "$rundir" rundir |
| 32 | |
Denys Vlasenko | 4fc82e0 | 2010-01-13 22:07:34 +0100 | [diff] [blame] | 33 | # Grab config from dhcp |
| 34 | cfg=-1 |
| 35 | for f in rundir/*.ntpconf; do |
Denys Vlasenko | aa75a7d | 2018-07-03 17:02:50 +0200 | [diff] [blame] | 36 | test -f "$f" || continue |
| 37 | . "$f" |
Denys Vlasenko | 4fc82e0 | 2010-01-13 22:07:34 +0100 | [diff] [blame] | 38 | done |
| 39 | |
| 40 | # Select peers |
| 41 | p_opt="" |
| 42 | cfg=0 |
| 43 | while test x"${ntpip[$cfg]}" != x""; do |
Denys Vlasenko | aa75a7d | 2018-07-03 17:02:50 +0200 | [diff] [blame] | 44 | p_opt="$p_opt -p ${ntpip[$cfg]}" |
| 45 | let cfg=cfg+1 |
Denys Vlasenko | 4fc82e0 | 2010-01-13 22:07:34 +0100 | [diff] [blame] | 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" |
Denys Vlasenko | aa75a7d | 2018-07-03 17:02:50 +0200 | [diff] [blame] | 51 | svc -o . |
Denys Vlasenko | 4fc82e0 | 2010-01-13 22:07:34 +0100 | [diff] [blame] | 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 |