[OpenWrt-Devel] [PATCH] use NTP server received via DHCP

Yousong Zhou yszhou4tech at gmail.com
Thu Dec 24 08:35:18 EST 2015


Hi, amine

On 23 December 2015 at 00:00, amine ahd <amine.ahd at gmail.com> wrote:
> The current state of NTP is to load the list of NTP servers
> from the static file /etc/config/system.
> This patch allows ntpd to get NTP servers from DHCP.
> ntpd will restart whenever the list of NTP servers is changed.
>
> Signed-off-by: amine hamed <amine.ahd at gmail.com>
> ---
>  package/utils/busybox/Makefile             |  3 +++
>  package/utils/busybox/files/sysntpd        | 15 +++++++++--
>  .../package/utils/busybox/files/sysntpd.hotplug    | 31 ++++++++++++++++++++++
>  3 files changed, 47 insertions(+), 2 deletions(-)
>  create mode 100755 package/utils/busybox/files/sysntpd.hotplug
>
> diff --git a/package/utils/busybox/Makefile b/package/utils/busybox/Makefile
> index 9571d48..3c33246 100644
> --- a/package/utils/busybox/Makefile
> +++ b/package/utils/busybox/Makefile
> @@ -112,6 +112,9 @@ define Package/busybox/install
>         $(INSTALL_BIN) ./files/cron $(1)/etc/init.d/cron
>         $(INSTALL_BIN) ./files/telnet $(1)/etc/init.d/telnet
>         $(INSTALL_BIN) ./files/sysntpd $(1)/etc/init.d/sysntpd
> +       $(INSTALL_DIR) $(1)/etc/hotplug.d
> +       $(INSTALL_DIR) $(1)/etc/hotplug.d/iface
> +       $(INSTALL_BIN) ./files/sysntpd.hotplug $(1)/etc/hotplug.d/iface/30-sysntpd
>         $(INSTALL_BIN) ./files/ntpd-hotplug $(1)/usr/sbin/ntpd-hotplug
>         -rm -rf $(1)/lib64
>  endef
> diff --git a/package/utils/busybox/files/sysntpd b/package/utils/busybox/files/sysntpd
> index f73bb83..fbe1838 100755
> --- a/package/utils/busybox/files/sysntpd
> +++ b/package/utils/busybox/files/sysntpd
> @@ -1,12 +1,12 @@
>  #!/bin/sh /etc/rc.common
>  # Copyright (C) 2011 OpenWrt.org
>
> +. /lib/functions.sh
>  START=98
>
>  USE_PROCD=1
>  PROG=/usr/sbin/ntpd
>  HOTPLUG_SCRIPT=/usr/sbin/ntpd-hotplug
> -
>  validate_ntp_section() {
>         uci_validate_section system timeserver "${1}" \
>                 'server:list(host)' 'enabled:bool:1' 'enable_server:bool:0'
> @@ -15,6 +15,8 @@ validate_ntp_section() {
>  start_service() {
>         local server enabled enable_server peer
>
> +       #get the list of ntp servers from DHCP using ubus.
> +       ntpservers=`ubus call network.interface dump | grep "ntpserver" | cut -d":" -f2 | tr -d '"'`

This can be done with help from jsonfilter

        ubus call network.interface dump | jsonfilter -e
'$["interface"][*]["data"]["ntpserver"]'

It should also be possible for users to specify sources of timeserver
settings, whether it be static list of servers in uci configs or dhcp
settings from some network interfaces.

>         validate_ntp_section ntp || {
>                 echo "validation failed"
>                 return 1
> @@ -22,12 +24,20 @@ start_service() {
>
>         [ $enabled = 0 ] && return
>
> -       [ -z "$server" ] && return
> +       [ -z "$server" ] && [ "$ntpservers" == "" ] && return
>
>         procd_open_instance
>         procd_set_param command "$PROG" -n
>         [ "$enable_server" = "1" ] && procd_append_param command -l
>         [ -x "$HOTPLUG_SCRIPT" ] && procd_append_param command -S "$HOTPLUG_SCRIPT"
> +
> +       #add this data so we can use it in the sysntpd hotplug script.
> +       procd_set_param data ntp_servers="$ntpservers $server"
> +

Not quite sure about this, but are we going to replace uci_set_state
with procd data param?  Personally I prefer uci state for such
"dynamic" data store

Regards,

                yousong

> +       for ntpserver in $ntpservers; do
> +               procd_append_param command -p $ntpserver
> +       done
> +
>         for peer in $server; do
>                 procd_append_param command -p $peer
>         done
> @@ -38,5 +48,6 @@ start_service() {
>  service_triggers()
>  {
>         procd_add_reload_trigger "system"
> +
>         procd_add_validation validate_ntp_section
>  }
> diff --git a/package/utils/busybox/files/sysntpd.hotplug b/package/utils/busybox/files/sysntpd.hotplug
> new file mode 100755
> index 0000000..de2946a
> --- /dev/null
> +++ b/package/utils/busybox/files/sysntpd.hotplug
> @@ -0,0 +1,31 @@
> +#!/bin/sh
> +
> +. /lib/functions.sh
> +
> +[ "$ACTION" = ifup ] || exit 0
> +
> +handle_default_ntp_servers() {
> +       local server="$1"
> +       # append the server to the list
> +       new_ntp_servers="$new_ntp_servers $server"
> +}
> +
> +local proto=`uci get network.$INTERFACE.proto`
> +
> +#get the list of ntp servers returned from DHCP, remote leading and trailing whitespaces as well as string quotes
> +dhcp_ntp_servers=`ubus call network.interface dump | grep "ntpserver" | cut -d":" -f2 | sed 's/\"//g;s/^[ \t]*//;s/[ \t]*$//'`
> +
> +new_ntp_servers="$dhcp_ntp_servers"
> +
> +#get the default list of ntp servers from the config file and append it to the new list
> +config_load system
> +config_list_foreach "ntp" "server" handle_default_ntp_servers
> +
> +#get the current list of ntp servers in the running instance
> +current_ntp_servers=`ubus call service get_data '{"name":"sysntpd"}' | grep "ntp_servers" | cut -d":" -f2 | sed 's/\"//g;s/^[ \t]*//;s/[ \t]*$//'`
> +
> +#if its an up action, the iface uses DHCP and the new list of ntp servers is different from the old, restart sysntpd
> +[ "$proto" == "dhcp" ]  && [ "$current_ntp_servers" != "$new_ntp_servers" ] || exit 0
> +
> +logger -t sysntpd "Reloading sysntpd due to $ACTION of interface $INTERFACE and a change of NTP servers"
> +/etc/init.d/sysntpd enabled && /etc/init.d/sysntpd reload
> \ No newline at end of file
> --
> 2.5.0
> _______________________________________________
> openwrt-devel mailing list
> openwrt-devel at lists.openwrt.org
> https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-devel
_______________________________________________
openwrt-devel mailing list
openwrt-devel at lists.openwrt.org
https://lists.openwrt.org/cgi-bin/mailman/listinfo/openwrt-devel



More information about the openwrt-devel mailing list