From 4d54633c6d05faefe0fa6761708e0ce404b844c4 Mon Sep 17 00:00:00 2001 From: Gordon Lack Date: Mon, 17 Feb 2025 01:21:42 +0000 Subject: [PATCH] Move ntpdate-sync from if-up.d to init.d script. Refactor ntpdate-sync. --- .../openvix/image/ntpd-sync.bb | 26 ++++- .../openvix/image/ntpd-sync/ntp-setdate | 26 +++++ .../openvix/image/ntpd-sync/ntpdate-sync | 103 +++++++----------- 3 files changed, 85 insertions(+), 70 deletions(-) create mode 100755 meta-oe/recipes-distros/openvix/image/ntpd-sync/ntp-setdate mode change 100644 => 100755 meta-oe/recipes-distros/openvix/image/ntpd-sync/ntpdate-sync diff --git a/meta-oe/recipes-distros/openvix/image/ntpd-sync.bb b/meta-oe/recipes-distros/openvix/image/ntpd-sync.bb index 19eed5a359..dadb39bf0d 100644 --- a/meta-oe/recipes-distros/openvix/image/ntpd-sync.bb +++ b/meta-oe/recipes-distros/openvix/image/ntpd-sync.bb @@ -3,19 +3,37 @@ PRIORITY = "required" MAINTAINER = "openvix team" require conf/license/license-gplv2.inc -inherit allarch +inherit allarch update-rc.d PV = "${IMAGE_VERSION}" -PR = "r2" +PR = "r3" NTPD_SYNC_LOC := "${THISDIR}/${PN}" S = "${WORKDIR}/sources" UNPACKDIR = "${S}" +# 95 seems to be the earliest useful point for some Wifi systems +# +INITSCRIPT_NAME = "ntp-setdate" +INITSCRIPT_PARAMS = "start 95 3 ." + do_install() { install -d ${D}${bindir} install -m 755 ${NTPD_SYNC_LOC}/ntpdate-sync ${D}${bindir}/ntpdate-sync - install -d ${D}${sysconfdir}/rc3.d - ln -sf ${bindir}/ntpdate-sync ${D}${sysconfdir}/rc3.d/S99ntpdate-sync + + install -d ${D}${sysconfdir}/init.d + install -m 755 ${NTPD_SYNC_LOC}/${INITSCRIPT_NAME} ${D}${sysconfdir}/init.d/${INITSCRIPT_NAME} +} + +# Remove /etc/network/if-up.d/wpa-supplicant link, if it is there +# It is no longer needed (nor wanted) +# This can be removed once OpenVix moves beyond OE 5.5.1 to a version +# that requires a reflash, as the file will no longer be there. +# +pkg_postinst:${PN}() { +#!/bin/sh +# +# Remove this iff still there. +rm -f "$D/etc/network/if-up.d/ntpdate-sync" } diff --git a/meta-oe/recipes-distros/openvix/image/ntpd-sync/ntp-setdate b/meta-oe/recipes-distros/openvix/image/ntpd-sync/ntp-setdate new file mode 100755 index 0000000000..0c7d073899 --- /dev/null +++ b/meta-oe/recipes-distros/openvix/image/ntpd-sync/ntp-setdate @@ -0,0 +1,26 @@ +#!/bin/sh +# + +### BEGIN INIT INFO +# Provides: ntp-setdate +# Required-Start: udev +# Required-Stop: +# Default-Start: 3 +# Default-Stop: +# Short-Description: Run script to set clock from NTP +### END INIT INFO + +# Add this as /etc/rc3.d/S95ntp-setdate +# It must follow the network start-up script (but that is now +# done by udev on rcS.d) +# This is to get a valid system time ASAP, but we also need to allow +# for Wifi to stabilize, hence the 95. +# + +case "$1" in +start) + /usr/bin/ntpdate-sync + ;; +esac + +exit 0 diff --git a/meta-oe/recipes-distros/openvix/image/ntpd-sync/ntpdate-sync b/meta-oe/recipes-distros/openvix/image/ntpd-sync/ntpdate-sync old mode 100644 new mode 100755 index 504b336548..4f6b6b6d1e --- a/meta-oe/recipes-distros/openvix/image/ntpd-sync/ntpdate-sync +++ b/meta-oe/recipes-distros/openvix/image/ntpd-sync/ntpdate-sync @@ -1,95 +1,66 @@ #!/bin/sh # Interlude script to set the time using time servers. # -Logfile=/tmp/ntpdatex_log -Logit=$Logfile.$$ -exec 3>&1 4>&2 -trap 'exec 2>&4 1>&3' 0 1 2 3 -exec 1>$Logit 2>&1 -set -x -# Everything below will go to the file 'log.out': -echo "0000 =====ntpdate-sync this is newlog4 =====" PATH=/sbin:/bin:/usr/sbin:/usr/bin # If this isn't present then we are doomed... # [ -x /usr/sbin/ntpd ] || exit 1 -# -# code originaly originally via if-up.d script but moved due to lock issues +# We don't want more than one process running this at a time, so +# implement a simple mutex. # -#echo "000 =====ntpdate-sync METHOD: $METHOD IFACE:$IFACE =====" -#[ "$IFACE" != "lo" ] || exit 0 -# [ "$METHOD" != "loopback" ] || exit 0 +LOCKFILE=/var/lock/ntpdate +TFILE=$LOCKFILE.$$ +touch $TFILE +ln $TFILE $LOCKFILE # Try to make hard-link (atomic) +status=$? # Remember the result +rm -f $TFILE # Remove the per-process one -# At boot time we'll check whether the external Internet is reachable. +# If we failed to link it then it already exists. So we exit. +# Someone else got here first. # -check_online() { - count=0 - while [ $count -lt 5 ]; do - sleep 0.5 -# No point in pinging by-name to test whether the network is up as that -# requires the network to be up to resolve the name!! -# So instead ping a known DNS server address. -# Possible ipv4/ipv6 choices (May 2022) are: -# Google: -# 8.8.8.8 or 2001:4860:4860::8888 -# Quad9 -# 9.9.9.9 or 2620:fe::fe:9 -# Cloudfare -# 1.1.1.1 or 2606:4700:4700::1111 +[ $status -ne 0 ] && exit 0 + +# Ensure we'll remove the lock file when we exit # -# Only send 1 packet (-c) and only wait for 3s (-W) - if ping -4 -c 1 -W 3 8.8.8.8 >/dev/null 2>&1 || - ping -6 -c 1 -W 3 2001:4860:4860::8888 >/dev/null 2>&1; then - return 0 - fi - count=$((count+1)) - done - exit 0 +free_lock() { + rm -f $LOCKFILE } +trap free_lock 0 +# This can be run at boot time from rc3.d (no arg), or from enigma2 (arg) +# Either way, we will set a 5s timeout for the attempt, which will catch +# any name-resolution or network access failures in one go. +# # Find out which NTP server to use.... # Historically this file could set NTPV4 to an NTP server, # -echo "0 =====ntpdate-sync from /usr/bin at `date` =====" [ -f /var/tmp/ntpv4.local ] && . /var/tmp/ntpv4.local -if [ -n "$NTPV4" ]; then # ntpv4.local setting + +# If we were called with a parameter, use it. +# We can set enigma2 to send its config.misc.NTPserver +# +if [ -n "$1" ]; then + NTPSERVER="$1" +elif [ -n "$NTPV4" ]; then # ntpv4.local setting NTPSERVER="$NTPV4" - echo "0a =====ntpdate-sync at `date` NTPSERVER: $NTPSERVER =====" -else # Any enigma2 setting? +else # Any enigma2 setting? NTPSERVER="`awk -F= '$1 == "config.misc.NTPserver" {print $2; exit;}' /etc/enigma2/settings`" - echo "0b =====ntpdate-sync at `date` NTPSERVER: $NTPSERVER =====" - if [ -z "$NTPSERVER" ]; then - NTPSERVER="pool.ntp.org" # The default.... - echo "0c =====ntpdate-sync at `date` NTPSERVER: $NTPSERVER =====" - fi fi -echo "1 =====ntpdate-sync at `date` NTPSERVER: $NTPSERVER =====" -# Interfaces are usually brought up during boot, but then we need to -# check that the Internet is reachable as well. +[ -z "$NTPSERVER" ] && NTPSERVER="pool.ntp.org" # The fall-back.... + +# Now do the work - allow 5 seconds, kill after 7 seconds, if still there. # -DELAY="" -if [ "$0" = "/etc/network/if-up.d/ntpdate-sync" ]; then - DELAY="check_online" +if timeout -k 7 5 /usr/sbin/ntpd -nq -p $NTPSERVER 2>/dev/null; then + if [ "$UPDATE_HWCLOCK" = "yes" ] && [ -x /sbin/stb-hwclock ]; then + /sbin/stb-hwclock --systohc + fi fi -( - $DELAY # No-op if empty.... - if /usr/sbin/ntpd -nq -p $NTPSERVER 2>/dev/null; then - echo "2a =====ntpdate-sync at UPDATE_HWCLOCK: $UPDATE_HWCLOCK =====" - if [ "$UPDATE_HWCLOCK" = "yes" ] && [ -x /sbin/stb-hwclock ]; then - echo "2b =====ntpdate-sync at UPDATE_HWCLOCK: $UPDATE_HWCLOCK =====" - /sbin/stb-hwclock --systohc - fi - fi - echo "3 =====ntpdate-sync at `date` =====" - exit 0 -) -echo "final exit ===== Done at `date` =====" # wait for all subprocesses to finish -# this is required when using systemd service as ntpd will start before ntpdate finishes -# and results in a bind error (port 123) +# This is required when using systemd service as ntpd will start before +# ntpdate finishes and results in a bind error (port 123) # wait