You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
1.6 KiB

  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=$(command -v 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 interface
  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=$(busybox 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