blob: 5c90aad412c016c8fed48b8458788e8640c93c8b [file] [log] [blame]
Denys Vlasenko4fc82e02010-01-13 22:07:34 +01001#!/bin/bash
2# (using bashism (arrays) in dhcp config)
3
4#exec >/dev/null
5exec 2>&1
6exec </dev/null
7
8user=root
Denys Vlasenkoaa75a7d2018-07-03 17:02:50 +02009start_delay=15
10net_down_delay=5
Denys Vlasenko4fc82e02010-01-13 22:07:34 +010011pool="us.pool.ntp.org" # replace "us" with your country code
12
13service="${PWD##*/}"
14rundir="/var/run/service/$service"
15default_p_opt="-p 0.$pool -p 1.$pool -p 2.$pool -p 3.$pool"
16
Denys Vlasenkoaa75a7d2018-07-03 17:02:50 +020017echo "* Checking network"
18test -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:
24sleep $start_delay
Denys Vlasenko4fc82e02010-01-13 22:07:34 +010025
26# Make sure rundir/ exists
27mkdir -p "$rundir" 2>/dev/null
Denys Vlasenko4f8ecf22015-10-24 14:55:33 +020028chown -R "$user": "$rundir"
Denys Vlasenko4fc82e02010-01-13 22:07:34 +010029chmod -R a=rX "$rundir"
30rm -rf rundir 2>/dev/null
31ln -s "$rundir" rundir
32
Denys Vlasenko4fc82e02010-01-13 22:07:34 +010033# Grab config from dhcp
34cfg=-1
35for f in rundir/*.ntpconf; do
Denys Vlasenkoaa75a7d2018-07-03 17:02:50 +020036 test -f "$f" || continue
37 . "$f"
Denys Vlasenko4fc82e02010-01-13 22:07:34 +010038done
39
40# Select peers
41p_opt=""
42cfg=0
43while test x"${ntpip[$cfg]}" != x""; do
Denys Vlasenkoaa75a7d2018-07-03 17:02:50 +020044 p_opt="$p_opt -p ${ntpip[$cfg]}"
45 let cfg=cfg+1
Denys Vlasenko4fc82e02010-01-13 22:07:34 +010046done
47test x"$p_opt" == x"" && p_opt="$default_p_opt"
48
49if test x"$p_opt" == x""; then
50 echo "* No NTP peers configured, stopping"
Denys Vlasenkoaa75a7d2018-07-03 17:02:50 +020051 svc -o .
Denys Vlasenko4fc82e02010-01-13 22:07:34 +010052 exec sleep 1
53fi
54
55
56# Let others know that we are up
57date '+%Y-%m-%d %H:%M:%S %Z' >rundir/up
58
59# Go go go
60echo "* Starting ntpd[$$]"
61exec \
62env - PATH="$PATH" \
63softlimit \
64setuidgid "$user" \
65ntpd -ddnNl -S ./ntp.script $p_opt