Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move ntpdate-sync from if-up.d to init.d script and refactor ntpdate-sync. #917

Merged
merged 1 commit into from
Feb 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions meta-oe/recipes-distros/openvix/image/ntpd-sync.bb
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
26 changes: 26 additions & 0 deletions meta-oe/recipes-distros/openvix/image/ntpd-sync/ntp-setdate
Original file line number Diff line number Diff line change
@@ -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
103 changes: 37 additions & 66 deletions meta-oe/recipes-distros/openvix/image/ntpd-sync/ntpdate-sync
100644 → 100755
Original file line number Diff line number Diff line change
@@ -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