From patchwork Tue Jan 12 08:44:53 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: amine ahd X-Patchwork-Id: 566411 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 BB0D71402F0 for ; Tue, 12 Jan 2016 19:45:19 +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=zgsp1XGb; dkim-atps=neutral Received: from arrakis.dune.hu (localhost [127.0.0.1]) by arrakis.dune.hu (Postfix) with ESMTP id 6523F28BF31; Tue, 12 Jan 2016 09:44:39 +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 9936F28073C for ; Tue, 12 Jan 2016 09:44:23 +0100 (CET) X-policyd-weight: using cached result; rate: -8.5 Received: from mail-wm0-f68.google.com (mail-wm0-f68.google.com [74.125.82.68]) by arrakis.dune.hu (Postfix) with ESMTPS for ; Tue, 12 Jan 2016 09:44:23 +0100 (CET) Received: by mail-wm0-f68.google.com with SMTP id u188so30048910wmu.0 for ; Tue, 12 Jan 2016 00:44:57 -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=pFjPvSguKibsqBVCBNiyW9SgnpfUBUNaj6vL/CvCk0E=; b=zgsp1XGbsQcNK2NuyFM31RrDA5v67+2y+AqmLhhZQERAmliW9sxM7GaKMdl15jhTkV lN7Ohy1GPm8UVb6FjY1akIuA1lxhXaDxdS7SwNK4cFaz3nztMst5qWhx7juYBiLVypw+ p6J+4NVioN4bAaVBk4SBSP+00/ikznZ3ed+gemf98Y3kRA4Q9UlyZ+X5SUnA5GQ2GrkY bX7kAybVU5ZaXuAZOe2rsfmi6Se853RFeVFlJ72vRVCwKUaxtZhRBchuprZllTqIITPi s0lv7km3JkRo7/L3yhIWaRBkrfEsnPrXsrjXHMfeMP9jSkknvxVrU4Go73R6w1KcAL+d 50kg== X-Received: by 10.28.105.25 with SMTP id e25mr19543503wmc.62.1452588296932; Tue, 12 Jan 2016 00:44:56 -0800 (PST) Received: from localhost.localdomain ([5.158.136.57]) by smtp.gmail.com with ESMTPSA id ha9sm27002335wjc.3.2016.01.12.00.44.55 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 12 Jan 2016 00:44:56 -0800 (PST) From: amine ahd To: openwrt-devel@lists.openwrt.org Date: Tue, 12 Jan 2016 09:44:53 +0100 Message-Id: <1452588293-3688-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 1/1] 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" --- package/utils/busybox/Makefile | 3 ++ package/utils/busybox/files/sysntpd | 26 ++++++++++- package/utils/busybox/files/sysntpd.hotplug | 53 ++++++++++++++++++++++ 3 files changed, 80 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..cc75cf9 100755 --- a/package/utils/busybox/files/sysntpd +++ b/package/utils/busybox/files/sysntpd @@ -1,6 +1,8 @@ #!/bin/sh /etc/rc.common # Copyright (C) 2011 OpenWrt.org +. /lib/functions.sh +. /usr/share/libubox/jshn.sh START=98 USE_PROCD=1 @@ -22,12 +24,32 @@ start_service() { [ $enabled = 0 ] && return - [ -z "$server" ] && 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" + + local use_dhcp="$(uci -q get system.ntp.use_dhcp)" + local dhcp_ifaces="$(uci -q get system.ntp.dhcp_ifaces)" + [ "$use_dhcp" = 1 ] && { + if [ -z "$dhcp_ifaces" ]; then + local dump=$(ubus call network.interface dump) + ntpservers=$(jsonfilter -s "$dump" -e '$["interface"][*]["data"]["ntpserver"]') + else + for iface in $dhcp_ifaces; do + local status=$(ubus call network.interface.$iface status) + local 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 100755 index 0000000..c0d0f37 --- /dev/null +++ b/package/utils/busybox/files/sysntpd.hotplug @@ -0,0 +1,53 @@ +#!/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 +} + +local proto="$(uci get network.$INTERFACE.proto)" +local use_dhcp="$(uci -q get system.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_ifaces="$(uci -q get system.ntp.dhcp_ifaces)" +if [ -z "$dhcp_ifaces" ]; then + local dump=$(ubus call network.interface dump) + local dhcp_ntp_servers=$(jsonfilter -s "$dump" -e '$["interface"][*]["data"]["ntpserver"]') +else + for iface in $dhcp_ifaces; do + local status=$(ubus call network.interface.$iface status) + local 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_load system +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