From patchwork Wed Dec 12 03:45:37 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Matt Weber X-Patchwork-Id: 1011531 Return-Path: X-Original-To: incoming-buildroot@patchwork.ozlabs.org Delivered-To: patchwork-incoming-buildroot@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (mailfrom) smtp.mailfrom=busybox.net (client-ip=140.211.166.138; helo=whitealder.osuosl.org; envelope-from=buildroot-bounces@busybox.net; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=rockwellcollins.com Received: from whitealder.osuosl.org (smtp1.osuosl.org [140.211.166.138]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 43F2m04jczz9s47 for ; Wed, 12 Dec 2018 14:46:08 +1100 (AEDT) Received: from localhost (localhost [127.0.0.1]) by whitealder.osuosl.org (Postfix) with ESMTP id 7F3F2863D9; Wed, 12 Dec 2018 03:46:05 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from whitealder.osuosl.org ([127.0.0.1]) by localhost (.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id rBoGCtJNRb2C; Wed, 12 Dec 2018 03:46:03 +0000 (UTC) Received: from ash.osuosl.org (ash.osuosl.org [140.211.166.34]) by whitealder.osuosl.org (Postfix) with ESMTP id 8D2B5863B7; Wed, 12 Dec 2018 03:46:03 +0000 (UTC) X-Original-To: buildroot@lists.busybox.net Delivered-To: buildroot@osuosl.org Received: from fraxinus.osuosl.org (smtp4.osuosl.org [140.211.166.137]) by ash.osuosl.org (Postfix) with ESMTP id 68E191BF3C1 for ; Wed, 12 Dec 2018 03:46:02 +0000 (UTC) Received: from localhost (localhost [127.0.0.1]) by fraxinus.osuosl.org (Postfix) with ESMTP id B276785E15 for ; Wed, 12 Dec 2018 03:45:40 +0000 (UTC) X-Virus-Scanned: amavisd-new at osuosl.org Received: from fraxinus.osuosl.org ([127.0.0.1]) by localhost (.osuosl.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id vzSYYrMLqgop for ; Wed, 12 Dec 2018 03:45:39 +0000 (UTC) X-Greylist: domain auto-whitelisted by SQLgrey-1.7.6 Received: from da1vs01.rockwellcollins.com (da1vs01.rockwellcollins.com [205.175.227.27]) by fraxinus.osuosl.org (Postfix) with ESMTPS id 852A885E0B for ; Wed, 12 Dec 2018 03:45:39 +0000 (UTC) Received: from ofwda1n02.rockwellcollins.com (HELO crulimr01.rockwellcollins.com) ([205.175.227.14]) by da1vs01.rockwellcollins.com with ESMTP; 11 Dec 2018 21:45:38 -0600 X-Received: from largo.rockwellcollins.com (unknown [192.168.140.76]) by crulimr01.rockwellcollins.com (Postfix) with ESMTP id 5EA81603E8; Tue, 11 Dec 2018 21:45:38 -0600 (CST) From: Matt Weber To: buildroot@buildroot.org Date: Tue, 11 Dec 2018 21:45:37 -0600 Message-Id: <1544586337-7888-1-git-send-email-matthew.weber@rockwellcollins.com> X-Mailer: git-send-email 1.9.1 Subject: [Buildroot] [PATCH v5] package/ntp: sntp time sync script X-BeenThere: buildroot@busybox.net X-Mailman-Version: 2.1.29 Precedence: list List-Id: Discussion and development of buildroot List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Oscar Gomez Fuente MIME-Version: 1.0 Errors-To: buildroot-bounces@busybox.net Sender: "buildroot" This patch adds the installation of a startup script if the sntp utility is selected as an option. The utility is design to do a one time step/slew adjustment of the system time (similar to the ntpdate tool http://support.ntp.org/bin/view/Dev/DeprecatingNtpdate). One nice benefit over ntpdate is that sntp can run while ntpd is still running. However, ntpd may still need to be restarted if the time step was large enough. The script provides the ability to override the arguments as part of a /etc/defaults/sntp file. On a local LAN, the initial large step adjustment took less then one second to be retrieved and system time updated. If a user already has a RTC maintaining the time and the system was powered off for a long period of time, the script assumes a slew adjustment when +/- 128ms, rather then a time step(jump). This could be further tuned by a user with the /etc/defaults/sntp configuration file. One NTP pool server is being set as sntp uses all of the servers provided when the DNS is resolved as servers to attempt to retrieve time from before timing out. It looks like currently that is 4 servers per *pool.ntp.org hostname. Cc: Oscar Gomez Fuente Signed-off-by: Matthew Weber Tested-by: Oscar Gomez Fuente --- Changes v4 -> v5 - Fixed permissions on S48sntp and S49ntp scripts [Arnout - Updated script to follow Carlos's recently merged syslog script refresh - Added kconfig help comment about the script - Added notes in S49sntp about behavior of the tool - shellcheck fixes - Moved touch of key database file into start function v3 -> v4 - Updated S48sntp script to just use a single pool host which provides 4 NTP servers sntp will query before timing out v2 -> v3 - Moved initial time set to a S48sntp script as originally suggested by Oscar and preferred by Arnout - Switched to sntp tool as it simplified the logic around when and how do we call the tool to perform this time sync (it handles when to slew/step and multiple servers at once) v1 -> v2 [Arnout - Moved NTP_WAIT_DELAY above sourcing of configuration file - Switched to using unix time for current time check - Added comment on why redirection of stdout and stderr - Attempted to use the pid file but found it added more calls to the shell then using a variable, so switched back - Adjusted printout so the actual ntpd service start was aligned with the respecting Ok/Fail. - Dropped -g from the main service call as the "big" time jump should have already occurred with the one-shot ntpd -gq. v1 Based on Oscar's v1 patch to add a ntpdate startup script. http://patchwork.ozlabs.org/patch/986852/ --- package/ntp/Config.in | 7 ++++++- package/ntp/S48sntp | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++ package/ntp/S49ntp | 0 package/ntp/ntp.mk | 9 +++++++++ 4 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 package/ntp/S48sntp mode change 100755 => 100644 package/ntp/S49ntp diff --git a/package/ntp/Config.in b/package/ntp/Config.in index efd47e1..97d933b 100644 --- a/package/ntp/Config.in +++ b/package/ntp/Config.in @@ -12,7 +12,12 @@ if BR2_PACKAGE_NTP config BR2_PACKAGE_NTP_SNTP bool "sntp" help - Simple network time protocol program + Simple network time protocol program (a replacement + for the ntpdate tool) + + A script is installed as S48sntp which will retrieve and + step the time if there is a large difference before ntpd + takes over the necessary slew adjustments in S49ntp. config BR2_PACKAGE_NTP_NTP_KEYGEN bool "ntp-keygen" diff --git a/package/ntp/S48sntp b/package/ntp/S48sntp new file mode 100644 index 0000000..96d8d50 --- /dev/null +++ b/package/ntp/S48sntp @@ -0,0 +1,55 @@ +#!/bin/sh + +DAEMON="sntp" +# sntp uses all the IPs resolved for the hostname (i.e. pool.ntp.org has 4). +# It will try each until they either all timeout or time has been set. Thus +# default to only providing one NTP pool host. +SNTP_SERVERS="pool.ntp.org" +# Step if time delta is greater then 128ms, otherwise slew +SNTP_ARGS="-Ss -M 128" +SNTP_KEY_CACHE="/tmp/kod" + +# shellcheck source=/dev/null +[ -r "/etc/default/$DAEMON" ] && . "/etc/default/$DAEMON" + +start() { + printf 'Starting %s: ' "$DAEMON" + # Create key cache file to prevents warning that file is missing + touch $SNTP_KEY_CACHE + # shellcheck disable=SC2086 # we need the word splitting + /usr/bin/$DAEMON $SNTP_ARGS -K $SNTP_KEY_CACHE $SNTP_SERVERS + # sntp behavior + # - Does not background + # - Does not infinitely block + # - Time-out w/o network = ~2 sec + # - Time-out w/ network = ~5sec * # of servers + status=$? + if [ "$status" -eq 0 ]; then + echo "OK" + else + echo "FAIL" + fi + return "$status" +} + +stop() { + echo "Nothing to do, $DAEMON is not a daemon." +} + +restart() { + stop + sleep 1 + start +} + +reload() { + echo "Nothing to do, $DAEMON does not support reload." +} + +case "$1" in + start|stop|restart|reload) + "$1";; + *) + echo "Usage: $0 {start|stop|restart|reload}" + exit 1 +esac diff --git a/package/ntp/S49ntp b/package/ntp/S49ntp old mode 100755 new mode 100644 diff --git a/package/ntp/ntp.mk b/package/ntp/ntp.mk index af3c1aa..aad1cdd 100644 --- a/package/ntp/ntp.mk +++ b/package/ntp/ntp.mk @@ -93,9 +93,18 @@ define NTP_INSTALL_TARGET_CMDS $(INSTALL) -m 644 package/ntp/ntpd.etc.conf $(TARGET_DIR)/etc/ntp.conf endef +# This script will step the time if there is a large difference +# before ntpd takes over the necessary slew adjustments +ifeq ($(BR2_PACKAGE_NTP_SNTP),y) +define NTP_INSTALL_INIT_SYSV_SNTP + $(INSTALL) -D -m 755 package/ntp/S48sntp $(TARGET_DIR)/etc/init.d/S48sntp +endef +endif + ifeq ($(BR2_PACKAGE_NTP_NTPD),y) define NTP_INSTALL_INIT_SYSV $(INSTALL) -D -m 755 package/ntp/S49ntp $(TARGET_DIR)/etc/init.d/S49ntp + $(NTP_INSTALL_INIT_SYSV_SNTP) endef define NTP_INSTALL_INIT_SYSTEMD