From patchwork Wed Jan 20 09:04:17 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: amine ahd X-Patchwork-Id: 570587 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from arrakis.dune.hu (arrakis.dune.hu [78.24.191.176]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id BD177140326 for ; Wed, 20 Jan 2016 20:04:42 +1100 (AEDT) Authentication-Results: ozlabs.org; dkim=fail reason="signature verification failed" (2048-bit key; unprotected) header.d=gmail.com header.i=@gmail.com header.b=Zt8z7E5w; dkim-atps=neutral Received: from arrakis.dune.hu (localhost [127.0.0.1]) by arrakis.dune.hu (Postfix) with ESMTP id 1A9FC28107B; Wed, 20 Jan 2016 10:03:54 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on arrakis.dune.hu X-Spam-Level: X-Spam-Status: No, score=-1.5 required=5.0 tests=BAYES_00,FREEMAIL_FROM, T_DKIM_INVALID autolearn=unavailable version=3.3.2 Received: from arrakis.dune.hu (localhost [127.0.0.1]) by arrakis.dune.hu (Postfix) with ESMTP id 3700828000F for ; Wed, 20 Jan 2016 10:03:47 +0100 (CET) X-policyd-weight: using cached result; rate: -8.5 Received: from mail-wm0-f44.google.com (mail-wm0-f44.google.com [74.125.82.44]) by arrakis.dune.hu (Postfix) with ESMTPS for ; Wed, 20 Jan 2016 10:03:47 +0100 (CET) Received: by mail-wm0-f44.google.com with SMTP id u188so175601830wmu.1 for ; Wed, 20 Jan 2016 01:04:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=V0HLKm7W+LwxanMaN8PCHycTq0nOqvTYsAi7UxdVkuQ=; b=Zt8z7E5wiETDcjSgYYHNSNIuc7AP3o9xMYzG0jtAloMjdNte48ut0OL/SXOOsNGmJ3 Q+dVcoLBVqXLFpfo0+hsQl1OwbBxfoR11KRev+4B/0tCThMpkI0vYH+TjNsGHDd6MV2w S7+kChF/itxXMH6Iaf8YhB//MEFCk3v3ur7tMeFiwAXImpqQPFLL8VaQTP64HC5YB284 B2uagSmb/aXUJ7ZcZRdmeNNEyPpdZyO+CRI6JL0hB2CmYJNFowYpnBW93vUj1+9dRpbx NwiD6ZbWYLe+WVwacZbi0U/FVYPMAoisvX3g4CA2T9dgjOxrclFwLR7u/s1yj0e0OOA7 +O2g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=V0HLKm7W+LwxanMaN8PCHycTq0nOqvTYsAi7UxdVkuQ=; b=Reg2bmVTXwxMW044fn5lneIMUP9FpeVhKPPGQ0bB8yYPzsJcxg+V7iaGgvV3napUI6 EntSd90RDrHuNEy60wq6JGFnfwL4kR1f0349r+47daJjxE9tfRpzBgHYfCWiCg1LCHVh I0OIOdDOtm7Pz7PnNFUI+IZivXIiXvvDq2YL9eREhRJG263uBbJzpcSmbm9ukZwAuuMf iEbcWhIyBTniQJ30m5RfmWqMyJztCv8qyxPvQYHQNAU6rHdjW006ZCYDKc27B1G8nbUt kwnWMmuNRQMmUSwt+iqqmCUgaaiHlcGxBWlxqj5JdOgHJQBdB3GHni8FpWJ3HkUVFO6p LTqQ== X-Gm-Message-State: AG10YOSd26UL3aRgQguyPRp82r99/6F+o+F4atsZfwWFuheX+wdmcWynSOAD8VDmXhssQw== X-Received: by 10.28.187.67 with SMTP id l64mr2528169wmf.39.1453280666638; Wed, 20 Jan 2016 01:04:26 -0800 (PST) Received: from localhost.localdomain ([5.158.136.57]) by smtp.gmail.com with ESMTPSA id l7sm32329437wjx.14.2016.01.20.01.04.25 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 20 Jan 2016 01:04:25 -0800 (PST) From: amine ahd To: openwrt-devel@lists.openwrt.org Date: Wed, 20 Jan 2016 10:04:17 +0100 Message-Id: <1453280657-4207-1-git-send-email-amine.ahd@gmail.com> X-Mailer: git-send-email 2.5.0 In-Reply-To: <1450800035-15465-1-git-send-email-amine.ahd@gmail.com> References: <1450800035-15465-1-git-send-email-amine.ahd@gmail.com> Subject: [OpenWrt-Devel] [PATCH] use NTP server received via DHCP X-BeenThere: openwrt-devel@lists.openwrt.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: OpenWrt Development List List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: openwrt-devel-bounces@lists.openwrt.org Sender: "openwrt-devel" 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. Changes from V1: -Users could choose not to use DHCP by setting "use_dhcp" to 0 in /etc/config/system under the ntp section. -Users could specify which interfaces to use to get NTP servers from. -Sysntpd will exit if no servers are specified in the static list and the DHCP option is disabled. -Sysntpd will restart only if all of the following conditions are met: *The user allowed DHCP to be used for NTP. *The iface action is UP. *The iface is specified in the list. *The protocol in use is either DHCP or DHCPv6. -Code improvements. Signed-off-by: amine hamed --- package/utils/busybox/Makefile | 3 ++ package/utils/busybox/files/sysntpd | 31 +++++++++++++++-- package/utils/busybox/files/sysntpd.hotplug | 54 +++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 3 deletions(-) create mode 100644 package/utils/busybox/files/sysntpd.hotplug diff --git a/package/utils/busybox/Makefile b/package/utils/busybox/Makefile index 5ca4363..3066a85 100644 --- a/package/utils/busybox/Makefile +++ b/package/utils/busybox/Makefile @@ -111,6 +111,9 @@ define Package/busybox/install $(CP) $(PKG_INSTALL_DIR)/* $(1)/ $(INSTALL_BIN) ./files/cron $(1)/etc/init.d/cron $(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..c5dac8b 100755 --- a/package/utils/busybox/files/sysntpd +++ b/package/utils/busybox/files/sysntpd @@ -1,6 +1,9 @@ #!/bin/sh /etc/rc.common # Copyright (C) 2011 OpenWrt.org +. /lib/functions.sh +. /usr/share/libubox/jshn.sh + START=98 USE_PROCD=1 @@ -13,21 +16,43 @@ validate_ntp_section() { } start_service() { - local server enabled enable_server peer + local server enabled enable_server peer ntpservers iface status ntpserver dump + local dhcp_ifaces="$(uci -q get system.ntp.dhcp_ifaces)" + config_load system + config_get_bool "use_dhcp" "ntp" "use_dhcp" validate_ntp_section ntp || { echo "validation failed" return 1 } [ $enabled = 0 ] && return - - [ -z "$server" ] && return + [ -z "$server" ] && [ "$use_dhcp" = 0 ] && 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" + + [ "$use_dhcp" = 1 ] && { + if [ -z "$dhcp_ifaces" ]; then + dump="$(ubus call network.interface dump)" + ntpservers=$(jsonfilter -s "$dump" -e '$["interface"][*]["data"]["ntpserver"]') + else + for iface in $dhcp_ifaces; do + status="$(ubus call network.interface.$iface status)" + ntpserver=$(jsonfilter -s "$status" -e '$["data"]["ntpserver"]') + [ -n "$ntpserver" ] && \ + ntpservers="$ntpservers $ntpserver" + done + fi + # add this data so we can use it in the sysntpd hotplug script. + procd_set_param data ntp_servers="$ntpservers $server" + for ntpserver in $ntpservers; do + procd_append_param command -p $ntpserver + done + } + for peer in $server; do procd_append_param command -p $peer done diff --git a/package/utils/busybox/files/sysntpd.hotplug b/package/utils/busybox/files/sysntpd.hotplug new file mode 100644 index 0000000..34a2f7a --- /dev/null +++ b/package/utils/busybox/files/sysntpd.hotplug @@ -0,0 +1,54 @@ +#!/bin/sh + +. /lib/functions.sh +. /usr/share/libubox/jshn.sh + +is_valid_interface() { + local list="$(uci get system.ntp.dhcp_ifaces)" + [ -z "$list" ] && return 0 + + case " $list " in + *" $INTERFACE "*) + return 0 + ;; + *) + return 1 + ;; + esac +} + +config_load system +local proto="$(uci get network.$INTERFACE.proto)" +config_get_bool "use_dhcp" "ntp" "use_dhcp" +[ "$use_dhcp" = 1 ] && [ "$ACTION" = ifup ] && is_valid_interface && [ "$proto" = dhcp -o "$proto" = dhcp6 ] || exit 0 + +handle_default_ntp_servers() { + local server="$1" + new_ntp_servers="$new_ntp_servers $server" +} + +local dhcp_ntp_servers iface status ntpserver dump +local dhcp_ifaces="$(uci -q get system.ntp.dhcp_ifaces)" +if [ -z "$dhcp_ifaces" ]; then + dump="$(ubus call network.interface dump)" + dhcp_ntp_servers=$(jsonfilter -s "$dump" -e '$["interface"][*]["data"]["ntpserver"]') +else + for iface in $dhcp_ifaces; do + status="$(ubus call network.interface.$iface status)" + ntpserver=$(jsonfilter -s "$status" -e '$["data"]["ntpserver"]') + [ -n "$ntpserver" ] && \ + dhcp_ntp_servers="$dhcp_ntp_servers $ntpserver" + done +fi + +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_list_foreach "ntp" "server" handle_default_ntp_servers + +#get the current list of ntp servers in the running instance +local current_ntp_servers=$(ubus call service list '{"name":"sysntpd", "verbose":true}' | jsonfilter -e '$["sysntpd"]["instances"][*]["data"]["ntp_servers"]') +#if its an up action, the iface uses DHCP and the new list of ntp servers is different from the old, restart sysntpd +[ "$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