[packages] remove 'svn:keywords' property on /packages as well, not needed anymore...
[openwrt/svn-archive/archive.git] / net / ntpclient / files / ntpclient.hotplug
1 #!/bin/sh
2 # Copyright (C) 2006 OpenWrt.org
3
4 . /etc/functions.sh
5
6 unset SERVER
7 unset PORT
8 unset INTERVAL
9 unset COUNT
10 NTPC=`which ntpclient`
11
12 check_server() {
13 local hostname
14 local port
15 [ -n "$SERVER" ] && return
16 config_get hostname $1 hostname
17 config_get port $1 port
18 [ -z "$hostname" ] && return
19 $NTPC -c 1 -p ${port:-123} -h $hostname > /dev/null && { SERVER=$hostname; PORT=${port:-123}; }
20 }
21
22 set_drift() {
23 config_get freq $1 freq
24 [ -n "$freq" ] && adjtimex -f $freq >/dev/null
25 }
26
27 start_ntpclient() {
28 config_foreach set_drift ntpdrift
29 config_foreach check_server ntpserver
30 [ -z "$SERVER" ] && exit 0
31 logger starting ntpclient
32 $NTPC ${COUNT:+-c $COUNT} ${INTERVAL:+-i $INTERVAL} -s -l -D -p $PORT -h $SERVER 2> /dev/null
33 }
34
35 stop_ntpclient() {
36 logger stopping ntpclient
37 killall ntpclient
38 }
39
40 load_settings() {
41 local interval
42 local count
43 local iface
44
45 config_get interval $1 interval
46 config_get count $1 count
47
48 [ -n "$count" ] && COUNT=$count
49 [ -n "$interval" ] && INTERVAL=$interval
50 }
51
52 config_load ntpclient
53 config_foreach load_settings ntpclient
54
55 DEF_ROUTE=`route -n | grep '^0.0.0.0'`
56 NTP_RUNNING=`ps | grep $NTPC | grep -v grep`
57
58 case "${ACTION:-ifup}" in
59 ifup)
60 [ -n "$DEF_ROUTE" -a -z "$NTP_RUNNING" ] && start_ntpclient
61 ;;
62 ifdown)
63 [ -n "$NTP_RUNNING" ] && stop_ntpclient
64 ;;
65 esac