0ed663750a8c25586f29cd84e59d4137b77e47dc
[feed/packages.git] / net / ntpd / files / ntpd.init
1 #!/bin/sh /etc/rc.common
2 # Copyright (C) 2006-2011 OpenWrt.org
3
4 START=65
5 STOP=65
6
7 USE_PROCD=1
8 PROG=/sbin/ntpd
9 HOTPLUG_HELPER=/usr/sbin/ntpd.hotplug-helper
10
11 config_file=/var/run/ntpd.conf
12
13 trunc() {
14 echo -n "" > $config_file
15 }
16
17 emit() {
18 echo -e "$@" >> $config_file
19 }
20
21 validate_ntp_section() {
22 uci_validate_section system timeserver "${1}" \
23 'server:list(host)' 'enabled:bool:1' 'enable_server:bool:0' \
24 'interface:list(string)'
25 }
26
27 start_service() {
28 local server enabled enable_server interface intf
29
30 validate_ntp_section ntp || {
31 echo "validation failed"
32 return 1
33 }
34
35 [ "$enabled" = 0 ] && return
36
37 [ -z "$server" -a "$enable_server" = 0 ] && return
38
39 # not sure that the interfaces enumerated should be validated,
40 # since some of them might be dynamic interfaces (like IPsec
41 # tunnels) which aren't known by ubus.
42
43 trunc
44 emit "driftfile /var/lib/ntp/ntp.drift\n"
45
46 if [ "$enable_server" != 0 ]; then
47 emit "restrict default limited kod nomodify notrap nopeer"
48 emit "restrict -6 default limited kod nomodify notrap nopeer"
49 else
50 emit "restrict -4 default noserve"
51 emit "restrict -6 default noserve"
52 fi
53 emit "restrict source noquery"
54
55 emit "\n# No limits for local monitoring"
56 emit "restrict 127.0.0.1"
57 emit "restrict -6 ::1\n"
58
59 if [ -n "$interface" ]; then
60 local loopback=$(ubus call network.interface dump | jsonfilter -e "@.interface[@.interface='loopback']['device']")
61
62 local saw_lo=
63 for intf in $interface; do
64 emit "interface listen $intf"
65 [ "$intf" = "$loopback" ] && saw_lo=1
66 done
67 [ -z "$saw_lo" ] && emit "interface listen $loopback"
68 emit ""
69 fi
70
71 emit "server $server iburst"
72
73 mkdir -p /var/lib/ntp
74 chown -R ntp:ntp /var/lib/ntp
75
76 procd_open_instance
77 procd_set_param command $PROG -g -u ntp:ntp -p /var/run/ntpd.pid -n \
78 -c $config_file
79 procd_close_instance
80
81 procd_open_instance
82 procd_set_param command $HOTPLUG_HELPER
83 procd_close_instance
84 }