1
0

ntpclient.hotplug 1.6 KB

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